Search in sources :

Example 36 with RowExpression

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

the class TestRowExpressionFormatter method testCalls.

@Test
public void testCalls() {
    RowExpression callExpression;
    // arithmetic
    callExpression = createCallExpression(ADD);
    assertEquals(format(callExpression), "(c_bigint) + (BIGINT'5')");
    callExpression = createCallExpression(SUBTRACT);
    assertEquals(format(callExpression), "(c_bigint) - (BIGINT'5')");
    callExpression = createCallExpression(MULTIPLY);
    assertEquals(format(callExpression), "(c_bigint) * (BIGINT'5')");
    callExpression = createCallExpression(DIVIDE);
    assertEquals(format(callExpression), "(c_bigint) / (BIGINT'5')");
    callExpression = createCallExpression(MODULUS);
    assertEquals(format(callExpression), "(c_bigint) % (BIGINT'5')");
    // comparison
    callExpression = createCallExpression(GREATER_THAN);
    assertEquals(format(callExpression), "(c_bigint) > (BIGINT'5')");
    callExpression = createCallExpression(LESS_THAN);
    assertEquals(format(callExpression), "(c_bigint) < (BIGINT'5')");
    callExpression = createCallExpression(GREATER_THAN_OR_EQUAL);
    assertEquals(format(callExpression), "(c_bigint) >= (BIGINT'5')");
    callExpression = createCallExpression(LESS_THAN_OR_EQUAL);
    assertEquals(format(callExpression), "(c_bigint) <= (BIGINT'5')");
    callExpression = createCallExpression(EQUAL);
    assertEquals(format(callExpression), "(c_bigint) = (BIGINT'5')");
    callExpression = createCallExpression(NOT_EQUAL);
    assertEquals(format(callExpression), "(c_bigint) <> (BIGINT'5')");
    callExpression = createCallExpression(IS_DISTINCT_FROM);
    assertEquals(format(callExpression), "(c_bigint) IS DISTINCT FROM (BIGINT'5')");
    // negation
    RowExpression expression = createCallExpression(ADD);
    callExpression = call(NEGATION.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(NEGATION, fromTypes(expression.getType())), expression.getType(), expression);
    assertEquals(format(callExpression), "-((c_bigint) + (BIGINT'5'))");
    // subscript
    ArrayType arrayType = (ArrayType) C_BIGINT_ARRAY.getType();
    Type elementType = arrayType.getElementType();
    RowExpression subscriptExpression = call(SUBSCRIPT.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(SUBSCRIPT, fromTypes(arrayType, elementType)), elementType, ImmutableList.of(C_BIGINT_ARRAY, constant(0L, INTEGER)));
    callExpression = subscriptExpression;
    assertEquals(format(callExpression), "c_bigint_array[INTEGER'0']");
    // cast
    callExpression = call(CAST.name(), FUNCTION_AND_TYPE_MANAGER.lookupCast(CastType.CAST, TINYINT.getTypeSignature(), BIGINT.getTypeSignature()), BIGINT, constant(1L, TINYINT));
    assertEquals(format(callExpression), "CAST(TINYINT'1' AS bigint)");
    // between
    callExpression = call(BETWEEN.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(BETWEEN, fromTypes(BIGINT, BIGINT, BIGINT)), BOOLEAN, subscriptExpression, constant(1L, BIGINT), constant(5L, BIGINT));
    assertEquals(format(callExpression), "c_bigint_array[INTEGER'0'] BETWEEN (BIGINT'1') AND (BIGINT'5')");
    // other
    callExpression = call(HASH_CODE.name(), FUNCTION_AND_TYPE_MANAGER.resolveOperator(HASH_CODE, fromTypes(BIGINT)), BIGINT, constant(1L, BIGINT));
    assertEquals(format(callExpression), "HASH_CODE(BIGINT'1')");
    // like
    callExpression = call("LIKE", FUNCTION_RESOLUTION.likeVarcharFunction(), BOOLEAN, C_VARCHAR, call(CAST.name(), FUNCTION_AND_TYPE_MANAGER.lookupCast(CastType.CAST, VARCHAR.getTypeSignature(), LIKE_PATTERN.getTypeSignature()), LIKE_PATTERN, constant(utf8Slice("prefix%"), VARCHAR)));
    assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'prefix%'");
    callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
    assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
    // like escape
    callExpression = call("LIKE", FUNCTION_RESOLUTION.likeVarcharFunction(), BOOLEAN, C_VARCHAR, call("LIKE_PATTERN", FUNCTION_RESOLUTION.likePatternFunction(), LIKE_PATTERN, constant(utf8Slice("%escaped$_"), VARCHAR), constant(utf8Slice("$"), VARCHAR)));
    assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'%escaped$_' ESCAPE VARCHAR'$'");
    callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
    assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
}
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) Test(org.testng.annotations.Test)

