Search in sources :

Example 31 with RowExpression

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

the class HashGenerationOptimizer method getHashExpression.

public static Optional<RowExpression> getHashExpression(FunctionAndTypeManager functionAndTypeManager, List<VariableReferenceExpression> variables) {
    if (variables.isEmpty()) {
        return Optional.empty();
    }
    RowExpression result = constant(INITIAL_HASH_VALUE, BIGINT);
    for (VariableReferenceExpression variable : variables) {
        RowExpression hashField = call(functionAndTypeManager, HASH_CODE, BIGINT, variable);
        hashField = orNullHashCode(hashField);
        result = call(functionAndTypeManager, "combine_hash", BIGINT, result, hashField);
    }
    return Optional.of(result);
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression)

Example 32 with RowExpression

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

the class PruneValuesColumns method pushDownProjectOff.

@Override
protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, PlanVariableAllocator variableAllocator, ValuesNode valuesNode, Set<VariableReferenceExpression> referencedOutputs) {
    List<VariableReferenceExpression> newOutputs = filteredCopy(valuesNode.getOutputVariables(), referencedOutputs::contains);
    List<VariableReferenceExpression> newOutputVariables = filteredCopy(valuesNode.getOutputVariables(), referencedOutputs::contains);
    // for each output of project, the corresponding column in the values node
    int[] mapping = new int[newOutputs.size()];
    for (int i = 0; i < mapping.length; i++) {
        mapping[i] = valuesNode.getOutputVariables().indexOf(newOutputs.get(i));
    }
    ImmutableList.Builder<List<RowExpression>> rowsBuilder = ImmutableList.builder();
    for (List<RowExpression> row : valuesNode.getRows()) {
        rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
    }
    return Optional.of(new ValuesNode(valuesNode.getSourceLocation(), valuesNode.getId(), newOutputVariables, rowsBuilder.build()));
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ImmutableList(com.google.common.collect.ImmutableList) RowExpression(com.facebook.presto.spi.relation.RowExpression) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 33 with RowExpression

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

the class RewriteAggregationIfToFilter method shouldRewriteAggregation.

private boolean shouldRewriteAggregation(Aggregation aggregation, ProjectNode sourceProject) {
    if (functionAndTypeManager.getFunctionMetadata(aggregation.getFunctionHandle()).isCalledOnNullInput()) {
        // This rewrite will filter out the null values. It could change the behavior if the aggregation is also applied on NULLs.
        return false;
    }
    if (!(aggregation.getArguments().size() == 1 && aggregation.getArguments().get(0) instanceof VariableReferenceExpression)) {
        // Currently we only handle aggregation with a single VariableReferenceExpression. The detailed expressions are in a project node below this aggregation.
        return false;
    }
    if (aggregation.getFilter().isPresent() || aggregation.getMask().isPresent()) {
        // Do not rewrite the aggregation if it already has a filter or mask.
        return false;
    }
    RowExpression sourceExpression = sourceProject.getAssignments().get((VariableReferenceExpression) aggregation.getArguments().get(0));
    if (sourceExpression instanceof CallExpression) {
        CallExpression callExpression = (CallExpression) sourceExpression;
        if (callExpression.getArguments().size() == 1 && standardFunctionResolution.isCastFunction(callExpression.getFunctionHandle())) {
            // If the expression is CAST(), check the expression inside.
            sourceExpression = callExpression.getArguments().get(0);
        }
    }
    if (!(sourceExpression instanceof SpecialFormExpression) || !rowExpressionDeterminismEvaluator.isDeterministic(sourceExpression)) {
        return false;
    }
    SpecialFormExpression expression = (SpecialFormExpression) sourceExpression;
    // Only rewrite the aggregation if the else branch is not present or the else result is NULL.
    return expression.getForm() == IF && Expressions.isNull(expression.getArguments().get(2));
}
Also used : VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression)

Example 34 with RowExpression

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

the class TestRowExpressionFormatter method testConstants.

