Skip to content

Commit

Permalink
sql: fix and enhance expression normalization.
Browse files Browse the repository at this point in the history
This patch is needed so that the expressions in the Sqllogictest do
not cause the server to panic.

Summary:

- Bug fixes:

  - an invalid cast was attempted for some expressions; is now fixed.
  - NULL caused invalid transformations, NULL is now normalized on a
    fast path.
  - transformations that rebalance arithmetic across a comparison now
    first check that the proposed replacement arithmetic expression
    has a valid overload w.r.t typing.

- Improvements: more simplifications.
  • Loading branch information
knz committed Jul 8, 2016
1 parent 7e2eb5b commit 6913ca1
Show file tree
Hide file tree
Showing 5 changed files with 507 additions and 96 deletions.
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 @@ -460,6 +460,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 @@ -496,6 +511,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 @@ -971,6 +991,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 @@ -1017,6 +1056,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

0 comments on commit 6913ca1

Please sign in to comment.