use of io.trino.sql.planner.rowpattern.ir.IrRowPattern in project trino by trinodb.
the class RelationPlanner method planPatternRecognitionComponents.
public PatternRecognitionComponents planPatternRecognitionComponents(Function<Expression, Expression> expressionRewrite, List<SubsetDefinition> subsets, List<MeasureDefinition> measures, Optional<SkipTo> skipTo, Optional<PatternSearchMode> searchMode, RowPattern pattern, List<VariableDefinition> variableDefinitions) {
// rewrite subsets
ImmutableMap.Builder<IrLabel, Set<IrLabel>> rewrittenSubsets = ImmutableMap.builder();
for (SubsetDefinition subsetDefinition : subsets) {
IrLabel label = irLabel(subsetDefinition.getName());
Set<IrLabel> elements = subsetDefinition.getIdentifiers().stream().map(RelationPlanner::irLabel).collect(toImmutableSet());
rewrittenSubsets.put(label, elements);
}
// NOTE: There might be aggregate functions in measure definitions and variable definitions.
// They are handled different than top level aggregations in a query:
// 1. Their arguments are not pre-projected and replaced with single symbols. This is because the arguments might
// not be eligible for pre-projection, when they contain references to CLASSIFIER() or MATCH_NUMBER() functions
// which are evaluated at runtime. If some aggregation arguments can be pre-projected, it will be done in the
// Optimizer.
// 2. Their arguments do not need to be coerced by hand. Since the pattern aggregation arguments are rewritten as
// parts of enclosing expressions, and not as standalone expressions, all necessary coercions will be applied by the
// TranslationMap.
// rewrite measures
ImmutableMap.Builder<Symbol, Measure> rewrittenMeasures = ImmutableMap.builder();
ImmutableList.Builder<Symbol> measureOutputs = ImmutableList.builder();
for (MeasureDefinition measureDefinition : measures) {
Type type = analysis.getType(measureDefinition.getExpression());
Symbol symbol = symbolAllocator.newSymbol(measureDefinition.getName().getValue().toLowerCase(ENGLISH), type);
Expression expression = expressionRewrite.apply(measureDefinition.getExpression());
ExpressionAndValuePointers measure = LogicalIndexExtractor.rewrite(expression, rewrittenSubsets.buildOrThrow(), symbolAllocator, plannerContext.getMetadata());
rewrittenMeasures.put(symbol, new Measure(measure, type));
measureOutputs.add(symbol);
}
// rewrite pattern to IR
IrRowPattern rewrittenPattern = RowPatternToIrRewriter.rewrite(pattern, analysis);
// rewrite variable definitions
ImmutableMap.Builder<IrLabel, ExpressionAndValuePointers> rewrittenVariableDefinitions = ImmutableMap.builder();
for (VariableDefinition variableDefinition : variableDefinitions) {
IrLabel label = irLabel(variableDefinition.getName());
Expression expression = expressionRewrite.apply(variableDefinition.getExpression());
ExpressionAndValuePointers definition = LogicalIndexExtractor.rewrite(expression, rewrittenSubsets.buildOrThrow(), symbolAllocator, plannerContext.getMetadata());
rewrittenVariableDefinitions.put(label, definition);
}
// add `true` definition for undefined labels
for (String label : analysis.getUndefinedLabels(pattern)) {
rewrittenVariableDefinitions.put(irLabel(label), ExpressionAndValuePointers.TRUE);
}
return new PatternRecognitionComponents(rewrittenSubsets.buildOrThrow(), rewrittenMeasures.buildOrThrow(), measureOutputs.build(), skipTo.flatMap(SkipTo::getIdentifier).map(RelationPlanner::irLabel), skipTo.map(SkipTo::getPosition).orElse(PAST_LAST), searchMode.map(mode -> mode.getMode() == INITIAL).orElse(TRUE), rewrittenPattern, rewrittenVariableDefinitions.buildOrThrow());
}
use of io.trino.sql.planner.rowpattern.ir.IrRowPattern in project trino by trinodb.
the class TestIrRowPatternOptimization method assertFlattened.
private void assertFlattened(IrRowPattern pattern, IrRowPattern expected) {
IrRowPattern flattened = IrRowPatternFlattener.optimize(pattern);
assertEquals(flattened, expected);
}
use of io.trino.sql.planner.rowpattern.ir.IrRowPattern in project trino by trinodb.
the class TestIrRowPatternOptimization method assertOptimized.
private void assertOptimized(IrRowPattern pattern, IrRowPattern expected) {
IrRowPattern optimized = IrPatternAlternationOptimizer.optimize(pattern);
assertEquals(optimized, expected);
}
use of io.trino.sql.planner.rowpattern.ir.IrRowPattern in project trino by trinodb.
the class RowPatternToIrRewriter method visitQuantifiedPattern.
@Override
protected IrRowPattern visitQuantifiedPattern(QuantifiedPattern node, Void context) {
IrRowPattern pattern = process(node.getPattern());
IrQuantifier quantifier = rewritePatternQuantifier(node.getPatternQuantifier());
return new IrQuantified(pattern, quantifier);
}
use of io.trino.sql.planner.rowpattern.ir.IrRowPattern in project trino by trinodb.
the class TestRowPatternSerialization method testRowPatternRoundtrip.
@Test
public void testRowPatternRoundtrip() {
JsonCodec<IrRowPattern> codec = new JsonCodecFactory(new ObjectMapperProvider()).jsonCodec(IrRowPattern.class);
assertJsonRoundTrip(codec, start());
assertJsonRoundTrip(codec, end());
assertJsonRoundTrip(codec, empty());
assertJsonRoundTrip(codec, excluded(empty()));
assertJsonRoundTrip(codec, label("name"));
assertJsonRoundTrip(codec, label(""));
assertJsonRoundTrip(codec, label("^"));
assertJsonRoundTrip(codec, label("$"));
assertJsonRoundTrip(codec, alternation(empty(), empty(), empty()));
assertJsonRoundTrip(codec, concatenation(empty(), empty(), empty()));
assertJsonRoundTrip(codec, permutation(empty(), empty(), empty()));
assertJsonRoundTrip(codec, starQuantified(empty(), true));
assertJsonRoundTrip(codec, concatenation(alternation(starQuantified(start(), true), plusQuantified(end(), false), questionMarkQuantified(empty(), true)), concatenation(excluded(rangeQuantified(empty(), 0, Optional.empty(), true)), rangeQuantified(label("name_0"), 5, Optional.empty(), false), label("name_1")), permutation(alternation(label("name_2"), rangeQuantified(label("name_3"), 0, Optional.of(5), true)), rangeQuantified(concatenation(label("name_4"), label("name_5")), 5, Optional.of(10), false))));
}
Aggregations