Skip to content

Commit 932aa43

Browse files
authored
[FLINK-38824][table] Fix incorrect default values for primitive types
This closes #27363.
1 parent eb65369 commit 932aa43

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/codegen/CodeGenUtils.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ object CodeGenUtils {
301301
// ordered by type root definition
302302
case CHAR | VARCHAR => s"$BINARY_STRING.EMPTY_UTF8"
303303
case BOOLEAN => "false"
304-
case TINYINT | SMALLINT | INTEGER | DATE | TIME_WITHOUT_TIME_ZONE | INTERVAL_YEAR_MONTH => "-1"
304+
case TINYINT => "((byte) -1)"
305+
case SMALLINT => "((short) -1)"
306+
case INTEGER | DATE | TIME_WITHOUT_TIME_ZONE | INTERVAL_YEAR_MONTH => "-1"
305307
case BIGINT | INTERVAL_DAY_TIME => "-1L"
306308
case FLOAT => "-1.0f"
307309
case DOUBLE => "-1.0d"

flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/codegen/CodeGenUtilsTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ object CodeGenUtilsTest {
9191
java.util.stream.Stream.of(
9292
// Basic primitive types
9393
Arguments.of(new BooleanType(), "false"),
94-
Arguments.of(new TinyIntType(), "-1"),
95-
Arguments.of(new SmallIntType(), "-1"),
94+
Arguments.of(new TinyIntType(), "((byte) -1)"),
95+
Arguments.of(new SmallIntType(), "((short) -1)"),
9696
Arguments.of(new IntType(), "-1"),
9797
Arguments.of(new BigIntType(), "-1L"),
9898
Arguments.of(new FloatType(), "-1.0f"),
@@ -141,12 +141,12 @@ object CodeGenUtilsTest {
141141
DistinctType
142142
.newBuilder(objectIdentifier, new SmallIntType())
143143
.build(),
144-
"-1"),
144+
"((short) -1)"),
145145
Arguments.of(
146146
DistinctType
147147
.newBuilder(objectIdentifier, new TinyIntType())
148148
.build(),
149-
"-1"),
149+
"((byte) -1)"),
150150
Arguments.of(
151151
DistinctType
152152
.newBuilder(objectIdentifier, new BigIntType())

flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/runtime/stream/sql/CalcITCase.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,4 +926,26 @@ class CalcITCase extends StreamingTestBase {
926926
assertThat(arr.apply(1).get("nested2").asInstanceOf[Array[String]])
927927
.isEqualTo(Array("Test2", "False"))
928928
}
929+
930+
@Test
931+
def testPrimitiveDefaultValues(): Unit = {
932+
val sql =
933+
"""
934+
|SELECT
935+
| a[1]
936+
| ,b[1]
937+
|FROM (
938+
| VALUES (CAST(ARRAY[1,2] AS ARRAY<SMALLINT>), CAST(ARRAY[2,3] AS ARRAY<TINYINT>))
939+
|) t(a, b)
940+
|""".stripMargin
941+
942+
val result = tEnv
943+
.executeSql(sql)
944+
.collect()
945+
.asScala
946+
.toList
947+
.map(_.toString)
948+
val expected = List("1,2")
949+
assertThat(result).isEqualTo(expected)
950+
}
929951
}

0 commit comments

Comments
 (0)