From 2159bfd82445d30d86d8d937be3c1f4167b9a413 Mon Sep 17 00:00:00 2001 From: ou yuanning <45346669+ouyuanning@users.noreply.github.com> Date: Mon, 6 Jun 2022 20:29:17 +0800 Subject: [PATCH] improve date_add/sub function building (#2808) --- pkg/sql/plan2/build_function.go | 8 ++++++++ pkg/sql/plan2/build_test.go | 1 + 2 files changed, 9 insertions(+) diff --git a/pkg/sql/plan2/build_function.go b/pkg/sql/plan2/build_function.go index e90c70f18..c3beb7a19 100644 --- a/pkg/sql/plan2/build_function.go +++ b/pkg/sql/plan2/build_function.go @@ -171,6 +171,14 @@ func getFunctionExprByNameAndAstExprs(name string, astExprs []tree.Expr, ctx Com if len(args) != 2 { return nil, false, errors.New(errno.SyntaxErrororAccessRuleViolation, "date_add/date_sub function need two args") } + if args[0].Typ.Id == plan.Type_VARCHAR { + args[0], err = appendCastExpr(args[0], &plan.Type{ + Id: plan.Type_DATE, + }) + if err != nil { + return + } + } args, err = resetIntervalFunctionExprs(args[0], args[1]) if err != nil { return diff --git a/pkg/sql/plan2/build_test.go b/pkg/sql/plan2/build_test.go index 326884f5e..6e0d20a5e 100644 --- a/pkg/sql/plan2/build_test.go +++ b/pkg/sql/plan2/build_test.go @@ -403,6 +403,7 @@ func TestSingleTableSqlBuilder(t *testing.T) { "select sum(n_nationkey) as s from nation order by s", "select date_add(date '2001-01-01', interval 1 day) as a", "select date_sub(date '2001-01-01', interval '1 day') as a", + "select date_add('2001-01-01', interval '1 day') as a", "SELECT N_REGIONKEY + 2 as a, N_REGIONKEY/2, N_REGIONKEY* N_NATIONKEY, N_REGIONKEY % N_NATIONKEY, N_REGIONKEY - N_NATIONKEY FROM NATION WHERE -N_NATIONKEY < -20", //test more expr "SELECT N_REGIONKEY FROM NATION where N_REGIONKEY >= N_NATIONKEY or (N_NAME like '%ddd' and N_REGIONKEY >0.5)", //test more expr -- GitLab