This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new 2539bb0d130 branch-4.1: [fix](udf) Reject variadic user-defined
functions #67373 (#67524)
2539bb0d130 is described below
commit 2539bb0d130b271a09bb2ae5d0ae66b117797a26
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Sun Sep 6 15:50:39 2026 +0800
branch-4.1: [fix](udf) Reject variadic user-defined functions #67373
(#67524)
Cherry-picked from #67373
Co-authored-by: linrrarity <[email protected]>
---
.../antlr4/org/apache/doris/nereids/DorisParser.g4 | 4 +-
.../doris/nereids/parser/LogicalPlanBuilder.java | 18 ++----
.../expressions/functions/udf/JavaUdafBuilder.java | 2 +-
.../expressions/functions/udf/JavaUdfBuilder.java | 2 +-
.../expressions/functions/udf/JavaUdtfBuilder.java | 2 +-
.../functions/udf/PythonUdafBuilder.java | 2 +-
.../functions/udf/PythonUdfBuilder.java | 2 +-
.../functions/udf/PythonUdtfBuilder.java | 2 +-
.../plans/commands/CreateFunctionCommand.java | 8 +--
.../doris/nereids/parser/NereidsParserTest.java | 21 +++++--
.../functions/udf/UdfBuilderArityTest.java | 68 ++++++++++++++++++++++
11 files changed, 101 insertions(+), 30 deletions(-)
diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
index e6fc906f478..cb84945ce76 100644
--- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
+++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4
@@ -247,12 +247,12 @@ supportedCreateStatement
| CREATE ENCRYPTKEY (IF NOT EXISTS)? multipartIdentifier AS STRING_LITERAL
#createEncryptkey
| CREATE statementScope?
(TABLES | AGGREGATE)? FUNCTION (IF NOT EXISTS)?
- functionIdentifier LEFT_PAREN functionArguments? RIGHT_PAREN
+ functionIdentifier LEFT_PAREN dataTypeList? RIGHT_PAREN
RETURNS returnType=dataType (INTERMEDIATE
intermediateType=dataType)?
properties=propertyClause?
(AS functionCode=dollarQuotedString)?
#createUserDefineFunction
| CREATE statementScope? ALIAS FUNCTION (IF NOT EXISTS)?
- functionIdentifier LEFT_PAREN functionArguments? RIGHT_PAREN
+ functionIdentifier LEFT_PAREN dataTypeList? RIGHT_PAREN
WITH PARAMETER LEFT_PAREN parameters=identifierSeq? RIGHT_PAREN
AS expression
#createAliasFunction
| CREATE USER (IF NOT EXISTS)? grantUserIdentify
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index 2fc12d07288..4659bf3b69e 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -5667,12 +5667,9 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
boolean isAggFunction = ctx.AGGREGATE() != null;
boolean isTableFunction = ctx.TABLES() != null;
FunctionName function =
visitFunctionIdentifier(ctx.functionIdentifier());
- FunctionArgTypesInfo functionArgTypesInfo;
- if (ctx.functionArguments() != null) {
- functionArgTypesInfo =
visitFunctionArguments(ctx.functionArguments());
- } else {
- functionArgTypesInfo = new FunctionArgTypesInfo(new ArrayList<>(),
false);
- }
+ List<DataType> argTypes = ctx.dataTypeList() == null
+ ? new ArrayList<>() : visitDataTypeList(ctx.dataTypeList());
+ FunctionArgTypesInfo functionArgTypesInfo = new
FunctionArgTypesInfo(argTypes, false);
DataType returnType = typedVisit(ctx.returnType);
returnType = returnType.conversion();
DataType intermediateType = ctx.intermediateType != null ?
typedVisit(ctx.intermediateType) : null;
@@ -5693,12 +5690,9 @@ public class LogicalPlanBuilder extends
DorisParserBaseVisitor<Object> {
SetType statementScope = visitStatementScope(ctx.statementScope());
boolean ifNotExists = ctx.EXISTS() != null;
FunctionName function =
visitFunctionIdentifier(ctx.functionIdentifier());
- FunctionArgTypesInfo functionArgTypesInfo;
- if (ctx.functionArguments() != null) {
- functionArgTypesInfo =
visitFunctionArguments(ctx.functionArguments());
- } else {
- functionArgTypesInfo = new FunctionArgTypesInfo(new ArrayList<>(),
false);
- }
+ List<DataType> argTypes = ctx.dataTypeList() == null
+ ? new ArrayList<>() : visitDataTypeList(ctx.dataTypeList());
+ FunctionArgTypesInfo functionArgTypesInfo = new
FunctionArgTypesInfo(argTypes, false);
List<String> parameters = ctx.parameters != null ?
visitIdentifierSeq(ctx.parameters) : new ArrayList<>();
Expression originFunction = getExpression(ctx.expression());
return new CreateFunctionCommand(statementScope, ifNotExists, false,
true, false,
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdafBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdafBuilder.java
index 4822ab6ef1a..6abb1f27876 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdafBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdafBuilder.java
@@ -63,7 +63,7 @@ public class JavaUdafBuilder extends UdfBuilder {
@Override
public boolean canApply(List<?> arguments) {
- if ((isVarArgs && arity > arguments.size() + 1) || (!isVarArgs &&
arguments.size() != arity)) {
+ if (arguments.size() != arity) {
return false;
}
for (Object argument : arguments) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdfBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdfBuilder.java
index 6ab90cb42cd..27d5c62da6d 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdfBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdfBuilder.java
@@ -65,7 +65,7 @@ public class JavaUdfBuilder extends UdfBuilder {
@Override
public boolean canApply(List<?> arguments) {
- if ((isVarArgs && arity > arguments.size() + 1) || (!isVarArgs &&
arguments.size() != arity)) {
+ if (arguments.size() != arity) {
return false;
}
for (Object argument : arguments) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdtfBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdtfBuilder.java
index f00fa22cf5b..201d583351d 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdtfBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/JavaUdtfBuilder.java
@@ -65,7 +65,7 @@ public class JavaUdtfBuilder extends UdfBuilder {
@Override
public boolean canApply(List<?> arguments) {
- if ((isVarArgs && arity > arguments.size() + 1) || (!isVarArgs &&
arguments.size() != arity)) {
+ if (arguments.size() != arity) {
return false;
}
for (Object argument : arguments) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdafBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdafBuilder.java
index d18800fd429..de8a69c7c48 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdafBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdafBuilder.java
@@ -63,7 +63,7 @@ public class PythonUdafBuilder extends UdfBuilder {
@Override
public boolean canApply(List<?> arguments) {
- if ((isVarArgs && arity > arguments.size() + 1) || (!isVarArgs &&
arguments.size() != arity)) {
+ if (arguments.size() != arity) {
return false;
}
for (Object argument : arguments) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdfBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdfBuilder.java
index 85ff1035c3d..af9f4586296 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdfBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdfBuilder.java
@@ -65,7 +65,7 @@ public class PythonUdfBuilder extends UdfBuilder {
@Override
public boolean canApply(List<?> arguments) {
- if ((isVarArgs && arity > arguments.size() + 1) || (!isVarArgs &&
arguments.size() != arity)) {
+ if (arguments.size() != arity) {
return false;
}
for (Object argument : arguments) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdtfBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdtfBuilder.java
index dd9638f3c20..e28e05fd827 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdtfBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/udf/PythonUdtfBuilder.java
@@ -65,7 +65,7 @@ public class PythonUdtfBuilder extends UdfBuilder {
@Override
public boolean canApply(List<?> arguments) {
- if ((isVarArgs && arity > arguments.size() + 1) || (!isVarArgs &&
arguments.size() != arity)) {
+ if (arguments.size() != arity) {
return false;
}
for (Object argument : arguments) {
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateFunctionCommand.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateFunctionCommand.java
index bc817d50fd3..0fcd335c1cb 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateFunctionCommand.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/CreateFunctionCommand.java
@@ -529,7 +529,7 @@ public class CreateFunctionCommand extends Command
implements ForwardWithSync {
}
function = ScalarFunction.createUdf(binaryType,
functionName, argsDef.getArgTypes(),
- ((ArrayType) (returnType.toCatalogDataType())).getItemType(),
argsDef.isVariadic(),
+ ((ArrayType) (returnType.toCatalogDataType())).getItemType(),
false,
location, symbol, null, null);
function.setChecksum(checksum);
function.setNullableMode(returnNullMode);
@@ -551,7 +551,7 @@ public class CreateFunctionCommand extends Command
implements ForwardWithSync {
location = null;
}
builder.name(functionName).argsType(argsDef.getArgTypes()).retType(returnType.toCatalogDataType())
-
.hasVarArgs(argsDef.isVariadic()).intermediateType(intermediateType.toCatalogDataType())
+
.hasVarArgs(false).intermediateType(intermediateType.toCatalogDataType())
.location(location);
String initFnSymbol = properties.get(INIT_KEY);
if (initFnSymbol == null && !(binaryType ==
TFunctionBinaryType.JAVA_UDF
@@ -641,7 +641,7 @@ public class CreateFunctionCommand extends Command
implements ForwardWithSync {
}
function = ScalarFunction.createUdf(binaryType,
functionName, argsDef.getArgTypes(),
- returnType.toCatalogDataType(), argsDef.isVariadic(),
+ returnType.toCatalogDataType(), false,
location, symbol, prepareFnSymbol, closeFnSymbol);
function.setChecksum(checksum);
function.setNullableMode(returnNullMode);
@@ -1174,7 +1174,7 @@ public class CreateFunctionCommand extends Command
implements ForwardWithSync {
}
Map<String, String> sessionVariables =
ConnectContextUtil.getAffectQueryResultInPlanVariables(ctx);
function = AliasFunction.createFunction(functionName,
argsDef.getArgTypes(),
- Type.VARCHAR, argsDef.isVariadic(), parameters,
translateToLegacyExpr(originFunction, ctx),
+ Type.VARCHAR, false, parameters,
translateToLegacyExpr(originFunction, ctx),
sessionVariables);
}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
index 8dd5fbe0f49..9fb470b46ca 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/NereidsParserTest.java
@@ -882,14 +882,23 @@ public class NereidsParserTest extends ParserTestBase {
@Test
public void testCreateFunction() {
NereidsParser nereidsParser = new NereidsParser();
- String sql = "create session tables function func_a (int, ...) returns
boolean properties('k'='v')";
- nereidsParser.parseSingle(sql);
+ nereidsParser.parseSingle(
+ "create session tables function func_a(int) returns boolean
properties('k'='v')");
+ nereidsParser.parseSingle("create local aggregate function func_a(int)
returns boolean "
+ + "intermediate varchar properties('k'='v')");
+ nereidsParser.parseSingle("create alias function func_a(int) with
parameter(id) as abs(id)");
- sql = "create local aggregate function func_a (int, ...) returns
boolean intermediate varchar properties('k'='v')";
- nereidsParser.parseSingle(sql);
+ Assertions.assertThrows(ParseException.class, () ->
nereidsParser.parseSingle(
+ "create function func_a(int, ...) returns boolean
properties('k'='v')"));
+ Assertions.assertThrows(ParseException.class, () ->
nereidsParser.parseSingle(
+ "create aggregate function func_a(int, ...) returns boolean
properties('k'='v')"));
+ Assertions.assertThrows(ParseException.class, () ->
nereidsParser.parseSingle(
+ "create tables function func_a(int, ...) returns boolean
properties('k'='v')"));
+ Assertions.assertThrows(ParseException.class, () ->
nereidsParser.parseSingle(
+ "create alias function func_a(int, ...) with parameter(id) as
abs(id)"));
- sql = "create alias function func_a (int) with parameter(id) as
abs(id)";
- nereidsParser.parseSingle(sql);
+ nereidsParser.parseSingle("drop function func_a(int, ...)");
+ nereidsParser.parseSingle("show create function func_a(int, ...)");
}
@Test
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/udf/UdfBuilderArityTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/udf/UdfBuilderArityTest.java
new file mode 100644
index 00000000000..fc7e3dd76c2
--- /dev/null
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/udf/UdfBuilderArityTest.java
@@ -0,0 +1,68 @@
+// 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.nereids.trees.expressions.functions.udf;
+
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+class UdfBuilderArityTest {
+
+ @Test
+ void testVariadicMetadataDoesNotEnableVariableArity() {
+ JavaUdf javaUdf = Mockito.mock(JavaUdf.class);
+ Mockito.when(javaUdf.hasVarArguments()).thenReturn(true);
+ Mockito.when(javaUdf.arity()).thenReturn(2);
+ assertFixedArity(new JavaUdfBuilder(javaUdf));
+
+ JavaUdaf javaUdaf = Mockito.mock(JavaUdaf.class);
+ Mockito.when(javaUdaf.hasVarArguments()).thenReturn(true);
+ Mockito.when(javaUdaf.arity()).thenReturn(2);
+ assertFixedArity(new JavaUdafBuilder(javaUdaf));
+
+ JavaUdtf javaUdtf = Mockito.mock(JavaUdtf.class);
+ Mockito.when(javaUdtf.hasVarArguments()).thenReturn(true);
+ Mockito.when(javaUdtf.arity()).thenReturn(2);
+ assertFixedArity(new JavaUdtfBuilder(javaUdtf));
+
+ PythonUdf pythonUdf = Mockito.mock(PythonUdf.class);
+ Mockito.when(pythonUdf.hasVarArguments()).thenReturn(true);
+ Mockito.when(pythonUdf.arity()).thenReturn(2);
+ assertFixedArity(new PythonUdfBuilder(pythonUdf));
+
+ PythonUdaf pythonUdaf = Mockito.mock(PythonUdaf.class);
+ Mockito.when(pythonUdaf.hasVarArguments()).thenReturn(true);
+ Mockito.when(pythonUdaf.arity()).thenReturn(2);
+ assertFixedArity(new PythonUdafBuilder(pythonUdaf));
+
+ PythonUdtf pythonUdtf = Mockito.mock(PythonUdtf.class);
+ Mockito.when(pythonUdtf.hasVarArguments()).thenReturn(true);
+ Mockito.when(pythonUdtf.arity()).thenReturn(2);
+ assertFixedArity(new PythonUdtfBuilder(pythonUdtf));
+ }
+
+ private void assertFixedArity(UdfBuilder builder) {
+ Assertions.assertFalse(builder.canApply(ImmutableList.of(new
IntegerLiteral(1))));
+ Assertions.assertTrue(builder.canApply(ImmutableList.of(new
IntegerLiteral(1), new IntegerLiteral(2))));
+ Assertions.assertFalse(builder.canApply(
+ ImmutableList.of(new IntegerLiteral(1), new IntegerLiteral(2),
new IntegerLiteral(3))));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]