Skip to content

Commit

Permalink
[fix](nereids)move ReplaceVariableByLiteral rule to analyze phase (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
starocean999 authored and weixingyu12 committed Apr 30, 2024
1 parent a34a1ab commit cb0bdd3
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -150,6 +151,17 @@ private static List<RewriteJob> buildAnalyzeJobs(Optional<CustomTableResolver> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ExpressionRewriteRule> NORMALIZE_REWRITE_RULES =
ImmutableList.of(ReplaceVariableByLiteral.INSTANCE);

public VariableToLiteral() {
super(new ExpressionRuleExecutor(NORMALIZE_REWRITE_RULES));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,7 +41,6 @@ public class ExpressionNormalization extends ExpressionRewrite {
// from_unixtime(timestamp, 'yyyyMMdd') to 'yyyyMMdd'
public static final List<ExpressionRewriteRule> NORMALIZE_REWRITE_RULES = ImmutableList.of(
SupportJavaDateFormatter.INSTANCE,
ReplaceVariableByLiteral.INSTANCE,
NormalizeBinaryPredicatesRule.INSTANCE,
InPredicateDedup.INSTANCE,
InPredicateToEqualToRule.INSTANCE,
Expand Down
28 changes: 28 additions & 0 deletions regression-test/suites/nereids_p0/test_user_var.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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=''"
Expand All @@ -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<convert_tz(date_add(@t_day,1),'Asia/Shanghai',@t_zone)
GROUP by 1,2,3,4;"""
sql """set @t_day='2024-02-01';"""
sql """set @t_zone='GMT';"""

explain {
sql("shape plan select * from dwd_login_ttt_view where day='2024-04-01';")
contains "dt < '2024-02-01 16:00:00'"
contains "dt >= '2024-01-31 16:00:00'"
}
}

0 comments on commit cb0bdd3

Please sign in to comment.