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 @@ -136,33 +136,28 @@ public FunctionBuilder findFunctionBuilder(String dbName, String name, List<?> a
int arity = arguments.size();
String qualifiedName = StringUtils.isEmpty(dbName) ? name : dbName + "." + name;

if (StringUtils.isEmpty(dbName)) {
// search internal function only if dbName is empty
functionBuilders = name2BuiltinBuilders.get(name.toLowerCase());
if (CollectionUtils.isEmpty(functionBuilders) && AggCombinerFunctionBuilder.isAggStateCombinator(name)) {
String nestedName = AggCombinerFunctionBuilder.getNestedName(name);
String combinatorSuffix = AggCombinerFunctionBuilder.getCombinatorSuffix(name);
functionBuilders = name2BuiltinBuilders.get(nestedName.toLowerCase());
if (functionBuilders != null) {
List<FunctionBuilder> candidateBuilders = Lists.newArrayListWithCapacity(functionBuilders.size());
for (FunctionBuilder functionBuilder : functionBuilders) {
AggCombinerFunctionBuilder combinerBuilder
= new AggCombinerFunctionBuilder(combinatorSuffix, functionBuilder);
if (combinerBuilder.canApply(arguments)) {
candidateBuilders.add(combinerBuilder);
}
}
functionBuilders = candidateBuilders;
}
}
}
boolean preferUdfOverBuiltin = ConnectContext.get() == null ? false
: ConnectContext.get().getSessionVariable().preferUdfOverBuiltin;

// search udf
if (CollectionUtils.isEmpty(functionBuilders)) {
if (preferUdfOverBuiltin) {
// find udf first, then find builtin function
functionBuilders = findUdfBuilder(dbName, name);
if (functionBuilders == null || functionBuilders.isEmpty()) {
throw new AnalysisException("Can not found function '" + qualifiedName + "'");
if (CollectionUtils.isEmpty(functionBuilders) && StringUtils.isEmpty(dbName)) {
// if dbName is not empty, we should search builtin functions first
functionBuilders = findBuiltinFunctionBuilder(name, arguments);
}
} else {
// find builtin function first, then find udf
if (StringUtils.isEmpty(dbName)) {
functionBuilders = findBuiltinFunctionBuilder(name, arguments);
}
if (CollectionUtils.isEmpty(functionBuilders)) {
functionBuilders = findUdfBuilder(dbName, name);
}
}

if (functionBuilders == null || functionBuilders.isEmpty()) {
throw new AnalysisException("Can not found function '" + qualifiedName + "'");
}

// check the arity and type
Expand Down Expand Up @@ -217,6 +212,29 @@ public FunctionBuilder findFunctionBuilder(String dbName, String name, List<?> a
return candidateBuilders.get(0);
}

private List<FunctionBuilder> findBuiltinFunctionBuilder(String name, List<?> arguments) {
List<FunctionBuilder> functionBuilders;
// search internal function only if dbName is empty
functionBuilders = name2BuiltinBuilders.get(name.toLowerCase());
if (CollectionUtils.isEmpty(functionBuilders) && AggCombinerFunctionBuilder.isAggStateCombinator(name)) {
String nestedName = AggCombinerFunctionBuilder.getNestedName(name);
String combinatorSuffix = AggCombinerFunctionBuilder.getCombinatorSuffix(name);
functionBuilders = name2BuiltinBuilders.get(nestedName.toLowerCase());
if (functionBuilders != null) {
List<FunctionBuilder> candidateBuilders = Lists.newArrayListWithCapacity(functionBuilders.size());
for (FunctionBuilder functionBuilder : functionBuilders) {
AggCombinerFunctionBuilder combinerBuilder
= new AggCombinerFunctionBuilder(combinatorSuffix, functionBuilder);
if (combinerBuilder.canApply(arguments)) {
candidateBuilders.add(combinerBuilder);
}
}
functionBuilders = candidateBuilders;
}
}
return functionBuilders;
}

/**
* public for test.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ default boolean nullable() {
NullableMode getNullableMode();

List<Expression> children();

@Override
default boolean foldable() {
// Udf should not be folded in FE.
// When session variable "prefer_udf_fold" is set to true,
// we may find udf with same signature as builtin function.
// If return true, the FE will calculate the result of the udf with builtin function's logic, which is wrong.
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ public class SessionVariable implements Serializable, Writable {

public static final String SQL_CONVERTOR_CONFIG = "sql_convertor_config";

public static final String PREFER_UDF_OVER_BUILTIN = "prefer_udf_over_builtin";

/**
* If set false, user couldn't submit analyze SQL and FE won't allocate any related resources.
*/
Expand Down Expand Up @@ -2603,6 +2605,13 @@ public void setDetailShapePlanNodes(String detailShapePlanNodes) {
})
public String sqlConvertorConfig = "{}";

@VariableMgr.VarAttr(name = PREFER_UDF_OVER_BUILTIN, needForward = true,
description = {
"是否优先查找 UDF 而不是内置函数",
"Whether to prefer UDF over builtin functions"
})
public boolean preferUdfOverBuiltin = false;

public void setEnableEsParallelScroll(boolean enableESParallelScroll) {
this.enableESParallelScroll = enableESParallelScroll;
}
Expand Down
21 changes: 21 additions & 0 deletions regression-test/data/javaudf_p0/test_javaudf_override.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !sql01 --
1 1 100
1 1 0
1 1 100

-- !sql02 --
-1 1 -100
-1 1 0
-1 1 100

-- !sql03 --
1 1 -1 1 100 -100
1 1 -1 1 0 0
1 1 -1 1 100 100

-- !sql04 --
-1 1 -1 1 -100 -100
-1 1 -1 1 0 0
-1 1 -1 1 100 100

82 changes: 82 additions & 0 deletions regression-test/suites/javaudf_p0/test_javaudf_override.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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.

import org.codehaus.groovy.runtime.IOGroovyMethods

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Paths

suite("test_javaudf_override") {
def tableName = "test_javaudf_override"
def jarPath = """${context.file.parent}/jars/java-udf-case-jar-with-dependencies.jar"""
scp_udf_file_to_all_be(jarPath)

log.info("Jar path: ${jarPath}".toString())
sql "drop database if exists test_javaudf_override_db"
sql "create database test_javaudf_override_db"
try {
sql """ use test_javaudf_override_db"""
sql """ DROP TABLE IF EXISTS ${tableName} """
sql """
CREATE TABLE IF NOT EXISTS ${tableName} (
int_col int
)
DISTRIBUTED BY HASH(int_col) PROPERTIES("replication_num" = "1");
"""
sql """insert into ${tableName} values(-100),(100),(0)"""

File path = new File(jarPath)
if (!path.exists()) {
throw new IllegalStateException("""${jarPath} doesn't exist! """)
}

// 1. create global udf "abs", same name as builtin function "abs"

sql """DROP GLOBAL FUNCTION IF EXISTS abs(int);"""
sql """CREATE GLOBAL FUNCTION abs(int) RETURNS int PROPERTIES (
"file"="file://${jarPath}",
"symbol"="org.apache.doris.udf.Echo\$EchoInt",
"type"="JAVA_UDF"
);"""

// default, will use builtin function first
sql "set prefer_udf_over_builtin=false"
qt_sql01 """select abs(-1), abs(1), abs(int_col) from ${tableName}"""
// set prefer_udf_over_builtin, will use udf first
sql "set prefer_udf_over_builtin=true"
qt_sql02 """select abs(-1), abs(1), abs(int_col) from ${tableName}"""

// 2. create database udf "abs", same name as builtin function "abs"
def curdb = "test_javaudf_override_db"
sql """DROP GLOBAL FUNCTION IF EXISTS abs(int);"""
sql "use ${curdb}"
sql """CREATE FUNCTION abs(int) RETURNS int PROPERTIES (
"file"="file://${jarPath}",
"symbol"="org.apache.doris.udf.Echo\$EchoInt",
"type"="JAVA_UDF"
);"""


sql "set prefer_udf_over_builtin=false"
qt_sql03 """select abs(-1), abs(1), ${curdb}.abs(-1), ${curdb}.abs(1), abs(int_col), ${curdb}.abs(int_col) from ${tableName}"""
sql "set prefer_udf_over_builtin=true"
qt_sql04 """select abs(-1), abs(1), ${curdb}.abs(-1), ${curdb}.abs(1), abs(int_col), ${curdb}.abs(int_col) from ${tableName}"""

} finally {
}
}
Loading