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 71e30e2c70 [CALCITE-4999] ARRAY, MULTISET functions should return a
collection of scalars if a sub-query returns 1 column
71e30e2c70 is described below
commit 71e30e2c70e454bdd09eef12ad239ba510ce
Author: dssysolyatin
AuthorDate: Mon Aug 29 12:13:50 2022 +0300
[CALCITE-4999] ARRAY, MULTISET functions should return a collection of
scalars if a sub-query returns 1 column
---
.../adapter/enumerable/EnumerableCollect.java | 30 ++-
.../java/org/apache/calcite/rel/core/Collect.java | 45 ++
.../apache/calcite/rel/mutable/MutableRels.java| 3 +-
.../calcite/rel/rules/SubQueryRemoveRule.java | 14 ++-
.../java/org/apache/calcite/rex/RexSubQuery.java | 9 -
.../calcite/sql/fun/SqlArrayQueryConstructor.java | 2 +-
.../sql/fun/SqlMultisetQueryConstructor.java | 2 +-
.../apache/calcite/sql/type/SqlTypeTransforms.java | 21 ++
.../org/apache/calcite/sql/type/SqlTypeUtil.java | 22 +++
.../apache/calcite/sql2rel/SqlToRelConverter.java | 6 +--
.../calcite/sql2rel/StandardConvertletTable.java | 16 ++--
.../java/org/apache/calcite/test/JdbcTest.java | 2 +-
.../org/apache/calcite/test/SqlValidatorTest.java | 32 ++-
.../org/apache/calcite/test/SqlOperatorTest.java | 31 +++
14 files changed, 189 insertions(+), 46 deletions(-)
diff --git
a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java
b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java
index da50dff617..64ead33167 100644
---
a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java
+++
b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCollect.java
@@ -25,8 +25,11 @@ import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.Collect;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.calcite.util.BuiltInMethod;
+import static java.util.Objects.requireNonNull;
+
/** Implementation of {@link org.apache.calcite.rel.core.Collect} in
* {@link org.apache.calcite.adapter.enumerable.EnumerableConvention
enumerable calling convention}. */
public class EnumerableCollect extends Collect implements EnumerableRel {
@@ -91,15 +94,24 @@ public class EnumerableCollect extends Collect implements
EnumerableRel {
Expression child_ =
builder.append(
"child", result.block);
-// In the internal representation of multisets , every element must be a
record. In case the
-// result above is a scalar type we have to wrap it around a physical type
capable of
-// representing records. For this reason the following conversion is
necessary.
-// REVIEW zabetak January 7, 2019: If we can ensure that the input to this
operator
-// has the correct physical type (e.g., respecting the Prefer.ARRAY above)
then this conversion
-// can be removed.
-Expression conv_ =
-builder.append(
-"converted", result.physType.convertTo(child_,
JavaRowFormat.ARRAY));
+
+RelDataType collectionComponentType =
+
requireNonNull(rowType().getFieldList().get(0).getType().getComponentType());
+RelDataType childRecordType =
result.physType.getRowType().getFieldList().get(0).getType();
+
+Expression conv_ = child_;
+if (!SqlTypeUtil.sameNamedType(collectionComponentType, childRecordType)) {
+ // In the internal representation of multisets , every element must be a
record. In case the
+ // result above is a scalar type we have to wrap it around a physical
type capable of
+ // representing records. For this reason the following conversion is
necessary.
+ // REVIEW zabetak January 7, 2019: If we can ensure that the input to
this operator
+ // has the correct physical type (e.g., respecting the Prefer.ARRAY
above)
+ // then this conversion can be removed.
+ conv_ =
+ builder.append(
+ "converted", result.physType.convertTo(child_,
JavaRowFormat.ARRAY));
+}
+
Expression list_ =
builder.append("list",
Expressions.call(conv_,
diff --git a/core/src/main/java/org/apache/calcite/rel/core/Collect.java
b/core/src/main/java/org/apache/calcite/rel/core/Collect.java
index 28b83c5ff5..50891d5a7e 100644
--- a/core/src/main/java/org/apache/calcite/rel/core/Collect.java
+++ b/core/src/main/java/org/apache/calcite/rel/core/Collect.java
@@ -25,6 +25,7 @@ import org.apache.calcite.rel.RelWriter;
import org.apache.calcite.rel.SingleRel;
import org.apache.calcite.rel.type.RelDataType;
import