You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by mo...@apache.org on 2023/04/07 00:58:15 UTC

[doris] 05/07: [Fix](planner) fix nested udf bind arguments exception (#18188)

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

morningman pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 8f8adea52bf2d7d7ea3d02b5d509945458261be5
Author: mch_ucchi <41...@users.noreply.github.com>
AuthorDate: Thu Mar 30 11:39:02 2023 +0800

    [Fix](planner) fix nested udf bind arguments exception (#18188)
    
    nested alias function will cause bind argument exception, sql like:
    ``` sql
    CREATE ALIAS FUNCTION f1(DATETIMEV2(3), INT)
                with PARAMETER (datetime1, int1) as date_trunc(days_sub(datetime1, int1), 'day')
    
    CREATE ALIAS FUNCTION f2(DATETIMEV2(3), int)
                with PARAMETER (datetime1, int1) as DATE_FORMAT(HOURS_ADD(
                    date_trunc(datetime1, 'day'),
                    add(multiply(floor(divide(HOUR(datetime1), divide(24,int1))), 1), 1)
                ), '%Y%m%d:%H')
    
    select f2(f1(now(3), 2), 3)
    ```
    
    bug in FunctionCallExpr#rewriteExpr(), the retExpr will be replaced to originExpr to change the alias function to builtin function, but the retExpr.fn is not null, so when return to outer scope, the fn will be covered. That's the example:
    ```
    f1(f1()) -> date_trunc(days_sub(date_trunc(days_sub()))) is correct and
    f1(f1()) -> date_trunc(days_sub(days_sub())) is bug.
    ```
    
    we fix it.
---
 .../apache/doris/analysis/FunctionCallExpr.java    |  3 ++
 .../sql_functions/test_alias_function.groovy       | 34 ++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
index a91a01c4a6..e562706073 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FunctionCallExpr.java
@@ -1583,6 +1583,9 @@ public class FunctionCallExpr extends Expr {
 
         retExpr.fnParams = new FunctionParams(oriExpr.fnParams.isDistinct(), oriParamsExprs);
 
+        // retExpr changed to original function, so the fn should be null.
+        retExpr.fn = null;
+
         // reset children
         retExpr.children.clear();
         retExpr.children.addAll(oriExpr.getChildren());
diff --git a/regression-test/suites/query_p0/sql_functions/test_alias_function.groovy b/regression-test/suites/query_p0/sql_functions/test_alias_function.groovy
new file mode 100644
index 0000000000..4693c79067
--- /dev/null
+++ b/regression-test/suites/query_p0/sql_functions/test_alias_function.groovy
@@ -0,0 +1,34 @@
+// 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.
+
+suite('test_alias_function') {
+    sql "use test_query_db"
+    sql '''
+        CREATE ALIAS FUNCTION IF NOT EXISTS f1(DATETIMEV2(3), INT)
+            with PARAMETER (datetime1, int1) as date_trunc(days_sub(datetime1, int1), 'day')'''
+    sql '''
+        CREATE ALIAS FUNCTION IF NOT EXISTS f2(DATETIMEV2(3), int)
+            with PARAMETER (datetime1, int1) as DATE_FORMAT(HOURS_ADD(
+                date_trunc(datetime1, 'day'),
+                add(multiply(floor(divide(HOUR(datetime1), divide(24,int1))), 1), 1)
+            ), '%Y%m%d:%H');'''
+
+    test {
+        sql 'select f2(f1(now(3), 2), 3)'
+        result([['20230327:01']])
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org