Search in sources :

Example 1 with SortItem

use of io.trino.spi.connector.SortItem in project trino by trinodb.

the class PushTopNIntoTableScan method apply.

@Override
public Result apply(TopNNode topNNode, Captures captures, Context context) {
    TableScanNode tableScan = captures.get(TABLE_SCAN);
    long topNCount = topNNode.getCount();
    List<SortItem> sortItems = topNNode.getOrderingScheme().toSortItems();
    Map<String, ColumnHandle> assignments = tableScan.getAssignments().entrySet().stream().collect(toImmutableMap(entry -> entry.getKey().getName(), Map.Entry::getValue));
    return metadata.applyTopN(context.getSession(), tableScan.getTable(), topNCount, sortItems, assignments).map(result -> {
        PlanNode node = new TableScanNode(context.getIdAllocator().getNextId(), result.getHandle(), tableScan.getOutputSymbols(), tableScan.getAssignments(), TupleDomain.all(), deriveTableStatisticsForPushdown(context.getStatsProvider(), context.getSession(), result.isPrecalculateStatistics(), result.isTopNGuaranteed() ? topNNode : tableScan), tableScan.isUpdateTarget(), // table scan partitioning might have changed with new table handle
        Optional.empty());
        if (!result.isTopNGuaranteed()) {
            node = topNNode.replaceChildren(ImmutableList.of(node));
        }
        return Result.ofPlanNode(node);
    }).orElseGet(Result::empty);
}
Also used : Rules.deriveTableStatisticsForPushdown(io.trino.sql.planner.iterative.rule.Rules.deriveTableStatisticsForPushdown) SortItem(io.trino.spi.connector.SortItem) TopNNode(io.trino.sql.planner.plan.TopNNode) Patterns.topN(io.trino.sql.planner.plan.Patterns.topN) TupleDomain(io.trino.spi.predicate.TupleDomain) Patterns.tableScan(io.trino.sql.planner.plan.Patterns.tableScan) Capture.newCapture(io.trino.matching.Capture.newCapture) Capture(io.trino.matching.Capture) PlanNode(io.trino.sql.planner.plan.PlanNode) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Pattern(io.trino.matching.Pattern) SystemSessionProperties.isAllowPushdownIntoConnectors(io.trino.SystemSessionProperties.isAllowPushdownIntoConnectors) ImmutableList(com.google.common.collect.ImmutableList) Patterns.source(io.trino.sql.planner.plan.Patterns.source) Captures(io.trino.matching.Captures) Map(java.util.Map) Metadata(io.trino.metadata.Metadata) ColumnHandle(io.trino.spi.connector.ColumnHandle) Optional(java.util.Optional) Rule(io.trino.sql.planner.iterative.Rule) TableScanNode(io.trino.sql.planner.plan.TableScanNode) Session(io.trino.Session) ColumnHandle(io.trino.spi.connector.ColumnHandle) SortItem(io.trino.spi.connector.SortItem) PlanNode(io.trino.sql.planner.plan.PlanNode) TableScanNode(io.trino.sql.planner.plan.TableScanNode) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Map(java.util.Map)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)1 Session (io.trino.Session)1 SystemSessionProperties.isAllowPushdownIntoConnectors (io.trino.SystemSessionProperties.isAllowPushdownIntoConnectors)1 Capture (io.trino.matching.Capture)1 Capture.newCapture (io.trino.matching.Capture.newCapture)1 Captures (io.trino.matching.Captures)1 Pattern (io.trino.matching.Pattern)1 Metadata (io.trino.metadata.Metadata)1 ColumnHandle (io.trino.spi.connector.ColumnHandle)1 SortItem (io.trino.spi.connector.SortItem)1 TupleDomain (io.trino.spi.predicate.TupleDomain)1 Rule (io.trino.sql.planner.iterative.Rule)1 Rules.deriveTableStatisticsForPushdown (io.trino.sql.planner.iterative.rule.Rules.deriveTableStatisticsForPushdown)1 Patterns.source (io.trino.sql.planner.plan.Patterns.source)1 Patterns.tableScan (io.trino.sql.planner.plan.Patterns.tableScan)1 Patterns.topN (io.trino.sql.planner.plan.Patterns.topN)1 PlanNode (io.trino.sql.planner.plan.PlanNode)1 TableScanNode (io.trino.sql.planner.plan.TableScanNode)1 TopNNode (io.trino.sql.planner.plan.TopNNode)1