You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by zh...@apache.org on 2025/09/24 13:02:37 UTC

(calcite) branch main updated (2797822f45 -> b8f2f7ecb6)

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

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


    omit 2797822f45 Revert code
    omit d8fcc4077d [CALCITE-7190] FETCH and OFFSET in SortMergeRule only supports BIGINT
     new b8f2f7ecb6 [CALCITE-7190] FETCH and OFFSET in SortMergeRule only supports BIGINT

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (2797822f45)
            \
             N -- N -- N   refs/heads/main (b8f2f7ecb6)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:


(calcite) 01/01: [CALCITE-7190] FETCH and OFFSET in SortMergeRule only supports BIGINT

Posted by zh...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit b8f2f7ecb69ff95a6b484b23e7dfbc58018269a8
Author: Zhen Chen <cz...@163.com>
AuthorDate: Wed Sep 24 21:00:20 2025 +0800

    [CALCITE-7190] FETCH and OFFSET in SortMergeRule only supports BIGINT
---
 .../org/apache/calcite/rel/rules/MeasureRules.java  |  4 ++--
 .../org/apache/calcite/rel/rules/SortMergeRule.java | 14 ++++++++------
 .../java/org/apache/calcite/tools/RelBuilder.java   | 21 +++++++++++++++------
 3 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rel/rules/MeasureRules.java b/core/src/main/java/org/apache/calcite/rel/rules/MeasureRules.java
index 707c7b2f5f..037a4d6054 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/MeasureRules.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/MeasureRules.java
@@ -508,8 +508,8 @@ protected ProjectSortMeasureRule(ProjectSortMeasureRuleConfig config) {
 
       relBuilder.push(sort.getInput())
           .projectPlus(map.keySet())
-          .sortLimit(sort.offset == null ? 0 : RexLiteral.longValue(sort.offset),
-              sort.fetch == null ? -1 : RexLiteral.longValue(sort.fetch),
+          .sortLimit(sort.offset == null ? 0 : RexLiteral.numberValue(sort.offset),
+              sort.fetch == null ? -1 : RexLiteral.numberValue(sort.fetch),
               sort.getSortExps())
           .project(newProjects);
       call.transformTo(relBuilder.build());
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java b/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java
index 797b7e354a..1622864329 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/SortMergeRule.java
@@ -28,6 +28,8 @@
 
 import org.immutables.value.Value;
 
+import java.math.BigDecimal;
+
 /**
  * This rule try to merge the double {@link Sort},one is Limit semantics,
  * another sort is Limit or TOPN semantics.
@@ -92,18 +94,18 @@ private static void limitMerge(SortMergeRule rule,
 
     final RelBuilder builder = call.builder();
 
-    final long topFetch = topSort.fetch instanceof RexLiteral
-        ? RexLiteral.longValue(topSort.fetch) : -1;
+    final Number topFetch = topSort.fetch instanceof RexLiteral
+        ? RexLiteral.numberValue(topSort.fetch) : null;
 
-    final long bottomFetch = bottomSort.fetch instanceof RexLiteral
-        ? RexLiteral.longValue(bottomSort.fetch) : -1;
+    final Number bottomFetch = bottomSort.fetch instanceof RexLiteral
+        ? RexLiteral.numberValue(bottomSort.fetch) : null;
 
-    if (topFetch == -1 || bottomFetch == -1) {
+    if (topFetch == null || bottomFetch == null) {
       return;
     }
 
     // Get the minimum limit value from parent and child sort RelNode
-    final long minFetch = Math.min(topFetch, bottomFetch);
+    final Number minFetch = ((BigDecimal) topFetch).min((BigDecimal) bottomFetch);
 
     builder.push(bottomSort.getInput());
 
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 b9b9c25a42..d84c031458 100644
--- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
@@ -3739,10 +3739,16 @@ public RelBuilder sort(RelCollation collation) {
    * @param fetch Maximum number of rows to fetch; negative means no limit
    * @param nodes Sort expressions
    */
-  public RelBuilder sortLimit(long offset, long fetch,
+  public RelBuilder sortLimit(Number offset, Number fetch,
       Iterable<? extends RexNode> nodes) {
-    final @Nullable RexNode offsetNode = offset <= 0 ? null : literal(offset);
-    final @Nullable RexNode fetchNode = fetch < 0 ? null : literal(fetch);
+    final @Nullable RexNode offsetNode =
+        new BigDecimal(offset.toString()).compareTo(BigDecimal.ZERO) <= 0
+            ? null
+            : literal(offset);
+    final @Nullable RexNode fetchNode =
+        new BigDecimal(fetch.toString()).compareTo(BigDecimal.ZERO) < 0
+            ? null
+            : literal(fetch);
     return sortLimit(offsetNode, fetchNode, nodes);
   }
 
@@ -3770,9 +3776,12 @@ public RelBuilder sortLimit(@Nullable RexNode offsetNode, @Nullable RexNode fetc
     final Registrar registrar = new Registrar(fields(), ImmutableList.of());
     final List<RelFieldCollation> fieldCollations =
         registrar.registerFieldCollations(nodes);
-    final long fetch = fetchNode instanceof RexLiteral
-        ? RexLiteral.longValue(fetchNode) : -1;
-    if (offsetNode == null && fetch == 0 && config.simplifyLimit()) {
+    final Number fetch = fetchNode instanceof RexLiteral
+        ? RexLiteral.numberValue(fetchNode) : null;
+    if (offsetNode == null
+        && fetch != null
+        && ((BigDecimal) fetch).compareTo(BigDecimal.ZERO) == 0
+        && config.simplifyLimit()) {
       return empty();
     }
     if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) {