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
The following commit(s) were added to refs/heads/main by this push:
new dbcab7f988 [CALCITE-7181] FETCH in SortRemoveRedundantRule do not
support BIGINT
dbcab7f988 is described below
commit dbcab7f988948af787e929ba53cbbf4eadec8edd
Author: Zhen Chen <[email protected]>
AuthorDate: Fri Sep 19 11:18:30 2025 +0800
[CALCITE-7181] FETCH in SortRemoveRedundantRule do not support BIGINT
---
.../calcite/rel/rules/SortRemoveRedundantRule.java | 20 ++++++++++++--------
.../org/apache/calcite/test/RelOptRulesTest.java | 12 ++++++++++++
.../org/apache/calcite/test/RelOptRulesTest.xml | 19 +++++++++++++++++++
3 files changed, 43 insertions(+), 8 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
b/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
index bf1871cbbb..9c950df5cb 100644
---
a/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
+++
b/core/src/main/java/org/apache/calcite/rel/rules/SortRemoveRedundantRule.java
@@ -24,6 +24,7 @@
import org.immutables.value.Value;
+import java.math.BigDecimal;
import java.util.Optional;
/**
@@ -114,7 +115,7 @@ protected SortRemoveRedundantRule(final
SortRemoveRedundantRule.Config config) {
// If the limit's fetch is 0, we could use
// CoreRules.SORT_FETCH_ZERO_INSTANCE to deal with it, so we don't need to
// deal with it in this rule.
- final Optional<Integer> rowCountThreshold = getRowCountThreshold(sort);
+ final Optional<BigDecimal> rowCountThreshold = getRowCountThreshold(sort);
if (!rowCountThreshold.isPresent()) {
return;
@@ -122,30 +123,33 @@ protected SortRemoveRedundantRule(final
SortRemoveRedundantRule.Config config) {
// If the threshold is not null and less than or equal to
targetMaxRowCount,
// then we could remove the redundant sort.
- if (inputMaxRowCount != null && inputMaxRowCount <=
rowCountThreshold.get()) {
+ if (inputMaxRowCount != null
+ && Double.isFinite(inputMaxRowCount)
+ && new BigDecimal(inputMaxRowCount).compareTo(rowCountThreshold.get())
<= 0) {
call.transformTo(sort.getInput());
}
}
- private static Optional<Integer> getRowCountThreshold(Sort sort) {
+ private static Optional<BigDecimal> getRowCountThreshold(Sort sort) {
if (RelOptUtil.isLimit(sort)) {
- final int fetch =
- sort.fetch instanceof RexLiteral ? RexLiteral.intValue(sort.fetch) :
0;
+ assert sort.fetch != null;
+ final BigDecimal fetch = ((RexLiteral)
sort.fetch).getValueAs(BigDecimal.class);
// We don't need to deal with fetch is 0.
- if (fetch == 0) {
+ assert fetch != null;
+ if (fetch.equals(BigDecimal.ZERO)) {
return Optional.empty();
}
// If sort is 'order by x limit n', the target threshold is 1.
if (RelOptUtil.isOrder(sort)) {
- return Optional.of(1);
+ return Optional.of(BigDecimal.ONE);
}
// If sort is 'limit n', the target threshold is the limit's fetch.
return Optional.of(fetch);
} else if (RelOptUtil.isPureOrder(sort)) {
- return Optional.of(1);
+ return Optional.of(BigDecimal.ONE);
}
return Optional.empty();
}
diff --git a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
index cfd0173b65..461370f7f0 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -1631,6 +1631,18 @@ private void
checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
.check();
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7181">[CALCITE-7181]
+ * FETCH in SortRemoveRedundantRule do not support BIGINT</a>. */
+ @Test void testSortRemoveWhenInputValuesMaxRowCntLessOrEqualLimitFetch2() {
+ final String sql = "select * from\n"
+ // The maximum value of BIGINT is 9223372036854775807.
+ + "(VALUES 1,2,3,4,5,6) as t1 limit 9823372036854775807";
+ sql(sql)
+ .withRule(CoreRules.SORT_REMOVE_REDUNDANT)
+ .check();
+ }
+
/** Test case for
* <a
href="https://issues.apache.org/jira/browse/CALCITE-6009">[CALCITE-6009]
* Add optimization to remove redundant LIMIT that is more than input
diff --git
a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
index aa5fe6d55c..5c76c2a92e 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -18246,6 +18246,25 @@ LogicalSort(fetch=[10])
<![CDATA[
LogicalProject(T1=[$0])
LogicalValues(tuples=[[{ 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }]])
+]]>
+ </Resource>
+ </TestCase>
+ <TestCase
name="testSortRemoveWhenInputValuesMaxRowCntLessOrEqualLimitFetch2">
+ <Resource name="sql">
+ <![CDATA[select * from
+(VALUES 1,2,3,4,5,6) as t1 limit 9823372036854775807]]>
+ </Resource>
+ <Resource name="planBefore">
+ <![CDATA[
+LogicalSort(fetch=[9823372036854775807:DECIMAL(19, 0)])
+ LogicalProject(T1=[$0])
+ LogicalValues(tuples=[[{ 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }]])
+]]>
+ </Resource>
+ <Resource name="planAfter">
+ <![CDATA[
+LogicalProject(T1=[$0])
+ LogicalValues(tuples=[[{ 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }]])
]]>
</Resource>
</TestCase>