Search in sources :

Example 1 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class DruidAggregationProjectConverter method visitCall.

@Override
public DruidExpression visitCall(CallExpression call, Map<VariableReferenceExpression, DruidQueryGeneratorContext.Selection> context) {
    Optional<DruidExpression> basicCallHandlingResult = basicCallHandling(call, context);
    if (basicCallHandlingResult.isPresent()) {
        return basicCallHandlingResult.get();
    }
    FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(call.getFunctionHandle());
    Optional<OperatorType> operatorTypeOptional = functionMetadata.getOperatorType();
    if (operatorTypeOptional.isPresent()) {
        OperatorType operatorType = operatorTypeOptional.get();
        if (operatorType.isArithmeticOperator()) {
            return handleArithmeticExpression(call, operatorType, context);
        }
        if (operatorType.isComparisonOperator()) {
            throw new PrestoException(DRUID_PUSHDOWN_UNSUPPORTED_EXPRESSION, "Unsupported operator: " + call + " to pushdown for Druid connector.");
        }
    }
    return handleFunction(call, context);
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) PrestoException(com.facebook.presto.spi.PrestoException) OperatorType(com.facebook.presto.common.function.OperatorType)

Example 2 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class BuiltInTypeAndFunctionNamespaceManager method getFunctionMetadata.

@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();
    Optional<OperatorType> operatorType = tryGetOperatorType(signature.getName());
    if (operatorType.isPresent()) {
        return new FunctionMetadata(operatorType.get(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), JAVA, function.isDeterministic(), function.isCalledOnNullInput());
    } else if (function instanceof SqlInvokedFunction) {
        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());
    } else {
        return new FunctionMetadata(signature.getName(), signature.getArgumentTypes(), signature.getReturnType(), signature.getKind(), JAVA, function.isDeterministic(), function.isCalledOnNullInput());
    }
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeSignature(com.facebook.presto.common.type.TypeSignature) Parameter(com.facebook.presto.spi.function.Parameter) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) TypeParameter(com.facebook.presto.common.type.TypeParameter) PrestoException(com.facebook.presto.spi.PrestoException) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) OperatorType.tryGetOperatorType(com.facebook.presto.common.function.OperatorType.tryGetOperatorType) OperatorType(com.facebook.presto.common.function.OperatorType) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 3 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class NullIfCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments, Optional<Variable> outputBlockVariable) {
    Scope scope = generatorContext.getScope();
    RowExpression first = arguments.get(0);
    RowExpression second = arguments.get(1);
    LabelNode notMatch = new LabelNode("notMatch");
    // push first arg on the stack
    Variable firstValue = scope.createTempVariable(first.getType().getJavaType());
    BytecodeBlock block = new BytecodeBlock().comment("check if first arg is null").append(generatorContext.generate(first, Optional.empty())).append(ifWasNullPopAndGoto(scope, notMatch, void.class)).dup(first.getType().getJavaType()).putVariable(firstValue);
    Type firstType = first.getType();
    Type secondType = second.getType();
    // if (equal(cast(first as <common type>), cast(second as <common type>))
    FunctionAndTypeManager functionAndTypeManager = generatorContext.getFunctionManager();
    FunctionHandle equalFunction = functionAndTypeManager.resolveOperator(EQUAL, fromTypes(firstType, secondType));
    FunctionMetadata equalFunctionMetadata = functionAndTypeManager.getFunctionMetadata(equalFunction);
    JavaScalarFunctionImplementation equalsFunction = generatorContext.getFunctionManager().getJavaScalarFunctionImplementation(equalFunction);
    BytecodeNode equalsCall = generatorContext.generateCall(EQUAL.name(), equalsFunction, ImmutableList.of(cast(generatorContext, firstValue, firstType, equalFunctionMetadata.getArgumentTypes().get(0)), cast(generatorContext, generatorContext.generate(second, Optional.empty()), secondType, equalFunctionMetadata.getArgumentTypes().get(1))));
    BytecodeBlock conditionBlock = new BytecodeBlock().append(equalsCall).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, notMatch, void.class, boolean.class));
    // if first and second are equal, return null
    BytecodeBlock trueBlock = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pop(first.getType().getJavaType()).pushJavaDefault(first.getType().getJavaType());
    // else return first (which is still on the stack
    block.append(new IfStatement().condition(conditionBlock).ifTrue(trueBlock).ifFalse(notMatch));
    outputBlockVariable.ifPresent(output -> block.append(generateWrite(generatorContext, returnType, output)));
    return block;
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) JavaScalarFunctionImplementation(com.facebook.presto.spi.function.JavaScalarFunctionImplementation) IfStatement(com.facebook.presto.bytecode.control.IfStatement) CastType(com.facebook.presto.metadata.CastType) Type(com.facebook.presto.common.type.Type) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) RowExpression(com.facebook.presto.spi.relation.RowExpression) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle)

Example 4 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class WindowFunctionValidator method visitFunctionCall.

@Override
protected Void visitFunctionCall(FunctionCall functionCall, Analysis analysis) {
    requireNonNull(analysis, "analysis is null");
    FunctionMetadata functionMetadata = functionAndTypeManager.getFunctionMetadata(analysis.getFunctionHandle(functionCall));
    if (functionMetadata != null && functionMetadata.getFunctionKind() == WINDOW && !functionCall.getWindow().isPresent()) {
        throw new SemanticException(WINDOW_REQUIRES_OVER, functionCall, "Window function %s requires an OVER clause", functionMetadata.getName());
    }
    return super.visitFunctionCall(functionCall, analysis);
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata)

Example 5 with FunctionMetadata

use of com.facebook.presto.spi.function.FunctionMetadata in project presto by prestodb.

the class PinotAggregationProjectConverter method visitCall.

@Override
public PinotExpression visitCall(CallExpression call, Map<VariableReferenceExpression, PinotQueryGeneratorContext.Selection> context) {
    Optional<PinotExpression> basicCallHandlingResult = basicCallHandling(call, context);
    if (basicCallHandlingResult.isPresent()) {
        return basicCallHandlingResult.get();
    }
    FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(call.getFunctionHandle());
    Optional<OperatorType> operatorTypeOptional = functionMetadata.getOperatorType();
    if (operatorTypeOptional.isPresent()) {
        OperatorType operatorType = operatorTypeOptional.get();
        if (operatorType.isArithmeticOperator()) {
            return handleArithmeticExpression(call, operatorType, context);
        }
        if (operatorType.isComparisonOperator()) {
            throw new PinotException(PINOT_UNSUPPORTED_EXPRESSION, Optional.empty(), "Comparison operator not supported: " + call);
        }
    }
    return handleFunction(call, context);
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) PinotException(com.facebook.presto.pinot.PinotException) OperatorType(com.facebook.presto.common.function.OperatorType)

Aggregations

FunctionMetadata (com.facebook.presto.spi.function.FunctionMetadata)19 OperatorType (com.facebook.presto.common.function.OperatorType)9 TypeSignature (com.facebook.presto.common.type.TypeSignature)4 PrestoException (com.facebook.presto.spi.PrestoException)4 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)4 CallExpression (com.facebook.presto.spi.relation.CallExpression)4 ImmutableList (com.google.common.collect.ImmutableList)4 Type (com.facebook.presto.common.type.Type)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)3 List (java.util.List)3 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)2 RowExpression (com.facebook.presto.spi.relation.RowExpression)2 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 String.format (java.lang.String.format)2 MethodHandle (java.lang.invoke.MethodHandle)2 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2