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

zhehu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 274cc2d83f [CALCITE-6745] UDF without parameters cannot be validated 
when use default conformance
274cc2d83f is described below

commit 274cc2d83f00fa3aa991dd801c1bca78f5c60c96
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Jan 7 08:58:18 2025 +0800

    [CALCITE-6745] UDF without parameters cannot be validated when use default 
conformance
---
 .../apache/calcite/prepare/CalciteCatalogReader.java | 20 ++++++++++++++++----
 .../test/java/org/apache/calcite/test/UdfTest.java   |  8 ++++++++
 2 files changed, 24 insertions(+), 4 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/prepare/CalciteCatalogReader.java 
b/core/src/main/java/org/apache/calcite/prepare/CalciteCatalogReader.java
index 264d6e88c8..3131fdec2b 100644
--- a/core/src/main/java/org/apache/calcite/prepare/CalciteCatalogReader.java
+++ b/core/src/main/java/org/apache/calcite/prepare/CalciteCatalogReader.java
@@ -49,6 +49,7 @@ import org.apache.calcite.sql.type.SqlReturnTypeInference;
 import org.apache.calcite.sql.type.SqlTypeFamily;
 import org.apache.calcite.sql.type.SqlTypeName;
 import org.apache.calcite.sql.util.SqlOperatorTables;
+import org.apache.calcite.sql.validate.SqlConformanceEnum;
 import org.apache.calcite.sql.validate.SqlMoniker;
 import org.apache.calcite.sql.validate.SqlMonikerImpl;
 import org.apache.calcite.sql.validate.SqlMonikerType;
@@ -344,10 +345,7 @@ public class CalciteCatalogReader implements 
Prepare.CatalogReader {
     if (function instanceof ScalarFunction) {
       final SqlReturnTypeInference returnTypeInference =
           infer((ScalarFunction) function);
-      SqlSyntax syntax = function.getParameters().isEmpty()
-          && !config.conformance().allowNiladicParentheses()
-          ? SqlSyntax.FUNCTION_ID
-          : SqlSyntax.FUNCTION;
+      SqlSyntax syntax = getSqlSyntax(function, config);
       return new SqlUserDefinedFunction(name, kind, returnTypeInference,
           operandTypeInference, operandMetadata, function, syntax);
     } else if (function instanceof AggregateFunction) {
@@ -368,6 +366,20 @@ public class CalciteCatalogReader implements 
Prepare.CatalogReader {
     }
   }
 
+  private static SqlSyntax getSqlSyntax(org.apache.calcite.schema.Function 
function,
+      CalciteConnectionConfig config) {
+    if (!function.getParameters().isEmpty()) {
+      return SqlSyntax.FUNCTION;
+    }
+    // Keep compatible with both Foo() and Foo function syntax for Calcite's 
default conformance
+    if (SqlConformanceEnum.DEFAULT == config.conformance()) {
+      return SqlSyntax.FUNCTION_ID_CONSTANT;
+    }
+    return config.conformance().allowNiladicParentheses()
+        ? SqlSyntax.FUNCTION
+        : SqlSyntax.FUNCTION_ID;
+  }
+
   /** Deduces the {@link org.apache.calcite.sql.SqlKind} of a user-defined
    * function based on a {@link Hints} annotation, if present. */
   private static SqlKind kind(org.apache.calcite.schema.Function function) {
diff --git a/core/src/test/java/org/apache/calcite/test/UdfTest.java 
b/core/src/test/java/org/apache/calcite/test/UdfTest.java
index b796a0c7bd..674b1e6c2a 100644
--- a/core/src/test/java/org/apache/calcite/test/UdfTest.java
+++ b/core/src/test/java/org/apache/calcite/test/UdfTest.java
@@ -427,6 +427,14 @@ class UdfTest {
         .query("select \"adhoc\".my_niladic_parentheses as p\n"
             + "from \"adhoc\".EMPLOYEES limit 1")
         .returns("P=foo\n");
+    with.with(SqlConformanceEnum.DEFAULT)
+        .query("select \"adhoc\".my_niladic_parentheses as p\n"
+            + "from \"adhoc\".EMPLOYEES limit 1")
+        .returns("P=foo\n");
+    with.with(SqlConformanceEnum.DEFAULT)
+        .query("select \"adhoc\".my_niladic_parentheses() as p\n"
+            + "from \"adhoc\".EMPLOYEES limit 1")
+        .returns("P=foo\n");
     // wrong niladic function with mysql_5 conformance
     with.with(SqlConformanceEnum.MYSQL_5)
         .query("select \"adhoc\".my_niladic_parentheses as p\n"

Reply via email to