Skip to content

Commit

Permalink
[release-14.0] Fix: Date math with Interval keyword (#12082) (#12103)
Browse files Browse the repository at this point in the history
* Fix: Date math with Interval keyword (#12082)

* feat: add failing parsing test and fix parser

Signed-off-by: Manan Gupta <[email protected]>

* test: add e2e tests for interval with math functions

Signed-off-by: Manan Gupta <[email protected]>

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

Signed-off-by: Manan Gupta <[email protected]>

Signed-off-by: Manan Gupta <[email protected]>

* test: fix expected output

Signed-off-by: Manan Gupta <[email protected]>

Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 authored Jan 18, 2023
1 parent 15b01c3 commit 9a5f084
Show file tree
Hide file tree
Showing 4 changed files with 724 additions and 703 deletions.
15 changes: 15 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,18 @@ func TestJoinWithMergedRouteWithPredicate(t *testing.T) {

utils.AssertMatches(t, conn, "select t3.id7, t2.id3, t3.id6 from t1 join t3 on t1.id2 = t3.id5 join t2 on t3.id6 = t2.id3 where t1.id2 = 13", `[[INT64(8) INT64(5) INT64(5)]]`)
}

// TestIntervalWithMathFunctions tests that the Interval keyword can be used with math functions.
func TestIntervalWithMathFunctions(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

// 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.
utils.Exec(t, conn, "SET time_zone = '+00:00'")
utils.AssertMatches(t, conn, "select '2020-01-01' + interval month(DATE_SUB(FROM_UNIXTIME(1234), interval 1 month))-1 month", `[[CHAR("2020-12-01")]]`)
utils.AssertMatches(t, conn, "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 @@ -830,6 +830,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 9a5f084

Please sign in to comment.