Search in sources :

Example 1 with ExpressionAndValuePointers

use of io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers in project trino by trinodb.

the class MergePatternRecognitionNodes method equivalent.

private static boolean equivalent(Map<IrLabel, ExpressionAndValuePointers> parentVariableDefinitions, Map<IrLabel, ExpressionAndValuePointers> childVariableDefinitions) {
    if (!parentVariableDefinitions.keySet().equals(childVariableDefinitions.keySet())) {
        return false;
    }
    for (Map.Entry<IrLabel, ExpressionAndValuePointers> parentDefinition : parentVariableDefinitions.entrySet()) {
        IrLabel label = parentDefinition.getKey();
        ExpressionAndValuePointers parentExpression = parentDefinition.getValue();
        ExpressionAndValuePointers childExpression = childVariableDefinitions.get(label);
        if (!ExpressionAndValuePointersEquivalence.equivalent(parentExpression, childExpression)) {
            return false;
        }
    }
    return true;
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with ExpressionAndValuePointers

use of io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers in project trino by trinodb.

the class PushDownProjectionsFromPatternRecognition method apply.

@Override
public Result apply(PatternRecognitionNode node, Captures captures, Context context) {
    Assignments.Builder assignments = Assignments.builder();
    Map<IrLabel, ExpressionAndValuePointers> rewrittenVariableDefinitions = rewriteVariableDefinitions(node.getVariableDefinitions(), assignments, context);
    Map<Symbol, Measure> rewrittenMeasureDefinitions = rewriteMeasureDefinitions(node.getMeasures(), assignments, context);
    if (assignments.build().isEmpty()) {
        return Result.empty();
    }
    assignments.putIdentities(node.getSource().getOutputSymbols());
    ProjectNode projectNode = new ProjectNode(context.getIdAllocator().getNextId(), node.getSource(), assignments.build());
    PatternRecognitionNode patternRecognitionNode = new PatternRecognitionNode(node.getId(), projectNode, node.getSpecification(), node.getHashSymbol(), node.getPrePartitionedInputs(), node.getPreSortedOrderPrefix(), node.getWindowFunctions(), rewrittenMeasureDefinitions, node.getCommonBaseFrame(), node.getRowsPerMatch(), node.getSkipToLabel(), node.getSkipToPosition(), node.isInitial(), node.getPattern(), node.getSubsets(), rewrittenVariableDefinitions);
    return Result.ofPlanNode(restrictOutputs(context.getIdAllocator(), patternRecognitionNode, ImmutableSet.copyOf(node.getOutputSymbols())).orElse(patternRecognitionNode));
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Symbol(io.trino.sql.planner.Symbol) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Assignments(io.trino.sql.planner.plan.Assignments) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) ProjectNode(io.trino.sql.planner.plan.ProjectNode)

Example 3 with ExpressionAndValuePointers

use of io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers in project trino by trinodb.

the class SymbolMapper method map.

public PatternRecognitionNode map(PatternRecognitionNode node, PlanNode source) {
    ImmutableMap.Builder<Symbol, WindowNode.Function> newFunctions = ImmutableMap.builder();
    node.getWindowFunctions().forEach((symbol, function) -> {
        List<Expression> newArguments = function.getArguments().stream().map(this::map).collect(toImmutableList());
        WindowNode.Frame newFrame = map(function.getFrame());
        newFunctions.put(map(symbol), new WindowNode.Function(function.getResolvedFunction(), newArguments, newFrame, function.isIgnoreNulls()));
    });
    ImmutableMap.Builder<Symbol, Measure> newMeasures = ImmutableMap.builder();
    node.getMeasures().forEach((symbol, measure) -> {
        ExpressionAndValuePointers newExpression = map(measure.getExpressionAndValuePointers());
        newMeasures.put(map(symbol), new Measure(newExpression, measure.getType()));
    });
    ImmutableMap.Builder<IrLabel, ExpressionAndValuePointers> newVariableDefinitions = ImmutableMap.builder();
    node.getVariableDefinitions().forEach((label, expression) -> newVariableDefinitions.put(label, map(expression)));
    return new PatternRecognitionNode(node.getId(), source, mapAndDistinct(node.getSpecification()), node.getHashSymbol().map(this::map), node.getPrePartitionedInputs().stream().map(this::map).collect(toImmutableSet()), node.getPreSortedOrderPrefix(), newFunctions.buildOrThrow(), newMeasures.buildOrThrow(), node.getCommonBaseFrame().map(this::map), node.getRowsPerMatch(), node.getSkipToLabel(), node.getSkipToPosition(), node.isInitial(), node.getPattern(), node.getSubsets(), newVariableDefinitions.buildOrThrow());
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) WindowNode(io.trino.sql.planner.plan.WindowNode) Symbol(io.trino.sql.planner.Symbol) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Function(java.util.function.Function) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Expression(io.trino.sql.tree.Expression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure)

Example 4 with ExpressionAndValuePointers

use of io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers 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());
}
Also used : IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) Set(java.util.Set) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) AggregationNode.singleGroupingSet(io.trino.sql.planner.plan.AggregationNode.singleGroupingSet) ImmutableSet(com.google.common.collect.ImmutableSet) VariableDefinition(io.trino.sql.tree.VariableDefinition) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) MeasureDefinition(io.trino.sql.tree.MeasureDefinition) SkipTo(io.trino.sql.tree.SkipTo) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ImmutableMap(com.google.common.collect.ImmutableMap) IrRowPattern(io.trino.sql.planner.rowpattern.ir.IrRowPattern) SubsetDefinition(io.trino.sql.tree.SubsetDefinition) RelationType(io.trino.sql.analyzer.RelationType) RowType(io.trino.spi.type.RowType) TypeSignatureTranslator.toSqlType(io.trino.sql.analyzer.TypeSignatureTranslator.toSqlType) Type(io.trino.spi.type.Type) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) CoalesceExpression(io.trino.sql.tree.CoalesceExpression) Expression(io.trino.sql.tree.Expression) SubqueryExpression(io.trino.sql.tree.SubqueryExpression) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure)