Example 37 with RowExpression

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

the class TestEffectivePredicateExtractor method testAggregation.

@Test
public void testAggregation() {
    PlanNode node = new AggregationNode(Optional.empty(), newId(), filter(baseTableScan, and(equals(AV, DV), equals(BV, EV), equals(CV, FV), lessThan(DV, bigintLiteral(10)), lessThan(CV, DV), greaterThan(AV, bigintLiteral(2)), equals(EV, FV))), ImmutableMap.of(CV, count(metadata.getFunctionAndTypeManager()), DV, count(metadata.getFunctionAndTypeManager())), singleGroupingSet(ImmutableList.of(AV, BV, CV)), ImmutableList.of(), AggregationNode.Step.FINAL, Optional.empty(), Optional.empty());
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Rewrite in terms of group by symbols
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(lessThan(AV, bigintLiteral(10)), lessThan(BV, AV), greaterThan(AV, bigintLiteral(2)), equals(BV, CV)));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) Test(org.testng.annotations.Test)

Example 38 with RowExpression

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

the class TestEffectivePredicateExtractor method normalizeConjuncts.

private Set<RowExpression> normalizeConjuncts(RowExpression predicate) {
    // Normalize the predicate by identity so that the EqualityInference will produce stable rewrites in this test
    // and thereby produce comparable Sets of conjuncts from this method.
    // Equality inference rewrites and equality generation will always be stable across multiple runs in the same JVM
    EqualityInference inference = EqualityInference.createEqualityInference(metadata, predicate);
    Set<RowExpression> rewrittenSet = new HashSet<>();
    for (RowExpression expression : nonInferrableConjuncts(metadata, predicate)) {
        RowExpression rewritten = inference.rewriteExpression(expression, Predicates.alwaysTrue());
        Preconditions.checkState(rewritten != null, "Rewrite with full symbol scope should always be possible");
        rewrittenSet.add(rewritten);
    }
    rewrittenSet.addAll(inference.generateEqualitiesPartitionedBy(Predicates.alwaysTrue()).getScopeEqualities());
    return rewrittenSet;
}
Also used : RowExpression(com.facebook.presto.spi.relation.RowExpression) HashSet(java.util.HashSet)

Example 39 with RowExpression

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

the class TestEffectivePredicateExtractor method testWindow.

@Test
public void testWindow() {
    PlanNode node = new WindowNode(Optional.empty(), newId(), filter(baseTableScan, and(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10)))), new WindowNode.Specification(ImmutableList.of(AV), Optional.of(new OrderingScheme(ImmutableList.of(new Ordering(AV, SortOrder.ASC_NULLS_LAST))))), ImmutableMap.of(), Optional.empty(), ImmutableSet.of(), 0);
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Pass through
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10))));
}
Also used : WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PlanNode(com.facebook.presto.spi.plan.PlanNode) Ordering(com.facebook.presto.spi.plan.Ordering) RowExpression(com.facebook.presto.spi.relation.RowExpression) Test(org.testng.annotations.Test)

Example 40 with RowExpression

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

the class TestEffectivePredicateExtractor method testFilter.

@Test
public void testFilter() {
    PlanNode node = filter(baseTableScan, and(greaterThan(AV, call(metadata.getFunctionAndTypeManager(), "rand", DOUBLE, ImmutableList.of())), lessThan(BV, bigintLiteral(10))));
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Non-deterministic functions should be purged
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(lessThan(BV, bigintLiteral(10))));
}
Also used : PlanNode(com.facebook.presto.spi.plan.PlanNode) RowExpression(com.facebook.presto.spi.relation.RowExpression) 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