This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
sql/(analyzer,parse,expression): implement case expression #576
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package expression | ||
|
||
import ( | ||
"bytes" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
) | ||
|
||
// CaseBranch is a single branch of a case expression. | ||
type CaseBranch struct { | ||
Cond sql.Expression | ||
Value sql.Expression | ||
} | ||
|
||
// Case is an expression that returns the value of one of its branches when a | ||
// condition is met. | ||
type Case struct { | ||
Expr sql.Expression | ||
Branches []CaseBranch | ||
Else sql.Expression | ||
} | ||
|
||
// NewCase returns an new Case expression. | ||
func NewCase(expr sql.Expression, branches []CaseBranch, elseExpr sql.Expression) *Case { | ||
return &Case{expr, branches, elseExpr} | ||
} | ||
|
||
// Type implements the sql.Expression interface. | ||
func (c *Case) Type() sql.Type { | ||
for _, b := range c.Branches { | ||
return b.Value.Type() | ||
} | ||
return c.Else.Type() | ||
} | ||
|
||
// IsNullable implements the sql.Expression interface. | ||
func (c *Case) IsNullable() bool { | ||
for _, b := range c.Branches { | ||
if b.Value.IsNullable() { | ||
return true | ||
} | ||
} | ||
|
||
return c.Else == nil || c.Else.IsNullable() | ||
} | ||
|
||
// Resolved implements the sql.Expression interface. | ||
func (c *Case) Resolved() bool { | ||
if (c.Expr != nil && !c.Expr.Resolved()) || | ||
(c.Else != nil && !c.Else.Resolved()) { | ||
return false | ||
} | ||
|
||
for _, b := range c.Branches { | ||
if !b.Cond.Resolved() || !b.Value.Resolved() { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Children implements the sql.Expression interface. | ||
func (c *Case) Children() []sql.Expression { | ||
var children []sql.Expression | ||
|
||
if c.Expr != nil { | ||
children = append(children, c.Expr) | ||
} | ||
|
||
for _, b := range c.Branches { | ||
children = append(children, b.Cond, b.Value) | ||
} | ||
|
||
if c.Else != nil { | ||
children = append(children, c.Else) | ||
} | ||
|
||
return children | ||
} | ||
|
||
// Eval implements the sql.Expression interface. | ||
func (c *Case) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
span, ctx := ctx.Span("expression.Case") | ||
defer span.Finish() | ||
|
||
var expr interface{} | ||
var err error | ||
if c.Expr != nil { | ||
expr, err = c.Expr.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
for _, b := range c.Branches { | ||
var cond sql.Expression | ||
if expr != nil { | ||
cond = NewEquals(NewLiteral(expr, c.Expr.Type()), b.Cond) | ||
} else { | ||
cond = b.Cond | ||
} | ||
|
||
v, err := cond.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
v, err = sql.Boolean.Convert(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if v == true { | ||
return b.Value.Eval(ctx, row) | ||
} | ||
} | ||
|
||
if c.Else != nil { | ||
return c.Else.Eval(ctx, row) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
// TransformUp implements the sql.Expression interface. | ||
func (c *Case) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { | ||
var expr sql.Expression | ||
var err error | ||
|
||
if c.Expr != nil { | ||
expr, err = c.Expr.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
var branches []CaseBranch | ||
for _, b := range c.Branches { | ||
var nb CaseBranch | ||
|
||
nb.Cond, err = b.Cond.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nb.Value, err = b.Value.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
branches = append(branches, nb) | ||
} | ||
|
||
var elseExpr sql.Expression | ||
if c.Else != nil { | ||
elseExpr, err = c.Else.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return f(NewCase(expr, branches, elseExpr)) | ||
} | ||
|
||
func (c *Case) String() string { | ||
var buf bytes.Buffer | ||
|
||
buf.WriteString("CASE ") | ||
if c.Expr != nil { | ||
buf.WriteString(c.Expr.String()) | ||
} | ||
|
||
for _, b := range c.Branches { | ||
buf.WriteString(" WHEN ") | ||
buf.WriteString(b.Cond.String()) | ||
buf.WriteString(" THEN ") | ||
buf.WriteString(b.Value.String()) | ||
} | ||
|
||
if c.Else != nil { | ||
buf.WriteString(" ELSE ") | ||
buf.WriteString(c.Else.String()) | ||
} | ||
|
||
buf.WriteString(" END") | ||
return buf.String() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just open question - doesn't make sense as a optimization to squash some rules?
For instance instead of 3 times looping, we may have one loop with some cases - loud thinking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this query phase is really fast compared with the rest. The idea of having separated rules is the synergy that we can obtain executing them again and again, interacting with other rules.