Skip to content

Commit 087c936

Browse files
author
Selina Song
committed
Fix limit pushdown bug when reverse comes before head
1 parent 50b2155 commit 087c936

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

opensearch/src/main/java/org/opensearch/sql/opensearch/planner/physical/OpenSearchLimitIndexScanRule.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public void onMatch(RelOptRuleCall call) {
3232

3333
Integer limitValue = extractLimitValue(sort.fetch);
3434
Integer offsetValue = extractOffsetValue(sort.offset);
35+
// Skip this rule if this is a reverse operation (indicated by row_number)
36+
if (hasRowNumberFunction(sort)) {
37+
return;
38+
}
3539
if (limitValue != null && offsetValue != null) {
3640
CalciteLogicalIndexScan newScan = scan.pushDownLimit(limitValue, offsetValue);
3741
if (newScan != null) {
@@ -40,6 +44,18 @@ public void onMatch(RelOptRuleCall call) {
4044
}
4145
}
4246

47+
/**
48+
* Check if the LogicalSort contains a row_number function, which indicates a reverse operation.
49+
*
50+
* @param sort The LogicalSort to check
51+
* @return True if a row_number function is found, false otherwise
52+
*/
53+
private boolean hasRowNumberFunction(LogicalSort sort) {
54+
// Check if the sort has a row_number function in its digest
55+
String digest = sort.getDigest();
56+
return digest != null && digest.contains("row_number");
57+
}
58+
4359
private static Integer extractLimitValue(RexNode fetch) {
4460
// fetch is always a integer literal (specified in our PPL/SQL syntax)
4561
if (fetch instanceof RexLiteral) {

0 commit comments

Comments
 (0)