Skip to content

Commit cd3e3c8

Browse files
authored
misc: Enable function resolution to resolve functions with full qualified name (#26640)
## Description Current FunctionResolution only has `lookupBuiltInFunction` which works for built in function in default namespace, this PR will enable it to look up functions in non-default namespace too. ## Motivation and Context In description ## Impact In description ## Test Plan Unit test ## Contributor checklist - [ ] Please make sure your submission complies with our [contributing guide](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md), in particular [code style](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md#code-style) and [commit standards](https://github.com/prestodb/presto/blob/master/CONTRIBUTING.md#commit-standards). - [ ] PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced. - [ ] Documented new properties (with its default value), SQL syntax, functions, or other functionality. - [ ] If release notes are required, they follow the [release notes guidelines](https://github.com/prestodb/presto/wiki/Release-Notes-Guidelines). - [ ] Adequate tests were added if applicable. - [ ] CI passed. - [ ] If adding new dependencies, verified they have an [OpenSSF Scorecard](https://securityscorecards.dev/#the-checks) score of 5.0 or higher (or obtained explicit TSC approval for lower scores). ## Release Notes Please follow [release notes guidelines](https://github.com/prestodb/presto/wiki/Release-Notes-Guidelines) and fill in the release notes below. ``` == NO RELEASE NOTE == ```
1 parent a116f4b commit cd3e3c8

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

presto-main-base/src/main/java/com/facebook/presto/sql/relational/FunctionResolution.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,10 @@ public FunctionHandle lookupBuiltInFunction(String functionName, List<Type> inpu
438438
{
439439
return functionAndTypeResolver.lookupFunction(functionName, fromTypes(inputTypes));
440440
}
441+
442+
@Override
443+
public FunctionHandle lookupFunction(String catalog, String schema, String functionName, List<Type> inputTypes)
444+
{
445+
return functionAndTypeResolver.resolveFunction(Optional.empty(), Optional.empty(), QualifiedObjectName.valueOf(catalog, schema, functionName), fromTypes(inputTypes));
446+
}
441447
}

presto-main-base/src/test/java/com/facebook/presto/sql/relational/TestFunctionResolution.java

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
*/
1414
package com.facebook.presto.sql.relational;
1515

16+
import com.facebook.presto.common.QualifiedObjectName;
1617
import com.facebook.presto.common.type.ArrayType;
18+
import com.facebook.presto.common.type.StandardTypes;
19+
import com.facebook.presto.functionNamespace.SqlInvokedFunctionNamespaceManagerConfig;
20+
import com.facebook.presto.functionNamespace.execution.NoopSqlFunctionExecutor;
21+
import com.facebook.presto.functionNamespace.execution.SqlFunctionExecutors;
22+
import com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager;
1723
import com.facebook.presto.metadata.FunctionAndTypeManager;
24+
import com.facebook.presto.spi.function.FunctionImplementationType;
25+
import com.facebook.presto.spi.function.Parameter;
26+
import com.facebook.presto.spi.function.RoutineCharacteristics;
27+
import com.facebook.presto.spi.function.SqlInvokedFunction;
1828
import com.facebook.presto.spi.function.StandardFunctionResolution;
1929
import com.google.common.collect.ImmutableList;
30+
import com.google.common.collect.ImmutableMap;
2031
import org.testng.annotations.BeforeClass;
2132
import org.testng.annotations.Test;
2233

@@ -25,19 +36,28 @@
2536
import static com.facebook.presto.common.type.BigintType.BIGINT;
2637
import static com.facebook.presto.common.type.BooleanType.BOOLEAN;
2738
import static com.facebook.presto.common.type.DoubleType.DOUBLE;
39+
import static com.facebook.presto.common.type.TypeSignature.parseTypeSignature;
2840
import static com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager;
41+
import static com.facebook.presto.spi.function.FunctionImplementationType.THRIFT;
42+
import static com.facebook.presto.spi.function.FunctionVersion.notVersioned;
43+
import static com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC;
44+
import static com.facebook.presto.spi.function.RoutineCharacteristics.Language.SQL;
45+
import static com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT;
2946
import static org.testng.Assert.assertEquals;
3047
import static org.testng.Assert.assertFalse;
3148
import static org.testng.Assert.assertTrue;
3249

3350
public class TestFunctionResolution
3451
{
52+
private static final RoutineCharacteristics.Language JAVA = new RoutineCharacteristics.Language("java");
53+
3554
private FunctionResolution functionResolution;
55+
private FunctionAndTypeManager functionAndTypeManager;
3656

3757
@BeforeClass
3858
public void setup()
3959
{
40-
FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
60+
functionAndTypeManager = createTestFunctionAndTypeManager();
4161
functionResolution = new FunctionResolution(functionAndTypeManager.getFunctionAndTypeResolver());
4262
}
4363

@@ -72,5 +92,54 @@ public void testStandardFunctionResolution()
7292
// BuiltInFunction
7393
assertEquals(standardFunctionResolution.notFunction(), standardFunctionResolution.lookupBuiltInFunction("not", ImmutableList.of(BOOLEAN)));
7494
assertEquals(standardFunctionResolution.countFunction(), standardFunctionResolution.lookupBuiltInFunction("count", ImmutableList.of()));
95+
96+
// full qualified name
97+
assertEquals(standardFunctionResolution.notFunction(), standardFunctionResolution.lookupFunction("presto", "default", "not", ImmutableList.of(BOOLEAN)));
98+
assertEquals(standardFunctionResolution.countFunction(), standardFunctionResolution.lookupFunction("presto", "default", "count", ImmutableList.of()));
99+
}
100+
101+
@Test
102+
public void testLookupFunctionWithNonDefaultSchemaAndCatalog()
103+
{
104+
StandardFunctionResolution standardFunctionResolution = functionResolution;
105+
106+
functionAndTypeManager.addFunctionNamespace(
107+
"custom_catalog",
108+
new InMemoryFunctionNamespaceManager(
109+
"custom_catalog",
110+
new SqlFunctionExecutors(
111+
ImmutableMap.of(
112+
SQL, FunctionImplementationType.SQL,
113+
JAVA, THRIFT),
114+
new NoopSqlFunctionExecutor()),
115+
new SqlInvokedFunctionNamespaceManagerConfig().setSupportedFunctionLanguages("sql,java")));
116+
117+
QualifiedObjectName customNotFunction = QualifiedObjectName.valueOf("custom_catalog", "custom_schema", "custom_not");
118+
SqlInvokedFunction notFunc = new SqlInvokedFunction(
119+
customNotFunction,
120+
ImmutableList.of(new Parameter("x", parseTypeSignature(StandardTypes.BOOLEAN))),
121+
parseTypeSignature(StandardTypes.BOOLEAN),
122+
"custom_not(x)",
123+
RoutineCharacteristics.builder().setLanguage(JAVA).setDeterminism(DETERMINISTIC).setNullCallClause(RETURNS_NULL_ON_NULL_INPUT).build(),
124+
"",
125+
notVersioned());
126+
127+
QualifiedObjectName customCountFunction = QualifiedObjectName.valueOf("custom_catalog", "custom_schema", "custom_count");
128+
SqlInvokedFunction countFunc = new SqlInvokedFunction(
129+
customCountFunction,
130+
ImmutableList.of(),
131+
parseTypeSignature(StandardTypes.BIGINT),
132+
"custom_count()",
133+
RoutineCharacteristics.builder().setLanguage(JAVA).setNullCallClause(RETURNS_NULL_ON_NULL_INPUT).build(),
134+
"",
135+
notVersioned());
136+
137+
functionAndTypeManager.createFunction(notFunc, true);
138+
functionAndTypeManager.createFunction(countFunc, true);
139+
140+
assertEquals(notFunc.getFunctionId().getFunctionName(),
141+
functionAndTypeManager.getFunctionMetadata(standardFunctionResolution.lookupFunction("custom_catalog", "custom_schema", "custom_not", ImmutableList.of(BOOLEAN))).getName());
142+
assertEquals(countFunc.getFunctionId().getFunctionName(),
143+
functionAndTypeManager.getFunctionMetadata(standardFunctionResolution.lookupFunction("custom_catalog", "custom_schema", "custom_count", ImmutableList.of())).getName());
75144
}
76145
}

presto-spi/src/main/java/com/facebook/presto/spi/function/StandardFunctionResolution.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ public interface StandardFunctionResolution
9191
FunctionHandle approximateSetFunction(Type valueType);
9292

9393
FunctionHandle lookupBuiltInFunction(String functionName, List<Type> inputTypes);
94+
95+
FunctionHandle lookupFunction(String catalog, String schema, String functionName, List<Type> inputTypes);
9496
}

0 commit comments

Comments
 (0)