Example 5 with ExpressionAndValuePointers

use of io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers in project trino by trinodb.

the class PatternRecognitionExpressionRewriter method rewrite.

public static ExpressionAndValuePointers rewrite(Expression definition, Map<IrLabel, Set<IrLabel>> subsets) {
    Expression expression = rewriteIdentifiers(definition);
    Map<Symbol, Type> types = extractExpressions(ImmutableList.of(expression), SymbolReference.class).stream().collect(toImmutableMap(Symbol::from, reference -> BIGINT));
    return LogicalIndexExtractor.rewrite(expression, subsets, new SymbolAllocator(types), createTestMetadataManager());
}
Also used : ExpressionTreeUtils.extractExpressions(io.trino.sql.analyzer.ExpressionTreeUtils.extractExpressions) ParsingOptions(io.trino.sql.parser.ParsingOptions) SymbolAllocator(io.trino.sql.planner.SymbolAllocator) Type(io.trino.spi.type.Type) LambdaExpression(io.trino.sql.tree.LambdaExpression) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) RowDataType(io.trino.sql.tree.RowDataType) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) GenericDataType(io.trino.sql.tree.GenericDataType) SqlParser(io.trino.sql.parser.SqlParser) ExpressionRewriter(io.trino.sql.tree.ExpressionRewriter) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) Identifier(io.trino.sql.tree.Identifier) Symbol(io.trino.sql.planner.Symbol) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) LabelDereference(io.trino.sql.tree.LabelDereference) ExpressionTreeRewriter(io.trino.sql.tree.ExpressionTreeRewriter) LogicalIndexExtractor(io.trino.sql.planner.rowpattern.LogicalIndexExtractor) Set(java.util.Set) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) BIGINT(io.trino.spi.type.BigintType.BIGINT) SymbolReference(io.trino.sql.tree.SymbolReference) MetadataManager.createTestMetadataManager(io.trino.metadata.MetadataManager.createTestMetadataManager) Expression(io.trino.sql.tree.Expression) SymbolAllocator(io.trino.sql.planner.SymbolAllocator) Type(io.trino.spi.type.Type) RowDataType(io.trino.sql.tree.RowDataType) GenericDataType(io.trino.sql.tree.GenericDataType) LambdaExpression(io.trino.sql.tree.LambdaExpression) DereferenceExpression(io.trino.sql.tree.DereferenceExpression) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol)

Aggregations

ExpressionAndValuePointers (io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers)12 IrLabel (io.trino.sql.planner.rowpattern.ir.IrLabel)11 Symbol (io.trino.sql.planner.Symbol)9 Expression (io.trino.sql.tree.Expression)9 Measure (io.trino.sql.planner.plan.PatternRecognitionNode.Measure)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 Type (io.trino.spi.type.Type)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)5 PatternRecognitionNode (io.trino.sql.planner.plan.PatternRecognitionNode)5 SymbolReference (io.trino.sql.tree.SymbolReference)5 Map (java.util.Map)5 Set (java.util.Set)5 SqlParser (io.trino.sql.parser.SqlParser)4 ScalarValuePointer (io.trino.sql.planner.rowpattern.ScalarValuePointer)4 ComparisonExpression (io.trino.sql.tree.ComparisonExpression)4 JsonCodecFactory (io.airlift.json.JsonCodecFactory)3 ObjectMapperProvider (io.airlift.json.ObjectMapperProvider)3 ArithmeticUnaryExpression (io.trino.sql.tree.ArithmeticUnaryExpression)3 IfExpression (io.trino.sql.tree.IfExpression)3