Search in sources :

Example 26 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class ImplementIntersectAsUnion method apply.

@Override
public Result apply(IntersectNode node, Captures captures, Context context) {
    SetOperationNodeTranslator translator = new SetOperationNodeTranslator(metadata, context.getSymbolAllocator(), context.getIdAllocator());
    SetOperationNodeTranslator.TranslationResult result = translator.makeSetContainmentPlan(node);
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), new FilterNode(context.getIdAllocator().getNextId(), result.getPlanNode(), castToRowExpression(and(result.getPresentExpressions()))), AssignmentUtils.identityAsSymbolReferences(node.getOutputSymbols())));
}
Also used : FilterNode(io.prestosql.spi.plan.FilterNode) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 27 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class ImplementLimitWithTies method apply.

@Override
public Result apply(LimitNode parent, Captures captures, Context context) {
    PlanNode child = captures.get(CHILD);
    Symbol rankSymbol = context.getSymbolAllocator().newSymbol("rank_num", BIGINT);
    WindowNode.Frame frame = new WindowNode.Frame(WindowFrameType.RANGE, FrameBoundType.UNBOUNDED_PRECEDING, Optional.empty(), FrameBoundType.CURRENT_ROW, Optional.empty(), Optional.empty(), Optional.empty());
    FunctionHandle functionHandle = metadata.getFunctionAndTypeManager().lookupFunction("rank", ImmutableList.of());
    WindowNode.Function rankFunction = new WindowNode.Function(call(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "rank").toString(), functionHandle, BIGINT), ImmutableList.of(), frame);
    WindowNode windowNode = new WindowNode(context.getIdAllocator().getNextId(), child, new WindowNode.Specification(ImmutableList.of(), parent.getTiesResolvingScheme()), ImmutableMap.of(rankSymbol, rankFunction), Optional.empty(), ImmutableSet.of(), 0);
    FilterNode filterNode = new FilterNode(context.getIdAllocator().getNextId(), windowNode, castToRowExpression(new ComparisonExpression(ComparisonExpression.Operator.LESS_THAN_OR_EQUAL, toSymbolReference(rankSymbol), new GenericLiteral("BIGINT", Long.toString(parent.getCount())))));
    ProjectNode projectNode = new ProjectNode(context.getIdAllocator().getNextId(), filterNode, AssignmentUtils.identityAsSymbolReferences(parent.getOutputSymbols()));
    return Result.ofPlanNode(projectNode);
}
Also used : WindowNode(io.prestosql.spi.plan.WindowNode) Symbol(io.prestosql.spi.plan.Symbol) FilterNode(io.prestosql.spi.plan.FilterNode) GenericLiteral(io.prestosql.sql.tree.GenericLiteral) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) PlanNode(io.prestosql.spi.plan.PlanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) FunctionHandle(io.prestosql.spi.function.FunctionHandle)

Example 28 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class ImplementOffset method apply.

@Override
public Result apply(OffsetNode parent, Captures captures, Context context) {
    Symbol rowNumberSymbol = context.getSymbolAllocator().newSymbol("row_number", BIGINT);
    RowNumberNode rowNumberNode = new RowNumberNode(context.getIdAllocator().getNextId(), parent.getSource(), ImmutableList.of(), rowNumberSymbol, Optional.empty(), Optional.empty());
    FilterNode filterNode = new FilterNode(context.getIdAllocator().getNextId(), rowNumberNode, castToRowExpression(new ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, toSymbolReference(rowNumberSymbol), new GenericLiteral("BIGINT", Long.toString(parent.getCount())))));
    ProjectNode projectNode = new ProjectNode(context.getIdAllocator().getNextId(), filterNode, AssignmentUtils.identityAsSymbolReferences(parent.getOutputSymbols()));
    return Result.ofPlanNode(projectNode);
}
Also used : ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Symbol(io.prestosql.spi.plan.Symbol) FilterNode(io.prestosql.spi.plan.FilterNode) RowNumberNode(io.prestosql.sql.planner.plan.RowNumberNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) GenericLiteral(io.prestosql.sql.tree.GenericLiteral)

Example 29 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class InlineProjections method apply.

