|
| 1 | +package org.opensearch.sql.opensearch.planner.physical; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import org.apache.calcite.plan.RelOptRuleCall; |
| 5 | +import org.apache.calcite.plan.RelRule; |
| 6 | +import org.apache.calcite.rel.logical.LogicalSort; |
| 7 | +import org.apache.calcite.rex.RexLiteral; |
| 8 | +import org.apache.calcite.rex.RexNode; |
| 9 | +import org.immutables.value.Value; |
| 10 | +import org.opensearch.sql.opensearch.storage.scan.CalciteLogicalIndexScan; |
| 11 | + |
| 12 | +@Value.Enclosing |
| 13 | +public class OpenSearchLimitIndexScanRule extends RelRule<OpenSearchLimitIndexScanRule.Config> { |
| 14 | + |
| 15 | + protected OpenSearchLimitIndexScanRule(Config config) { |
| 16 | + super(config); |
| 17 | + } |
| 18 | + |
| 19 | + @Override |
| 20 | + public void onMatch(RelOptRuleCall call) { |
| 21 | + final LogicalSort sort = call.rel(0); |
| 22 | + final CalciteLogicalIndexScan scan = call.rel(1); |
| 23 | + |
| 24 | + // The LogicalSort is a LIMIT that should be pushed down when its fetch field is not null and |
| 25 | + // its collation is empty. |
| 26 | + // For example: `sort name | head 5` should not be pushed down because it has a field collation. |
| 27 | + if (sort.fetch != null && sort.getCollation().getFieldCollations().isEmpty()) { |
| 28 | + Integer limitValue = extractLimitValue(sort.fetch); |
| 29 | + Integer offsetValue = extractOffsetValue(sort.offset); |
| 30 | + if (limitValue != null) { |
| 31 | + CalciteLogicalIndexScan newScan = scan.pushDownLimit(limitValue, offsetValue); |
| 32 | + if (newScan != null) { |
| 33 | + call.transformTo(newScan); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private static Integer extractLimitValue(RexNode fetch) { |
| 40 | + if (fetch instanceof RexLiteral) { |
| 41 | + return ((RexLiteral) fetch).getValueAs(Integer.class); |
| 42 | + } |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Extracts the offset value from the given `RexNode`. If the offset is `null`, it defaults to 0. |
| 48 | + * For example: |
| 49 | + * |
| 50 | + * <ul> |
| 51 | + * <li><code>source=people | head 1</code> will have a <code>null</code> offset, which is |
| 52 | + * converted to 0. |
| 53 | + * <li><code>source=people | head 1 from 2</code> will have an offset of 2. |
| 54 | + * </ul> |
| 55 | + * |
| 56 | + * @param offset The `RexNode` representing the offset. |
| 57 | + * @return The extracted offset value, or `null` if it cannot be determined. |
| 58 | + */ |
| 59 | + private static Integer extractOffsetValue(RexNode offset) { |
| 60 | + if (Objects.isNull(offset)) { |
| 61 | + return 0; |
| 62 | + } |
| 63 | + if (offset instanceof RexLiteral) { |
| 64 | + return ((RexLiteral) offset).getValueAs(Integer.class); |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + /** Rule configuration. */ |
| 70 | + @Value.Immutable |
| 71 | + public interface Config extends RelRule.Config { |
| 72 | + OpenSearchLimitIndexScanRule.Config DEFAULT = |
| 73 | + ImmutableOpenSearchLimitIndexScanRule.Config.builder() |
| 74 | + .build() |
| 75 | + .withOperandSupplier( |
| 76 | + b0 -> |
| 77 | + b0.operand(LogicalSort.class) |
| 78 | + .oneInput( |
| 79 | + b1 -> |
| 80 | + b1.operand(CalciteLogicalIndexScan.class) |
| 81 | + .predicate(OpenSearchIndexScanRule::test) |
| 82 | + .noInputs())); |
| 83 | + |
| 84 | + @Override |
| 85 | + default OpenSearchLimitIndexScanRule toRule() { |
| 86 | + return new OpenSearchLimitIndexScanRule(this); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments