Search in sources :

Example 51 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class JdbcFilterToSqlTranslator method translateCall.

@Override
public TranslatedExpression<JdbcExpression> translateCall(CallExpression call, Map<VariableReferenceExpression, ColumnHandle> context, RowExpressionTreeTranslator<JdbcExpression, Map<VariableReferenceExpression, ColumnHandle>> rowExpressionTreeTranslator) {
    List<TranslatedExpression<JdbcExpression>> translatedExpressions = call.getArguments().stream().map(expression -> rowExpressionTreeTranslator.rewrite(expression, context)).collect(toImmutableList());
    FunctionMetadata functionMetadata = functionMetadataManager.getFunctionMetadata(call.getFunctionHandle());
    try {
        return functionTranslator.translate(functionMetadata, call, translatedExpressions);
    } catch (Throwable t) {
    // no-op
    }
    return untranslated(call, translatedExpressions);
}
Also used : TranslatedExpression.untranslated(com.facebook.presto.expressions.translator.TranslatedExpression.untranslated) BooleanType(com.facebook.presto.common.type.BooleanType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression) IntegerType(com.facebook.presto.common.type.IntegerType) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) TimeWithTimeZoneType(com.facebook.presto.common.type.TimeWithTimeZoneType) CharType(com.facebook.presto.common.type.CharType) CallExpression(com.facebook.presto.spi.relation.CallExpression) RowExpressionTreeTranslator(com.facebook.presto.expressions.translator.RowExpressionTreeTranslator) FunctionMetadataManager(com.facebook.presto.spi.function.FunctionMetadataManager) Type(com.facebook.presto.common.type.Type) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) RowExpressionTranslator(com.facebook.presto.expressions.translator.RowExpressionTranslator) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) String.format(java.lang.String.format) TimeType(com.facebook.presto.common.type.TimeType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) SmallintType(com.facebook.presto.common.type.SmallintType) List(java.util.List) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Optional(java.util.Optional) DateType(com.facebook.presto.common.type.DateType) FunctionTranslator(com.facebook.presto.expressions.translator.FunctionTranslator) DoubleType(com.facebook.presto.common.type.DoubleType) TimestampType(com.facebook.presto.common.type.TimestampType) JdbcColumnHandle(com.facebook.presto.plugin.jdbc.JdbcColumnHandle) Joiner(com.google.common.base.Joiner) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) TranslatedExpression(com.facebook.presto.expressions.translator.TranslatedExpression)

Example 52 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class SubfieldExtractor method toSubfield.

private static Optional<Subfield> toSubfield(RowExpression expression, StandardFunctionResolution functionResolution, ExpressionOptimizer expressionOptimizer, ConnectorSession connectorSession) {
    List<Subfield.PathElement> elements = new ArrayList<>();
    while (true) {
        if (expression instanceof VariableReferenceExpression) {
            Collections.reverse(elements);
            return Optional.of(new Subfield(((VariableReferenceExpression) expression).getName(), unmodifiableList(elements)));
        }
        if (expression instanceof SpecialFormExpression && ((SpecialFormExpression) expression).getForm() == DEREFERENCE) {
            SpecialFormExpression dereferenceExpression = (SpecialFormExpression) expression;
            RowExpression base = dereferenceExpression.getArguments().get(0);
            RowType baseType = (RowType) base.getType();
            RowExpression indexExpression = expressionOptimizer.optimize(dereferenceExpression.getArguments().get(1), ExpressionOptimizer.Level.OPTIMIZED, connectorSession);
            if (indexExpression instanceof ConstantExpression) {
                Object index = ((ConstantExpression) indexExpression).getValue();
                if (index instanceof Number) {
                    Optional<String> fieldName = baseType.getFields().get(((Number) index).intValue()).getName();
                    if (fieldName.isPresent()) {
                        elements.add(new Subfield.NestedField(fieldName.get()));
                        expression = base;
                        continue;
                    }
                }
            }
            return Optional.empty();
        }
        if (expression instanceof CallExpression && functionResolution.isSubscriptFunction(((CallExpression) expression).getFunctionHandle())) {
            List<RowExpression> arguments = ((CallExpression) expression).getArguments();
            RowExpression indexExpression = expressionOptimizer.optimize(arguments.get(1), ExpressionOptimizer.Level.OPTIMIZED, connectorSession);
            if (indexExpression instanceof ConstantExpression) {
                Object index = ((ConstantExpression) indexExpression).getValue();
                if (index instanceof Number) {
                    elements.add(new Subfield.LongSubscript(((Number) index).longValue()));
                    expression = arguments.get(0);
                    continue;
                }
                if (isVarcharType(indexExpression.getType())) {
                    elements.add(new Subfield.StringSubscript(((Slice) index).toStringUtf8()));
                    expression = arguments.get(0);
                    continue;
                }
            }
            return Optional.empty();
        }
        return Optional.empty();
    }
}
Also used : ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.spi.relation.RowExpression) RowType(com.facebook.presto.common.type.RowType) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Slice(io.airlift.slice.Slice) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) Subfield(com.facebook.presto.common.Subfield)

