Search in sources :

Example 51 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class UnwrapSingleColumnRowInApply method apply.

@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
    Assignments.Builder inputAssignments = Assignments.builder().putIdentities(node.getInput().getOutputSymbols());
    Assignments.Builder nestedPlanAssignments = Assignments.builder().putIdentities(node.getSubquery().getOutputSymbols());
    boolean applied = false;
    Assignments.Builder applyAssignments = Assignments.builder();
    for (Map.Entry<Symbol, Expression> assignment : node.getSubqueryAssignments().entrySet()) {
        Symbol output = assignment.getKey();
        Expression expression = assignment.getValue();
        Optional<Unwrapping> unwrapped = Optional.empty();
        if (expression instanceof InPredicate) {
            InPredicate predicate = (InPredicate) expression;
            unwrapped = unwrapSingleColumnRow(context, predicate.getValue(), predicate.getValueList(), (value, list) -> new InPredicate(value.toSymbolReference(), list.toSymbolReference()));
        } else if (expression instanceof QuantifiedComparisonExpression) {
            QuantifiedComparisonExpression comparison = (QuantifiedComparisonExpression) expression;
            unwrapped = unwrapSingleColumnRow(context, comparison.getValue(), comparison.getSubquery(), (value, list) -> new QuantifiedComparisonExpression(comparison.getOperator(), comparison.getQuantifier(), value.toSymbolReference(), list.toSymbolReference()));
        }
        if (unwrapped.isPresent()) {
            applied = true;
            Unwrapping unwrapping = unwrapped.get();
            inputAssignments.add(unwrapping.getInputAssignment());
            nestedPlanAssignments.add(unwrapping.getNestedPlanAssignment());
            applyAssignments.put(output, unwrapping.getExpression());
        } else {
            applyAssignments.put(assignment);
        }
    }
    if (!applied) {
        return Result.empty();
    }
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), new ApplyNode(node.getId(), new ProjectNode(context.getIdAllocator().getNextId(), node.getInput(), inputAssignments.build()), new ProjectNode(context.getIdAllocator().getNextId(), node.getSubquery(), nestedPlanAssignments.build()), applyAssignments.build(), node.getCorrelation(), node.getOriginSubquery()), Assignments.identity(node.getOutputSymbols())));
}
Also used : Assignment(io.trino.sql.planner.plan.Assignments.Assignment) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) BiFunction(java.util.function.BiFunction) Type(io.trino.spi.type.Type) Assignments(io.trino.sql.planner.plan.Assignments) InPredicate(io.trino.sql.tree.InPredicate) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) Pattern(io.trino.matching.Pattern) TypeAnalyzer(io.trino.sql.planner.TypeAnalyzer) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Captures(io.trino.matching.Captures) LongLiteral(io.trino.sql.tree.LongLiteral) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ApplyNode(io.trino.sql.planner.plan.ApplyNode) Optional(java.util.Optional) Rule(io.trino.sql.planner.iterative.Rule) Expression(io.trino.sql.tree.Expression) Patterns.applyNode(io.trino.sql.planner.plan.Patterns.applyNode) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Symbol(io.trino.sql.planner.Symbol) ApplyNode(io.trino.sql.planner.plan.ApplyNode) Assignments(io.trino.sql.planner.plan.Assignments) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) InPredicate(io.trino.sql.tree.InPredicate) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Map(java.util.Map)

Example 52 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class UnwrapSingleColumnRowInApply method unwrapSingleColumnRow.

