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

mbudiu 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 eeb9c3d076 [CALCITE-6985] Add rule to transform MIN/MAX with ORDER BY 
and LIMIT 1
eeb9c3d076 is described below

commit eeb9c3d076889a5bf25d28a24318e5c1c6c89549
Author: Zhen Chen <[email protected]>
AuthorDate: Sun May 4 07:57:30 2025 +0800

    [CALCITE-6985] Add rule to transform MIN/MAX with ORDER BY and LIMIT 1
---
 .../rel/rules/AggregateMinMaxToLimitRule.java      | 128 +++++++++++++++++++++
 .../org/apache/calcite/rel/rules/CoreRules.java    |   5 +
 .../org/apache/calcite/test/RelOptRulesTest.java   |   6 +
 .../org/apache/calcite/test/RelOptRulesTest.xml    |  26 +++++
 core/src/test/resources/sql/planner.iq             |  44 ++++++-
 5 files changed, 208 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/rel/rules/AggregateMinMaxToLimitRule.java
 
b/core/src/main/java/org/apache/calcite/rel/rules/AggregateMinMaxToLimitRule.java
new file mode 100644
index 0000000000..51c2996176
--- /dev/null
+++ 
b/core/src/main/java/org/apache/calcite/rel/rules/AggregateMinMaxToLimitRule.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+
+ /**
+ * Rule that transforms an MIN/MAX {@link Aggregate} functions into equivalent 
subqueries
+ * with ORDER BY and LIMIT 1 for potential performance optimization.
+ *
+ * <p>This rule converts queries of the form:
+ * <pre>{@code
+ * SELECT MIN(c1), MAX(c2) FROM t;
+ * }</pre>
+ * into:
+ * <pre>{@code
+ * SELECT
+ *   (SELECT c1 FROM t WHERE c1 IS NOT NULL ORDER BY c1 ASC LIMIT 1)
+ *     AS min_c1,
+ *   (SELECT c2 FROM t WHERE c2 IS NOT NULL ORDER BY c2 DESC LIMIT 1)
+ *    AS max_c2
+ * FROM (VALUES(1));
+ * }</pre>
+ */
[email protected]
+public class AggregateMinMaxToLimitRule
+    extends RelRule<AggregateMinMaxToLimitRule.Config>
+    implements TransformationRule {
+
+  /** Creates a AggregateMinMaxToLimitRule. */
+  protected AggregateMinMaxToLimitRule(Config config) {
+    super(config);
+  }
+
+  //~ Methods ----------------------------------------------------------------
+
+  @Override public boolean matches(RelOptRuleCall call) {
+    final Aggregate agg = call.rel(0);
+
+    // Only match no group by aggregate
+    if (!agg.getGroupSet().isEmpty()) {
+      return false;
+    }
+
+    // Only match if all aggregate functions are MIN or MAX
+    return agg.getAggCallList().stream()
+        .allMatch(aggCall ->
+            aggCall.getAggregation().getKind() == SqlKind.MIN
+                || aggCall.getAggregation().getKind() == SqlKind.MAX);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Aggregate agg = call.rel(0);
+    RelNode aggInput = agg.getInput().stripped();
+    RelBuilder builder = call.builder();
+
+    builder.push(aggInput);
+    List<RexNode> newProjects = new ArrayList<>();
+    for (AggregateCall aggCall : agg.getAggCallList()) {
+      int idx = aggCall.getArgList().get(0);
+      final RexNode r = builder.field(idx);
+      if (!RexUtil.isDeterministic(r)) {
+        return;
+      }
+      // MIN is ASC, MAX is DESC
+      final boolean isDesc = aggCall.getAggregation().kind == SqlKind.MAX;
+
+      RexNode subQuery = builder.scalarQuery(b -> b.push(aggInput)
+          .project(r)
+          .filter(b.isNotNull(r))
+          .sortLimit(0, 1,
+              isDesc ? builder.desc(r) : r)
+          .build());
+
+      newProjects.add(subQuery);
+    }
+
+    builder.clear();
+    builder.values(new String[] {"i"}, 1)
+        .project(newProjects);
+    call.transformTo(builder.build());
+  }
+
+  /** Rule configuration. */
+  @Value.Immutable
+  public interface Config extends RelRule.Config {
+    Config DEFAULT = ImmutableAggregateMinMaxToLimitRule.Config.of()
+        .withOperandFor(Aggregate.class);
+
+    @Override default AggregateMinMaxToLimitRule toRule() {
+      return new AggregateMinMaxToLimitRule(this);
+    }
+
+    /** Defines an operand tree for the given classes. */
+    default Config withOperandFor(Class<? extends Aggregate> aggregateClass) {
+      return withOperandSupplier(
+          b0 -> b0.operand(aggregateClass).anyInputs())
+          .as(Config.class);
+    }
+  }
+}
diff --git a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java 
b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
index 73db208b14..94b69febcc 100644
--- a/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
+++ b/core/src/main/java/org/apache/calcite/rel/rules/CoreRules.java
@@ -107,6 +107,11 @@ private CoreRules() {}
   public static final AggregateRemoveRule AGGREGATE_REMOVE =
       AggregateRemoveRule.Config.DEFAULT.toRule();
 
