xiedeyantu commented on code in PR #4288:
URL: https://github.com/apache/calcite/pull/4288#discussion_r2034164569
##########
core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java:
##########
@@ -1104,6 +1107,140 @@ private static Stream<Arguments>
testData4testMakeZeroLiteral() {
relDataType -> new TimestampWithTimeZoneString(0, 1, 1, 0, 0, 0,
"GMT+00:00")));
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6938">[CALCITE-6938]
+ * Support zero value creation of nested data types</a>. */
+ @ParameterizedTest
+ @MethodSource("testData4testMakeZeroForNestedType")
+ void testMakeZeroForNestedType(RelDataType type, RexNode expected) {
+ final RelDataTypeFactory typeFactory = new
SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+ final RexBuilder rexBuilder = new RexBuilder(typeFactory);
+ assertThat(rexBuilder.makeZeroForNestedType(type), is(equalTo(expected)));
+ }
+
+ @Test void testCreateCoalesce() {
+ RelDataTypeFactory typeFactory = new
SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+ RexBuilder b = new RexBuilder(typeFactory);
+ RelDataType varcharType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
+
+ RelDataType arrayType = new ArraySqlType(varcharType, false);
+ RexNode arrayZero = b.makeZeroForNestedType(arrayType);
+
+ RexNode array =
+ b.makeCall(arrayType, SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR,
+ ImmutableList.of(
+ b.makeLiteral("1", varcharType)));
+
+ RexNode coalesce1 = b.makeCall(SqlStdOperatorTable.COALESCE, array,
arrayZero);
+ assertThat(
+ coalesce1, hasToString(
+ "COALESCE(ARRAY('1'), CAST(ARRAY()):VARCHAR NOT NULL ARRAY NOT
NULL)"));
+
+ RelDataType mapType = new MapSqlType(arrayType, arrayType, true);
+ RexNode mapZero = b.makeZeroForNestedType(mapType);
+
+ RexNode map =
+ b.makeCall(new MapSqlType(arrayType, arrayType, true),
+ SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR,
+ ImmutableList.of(array, array));
+
+ RexNode coalesce2 = b.makeCall(SqlStdOperatorTable.COALESCE, map, mapZero);
+ assertThat(
+ coalesce2, hasToString(
+ "COALESCE(MAP(ARRAY('1'), ARRAY('1')), "
+ + "CAST(MAP()):(VARCHAR NOT NULL ARRAY NOT NULL, VARCHAR NOT NULL
ARRAY NOT NULL) MAP)"));
+ }
+
+ private static Stream<Arguments> testData4testMakeZeroForNestedType() {
+ RelDataTypeFactory typeFactory = new
SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT);
+ RexBuilder b = new RexBuilder(typeFactory);
+
+ RelDataType integerType = typeFactory.createSqlType(SqlTypeName.INTEGER);
+ RelDataType varcharType = typeFactory.createSqlType(SqlTypeName.VARCHAR);
+
+ // ARRAY<INTEGER>
+ RelDataType arrayType = new ArraySqlType(integerType, false);
+ RexNode expectedArray =
+ b.makeCast(
+ arrayType, b.makeCall(arrayType,
SqlStdOperatorTable.ARRAY_VALUE_CONSTRUCTOR,
+ ImmutableList.of()));
+
+ // MULTISET<INTEGER>
+ RelDataType multisetType = new MultisetSqlType(integerType, false);
+ RexNode expectedMultiset =
+ b.makeCast(
+ multisetType, b.makeCall(multisetType,
SqlStdOperatorTable.MULTISET_VALUE,
+ ImmutableList.of()));
+
+ // MAP<VARCHAR, INTEGER>
+ RelDataType mapType = new MapSqlType(varcharType, integerType, false);
+ RexNode expectedMap =
+ b.makeCast(
+ mapType, b.makeCall(mapType,
SqlStdOperatorTable.MAP_VALUE_CONSTRUCTOR,
+ ImmutableList.of()));
+
+ // ROW<INTEGER, VARCHAR>
+ RelDataType rowType =
Review Comment:
Sorry, I didn't express myself clearly. I changed the zero value logic of
ROW, and its corresponding validation will also be changed. I will keep the
rest as it is.
--
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]