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

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


The following commit(s) were added to refs/heads/master by this push:
     new 8277b54925 [spark] Reject SQL function types with a collation Paimon 
cannot persist (#11050)
8277b54925 is described below

commit 8277b549253e3b144d28114c4fd3c64d2be838f2
Author: YangJie <[email protected]>
AuthorDate: Tue Nov 10 04:12:34 2026 -0500

    [spark] Reject SQL function types with a collation Paimon cannot persist 
(#11050)
---
 .../spark/sql/PaimonSQLFunctionTestBase.scala      | 57 ++++++++++++++++++++++
 .../catalog/functions/SQLFunctionConverter.scala   |  8 +--
 .../spark/catalog/functions/SQLFunctionTypes.scala | 46 +++++++++++++++++
 3 files changed, 108 insertions(+), 3 deletions(-)

diff --git 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/PaimonSQLFunctionTestBase.scala
 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/PaimonSQLFunctionTestBase.scala
index 8d13da7d60..4eef0aff6b 100644
--- 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/PaimonSQLFunctionTestBase.scala
+++ 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/sql/PaimonSQLFunctionTestBase.scala
@@ -229,4 +229,61 @@ abstract class PaimonSQLFunctionTestBase extends 
PaimonSparkTestWithRestCatalogB
     }
     assert(e.getMessage.contains("does not support creating SQL table 
functions"))
   }
+
+  private def assertCollatedTypeRejected(funcName: String, createSql: String): 
Unit = {
+    withUserDefinedFunction(funcName -> false) {
+      val e = intercept[Exception](sql(createSql))
+      assert(e.getMessage.contains("does not support collated string types"), 
e.getMessage)
+      val names = sql("SHOW USER 
FUNCTIONS").collect().map(_.getString(0).toLowerCase)
+      assert(!names.exists(_.contains(funcName)), names.mkString(", "))
+    }
+  }
+
+  test("Paimon SQL Function: collated parameter type is not supported yet") {
+    Seq(
+      "STRING COLLATE UTF8_LCASE",
+      "ARRAY<STRING COLLATE UTF8_LCASE>",
+      "MAP<STRING, STRING COLLATE UTF8_LCASE>",
+      "MAP<STRING COLLATE UTF8_LCASE, STRING>",
+      "STRUCT<s: STRING COLLATE UTF8_LCASE>"
+    ).foreach {
+      paramType =>
+        assertCollatedTypeRejected(
+          "lcase_param",
+          s"CREATE FUNCTION lcase_param(p $paramType) RETURNS BOOLEAN RETURN p 
IS NULL")
+    }
+  }
+
+  test("Paimon SQL Function: collated return type is not supported yet") {
+    assertCollatedTypeRejected(
+      "lcase_ret",
+      "CREATE FUNCTION lcase_ret() RETURNS STRING COLLATE UTF8_LCASE RETURN 
'a'")
+  }
+
+  test("Paimon SQL Function: inferred collated return type is not supported 
yet") {
+    assume(gteqSpark4_1)
+    assertCollatedTypeRejected(
+      "lcase_inferred",
+      "CREATE FUNCTION lcase_inferred() RETURN 'a' COLLATE UTF8_LCASE")
+  }
+
+  test("Paimon SQL Function: explicit UTF8_BINARY collation is accepted") {
+    withUserDefinedFunction("binary_eq" -> false) {
+      sql(
+        "CREATE FUNCTION binary_eq(p STRING COLLATE UTF8_BINARY) " +
+          "RETURNS BOOLEAN RETURN p = 'A'")
+      checkAnswer(sql("SELECT binary_eq('a'), binary_eq('A')"), Row(false, 
true))
+    }
+  }
+
+  test("Paimon SQL Function: rejected CREATE OR REPLACE keeps the existing 
function") {
+    withUserDefinedFunction("lcase_keep" -> false) {
+      sql("CREATE FUNCTION lcase_keep(p STRING) RETURNS BOOLEAN RETURN p = 
'A'")
+      val e = intercept[Exception](
+        sql("CREATE OR REPLACE FUNCTION lcase_keep(p STRING COLLATE 
UTF8_LCASE) " +
+          "RETURNS BOOLEAN RETURN p = 'a'"))
+      assert(e.getMessage.contains("does not support collated string types"), 
e.getMessage)
+      checkAnswer(sql("SELECT lcase_keep('a'), lcase_keep('A')"), Row(false, 
true))
+    }
+  }
 }
diff --git 
a/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionConverter.scala
 
b/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionConverter.scala
index 4aee17aa2e..178232faf4 100644
--- 
a/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionConverter.scala
+++ 
b/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionConverter.scala
@@ -62,12 +62,14 @@ object SQLFunctionConverter {
 
     val inputParams: JList[DataField] = inputParamText.filter(_.trim.nonEmpty) 
match {
       case Some(text) =>
-        SparkTypeUtils
-          .toPaimonRowType(UserDefinedFunction.parseRoutineParam(text, parser))
-          .getFields
+        val params = UserDefinedFunction.parseRoutineParam(text, parser)
+        params.fields.foreach(
+          p => SQLFunctionTypes.requireNoCollation(funcIdent, s"parameter 
${p.name}", p.dataType))
+        SparkTypeUtils.toPaimonRowType(params).getFields
       case None => Collections.emptyList[DataField]()
     }
     val returnSparkType = parseScalarReturnType(funcIdent, returnTypeText, 
parser)
+    SQLFunctionTypes.requireNoCollation(funcIdent, "return value", 
returnSparkType)
     val returnParams: JList[DataField] =
       Collections.singletonList(
         new DataField(0, funcIdent.funcName, 
SparkTypeUtils.toPaimonType(returnSparkType)))
diff --git 
a/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionTypes.scala
 
b/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionTypes.scala
new file mode 100644
index 0000000000..def4c14eb9
--- /dev/null
+++ 
b/paimon-spark/paimon-spark4-common/src/main/scala/org/apache/paimon/spark/catalog/functions/SQLFunctionTypes.scala
@@ -0,0 +1,46 @@
+/*
+ * 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.paimon.spark.catalog.functions
+
+import org.apache.spark.sql.catalyst.FunctionIdentifier
+import org.apache.spark.sql.catalyst.util.CollationFactory
+import org.apache.spark.sql.types.{ArrayType, DataType, MapType, StringType, 
StructType}
+
+/** Checks on SQL function parameter and return types that Paimon cannot 
persist. */
+object SQLFunctionTypes {
+
+  /**
+   * Throws `UnsupportedOperationException` if `dataType` has a string 
collated other than
+   * UTF8_BINARY. Paimon stores a Spark string type without its collation, so 
it would be lost.
+   */
+  def requireNoCollation(funcIdent: FunctionIdentifier, what: String, 
dataType: DataType): Unit =
+    if (hasCollation(dataType)) {
+      throw new UnsupportedOperationException(
+        s"Paimon does not support collated string types in SQL functions yet: 
$funcIdent $what " +
+          s"has type ${dataType.sql}")
+    }
+
+  private def hasCollation(dataType: DataType): Boolean = dataType match {
+    case s: StringType => s.collationId != 
CollationFactory.UTF8_BINARY_COLLATION_ID
+    case a: ArrayType => hasCollation(a.elementType)
+    case m: MapType => hasCollation(m.keyType) || hasCollation(m.valueType)
+    case s: StructType => s.fields.exists(f => hasCollation(f.dataType))
+    case _ => false
+  }
+}

Reply via email to