Example 53 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class RowExpressionVerifier method visitComparisonExpression.

@Override
protected Boolean visitComparisonExpression(ComparisonExpression expected, RowExpression actual) {
    if (actual instanceof CallExpression) {
        FunctionMetadata functionMetadata = metadata.getFunctionAndTypeManager().getFunctionMetadata(((CallExpression) actual).getFunctionHandle());
        if (!functionMetadata.getOperatorType().isPresent() || !functionMetadata.getOperatorType().get().isComparisonOperator()) {
            return false;
        }
        OperatorType actualOperatorType = functionMetadata.getOperatorType().get();
        OperatorType expectedOperatorType = getOperatorType(expected.getOperator());
        if (expectedOperatorType.equals(actualOperatorType)) {
            if (actualOperatorType == EQUAL) {
                return (process(expected.getLeft(), ((CallExpression) actual).getArguments().get(0)) && process(expected.getRight(), ((CallExpression) actual).getArguments().get(1))) || (process(expected.getLeft(), ((CallExpression) actual).getArguments().get(1)) && process(expected.getRight(), ((CallExpression) actual).getArguments().get(0)));
            }
            // TODO support other comparison operators
            return process(expected.getLeft(), ((CallExpression) actual).getArguments().get(0)) && process(expected.getRight(), ((CallExpression) actual).getArguments().get(1));
        }
    }
    return false;
}
Also used : FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) CallExpression(com.facebook.presto.spi.relation.CallExpression) OperatorType(com.facebook.presto.common.function.OperatorType)

Example 54 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class RowExpressionVerifier method visitCast.

@Override
protected Boolean visitCast(Cast expected, RowExpression actual) {
    // TODO: clean up cast path
    if (actual instanceof ConstantExpression && expected.getExpression() instanceof Literal && expected.getType().equals(actual.getType().toString())) {
        Literal literal = (Literal) expected.getExpression();
        if (literal instanceof StringLiteral) {
            Object value = LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actual);
            String actualString = value instanceof Slice ? ((Slice) value).toStringUtf8() : String.valueOf(value);
            return ((StringLiteral) literal).getValue().equals(actualString);
        }
        return getValueFromLiteral(literal).equals(String.valueOf(LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actual)));
    }
    if (actual instanceof VariableReferenceExpression && expected.getExpression() instanceof SymbolReference && expected.getType().equals(actual.getType().toString())) {
        return visitSymbolReference((SymbolReference) expected.getExpression(), actual);
    }
    if (!(actual instanceof CallExpression) || !functionResolution.isCastFunction(((CallExpression) actual).getFunctionHandle())) {
        return false;
    }
    if (!expected.getType().equalsIgnoreCase(actual.getType().toString()) && !(expected.getType().toLowerCase(ENGLISH).equals(VARCHAR) && actual.getType().getTypeSignature().getBase().equals(VARCHAR))) {
        return false;
    }
    return process(expected.getExpression(), ((CallExpression) actual).getArguments().get(0));
}
Also used : StringLiteral(com.facebook.presto.sql.tree.StringLiteral) Slice(io.airlift.slice.Slice) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) Literal(com.facebook.presto.sql.tree.Literal) DecimalLiteral(com.facebook.presto.sql.tree.DecimalLiteral) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 55 with CallExpression

use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.

the class TestTypeValidator method testInvalidAggregationFunctionSignature.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of variable 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidAggregationFunctionSignature() {
    VariableReferenceExpression aggregationVariable = variableAllocator.newVariable("sum", DOUBLE);
    PlanNode node = new AggregationNode(Optional.empty(), newId(), baseTableScan, ImmutableMap.of(aggregationVariable, new Aggregation(new CallExpression("sum", // should be DOUBLE
    FUNCTION_MANAGER.lookupFunction("sum", fromTypes(BIGINT)), DOUBLE, ImmutableList.of(variableC)), Optional.empty(), Optional.empty(), false, Optional.empty())), singleGroupingSet(ImmutableList.of(variableA, variableB)), ImmutableList.of(), SINGLE, Optional.empty(), Optional.empty());
    assertTypesValid(node);
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) PlanNode(com.facebook.presto.spi.plan.PlanNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Aggregations

CallExpression (com.facebook.presto.spi.relation.CallExpression)64 RowExpression (com.facebook.presto.spi.relation.RowExpression)33 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)33 Test (org.testng.annotations.Test)22 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)20 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)19 ImmutableList (com.google.common.collect.ImmutableList)18 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)16 Type (com.facebook.presto.common.type.Type)14 Map (java.util.Map)14 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)13 ImmutableMap (com.google.common.collect.ImmutableMap)13 Optional (java.util.Optional)12 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)11 OperatorType (com.facebook.presto.common.function.OperatorType)10 Aggregation (com.facebook.presto.spi.plan.AggregationNode.Aggregation)10 PlanNode (com.facebook.presto.spi.plan.PlanNode)10 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)10 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)10 Page (com.facebook.presto.common.Page)8