@Test
public void testConstants() {
    // null
    RowExpression constantExpression = constantNull(UNKNOWN);
    assertEquals(format(constantExpression), "null");
    // boolean
    constantExpression = constant(true, BOOLEAN);
    assertEquals(format(constantExpression), "BOOLEAN'true'");
    // double
    constantExpression = constant(1.1, DOUBLE);
    assertEquals(format(constantExpression), "DOUBLE'1.1'");
    constantExpression = constant(Double.NaN, DOUBLE);
    assertEquals(format(constantExpression), "DOUBLE'NaN'");
    constantExpression = constant(Double.POSITIVE_INFINITY, DOUBLE);
    assertEquals(format(constantExpression), "DOUBLE'Infinity'");
    // real
    constantExpression = constant((long) floatToIntBits(1.1f), REAL);
    assertEquals(format(constantExpression), "REAL'1.1'");
    constantExpression = constant((long) floatToIntBits(Float.NaN), REAL);
    assertEquals(format(constantExpression), "REAL'NaN'");
    constantExpression = constant((long) floatToIntBits(Float.POSITIVE_INFINITY), REAL);
    assertEquals(format(constantExpression), "REAL'Infinity'");
    // string
    constantExpression = constant(utf8Slice("abcde"), VARCHAR);
    assertEquals(format(constantExpression), "VARCHAR'abcde'");
    constantExpression = constant(utf8Slice("fgh"), createCharType(3));
    assertEquals(format(constantExpression), "CHAR'fgh'");
    // integer
    constantExpression = constant(1L, TINYINT);
    assertEquals(format(constantExpression), "TINYINT'1'");
    constantExpression = constant(1L, SMALLINT);
    assertEquals(format(constantExpression), "SMALLINT'1'");
    constantExpression = constant(1L, INTEGER);
    assertEquals(format(constantExpression), "INTEGER'1'");
    constantExpression = constant(1L, BIGINT);
    assertEquals(format(constantExpression), "BIGINT'1'");
    // varbinary
    Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456AB"));
    constantExpression = constant(value, VARBINARY);
    assertEquals(format(constantExpression), "X'12 34 56 ab'");
    // color
    constantExpression = constant(256L, COLOR);
    assertEquals(format(constantExpression), "COLOR'256'");
    // long and short decimals
    constantExpression = constant(decimal("1.2345678910"), DecimalType.createDecimalType(11, 10));
    assertEquals(format(constantExpression), "DECIMAL'1.2345678910'");
    constantExpression = constant(decimal("1.281734081274028174012432412423134"), DecimalType.createDecimalType(34, 33));
    assertEquals(format(constantExpression), "DECIMAL'1.281734081274028174012432412423134'");
    // time
    constantExpression = constant(662727600000L, TIMESTAMP);
    assertEquals(format(constantExpression), "TIMESTAMP'1991-01-01 00:00:00.000'");
    constantExpression = constant(7670L, DATE);
    assertEquals(format(constantExpression), "DATE'1991-01-01'");
    // interval
    constantExpression = constant(24L, INTERVAL_DAY_TIME);
    assertEquals(format(constantExpression), "INTERVAL DAY TO SECOND'0 00:00:00.024'");
    constantExpression = constant(25L, INTERVAL_YEAR_MONTH);
    assertEquals(format(constantExpression), "INTERVAL YEAR TO MONTH'2-1'");
    // block
    constantExpression = constant(new LongArrayBlockBuilder(null, 4).writeLong(1L).writeLong(2).build(), new ArrayType(BIGINT));
    assertEquals(format(constantExpression), "[Block: position count: 2; size: 96 bytes]");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) RowExpression(com.facebook.presto.spi.relation.RowExpression) LongArrayBlockBuilder(com.facebook.presto.common.block.LongArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 35 with RowExpression

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

the class TestRowExpressionFormatter method testComplex.

@Test
public void testComplex() {
    RowExpression complexExpression;
    RowExpression expression = createCallExpression(ADD);
    complexExpression = call(SUBTRACT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBTRACT, fromTypes(BIGINT, BIGINT)), BIGINT, C_BIGINT, expression);
    assertEquals(format(complexExpression), "(c_bigint) - ((c_bigint) + (BIGINT'5'))");
    RowExpression expression1 = createCallExpression(ADD);
    RowExpression expression2 = call(MULTIPLY.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(MULTIPLY, fromTypes(BIGINT, BIGINT)), BIGINT, expression1, C_BIGINT);
    RowExpression expression3 = createCallExpression(GREATER_THAN);
    complexExpression = new SpecialFormExpression(OR, BOOLEAN, expression2, expression3);
    assertEquals(format(complexExpression), "(((c_bigint) + (BIGINT'5')) * (c_bigint)) OR ((c_bigint) > (BIGINT'5'))");
    ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
    Type elementType = arrayType.getElementType();
    expression1 = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(5L, INTEGER)));
    expression2 = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression1.getType())), expression1.getType(), expression1);
    expression3 = call(ADD.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(ADD, fromTypes(expression2.getType(), BIGINT)), BIGINT, expression2, constant(5L, BIGINT));
    assertEquals(format(expression3), "(-(c_bigint_array[INTEGER'5'])) + (BIGINT'5')");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) DecimalType(com.facebook.presto.common.type.DecimalType) CastType(com.facebook.presto.metadata.CastType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) RowExpression(com.facebook.presto.spi.relation.RowExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) Test(org.testng.annotations.Test)

Aggregations

RowExpression (com.facebook.presto.spi.relation.RowExpression)237 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)97 Test (org.testng.annotations.Test)87 ImmutableList (com.google.common.collect.ImmutableList)58 CallExpression (com.facebook.presto.spi.relation.CallExpression)52 Map (java.util.Map)49 List (java.util.List)42 Type (com.facebook.presto.common.type.Type)41 PlanNode (com.facebook.presto.spi.plan.PlanNode)41 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)40 ImmutableMap (com.google.common.collect.ImmutableMap)38 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)37 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)35 Optional (java.util.Optional)35 Expression (com.facebook.presto.sql.tree.Expression)31 ColumnHandle (com.facebook.presto.spi.ColumnHandle)27 Objects.requireNonNull (java.util.Objects.requireNonNull)27 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)24 Set (java.util.Set)24 ArrayList (java.util.ArrayList)23