Search in sources :

Example 6 with OrderingScheme

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

the class PlanPrinter method formatAggregation.

public static String formatAggregation(Aggregation aggregation) {
    StringBuilder builder = new StringBuilder();
    String arguments = Joiner.on(", ").join(aggregation.getArguments());
    if (aggregation.getArguments().isEmpty() && "count".equalsIgnoreCase(aggregation.getResolvedFunction().getSignature().getName())) {
        arguments = "*";
    }
    if (aggregation.isDistinct()) {
        arguments = "DISTINCT " + arguments;
    }
    builder.append(aggregation.getResolvedFunction().getSignature().getName()).append('(').append(arguments);
    aggregation.getOrderingScheme().ifPresent(orderingScheme -> builder.append(' ').append(orderingScheme.getOrderBy().stream().map(input -> input + " " + orderingScheme.getOrdering(input)).collect(joining(", "))));
    builder.append(')');
    aggregation.getFilter().ifPresent(expression -> builder.append(" FILTER (WHERE ").append(expression).append(")"));
    aggregation.getMask().ifPresent(symbol -> builder.append(" (mask = ").append(symbol).append(")"));
    return builder.toString();
}
Also used : PlanFragment(io.trino.sql.planner.PlanFragment) Scope(io.trino.sql.planner.plan.ExchangeNode.Scope) ColumnStatisticMetadata(io.trino.spi.statistics.ColumnStatisticMetadata) AggregationValuePointer(io.trino.sql.planner.rowpattern.AggregationValuePointer) CorrelatedJoinNode(io.trino.sql.planner.plan.CorrelatedJoinNode) PlanNode(io.trino.sql.planner.plan.PlanNode) RemoteSourceNode(io.trino.sql.planner.plan.RemoteSourceNode) TypedSymbol(io.trino.sql.planner.planprinter.NodeRepresentation.TypedSymbol) DynamicFilters(io.trino.sql.DynamicFilters) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) Map(java.util.Map) TextRenderer.formatPositions(io.trino.sql.planner.planprinter.TextRenderer.formatPositions) OutputNode(io.trino.sql.planner.plan.OutputNode) TableScanNode(io.trino.sql.planner.plan.TableScanNode) PlanNodeStatsEstimate(io.trino.cost.PlanNodeStatsEstimate) ExplainAnalyzeNode(io.trino.sql.planner.plan.ExplainAnalyzeNode) Range(io.trino.spi.predicate.Range) Domain(io.trino.spi.predicate.Domain) StatisticsWriterNode(io.trino.sql.planner.plan.StatisticsWriterNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Assignments(io.trino.sql.planner.plan.Assignments) Set(java.util.Set) SemiJoinNode(io.trino.sql.planner.plan.SemiJoinNode) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Collectors.joining(java.util.stream.Collectors.joining) IntersectNode(io.trino.sql.planner.plan.IntersectNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Stream(java.util.stream.Stream) SubPlan(io.trino.sql.planner.SubPlan) SymbolReference(io.trino.sql.tree.SymbolReference) TopNRankingNode(io.trino.sql.planner.plan.TopNRankingNode) PlanFragmentId(io.trino.sql.planner.plan.PlanFragmentId) ValuesNode(io.trino.sql.planner.plan.ValuesNode) Position(io.trino.sql.tree.SkipTo.Position) Joiner(com.google.common.base.Joiner) Session(io.trino.Session) TableExecuteNode(io.trino.sql.planner.plan.TableExecuteNode) NullableValue(io.trino.spi.predicate.NullableValue) LimitNode(io.trino.sql.planner.plan.LimitNode) StageStats(io.trino.execution.StageStats) PlanCostEstimate(io.trino.cost.PlanCostEstimate) StatsAndCosts(io.trino.cost.StatsAndCosts) DynamicFilterId(io.trino.sql.planner.plan.DynamicFilterId) ArrayList(java.util.ArrayList) RowNumberNode(io.trino.sql.planner.plan.RowNumberNode) ColumnHandle(io.trino.spi.connector.ColumnHandle) AggregationNode(io.trino.sql.planner.plan.AggregationNode) StageExecutionDescriptor.ungroupedExecution(io.trino.operator.StageExecutionDescriptor.ungroupedExecution) TextRenderer.formatDouble(io.trino.sql.planner.planprinter.TextRenderer.formatDouble) StageInfo.getAllStages(io.trino.execution.StageInfo.getAllStages) StageExecutionDescriptor(io.trino.operator.StageExecutionDescriptor) IrLabel(io.trino.sql.planner.rowpattern.ir.IrLabel) UnnestNode(io.trino.sql.planner.plan.UnnestNode) TableHandle(io.trino.metadata.TableHandle) ExceptNode(io.trino.sql.planner.plan.ExceptNode) GroupIdNode(io.trino.sql.planner.plan.GroupIdNode) OffsetNode(io.trino.sql.planner.plan.OffsetNode) TableFinishNode(io.trino.sql.planner.plan.TableFinishNode) SampleNode(io.trino.sql.planner.plan.SampleNode) IndexSourceNode(io.trino.sql.planner.plan.IndexSourceNode) GroupReference(io.trino.sql.planner.iterative.GroupReference) Aggregation(io.trino.sql.planner.plan.AggregationNode.Aggregation) TableWriterNode(io.trino.sql.planner.plan.TableWriterNode) DynamicFilters.extractDynamicFilters(io.trino.sql.DynamicFilters.extractDynamicFilters) DynamicFilterDomainStats(io.trino.server.DynamicFilterService.DynamicFilterDomainStats) Math.abs(java.lang.Math.abs) FilterNode(io.trino.sql.planner.plan.FilterNode) Duration(io.airlift.units.Duration) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TableDeleteNode(io.trino.sql.planner.plan.TableDeleteNode) TextRenderer.indentString(io.trino.sql.planner.planprinter.TextRenderer.indentString) SpatialJoinNode(io.trino.sql.planner.plan.SpatialJoinNode) DeleteNode(io.trino.sql.planner.plan.DeleteNode) LogicalIndexPointer(io.trino.sql.planner.rowpattern.LogicalIndexPointer) SINGLE_DISTRIBUTION(io.trino.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION) WINDOW(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch.WINDOW) JoinNode(io.trino.sql.planner.plan.JoinNode) AssignUniqueId(io.trino.sql.planner.plan.AssignUniqueId) FunctionCall(io.trino.sql.tree.FunctionCall) PlanNodeStatsSummarizer.aggregateStageStats(io.trino.sql.planner.planprinter.PlanNodeStatsSummarizer.aggregateStageStats) MarkDistinctNode(io.trino.sql.planner.plan.MarkDistinctNode) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) EnforceSingleRowNode(io.trino.sql.planner.plan.EnforceSingleRowNode) ExpressionTreeRewriter(io.trino.sql.tree.ExpressionTreeRewriter) Collection(java.util.Collection) IndexJoinNode(io.trino.sql.planner.plan.IndexJoinNode) Streams(com.google.common.collect.Streams) PatternRecognitionNode(io.trino.sql.planner.plan.PatternRecognitionNode) Collectors(java.util.stream.Collectors) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) List(java.util.List) StatisticAggregationsDescriptor(io.trino.sql.planner.plan.StatisticAggregationsDescriptor) TableInfo(io.trino.execution.TableInfo) Entry(java.util.Map.Entry) Function.identity(java.util.function.Function.identity) ApplyNode(io.trino.sql.planner.plan.ApplyNode) Optional(java.util.Optional) ExchangeNode(io.trino.sql.planner.plan.ExchangeNode) Expression(io.trino.sql.tree.Expression) TableStatisticType(io.trino.spi.statistics.TableStatisticType) WindowNode(io.trino.sql.planner.plan.WindowNode) Arrays.stream(java.util.Arrays.stream) UnionNode(io.trino.sql.planner.plan.UnionNode) INNER(io.trino.sql.planner.plan.JoinNode.Type.INNER) Type(io.trino.spi.type.Type) Measure(io.trino.sql.planner.plan.PatternRecognitionNode.Measure) StatisticAggregations(io.trino.sql.planner.plan.StatisticAggregations) Partitioning(io.trino.sql.planner.Partitioning) PartitioningScheme(io.trino.sql.planner.PartitioningScheme) SortNode(io.trino.sql.planner.plan.SortNode) Function(java.util.function.Function) ImmutableList(com.google.common.collect.ImmutableList) Verify.verify(com.google.common.base.Verify.verify) Objects.requireNonNull(java.util.Objects.requireNonNull) RefreshMaterializedViewNode(io.trino.sql.planner.plan.RefreshMaterializedViewNode) ExpressionRewriter(io.trino.sql.tree.ExpressionRewriter) LinkedList(java.util.LinkedList) ProjectNode(io.trino.sql.planner.plan.ProjectNode) ExpressionAndValuePointers(io.trino.sql.planner.rowpattern.LogicalIndexExtractor.ExpressionAndValuePointers) StageInfo(io.trino.execution.StageInfo) Symbol(io.trino.sql.planner.Symbol) ExpressionUtils.combineConjunctsWithDuplicates(io.trino.sql.ExpressionUtils.combineConjunctsWithDuplicates) RowsPerMatch(io.trino.sql.tree.PatternRecognitionRelation.RowsPerMatch) CaseFormat(com.google.common.base.CaseFormat) PlanVisitor(io.trino.sql.planner.plan.PlanVisitor) TopNNode(io.trino.sql.planner.plan.TopNNode) ScalarValuePointer(io.trino.sql.planner.rowpattern.ScalarValuePointer) TRUE_LITERAL(io.trino.sql.tree.BooleanLiteral.TRUE_LITERAL) TupleDomain(io.trino.spi.predicate.TupleDomain) OrderingScheme(io.trino.sql.planner.OrderingScheme) PlanNodeStatsAndCostSummary(io.trino.cost.PlanNodeStatsAndCostSummary) FunctionManager(io.trino.metadata.FunctionManager) UpdateNode(io.trino.sql.planner.plan.UpdateNode) QualifiedName(io.trino.sql.tree.QualifiedName) UPPER_UNDERSCORE(com.google.common.base.CaseFormat.UPPER_UNDERSCORE) Collectors.toList(java.util.stream.Collectors.toList) QueryStats(io.trino.execution.QueryStats) DistinctLimitNode(io.trino.sql.planner.plan.DistinctLimitNode) ValuePointer(io.trino.sql.planner.rowpattern.ValuePointer) Row(io.trino.sql.tree.Row) Metadata(io.trino.metadata.Metadata) TypeProvider(io.trino.sql.planner.TypeProvider) ResolvedFunction.extractFunctionName(io.trino.metadata.ResolvedFunction.extractFunctionName) TextRenderer.indentString(io.trino.sql.planner.planprinter.TextRenderer.indentString)

