Skip to content

Commit f31b05d

Browse files
committed
Refactor built in function
1 parent 4f0c792 commit f31b05d

File tree

10 files changed

+84
-40
lines changed

10 files changed

+84
-40
lines changed

presto-main-base/src/main/java/com/facebook/presto/metadata/BuiltInFunctionHandle.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.facebook.presto.common.CatalogSchemaName;
1717
import com.facebook.presto.common.type.TypeSignature;
18+
import com.facebook.presto.spi.function.BuiltInType;
1819
import com.facebook.presto.spi.function.FunctionHandle;
1920
import com.facebook.presto.spi.function.FunctionKind;
2021
import com.facebook.presto.spi.function.Signature;
@@ -30,19 +31,16 @@ public class BuiltInFunctionHandle
3031
implements FunctionHandle
3132
{
3233
private final Signature signature;
33-
private final boolean isBuiltInNativeFunction;
34-
private final boolean isBuiltInPluginFunction;
34+
private final BuiltInType builtInType;
3535

3636
@JsonCreator
3737
public BuiltInFunctionHandle(
3838
@JsonProperty("signature") Signature signature,
39-
@JsonProperty("isBuiltInNativeFunction") boolean isBuiltInNativeFunction,
40-
@JsonProperty("isBuiltInPluginFunction") boolean isBuiltInPluginFunction)
39+
@JsonProperty("builtInType") BuiltInType builtInType)
4140
{
4241
this.signature = requireNonNull(signature, "signature is null");
4342
checkArgument(signature.getTypeVariableConstraints().isEmpty(), "%s has unbound type parameters", signature);
44-
this.isBuiltInNativeFunction = isBuiltInNativeFunction;
45-
this.isBuiltInPluginFunction = isBuiltInPluginFunction;
43+
this.builtInType = builtInType;
4644
}
4745

4846
@JsonProperty
@@ -71,16 +69,9 @@ public List<TypeSignature> getArgumentTypes()
7169

7270
@JsonProperty
7371
@Override
74-
public boolean isBuiltInNativeFunction()
72+
public BuiltInType getBuiltInType()
7573
{
76-
return isBuiltInNativeFunction;
77-
}
78-
79-
@JsonProperty
80-
@Override
81-
public boolean isBuiltInPluginFunction()
82-
{
83-
return isBuiltInPluginFunction;
74+
return builtInType;
8475
}
8576

8677
@Override
@@ -100,13 +91,13 @@ public boolean equals(Object o)
10091
}
10192
BuiltInFunctionHandle that = (BuiltInFunctionHandle) o;
10293
return Objects.equals(signature, that.signature)
103-
&& Objects.equals(isBuiltInNativeFunction, that.isBuiltInNativeFunction);
94+
&& Objects.equals(builtInType, that.builtInType);
10495
}
10596

10697
@Override
10798
public int hashCode()
10899
{
109-
return Objects.hash(signature, isBuiltInNativeFunction);
100+
return Objects.hash(signature, builtInType);
110101
}
111102

112103
@Override

presto-main-base/src/main/java/com/facebook/presto/metadata/BuiltInNativeFunctionNamespaceManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.facebook.airlift.log.Logger;
1717
import com.facebook.presto.spi.PrestoException;
18+
import com.facebook.presto.spi.function.BuiltInType;
1819
import com.facebook.presto.spi.function.FunctionHandle;
1920
import com.facebook.presto.spi.function.FunctionMetadata;
2021
import com.facebook.presto.spi.function.Parameter;
@@ -70,7 +71,7 @@ public synchronized void registerNativeFunctions()
7071
@Override
7172
public FunctionHandle getFunctionHandle(Signature signature)
7273
{
73-
return new BuiltInFunctionHandle(signature, true, false);
74+
return new BuiltInFunctionHandle(signature, BuiltInType.NATIVE);
7475
}
7576

7677
@Override

presto-main-base/src/main/java/com/facebook/presto/metadata/BuiltInPluginFunctionNamespaceManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.facebook.presto.metadata;
1515

1616
import com.facebook.presto.spi.PrestoException;
17+
import com.facebook.presto.spi.function.BuiltInType;
1718
import com.facebook.presto.spi.function.FunctionHandle;
1819
import com.facebook.presto.spi.function.FunctionMetadata;
1920
import com.facebook.presto.spi.function.Parameter;
@@ -78,7 +79,7 @@ public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
7879
@Override
7980
public FunctionHandle getFunctionHandle(Signature signature)
8081
{
81-
return new BuiltInFunctionHandle(signature, false, true);
82+
return new BuiltInFunctionHandle(signature, BuiltInType.PLUGIN);
8283
}
8384

8485
@Override

