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

mihaibudiu 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 334fc71ad5 [CALCITE-7858] ARG_MIN, ARG_MAX, MIN_BY and MAX_BY should 
reject a non-comparable key
334fc71ad5 is described below

commit 334fc71ad5db52f11af4d1d68cf9ad40ee415cc2
Author: Alan Jin <[email protected]>
AuthorDate: Thu Oct 15 06:49:38 2026 +0000

    [CALCITE-7858] ARG_MIN, ARG_MAX, MIN_BY and MAX_BY should reject a 
non-comparable key
---
 .../org/apache/calcite/sql/type/OperandTypes.java  | 12 ++++++---
 .../org/apache/calcite/test/SqlValidatorTest.java  | 29 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/sql/type/OperandTypes.java 
b/core/src/main/java/org/apache/calcite/sql/type/OperandTypes.java
index e36017ad30..a0cd7e6a4a 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/OperandTypes.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/OperandTypes.java
@@ -1244,9 +1244,15 @@ public static SqlSingleOperandTypeChecker same(int 
operandCount,
       new SqlOperandTypeChecker() {
         @Override public boolean checkOperandTypes(SqlCallBinding callBinding,
             boolean throwOnFailure) {
-          getOperandCountRange().isValidCount(callBinding.getOperandCount());
-          RelDataType type = callBinding.getOperandType(1);
-          return type.getComparability() == RelDataTypeComparability.ALL;
+          if 
(getOperandCountRange().isValidCount(callBinding.getOperandCount())
+              && callBinding.getOperandType(1).getComparability()
+                  == RelDataTypeComparability.ALL) {
+            return true;
+          }
+          if (throwOnFailure) {
+            throw callBinding.newValidationSignatureError();
+          }
+          return false;
         }
 
         @Override public SqlOperandCountRange getOperandCountRange() {
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 1ab392832d..59f7b1e8ad 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -10291,6 +10291,35 @@ void testGroupExpressionEquivalenceParams() {
     sql("SELECT ARG_MAX('b', 5) FROM emp").ok();
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7858";>[CALCITE-7858]
+   * ARG_MIN, ARG_MAX, MIN_BY and MAX_BY should reject a non-comparable
+   * key</a>.
+   *
+   * <p>The type of column {@code HOME_ADDRESS} is a structured type whose
+   * comparability is {@code NONE}. */
+  @Test void testArgMinMaxNonComparableKey() {
+    final String expected = "Cannot apply '%s' to arguments of type "
+        + "'%s\\(<INTEGER>, <>\\)'\\. "
+        + "Supported form\\(s\\): %s\\(<ANY>, <COMPARABLE_TYPE>\\)";
+    sql("select ^arg_min(empno, home_address)^ from emp_address")
+        .fails(String.format(Locale.ROOT, expected, "ARG_MIN", "ARG_MIN", 
"ARG_MIN"));
+    sql("select ^arg_max(empno, home_address)^ from emp_address")
+        .fails(String.format(Locale.ROOT, expected, "ARG_MAX", "ARG_MAX", 
"ARG_MAX"));
+    sql("select ^max_by(empno, home_address)^ from emp_address")
+        .withOperatorTable(operatorTableFor(SqlLibrary.SPARK))
+        .fails(String.format(Locale.ROOT, expected, "MAX_BY", "MAX_BY", 
"MAX_BY"));
+    sql("select ^min_by(empno, home_address)^ from emp_address")
+        .withOperatorTable(operatorTableFor(SqlLibrary.SPARK))
+        .fails(String.format(Locale.ROOT, expected, "MIN_BY", "MIN_BY", 
"MIN_BY"));
+
+    // The value (the first argument) need not be comparable
+    sql("select arg_min(home_address, empno) from emp_address").ok();
+    sql("select max_by(home_address, empno) from emp_address")
+        .withOperatorTable(operatorTableFor(SqlLibrary.SPARK))
+        .ok();
+  }
+
   @Test void testModeFunction() {
     sql("select MODE(sal) from emp").ok();
     sql("select MODE(sal) over (order by empno) from emp").ok();

Reply via email to