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

sqlparser: Add support for TIMESTAMPADD #13314

Merged
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
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now only has TIMESTAMPDIFF left, so make it specialized for that. It might be something we want to treat as an interval or not in the future, but not sure yet so leaving it here for now. Did change the parsing here as well though to match TIMESTAMPADD for the interval types it supports.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IntervalDateExprTimestampadd => IntervalDateExprTimestampAdd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually deliberate to match how MySQL names the functions. It doesn't use a _ separator in TIMESTAMPADD. We do the same for ADDDATE vs. DATE_ADD for example.

)
Loading