Example 7 with OrderingScheme

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

the class TestPruneWindowColumns method buildProjectedWindow.

private static PlanNode buildProjectedWindow(PlanBuilder p, Predicate<Symbol> projectionFilter, Predicate<Symbol> sourceFilter) {
    Symbol orderKey = p.symbol("orderKey");
    Symbol partitionKey = p.symbol("partitionKey");
    Symbol hash = p.symbol("hash");
    Symbol startValue1 = p.symbol("startValue1");
    Symbol startValue2 = p.symbol("startValue2");
    Symbol endValue1 = p.symbol("endValue1");
    Symbol endValue2 = p.symbol("endValue2");
    Symbol input1 = p.symbol("input1");
    Symbol input2 = p.symbol("input2");
    Symbol unused = p.symbol("unused");
    Symbol output1 = p.symbol("output1");
    Symbol output2 = p.symbol("output2");
    List<Symbol> inputs = ImmutableList.of(orderKey, partitionKey, hash, startValue1, startValue2, endValue1, endValue2, input1, input2, unused);
    List<Symbol> outputs = ImmutableList.<Symbol>builder().addAll(inputs).add(output1, output2).build();
    return p.project(Assignments.identity(outputs.stream().filter(projectionFilter).collect(toImmutableList())), p.window(new WindowNode.Specification(ImmutableList.of(partitionKey), Optional.of(new OrderingScheme(ImmutableList.of(orderKey), ImmutableMap.of(orderKey, SortOrder.ASC_NULLS_FIRST)))), ImmutableMap.of(output1, new WindowNode.Function(MIN_FUNCTION, ImmutableList.of(input1.toSymbolReference()), new WindowNode.Frame(WindowFrame.Type.RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue1), Optional.of(orderKey), CURRENT_ROW, Optional.of(endValue1), Optional.of(orderKey), Optional.of(startValue1.toSymbolReference()), Optional.of(endValue2.toSymbolReference())), false), output2, new WindowNode.Function(MIN_FUNCTION, ImmutableList.of(input2.toSymbolReference()), new WindowNode.Frame(WindowFrame.Type.RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue2), Optional.of(orderKey), CURRENT_ROW, Optional.of(endValue2), Optional.of(orderKey), Optional.of(startValue2.toSymbolReference()), Optional.of(endValue2.toSymbolReference())), false)), hash, p.values(inputs.stream().filter(sourceFilter).collect(toImmutableList()), ImmutableList.of())));
}
Also used : OrderingScheme(io.trino.sql.planner.OrderingScheme) WindowNode(io.trino.sql.planner.plan.WindowNode) PlanMatchPattern.windowFrame(io.trino.sql.planner.assertions.PlanMatchPattern.windowFrame) WindowFrame(io.trino.sql.tree.WindowFrame) Symbol(io.trino.sql.planner.Symbol)

