This is an automated email from the ASF dual-hosted git repository.
kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-2.0 by this push:
new dafbd7e704b [Feature](materialized-view) support match function with
alias in materialized-view #30025 (#30127)
dafbd7e704b is described below
commit dafbd7e704b9e3a1cb08e59e7e1cce63bece7e27
Author: Pxl <[email protected]>
AuthorDate: Tue Feb 27 19:54:55 2024 +0800
[Feature](materialized-view) support match function with alias in
materialized-view #30025 (#30127)
---
.../java/org/apache/doris/analysis/Analyzer.java | 2 +
.../org/apache/doris/analysis/FunctionName.java | 4 ++
.../org/apache/doris/rewrite/FunctionAlias.java | 58 ++++++++++++++++++++++
gensrc/script/doris_builtins_functions.py | 2 +-
.../data/mv_p0/test_substr/test_substr.out | 4 ++
.../suites/mv_p0/test_substr/test_substr.groovy | 56 +++++++++++++++++++++
6 files changed, 125 insertions(+), 1 deletion(-)
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
index ddee3b3ecc9..907ecf63a34 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
@@ -54,6 +54,7 @@ import org.apache.doris.rewrite.ExprRewriteRule;
import org.apache.doris.rewrite.ExprRewriter;
import org.apache.doris.rewrite.ExtractCommonFactorsRule;
import org.apache.doris.rewrite.FoldConstantsRule;
+import org.apache.doris.rewrite.FunctionAlias;
import org.apache.doris.rewrite.InferFiltersRule;
import org.apache.doris.rewrite.MatchPredicateRule;
import org.apache.doris.rewrite.NormalizeBinaryPredicatesRule;
@@ -453,6 +454,7 @@ public class Analyzer {
rules.add(RewriteIsNullIsNotNullRule.INSTANCE);
rules.add(MatchPredicateRule.INSTANCE);
rules.add(EliminateUnnecessaryFunctions.INSTANCE);
+ rules.add(FunctionAlias.INSTANCE);
List<ExprRewriteRule> onceRules = Lists.newArrayList();
onceRules.add(ExtractCommonFactorsRule.INSTANCE);
onceRules.add(InferFiltersRule.INSTANCE);
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionName.java
b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionName.java
index f3cc637c2be..29d10ad3bcb 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionName.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionName.java
@@ -105,6 +105,10 @@ public class FunctionName implements Writable {
this.db = db;
}
+ public void setFn(String fn) {
+ this.fn = fn;
+ }
+
public String getFunction() {
return fn;
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FunctionAlias.java
b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FunctionAlias.java
new file mode 100644
index 00000000000..a5388bdceaa
--- /dev/null
+++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FunctionAlias.java
@@ -0,0 +1,58 @@
+// 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.rewrite;
+
+import org.apache.doris.analysis.Analyzer;
+import org.apache.doris.analysis.Expr;
+import org.apache.doris.analysis.FunctionCallExpr;
+import org.apache.doris.common.AnalysisException;
+
+import com.google.common.collect.ImmutableMap;
+
+import java.util.Map;
+
+/**
+ * Change functio name to function class name on nereids
+ * alias list: catalog/BuiltinScalarFunctions.java
+ */
+public final class FunctionAlias implements ExprRewriteRule {
+ public static ExprRewriteRule INSTANCE = new FunctionAlias();
+
+ static final Map<String, String> aliasToName = ImmutableMap.<String,
String>builder()
+ .put("array_size", "cardinality").put("size",
"cardinality").put("ceiling", "ceil")
+ .put("char_length", "character_length").put("curdate",
"current_date").put("curtime", "current_time")
+ .put("schema", "database").put("day",
"dayofmonth").put("date_add", "days_add").put("adddate", "days_add")
+ .put("date_sub", "days_sub").put("subdate",
"days_sub").put("lcase", "lower")
+ .put("add_months", "months_add")
+ .put("current_timestamp", "now").put("localtime",
"now").put("localtimestamp", "now").put("ifnull", "nvl")
+ .put("rand", "random").put("sha", "sha1").put("substr",
"substring").put("ucase", "upper").build();
+
+ @Override
+ public Expr apply(Expr expr, Analyzer analyzer, ExprRewriter.ClauseType
clauseType) throws AnalysisException {
+ if (!(expr instanceof FunctionCallExpr)) {
+ return expr;
+ }
+ FunctionCallExpr functionCall = (FunctionCallExpr) expr;
+ if (aliasToName.containsKey(functionCall.getFnName().getFunction())) {
+ FunctionCallExpr result = (FunctionCallExpr) functionCall.clone();
+
result.getFnName().setFn(aliasToName.get(functionCall.getFnName().getFunction()));
+ return result;
+ }
+ return expr;
+ }
+}
diff --git a/gensrc/script/doris_builtins_functions.py
b/gensrc/script/doris_builtins_functions.py
index d94d26046d1..e7a0f1ec278 100644
--- a/gensrc/script/doris_builtins_functions.py
+++ b/gensrc/script/doris_builtins_functions.py
@@ -75,7 +75,7 @@ visible_functions = {
"map": [
[['map'], 'MAP<K, V>', ['K', 'V', '...'], 'ALWAYS_NOT_NULLABLE', ['K',
'V']],
[['element_at', '%element_extract%'], 'V', ['MAP<K, V>', 'K'],
'ALWAYS_NULLABLE', ['K', 'V']],
- [['size', 'map_size'], 'BIGINT', ['MAP<K, V>'], '', ['K', 'V']],
+ [['size', 'map_size', 'cardinality'], 'BIGINT', ['MAP<K, V>'], '',
['K', 'V']],
[['map_contains_key'], 'BOOLEAN', ['MAP<K, V>', 'K'],
'ALWAYS_NULLABLE', ['K', 'V']],
[['map_contains_value'], 'BOOLEAN', ['MAP<K, V>', 'V'],
'ALWAYS_NULLABLE', ['K', 'V']],
#[['map_contains_key_like'], 'BOOLEAN', ['MAP<K, V>', 'K'], '', ['K',
'V']],
diff --git a/regression-test/data/mv_p0/test_substr/test_substr.out
b/regression-test/data/mv_p0/test_substr/test_substr.out
new file mode 100644
index 00000000000..8ebcba7eec0
--- /dev/null
+++ b/regression-test/data/mv_p0/test_substr/test_substr.out
@@ -0,0 +1,4 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !select_mv --
+\N \N
+
diff --git a/regression-test/suites/mv_p0/test_substr/test_substr.groovy
b/regression-test/suites/mv_p0/test_substr/test_substr.groovy
new file mode 100644
index 00000000000..5397bac6dc9
--- /dev/null
+++ b/regression-test/suites/mv_p0/test_substr/test_substr.groovy
@@ -0,0 +1,56 @@
+// 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.
+
+import org.codehaus.groovy.runtime.IOGroovyMethods
+
+suite ("test_substr") {
+ sql """set enable_nereids_planner=true"""
+ sql """SET enable_fallback_to_original_planner=false"""
+ sql """ drop table if exists dwd;"""
+
+ sql """
+ CREATE TABLE `dwd` (
+ `id` bigint(20) NULL COMMENT 'id',
+ `created_at` datetime NULL,
+ `dt` date NULL
+ ) ENGINE=OLAP
+ DUPLICATE KEY(`id`)
+ DISTRIBUTED BY HASH(`id`) BUCKETS 10
+ PROPERTIES (
+ "replication_allocation" = "tag.location.default: 1"
+ );
+ """
+
+ sql """insert into dwd(id) values(1);"""
+
+ createMV ("""
+ create materialized view dwd_mv as
+ SELECT
+ substr(created_at,1,10) as statistic_date,
+ max(dt) as dt
+ FROM dwd
+ group by substr(created_at,1,10);
+ """)
+
+ sql """insert into dwd(id) values(2);"""
+
+ explain {
+ sql("SELECT substr(created_at,1,10) as statistic_date, max(dt) as dt
FROM dwd group by substr(created_at,1,10);")
+ contains "(dwd_mv)"
+ }
+ qt_select_mv "SELECT substr(created_at,1,10) as statistic_date, max(dt) as
dt FROM dwd group by substr(created_at,1,10);"
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]