xiedeyantu commented on code in PR #4515:
URL: https://github.com/apache/calcite/pull/4515#discussion_r2313253628


##########
core/src/main/java/org/apache/calcite/rel/rules/SortJoinTransposeRule.java:
##########
@@ -23,25 +23,53 @@
 import org.apache.calcite.rel.RelFieldCollation;
 import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.Join;
-import org.apache.calcite.rel.core.JoinInfo;
 import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.calcite.rel.core.Sort;
 import org.apache.calcite.rel.logical.LogicalJoin;
 import org.apache.calcite.rel.logical.LogicalSort;
 import org.apache.calcite.rel.metadata.RelMdUtil;
 import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexBuilder;
 import org.apache.calcite.rex.RexDynamicParam;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.tools.RelBuilderFactory;
 
+import org.checkerframework.checker.nullness.qual.Nullable;
 import org.immutables.value.Value;
 
+import java.math.BigDecimal;
+
 /**
- * Planner rule that pushes a {@link org.apache.calcite.rel.core.Sort} past a
- * {@link org.apache.calcite.rel.core.Join}.
+ * Planner rule that pushes a {@link Sort} past a {@link Join}.
+ *
+ * <p>This rule applies to left/right outer joins, and only pushes the sort if 
its keys are
+ * entirely from one input. It will not fire if the sort uses dynamic 
parameters or the input
+ * is already sorted and limited. However, an extension for full outer joins 
for this rule could
+ * be envisioned.
  *
- * <p>At the moment, we only consider left/right outer joins.
- * However, an extension for full outer joins for this rule could be 
envisioned.
- * Special attention should be paid to null values for correctness issues.
+ * <p>For example, given the SQL:
+ * <pre>
+ *   select d.deptno, empno from sales.dept d
+ *   right join sales.emp e using (deptno) limit 10 offset 2
+ * </pre>
+ * The initial plan:
+ * <pre>
+ *   LogicalProject(DEPTNO=[$0], EMPNO=[$2])
+ *     LogicalSort(offset=[2], fetch=[10])
+ *       LogicalJoin(condition=[=($0, $9)], joinType=[right])
+ *         LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ *         LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+ * </pre>
+ * After applying this rule:
+ * <pre>
+ *   LogicalProject(DEPTNO=[$0], EMPNO=[$2])
+ *     LogicalSort(offset=[2], fetch=[10])
+ *       LogicalJoin(condition=[=($0, $9)], joinType=[right])
+ *         LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
+ *         LogicalSort(fetch=[12])

Review Comment:
   I've also noticed a problem with Calcite calculations: join results are 
automatically sorted by the join key. Is this the problem you're referring to? 
If the right table isn't sorted by the join key, is it also incorrect to push 
down the limit?



-- 
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