chucheng92 commented on code in PR #3705:
URL: https://github.com/apache/calcite/pull/3705#discussion_r1507208559
##########
testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java:
##########
@@ -6538,6 +6586,54 @@ void checkRegexpExtract(SqlOperatorFixture f0,
FunctionAlias functionAlias) {
"INTEGER is not comparable to BOOLEAN", false);
}
+ /** Test case for
+ * <a href="https://github.com/apache/calcite/pull/3705">[CALCITE-5976]
+ * Use explicit casting if inserted element type in
ArrayPrepend/ArrayAppend/ArrayInsert
+ * does not equal derived component type)</a>. */
+ @Test void testArrayPrependFuncByCast() {
+ final SqlOperatorFixture f0 = fixture();
+ f0.setFor(SqlLibraryOperators.ARRAY_PREPEND);
+ f0.checkFails("^array_prepend(array[1], 2)^",
+ "No match found for function signature ARRAY_PREPEND\\("
+ + "<INTEGER ARRAY>, <NUMERIC>\\)", false);
+
+ final SqlOperatorFixture f = f0.withLibrary(SqlLibrary.SPARK);
+ f.checkScalar("array_prepend(array[1], cast(3 as float))", "[3.0, 1.0]",
+ "FLOAT NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array[1], cast(3 as bigint))", "[3, 1]",
+ "BIGINT NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array[2], cast(3 as double))", "[3.0, 2.0]",
+ "DOUBLE NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array[1, 2], cast(3 as float))", "[3.0, 1.0,
2.0]",
+ "FLOAT NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array[2, 1], cast(3 as double))", "[3.0, 2.0,
1.0]",
+ "DOUBLE NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array[1, 2], cast(3 as tinyint))", "[3, 1,
2]",
+ "INTEGER NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array[1, 2], cast(3 as bigint))", "[3, 1, 2]",
+ "BIGINT NOT NULL ARRAY NOT NULL");
+ f.checkScalar("array_prepend(array(1, 2), cast(null as double))", "[null,
1.0, 2.0]",
Review Comment:
use spark array is enough. we have a issue:
https://issues.apache.org/jira/browse/CALCITE-6042
If we have a better solution, then we can use more clean way to support std
array & spark array function.
The current way is a bit bloated.
##########
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorUtil.java:
##########
@@ -1326,6 +1327,48 @@ public static void adjustTypeForArrayConstructor(
}
}
+ /**
+ * When the array_append and array_prepend element does not equal the array
component type
+ * make explicit casting.
+ *
+ * @param componentType derived array component type
+ * @param opBinding description of call
+ */
+ public static void adjustTypeForArrayFunctionConstructor(
Review Comment:
1.about method name, maybe adjustTypeForArrayRelatedFunction is better. here
is not Constructor
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]