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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.doris.nereids.rules.analysis.ProjectWithDistinctToAggregate;
import org.apache.doris.nereids.rules.analysis.ReplaceExpressionByChildOutput;
import org.apache.doris.nereids.rules.analysis.SubqueryToApply;
import org.apache.doris.nereids.rules.analysis.VariableToLiteral;
import org.apache.doris.nereids.rules.rewrite.MergeProjects;
import org.apache.doris.nereids.rules.rewrite.SemiJoinCommute;
import org.apache.doris.nereids.rules.rewrite.SimplifyAggGroupBy;
Expand Down Expand Up @@ -157,6 +158,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()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment to explain why must replace here

// 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(bottomUp(ReplaceVariableByLiteral.INSTANCE));

public VariableToLiteral() {
super(new ExpressionRuleExecutor(NORMALIZE_REWRITE_RULES));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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 @@ -43,7 +42,6 @@ public class ExpressionNormalization extends ExpressionRewrite {
// from_unixtime(timestamp, 'yyyyMMdd') to 'yyyyMMdd'
public static final List<ExpressionRewriteRule> NORMALIZE_REWRITE_RULES = ImmutableList.of(
bottomUp(
ReplaceVariableByLiteral.INSTANCE,
SupportJavaDateFormatter.INSTANCE,
NormalizeBinaryPredicatesRule.INSTANCE,
InPredicateDedup.INSTANCE,
Expand Down
35 changes: 35 additions & 0 deletions regression-test/suites/nereids_p0/test_user_var.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,39 @@ suite("test_user_var") {
qt_boolean 'select @d1, @d2;'
qt_null_literal 'select @f1, @f2;'
qt_function 'select @func_1'

multi_sql(
"""
drop table if exists dwd_login_ttt;

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");

drop view if exists dwd_login_ttt_view;

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;

set @t_day='2024-02-01';
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'"
}
}