private Optional<Unwrapping> unwrapSingleColumnRow(Context context, Expression value, Expression list, BiFunction<Symbol, Symbol, Expression> function) {
    Type type = typeAnalyzer.getType(context.getSession(), context.getSymbolAllocator().getTypes(), value);
    if (type instanceof RowType) {
        RowType rowType = (RowType) type;
        if (rowType.getFields().size() == 1) {
            Type elementType = rowType.getTypeParameters().get(0);
            Symbol valueSymbol = context.getSymbolAllocator().newSymbol("input", elementType);
            Symbol listSymbol = context.getSymbolAllocator().newSymbol("subquery", elementType);
            Assignment inputAssignment = new Assignment(valueSymbol, new SubscriptExpression(value, new LongLiteral("1")));
            Assignment nestedPlanAssignment = new Assignment(listSymbol, new SubscriptExpression(list, new LongLiteral("1")));
            Expression comparison = function.apply(valueSymbol, listSymbol);
            return Optional.of(new Unwrapping(comparison, inputAssignment, nestedPlanAssignment));
        }
    }
    return Optional.empty();
}
Also used : Assignment(io.trino.sql.planner.plan.Assignments.Assignment) RowType(io.trino.spi.type.RowType) Type(io.trino.spi.type.Type) LongLiteral(io.trino.sql.tree.LongLiteral) SubscriptExpression(io.trino.sql.tree.SubscriptExpression) QuantifiedComparisonExpression(io.trino.sql.tree.QuantifiedComparisonExpression) Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) RowType(io.trino.spi.type.RowType) SubscriptExpression(io.trino.sql.tree.SubscriptExpression)

Example 53 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class SymbolMapper method map.

public GroupIdNode map(GroupIdNode node, PlanNode source) {
    Map<Symbol, Symbol> newGroupingMappings = new HashMap<>();
    ImmutableList.Builder<List<Symbol>> newGroupingSets = ImmutableList.builder();
    for (List<Symbol> groupingSet : node.getGroupingSets()) {
        ImmutableList.Builder<Symbol> newGroupingSet = ImmutableList.builder();
        for (Symbol output : groupingSet) {
            Symbol newOutput = map(output);
            newGroupingMappings.putIfAbsent(newOutput, map(node.getGroupingColumns().get(output)));
            newGroupingSet.add(newOutput);
        }
        newGroupingSets.add(newGroupingSet.build());
    }
    return new GroupIdNode(node.getId(), source, newGroupingSets.build(), newGroupingMappings, mapAndDistinct(node.getAggregationArguments()), map(node.getGroupIdSymbol()));
}
Also used : HashMap(java.util.HashMap) GroupIdNode(io.trino.sql.planner.plan.GroupIdNode) Symbol(io.trino.sql.planner.Symbol) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List)

Example 54 with Symbol

use of io.trino.sql.planner.Symbol 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 55 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class SymbolMapper method symbolReallocator.

public static SymbolMapper symbolReallocator(Map<Symbol, Symbol> mapping, SymbolAllocator symbolAllocator) {
    return new SymbolMapper(symbol -> {
        if (mapping.containsKey(symbol)) {
            while (mapping.containsKey(symbol) && !mapping.get(symbol).equals(symbol)) {
                symbol = mapping.get(symbol);
            }
            // do not remap the symbol further
            mapping.put(symbol, symbol);
            return symbol;
        }
        Symbol newSymbol = symbolAllocator.newSymbol(symbol);
        mapping.put(symbol, newSymbol);
        // do not remap the symbol further
        mapping.put(newSymbol, newSymbol);
        return newSymbol;
    });
}
Also used : Symbol(io.trino.sql.planner.Symbol)

Aggregations

Symbol (io.trino.sql.planner.Symbol)366 Test (org.testng.annotations.Test)171 ImmutableList (com.google.common.collect.ImmutableList)124 ImmutableMap (com.google.common.collect.ImmutableMap)106 Optional (java.util.Optional)96 PlanNode (io.trino.sql.planner.plan.PlanNode)85 Expression (io.trino.sql.tree.Expression)85 Map (java.util.Map)66 Assignments (io.trino.sql.planner.plan.Assignments)65 JoinNode (io.trino.sql.planner.plan.JoinNode)64 ProjectNode (io.trino.sql.planner.plan.ProjectNode)61 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)56 SymbolReference (io.trino.sql.tree.SymbolReference)55 BIGINT (io.trino.spi.type.BigintType.BIGINT)52 Type (io.trino.spi.type.Type)51 Session (io.trino.Session)46 List (java.util.List)46 RuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester)43 RuleTester.defaultRuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester)43 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)43