Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5667,12 +5667,9 @@ public Command visitCreateUserDefineFunction(CreateUserDefineFunctionContext ctx
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;
Expand All @@ -5693,12 +5690,9 @@ public Command visitCreateAliasFunction(CreateAliasFunctionContext ctx) {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Class<? extends BoundFunction> functionClass() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Class<? extends BoundFunction> functionClass() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Class<? extends BoundFunction> functionClass() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Class<? extends BoundFunction> functionClass() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Class<? extends BoundFunction> functionClass() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Class<? extends BoundFunction> functionClass() {

@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ private void analyzeUdtf() throws AnalysisException {
}
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);
Expand All @@ -551,7 +551,7 @@ private void analyzeUdaf() throws AnalysisException {
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
Expand Down Expand Up @@ -641,7 +641,7 @@ private void analyzeUdf() throws AnalysisException {
}
function = ScalarFunction.createUdf(binaryType,
functionName, argsDef.getArgTypes(),
returnType.toCatalogDataType(), argsDef.isVariadic(),
returnType.toCatalogDataType(), false,
location, symbol, prepareFnSymbol, closeFnSymbol);
function.setChecksum(checksum);
function.setNullableMode(returnNullMode);
Expand Down Expand Up @@ -1174,7 +1174,7 @@ private void analyzeAliasFunction(ConnectContext ctx) throws AnalysisException {
}
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,14 +882,23 @@ public void testCreateCatalog() {
@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
Expand Down
Original file line number Diff line number Diff line change
@@ -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))));
}
}
Loading