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 Jun 28, 2016
1 parent c035df2 commit 662245b
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 61 deletions.
27 changes: 27 additions & 0 deletions sql/parser/col_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,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 {
switch d.(type) {
case *DInt:
return &IntColType{"INT", 0}
case *DFloat:
return &FloatColType{"FLOAT", 0}
case *DDecimal:
return &DecimalColType{"DECIMAL", 0, 0}
case *DTimestamp:
return &TimestampColType{}
case *DTimestampTZ:
return &TimestampTZColType{}
case *DInterval:
return &IntervalColType{}
case *DDate:
return &DateColType{}
case *DString:
return &StringColType{"STRING", 0}
case *DBytes:
return &BytesColType{"BYTES"}
}
panic("unknown type")
}
23 changes: 23 additions & 0 deletions sql/parser/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,24 @@ func (node *BinaryExpr) memoizeFn() {
node.fn = fn
}

// newBinExprIfValidOverloads constructs a new BinaryExpr if and only
// if the pair of arguments have a valid implementation.
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 @@ -1013,6 +1031,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 662245b

Please sign in to comment.