Skip to content

Commit bf19c6e

Browse files
committed
Refactor: create a isLogicalSortLimit method to enhance readability
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 8c92930 commit bf19c6e

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"calcite":{
3-
"logical":"LogicalProject(ageMinus=[$17])\n LogicalSort(fetch=[5])\n LogicalProject(account_number=[$0], firstname=[$1], address=[$2], balance=[$3], gender=[$4], city=[$5], employer=[$6], state=[$7], age=[$8], email=[$9], lastname=[$10], _id=[$11], _index=[$12], _score=[$13], _maxscore=[$14], _sort=[$15], _routing=[$16], ageMinus=[-($8, 30)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n",
4-
"physical":"EnumerableCalc(expr#0=[{inputs}], expr#1=[30], expr#2=[-($t0, $t1)], $f0=[$t2])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]], PushDownContext=[[LIMIT->5, PROJECT->[age]], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":5,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}}, requestedTotalSize=5, pageSize=null, startFrom=0)])\n"
2+
"calcite": {
3+
"logical": "LogicalProject(ageMinus=[$17])\n LogicalSort(fetch=[5])\n LogicalProject(account_number=[$0], firstname=[$1], address=[$2], balance=[$3], gender=[$4], city=[$5], employer=[$6], state=[$7], age=[$8], email=[$9], lastname=[$10], _id=[$11], _index=[$12], _score=[$13], _maxscore=[$14], _sort=[$15], _routing=[$16], ageMinus=[-($8, 30)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n",
4+
"physical": "EnumerableCalc(expr#0=[{inputs}], expr#1=[30], expr#2=[-($t0, $t1)], $f0=[$t2])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]], PushDownContext=[[LIMIT->5, PROJECT->[age]], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"size\":5,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}}, requestedTotalSize=5, pageSize=null, startFrom=0)])\n"
55
}
66
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public void onMatch(RelOptRuleCall call) {
3030
final LogicalSort sort = call.rel(0);
3131
final CalciteLogicalIndexScan scan = call.rel(1);
3232

33-
// The LogicalSort is a LIMIT that should be pushed down when its fetch field is not null and
34-
// its collation is empty.
35-
// For example: `sort name | head 5` should not be pushed down because it has a field collation.
36-
if (sort.fetch != null && sort.getCollation().getFieldCollations().isEmpty()) {
33+
if (isLogicalSortLimit(sort)) {
3734
Integer limitValue = extractLimitValue(sort.fetch);
3835
Integer offsetValue = extractOffsetValue(sort.offset);
3936
if (limitValue != null && offsetValue != null) {
@@ -45,6 +42,18 @@ public void onMatch(RelOptRuleCall call) {
4542
}
4643
}
4744

45+
/**
46+
* The LogicalSort is a LIMIT that should be pushed down when its fetch field is not null and its
47+
* collation is empty. For example: <code>sort name | head 5</code> should not be pushed down
48+
* because it has a field collation.
49+
*
50+
* @param sort The LogicalSort to check.
51+
* @return True if the LogicalSort is a LIMIT that can be pushed down, false otherwise.
52+
*/
53+
private static boolean isLogicalSortLimit(LogicalSort sort) {
54+
return sort.fetch != null && sort.getCollation().getFieldCollations().isEmpty();
55+
}
56+
4857
private static Integer extractLimitValue(RexNode fetch) {
4958
if (fetch instanceof RexLiteral) {
5059
return ((RexLiteral) fetch).getValueAs(Integer.class);
@@ -53,17 +62,17 @@ private static Integer extractLimitValue(RexNode fetch) {
5362
}
5463

5564
/**
56-
* Extracts the offset value from the given `RexNode`. If the offset is `null`, it defaults to 0.
57-
* For example:
65+
* Extracts the offset value from the given <code>RexNode</code>. If the offset is <code>null
66+
* </code>, it defaults to 0. For example:
5867
*
5968
* <ul>
6069
* <li><code>source=people | head 1</code> will have a <code>null</code> offset, which is
6170
* converted to 0.
6271
* <li><code>source=people | head 1 from 2</code> will have an offset of 2.
6372
* </ul>
6473
*
65-
* @param offset The `RexNode` representing the offset.
66-
* @return The extracted offset value, or `null` if it cannot be determined.
74+
* @param offset The <code>RexNode</code> representing the offset.
75+
* @return The extracted offset value, or <code>null</code> if it cannot be determined.
6776
*/
6877
private static Integer extractOffsetValue(RexNode offset) {
6978
if (Objects.isNull(offset)) {

0 commit comments

Comments
 (0)