Skip to content

Commit

Permalink
sqlparser: Add support for TIMESTAMPADD (#13314)
Browse files Browse the repository at this point in the history
This changes the parsing support for `TIMESTAMPADD` so that it is
treated as a date interval function like for example `DATE_ADD`.

Doing this makes it also supported in an immediate way for the
`evalengine` since it functions as such an interval function.

Signed-off-by: Dirkjan Bussink <d.bussink@gmail.com>
  • Loading branch information
dbussink authored Jun 14, 2023
1 parent 51c495d commit 5ded6ec
Show file tree
Hide file tree
Showing 18 changed files with 8,692 additions and 8,481 deletions.
11 changes: 5 additions & 6 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2343,12 +2343,11 @@ type (
Expr Expr
}

// TimestampFuncExpr represents the function and arguments for TIMESTAMP{ADD,DIFF} functions.
TimestampFuncExpr struct {
Name string
// TimestampDiffExpr represents the function and arguments for TIMESTAMPDIFF functions.
TimestampDiffExpr struct {
Expr1 Expr
Expr2 Expr
Unit string
Unit IntervalType
}

// ExtractFuncExpr represents the function and arguments for EXTRACT(YEAR FROM '2019-07-02') type functions.
Expand Down Expand Up @@ -3140,7 +3139,7 @@ func (*UnaryExpr) iExpr() {}
func (*IntroducerExpr) iExpr() {}
func (*CollateExpr) iExpr() {}
func (*FuncExpr) iExpr() {}
func (*TimestampFuncExpr) iExpr() {}
func (*TimestampDiffExpr) iExpr() {}
func (*ExtractFuncExpr) iExpr() {}
func (*WeightStringFuncExpr) iExpr() {}
func (*CurTimeFuncExpr) iExpr() {}
Expand Down Expand Up @@ -3236,7 +3235,7 @@ func (*GeomFromGeoJSONExpr) iExpr() {}

// iCallable marks all expressions that represent function calls
func (*FuncExpr) iCallable() {}
func (*TimestampFuncExpr) iCallable() {}
func (*TimestampDiffExpr) iCallable() {}
func (*ExtractFuncExpr) iCallable() {}
func (*WeightStringFuncExpr) iCallable() {}
func (*CurTimeFuncExpr) iCallable() {}
Expand Down
16 changes: 8 additions & 8 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions go/vt/sqlparser/ast_copy_on_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,8 +1392,8 @@ func (node *IntroducerExpr) Format(buf *TrackedBuffer) {
}

// Format formats the node.
func (node *TimestampFuncExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "%#s(%#s, %v, %v)", node.Name, node.Unit, node.Expr1, node.Expr2)
func (node *TimestampDiffExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "timestampdiff(%#s, %v, %v)", node.Unit.ToString(), node.Expr1, node.Expr2)
}

// Format formats the node.
Expand Down Expand Up @@ -1475,6 +1475,8 @@ func (node *IntervalDateExpr) Format(buf *TrackedBuffer) {
buf.astPrintf(node, "interval %l %#s + %r", node.Interval, node.Unit.ToString(), node.Date)
case IntervalDateExprBinarySub:
buf.astPrintf(node, "%l - interval %r %#s", node.Date, node.Interval, node.Unit.ToString())
case IntervalDateExprTimestampadd:
buf.astPrintf(node, "timestampadd(%#s, %v, %v)", node.Unit.ToString(), node.Interval, node.Date)
}
}

Expand Down
15 changes: 11 additions & 4 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,7 @@ func getAliasedTableExprFromTableName(tblName TableName) *AliasedTableExpr {

func (node *IntervalDateExpr) IsSubtraction() bool {
switch node.Syntax {
case IntervalDateExprDateAdd, IntervalDateExprAdddate, IntervalDateExprBinaryAdd, IntervalDateExprBinaryAddLeft:
case IntervalDateExprDateAdd, IntervalDateExprAdddate, IntervalDateExprBinaryAdd, IntervalDateExprBinaryAddLeft, IntervalDateExprTimestampadd:
return false
case IntervalDateExprDateSub, IntervalDateExprSubdate, IntervalDateExprBinarySub:
return true
Expand Down Expand Up @@ -2391,6 +2391,8 @@ func (node *IntervalDateExpr) FnName() string {
return "adddate"
case IntervalDateExprSubdate:
return "subdate"
case IntervalDateExprTimestampadd:
return "timestampadd"
case IntervalDateExprBinaryAdd, IntervalDateExprBinaryAddLeft:
return "<arithmetic interval addition>"
case IntervalDateExprBinarySub:
Expand Down
18 changes: 9 additions & 9 deletions go/vt/sqlparser/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions go/vt/sqlparser/ast_visit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions go/vt/sqlparser/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,4 +1014,5 @@ const (
IntervalDateExprBinaryAdd
IntervalDateExprBinaryAddLeft
IntervalDateExprBinarySub
IntervalDateExprTimestampadd
)
Loading

0 comments on commit 5ded6ec

Please sign in to comment.