Skip to content

Commit 464830f

Browse files
kevintang2022facebook-github-bot
authored andcommitted
Sidecar native built in demo 1
Summary: Demo 1 Differential Revision: D79301760
1 parent e0d3305 commit 464830f

30 files changed

+976
-244
lines changed

presto-main-base/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
<artifactId>bootstrap</artifactId>
9292
</dependency>
9393

94+
<dependency>
95+
<groupId>com.facebook.airlift</groupId>
96+
<artifactId>http-client</artifactId>
97+
</dependency>
98+
9499
<dependency>
95100
<groupId>io.airlift</groupId>
96101
<artifactId>aircompressor</artifactId>

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ public class BuiltInFunctionHandle
3030
implements FunctionHandle
3131
{
3232
private final Signature signature;
33+
private final boolean isBuiltInNativeFunction;
34+
private final boolean isBuiltInPluginFunction;
3335

3436
@JsonCreator
35-
public BuiltInFunctionHandle(@JsonProperty("signature") Signature signature)
37+
public BuiltInFunctionHandle(
38+
@JsonProperty("signature") Signature signature,
39+
@JsonProperty("isBuiltInNativeFunction") boolean isBuiltInNativeFunction,
40+
@JsonProperty("isBuiltInPluginFunction") boolean isBuiltInPluginFunction)
3641
{
3742
this.signature = requireNonNull(signature, "signature is null");
3843
checkArgument(signature.getTypeVariableConstraints().isEmpty(), "%s has unbound type parameters", signature);
44+
this.isBuiltInNativeFunction = isBuiltInNativeFunction;
45+
this.isBuiltInPluginFunction = isBuiltInPluginFunction;
3946
}
4047

4148
@JsonProperty
@@ -62,6 +69,20 @@ public List<TypeSignature> getArgumentTypes()
6269
return signature.getArgumentTypes();
6370
}
6471

72+
@JsonProperty
73+
@Override
74+
public boolean isBuiltInNativeFunction()
75+
{
76+
return isBuiltInNativeFunction;
77+
}
78+
79+
@JsonProperty
80+
@Override
81+
public boolean isBuiltInPluginFunction()
82+
{
83+
return isBuiltInPluginFunction;
84+
}
85+
6586
@Override
6687
public CatalogSchemaName getCatalogSchemaName()
6788
{
@@ -78,13 +99,14 @@ public boolean equals(Object o)
7899
return false;
79100
}
80101
BuiltInFunctionHandle that = (BuiltInFunctionHandle) o;
81-
return Objects.equals(signature, that.signature);
102+
return Objects.equals(signature, that.signature)
103+
&& Objects.equals(isBuiltInNativeFunction, that.isBuiltInNativeFunction);
82104
}
83105

84106
@Override
85107
public int hashCode()
86108
{
87-
return Objects.hash(signature);
109+
return Objects.hash(signature, isBuiltInNativeFunction);
88110
}
89111