@Override
public Result apply(ProjectNode parent, Captures captures, Context context) {
    ProjectNode child = captures.get(CHILD);
    Sets.SetView<Symbol> targets = extractInliningTargets(parent, child);
    if (targets.isEmpty()) {
        return Result.empty();
    }
    // inline the expressions
    Assignments assignments = child.getAssignments().filter(targets::contains);
    Map<Symbol, RowExpression> parentAssignments = parent.getAssignments().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> (inlineReferences(entry.getValue(), assignments))));
    // Synthesize identity assignments for the inputs of expressions that were inlined
    // to place in the child projection.
    // If all assignments end up becoming identity assignments, they'll get pruned by
    // other rules
    Set<Symbol> inputs = child.getAssignments().entrySet().stream().filter(entry -> targets.contains(entry.getKey())).map(Map.Entry::getValue).flatMap(entry -> extractInputs(entry).stream()).collect(toSet());
    Assignments.Builder childAssignments = Assignments.builder();
    for (Map.Entry<Symbol, RowExpression> assignment : child.getAssignments().entrySet()) {
        if (!targets.contains(assignment.getKey())) {
            childAssignments.put(assignment);
        }
    }
    boolean allTranslated = child.getAssignments().entrySet().stream().map(Map.Entry::getValue).noneMatch(OriginalExpressionUtils::isExpression);
    for (Symbol input : inputs) {
        if (allTranslated) {
            Type inputType = context.getSymbolAllocator().getSymbols().get(input);
            childAssignments.put(input, new VariableReferenceExpression(input.getName(), inputType));
        } else {
            childAssignments.put(input, castToRowExpression(toSymbolReference(input)));
        }
    }
    return Result.ofPlanNode(new ProjectNode(parent.getId(), new ProjectNode(child.getId(), child.getSource(), childAssignments.build()), Assignments.copyOf(parentAssignments)));
}
Also used : Patterns.source(io.prestosql.sql.planner.plan.Patterns.source) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) ExpressionSymbolInliner.inlineSymbols(io.prestosql.sql.planner.ExpressionSymbolInliner.inlineSymbols) Literal(io.prestosql.sql.tree.Literal) TryFunction(io.prestosql.operator.scalar.TryFunction) Pattern(io.prestosql.matching.Pattern) RowExpressionVariableInliner(io.prestosql.sql.planner.RowExpressionVariableInliner) Function(java.util.function.Function) OriginalExpressionUtils.isExpression(io.prestosql.sql.relational.OriginalExpressionUtils.isExpression) CallExpression(io.prestosql.spi.relation.CallExpression) Capture.newCapture(io.prestosql.matching.Capture.newCapture) Map(java.util.Map) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) Type(io.prestosql.spi.type.Type) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Patterns.project(io.prestosql.sql.planner.plan.Patterns.project) Collectors.toSet(java.util.stream.Collectors.toSet) Symbol(io.prestosql.spi.plan.Symbol) ImmutableSet(com.google.common.collect.ImmutableSet) SymbolsExtractor(io.prestosql.sql.planner.SymbolsExtractor) Assignments(io.prestosql.spi.plan.Assignments) Rule(io.prestosql.sql.planner.iterative.Rule) DefaultRowExpressionTraversalVisitor(io.prestosql.expressions.DefaultRowExpressionTraversalVisitor) Set(java.util.Set) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ProjectNode(io.prestosql.spi.plan.ProjectNode) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Metadata(io.prestosql.metadata.Metadata) Captures(io.prestosql.matching.Captures) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) AstUtils(io.prestosql.sql.util.AstUtils) List(java.util.List) FunctionResolution(io.prestosql.sql.relational.FunctionResolution) Capture(io.prestosql.matching.Capture) RowExpression(io.prestosql.spi.relation.RowExpression) TryExpression(io.prestosql.sql.tree.TryExpression) AssignmentUtils.isIdentity(io.prestosql.sql.planner.plan.AssignmentUtils.isIdentity) Expression(io.prestosql.sql.tree.Expression) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Type(io.prestosql.spi.type.Type) Sets(com.google.common.collect.Sets) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) ProjectNode(io.prestosql.spi.plan.ProjectNode) OriginalExpressionUtils(io.prestosql.sql.relational.OriginalExpressionUtils) Map(java.util.Map)

Example 30 with ProjectNode

use of io.prestosql.spi.plan.ProjectNode in project hetu-core by openlookeng.

the class PushProjectionIntoTableScan method apply.

