use of com.facebook.presto.spi.relation.PredicateCompiler in project presto by prestodb.
the class TestRowExpressionPredicateCompiler method test.
@Test
public void test() {
InputReferenceExpression a = new InputReferenceExpression(Optional.empty(), 0, BIGINT);
InputReferenceExpression b = new InputReferenceExpression(Optional.empty(), 1, BIGINT);
Block aBlock = createLongBlock(5, 5, 5, 5, 5);
Block bBlock = createLongBlock(1, 3, 5, 7, 0);
// b - a >= 0
RowExpression sum = call("<", functionResolution.comparisonFunction(GREATER_THAN_OR_EQUAL, BIGINT, BIGINT), BOOLEAN, call("b - a", functionResolution.arithmeticFunction(SUBTRACT, BIGINT, BIGINT), BIGINT, b, a), constant(0L, BIGINT));
PredicateCompiler compiler = new RowExpressionPredicateCompiler(metadata, 10_000);
Predicate compiledSum = compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), sum).get();
assertEquals(Arrays.asList(1, 0), Ints.asList(compiledSum.getInputChannels()));
Page page = new Page(bBlock, aBlock);
assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 0));
assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 1));
assertTrue(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 2));
assertTrue(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 3));
assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 4));
// b * 2 < 10
RowExpression timesTwo = call("=", functionResolution.comparisonFunction(LESS_THAN, BIGINT, BIGINT), BOOLEAN, call("b * 2", functionResolution.arithmeticFunction(MULTIPLY, BIGINT, BIGINT), BIGINT, b, constant(2L, BIGINT)), constant(10L, BIGINT));
Predicate compiledTimesTwo = compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), timesTwo).get();
assertEquals(Arrays.asList(1), Ints.asList(compiledTimesTwo.getInputChannels()));
page = new Page(bBlock);
assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 0));
assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 1));
assertFalse(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 2));
assertFalse(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 3));
assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 4));
}
use of com.facebook.presto.spi.relation.PredicateCompiler in project presto by prestodb.
the class OrcSelectivePageSourceFactory method toFilterFunctions.
/**
* Split filter expression into groups of conjuncts that depend on the same set of inputs,
* then compile each group into FilterFunction.
*/
private static List<FilterFunction> toFilterFunctions(RowExpression filter, Optional<BucketAdapter> bucketAdapter, ConnectorSession session, DeterminismEvaluator determinismEvaluator, PredicateCompiler predicateCompiler) {
ImmutableList.Builder<FilterFunction> filterFunctions = ImmutableList.builder();
bucketAdapter.map(predicate -> new FilterFunction(session.getSqlFunctionProperties(), true, predicate)).ifPresent(filterFunctions::add);
if (TRUE_CONSTANT.equals(filter)) {
return filterFunctions.build();
}
DynamicFilterExtractResult extractDynamicFilterResult = extractDynamicFilters(filter);
// dynamic filter will be added through subfield pushdown
filter = and(extractDynamicFilterResult.getStaticConjuncts());
if (!isAdaptiveFilterReorderingEnabled(session)) {
filterFunctions.add(new FilterFunction(session.getSqlFunctionProperties(), determinismEvaluator.isDeterministic(filter), predicateCompiler.compilePredicate(session.getSqlFunctionProperties(), session.getSessionFunctions(), filter).get()));
return filterFunctions.build();
}
List<RowExpression> conjuncts = extractConjuncts(filter);
if (conjuncts.size() == 1) {
filterFunctions.add(new FilterFunction(session.getSqlFunctionProperties(), determinismEvaluator.isDeterministic(filter), predicateCompiler.compilePredicate(session.getSqlFunctionProperties(), session.getSessionFunctions(), filter).get()));
return filterFunctions.build();
}
// Use LinkedHashMap to preserve user-specified order of conjuncts. This will be the initial order in which filters are applied.
Map<Set<Integer>, List<RowExpression>> inputsToConjuncts = new LinkedHashMap<>();
for (RowExpression conjunct : conjuncts) {
inputsToConjuncts.computeIfAbsent(extractInputs(conjunct), k -> new ArrayList<>()).add(conjunct);
}
inputsToConjuncts.values().stream().map(expressions -> binaryExpression(AND, expressions)).map(predicate -> new FilterFunction(session.getSqlFunctionProperties(), determinismEvaluator.isDeterministic(predicate), predicateCompiler.compilePredicate(session.getSqlFunctionProperties(), session.getSessionFunctions(), predicate).get())).forEach(filterFunctions::add);
return filterFunctions.build();
}
use of com.facebook.presto.spi.relation.PredicateCompiler in project presto by prestodb.
the class TestRowExpressionPredicateCompiler method testCache.
@Test
public void testCache() {
// a * 2 < 10
RowExpression predicate = call("=", functionResolution.comparisonFunction(LESS_THAN, BIGINT, BIGINT), BOOLEAN, call("a * 2", functionResolution.arithmeticFunction(MULTIPLY, BIGINT, BIGINT), BIGINT, new InputReferenceExpression(Optional.empty(), 1, BIGINT), constant(2L, BIGINT)), constant(10L, BIGINT));
PredicateCompiler compiler = new RowExpressionPredicateCompiler(metadata, 10_000);
assertSame(compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), predicate), compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), predicate));
PredicateCompiler noCacheCompiler = new RowExpressionPredicateCompiler(metadata, 0);
assertNotSame(noCacheCompiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), predicate), noCacheCompiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), predicate));
}
Aggregations