Example 8 with OrderingScheme

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

the class PushAggregationIntoTableScan method toAggregateFunction.

private static AggregateFunction toAggregateFunction(Metadata metadata, Context context, AggregationNode.Aggregation aggregation) {
    String canonicalName = metadata.getFunctionMetadata(aggregation.getResolvedFunction()).getCanonicalName();
    BoundSignature signature = aggregation.getResolvedFunction().getSignature();
    ImmutableList.Builder<ConnectorExpression> arguments = ImmutableList.builder();
    for (int i = 0; i < aggregation.getArguments().size(); i++) {
        SymbolReference argument = (SymbolReference) aggregation.getArguments().get(i);
        arguments.add(new Variable(argument.getName(), signature.getArgumentTypes().get(i)));
    }
    Optional<OrderingScheme> orderingScheme = aggregation.getOrderingScheme();
    Optional<List<SortItem>> sortBy = orderingScheme.map(OrderingScheme::toSortItems);
    Optional<ConnectorExpression> filter = aggregation.getFilter().map(symbol -> new Variable(symbol.getName(), context.getSymbolAllocator().getTypes().get(symbol)));
    return new AggregateFunction(canonicalName, signature.getReturnType(), arguments.build(), sortBy.orElse(ImmutableList.of()), aggregation.isDistinct(), filter);
}
Also used : OrderingScheme(io.trino.sql.planner.OrderingScheme) Variable(io.trino.spi.expression.Variable) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) SymbolReference(io.trino.sql.tree.SymbolReference) BoundSignature(io.trino.metadata.BoundSignature) AggregateFunction(io.trino.spi.connector.AggregateFunction) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List)

