This is an automated email from the ASF dual-hosted git repository.

panxiaolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 468226c44dd [Feature](materialized-view) support match function with 
alias in materialized-view (#30025)
468226c44dd is described below

commit 468226c44dd1d3c81dfa4453578de3393cff0219
Author: Pxl <pxl...@qq.com>
AuthorDate: Wed Jan 17 14:33:38 2024 +0800

    [Feature](materialized-view) support match function with alias in 
materialized-view (#30025)
    
    support match function with alias in materialized-view
---
 .../java/org/apache/doris/analysis/Analyzer.java   |  2 +
 .../org/apache/doris/analysis/FunctionName.java    |  4 ++
 .../org/apache/doris/rewrite/FunctionAlias.java    | 59 ++++++++++++++++++++++
 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, 126 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 5dcb9c193e2..b41666820b5 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
@@ -56,6 +56,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;
@@ -458,6 +459,7 @@ public class Analyzer {
             rules.add(MatchPredicateRule.INSTANCE);
             rules.add(EliminateUnnecessaryFunctions.INSTANCE);
             rules.add(ElementAtToSlotRefRule.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 510af96e4a8..cb243ef03e3 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
@@ -104,6 +104,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..9e9c6dc9f75
--- /dev/null
+++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FunctionAlias.java
@@ -0,0 +1,59 @@
+// 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("inet_ntoa", "ipv4_num_to_string")
+            .put("inet_aton", "ipv4_string_to_num").put("inet6_ntoa", 
"ipv6_num_to_string")
+            .put("inet6_aton", "ipv6_string_to_num").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 4f4c2f37b8e..0ca8dfad354 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'], 'CUSTOM', ['K', 
'V']],
         [['map_contains_value'], 'BOOLEAN', ['MAP<K, V>', 'V'], 'CUSTOM', 
['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: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to