|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.calcite.plan; |
| 7 | + |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import lombok.Getter; |
| 11 | +import org.apache.calcite.plan.Convention; |
| 12 | +import org.apache.calcite.plan.RelOptCluster; |
| 13 | +import org.apache.calcite.plan.RelTraitSet; |
| 14 | +import org.apache.calcite.rel.RelCollation; |
| 15 | +import org.apache.calcite.rel.RelCollationTraitDef; |
| 16 | +import org.apache.calcite.rel.RelNode; |
| 17 | +import org.apache.calcite.rel.RelWriter; |
| 18 | +import org.apache.calcite.rel.core.Sort; |
| 19 | +import org.apache.calcite.rel.hint.RelHint; |
| 20 | +import org.apache.calcite.rel.logical.LogicalSort; |
| 21 | +import org.apache.calcite.rex.RexNode; |
| 22 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 23 | + |
| 24 | +/** System level limit logical plan, comparing to user level plan {@link LogicalSort}. */ |
| 25 | +public class LogicalSystemLimit extends Sort { |
| 26 | + |
| 27 | + public enum SystemLimitType { |
| 28 | + /** |
| 29 | + * System limit type for system level limit. |
| 30 | + * |
| 31 | + * <p>This type is used to indicate that the limit is applied to the system level. |
| 32 | + */ |
| 33 | + QUERY_SIZE_LIMIT |
| 34 | + } |
| 35 | + |
| 36 | + @Getter private final SystemLimitType type; |
| 37 | + |
| 38 | + private LogicalSystemLimit( |
| 39 | + SystemLimitType type, |
| 40 | + RelOptCluster cluster, |
| 41 | + RelTraitSet traitSet, |
| 42 | + RelNode input, |
| 43 | + RelCollation collation, |
| 44 | + @Nullable RexNode offset, |
| 45 | + @Nullable RexNode fetch) { |
| 46 | + this(type, cluster, traitSet, Collections.emptyList(), input, collation, offset, fetch); |
| 47 | + } |
| 48 | + |
| 49 | + private LogicalSystemLimit( |
| 50 | + SystemLimitType type, |
| 51 | + RelOptCluster cluster, |
| 52 | + RelTraitSet traitSet, |
| 53 | + List<RelHint> hints, |
| 54 | + RelNode input, |
| 55 | + RelCollation collation, |
| 56 | + @Nullable RexNode offset, |
| 57 | + @Nullable RexNode fetch) { |
| 58 | + super(cluster, traitSet, hints, input, collation, offset, fetch); |
| 59 | + assert traitSet.containsIfApplicable(Convention.NONE); |
| 60 | + this.type = type; |
| 61 | + } |
| 62 | + |
| 63 | + public static LogicalSystemLimit create(SystemLimitType type, RelNode input, RexNode fetch) { |
| 64 | + return create(type, input, input.getTraitSet().getCollation(), null, fetch); |
| 65 | + } |
| 66 | + |
| 67 | + public static LogicalSystemLimit create( |
| 68 | + SystemLimitType type, |
| 69 | + RelNode input, |
| 70 | + RelCollation collation, |
| 71 | + @Nullable RexNode offset, |
| 72 | + @Nullable RexNode fetch) { |
| 73 | + RelOptCluster cluster = input.getCluster(); |
| 74 | + collation = RelCollationTraitDef.INSTANCE.canonize(collation); |
| 75 | + RelTraitSet traitSet = input.getTraitSet().replace(Convention.NONE).replace(collation); |
| 76 | + return new LogicalSystemLimit(type, cluster, traitSet, input, collation, offset, fetch); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Sort copy( |
| 81 | + RelTraitSet traitSet, |
| 82 | + RelNode newInput, |
| 83 | + RelCollation newCollation, |
| 84 | + @Nullable RexNode offset, |
| 85 | + @Nullable RexNode fetch) { |
| 86 | + return new LogicalSystemLimit( |
| 87 | + this.type, getCluster(), traitSet, hints, newInput, newCollation, offset, fetch); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public RelWriter explainTerms(RelWriter pw) { |
| 92 | + super.explainTerms(pw); |
| 93 | + // Show type in the explain |
| 94 | + pw.item("type", type); |
| 95 | + return pw; |
| 96 | + } |
| 97 | +} |
0 commit comments