use of com.facebook.presto.spi.function.SqlFunctionId in project presto by prestodb.
the class GrpcSqlFunctionExecutor method executeFunction.
@Override
public CompletableFuture<SqlFunctionResult> executeFunction(String source, RemoteScalarFunctionImplementation functionImplementation, Page input, List<Integer> channels, List<Type> argumentTypes, Type returnType) {
GrpcUdfPage grpcUdfPage = buildGrpcUdfPage(input, channels, grpcUdfConfigs.get(functionImplementation.getLanguage()).getGrpcUdfPageFormat());
SqlFunctionHandle functionHandle = functionImplementation.getFunctionHandle();
SqlFunctionId functionId = functionHandle.getFunctionId();
GrpcFunctionHandle grpcFunctionHandle = GrpcFunctionHandle.newBuilder().setFunctionName(functionId.getFunctionName().toString()).addAllArgumentTypes(functionId.getArgumentTypes().stream().map(TypeSignature::toString).collect(toImmutableList())).setReturnType(returnType.toString()).setVersion(functionHandle.getVersion()).build();
GrpcUdfRequest grpcUdfRequest = GrpcUdfRequest.newBuilder().setSource(source).setGrpcFunctionHandle(grpcFunctionHandle).setInputs(grpcUdfPage).build();
return invokeUdfWithRetry(futureStubs.get(functionImplementation.getLanguage()), grpcUdfRequest).thenApply(grpcResult -> toSqlFunctionResult(grpcResult));
}
use of com.facebook.presto.spi.function.SqlFunctionId in project presto by prestodb.
the class CreateFunctionTask method execute.
@Override
public ListenableFuture<?> execute(CreateFunction statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
Analysis analysis = analyzer.analyze(statement);
if (analysis.getFunctionHandles().values().stream().anyMatch(SqlFunctionHandle.class::isInstance)) {
throw new PrestoException(NOT_SUPPORTED, "Invoking a dynamically registered function in SQL function body is not supported");
}
SqlInvokedFunction function = createSqlInvokedFunction(statement, metadata, analysis);
if (statement.isTemporary()) {
addSessionFunction(session, new SqlFunctionId(function.getSignature().getName(), function.getSignature().getArgumentTypes()), function);
} else {
metadata.getFunctionAndTypeManager().createFunction(function, statement.isReplace());
}
return immediateFuture(null);
}
use of com.facebook.presto.spi.function.SqlFunctionId in project presto by prestodb.
the class TestDropFunctionTask method testDropTemporaryFunction.
@Test
public void testDropTemporaryFunction() {
String sqlString = "DROP TEMPORARY FUNCTION foo()";
SqlFunctionId signature = new SqlFunctionId(QualifiedObjectName.valueOf("presto.session.foo"), emptyList());
Session session = testSessionBuilder().addSessionFunction(signature, null).build();
Set<SqlFunctionId> removedSessionFunctions = executeAndGetRemovedSessionFunctions(sqlString, session);
assertTrue(removedSessionFunctions.contains(signature));
}
use of com.facebook.presto.spi.function.SqlFunctionId in project presto by prestodb.
the class PageFunctionCompiler method defineFilterClass.
private ClassDefinition defineFilterClass(SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, RowExpression filter, InputChannels inputChannels, CallSiteBinder callSiteBinder, boolean isOptimizeCommonSubExpression, Optional<String> classNameSuffix) {
ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), generateFilterClassName(classNameSuffix), type(Object.class), type(PageFilter.class));
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(classDefinition, callSiteBinder);
Map<LambdaDefinitionExpression, CompiledLambda> compiledLambdaMap = generateMethodsForLambda(classDefinition, callSiteBinder, cachedInstanceBinder, filter, metadata, sqlFunctionProperties, sessionFunctions);
// cse
Map<VariableReferenceExpression, CommonSubExpressionFields> cseFields = ImmutableMap.of();
RowExpressionCompiler compiler = new RowExpressionCompiler(classDefinition, callSiteBinder, cachedInstanceBinder, new FieldAndVariableReferenceCompiler(callSiteBinder, cseFields), metadata, sqlFunctionProperties, sessionFunctions, compiledLambdaMap);
if (isOptimizeCommonSubExpression) {
Map<Integer, Map<RowExpression, VariableReferenceExpression>> commonSubExpressionsByLevel = collectCSEByLevel(filter);
if (!commonSubExpressionsByLevel.isEmpty()) {
cseFields = declareCommonSubExpressionFields(classDefinition, commonSubExpressionsByLevel);
compiler = new RowExpressionCompiler(classDefinition, callSiteBinder, cachedInstanceBinder, new FieldAndVariableReferenceCompiler(callSiteBinder, cseFields), metadata, sqlFunctionProperties, sessionFunctions, compiledLambdaMap);
generateCommonSubExpressionMethods(classDefinition, compiler, commonSubExpressionsByLevel, cseFields);
Map<RowExpression, VariableReferenceExpression> commonSubExpressions = commonSubExpressionsByLevel.values().stream().flatMap(m -> m.entrySet().stream()).collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
filter = rewriteExpressionWithCSE(filter, commonSubExpressions);
if (log.isDebugEnabled()) {
log.debug("Extracted %d common sub-expressions", commonSubExpressions.size());
commonSubExpressions.entrySet().forEach(entry -> log.debug("\t%s = %s", entry.getValue(), entry.getKey()));
log.debug("Rewrote filter: %s", filter);
}
}
}
generateFilterMethod(classDefinition, compiler, filter, cseFields);
FieldDefinition selectedPositions = classDefinition.declareField(a(PRIVATE), "selectedPositions", boolean[].class);
generatePageFilterMethod(classDefinition, selectedPositions);
// isDeterministic
classDefinition.declareMethod(a(PUBLIC), "isDeterministic", type(boolean.class)).getBody().append(constantBoolean(determinismEvaluator.isDeterministic(filter))).retBoolean();
// getInputChannels
classDefinition.declareMethod(a(PUBLIC), "getInputChannels", type(InputChannels.class)).getBody().append(invoke(callSiteBinder.bind(inputChannels, InputChannels.class), "getInputChannels")).retObject();
// toString
String toStringResult = toStringHelper(classDefinition.getType().getJavaClassName()).add("filter", filter).toString();
classDefinition.declareMethod(a(PUBLIC), "toString", type(String.class)).getBody().append(invoke(callSiteBinder.bind(toStringResult, String.class), "toString")).retObject();
// constructor
MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC));
BytecodeBlock body = constructorDefinition.getBody();
Variable thisVariable = constructorDefinition.getThis();
body.comment("super();").append(thisVariable).invokeConstructor(Object.class).append(thisVariable.setField(selectedPositions, newArray(type(boolean[].class), 0)));
initializeCommonSubExpressionFields(cseFields.values(), thisVariable, body);
cachedInstanceBinder.generateInitializations(thisVariable, body);
body.ret();
return classDefinition;
}
use of com.facebook.presto.spi.function.SqlFunctionId in project presto by prestodb.
the class PageFunctionCompiler method compileProjections.
public List<Supplier<PageProjectionWithOutputs>> compileProjections(SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, List<? extends RowExpression> projections, boolean isOptimizeCommonSubExpression, Optional<String> classNameSuffix) {
if (isOptimizeCommonSubExpression) {
ImmutableList.Builder<Supplier<PageProjectionWithOutputs>> pageProjections = ImmutableList.builder();
ImmutableMap.Builder<RowExpression, Integer> expressionsWithPositionBuilder = ImmutableMap.builder();
for (int i = 0; i < projections.size(); i++) {
RowExpression projection = projections.get(i);
if (projection instanceof ConstantExpression || projection instanceof InputReferenceExpression) {
pageProjections.add(toPageProjectionWithOutputs(compileProjection(sqlFunctionProperties, sessionFunctions, projection, classNameSuffix), new int[] { i }));
} else {
expressionsWithPositionBuilder.put(projection, i);
}
}
Map<RowExpression, Integer> expressionsWithPosition = expressionsWithPositionBuilder.build();
Map<List<RowExpression>, Boolean> projectionsPartitionedByCSE = getExpressionsPartitionedByCSE(expressionsWithPosition.keySet(), MAX_PROJECTION_GROUP_SIZE);
for (Map.Entry<List<RowExpression>, Boolean> entry : projectionsPartitionedByCSE.entrySet()) {
if (entry.getValue()) {
pageProjections.add(toPageProjectionWithOutputs(compileProjectionCached(sqlFunctionProperties, sessionFunctions, entry.getKey(), true, classNameSuffix), toIntArray(entry.getKey().stream().map(expressionsWithPosition::get).collect(toImmutableList()))));
} else {
verify(entry.getKey().size() == 1, "Expect non-cse expression list to only have one element");
RowExpression projection = entry.getKey().get(0);
pageProjections.add(toPageProjectionWithOutputs(compileProjection(sqlFunctionProperties, sessionFunctions, projection, classNameSuffix), new int[] { expressionsWithPosition.get(projection) }));
}
}
return pageProjections.build();
}
return IntStream.range(0, projections.size()).mapToObj(outputChannel -> toPageProjectionWithOutputs(compileProjection(sqlFunctionProperties, sessionFunctions, projections.get(outputChannel), classNameSuffix), new int[] { outputChannel })).collect(toImmutableList());
}
Aggregations