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 85d5b3ef9a [CALCITE-7061] RelMdSize does not handle nested ARRAY/MAP 
constructor calls
85d5b3ef9a is described below

commit 85d5b3ef9a9e68798e4930a2786c8233b71f050b
Author: xuzifu666 <[email protected]>
AuthorDate: Thu Jun 19 15:17:49 2025 +0800

    [CALCITE-7061] RelMdSize does not handle nested ARRAY/MAP constructor calls
---
 .../org/apache/calcite/rel/metadata/RelMdSize.java | 24 ++++++++++++++++++----
 .../org/apache/calcite/test/RelMetadataTest.java   | 23 ++++++++++++++++++++-
 2 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdSize.java 
b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdSize.java
index 181da76464..5bdbf9ce16 100644
--- a/core/src/main/java/org/apache/calcite/rel/metadata/RelMdSize.java
+++ b/core/src/main/java/org/apache/calcite/rel/metadata/RelMdSize.java
@@ -403,6 +403,20 @@ public double typeValueSize(RelDataType type, @Nullable 
Comparable value) {
     }
   }
 
+  /** Returns size for nested type ARRAY/MAP. */
+  public Double computeSizeForNestedType(RexCall call,
+      List<? extends @Nullable Double> inputColumnSizes) {
+    if (call.operands.size() == 0) {
+      return 0d;
+    }
+    Double compSize = 0d;
+    for (RexNode operand : call.getOperands()) {
+      Double nonNullSize = requireNonNull(averageRexSize(operand, 
inputColumnSizes));
+      compSize += nonNullSize;
+    }
+    return compSize;
+  }
+
   public @Nullable Double averageRexSize(RexNode node,
       List<? extends @Nullable Double> inputColumnSizes) {
     switch (node.getKind()) {
@@ -418,10 +432,12 @@ public double typeValueSize(RelDataType type, @Nullable 
Comparable value) {
         // ARRAY constructor: array size multiplied by the average size of its 
component type
         if (call.isA(SqlKind.ARRAY_VALUE_CONSTRUCTOR)) {
           assert SqlTypeUtil.isArray(call.getType());
-          Double compSize = 
averageTypeValueSize(requireNonNull(call.getType().getComponentType()));
-          if (compSize != null) {
-            return compSize * call.operands.size();
-          }
+          return computeSizeForNestedType(call, inputColumnSizes);
+        }
+
+        if (call.isA(SqlKind.MAP_VALUE_CONSTRUCTOR)) {
+          assert SqlTypeUtil.isMap(call.getType());
+          return computeSizeForNestedType(call, inputColumnSizes);
         }
 
         for (RexNode operand : call.getOperands()) {
diff --git a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java 
b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
index 0bd87a9c37..6fe7b02553 100644
--- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
@@ -2709,11 +2709,32 @@ private void checkAverageRowSize(RelOptCluster cluster, 
RelOptTable empTable,
 
   /** Test case for
    * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6594";>[CALCITE-6594]
-   * RelMdSize does not handle ARRAY constructor calls</a>. */
+   * RelMdSize does not handle ARRAY constructor calls</a>,
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7061";>[CALCITE-7061]
+   * RelMdSize does not handle nested ARRAY/MAP constructor calls</a>. */
   @Test void testSizeArrayConstructor() {
     checkSizeArrayConstructor("SELECT ARRAY[1, 2, 3, 4]", 16d);
     checkSizeArrayConstructor("SELECT ARRAY[true, false]", 2d);
     checkSizeArrayConstructor("SELECT ARRAY[CAST(3.14 AS DOUBLE)]", 8d);
+    checkSizeArrayConstructor(
+        "SELECT ARRAY[ARRAY[1,2], ARRAY[2,2], ARRAY[1,1], ARRAY[2,3]]", 32d);
+    checkSizeArrayConstructor(
+        "SELECT ARRAY[ARRAY[1,2], ARRAY[1,1,1], ARRAY[1,1], ARRAY[2,3]]", 36d);
+    checkSizeArrayConstructor(
+        "SELECT ARRAY[MAP[1,2], MAP[1,1,1,2], MAP[1,1], MAP[2,3,4,5,6,7]]", 
56d);
+  }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7061";>[CALCITE-7061]
+   * RelMdSize does not handle nested ARRAY/MAP constructor calls</a>. */
+  @Test void testSizeMapConstructor() {
+    checkSizeArrayConstructor("SELECT MAP[1, 2, 3, 4]", 16d);
+    checkSizeArrayConstructor("SELECT MAP[1,true,3,false]", 10d);
+    checkSizeArrayConstructor("SELECT MAP[CAST(3.14 AS DOUBLE),CAST(3.14 AS 
DOUBLE)]", 16d);
+    checkSizeArrayConstructor("SELECT 
MAP[1,ARRAY[true,false],3,ARRAY[true,false]]",
+        12d);
+    checkSizeArrayConstructor("SELECT MAP[1,MAP[true,2],3,MAP[false,1]]",
+        18d);
   }
 
   private void checkSizeArrayConstructor(String query, double expected) {

Reply via email to