From c9cac8fc9f8a5d31e2a5f3fd6226c965a7008ace Mon Sep 17 00:00:00 2001 From: LiBinfeng Date: Wed, 22 Jan 2025 10:26:26 +0800 Subject: [PATCH] [fix](Nereids) fix fold constant of time acquired functions (#47288) Problem Summary: explain select substr(current_date, 1, 10); when logicalPlanBuilder build ast from original sql of date acquired functions like current_date, it would add an alias above. Which would stop folding constant when fold constant rule traversing expression tree So remove alias when translate to ast --- .../nereids/parser/LogicalPlanBuilder.java | 14 ++++---- .../fold_constant_date_arithmatic.groovy | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 72f427c5095727..a3d215441374f5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -2387,37 +2387,37 @@ public Expression visitArithmeticBinary(ArithmeticBinaryContext ctx) { @Override public Expression visitCurrentDate(DorisParser.CurrentDateContext ctx) { - return new CurrentDate().alias("CURRENT_DATE"); + return new CurrentDate(); } @Override public Expression visitCurrentTime(DorisParser.CurrentTimeContext ctx) { - return new CurrentTime().alias("CURRENT_TIME"); + return new CurrentTime(); } @Override public Expression visitCurrentTimestamp(DorisParser.CurrentTimestampContext ctx) { - return new Now().alias("CURRENT_TIMESTAMP"); + return new Now(); } @Override public Expression visitLocalTime(DorisParser.LocalTimeContext ctx) { - return new CurrentTime().alias("LOCALTIME"); + return new CurrentTime(); } @Override public Expression visitLocalTimestamp(DorisParser.LocalTimestampContext ctx) { - return new Now().alias("LOCALTIMESTAMP"); + return new Now(); } @Override public Expression visitCurrentUser(DorisParser.CurrentUserContext ctx) { - return new CurrentUser().alias("CURRENT_USER"); + return new CurrentUser(); } @Override public Expression visitSessionUser(DorisParser.SessionUserContext ctx) { - return new SessionUser().alias("SESSION_USER"); + return new SessionUser(); } @Override diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy new file mode 100644 index 00000000000000..5e0ed2e5d251ca --- /dev/null +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_date_arithmatic.groovy @@ -0,0 +1,32 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("fold_constant_date_arithmatic") { + def db = "fold_constant_date_arithmatic" + sql "create database if not exists ${db}" + + sql "set enable_nereids_planner=true" + sql "set enable_fallback_to_original_planner=false" + sql "set enable_fold_constant_by_be=false" + + testFoldConst("select substr(now(), 1, 10);") + testFoldConst("select substr(now(3), 1, 10);") + testFoldConst("select substr(curdate(), 1, 10);") + testFoldConst("select substr(current_date(), 1, 10);") + testFoldConst("select substr(current_timestamp(), 1, 10);") + testFoldConst("select substr(current_timestamp(3), 1, 10);") +}