Skip to content
Closed
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
5 changes: 5 additions & 0 deletions presto-main-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
<artifactId>bootstrap</artifactId>
</dependency>

<dependency>
<groupId>com.facebook.airlift</groupId>
<artifactId>http-client</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>aircompressor</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.presto.common.CatalogSchemaName;
import com.facebook.presto.common.type.TypeSignature;
import com.facebook.presto.spi.function.BuiltInType;
import com.facebook.presto.spi.function.FunctionHandle;
import com.facebook.presto.spi.function.FunctionKind;
import com.facebook.presto.spi.function.Signature;
Expand All @@ -30,12 +31,16 @@ public class BuiltInFunctionHandle
implements FunctionHandle
{
private final Signature signature;
private final BuiltInType builtInType;

@JsonCreator
public BuiltInFunctionHandle(@JsonProperty("signature") Signature signature)
public BuiltInFunctionHandle(
@JsonProperty("signature") Signature signature,
@JsonProperty("builtInType") BuiltInType builtInType)
{
this.signature = requireNonNull(signature, "signature is null");
checkArgument(signature.getTypeVariableConstraints().isEmpty(), "%s has unbound type parameters", signature);
this.builtInType = builtInType;
}

@JsonProperty
Expand All @@ -62,6 +67,13 @@ public List<TypeSignature> getArgumentTypes()
return signature.getArgumentTypes();
}

@JsonProperty
@Override
public BuiltInType getBuiltInType()
{
return builtInType;
}

@Override
public CatalogSchemaName getCatalogSchemaName()
{
Expand All @@ -78,13 +90,14 @@ public boolean equals(Object o)
return false;
}
BuiltInFunctionHandle that = (BuiltInFunctionHandle) o;
return Objects.equals(signature, that.signature);
return Objects.equals(signature, that.signature)
&& Objects.equals(builtInType, that.builtInType);
}

