use of io.prestosql.sql.relational.RowExpressionDeterminismEvaluator in project hetu-core by openlookeng.
the class FilterMatcher method detailMatches.
@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
checkState(shapeMatches(node), "Plan testing framework error: shapeMatches returned false in detailMatches in %s", this.getClass().getName());
FunctionResolution functionResolution = new FunctionResolution(metadata.getFunctionAndTypeManager());
LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), functionResolution, metadata.getFunctionAndTypeManager());
FilterNode filterNode = (FilterNode) node;
if (isExpression(filterNode.getPredicate())) {
ExpressionVerifier verifier = new ExpressionVerifier(symbolAliases);
if (dynamicFilter.isPresent()) {
return new MatchResult(verifier.process(castToExpression(filterNode.getPredicate()), combineConjuncts(castToExpression(predicate), castToExpression(dynamicFilter.get()))));
}
DynamicFilters.ExtractResult extractResult = extractDynamicFilters(filterNode.getPredicate());
List<Expression> expressionList = extractResult.getStaticConjuncts().stream().map(OriginalExpressionUtils::castToExpression).collect(Collectors.toList());
return new MatchResult(verifier.process(combineConjuncts(expressionList), castToExpression(predicate)));
}
RowExpressionVerifier verifier = new RowExpressionVerifier(symbolAliases, metadata, session, filterNode.getOutputSymbols());
if (dynamicFilter.isPresent()) {
return new MatchResult(verifier.process(combineConjuncts(castToExpression(predicate), castToExpression(dynamicFilter.get())), filterNode.getPredicate()));
}
DynamicFilters.ExtractResult extractResult = extractDynamicFilters(filterNode.getPredicate());
return new MatchResult(verifier.process(castToExpression(predicate), logicalRowExpressions.combineConjuncts(extractResult.getStaticConjuncts())));
}
use of io.prestosql.sql.relational.RowExpressionDeterminismEvaluator in project hetu-core by openlookeng.
the class DynamicFilterMatcher method match.
public MatchResult match(FilterNode filterNode, Metadata metadata, Session session, SymbolAliases symbolAliases) {
checkState(this.filterNode == null, "filterNode must be null at this point");
this.filterNode = filterNode;
this.symbolAliases = symbolAliases;
FunctionResolution functionResolution = new FunctionResolution(metadata.getFunctionAndTypeManager());
LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), functionResolution, metadata.getFunctionAndTypeManager());
boolean staticFilterMatches = expectedStaticFilter.map(filter -> {
RowExpressionVerifier verifier = new RowExpressionVerifier(symbolAliases, metadata, session, filterNode.getOutputSymbols());
RowExpression staticFilter = logicalRowExpressions.combineConjuncts(extractDynamicFilters(filterNode.getPredicate()).getStaticConjuncts());
return verifier.process(filter, staticFilter);
}).orElse(true);
return new MatchResult(match() && staticFilterMatches);
}
use of io.prestosql.sql.relational.RowExpressionDeterminismEvaluator in project hetu-core by openlookeng.
the class TestDynamicFilters method testExtractStaticFilters.
@Test
public void testExtractStaticFilters() {
LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), new FunctionResolution(metadata.getFunctionAndTypeManager()), metadata.getFunctionAndTypeManager());
ConstantExpression staticExpression = new ConstantExpression(utf8Slice("age"), VarcharType.VARCHAR);
VariableReferenceExpression variableExpression = new VariableReferenceExpression("col", VarcharType.VARCHAR);
FunctionHandle functionHandle = metadata.getFunctionAndTypeManager().lookupFunction("rank", ImmutableList.of());
RowExpression dynamicFilterExpression = call(DynamicFilters.Function.NAME, functionHandle, staticExpression.getType(), asList(staticExpression, variableExpression), Optional.empty());
RowExpression combinedExpression = logicalRowExpressions.combineConjuncts(staticExpression, dynamicFilterExpression);
Optional<RowExpression> extractedExpression = DynamicFilters.extractStaticFilters(Optional.of(combinedExpression), metadata);
assertEquals(extractedExpression.get(), staticExpression);
RowExpression combinedStaticExpression = logicalRowExpressions.combineConjuncts(staticExpression, variableExpression);
combinedExpression = logicalRowExpressions.combineConjuncts(staticExpression, variableExpression, dynamicFilterExpression);
extractedExpression = DynamicFilters.extractStaticFilters(Optional.of(combinedExpression), metadata);
assertEquals(extractedExpression.get(), combinedStaticExpression);
}
use of io.prestosql.sql.relational.RowExpressionDeterminismEvaluator in project hetu-core by openlookeng.
the class TestDynamicFilters method testExtractDynamicFilterExpression.
@Test
public void testExtractDynamicFilterExpression() {
LogicalRowExpressions logicalRowExpressions = new LogicalRowExpressions(new RowExpressionDeterminismEvaluator(metadata), new FunctionResolution(metadata.getFunctionAndTypeManager()), metadata.getFunctionAndTypeManager());
ConstantExpression staticExpression1 = new ConstantExpression(utf8Slice("age"), VarcharType.VARCHAR);
VariableReferenceExpression variableExpression1 = new VariableReferenceExpression("col", VarcharType.VARCHAR);
ConstantExpression staticExpression2 = new ConstantExpression(utf8Slice("age"), VarcharType.VARCHAR);
VariableReferenceExpression variableExpression2 = new VariableReferenceExpression("col", VarcharType.VARCHAR);
FunctionHandle functionHandle = metadata.getFunctionAndTypeManager().lookupFunction("rank", ImmutableList.of());
RowExpression dynamicFilterExpression1 = call(DynamicFilters.Function.NAME, functionHandle, staticExpression1.getType(), asList(staticExpression1, variableExpression1), Optional.empty());
RowExpression dynamicFilterExpression2 = call(DynamicFilters.Function.NAME, functionHandle, staticExpression2.getType(), asList(staticExpression2, variableExpression2), Optional.empty());
RowExpression combineDynamicFilterExpression = logicalRowExpressions.combineDisjuncts(asList(dynamicFilterExpression1, dynamicFilterExpression2));
RowExpression combinedExpression = logicalRowExpressions.combineConjuncts(staticExpression1, combineDynamicFilterExpression);
RowExpression extractedExpression = DynamicFilters.extractDynamicFilterExpression(combinedExpression, metadata);
assertEquals(extractedExpression, combineDynamicFilterExpression);
}
use of io.prestosql.sql.relational.RowExpressionDeterminismEvaluator in project hetu-core by openlookeng.
the class SortExpressionExtractor method extractSortExpression.
public static Optional<SortExpressionContext> extractSortExpression(Metadata metadata, Set<Symbol> buildSymbols, RowExpression filter) {
List<RowExpression> filterConjuncts = extractConjuncts(filter);
SortExpressionVisitor visitor = new SortExpressionVisitor(buildSymbols, metadata.getFunctionAndTypeManager());
RowExpressionDeterminismEvaluator determinismEvaluator = new RowExpressionDeterminismEvaluator(metadata);
List<SortExpressionContext> sortExpressionCandidates = filterConjuncts.stream().filter(determinismEvaluator::isDeterministic).map(conjunct -> conjunct.accept(visitor, null)).filter(Optional::isPresent).map(Optional::get).collect(toMap(SortExpressionContext::getSortExpression, identity(), SortExpressionExtractor::merge)).values().stream().collect(toImmutableList());
// TODO: make it cost based decision based on symbol statistics
return sortExpressionCandidates.stream().sorted(comparing(context -> -1 * context.getSearchExpressions().size())).findFirst();
}
Aggregations