+  /** Rule that transforms an MIN/MAX {@link Aggregate} functions
+   * into equivalent subqueries with ORDER BY and LIMIT 1. */
+  public static final AggregateMinMaxToLimitRule AGGREGATE_MIN_MAX_TO_LIMIT =
+      AggregateMinMaxToLimitRule.Config.DEFAULT.toRule();
+
   /** Rule that expands distinct aggregates
    * (such as {@code COUNT(DISTINCT x)}) from a
    * {@link Aggregate}.
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 3278dab3e5..765e5ecd55 100644
--- a/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
@@ -10551,4 +10551,10 @@ private void 
checkLoptOptimizeJoinRule(LoptOptimizeJoinRule rule) {
         .withRule(CoreRules.JOIN_CONDITION_PUSH, CoreRules.FILTER_INTO_JOIN)
         .check();
   }
+
+  @Test void testAggregateMinMaxToLimitRule() {
+    final String sql = "select min(deptno), max(deptno) from emp";
+    sql(sql).withRule(CoreRules.AGGREGATE_MIN_MAX_TO_LIMIT)
+        .check();
+  }
 }
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 37915af905..96fdbe60cb 100644
--- a/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
+++ b/core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml
@@ -776,6 +776,32 @@ LogicalProject(EXPR$0=[$0])
 ]]>
     </Resource>
   </TestCase>
+  <TestCase name="testAggregateMinMaxToLimitRule">
+    <Resource name="planBefore">
+      <![CDATA[
+LogicalAggregate(group=[{}], EXPR$0=[MIN($0)], EXPR$1=[MAX($0)])
+  LogicalProject(DEPTNO=[$7])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+]]>
+    </Resource>
+    <Resource name="planAfter">
+      <![CDATA[
+LogicalProject($f0=[$SCALAR_QUERY({
+LogicalSort(sort0=[$0], dir0=[ASC], fetch=[1])
+  LogicalProject(DEPTNO=[$7])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})], $f1=[$SCALAR_QUERY({
+LogicalSort(sort0=[$0], dir0=[DESC], fetch=[1])
+  LogicalProject(DEPTNO=[$7])
+    LogicalTableScan(table=[[CATALOG, SALES, EMP]])
+})])
+  LogicalValues(tuples=[[{ 1 }]])
+]]>
+    </Resource>
+    <Resource name="sql">
+      <![CDATA[select min(deptno), max(deptno) from emp]]>
+    </Resource>
+  </TestCase>
   <TestCase name="testAggregateProjectMerge">
     <Resource name="sql">
       <![CDATA[select x, sum(z), y from (
diff --git a/core/src/test/resources/sql/planner.iq 
b/core/src/test/resources/sql/planner.iq
index ae3599dfee..9d6298b58c 100644
--- a/core/src/test/resources/sql/planner.iq
+++ b/core/src/test/resources/sql/planner.iq
@@ -88,8 +88,50 @@ EnumerableIntersect(all=[false])
     EnumerableValues(tuples=[[{ 0 }, { 1 }]])
 !plan
 
-# Test predicate push down with/without expand disjunction.
+# [CALCITE-6985] Add rule to transform MIN/MAX with ORDER BY and LIMIT 1
+select min(deptno), max(deptno) from emp;
++--------+--------+
+| EXPR$0 | EXPR$1 |
++--------+--------+
+|     10 |     60 |
++--------+--------+
+(1 row)
+
+!ok
+
+EnumerableAggregate(group=[{}], EXPR$0=[MIN($0)], EXPR$1=[MAX($0)])
+  EnumerableValues(tuples=[[{ 10 }, { 10 }, { 20 }, { 30 }, { 30 }, { 50 }, { 
50 }, { 60 }, { null }]])
+!plan
+
+!set planner-rules "+AGGREGATE_MIN_MAX_TO_LIMIT,
+  -EnumerableRules.ENUMERABLE_AGGREGATE_RULE,
+  +PROJECT_SUB_QUERY_TO_CORRELATE"
+select min(deptno), max(deptno) from emp;
++--------+--------+
+| EXPR$0 | EXPR$1 |
++--------+--------+
+|     10 |     60 |
++--------+--------+
+(1 row)
+
+!ok
+
+EnumerableCalc(expr#0..2=[{inputs}], $f0=[$t1], $f1=[$t2])
+  EnumerableNestedLoopJoin(condition=[true], joinType=[left])
+    EnumerableNestedLoopJoin(condition=[true], joinType=[left])
+      EnumerableValues(tuples=[[{ 1 }]])
+      EnumerableLimit(fetch=[1])
+        EnumerableCalc(expr#0=[{inputs}], expr#1=[IS NOT NULL($t0)], 
DEPTNO=[$t0], $condition=[$t1])
+          EnumerableValues(tuples=[[{ 10 }, { 10 }, { 20 }, { 30 }, { 30 }, { 
50 }, { 50 }, { 60 }, { null }]])
+    EnumerableLimit(fetch=[1])
+      EnumerableSort(sort0=[$0], dir0=[DESC])
+        EnumerableCalc(expr#0=[{inputs}], expr#1=[IS NOT NULL($t0)], 
DEPTNO=[$t0], $condition=[$t1])
+          EnumerableValues(tuples=[[{ 10 }, { 10 }, { 20 }, { 30 }, { 30 }, { 
50 }, { 50 }, { 60 }, { null }]])
+!plan
+
 !set planner-rules original
+
+# Test predicate push down with/without expand disjunction.
 with t1 (id1, col11, col12) as (values (1, 11, 111), (2, 12, 122), (3, 13, 
133), (4, 14, 144), (5, 15, 155)),
 t2 (id2, col21, col22) as (values (1, 21, 211), (2, 22, 222), (3, 23, 233), 
(4, 24, 244), (5, 25, 255)),
 t3 (id3, col31, col32) as (values (1, 31, 311), (2, 32, 322), (3, 33, 333), 
(4, 34, 344), (5, 35, 355))

Reply via email to