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@"));
}
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)));
}
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;
}
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))));
}
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))));
}
Aggregations