90112
@Override
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.metadata;
15+
16+
import com.facebook.airlift.log.Logger;
17+
import com.facebook.presto.spi.PrestoException;
18+
import com.facebook.presto.spi.function.FunctionHandle;
19+
import com.facebook.presto.spi.function.FunctionMetadata;
20+
import com.facebook.presto.spi.function.Parameter;
21+
import com.facebook.presto.spi.function.Signature;
22+
import com.facebook.presto.spi.function.SqlFunction;
23+
import com.facebook.presto.spi.function.SqlInvokedFunction;
24+
import com.google.common.util.concurrent.UncheckedExecutionException;
25+
26+
import javax.inject.Provider;
27+
28+
import java.util.Collection;
29+
import java.util.List;
30+
import java.util.stream.Collectors;
31+
32+
import static com.facebook.presto.spi.function.FunctionImplementationType.CPP;
33+
import static com.google.common.base.Preconditions.checkArgument;
34+
import static com.google.common.base.Throwables.throwIfInstanceOf;
35+
import static com.google.common.collect.ImmutableList.toImmutableList;
36+
import static java.util.Objects.requireNonNull;
37+
38+
public class BuiltInNativeFunctionNamespaceManager
39+
extends BuiltInSpecialFunctionNamespaceManager
40+
{
41+
private static final Logger log = Logger.get(BuiltInNativeFunctionNamespaceManager.class);
42+
43+
private final Provider<NativeFunctionRegistryTool> nativeFunctionRegistryToolProvider;
44+
45+
public BuiltInNativeFunctionNamespaceManager(FunctionAndTypeManager functionAndTypeManager, Provider<NativeFunctionRegistryTool> nativeFunctionRegistryToolProvider)
46+
{
47+
super(functionAndTypeManager);
48+
this.nativeFunctionRegistryToolProvider = requireNonNull(nativeFunctionRegistryToolProvider, "nativeFunctionRegistryToolProvider is null");
49+
}
50+
51+
public synchronized void registerNativeFunctions()
52+
{
53+
// only register functions once
54+
if (!this.functions.list().isEmpty()) {
55+
return;
56+
}
57+
58+
List<SqlFunction> allNativeFunctions = nativeFunctionRegistryToolProvider
59+
.get()
60+
.getNativeFunctionSignatureMap()
61+
.getUDFSignatureMap()
62+
.entrySet()
63+
.stream()
64+
.flatMap(entry -> entry.getValue().stream()
65+
.map(metaInfo -> NativeSidecarFunctionUtil.createSqlInvokedFunction(entry.getKey(), metaInfo)))
66+
.collect(Collectors.toList());
67+
this.functions = new FunctionMap(this.functions, allNativeFunctions);
68+
}
69+
70+
@Override
71+
public FunctionHandle getFunctionHandle(Signature signature)
72+
{
73+
return new BuiltInFunctionHandle(signature, true, false);
74+
}
75+
76+
@Override
77+
public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
78+
{
79+
checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
80+
Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
81+
SpecializedFunctionKey functionKey;
82+
try {
83+
functionKey = specializedFunctionKeyCache.getUnchecked(signature);
84+
}
85+
catch (UncheckedExecutionException e) {
86+
throwIfInstanceOf(e.getCause(), PrestoException.class);
87+
throw e;
88+
}
89+
SqlFunction function = functionKey.getFunction();
90+
checkArgument(function instanceof SqlInvokedFunction, "BuiltInPluginFunctionNamespaceManager only support SqlInvokedFunctions");
91+
SqlInvokedFunction sqlFunction = (SqlInvokedFunction) function;
92+
List<String> argumentNames = sqlFunction.getParameters().stream().map(Parameter::getName).collect(toImmutableList());
93+
return new FunctionMetadata(
94+
signature.getName(),
95+
signature.getArgumentTypes(),
96+
argumentNames,
97+
signature.getReturnType(),
98+
signature.getKind(),
99+
sqlFunction.getRoutineCharacteristics().getLanguage(),
100+
CPP,
101+
function.isDeterministic(),
102+
function.isCalledOnNullInput(),
103+
sqlFunction.getVersion(),
104+
sqlFunction.getComplexTypeFunctionDescriptor());
105+
}
106+
107+
@Override
108+
protected synchronized void checkForNamingConflicts(Collection<? extends SqlFunction> functions)
109+
{
110+
}
111+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.metadata;
15+
16+
import com.facebook.presto.spi.PrestoException;
17+
import com.facebook.presto.spi.function.FunctionHandle;
18+
import com.facebook.presto.spi.function.FunctionMetadata;
19+
import com.facebook.presto.spi.function.Parameter;
20+
import com.facebook.presto.spi.function.Signature;
21+
import com.facebook.presto.spi.function.SqlFunction;
22+
import com.facebook.presto.spi.function.SqlInvokedFunction;
23+
import com.google.common.util.concurrent.UncheckedExecutionException;
24+
25+
import java.util.Collection;
26+
import java.util.List;
27+
28+
import static com.facebook.presto.spi.function.FunctionImplementationType.SQL;
29+
import static com.google.common.base.Preconditions.checkArgument;
30+
import static com.google.common.base.Throwables.throwIfInstanceOf;
31+
import static com.google.common.collect.ImmutableList.toImmutableList;
32+
33+
public class BuiltInPluginFunctionNamespaceManager
34+
extends BuiltInSpecialFunctionNamespaceManager
35+
{
36+
public BuiltInPluginFunctionNamespaceManager(FunctionAndTypeManager functionAndTypeManager)
37+
{
38+
super(functionAndTypeManager);
39+
}
40+
41+
public synchronized void registerPluginFunctions(List<? extends SqlFunction> functions)
42+
{
43+
checkForNamingConflicts(functions);
44+
this.functions = new FunctionMap(this.functions, functions);
45+
}
46+
47+
@Override
48+
public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
49+
{
50+
checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
51+
Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
52+
SpecializedFunctionKey functionKey;
53+
try {
54+
functionKey = specializedFunctionKeyCache.getUnchecked(signature);
55+
}
56+
catch (UncheckedExecutionException e) {
57+
throwIfInstanceOf(e.getCause(), PrestoException.class);
58+
throw e;
59+
}
60+
SqlFunction function = functionKey.getFunction();
61+
checkArgument(function instanceof SqlInvokedFunction, "BuiltInPluginFunctionNamespaceManager only support SqlInvokedFunctions");
62+
SqlInvokedFunction sqlFunction = (SqlInvokedFunction) function;
63+
List<String> argumentNames = sqlFunction.getParameters().stream().map(Parameter::getName).collect(toImmutableList());
64+
return new FunctionMetadata(
65+
signature.getName(),
66+
signature.getArgumentTypes(),
67+
argumentNames,
68+
signature.getReturnType(),
69+
signature.getKind(),
70+
sqlFunction.getRoutineCharacteristics().getLanguage(),
71+
SQL,
72+
function.isDeterministic(),
73+
function.isCalledOnNullInput(),
74+
sqlFunction.getVersion(),
75+
sqlFunction.getComplexTypeFunctionDescriptor());
76+
}
77+
78+
@Override
79+
public FunctionHandle getFunctionHandle(Signature signature)
80+
{
81+
return new BuiltInFunctionHandle(signature, false, true);
82+
}
83+
84+
@Override
85+
protected synchronized void checkForNamingConflicts(Collection<? extends SqlFunction> functions)
86+
{
87+
for (SqlFunction function : functions) {
88+
for (SqlFunction existingFunction : this.functions.list()) {
89+
checkArgument(!function.getSignature().equals(existingFunction.getSignature()), "Function already registered: %s", function.getSignature());
90+
}
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)