asolimando commented on code in PR #4754:
URL: https://github.com/apache/calcite/pull/4754#discussion_r2702335347


##########
core/src/main/java/org/apache/calcite/sql2rel/TopDownGeneralDecorrelator.java:
##########
@@ -598,50 +599,48 @@ public RelNode unnestInternal(Sort sort, boolean 
allowEmptyOutputFromRewrite) {
     RelCollation shiftCollation = sort.getCollation().apply(targetMapping);
     builder.push(newInput);
 
-    if (!sort.collation.getFieldCollations().isEmpty()
-        && (sort.offset != null || sort.fetch != null)) {
-      // the Sort with ORDER BY and LIMIT or OFFSET have to be changed during 
rewriting because
-      // now the limit has to be enforced per value of the outer bindings 
instead of globally.
-      // It can be rewritten using ROW_NUMBER() window function and filtering 
on it,
-      // see section 4.4 in paper Improving Unnesting of Complex Queries
-      List<RexNode> partitionKeys = new ArrayList<>();
-      for (CorDef corDef : corDefs) {
-        int partitionKeyIndex = 
requireNonNull(inputInfo.corDefOutputs.get(corDef));
-        partitionKeys.add(builder.field(partitionKeyIndex));
-      }
-      RexNode rowNumber = builder.aggregateCall(SqlStdOperatorTable.ROW_NUMBER)
-          .over()
-          .partitionBy(partitionKeys)
-          .orderBy(builder.fields(shiftCollation))
-          .toRex();
-      List<RexNode> projectsWithRowNumber = new ArrayList<>(builder.fields());
-      projectsWithRowNumber.add(rowNumber);
-      builder.project(projectsWithRowNumber);
-
-      List<RexNode> conditions = new ArrayList<>();
-      if (sort.offset != null) {
-        RexNode greaterThenLowerBound =
-            builder.call(
-                SqlStdOperatorTable.GREATER_THAN,
-                builder.field(projectsWithRowNumber.size() - 1),
-                sort.offset);
-        conditions.add(greaterThenLowerBound);
-      }
-      if (sort.fetch != null) {
-        RexNode upperBound = sort.offset == null
-            ? sort.fetch
-            : builder.call(SqlStdOperatorTable.PLUS, sort.offset, sort.fetch);
-        RexNode lessThenOrEqualUpperBound =
-            builder.call(
-                SqlStdOperatorTable.LESS_THAN_OR_EQUAL,
-                builder.field(projectsWithRowNumber.size() - 1),
-                upperBound);
-        conditions.add(lessThenOrEqualUpperBound);
-      }
-      builder.filter(conditions);
-    } else {
-      builder.sortLimit(sort.offset, sort.fetch, 
builder.fields(shiftCollation));
+    // the Sort have to be changed during rewriting because now the 
order/limit/offset has to be

Review Comment:
   Q: was this a shortcoming of the original paper or an implementation issue?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to