presto-main-base/src/main/java/com/facebook/presto/metadata/BuiltInTypeAndFunctionNamespaceManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
import com.facebook.presto.spi.PrestoException;
226226
import com.facebook.presto.spi.function.AggregationFunctionImplementation;
227227
import com.facebook.presto.spi.function.AlterRoutineCharacteristics;
228+
import com.facebook.presto.spi.function.BuiltInType;
228229
import com.facebook.presto.spi.function.FunctionHandle;
229230
import com.facebook.presto.spi.function.FunctionMetadata;
230231
import com.facebook.presto.spi.function.FunctionNamespaceManager;
@@ -1129,7 +1130,7 @@ public Collection<SqlFunction> getFunctions(Optional<? extends FunctionNamespace
11291130
@Override
11301131
public FunctionHandle getFunctionHandle(Optional<? extends FunctionNamespaceTransactionHandle> transactionHandle, Signature signature)
11311132
{
1132-
return new BuiltInFunctionHandle(signature, false, false);
1133+
return new BuiltInFunctionHandle(signature, BuiltInType.NONE);
11331134
}
11341135

11351136
@Override

presto-main-base/src/main/java/com/facebook/presto/metadata/FunctionAndTypeManager.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.facebook.presto.spi.StandardErrorCode;
4040
import com.facebook.presto.spi.function.AggregationFunctionImplementation;
4141
import com.facebook.presto.spi.function.AlterRoutineCharacteristics;
42+
import com.facebook.presto.spi.function.BuiltInType;
4243
import com.facebook.presto.spi.function.FunctionHandle;
4344
import com.facebook.presto.spi.function.FunctionImplementationType;
4445
import com.facebook.presto.spi.function.FunctionMetadata;
@@ -360,12 +361,12 @@ public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
360361
if (functionHandle.getCatalogSchemaName().equals(SESSION_NAMESPACE)) {
361362
return ((SessionFunctionHandle) functionHandle).getFunctionMetadata();
362363
}
363-
if (functionHandle.isBuiltInNativeFunction()) {
364-
return builtInNativeFunctionNamespaceManager.getFunctionMetadata(functionHandle);
365-
}
366-
if (functionHandle.isBuiltInPluginFunction()) {
364+
if (functionHandle.getBuiltInType().equals(BuiltInType.PLUGIN)) {
367365
return builtInPluginFunctionNamespaceManager.getFunctionMetadata(functionHandle);
368366
}
367+
if (functionHandle.getBuiltInType().equals(BuiltInType.NATIVE)) {
368+
return builtInNativeFunctionNamespaceManager.getFunctionMetadata(functionHandle);
369+
}
369370

370371
Optional<FunctionNamespaceManager<?>> functionNamespaceManager = getServingFunctionNamespaceManager(functionHandle.getCatalogSchemaName());
371372
checkArgument(functionNamespaceManager.isPresent(), "Cannot find function namespace for '%s'", functionHandle.getCatalogSchemaName());
@@ -640,12 +641,13 @@ public ScalarFunctionImplementation getScalarFunctionImplementation(FunctionHand
640641
if (functionHandle.getCatalogSchemaName().equals(SESSION_NAMESPACE)) {
641642
return ((SessionFunctionHandle) functionHandle).getScalarFunctionImplementation();
642643
}
643-
if (functionHandle.isBuiltInNativeFunction()) {
644-
return builtInNativeFunctionNamespaceManager.getScalarFunctionImplementation(functionHandle);
645-
}
646-
if (functionHandle.isBuiltInPluginFunction()) {
644+
if (functionHandle.getBuiltInType().equals(BuiltInType.PLUGIN)) {
647645
return builtInPluginFunctionNamespaceManager.getScalarFunctionImplementation(functionHandle);
648646
}
647+
if (functionHandle.getBuiltInType().equals(BuiltInType.NATIVE)) {
648+
return builtInNativeFunctionNamespaceManager.getScalarFunctionImplementation(functionHandle);
649+
}
650+
649651
Optional<FunctionNamespaceManager<?>> functionNamespaceManager = getServingFunctionNamespaceManager(functionHandle.getCatalogSchemaName());
650652
checkArgument(functionNamespaceManager.isPresent(), "Cannot find function namespace for '%s'", functionHandle.getCatalogSchemaName());
651653
return functionNamespaceManager.get().getScalarFunctionImplementation(functionHandle);
@@ -844,7 +846,7 @@ private FunctionHandle resolveFunctionInternal(Optional<TransactionId> transacti
844846
// verify we have one parameter of the proper type
845847
checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
846848

847-
return new BuiltInFunctionHandle(getMagicLiteralFunctionSignature(type), false, false);
849+
return new BuiltInFunctionHandle(getMagicLiteralFunctionSignature(type), BuiltInType.NONE);
848850
}
849851

850852
throw new PrestoException(FUNCTION_NOT_FOUND, constructFunctionNotFoundErrorMessage(

presto-main-base/src/test/java/com/facebook/presto/metadata/TestFunctionAndTypeManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.facebook.presto.common.type.TypeSignature;
2121
import com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation;
2222
import com.facebook.presto.operator.scalar.CustomFunctions;
23+
import com.facebook.presto.spi.function.BuiltInType;
2324
import com.facebook.presto.spi.function.FunctionHandle;
2425
import com.facebook.presto.spi.function.Parameter;
2526
import com.facebook.presto.spi.function.RoutineCharacteristics;
@@ -83,7 +84,7 @@ public void testIdentityCast()
8384
{
8485
FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
8586
FunctionHandle exactOperator = functionAndTypeManager.lookupCast(CastType.CAST, HYPER_LOG_LOG, HYPER_LOG_LOG);
86-
assertEquals(exactOperator, new BuiltInFunctionHandle(new Signature(CAST.getFunctionName(), SCALAR, HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature()), false, false));
87+
assertEquals(exactOperator, new BuiltInFunctionHandle(new Signature(CAST.getFunctionName(), SCALAR, HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature()), BuiltInType.NONE));
8788
}
8889

8990
@Test
@@ -444,7 +445,7 @@ public ResolveFunctionAssertion forParameters(String... parameters)
444445

445446
public ResolveFunctionAssertion returns(SignatureBuilder functionSignature)
446447
{
447-
FunctionHandle expectedFunction = new BuiltInFunctionHandle(functionSignature.name(TEST_FUNCTION_NAME).build(), false, false);
448+
FunctionHandle expectedFunction = new BuiltInFunctionHandle(functionSignature.name(TEST_FUNCTION_NAME).build(), BuiltInType.NONE);
448449
FunctionHandle actualFunction = resolveFunctionHandle();
449450
assertEquals(expectedFunction, actualFunction);
450451
return this;

presto-native-execution/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@
3636
<scope>test</scope>
3737
</dependency>
3838

39+
<dependency>
40+
<groupId>org.jetbrains</groupId>
41+
<artifactId>annotations</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.weakref</groupId>
46+
<artifactId>jmxutils</artifactId>
47+
</dependency>
48+
3949
<dependency>
4050
<groupId>io.airlift.tpch</groupId>
4151
<artifactId>tpch</artifactId>
@@ -232,6 +242,11 @@
232242
<scope>test</scope>
233243
</dependency>
234244

245+
<dependency>
246+
<groupId>com.facebook.presto</groupId>
247+
<artifactId>presto-parser</artifactId>
248+
</dependency>
249+
235250
<dependency>
236251
<groupId>com.facebook.presto</groupId>
237252
<artifactId>presto-spark-base</artifactId>

presto-native-execution/src/test/java/com/facebook/presto/nativeworker/PrestoNativeQueryRunnerUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ public HiveQueryRunnerBuilder setCoordinatorSidecarEnabled(boolean coordinatorSi
219219
return this;
220220
}
221221

222-
223222
public HiveQueryRunnerBuilder setWorkerSidecarEnabled(boolean workerSidecarEnabled)
224223
{
225224
this.workerSidecarEnabled = workerSidecarEnabled;
@@ -358,7 +357,7 @@ public QueryRunner build()
358357
Optional<BiFunction<Integer, URI, Process>> externalWorkerLauncher = Optional.empty();
359358
if (this.useExternalWorkerLauncher) {
360359
externalWorkerLauncher = getExternalWorkerLauncher("iceberg", serverBinary, cacheMaxSize, remoteFunctionServerUds,
361-
false, false, false,false, false);
360+
false, false, false, false, false);
362361
}
363362
return IcebergQueryRunner.builder()
364363
.setExtraProperties(extraProperties)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
package com.facebook.presto.spi.function;
15+
16+
import com.facebook.drift.annotations.ThriftEnum;
17+
import com.facebook.drift.annotations.ThriftEnumValue;
18+
19+
@ThriftEnum
20+
public enum BuiltInType
21+
{
22+
NONE(0),
23+
PLUGIN(1),
24+
NATIVE(2);
25+
26+
private final int value;
27+
28+
BuiltInType(int value)
29+
{
30+
this.value = value;
31+
}
32+
33+
@ThriftEnumValue
34+
public int getValue()
35+
{
36+
return value;
37+
}
38+
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ public interface FunctionHandle
3333
List<TypeSignature> getArgumentTypes();
3434

3535
// todo: fix this hack
36-
default boolean isBuiltInNativeFunction()
36+
default BuiltInType getBuiltInType()
3737
{
38-
return false;
39-
}
40-
41-
default boolean isBuiltInPluginFunction()
42-
{
43-
return false;
38+
return BuiltInType.NONE;
4439
}
4540
}

0 commit comments

Comments
 (0)