Search in sources :

Example 6 with ConstantExpression

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

the class ActualProperties method translateRowExpression.

public ActualProperties translateRowExpression(Map<VariableReferenceExpression, RowExpression> assignments, TypeProvider types) {
    Map<VariableReferenceExpression, VariableReferenceExpression> inputToOutputVariables = new HashMap<>();
    for (Map.Entry<VariableReferenceExpression, RowExpression> assignment : assignments.entrySet()) {
        RowExpression expression = assignment.getValue();
        if (isExpression(expression)) {
            if (castToExpression(expression) instanceof SymbolReference) {
                inputToOutputVariables.put(toVariableReference(castToExpression(expression), types), assignment.getKey());
            }
        } else {
            if (expression instanceof VariableReferenceExpression) {
                inputToOutputVariables.put((VariableReferenceExpression) expression, assignment.getKey());
            }
        }
    }
    Map<VariableReferenceExpression, ConstantExpression> translatedConstants = new HashMap<>();
    for (Map.Entry<VariableReferenceExpression, ConstantExpression> entry : constants.entrySet()) {
        if (inputToOutputVariables.containsKey(entry.getKey())) {
            translatedConstants.put(inputToOutputVariables.get(entry.getKey()), entry.getValue());
        }
    }
    ImmutableMap.Builder<VariableReferenceExpression, RowExpression> inputToOutputMappings = ImmutableMap.builder();
    inputToOutputMappings.putAll(inputToOutputVariables);
    constants.entrySet().stream().filter(entry -> !inputToOutputVariables.containsKey(entry.getKey())).forEach(inputToOutputMappings::put);
    return builder().global(global.translateRowExpression(inputToOutputMappings.build(), assignments, types)).local(LocalProperties.translate(localProperties, variable -> Optional.ofNullable(inputToOutputVariables.get(variable)))).constants(translatedConstants).build();
}
Also used : SINGLE_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION) Iterables.transform(com.google.common.collect.Iterables.transform) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) Function(java.util.function.Function) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) MoreLists.filteredCopy(com.facebook.presto.util.MoreLists.filteredCopy) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) LocalProperty(com.facebook.presto.spi.LocalProperty) Partitioning(com.facebook.presto.sql.planner.Partitioning) RowExpression(com.facebook.presto.spi.relation.RowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Session(com.facebook.presto.Session) Collection(java.util.Collection) Set(java.util.Set) ConstantProperty(com.facebook.presto.spi.ConstantProperty) SOURCE_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.SOURCE_DISTRIBUTION) Objects(java.util.Objects) PlannerUtils.toVariableReference(com.facebook.presto.sql.planner.PlannerUtils.toVariableReference) List(java.util.List) PartitioningHandle(com.facebook.presto.sql.planner.PartitioningHandle) Optional(java.util.Optional) Immutable(javax.annotation.concurrent.Immutable) Metadata(com.facebook.presto.metadata.Metadata) COORDINATOR_DISTRIBUTION(com.facebook.presto.sql.planner.SystemPartitioningHandle.COORDINATOR_DISTRIBUTION) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) HashMap(java.util.HashMap) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) ImmutableMap(com.google.common.collect.ImmutableMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with ConstantExpression

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

the class Partitioning method translateToCoalesce.

// Maps VariableReferenceExpression in both partitions to an COALESCE expression, keeps constant arguments unchanged.
public Optional<Partitioning> translateToCoalesce(Partitioning other, Metadata metadata, Session session) {
    checkArgument(arePartitionHandlesCompatibleForCoalesce(this.handle, other.handle, metadata, session), "incompatible partitioning handles: cannot coalesce %s and %s", this.handle, other.handle);
    checkArgument(this.arguments.size() == other.arguments.size(), "incompatible number of partitioning arguments: %s != %s", this.arguments.size(), other.arguments.size());
    ImmutableList.Builder<RowExpression> arguments = ImmutableList.builder();
    for (int i = 0; i < this.arguments.size(); i++) {
        RowExpression leftArgument = this.arguments.get(i);
        RowExpression rightArgument = other.arguments.get(i);
        if (leftArgument instanceof ConstantExpression) {
            arguments.add(leftArgument);
        } else if (rightArgument instanceof ConstantExpression) {
            arguments.add(rightArgument);
        } else if (leftArgument instanceof VariableReferenceExpression && rightArgument instanceof VariableReferenceExpression) {
            VariableReferenceExpression leftVariable = (VariableReferenceExpression) leftArgument;
            VariableReferenceExpression rightVariable = (VariableReferenceExpression) rightArgument;
            checkArgument(leftVariable.getType().equals(rightVariable.getType()), "incompatible types: %s != %s", leftVariable.getType(), rightVariable.getType());
            arguments.add(new SpecialFormExpression(COALESCE, leftVariable.getType(), ImmutableList.of(leftVariable, rightVariable)));
        } else {
            return Optional.empty();
        }
    }
    return Optional.of(new Partitioning(metadata.isRefinedPartitioningOver(session, other.handle, this.handle) ? this.handle : other.handle, arguments.build()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression)

Example 8 with ConstantExpression

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

the class TestRowExpressionSerde method assertStringLiteral.

private void assertStringLiteral(@Language("SQL") String sql, String expectedString, Type expectedType) {
    RowExpression roundTrip = getRoundTrip(sql, true);
    assertTrue(roundTrip instanceof ConstantExpression);
    String roundTripValue = ((Slice) ((ConstantExpression) roundTrip).getValue()).toStringUtf8();
    Type roundTripType = roundTrip.getType();
    assertEquals(roundTripValue, expectedString);
    assertEquals(roundTripType, expectedType);
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) RowType(com.facebook.presto.common.type.RowType) Slice(io.airlift.slice.Slice) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 9 with ConstantExpression

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

the class TestRowExpressionSerde method testHllLiteral.

@Test
public void testHllLiteral() {
    RowExpression rowExpression = getRoundTrip("empty_approx_set()", true);
    assertTrue(rowExpression instanceof ConstantExpression);
    Object value = ((ConstantExpression) rowExpression).getValue();
    assertEquals(HyperLogLog.newInstance((Slice) value).cardinality(), 0);
}
Also used : ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 10 with ConstantExpression

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

the class ValuesMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
    ValuesNode valuesNode = (ValuesNode) node;
    if (!expectedRows.map(rows -> rows.equals(valuesNode.getRows().stream().map(rowExpressions -> rowExpressions.stream().map(rowExpression -> {
        if (isExpression(rowExpression)) {
            return castToExpression(rowExpression);
        }
        ConstantExpression expression = (ConstantExpression) rowExpression;
        if (expression.getType().getJavaType() == boolean.class) {
            return new BooleanLiteral(String.valueOf(expression.getValue()));
        }
        if (expression.getType().getJavaType() == long.class) {
            return new LongLiteral(String.valueOf(expression.getValue()));
        }
        if (expression.getType().getJavaType() == double.class) {
            return new DoubleLiteral(String.valueOf(expression.getValue()));
        }
        if (expression.getType().getJavaType() == Slice.class) {
            return new StringLiteral(((Slice) expression.getValue()).toStringUtf8());
        }
        return new GenericLiteral(expression.getType().toString(), String.valueOf(expression.getValue()));
    }).collect(toImmutableList())).collect(toImmutableSet()))).orElse(true)) {
        return NO_MATCH;
    }
    return match(SymbolAliases.builder().putAll(Maps.transformValues(outputSymbolAliases, index -> createSymbolReference(valuesNode.getOutputVariables().get(index)))).build());
}
Also used : Slice(io.airlift.slice.Slice) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Session(com.facebook.presto.Session) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StatsProvider(com.facebook.presto.cost.StatsProvider) Set(java.util.Set) Maps(com.google.common.collect.Maps) Preconditions.checkState(com.google.common.base.Preconditions.checkState) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Expression(com.facebook.presto.sql.tree.Expression) MatchResult.match(com.facebook.presto.sql.planner.assertions.MatchResult.match) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Optional(java.util.Optional) NO_MATCH(com.facebook.presto.sql.planner.assertions.MatchResult.NO_MATCH) Metadata(com.facebook.presto.metadata.Metadata) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) ValuesNode(com.facebook.presto.spi.plan.ValuesNode) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) Slice(io.airlift.slice.Slice) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) DoubleLiteral(com.facebook.presto.sql.tree.DoubleLiteral) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral)

Aggregations

ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)33 RowExpression (com.facebook.presto.spi.relation.RowExpression)25 CallExpression (com.facebook.presto.spi.relation.CallExpression)14 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)14 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)10 Map (java.util.Map)8 ImmutableList (com.google.common.collect.ImmutableList)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Type (com.facebook.presto.common.type.Type)6 InputReferenceExpression (com.facebook.presto.spi.relation.InputReferenceExpression)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Slice (io.airlift.slice.Slice)6 List (java.util.List)6 Scope (com.facebook.presto.bytecode.Scope)5 Variable (com.facebook.presto.bytecode.Variable)5 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 Optional (java.util.Optional)5 IfStatement (com.facebook.presto.bytecode.control.IfStatement)4