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

rubenql 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 fe2e1385d2 [CALCITE-6594] RelMdSize does not handle ARRAY constructor 
calls
fe2e1385d2 is described below

commit fe2e1385d29243892e8a6ca341cc5fcfc294f706
Author: Ruben Quesada Lopez <[email protected]>
AuthorDate: Mon Apr 28 13:39:18 2025 +0100

    [CALCITE-6594] RelMdSize does not handle ARRAY constructor calls
---
 .../org/apache/calcite/rel/metadata/RelMdSize.java    | 14 ++++++++++++++
 .../java/org/apache/calcite/test/RelMetadataTest.java | 19 +++++++++++++++++++
 2 files changed, 33 insertions(+)

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 f2d2c6a882..181da76464 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
@@ -38,6 +38,8 @@
 import org.apache.calcite.rex.RexInputRef;
 import org.apache.calcite.rex.RexLiteral;
 import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.type.SqlTypeUtil;
 import org.apache.calcite.util.ImmutableNullableList;
 import org.apache.calcite.util.NlsString;
 import org.apache.calcite.util.Pair;
@@ -49,6 +51,8 @@
 import java.util.ArrayList;
 import java.util.List;
 
+import static java.util.Objects.requireNonNull;
+
 /**
  * Default implementations of the
  * {@link org.apache.calcite.rel.metadata.BuiltInMetadata.Size}
@@ -410,6 +414,16 @@ public double typeValueSize(RelDataType type, @Nullable 
Comparable value) {
     default:
       if (node instanceof RexCall) {
         RexCall call = (RexCall) node;
+
+        // 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();
+          }
+        }
+
         for (RexNode operand : call.getOperands()) {
           // It's a reasonable assumption that a function's result will have
           // similar size to its argument of a similar type. For example,
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 ea5fd08046..798e750e11 100644
--- a/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelMetadataTest.java
@@ -160,6 +160,7 @@
 import static org.hamcrest.Matchers.closeTo;
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.hasToString;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
@@ -2706,6 +2707,24 @@ private void checkAverageRowSize(RelOptCluster cluster, 
RelOptTable empTable,
     assertThat(mq.splitCount(aggregate), is(1));
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6594";>[CALCITE-6594]
+   * RelMdSize does not handle ARRAY 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);
+  }
+
+  private void checkSizeArrayConstructor(String query, double expected) {
+    final RelNode rel = sql(query).toRel();
+    final RelMetadataQuery mq = rel.getCluster().getMetadataQuery();
+    final List<@Nullable Double> averageColumnSizes = 
mq.getAverageColumnSizes(rel);
+    assertNotNull(averageColumnSizes);
+    assertThat(averageColumnSizes, hasSize(1));
+    assertThat(averageColumnSizes.get(0), is(expected));
+  }
+
   /** Unit test for
    * {@link 
org.apache.calcite.rel.metadata.RelMdPredicates#getPredicates(Join, 
RelMetadataQuery)}. */
   @Test void testPredicates() {

Reply via email to