Skip to content

Commit

Permalink
Fix: Date math with Interval keyword (#12082)
Browse files Browse the repository at this point in the history
* feat: add failing parsing test and fix parser

Signed-off-by: Manan Gupta <manan@planetscale.com>

* test: add e2e tests for interval with math functions

Signed-off-by: Manan Gupta <manan@planetscale.com>

* test: explictly set the time-zone to prevent failures in CI

Signed-off-by: Manan Gupta <manan@planetscale.com>

Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 authored Jan 16, 2023
1 parent 976dfed commit 0bdc9b8
Show file tree
Hide file tree
Showing 4 changed files with 908 additions and 890 deletions.
12 changes: 12 additions & 0 deletions go/test/endtoend/vtgate/queries/misc/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,15 @@ func TestQueryTimeoutWithTables(t *testing.T) {
assert.Contains(t, err.Error(), "context deadline exceeded")
assert.Contains(t, err.Error(), "(errno 1317) (sqlstate 70100)")
}

// TestIntervalWithMathFunctions tests that the Interval keyword can be used with math functions.
func TestIntervalWithMathFunctions(t *testing.T) {
mcmp, closer := start(t)
defer closer()

// Set the time zone explicitly to UTC, otherwise the output of FROM_UNIXTIME is going to be dependent
// on the time zone of the system.
mcmp.Exec("SET time_zone = '+00:00'")
mcmp.AssertMatches("select '2020-01-01' + interval month(DATE_SUB(FROM_UNIXTIME(1234), interval 1 month))-1 month", `[[CHAR("2020-12-01")]]`)
mcmp.AssertMatches("select DATE_ADD(MIN(FROM_UNIXTIME(1673444922)),interval -DAYOFWEEK(MIN(FROM_UNIXTIME(1673444922)))+1 DAY)", `[[DATETIME("2023-01-08 13:48:42")]]`)
}
6 changes: 6 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,12 @@ var (
}, {
input: "select /* TIMESTAMPDIFF */ TIMESTAMPDIFF(MINUTE, '2008-01-02', '2008-01-04') from t",
output: "select /* TIMESTAMPDIFF */ timestampdiff(MINUTE, '2008-01-02', '2008-01-04') from t",
}, {
input: "select DATE_ADD(MIN(FROM_UNIXTIME(1673444922)),interval -DAYOFWEEK(MIN(FROM_UNIXTIME(1673444922)))+1 DAY)",
output: "select DATE_ADD(min(FROM_UNIXTIME(1673444922)), interval (-DAYOFWEEK(min(FROM_UNIXTIME(1673444922))) + 1) DAY) from dual",
}, {
input: "select '2020-01-01' + interval month(DATE_SUB(FROM_UNIXTIME(1234), interval 1 month))-1 month",
output: "select '2020-01-01' + interval (month(DATE_SUB(FROM_UNIXTIME(1234), interval 1 month)) - 1) month from dual",
}, {
input: "select /* dual */ 1 from dual",
}, {
Expand Down
Loading

0 comments on commit 0bdc9b8

Please sign in to comment.