This is an automated email from the ASF dual-hosted git repository.

cancai pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 3bbfadda77 [CALCITE-7144] LIMIT should not be pushed through 
projections containing window functions
3bbfadda77 is described below

commit 3bbfadda77f41a48985eb926c9e4f067e38e9993
Author: Arnaud Jegou <[email protected]>
AuthorDate: Thu Aug 21 15:54:10 2025 +0200

    [CALCITE-7144] LIMIT should not be pushed through projections containing 
window functions
---
 .../java/org/apache/calcite/tools/RelBuilder.java  |  2 +-
 .../org/apache/calcite/test/RelBuilderTest.java    | 26 ++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java 
b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
index 6bd4102bbe..9831e65a76 100644
--- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
@@ -3705,7 +3705,7 @@ public RelBuilder sortLimit(@Nullable RexNode offsetNode, 
@Nullable RexNode fetc
       }
       if (top instanceof Project) {
         final Project project = (Project) top;
-        if (project.getInput() instanceof Sort) {
+        if (!project.containsOver() && project.getInput() instanceof Sort) {
           final Sort sort2 = (Sort) project.getInput();
           if (sort2.offset == null && sort2.fetch == null) {
             final RelNode sort =
diff --git a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java 
b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
index ba2c10c701..0e105ed784 100644
--- a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
@@ -4308,6 +4308,32 @@ private static RelBuilder assertSize(RelBuilder b,
     assertThat(root2, hasTree(expected));
   }
 
+  /**
+   * Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7144";>[CALCITE-7144]
+   * LIMIT should not be pushed through projections containing window 
functions</a>.
+   * The test checks that the RelBuilder does not merge a limit through a 
Project
+   * that contains a windowed aggregate function into a lower sort. */
+  @Test void testLimitOverProjectWithWindowFunctions() {
+    final Function<RelBuilder, RelNode> f = b -> b.scan("EMP")
+        .sort(1)
+        .project(b.field("DEPTNO"),
+            b.aggregateCall(SqlStdOperatorTable.ROW_NUMBER)
+                .over()
+                .partitionBy()
+                .orderBy(b.field("EMPNO"))
+                .rowsUnbounded()
+                .as("x"))
+        .limit(0, 10)
+        .build();
+    final String expected = ""
+        + "LogicalSort(fetch=[10])\n"
+        + "  LogicalProject(DEPTNO=[$7], x=[ROW_NUMBER() OVER (ORDER BY 
$0)])\n"
+        + "    LogicalSort(sort0=[$1], dir0=[ASC])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+    assertThat(f.apply(createBuilder()), hasTree(expected));
+  }
+
   /** Tests {@link org.apache.calcite.tools.RelRunner} for a VALUES query. */
   @Test void testRunValues() throws Exception {
     // Equivalent SQL:

Reply via email to