Example 9 with OrderingScheme

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

the class SymbolMapper method map.

public OrderingScheme map(OrderingScheme orderingScheme) {
    ImmutableList.Builder<Symbol> newSymbols = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, SortOrder> newOrderings = ImmutableMap.builder();
    Set<Symbol> added = new HashSet<>(orderingScheme.getOrderBy().size());
    for (Symbol symbol : orderingScheme.getOrderBy()) {
        Symbol canonical = map(symbol);
        if (added.add(canonical)) {
            newSymbols.add(canonical);
            newOrderings.put(canonical, orderingScheme.getOrdering(symbol));
        }
    }
    return new OrderingScheme(newSymbols.build(), newOrderings.buildOrThrow());
}
Also used : OrderingScheme(io.trino.sql.planner.OrderingScheme) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Symbol(io.trino.sql.planner.Symbol) SortOrder(io.trino.spi.connector.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) HashSet(java.util.HashSet)

Example 10 with OrderingScheme

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

the class LimitMatcher method detailMatches.

@Override
public MatchResult detailMatches(PlanNode node, StatsProvider stats, Session session, Metadata metadata, SymbolAliases symbolAliases) {
    checkState(shapeMatches(node));
    LimitNode limitNode = (LimitNode) node;
    if (!limitNode.isWithTies()) {
        return match();
    }
    OrderingScheme tiesResolvingScheme = limitNode.getTiesResolvingScheme().get();
    if (orderingSchemeMatches(tiesResolvers, tiesResolvingScheme, symbolAliases)) {
        return match();
    }
    if (!limitNode.requiresPreSortedInputs()) {
        return match();
    }
    if (preSortedInputs.stream().map(alias -> alias.toSymbol(symbolAliases)).collect(toImmutableSet()).equals(ImmutableSet.copyOf(limitNode.getPreSortedInputs()))) {
        return match();
    }
    return NO_MATCH;
}
Also used : OrderingScheme(io.trino.sql.planner.OrderingScheme) LimitNode(io.trino.sql.planner.plan.LimitNode)

Aggregations

OrderingScheme (io.trino.sql.planner.OrderingScheme)18 ImmutableList (com.google.common.collect.ImmutableList)15 Symbol (io.trino.sql.planner.Symbol)15 WindowNode (io.trino.sql.planner.plan.WindowNode)13 ImmutableMap (com.google.common.collect.ImmutableMap)11 ResolvedFunction (io.trino.metadata.ResolvedFunction)10 QualifiedName (io.trino.sql.tree.QualifiedName)10 Optional (java.util.Optional)10 Test (org.testng.annotations.Test)10 SortOrder (io.trino.spi.connector.SortOrder)9 TypeSignatureProvider.fromTypes (io.trino.sql.analyzer.TypeSignatureProvider.fromTypes)9 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)9 BaseRuleTest (io.trino.sql.planner.iterative.rule.test.BaseRuleTest)9 DEFAULT_FRAME (io.trino.sql.planner.plan.WindowNode.Frame.DEFAULT_FRAME)9 PlanMatchPattern.topNRanking (io.trino.sql.planner.assertions.PlanMatchPattern.topNRanking)7 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)5 Assignments (io.trino.sql.planner.plan.Assignments)5 SymbolReference (io.trino.sql.tree.SymbolReference)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 BIGINT (io.trino.spi.type.BigintType.BIGINT)4