diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java index 78fdd17a7a6453..2432661a5683ea 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Analyzer.java @@ -41,6 +41,7 @@ import org.apache.doris.nereids.rules.analysis.ReplaceExpressionByChildOutput; import org.apache.doris.nereids.rules.analysis.ResolveOrdinalInOrderByAndGroupBy; import org.apache.doris.nereids.rules.analysis.SubqueryToApply; +import org.apache.doris.nereids.rules.analysis.VariableToLiteral; import org.apache.doris.nereids.rules.rewrite.JoinCommute; import org.apache.doris.nereids.rules.rewrite.MergeProjects; @@ -150,6 +151,17 @@ private static List buildAnalyzeJobs(Optional c new NormalizeRepeat() ), bottomUp(new AdjustAggregateNullableForEmptySet()), + // consider sql with user defined var @t_zone + // set @t_zone='GMT'; + // SELECT + // DATE_FORMAT(convert_tz(dt, time_zone, @t_zone),'%Y-%m-%d') day + // FROM + // t + // GROUP BY + // 1; + // @t_zone must be replaced as 'GMT' before EliminateGroupByConstant and NormalizeAggregate rule. + // So need run VariableToLiteral rule before the two rules. + topDown(new VariableToLiteral()), // run CheckAnalysis before EliminateGroupByConstant in order to report error message correctly like bellow // select SUM(lo_tax) FROM lineorder group by 1; // errCode = 2, detailMessage = GROUP BY expression must not contain aggregate functions: sum(lo_tax) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/VariableToLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/VariableToLiteral.java new file mode 100644 index 00000000000000..912310ea32b5aa --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/VariableToLiteral.java @@ -0,0 +1,39 @@ +// 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. + +package org.apache.doris.nereids.rules.analysis; + +import org.apache.doris.nereids.rules.expression.ExpressionRewrite; +import org.apache.doris.nereids.rules.expression.ExpressionRewriteRule; +import org.apache.doris.nereids.rules.expression.ExpressionRuleExecutor; +import org.apache.doris.nereids.rules.expression.rules.ReplaceVariableByLiteral; + +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * replace Variable To Literal + */ +public class VariableToLiteral extends ExpressionRewrite { + public static final List NORMALIZE_REWRITE_RULES = + ImmutableList.of(ReplaceVariableByLiteral.INSTANCE); + + public VariableToLiteral() { + super(new ExpressionRuleExecutor(NORMALIZE_REWRITE_RULES)); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java index 6cff3553b43202..b6fff54b2977e9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionNormalization.java @@ -23,7 +23,6 @@ import org.apache.doris.nereids.rules.expression.rules.InPredicateDedup; import org.apache.doris.nereids.rules.expression.rules.InPredicateToEqualToRule; import org.apache.doris.nereids.rules.expression.rules.NormalizeBinaryPredicatesRule; -import org.apache.doris.nereids.rules.expression.rules.ReplaceVariableByLiteral; import org.apache.doris.nereids.rules.expression.rules.SimplifyArithmeticComparisonRule; import org.apache.doris.nereids.rules.expression.rules.SimplifyArithmeticRule; import org.apache.doris.nereids.rules.expression.rules.SimplifyCastRule; @@ -42,7 +41,6 @@ public class ExpressionNormalization extends ExpressionRewrite { // from_unixtime(timestamp, 'yyyyMMdd') to 'yyyyMMdd' public static final List NORMALIZE_REWRITE_RULES = ImmutableList.of( SupportJavaDateFormatter.INSTANCE, - ReplaceVariableByLiteral.INSTANCE, NormalizeBinaryPredicatesRule.INSTANCE, InPredicateDedup.INSTANCE, InPredicateToEqualToRule.INSTANCE, diff --git a/regression-test/suites/nereids_p0/test_user_var.groovy b/regression-test/suites/nereids_p0/test_user_var.groovy index f54e139632da93..81b611b5c151ce 100644 --- a/regression-test/suites/nereids_p0/test_user_var.groovy +++ b/regression-test/suites/nereids_p0/test_user_var.groovy @@ -18,6 +18,7 @@ suite("test_user_var") { sql "SET enable_nereids_planner=true" sql "SET enable_fallback_to_original_planner=false" + sql "SET enable_fold_constant_by_be=true;" sql "SET @a1=1, @a2=0, @a3=-1" sql "SET @b1=1.1, @b2=0.0, @b3=-1.1" sql "SET @c1='H', @c2=''" @@ -29,4 +30,31 @@ suite("test_user_var") { qt_select3 'select @c1, @c2;' qt_select4 'select @d1, @d2;' qt_select5 'select @f1, @f2;' + + sql """drop table if exists dwd_login_ttt;""" + sql """CREATE TABLE `dwd_login_ttt` ( + `game_code` varchar(100) NOT NULL DEFAULT "-" , + `plat_code` varchar(100) NOT NULL DEFAULT "-" , + `userid` varchar(255) NULL DEFAULT "-" , + `dt` datetime NOT NULL, + `time_zone` varchar(100) NULL + ) ENGINE=OLAP + UNIQUE KEY(`game_code`, `plat_code`) + DISTRIBUTED BY HASH(`game_code`) BUCKETS 16 + PROPERTIES("replication_num" = "1");""" + sql """drop view if exists dwd_login_ttt_view;""" + sql """create view dwd_login_ttt_view as + SELECT game_code,plat_code,time_zone,DATE_FORMAT(convert_tz(dt,time_zone,@t_zone),'%Y-%m-%d') day,count(distinct userid) + from dwd_login_ttt + where dt>=convert_tz(@t_day,'Asia/Shanghai',@t_zone) + and dt= '2024-01-31 16:00:00'" + } } \ No newline at end of file