Search in sources :

Example 1 with Assignment

use of io.trino.sql.planner.plan.Assignments.Assignment 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 2 with Assignment

use of io.trino.sql.planner.plan.Assignments.Assignment 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)

Aggregations

RowType (io.trino.spi.type.RowType)2 Type (io.trino.spi.type.Type)2 Symbol (io.trino.sql.planner.Symbol)2 Assignment (io.trino.sql.planner.plan.Assignments.Assignment)2 Expression (io.trino.sql.tree.Expression)2 LongLiteral (io.trino.sql.tree.LongLiteral)2 QuantifiedComparisonExpression (io.trino.sql.tree.QuantifiedComparisonExpression)2 SubscriptExpression (io.trino.sql.tree.SubscriptExpression)2 Captures (io.trino.matching.Captures)1 Pattern (io.trino.matching.Pattern)1 TypeAnalyzer (io.trino.sql.planner.TypeAnalyzer)1 Rule (io.trino.sql.planner.iterative.Rule)1 ApplyNode (io.trino.sql.planner.plan.ApplyNode)1 Assignments (io.trino.sql.planner.plan.Assignments)1 Patterns.applyNode (io.trino.sql.planner.plan.Patterns.applyNode)1 ProjectNode (io.trino.sql.planner.plan.ProjectNode)1 InPredicate (io.trino.sql.tree.InPredicate)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1