@Override
public Result apply(ProjectNode project, Captures captures, Context context) {
    TableScanNode tableScan = captures.get(TABLE_SCAN);
    List<ConnectorExpression> projections;
    try {
        projections = project.getAssignments().getExpressions().stream().map(expression -> ConnectorExpressionTranslator.translate(expression)).collect(toImmutableList());
    } catch (UnsupportedOperationException e) {
        // rewritten in terms of the new assignments for the columns passed in #2
        return Result.empty();
    }
    Map<String, ColumnHandle> assignments = tableScan.getAssignments().entrySet().stream().collect(toImmutableMap(entry -> entry.getKey().getName(), Map.Entry::getValue));
    Optional<ProjectionApplicationResult<TableHandle>> result = metadata.applyProjection(context.getSession(), tableScan.getTable(), projections, assignments);
    if (!result.isPresent()) {
        return Result.empty();
    }
    List<Symbol> newScanOutputs = new ArrayList<>();
    Map<Symbol, ColumnHandle> newScanAssignments = new HashMap<>();
    Map<String, Symbol> variableMappings = new HashMap<>();
    for (ProjectionApplicationResult.Assignment assignment : result.get().getAssignments()) {
        Symbol symbol = context.getSymbolAllocator().newSymbol(assignment.getVariable(), assignment.getType());
        newScanOutputs.add(symbol);
        newScanAssignments.put(symbol, assignment.getColumn());
        variableMappings.put(assignment.getVariable(), symbol);
    }
    // TODO: ensure newProjections.size == original projections.size
    List<RowExpression> newProjections = result.get().getProjections().stream().map(expression -> ConnectorExpressionTranslator.translate(expression, variableMappings, new LiteralEncoder(metadata))).collect(toImmutableList());
    Assignments.Builder newProjectionAssignments = Assignments.builder();
    for (int i = 0; i < project.getOutputSymbols().size(); i++) {
        newProjectionAssignments.put(project.getOutputSymbols().get(i), newProjections.get(i));
    }
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), TableScanNode.newInstance(tableScan.getId(), result.get().getHandle(), newScanOutputs, newScanAssignments, tableScan.getStrategy(), tableScan.getReuseTableScanMappingId(), tableScan.getConsumerTableScanNodeCount(), tableScan.isForDelete()), newProjectionAssignments.build()));
}
Also used : Patterns.source(io.prestosql.sql.planner.plan.Patterns.source) ProjectionApplicationResult(io.prestosql.spi.connector.ProjectionApplicationResult) HashMap(java.util.HashMap) Pattern(io.prestosql.matching.Pattern) TableHandle(io.prestosql.spi.metadata.TableHandle) ArrayList(java.util.ArrayList) TypeAnalyzer(io.prestosql.sql.planner.TypeAnalyzer) ConnectorExpressionTranslator(io.prestosql.spi.expression.ConnectorExpressionTranslator) Capture.newCapture(io.prestosql.matching.Capture.newCapture) Map(java.util.Map) Patterns.project(io.prestosql.sql.planner.plan.Patterns.project) Symbol(io.prestosql.spi.plan.Symbol) Assignments(io.prestosql.spi.plan.Assignments) Rule(io.prestosql.sql.planner.iterative.Rule) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TableScanNode(io.prestosql.spi.plan.TableScanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Metadata(io.prestosql.metadata.Metadata) Captures(io.prestosql.matching.Captures) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) Capture(io.prestosql.matching.Capture) RowExpression(io.prestosql.spi.relation.RowExpression) LiteralEncoder(io.prestosql.sql.planner.LiteralEncoder) Optional(java.util.Optional) Patterns.tableScan(io.prestosql.sql.planner.plan.Patterns.tableScan) ConnectorExpression(io.prestosql.spi.expression.ConnectorExpression) HashMap(java.util.HashMap) ConnectorExpression(io.prestosql.spi.expression.ConnectorExpression) Symbol(io.prestosql.spi.plan.Symbol) ArrayList(java.util.ArrayList) Assignments(io.prestosql.spi.plan.Assignments) LiteralEncoder(io.prestosql.sql.planner.LiteralEncoder) ColumnHandle(io.prestosql.spi.connector.ColumnHandle) RowExpression(io.prestosql.spi.relation.RowExpression) ProjectionApplicationResult(io.prestosql.spi.connector.ProjectionApplicationResult) TableScanNode(io.prestosql.spi.plan.TableScanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap)

Aggregations

ProjectNode (io.prestosql.spi.plan.ProjectNode)79 Symbol (io.prestosql.spi.plan.Symbol)56 Assignments (io.prestosql.spi.plan.Assignments)47 PlanNode (io.prestosql.spi.plan.PlanNode)40 RowExpression (io.prestosql.spi.relation.RowExpression)30 Expression (io.prestosql.sql.tree.Expression)30 ImmutableList (com.google.common.collect.ImmutableList)29 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)27 ImmutableMap (com.google.common.collect.ImmutableMap)25 AggregationNode (io.prestosql.spi.plan.AggregationNode)25 Map (java.util.Map)24 FilterNode (io.prestosql.spi.plan.FilterNode)22 TableScanNode (io.prestosql.spi.plan.TableScanNode)21 Cast (io.prestosql.sql.tree.Cast)20 List (java.util.List)19 CallExpression (io.prestosql.spi.relation.CallExpression)18 HashMap (java.util.HashMap)18 Type (io.prestosql.spi.type.Type)17 Test (org.testng.annotations.Test)17 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)16