Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix and enhance expression normalization. #7512

Merged
merged 1 commit into from
Jul 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions sql/parser/col_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package parser
import (
"bytes"
"fmt"

"github.com/pkg/errors"
)

// ColumnType represents a type in a column definition.
Expand Down Expand Up @@ -247,3 +249,30 @@ func (node *TimestampTZColType) String() string { return AsString(node) }
func (node *IntervalColType) String() string { return AsString(node) }
func (node *StringColType) String() string { return AsString(node) }
func (node *BytesColType) String() string { return AsString(node) }

// DatumTypeToColumnType produces a SQL column type equivalent to the
// given Datum type. Used to generate CastExpr nodes during
// normalization.
func DatumTypeToColumnType(d Datum) (ColumnType, error) {
switch d.(type) {
case *DInt:
return intColTypeInt, nil
case *DFloat:
return floatColTypeFloat, nil
case *DDecimal:
return decimalColTypeDecimal, nil
case *DTimestamp:
return timestampColTypeTimestamp, nil
case *DTimestampTZ:
return timestampTzColTypeTimestampWithTZ, nil
case *DInterval:
return intervalColTypeInterval, nil
case *DDate:
return dateColTypeDate, nil
case *DString:
return stringColTypeString, nil
case *DBytes:
return bytesColTypeBytes, nil
}
return nil, errors.Errorf("internal error: unknown Datum type %T", d)
}
44 changes: 44 additions & 0 deletions sql/parser/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,21 @@ type IfExpr struct {
typeAnnotation
}

// TypedTrueExpr returns the IfExpr's True expression as a TypedExpr.
func (node *IfExpr) TypedTrueExpr() TypedExpr {
return node.True.(TypedExpr)
}

// TypedCondExpr returns the IfExpr's Cond expression as a TypedExpr.
func (node *IfExpr) TypedCondExpr() TypedExpr {
return node.Cond.(TypedExpr)
}

// TypedElseExpr returns the IfExpr's Else expression as a TypedExpr.
func (node *IfExpr) TypedElseExpr() TypedExpr {
return node.Else.(TypedExpr)
}

// Format implements the NodeFormatter interface.
func (node *IfExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString("IF(")
Expand Down Expand Up @@ -504,6 +519,11 @@ type CoalesceExpr struct {
typeAnnotation
}

// TypedExprAt returns the expression at the specified index as a TypedExpr.
func (node *CoalesceExpr) TypedExprAt(idx int) TypedExpr {
return node.Exprs[idx].(TypedExpr)
}

// Format implements the NodeFormatter interface.
func (node *CoalesceExpr) Format(buf *bytes.Buffer, f FmtFlags) {
buf.WriteString(node.Name)
Expand Down Expand Up @@ -979,6 +999,25 @@ func (node *BinaryExpr) memoizeFn() {
node.fn = fn
}

// newBinExprIfValidOverload constructs a new BinaryExpr if and only
// if the pair of arguments have a valid implementation for the given
// BinaryOperator.
func newBinExprIfValidOverload(op BinaryOperator, left TypedExpr, right TypedExpr) *BinaryExpr {
leftRet, rightRet := left.ReturnType(), right.ReturnType()
fn, ok := BinOps[op].lookupImpl(leftRet, rightRet)
if ok {
expr := &BinaryExpr{
Operator: op,
Left: left,
Right: right,
fn: fn,
}
expr.typ = fn.returnType()
return expr
}
return nil
}

// Format implements the NodeFormatter interface.
func (node *BinaryExpr) Format(buf *bytes.Buffer, f FmtFlags) {
binExprFmtWithParen(buf, f, node.Left, node.Operator.String(), node.Right)
Expand Down Expand Up @@ -1025,6 +1064,11 @@ func (node *UnaryExpr) Format(buf *bytes.Buffer, f FmtFlags) {
exprFmtWithParen(buf, f, node.Expr)
}

// TypedInnerExpr returns the UnaryExpr's inner expression as a TypedExpr.
func (node *UnaryExpr) TypedInnerExpr() TypedExpr {
return node.Expr.(TypedExpr)
}

// FuncExpr represents a function call.
type FuncExpr struct {
Name *QualifiedName
Expand Down
Loading