@Override
public int hashCode()
{
return Objects.hash(signature);
return Objects.hash(signature, builtInType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed 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 com.facebook.presto.metadata;

import com.facebook.airlift.log.Logger;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.BuiltInType;
import com.facebook.presto.spi.function.FunctionHandle;
import com.facebook.presto.spi.function.FunctionMetadata;
import com.facebook.presto.spi.function.Parameter;
import com.facebook.presto.spi.function.Signature;
import com.facebook.presto.spi.function.SqlFunction;
import com.facebook.presto.spi.function.SqlInvokedFunction;
import com.google.common.util.concurrent.UncheckedExecutionException;

import javax.inject.Provider;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.facebook.presto.spi.function.FunctionImplementationType.CPP;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;

public class BuiltInNativeFunctionNamespaceManager
extends BuiltInSpecialFunctionNamespaceManager
{
private static final Logger log = Logger.get(BuiltInNativeFunctionNamespaceManager.class);

private final Provider<NativeFunctionRegistryTool> nativeFunctionRegistryToolProvider;

public BuiltInNativeFunctionNamespaceManager(FunctionAndTypeManager functionAndTypeManager, Provider<NativeFunctionRegistryTool> nativeFunctionRegistryToolProvider)
{
super(functionAndTypeManager);
this.nativeFunctionRegistryToolProvider = requireNonNull(nativeFunctionRegistryToolProvider, "nativeFunctionRegistryToolProvider is null");
}

public synchronized void registerNativeFunctions()
{
// only register functions once
if (!this.functions.list().isEmpty()) {
return;
}

List<SqlFunction> allNativeFunctions = nativeFunctionRegistryToolProvider
.get()
.getNativeFunctionSignatureMap()
.getUDFSignatureMap()
.entrySet()
.stream()
.flatMap(entry -> entry.getValue().stream()
.map(metaInfo -> NativeSidecarFunctionUtil.createSqlInvokedFunction(entry.getKey(), metaInfo)))
.collect(Collectors.toList());
this.functions = new FunctionMap(this.functions, allNativeFunctions);
}

@Override
public FunctionHandle getFunctionHandle(Signature signature)
{
return new BuiltInFunctionHandle(signature, BuiltInType.NATIVE);
}

@Override
public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
{
checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
SpecializedFunctionKey functionKey;
try {
functionKey = specializedFunctionKeyCache.getUnchecked(signature);
}
catch (UncheckedExecutionException e) {
throwIfInstanceOf(e.getCause(), PrestoException.class);
throw e;
}
SqlFunction function = functionKey.getFunction();
checkArgument(function instanceof SqlInvokedFunction, "BuiltInPluginFunctionNamespaceManager only support SqlInvokedFunctions");
SqlInvokedFunction sqlFunction = (SqlInvokedFunction) function;
List<String> argumentNames = sqlFunction.getParameters().stream().map(Parameter::getName).collect(toImmutableList());
return new FunctionMetadata(
signature.getName(),
signature.getArgumentTypes(),
argumentNames,
signature.getReturnType(),
signature.getKind(),
sqlFunction.getRoutineCharacteristics().getLanguage(),
CPP,
function.isDeterministic(),
function.isCalledOnNullInput(),
sqlFunction.getVersion(),
sqlFunction.getComplexTypeFunctionDescriptor());
}

@Override
protected synchronized void checkForNamingConflicts(Collection<? extends SqlFunction> functions)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed 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 com.facebook.presto.metadata;

import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.BuiltInType;
import com.facebook.presto.spi.function.FunctionHandle;
import com.facebook.presto.spi.function.FunctionMetadata;
import com.facebook.presto.spi.function.Parameter;
import com.facebook.presto.spi.function.Signature;
import com.facebook.presto.spi.function.SqlFunction;
import com.facebook.presto.spi.function.SqlInvokedFunction;
import com.google.common.util.concurrent.UncheckedExecutionException;

import java.util.Collection;
import java.util.List;

import static com.facebook.presto.spi.function.FunctionImplementationType.SQL;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static com.google.common.collect.ImmutableList.toImmutableList;

public class BuiltInPluginFunctionNamespaceManager
extends BuiltInSpecialFunctionNamespaceManager
{
public BuiltInPluginFunctionNamespaceManager(FunctionAndTypeManager functionAndTypeManager)
{
super(functionAndTypeManager);
}

public synchronized void registerPluginFunctions(List<? extends SqlFunction> functions)
{
checkForNamingConflicts(functions);
this.functions = new FunctionMap(this.functions, functions);
}

@Override
public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
{
checkArgument(functionHandle instanceof BuiltInFunctionHandle, "Expect BuiltInFunctionHandle");
Signature signature = ((BuiltInFunctionHandle) functionHandle).getSignature();
SpecializedFunctionKey functionKey;
try {
functionKey = specializedFunctionKeyCache.getUnchecked(signature);
}
catch (UncheckedExecutionException e) {
throwIfInstanceOf(e.getCause(), PrestoException.class);
throw e;
}
SqlFunction function = functionKey.getFunction();
checkArgument(function instanceof SqlInvokedFunction, "BuiltInPluginFunctionNamespaceManager only support SqlInvokedFunctions");
SqlInvokedFunction sqlFunction = (SqlInvokedFunction) function;
List<String> argumentNames = sqlFunction.getParameters().stream().map(Parameter::getName).collect(toImmutableList());
return new FunctionMetadata(
signature.getName(),
signature.getArgumentTypes(),
argumentNames,
signature.getReturnType(),
signature.getKind(),
sqlFunction.getRoutineCharacteristics().getLanguage(),
SQL,
function.isDeterministic(),
function.isCalledOnNullInput(),
sqlFunction.getVersion(),
sqlFunction.getComplexTypeFunctionDescriptor());
}

@Override
public FunctionHandle getFunctionHandle(Signature signature)
{
return new BuiltInFunctionHandle(signature, BuiltInType.PLUGIN);
}

@Override
protected synchronized void checkForNamingConflicts(Collection<? extends SqlFunction> functions)
{
for (SqlFunction function : functions) {
for (SqlFunction existingFunction : this.functions.list()) {
checkArgument(!function.getSignature().equals(existingFunction.getSignature()), "Function already registered: %s", function.getSignature());
}
}
}
}
Loading
Loading