Skip to content
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
4,121 changes: 2,002 additions & 2,119 deletions pkg/sql/parsers/dialect/mysql/mysql_sql.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pkg/sql/parsers/dialect/mysql/mysql_sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -12030,7 +12030,6 @@ name_confict:
| DEDUP
| HOUR
| IF
| INTERVAL
| FORMAT
| LEFT
| MICROSECOND
Expand Down
26 changes: 13 additions & 13 deletions pkg/sql/parsers/dialect/mysql/mysql_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ var (
output: "select day_key, day_date, day, month, quarter, year, week, day_of_week from bi_date where 1 = 2",
}, {
input: "select sum(a) over(partition by a range between interval 1 day preceding and interval 2 day following) from t1",
output: "select sum(a) over (partition by a range between interval(1, day) preceding and interval(2, day) following) from t1",
output: "select sum(a) over (partition by a range between INTERVAL 1 day preceding and INTERVAL 2 day following) from t1",
}, {
input: "select rank() over(partition by a range between 1 preceding and current row) from t1",
output: "select rank() over (partition by a range between 1 preceding and current row) from t1",
Expand Down Expand Up @@ -730,13 +730,13 @@ var (
output: "select FROM_UNIXTIME(2147483647) as c1, FROM_UNIXTIME(2147483648) as c2, FROM_UNIXTIME(2147483647.9999999) as c3, FROM_UNIXTIME(32536771199) as c4, FROM_UNIXTIME(32536771199.9999999) as c5",
}, {
input: "select date_add(\"1997-12-31 23:59:59\",INTERVAL -100000 YEAR);",
output: "select date_add(1997-12-31 23:59:59, INTERVAL(-100000, year))",
output: "select date_add(1997-12-31 23:59:59, INTERVAL -100000 year)",
}, {
input: "SELECT ADDDATE(DATE'2021-01-01', INTERVAL 1 DAY);",
output: "select ADDDATE(DATE(2021-01-01), INTERVAL(1, day))",
output: "select ADDDATE(DATE(2021-01-01), INTERVAL 1 day)",
}, {
input: "select '2007-01-01' + interval a day from t1;",
output: "select 2007-01-01 + interval(a, day) from t1",
output: "select 2007-01-01 + INTERVAL a day from t1",
}, {
input: "SELECT CAST(COALESCE(t0.c0, -1) AS UNSIGNED) IS TRUE FROM t0;",
output: "select cast(COALESCE(t0.c0, -1) as unsigned) is true from t0",
Expand Down Expand Up @@ -797,13 +797,13 @@ var (
output: "select sum(all a), count(all a), avg(all a), std(all a), variance(all a), bit_or(all a), bit_and(all a), min(all a), max(all a), min(all c), max(all c) from t",
}, {
input: "insert into t1 values (date_add(NULL, INTERVAL 1 DAY));",
output: "insert into t1 values (date_add(null, INTERVAL(1, day)))",
output: "insert into t1 values (date_add(null, INTERVAL 1 day))",
}, {
input: "replace into t1 values (date_add(NULL, INTERVAL 1 DAY));",
output: "replace into t1 values (date_add(null, INTERVAL(1, day)))",
output: "replace into t1 values (date_add(null, INTERVAL 1 day))",
}, {
input: "SELECT DATE_ADD('2022-02-28 23:59:59.9999', INTERVAL 1 SECOND) '1 second later';",
output: "select DATE_ADD(2022-02-28 23:59:59.9999, INTERVAL(1, second)) as 1 second later",
output: "select DATE_ADD(2022-02-28 23:59:59.9999, INTERVAL 1 second) as 1 second later",
}, {
input: "SELECT sum(a) as 'hello' from t1;",
output: "select sum(a) as hello from t1",
Expand All @@ -812,7 +812,7 @@ var (
output: "select stream from t1",
}, {
input: "SELECT DATE_ADD(\"2017-06-15\", INTERVAL -10 MONTH);",
output: "select DATE_ADD(2017-06-15, INTERVAL(-10, month))",
output: "select DATE_ADD(2017-06-15, INTERVAL -10 month)",
}, {
input: "create table t1 (a varchar)",
output: "create table t1 (a varchar)",
Expand All @@ -827,13 +827,13 @@ var (
output: "select cast(19999999999999999999 as signed)",
}, {
input: "select date_sub(now(), interval 1 day) from t1;",
output: "select date_sub(now(), interval(1, day)) from t1",
output: "select date_sub(now(), INTERVAL 1 day) from t1",
}, {
input: "select date_sub(now(), interval '1' day) from t1;",
output: "select date_sub(now(), interval(1, day)) from t1",
output: "select date_sub(now(), INTERVAL 1 day) from t1",
}, {
input: "select date_add(now(), interval '1' day) from t1;",
output: "select date_add(now(), interval(1, day)) from t1",
output: "select date_add(now(), INTERVAL 1 day) from t1",
}, {
input: "SELECT md.datname as `Database` FROM TT md",
output: "select md.datname as Database from tt as md",
Expand Down Expand Up @@ -879,7 +879,7 @@ var (
output: "select extract(year, l_shipdate) as l_year from t",
}, {
input: "select * from R join S on R.uid = S.uid where l_shipdate <= date '1998-12-01' - interval '112' day",
output: "select * from r inner join s on R.uid = S.uid where l_shipdate <= date(1998-12-01) - interval(112, day)",
output: "select * from r inner join s on R.uid = S.uid where l_shipdate <= date(1998-12-01) - INTERVAL 112 day",
}, {
input: "create table deci_table (a decimal(10, 5))",
}, {
Expand Down Expand Up @@ -1303,7 +1303,7 @@ var (
input: "select sum(distinct s) from tbl where 1",
}, {
input: "select u.a, interval 1 second from t",
output: "select u.a, interval(1, second) from t",
output: "select u.a, INTERVAL 1 second from t",
}, {
input: "select u.a, (select t.a from sa.t, u) from t where (u.a, u.b, u.c) in (select * from t)",
output: "select u.a, (select t.a from sa.t cross join u) from t where (u.a, u.b, u.c) in (select * from t)",
Expand Down
19 changes: 18 additions & 1 deletion pkg/sql/parsers/tree/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,25 @@ type FuncExpr struct {
}

func (node *FuncExpr) Format(ctx *FmtCtx) {
funcName := ""
if node.FuncName != nil {
ctx.WriteString(node.FuncName.Origin())
funcName = node.FuncName.Origin()
}

if strings.ToLower(funcName) == "interval" && len(node.Exprs) == 2 {
ctx.WriteString("INTERVAL ")
node.Exprs[0].Format(ctx)
ctx.WriteByte(' ')
if nv, ok := node.Exprs[1].(*NumVal); ok {
ctx.WriteString(nv.String())
} else {
node.Exprs[1].Format(ctx)
}
return
}

if funcName != "" {
ctx.WriteString(funcName)
} else {
node.Func.Format(ctx)
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/plan/base_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2440,8 +2440,12 @@ func appendCastBeforeExpr(ctx context.Context, expr *Expr, toType Type, isBin ..
}

func resetDateFunctionArgs(ctx context.Context, dateExpr *Expr, intervalExpr *Expr) ([]*Expr, error) {
firstExpr := intervalExpr.GetList().List[0]
secondExpr := intervalExpr.GetList().List[1]
list := intervalExpr.GetList()
if list == nil || len(list.List) < 2 {
return nil, moerr.NewInvalidArg(ctx, "interval expression requires a value and a unit", intervalExpr)
}
firstExpr := list.List[0]
secondExpr := list.List[1]

// MySQL behavior: INTERVAL NULL SECOND is valid and returns NULL at execution time
// Only date_add(..., null) (without INTERVAL) should return syntax error
Expand Down Expand Up @@ -2651,8 +2655,12 @@ func resetIntervalFunction(ctx context.Context, intervalExpr *Expr) ([]*Expr, er
}

func resetIntervalFunctionArgs(ctx context.Context, intervalExpr *Expr) ([]*Expr, error) {
firstExpr := intervalExpr.GetList().List[0]
secondExpr := intervalExpr.GetList().List[1]
list := intervalExpr.GetList()
if list == nil || len(list.List) < 2 {
return nil, moerr.NewInvalidArg(ctx, "interval expression requires a value and a unit", intervalExpr)
}
firstExpr := list.List[0]
secondExpr := list.List[1]

// MySQL behavior: INTERVAL NULL SECOND is valid and returns NULL at execution time
// NULL values will be handled at execution time (null1 || null2 check)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/plan/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ func TestSingleTableSQLBuilder(t *testing.T) {
"select 1 as c, 1/2, abs(-2)",

"select date('2022-01-01'), adddate(time'00:00:00', interval 1 day), subdate(time'00:00:00', interval 1 week), '2007-01-01' + interval 1 month, '2007-01-01' - interval 1 hour",
"SELECT '2024-01-01' + INTERVAL n_nationkey DAY FROM nation",
"SELECT '2024-01-01' - INTERVAL n_nationkey HOUR FROM nation",
"SELECT '2024-01-01' + INTERVAL n_nationkey % 365 DAY FROM nation",
"SELECT '2024-01-01' + INTERVAL (n_nationkey % 365) DAY FROM nation",
"select 2222332222222223333333333333333333, 0x616263,-10, bit_and(2), bit_or(2), 'aaa' like '%a',str_to_date('04/31/2004', '%m/%d/%Y'),unix_timestamp(from_unixtime(2147483647))",
"select max(n_nationkey) over (partition by N_REGIONKEY) from nation",
"select * from generate_series(1, 5) g",
Expand Down
4 changes: 2 additions & 2 deletions test/distributed/cases/ddl/create_table_as_select.result
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ drop table if exists time04;
create table time04 as select date_add(col2, interval 45 day), date_sub(col2, interval 5 day) from time01;
show create table time04;
Table Create Table
time04 CREATE TABLE `time04` (\n `date_add(col2, interval(45, day))` datetime DEFAULT NULL,\n `date_sub(col2, interval(5, day))` datetime DEFAULT NULL\n)
time04 CREATE TABLE `time04` (\n `date_add(col2, interval 45 day)` datetime DEFAULT NULL,\n `date_sub(col2, interval 5 day)` datetime DEFAULT NULL\n)
select * from time04;
date_add(col2, interval(45, day)) date_sub(col2, interval(5, day))
date_add(col2, interval 45 day) date_sub(col2, interval 5 day)
2023-12-26 10:00:01 2023-11-06 10:00:01
1990-11-24 01:01:01 1990-10-05 01:01:01
null null
Expand Down
4 changes: 2 additions & 2 deletions test/distributed/cases/dtype/TimeToInt.result
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ cast(date(col2) as int)
19464
null
select cast(date_add(col2,interval 45 day) as int) from test14;
cast(date_add(col2, interval(45, day)) as int)
cast(date_add(col2, interval 45 day) as int)
1582806059
1685581305
null
select cast(date_sub(col2,interval 45 day) as int) from test14;
cast(date_sub(col2, interval(45, day)) as int)
cast(date_sub(col2, interval 45 day) as int)
1575030059
1677805305
null
Expand Down
6 changes: 3 additions & 3 deletions test/distributed/cases/dtype/boundary_comprehensive.result
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,13 @@ INSERT INTO t_date_arith VALUES (1, '9999-12-31');
INSERT INTO t_date_arith VALUES (2, '1000-01-01');
INSERT INTO t_date_arith VALUES (3, '2024-02-29');
SELECT dt, DATE_ADD(dt, INTERVAL 1 DAY) FROM t_date_arith WHERE id = 1;
➤ dt[91,64,0] ¦ DATE_ADD(dt, INTERVAL(1, day))[91,64,0] 𝄀
➤ dt[91,64,0] ¦ DATE_ADD(dt, INTERVAL 1 day)[91,64,0] 𝄀
9999-12-31 ¦ null
SELECT dt, DATE_SUB(dt, INTERVAL 1 DAY) FROM t_date_arith WHERE id = 2;
➤ dt[91,64,0] ¦ DATE_SUB(dt, INTERVAL(1, day))[91,64,0] 𝄀
➤ dt[91,64,0] ¦ DATE_SUB(dt, INTERVAL 1 day)[91,64,0] 𝄀
1000-01-01 ¦ 0999-12-31
SELECT dt, DATE_ADD(dt, INTERVAL 1 MONTH) FROM t_date_arith WHERE id = 3;
➤ dt[91,64,0] ¦ DATE_ADD(dt, INTERVAL(1, month))[91,64,0] 𝄀
➤ dt[91,64,0] ¦ DATE_ADD(dt, INTERVAL 1 month)[91,64,0] 𝄀
2024-02-29 ¦ 2024-03-29
DROP TABLE t_date_arith;
DROP TABLE IF EXISTS t_diff;
Expand Down
8 changes: 4 additions & 4 deletions test/distributed/cases/dtype/date.result
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ null
select cast(cast(0x13488c5 as signed) as date);
invalid argument operator cast, bad value [BIGINT DATE]
SELECT DATE_ADD('2017-06-15', INTERVAL 10 DAY);
➤ DATE_ADD(2017-06-15, INTERVAL(10, day))[12,-1,0] 𝄀
➤ DATE_ADD(2017-06-15, INTERVAL 10 day)[12,-1,0] 𝄀
2017-06-25
SELECT DATE_ADD('2017-06-15', INTERVAL 10 MONTH);
➤ DATE_ADD(2017-06-15, INTERVAL(10, month))[12,-1,0] 𝄀
➤ DATE_ADD(2017-06-15, INTERVAL 10 month)[12,-1,0] 𝄀
2018-04-15
SELECT DATE_ADD('2017-06-15', INTERVAL 1 YEAR);
➤ DATE_ADD(2017-06-15, INTERVAL(1, year))[12,-1,0] 𝄀
➤ DATE_ADD(2017-06-15, INTERVAL 1 year)[12,-1,0] 𝄀
2018-06-15
SELECT DATE_ADD('20200215', INTERVAL 14 DAY);
➤ DATE_ADD(20200215, INTERVAL(14, day))[12,-1,0] 𝄀
➤ DATE_ADD(20200215, INTERVAL 14 day)[12,-1,0] 𝄀
2020-02-29
drop table if exists t1;
create table t1 (a date not null, primary key(a));
Expand Down
12 changes: 6 additions & 6 deletions test/distributed/cases/dtype/time.result
Original file line number Diff line number Diff line change
Expand Up @@ -119,24 +119,24 @@ select cast('123.445' as time(6));
cast(123.445 as time(26, 6))
00:01:23.445000000
select adddate(time'00:00:00', interval 1 hour);
adddate(time(00:00:00), interval(1, hour))
adddate(time(00:00:00), interval 1 hour)
01:00:00
select adddate(time'00:00:00', interval 1 minute);
adddate(time(00:00:00), interval(1, minute))
adddate(time(00:00:00), interval 1 minute)
00:01:00
select adddate(time'00:00:00', interval 1 second);
adddate(time(00:00:00), interval(1, second))
adddate(time(00:00:00), interval 1 second)
00:00:01
select adddate(time'800:00:00', interval 1 hour);
adddate(time(800:00:00), interval(1, hour))
adddate(time(800:00:00), interval 1 hour)
801:00:00
select adddate(time'2562047787:00:00', interval 1 hour);
Data truncation: data out of range: data type time,
select adddate(time'100:00:00', interval -100 minute);
adddate(time(100:00:00), interval(-100, minute))
adddate(time(100:00:00), interval -100 minute)
98:20:00
select adddate(time'100:00:00', interval -1000 second);
adddate(time(100:00:00), interval(-1000, second))
adddate(time(100:00:00), interval -1000 second)
99:43:20
select cast(time("11:22:33") as BIGINT);
cast(time(11:22:33) as bigint)
Expand Down
Loading
Loading