use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.
the class RowExpressionVerifier method visitArithmeticBinary.
@Override
protected Boolean visitArithmeticBinary(ArithmeticBinaryExpression expected, RowExpression actual) {
if (actual instanceof CallExpression) {
FunctionMetadata functionMetadata = metadata.getFunctionAndTypeManager().getFunctionMetadata(((CallExpression) actual).getFunctionHandle());
if (!functionMetadata.getOperatorType().isPresent() || !functionMetadata.getOperatorType().get().isArithmeticOperator()) {
return false;
}
OperatorType actualOperatorType = functionMetadata.getOperatorType().get();
OperatorType expectedOperatorType = getOperatorType(expected.getOperator());
if (expectedOperatorType.equals(actualOperatorType)) {
return process(expected.getLeft(), ((CallExpression) actual).getArguments().get(0)) && process(expected.getRight(), ((CallExpression) actual).getArguments().get(1));
}
}
return false;
}
use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.
the class TestTranslateExpressions method testTranslateIntermediateAggregationWithLambda.
@Test
public void testTranslateIntermediateAggregationWithLambda() {
PlanNode result = tester().assertThat(new TranslateExpressions(METADATA, new SqlParser()).aggregationRowExpressionRewriteRule()).on(p -> p.aggregation(builder -> builder.globalGrouping().addAggregation(variable("reduce_agg", INTEGER), new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(castToRowExpression(expression("input")), castToRowExpression(expression("(x,y) -> x*y")), castToRowExpression(expression("(a,b) -> a*b")))), Optional.of(castToRowExpression(expression("input > 10"))), Optional.empty(), false, Optional.empty())).source(p.values(p.variable("input", INTEGER))))).get();
AggregationNode.Aggregation translated = ((AggregationNode) result).getAggregations().get(variable("reduce_agg", INTEGER));
assertEquals(translated, new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(variable("input", INTEGER), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("x", "y"), multiply(variable("x", INTEGER), variable("y", INTEGER))), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("a", "b"), multiply(variable("a", INTEGER), variable("b", INTEGER))))), Optional.of(greaterThan(variable("input", INTEGER), constant(10L, INTEGER))), Optional.empty(), false, Optional.empty()));
assertFalse(isUntranslated(translated));
}
use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.
the class TestTranslateExpressions method testTranslateAggregationWithLambda.
@Test
public void testTranslateAggregationWithLambda() {
PlanNode result = tester().assertThat(new TranslateExpressions(METADATA, new SqlParser()).aggregationRowExpressionRewriteRule()).on(p -> p.aggregation(builder -> builder.globalGrouping().addAggregation(variable("reduce_agg", INTEGER), new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(castToRowExpression(expression("input")), castToRowExpression(expression("0")), castToRowExpression(expression("(x,y) -> x*y")), castToRowExpression(expression("(a,b) -> a*b")))), Optional.of(castToRowExpression(expression("input > 10"))), Optional.empty(), false, Optional.empty())).source(p.values(p.variable("input", INTEGER))))).get();
// TODO migrate this to RowExpressionMatcher
AggregationNode.Aggregation translated = ((AggregationNode) result).getAggregations().get(variable("reduce_agg", INTEGER));
assertEquals(translated, new AggregationNode.Aggregation(new CallExpression("reduce_agg", REDUCE_AGG, INTEGER, ImmutableList.of(variable("input", INTEGER), constant(0L, INTEGER), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("x", "y"), multiply(variable("x", INTEGER), variable("y", INTEGER))), new LambdaDefinitionExpression(Optional.empty(), ImmutableList.of(INTEGER, INTEGER), ImmutableList.of("a", "b"), multiply(variable("a", INTEGER), variable("b", INTEGER))))), Optional.of(greaterThan(variable("input", INTEGER), constant(10L, INTEGER))), Optional.empty(), false, Optional.empty()));
assertFalse(isUntranslated(translated));
}
use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.
the class DynamicFilters method getPlaceholder.
public static Optional<DynamicFilterPlaceholder> getPlaceholder(RowExpression expression) {
if (!(expression instanceof CallExpression)) {
return Optional.empty();
}
CallExpression call = (CallExpression) expression;
List<RowExpression> arguments = call.getArguments();
if (!call.getDisplayName().equals(DynamicFilterPlaceholderFunction.NAME)) {
return Optional.empty();
}
checkArgument(arguments.size() == 3, "invalid arguments count: %s", arguments.size());
RowExpression probeSymbol = arguments.get(0);
RowExpression operatorExpression = arguments.get(1);
checkArgument(operatorExpression instanceof ConstantExpression);
checkArgument(operatorExpression.getType() instanceof VarcharType);
String operator = ((Slice) ((ConstantExpression) operatorExpression).getValue()).toStringUtf8();
RowExpression idExpression = arguments.get(2);
checkArgument(idExpression instanceof ConstantExpression);
checkArgument(idExpression.getType() instanceof VarcharType);
String id = ((Slice) ((ConstantExpression) idExpression).getValue()).toStringUtf8();
OperatorType operatorType = OperatorType.valueOf(operator);
if (operatorType.isComparisonOperator()) {
return Optional.of(new DynamicFilterPlaceholder(id, probeSymbol, operatorType));
}
return Optional.empty();
}
use of com.facebook.presto.spi.relation.CallExpression in project presto by prestodb.
the class ScalarAggregationToJoinRewriter method createAggregationNode.
private Optional<AggregationNode> createAggregationNode(AggregationNode scalarAggregation, JoinNode leftOuterJoin, VariableReferenceExpression nonNull) {
ImmutableMap.Builder<VariableReferenceExpression, Aggregation> aggregations = ImmutableMap.builder();
for (Map.Entry<VariableReferenceExpression, Aggregation> entry : scalarAggregation.getAggregations().entrySet()) {
VariableReferenceExpression variable = entry.getKey();
if (functionResolution.isCountFunction(entry.getValue().getFunctionHandle())) {
Type scalarAggregationSourceType = nonNull.getType();
aggregations.put(variable, new Aggregation(new CallExpression(variable.getSourceLocation(), "count", functionResolution.countFunction(scalarAggregationSourceType), BIGINT, ImmutableList.of(castToRowExpression(asSymbolReference(nonNull)))), Optional.empty(), Optional.empty(), false, entry.getValue().getMask()));
} else {
aggregations.put(variable, entry.getValue());
}
}
return Optional.of(new AggregationNode(scalarAggregation.getSourceLocation(), idAllocator.getNextId(), leftOuterJoin, aggregations.build(), singleGroupingSet(leftOuterJoin.getLeft().getOutputVariables()), ImmutableList.of(), scalarAggregation.getStep(), scalarAggregation.getHashVariable(), Optional.empty()));
}
Aggregations