You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/09/30 12:29:21 UTC

[GitHub] [flink] matriv opened a new pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

matriv opened a new pull request #17396:
URL: https://github.com/apache/flink/pull/17396


   ## What is the purpose of the change
   
   Test currently supported CAST functionality
   
   ## Brief change log
   
     - Add tests for currently supported type combinations
     - List unsupported ones in comment sections alongside a url pointing to the corresponding issue.
     - List the unsupported type combination that will never be supported
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     -  Added integration tests for CAST functionality
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): no
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: no
     - The serializers: no
     - The runtime per-record code paths (performance sensitive): no
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
     - The S3 file system connector: no
   
   ## Documentation
   
     - Does this pull request introduce a new feature? no
     - If yes, how is the feature documented? not applicable
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 268f980ade47dbc2e0b95f7a734298a0c5823cb2 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664) 
   * 446362585c664a26aeedf7864f1639134eb90fa9 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 268f980ade47dbc2e0b95f7a734298a0c5823cb2 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664) 
   * 446362585c664a26aeedf7864f1639134eb90fa9 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720324115



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")

Review comment:
       I would rename it to plain `case()`, what do you think?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * e144e131c041cc6cbd232ef4cfe6a00fe3710813 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-933357929


   @slinkydeveloper 
   
   > > * I see there are some failing "casts" returning null. Perhaps, given the [TRY_CAST issue](https://issues.apache.org/jira/browse/FLINK-24385), would it be better for the `CastTestSpecBuilder` to have a method like `fails` or something like that? Now such a method should just wrap the `resultsIn(null)` invocation, but when the TRY_CAST issue is tackled it can be modified to assert if an exception is thrown when executing the query. So we can just reuse these test cases for TRY_CAST as well
   > 
   > Good idea, I'll try to "prepare the ground" for the upcoming changes and tests, thx!
   
   I'd actually postpone this work until the TRY_CAST. If we introduce a `fails()` method then this method should create `ErrorTestItem`s which will have the corresponding expected error msg, something that cannot be used at the moment.


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720324115



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")

Review comment:
       I would rename it to plain `case()`, what do you think?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931280259


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit 268f980ade47dbc2e0b95f7a734298a0c5823cb2 (Thu Sep 30 12:34:00 UTC 2021)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] twalthr commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
twalthr commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720221129



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY

Review comment:
       ignore this comment, I forgot that we are casting to BYTES not from BYTES




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] twalthr commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
twalthr commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720176453



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")

Review comment:
       nit: trailing zero is remove by Java already

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalDate.of(2021, 9, 25))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIME(), "To TIME")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalTime.of(23, 1, 1))
+                        .testCase(STRING(), "2021-09-27", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        .testCase(STRING(), "12:34:56.123456789", LocalTime.of(12, 34, 56, 0))
+                        .testCase(STRING(), "2021-09-27 12:34:56.123456789", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        .testCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalTime.of(1, 34, 56, 0))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIMESTAMP(9), "To TIMESTAMP")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "123", null)
+                        .testCase(STRING(), "2021-09-27", LocalDateTime.of(2021, 9, 27, 0, 0, 0, 0))
+                        .testCase(STRING(), "2021/09/27", null)
+                        .testCase(
+                                STRING(),
+                                "2021-09-27 12:34:56.123456789",
+                                LocalDateTime.of(2021, 9, 27, 12, 34, 56, 123456789))
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(
+                                DATE(),
+                                LocalDate.parse("2021-09-24"),
+                                LocalDateTime.of(2021, 9, 24, 0, 0, 0, 0))
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        // https://issues.apache.org/jira/browse/FLINK-24423 Continue using EPOCH
+                        // date or use 0 for the year?
+                        .testCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalDateTime.of(1970, 1, 1, 12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123456000))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000))
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalDateTime.of(2021, 9, 25, 1, 34, 56, 123400000))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIMESTAMP_LTZ(9), "To TIMESTAMP_LTZ")
+                        // Causes NPE
+                        // .testCase(CHAR(3), "foo", null)
+                        // .testCase(VARCHAR(5), "Flink", null)
+                        // .testCase(STRING(), "123", null)
+                        .testCase(
+                                STRING(),
+                                "2021-09-27",
+                                fromLocalToUTC(LocalDateTime.of(2021, 9, 27, 0, 0, 0, 0)))
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        .testCase(

Review comment:
       this is not related to FLINK-24415, we are using TIMESTAMP not TIME here

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/BuiltInFunctionTestBase.java
##########
@@ -80,10 +80,13 @@
 
     @Parameter public TestSpec testSpec;
 
+    protected TableEnvironment env() {

Review comment:
       It would be good to not give tests full access to the entire API. Once such a member is visible, unexperienced contributors will use it for things that are not intended. This would make it impossible to make the tests more efficient in the future. Instead, we should only expose what it really needed (e.g. functions, or configuration).

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/BuiltInFunctionTestBase.java
##########
@@ -80,10 +80,13 @@
 
     @Parameter public TestSpec testSpec;
 
+    protected TableEnvironment env() {

Review comment:
       let's only add `getConfiguration()` as a method for now

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info

Review comment:
       TIMESTAMP_LTZ should apply the timezone in the string, a timezone id/offset info is not required

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")

Review comment:
       I think we don't need the second string parameter, it is always equal to the first parameter. We could rename the method to make the code more readable: `testCastTo(CHAR(3)).fromCase(...).build()`

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),

Review comment:
       the commented code does not make much sense, let's just write `TIMESTAMP_WITH_TIME_ZONE` here without code

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())

Review comment:
       nit: use the `LogicalTypeRoot` for such lists, in this case `INTERVAL_YEAR_MONTH` and `INTERVAL_DAY_TIME` below

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))

Review comment:
       Why not `new BigDecimal(String)`?

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY

Review comment:
       let's support casting to `ARRAY<NUMERIC>` or at least `ARRAY<TINYINT>`? otherwise a user cannot use all the built-in array function to access elements or cardinality etc.

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")

Review comment:
       how does the instant look like after this? we should design the test in a way that the instant shows `24` but the resulting `LocalDate` is 25 due to the session timezone

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalDate.of(2021, 9, 25))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIME(), "To TIME")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalTime.of(23, 1, 1))
+                        .testCase(STRING(), "2021-09-27", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        .testCase(STRING(), "12:34:56.123456789", LocalTime.of(12, 34, 56, 0))
+                        .testCase(STRING(), "2021-09-27 12:34:56.123456789", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        .testCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE

Review comment:
       link to https://issues.apache.org/jira/browse/FLINK-20869

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN

Review comment:
       for the unsupported cases, did you check that the exception came from the validation layer instead of the code generator?

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")

Review comment:
       or reference `TEST_TZ` instead of `5`




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720345941



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")

Review comment:
       I am a bit confused, isn't the case currently?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] slinkydeveloper commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
slinkydeveloper commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720215123



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")

Review comment:
       this should be uppercase: `TRUE`




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "",
       "status" : "DELETED",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "934531870",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "",
       "status" : "CANCELED",
       "url" : "TBD",
       "triggerID" : "934531870",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   *  Unknown: [CANCELED](TBD) 
   * 0a209d095234617d30fcf95d5abbb96edb1f1529 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] twalthr commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
twalthr commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r722240386



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,921 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+    private static final ZoneOffset TEST_OFFSET = ZoneOffset.ofHoursMinutes(-1, -20);
+
+    @Override
+    protected Configuration configuration() {
+        return super.configuration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.testCastTo(CHAR(3))
+                        .fromCase(CHAR(3), "foo", "foo")
+                        .fromCase(CHAR(4), "foo", "foo ")
+                        .fromCase(CHAR(4), "foo ", "foo ")
+                        .fromCase(VARCHAR(3), "foo", "foo")
+                        .fromCase(VARCHAR(5), "foo", "foo")
+                        .fromCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .fromCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .fromCase(DATE(), LocalDate.parse("2021-09-30"), "2021-09-30") // "202"
+                        .build(),
+                CastTestSpecBuilder.testCastTo(VARCHAR(3))
+                        .fromCase(CHAR(3), "foo", "foo")
+                        .fromCase(CHAR(4), "foo", "foo ")
+                        .fromCase(CHAR(4), "foo ", "foo ")
+                        .fromCase(VARCHAR(3), "foo", "foo")
+                        .fromCase(VARCHAR(5), "foo", "foo")
+                        .fromCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .fromCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.testCastTo(STRING())
+                        .fromCase(CHAR(3), "foo", "foo")
+                        .fromCase(CHAR(5), "foo", "foo  ")
+                        .fromCase(VARCHAR(5), "Flink", "Flink")
+                        .fromCase(VARCHAR(10), "Flink", "Flink")
+                        .fromCase(STRING(), "Apache Flink", "Apache Flink")
+                        .fromCase(STRING(), null, null)
+                        .fromCase(BOOLEAN(), true, "true")
+                        .fromCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .fromCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .fromCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .fromCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .fromCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .fromCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .fromCase(TINYINT(), -125, "-125")
+                        .fromCase(SMALLINT(), 32767, "32767")
+                        .fromCase(INT(), -12345678, "-12345678")
+                        .fromCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .fromCase(FLOAT(), -123.456, "-123.456")
+                        .fromCase(DOUBLE(), 12345.678901, "12345.678901")
+                        .fromCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-17224 Currently, fractional
+                        // seconds are lost
+                        .fromCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .fromCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .fromCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+
+                        // https://issues.apache.org/jira/browse/FLINK-20869
+                        // TIMESTAMP_WITH_TIME_ZONE
+
+                        .fromCase(
+                                TIMESTAMP_LTZ(5),
+                                fromLocalTZ("2021-09-24T18:34:56.123456"),
+                                "2021-09-25 03:54:56.12345")
+                        .fromCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .fromCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .fromCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .fromCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .fromCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .fromCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .fromCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(BOOLEAN())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(CHAR(4), "true", true)
+                        .fromCase(VARCHAR(5), "FalsE", false)
+                        .fromCase(STRING(), "Apache Flink", null)
+                        .fromCase(STRING(), "TRUE", true)
+                        .fromCase(STRING(), "", null)
+                        .fromCase(BOOLEAN(), true, true)
+                        .fromCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, true)
+                        .fromCase(DECIMAL(2, 1), 0.0, false)
+                        .fromCase(TINYINT(), -125, true)
+                        .fromCase(TINYINT(), 0, false)
+                        .fromCase(SMALLINT(), 32767, true)
+                        .fromCase(SMALLINT(), 0, false)
+                        .fromCase(INT(), -12345678, true)
+                        .fromCase(INT(), 0, false)
+                        .fromCase(BIGINT(), 1234567891234L, true)
+                        .fromCase(BIGINT(), 0, false)
+                        .fromCase(FLOAT(), -123.456, true)
+                        .fromCase(FLOAT(), 0, false)
+                        .fromCase(DOUBLE(), 12345.67890, true)
+                        .fromCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(BINARY(2))
+                        .fromCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .fromCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .fromCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .fromCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .fromCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .fromCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.testCastTo(VARBINARY(4))
+                        .fromCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .fromCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .fromCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .fromCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .fromCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .fromCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.testCastTo(BYTES())
+                        .fromCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .fromCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .fromCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .fromCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .fromCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .fromCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(DECIMAL(4, 3))
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .fromCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .fromCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870"))
+                        .fromCase(TINYINT(), -125, null)
+                        .fromCase(SMALLINT(), 32767, null)
+                        .fromCase(INT(), -12345678, null)
+                        .fromCase(BIGINT(), 1234567891234L, null)
+                        .fromCase(FLOAT(), -123.456, null)
+                        .fromCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(TINYINT())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", (byte) 1)
+                        .fromCase(STRING(), "123", (byte) 123)
+                        .fromCase(STRING(), "-130", null)
+                        .fromCase(BOOLEAN(), true, (byte) 1)
+                        .fromCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .fromCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .fromCase(TINYINT(), -125, (byte) -125)
+                        .fromCase(SMALLINT(), 32, (byte) 32)
+                        .fromCase(SMALLINT(), 32767, (byte) -1)
+                        .fromCase(INT(), -12, (byte) -12)
+                        .fromCase(INT(), -12345678, (byte) -78)
+                        .fromCase(BIGINT(), 123, (byte) 123)
+                        .fromCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .fromCase(FLOAT(), -123.456, (byte) -123)
+                        .fromCase(FLOAT(), 128.456, (byte) -128)
+                        .fromCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .fromCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(SMALLINT())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", (short) 1)
+                        .fromCase(STRING(), "123", (short) 123)
+                        .fromCase(STRING(), "-32769", null)
+                        .fromCase(BOOLEAN(), true, (short) 1)
+                        .fromCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .fromCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .fromCase(TINYINT(), -125, (short) -125)
+                        .fromCase(SMALLINT(), 32, (short) 32)
+                        .fromCase(SMALLINT(), 32780, (short) -32756)
+                        .fromCase(INT(), -12, (short) -12)
+                        .fromCase(INT(), -12345678, (short) -24910)
+                        .fromCase(BIGINT(), 123, (short) 123)
+                        .fromCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .fromCase(FLOAT(), -123.456, (short) -123)
+                        .fromCase(FLOAT(), 123456.78, (short) -7616)
+                        .fromCase(DOUBLE(), 123.4567890, (short) 123)
+                        .fromCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(INT())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", 1)
+                        .fromCase(STRING(), "123", 123)
+                        .fromCase(STRING(), "-3276913443134", null)
+                        .fromCase(BOOLEAN(), true, 1)
+                        .fromCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .fromCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .fromCase(TINYINT(), -125, -125)
+                        .fromCase(SMALLINT(), 32, 32)
+                        .fromCase(INT(), -12345678, -12345678)
+                        .fromCase(BIGINT(), 123, 123)
+                        .fromCase(BIGINT(), 1234567891234L, 1912277282)
+                        .fromCase(FLOAT(), -123.456, -123)
+                        .fromCase(FLOAT(), 9234567891.12, 644633299)
+                        .fromCase(DOUBLE(), 123.4567890, 123)
+                        .fromCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(BIGINT())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", 1L)
+                        .fromCase(STRING(), "123", 123L)
+                        .fromCase(STRING(), "-3276913443134", -3276913443134L)
+                        .fromCase(BOOLEAN(), true, 1L)
+                        .fromCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, 9L)
+                        .fromCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .fromCase(TINYINT(), -125, -125L)
+                        .fromCase(SMALLINT(), 32, 32L)
+                        .fromCase(INT(), -12345678, -12345678L)
+                        .fromCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .fromCase(FLOAT(), -123.456, -123L)
+                        .fromCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .fromCase(DOUBLE(), 123.4567890, 123L)
+                        .fromCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(FLOAT())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", 1.234f)
+                        .fromCase(STRING(), "123", 123.0f)
+                        .fromCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .fromCase(BOOLEAN(), true, 1.0f)
+                        .fromCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .fromCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .fromCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .fromCase(TINYINT(), -125, -125f)
+                        .fromCase(SMALLINT(), 32, 32f)
+                        .fromCase(INT(), -12345678, -12345678f)
+                        .fromCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .fromCase(FLOAT(), -123.456, -123.456f)
+                        .fromCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .fromCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .fromCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(DOUBLE())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "Apache", null)
+                        .fromCase(STRING(), "1.234", 1.234d)
+                        .fromCase(STRING(), "123", 123.0d)
+                        .fromCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .fromCase(BOOLEAN(), true, 1.0d)
+                        .fromCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .fromCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .fromCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .fromCase(TINYINT(), -125, -125d)
+                        .fromCase(SMALLINT(), 32, 32d)
+                        .fromCase(INT(), -12345678, -12345678d)
+                        .fromCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .fromCase(FLOAT(), -123.456, -123.456d)
+                        .fromCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .fromCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .fromCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(DATE())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .fromCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .fromCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .fromCase(
+                                STRING(),
+                                "2021-09-27 12:34:56.123456789",
+                                LocalDate.of(2021, 9, 27))
+                        .fromCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .fromCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .fromCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .fromCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+
+                        // https://issues.apache.org/jira/browse/FLINK-20869
+                        // TIMESTAMP_WITH_TIME_ZONE
+
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .fromCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .fromCase(
+                                TIMESTAMP_LTZ(),
+                                fromLocalTZ("2021-09-24T18:34:56.123456"),
+                                LocalDate.of(2021, 9, 25))
+                        // Not supported - no fix
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(TIME())
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .fromCase(STRING(), "123", LocalTime.of(23, 1, 1))
+                        .fromCase(STRING(), "2021-09-27", null)
+                        // https://issues.apache.org/jira/browse/FLINK-17224 Fractional seconds are
+                        // lost
+                        .fromCase(STRING(), "12:34:56.123456789", LocalTime.of(12, 34, 56, 0))
+                        .fromCase(STRING(), "2021-09-27 12:34:56.123456789", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        .fromCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .fromCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .fromCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+
+                        // https://issues.apache.org/jira/browse/FLINK-20869
+                        // TIMESTAMP_WITH_TIME_ZONE
+
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .fromCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .fromCase(
+                                TIMESTAMP_LTZ(4),
+                                fromLocalTZ("2021-09-24T22:34:56.123456"),
+                                LocalTime.of(7, 54, 56, 0))
+                        // Not supported - no fix
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(TIMESTAMP(9))
+                        .fromCase(CHAR(3), "foo", null)
+                        .fromCase(VARCHAR(5), "Flink", null)
+                        .fromCase(STRING(), "123", null)
+                        .fromCase(STRING(), "2021-09-27", LocalDateTime.of(2021, 9, 27, 0, 0, 0, 0))
+                        .fromCase(STRING(), "2021/09/27", null)
+                        .fromCase(
+                                STRING(),
+                                "2021-09-27 12:34:56.123456789",
+                                LocalDateTime.of(2021, 9, 27, 12, 34, 56, 123456789))
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .fromCase(
+                                DATE(),
+                                LocalDate.parse("2021-09-24"),
+                                LocalDateTime.of(2021, 9, 24, 0, 0, 0, 0))
+
+                        // https://issues.apache.org/jira/browse/FLINK-17224 Fractional seconds are
+                        // lost
+                        // https://issues.apache.org/jira/browse/FLINK-24423 Continue using EPOCH
+                        // date or use 0 for the year?
+                        .fromCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalDateTime.of(1970, 1, 1, 12, 34, 56, 0))
+                        .fromCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123456000))
+                        .fromCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000))
+
+                        // https://issues.apache.org/jira/browse/FLINK-20869
+                        // TIMESTAMP_WITH_TIME_ZONE
+
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .fromCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000))
+                        .fromCase(
+                                TIMESTAMP_LTZ(4),
+                                fromLocalTZ("2021-09-24T22:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 25, 7, 54, 56, 123400000))
+                        // Not supported - no fix
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.testCastTo(TIMESTAMP_LTZ(9))
+                        // https://issues.apache.org/jira/browse/FLINK-24424 - Throws NPE
+                        // .testCase(CHAR(3), "foo", null)
+                        // .testCase(VARCHAR(5), "Flink", null)
+                        // .testCase(STRING(), "123", null)
+                        .fromCase(
+                                STRING(),
+                                "2021-09-27",
+                                fromLocalToUTC(LocalDateTime.of(2021, 9, 27, 0, 0, 0, 0)))
+                        .fromCase(
+                                STRING(),
+                                "2021-09-27 12:34:56.123",
+                                fromLocalToUTC(
+                                        LocalDateTime.of(2021, 9, 27, 12, 34, 56, 123000000)))
+                        // https://issues.apache.org/jira/browse/FLINK-24403 Fractional seconds are
+                        // lost
+                        .fromCase(
+                                STRING(),
+                                "2021-09-27 12:34:56.123456789",
+                                fromLocalToUTC(LocalDateTime.of(2021, 9, 27, 12, 34, 56, 0)))
+
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .fromCase(
+                                DATE(),
+                                LocalDate.parse("2021-09-24"),
+                                fromLocalToUTC(LocalDateTime.of(2021, 9, 24, 0, 0, 0, 0)))
+
+                        // https://issues.apache.org/jira/browse/FLINK-17224 Fractional seconds are
+                        // lost
+                        // https://issues.apache.org/jira/browse/FLINK-24423 Continue using EPOCH
+                        // date or use 0 for the year?
+                        .fromCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                fromLocalToUTC(LocalDateTime.of(1970, 1, 1, 12, 34, 56, 0)))
+                        .fromCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                fromLocalToUTC(
+                                        LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123456000)))
+                        .fromCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                fromLocalToUTC(
+                                        LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000)))
+
+                        // https://issues.apache.org/jira/browse/FLINK-20869
+                        // TIMESTAMP_WITH_TIME_ZONE
+
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .fromCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                fromLocalToUTC(
+                                        LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000)))
+                        .fromCase(
+                                TIMESTAMP_LTZ(4),
+                                fromLocalTZ("2021-09-24T22:34:56.123456"),
+                                fromLocalToUTC(LocalDateTime.of(2021, 9, 25, 7, 54, 56, 123400000)))
+                        // Not supported - no fix
+                        // INTERVAL_YEAR_MONTH
+                        // INTERVAL_DAY_TIME
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                // CastTestSpecBuilder
+                // .testCast(INTERVAL(YEAR()), "To INTERVAL(YEAR())")

Review comment:
       update the comments to the new method names as well?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721176365



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())

Review comment:
       As discussed I will keep all types and use the LogicalTypeRoot ones as reference.
   




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 446362585c664a26aeedf7864f1639134eb90fa9 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670) 
   * e144e131c041cc6cbd232ef4cfe6a00fe3710813 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 446362585c664a26aeedf7864f1639134eb90fa9 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670) 
   * e144e131c041cc6cbd232ef4cfe6a00fe3710813 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720322270



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))

Review comment:
       I was playing around, that's a leftover, thx!




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-934530802


   @twalthr @slinkydeveloper Thx both for your reviews!


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 268f980ade47dbc2e0b95f7a734298a0c5823cb2 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721210828



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")

Review comment:
       the inverse works as case insensitive, if you look at the tests listed:
   ```
   .fromCase(CHAR(3), "foo", null)
   .fromCase(CHAR(4), "true", true)
   .fromCase(VARCHAR(5), "FalsE", false)
   .fromCase(STRING(), "Apache Flink", null)
   .fromCase(STRING(), "TRUE", true)
   ```




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 268f980ade47dbc2e0b95f7a734298a0c5823cb2 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720334393



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info

Review comment:
       This is what I mean, no tz info in the result string, I'll rephrase.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "",
       "status" : "DELETED",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     } ]
   }-->
   ## CI report:
   
   * 5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 446362585c664a26aeedf7864f1639134eb90fa9 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721239297



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalDate.of(2021, 9, 25))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIME(), "To TIME")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalTime.of(23, 1, 1))
+                        .testCase(STRING(), "2021-09-27", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        .testCase(STRING(), "12:34:56.123456789", LocalTime.of(12, 34, 56, 0))
+                        .testCase(STRING(), "2021-09-27 12:34:56.123456789", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        .testCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalTime.of(12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalTime.of(1, 34, 56, 0))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIMESTAMP(9), "To TIMESTAMP")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "123", null)
+                        .testCase(STRING(), "2021-09-27", LocalDateTime.of(2021, 9, 27, 0, 0, 0, 0))
+                        .testCase(STRING(), "2021/09/27", null)
+                        .testCase(
+                                STRING(),
+                                "2021-09-27 12:34:56.123456789",
+                                LocalDateTime.of(2021, 9, 27, 12, 34, 56, 123456789))
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(
+                                DATE(),
+                                LocalDate.parse("2021-09-24"),
+                                LocalDateTime.of(2021, 9, 24, 0, 0, 0, 0))
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        // https://issues.apache.org/jira/browse/FLINK-24423 Continue using EPOCH
+                        // date or use 0 for the year?
+                        .testCase(
+                                TIME(5),
+                                LocalTime.parse("12:34:56.1234567"),
+                                LocalDateTime.of(1970, 1, 1, 12, 34, 56, 0))
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123456000))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDateTime.of(2021, 9, 24, 12, 34, 56, 123400000))
+                        .testCase(
+                                TIMESTAMP_LTZ(4),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")
+                                        .toInstant(ZoneOffset.ofHours(5)),
+                                LocalDateTime.of(2021, 9, 25, 1, 34, 56, 123400000))
+                        // Not supported - no fix
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TIMESTAMP_LTZ(9), "To TIMESTAMP_LTZ")
+                        // Causes NPE
+                        // .testCase(CHAR(3), "foo", null)
+                        // .testCase(VARCHAR(5), "Flink", null)
+                        // .testCase(STRING(), "123", null)
+                        .testCase(
+                                STRING(),
+                                "2021-09-27",
+                                fromLocalToUTC(LocalDateTime.of(2021, 9, 27, 0, 0, 0, 0)))
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Fractional seconds are
+                        // lost
+                        .testCase(

Review comment:
       Thx, created a new issue for that [FLINK-24446](https://issues.apache.org/jira/browse/FLINK-24446)




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] twalthr closed pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
twalthr closed pull request #17396:
URL: https://github.com/apache/flink/pull/17396


   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] slinkydeveloper commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
slinkydeveloper commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r719387925



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,1167 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
+        // spotless:off
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
-    }
+                CastTestSpecBuilder.testCast(CHAR(3), "To CHAR(3)")
+                        .from(CHAR(3), "foo")
+                        .resultsIn("foo")
+                        .from(CHAR(4), "foo")
+                        .resultsIn("foo ")
+                        .from(CHAR(4), "foo ")
+                        .resultsIn("foo ")
+                        .from(VARCHAR(3), "foo")
+                        .resultsIn("foo")
+                        .from(VARCHAR(5), "foo")
+                        .resultsIn("foo")
+                        .from(VARCHAR(5), "foo ")
+                        .resultsIn("foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .from(STRING(), "abcdef")

Review comment:
       Can you just add a single extra test case with timestamp or any other type that requires prior casting to string and then the trim? Same for VARCHAR below 

##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,1167 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
+        // spotless:off
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
-    }
+                CastTestSpecBuilder.testCast(CHAR(3), "To CHAR(3)")
+                        .from(CHAR(3), "foo")
+                        .resultsIn("foo")
+                        .from(CHAR(4), "foo")
+                        .resultsIn("foo ")
+                        .from(CHAR(4), "foo ")
+                        .resultsIn("foo ")
+                        .from(VARCHAR(3), "foo")
+                        .resultsIn("foo")
+                        .from(VARCHAR(5), "foo")
+                        .resultsIn("foo")
+                        .from(VARCHAR(5), "foo ")
+                        .resultsIn("foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .from(STRING(), "abcdef")
+                        .resultsIn("abcdef")

Review comment:
       Perhaps can you comment this one like you did below for other wrong cases/results and write the correct result here?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721341645



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),

Review comment:
       Also added the corresponding issue as a comment on top of all TIMESTAMP_WITH_TIME_ZONE references.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "",
       "status" : "DELETED",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "",
       "status" : "CANCELED",
       "url" : "TBD",
       "triggerID" : "934531870",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24778",
       "triggerID" : "934531870",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24778",
       "triggerID" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   *  Unknown: [CANCELED](TBD) 
   * 0a209d095234617d30fcf95d5abbb96edb1f1529 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24778) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "triggerType" : "PUSH"
     }, {
       "hash" : "",
       "status" : "DELETED",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24778",
       "triggerID" : "934531870",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24778",
       "triggerID" : "0a209d095234617d30fcf95d5abbb96edb1f1529",
       "triggerType" : "PUSH"
     }, {
       "hash" : "",
       "status" : "DELETED",
       "url" : "TBD",
       "triggerID" : "934531870",
       "triggerType" : "MANUAL"
     } ]
   }-->
   ## CI report:
   
   * 0a209d095234617d30fcf95d5abbb96edb1f1529 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24778) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720358790



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())

Review comment:
       Should I remove `BYTES()` too from the unsupported lists and maybe also use the family, like for all 3 binary types: `BINARY_STRING`?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-934531870


   @flinkbot run azure


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-934519328


   @twalthr as discussed, I moved the tests from `ScalarOperatorTests` into the new class, but for the `DecimalCastTest` and any other related tests I created a new subtask: [FLINK-24454](https://issues.apache.org/jira/browse/FLINK-24454) since it required more work to consolidate everything into the new format.


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721169320



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN

Review comment:
       Yep, There is this case of casting from string types with invalid values to `TIMESTAMP_LTZ` which cause NPEs which is marked with the corresponding jira issue.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "CANCELED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * 268f980ade47dbc2e0b95f7a734298a0c5823cb2 Azure: [CANCELED](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664) 
   * 446362585c664a26aeedf7864f1639134eb90fa9 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931372324


   > * I see there are some failing "casts" returning null. Perhaps, given the [TRY_CAST issue](https://issues.apache.org/jira/browse/FLINK-24385), would it be better for the `CastTestSpecBuilder` to have a method like `fails` or something like that? Now such a method should just wrap the `resultsIn(null)` invocation, but when the TRY_CAST issue is tackled it can be modified to assert if an exception is thrown when executing the query. So we can just reuse these test cases for TRY_CAST as well
   
   Good idea, I'll try to "prepare the ground" for the upcoming changes and tests, thx!
   
   > * Related to errors with casting, what happens when casting to NON NULL types, eg what happens when invoking `CAST(f0 AS STRING NON NULL)`?
   
   Adding nullability combinations here would explode the number of tests, the nullability itself is unit tested in `LogicalTypeCastsTest`.
   
   


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] slinkydeveloper commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
slinkydeveloper commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721158964



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")

Review comment:
       I don't think my fix will influence this code path. Does `CAST("true" AS BOOLEAN)` works? Or it requires `TRUE` uppercase?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     }, {
       "hash" : "",
       "status" : "CANCELED",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758",
       "triggerID" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   *  Unknown: [CANCELED](TBD) 
   * 5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24758) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720330049



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")

Review comment:
       Currently, it's lowercase, I guess it will be fixed with you current work?




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r721340864



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(INTERVAL(YEAR()), 84, "+7-00")
+                        .testCase(INTERVAL(MONTH()), 5, "+0-05")
+                        .testCase(INTERVAL(MONTH()), 123, "+10-03")
+                        .testCase(INTERVAL(MONTH()), 12334, "+1027-10")
+                        .testCase(INTERVAL(DAY()), 10, "+0 00:00:00.010")
+                        .testCase(INTERVAL(DAY()), 123456789L, "+1 10:17:36.789")
+                        .testCase(INTERVAL(DAY()), Duration.ofHours(36), "+1 12:00:00.000")
+                        // https://issues.apache.org/jira/browse/FLINK-21456 Not supported currently
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BOOLEAN(), "To BOOLEAN")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(CHAR(4), "true", true)
+                        .testCase(VARCHAR(5), "FalsE", false)
+                        .testCase(STRING(), "Apache Flink", null)
+                        .testCase(STRING(), "TRUE", true)
+                        .testCase(STRING(), "", null)
+                        .testCase(BOOLEAN(), true, true)
+                        .testCase(BOOLEAN(), false, false)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, true)
+                        .testCase(DECIMAL(2, 1), 0.0, false)
+                        .testCase(TINYINT(), -125, true)
+                        .testCase(TINYINT(), 0, false)
+                        .testCase(SMALLINT(), 32767, true)
+                        .testCase(SMALLINT(), 0, false)
+                        .testCase(INT(), -12345678, true)
+                        .testCase(INT(), 0, false)
+                        .testCase(BIGINT(), 1234567891234L, true)
+                        .testCase(BIGINT(), 0, false)
+                        .testCase(FLOAT(), -123.456, true)
+                        .testCase(FLOAT(), 0, false)
+                        .testCase(DOUBLE(), 12345.67890, true)
+                        .testCase(DOUBLE(), 0, false)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BINARY(2), "To BINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(VARBINARY(4), "To VARBINARY")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        // https://issues.apache.org/jira/browse/FLINK-24419 - not trimmed to 2
+                        // bytes
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // .testCase(RAW(byte[].class), new byte[] {1}, new byte[] {1})
+                        .build(),
+                CastTestSpecBuilder.test(BYTES(), "To BYTES")
+                        .testCase(CHAR(3), "foo", new byte[] {102, 111, 111})
+                        .testCase(VARCHAR(5), "Flink", new byte[] {70, 108, 105, 110, 107})
+                        .testCase(STRING(), "Apache", new byte[] {65, 112, 97, 99, 104, 101})
+                        // Not supported - no fix
+                        // BOOLEAN
+                        .testCase(BINARY(2), new byte[] {0, 1}, new byte[] {0, 1})
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, new byte[] {0, 1, 2})
+                        .testCase(BYTES(), new byte[] {0, 1, 2, 3, 4}, new byte[] {0, 1, 2, 3, 4})
+                        // Not supported - no fix
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24418
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DECIMAL(4, 3), "To DECIMAL")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", new BigDecimal("1.234".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), true, new BigDecimal("1.000".toCharArray(), 0, 5))
+                        .testCase(BOOLEAN(), false, new BigDecimal("0.000".toCharArray(), 0, 5))
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, new BigDecimal("9.870".toCharArray(), 0, 5))
+                        .testCase(TINYINT(), -125, null)
+                        .testCase(SMALLINT(), 32767, null)
+                        .testCase(INT(), -12345678, null)
+                        .testCase(BIGINT(), 1234567891234L, null)
+                        .testCase(FLOAT(), -123.456, null)
+                        .testCase(DOUBLE(), 12345.67890, null)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(TINYINT(), "To TINYINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (byte) 1)
+                        .testCase(STRING(), "123", (byte) 123)
+                        .testCase(STRING(), "-130", null)
+                        .testCase(BOOLEAN(), true, (byte) 1)
+                        .testCase(BOOLEAN(), false, (byte) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (byte) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 9123.87, (byte) -93)
+                        .testCase(TINYINT(), -125, (byte) -125)
+                        .testCase(SMALLINT(), 32, (byte) 32)
+                        .testCase(SMALLINT(), 32767, (byte) -1)
+                        .testCase(INT(), -12, (byte) -12)
+                        .testCase(INT(), -12345678, (byte) -78)
+                        .testCase(BIGINT(), 123, (byte) 123)
+                        .testCase(BIGINT(), 1234567891234L, (byte) 34)
+                        .testCase(FLOAT(), -123.456, (byte) -123)
+                        .testCase(FLOAT(), 128.456, (byte) -128)
+                        .testCase(DOUBLE(), 123.4567890, (byte) 123)
+                        .testCase(DOUBLE(), 12345.67890, (byte) 57)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(SMALLINT(), "To SMALLINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", (short) 1)
+                        .testCase(STRING(), "123", (short) 123)
+                        .testCase(STRING(), "-32769", null)
+                        .testCase(BOOLEAN(), true, (short) 1)
+                        .testCase(BOOLEAN(), false, (short) 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, (short) 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(10, 3), 91235.87, (short) 25699)
+                        .testCase(TINYINT(), -125, (short) -125)
+                        .testCase(SMALLINT(), 32, (short) 32)
+                        .testCase(SMALLINT(), 32780, (short) -32756)
+                        .testCase(INT(), -12, (short) -12)
+                        .testCase(INT(), -12345678, (short) -24910)
+                        .testCase(BIGINT(), 123, (short) 123)
+                        .testCase(BIGINT(), 1234567891234L, (short) 2338)
+                        .testCase(FLOAT(), -123.456, (short) -123)
+                        .testCase(FLOAT(), 123456.78, (short) -7616)
+                        .testCase(DOUBLE(), 123.4567890, (short) 123)
+                        .testCase(DOUBLE(), 123456.7890, (short) -7616)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(INT(), "To INT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1)
+                        .testCase(STRING(), "123", 123)
+                        .testCase(STRING(), "-3276913443134", null)
+                        .testCase(BOOLEAN(), true, 1)
+                        .testCase(BOOLEAN(), false, 0)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, -146603714)
+                        .testCase(TINYINT(), -125, -125)
+                        .testCase(SMALLINT(), 32, 32)
+                        .testCase(INT(), -12345678, -12345678)
+                        .testCase(BIGINT(), 123, 123)
+                        .testCase(BIGINT(), 1234567891234L, 1912277282)
+                        .testCase(FLOAT(), -123.456, -123)
+                        .testCase(FLOAT(), 9234567891.12, 644633299)
+                        .testCase(DOUBLE(), 123.4567890, 123)
+                        .testCase(DOUBLE(), 9234567891.12345, 644633299)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(BIGINT(), "To BIGINT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1L)
+                        .testCase(STRING(), "123", 123L)
+                        .testCase(STRING(), "-3276913443134", -3276913443134L)
+                        .testCase(BOOLEAN(), true, 1L)
+                        .testCase(BOOLEAN(), false, 0L)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9L)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3276913443134L)
+                        .testCase(TINYINT(), -125, -125L)
+                        .testCase(SMALLINT(), 32, 32L)
+                        .testCase(INT(), -12345678, -12345678L)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234L)
+                        .testCase(FLOAT(), -123.456, -123L)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891L)
+                        .testCase(DOUBLE(), 123.4567890, 123L)
+                        .testCase(DOUBLE(), 9234567891.12345, 9234567891L)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(FLOAT(), "To FLOAT")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234f)
+                        .testCase(STRING(), "123", 123.0f)
+                        .testCase(STRING(), "-3276913443134", -3.27691403E12f)
+                        .testCase(BOOLEAN(), true, 1.0f)
+                        .testCase(BOOLEAN(), false, 0.0f)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        // https://issues.apache.org/jira/browse/FLINK-24420 - Check out of range
+                        // instead of overflow
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87f)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691351E12f)
+                        .testCase(TINYINT(), -125, -125f)
+                        .testCase(SMALLINT(), 32, 32f)
+                        .testCase(INT(), -12345678, -12345678f)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234f)
+                        .testCase(FLOAT(), -123.456, -123.456f)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12f)
+                        .testCase(DOUBLE(), 123.4567890, 123.45679f)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.23923451E12f)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DOUBLE(), "To DOUBLE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        .testCase(STRING(), "Apache", null)
+                        .testCase(STRING(), "1.234", 1.234d)
+                        .testCase(STRING(), "123", 123.0d)
+                        .testCase(STRING(), "-3276913443134", -3.276913443134E12)
+                        .testCase(BOOLEAN(), true, 1.0d)
+                        .testCase(BOOLEAN(), false, 0.0d)
+                        // Not supported - no fix
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        .testCase(DECIMAL(4, 3), 9.87, 9.87d)
+                        .testCase(DECIMAL(20, 3), 3276913443134.87, 3.27691344313487E12d)
+                        .testCase(TINYINT(), -125, -125d)
+                        .testCase(SMALLINT(), 32, 32d)
+                        .testCase(INT(), -12345678, -12345678d)
+                        .testCase(BIGINT(), 1234567891234L, 1234567891234d)
+                        .testCase(FLOAT(), -123.456, -123.456d)
+                        .testCase(FLOAT(), 9234567891.12, 9234567891.12d)
+                        .testCase(DOUBLE(), 123.4567890, 123.456789d)
+                        .testCase(DOUBLE(), 1239234567891.1234567891234, 1.2392345678911235E12d)
+                        // Not supported - no fix
+                        // DATE
+                        // TIME
+                        // TIMESTAMP
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        // TIMESTAMP_LTZ
+                        // INTERVAL(YEAR())
+                        // INTERVAL(DAY())
+                        // ARRAY
+                        // MULTISET
+                        // MAP
+                        // ROW
+                        // RAW
+                        .build(),
+                CastTestSpecBuilder.test(DATE(), "To DATE")
+                        .testCase(CHAR(3), "foo", null)
+                        .testCase(VARCHAR(5), "Flink", null)
+                        // https://issues.apache.org/jira/browse/FLINK-24421 Bug
+                        .testCase(STRING(), "123", LocalDate.of(123, 1, 1))
+                        .testCase(STRING(), "2021-09-27", LocalDate.of(2021, 9, 27))
+                        .testCase(STRING(), "2021/09/27", null)
+                        // Not supported - no fix
+                        // BOOLEAN
+                        // BINARY
+                        // VARBINARY
+                        // BYTES
+                        // DECIMAL
+                        // TINYINT
+                        // SMALLINT
+                        // INT
+                        // BIGINT
+                        // FLOAT
+                        // DOUBLE
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), LocalDate.of(2021, 9, 24))
+                        // Not supported - no fix
+                        // TIME
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        // Not supported currently
+                        // TIMESTAMP_WITH_TIME_ZONE
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24422 - Accept only Instant
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                LocalDate.of(2021, 9, 24))
+                        .testCase(
+                                TIMESTAMP_LTZ(),
+                                LocalDateTime.parse("2021-09-24T22:34:56.123456")

Review comment:
       I ended up using both a TEST_TZ and a TEST_OFFSET which are different, so the OFFSET is applied when creating source timestamps and the TEST_TZ to simulate the session timezone set by a user.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931366735


   Added this commit f25f9ba57ecf6f76d63aef4af3bca4e03b33c665 which imho improves overall readability, what do you think?


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-931316432


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24664",
       "triggerID" : "268f980ade47dbc2e0b95f7a734298a0c5823cb2",
       "triggerType" : "PUSH"
     }, {
       "hash" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24670",
       "triggerID" : "446362585c664a26aeedf7864f1639134eb90fa9",
       "triggerType" : "PUSH"
     }, {
       "hash" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=24740",
       "triggerID" : "e144e131c041cc6cbd232ef4cfe6a00fe3710813",
       "triggerType" : "PUSH"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "",
       "status" : "CANCELED",
       "url" : "TBD",
       "triggerID" : "933644982",
       "triggerType" : "MANUAL"
     }, {
       "hash" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   *  Unknown: [CANCELED](TBD) 
   * 5dc334a5b4a1fd0ae61fec6654a8502f8c53eb53 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on pull request #17396:
URL: https://github.com/apache/flink/pull/17396#issuecomment-933644982


   @flinkbot run azure


-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [flink] matriv commented on a change in pull request #17396: [FLINK-24393][test] Add CAST tests for type combinations

Posted by GitBox <gi...@apache.org>.
matriv commented on a change in pull request #17396:
URL: https://github.com/apache/flink/pull/17396#discussion_r720334393



##########
File path: flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/CastFunctionITCase.java
##########
@@ -18,176 +18,905 @@
 
 package org.apache.flink.table.planner.functions;
 
-import org.apache.flink.table.annotation.DataTypeHint;
-import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.TableEnvironment;
+import org.apache.flink.table.api.config.TableConfigOptions;
 import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
-import org.apache.flink.table.functions.ScalarFunction;
-import org.apache.flink.types.Row;
+import org.apache.flink.table.types.AbstractDataType;
+import org.apache.flink.table.types.DataType;
 
 import org.junit.runners.Parameterized;
 
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 
+import static org.apache.flink.table.api.DataTypes.ARRAY;
+import static org.apache.flink.table.api.DataTypes.BIGINT;
+import static org.apache.flink.table.api.DataTypes.BINARY;
+import static org.apache.flink.table.api.DataTypes.BOOLEAN;
+import static org.apache.flink.table.api.DataTypes.BYTES;
+import static org.apache.flink.table.api.DataTypes.CHAR;
+import static org.apache.flink.table.api.DataTypes.DATE;
+import static org.apache.flink.table.api.DataTypes.DAY;
+import static org.apache.flink.table.api.DataTypes.DECIMAL;
+import static org.apache.flink.table.api.DataTypes.DOUBLE;
+import static org.apache.flink.table.api.DataTypes.FLOAT;
+import static org.apache.flink.table.api.DataTypes.INT;
+import static org.apache.flink.table.api.DataTypes.INTERVAL;
+import static org.apache.flink.table.api.DataTypes.MONTH;
+import static org.apache.flink.table.api.DataTypes.SMALLINT;
+import static org.apache.flink.table.api.DataTypes.STRING;
+import static org.apache.flink.table.api.DataTypes.TIME;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP;
+import static org.apache.flink.table.api.DataTypes.TIMESTAMP_LTZ;
+import static org.apache.flink.table.api.DataTypes.TINYINT;
+import static org.apache.flink.table.api.DataTypes.VARBINARY;
+import static org.apache.flink.table.api.DataTypes.VARCHAR;
+import static org.apache.flink.table.api.DataTypes.YEAR;
 import static org.apache.flink.table.api.Expressions.$;
-import static org.apache.flink.table.api.Expressions.call;
-import static org.apache.flink.table.api.Expressions.row;
 
 /** Tests for {@link BuiltInFunctionDefinitions#CAST}. */
 public class CastFunctionITCase extends BuiltInFunctionTestBase {
 
+    private static final ZoneId TEST_TZ = ZoneId.of("Asia/Shanghai");
+
+    @Override
+    protected TableEnvironment env() {
+        TableEnvironment env = super.env();
+        env.getConfig().getConfiguration().set(TableConfigOptions.LOCAL_TIME_ZONE, TEST_TZ.getId());
+        return env;
+    }
+
     @Parameterized.Parameters(name = "{index}: {0}")
     public static List<TestSpec> testData() {
         return Arrays.asList(
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with different field names")
-                        .onFieldsWithData(Row.of(12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<otherNameInt INT, otherNameString STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(BuiltInFunctionDefinitions.CAST, "implicit with type widening")
-                        .onFieldsWithData(Row.of((byte) 12, "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<i TINYINT, s STRING>"))
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call("RowToFirstField", $("f0")),
-                                "RowToFirstField(f0)",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit with nested type widening")
-                        .onFieldsWithData(Row.of(Row.of(12, 42), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT>, s STRING>"))
-                        .withFunction(NestedRowToFirstField.class)
-                        .testResult(
-                                call("NestedRowToFirstField", $("f0")),
-                                "NestedRowToFirstField(f0)",
-                                Row.of(12, 42.0),
-                                DataTypes.of("ROW<i INT, d DOUBLE>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and implicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                "CAST(f0 AS ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>)",
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                // the inner NOT NULL is ignored in SQL because the outer ROW is
-                                // nullable and the cast does not allow setting the outer
-                                // nullability but derives it from the source operand
-                                DataTypes.of("ROW<r ROW<s STRING, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit with nested rows and explicit nullability change")
-                        .onFieldsWithData(Row.of(Row.of(12, 42, null), "Hello"))
-                        .andDataTypes(DataTypes.of("ROW<r ROW<i1 INT, i2 INT, i3 INT>, s STRING>"))
-                        .testTableApiResult(
-                                $("f0").cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.FIELD(
-                                                                "r",
-                                                                DataTypes.ROW(
-                                                                        DataTypes.FIELD(
-                                                                                "s",
-                                                                                DataTypes.STRING()
-                                                                                        .notNull()),
-                                                                        DataTypes.FIELD(
-                                                                                "b",
-                                                                                DataTypes
-                                                                                        .BOOLEAN()),
-                                                                        DataTypes.FIELD(
-                                                                                "i",
-                                                                                DataTypes.INT()))),
-                                                        DataTypes.FIELD("s", DataTypes.STRING()))),
-                                Row.of(Row.of("12", true, null), "Hello"),
-                                DataTypes.of(
-                                        "ROW<r ROW<s STRING NOT NULL, b BOOLEAN, i INT>, s STRING>")),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "implicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .withFunction(RowToFirstField.class)
-                        .testResult(
-                                call(
-                                        "RowToFirstField",
-                                        call("StructuredTypeConstructor", row($("f0"), $("f1")))),
-                                "RowToFirstField(StructuredTypeConstructor((f0, f1)))",
-                                12,
-                                DataTypes.INT()),
-                TestSpec.forFunction(
-                                BuiltInFunctionDefinitions.CAST,
-                                "explicit between structured type and row")
-                        .onFieldsWithData(12, "Ingo")
-                        .withFunction(StructuredTypeConstructor.class)
-                        .testTableApiResult(
-                                call("StructuredTypeConstructor", row($("f0"), $("f1")))
-                                        .cast(
-                                                DataTypes.ROW(
-                                                        DataTypes.BIGINT(), DataTypes.STRING())),
-                                Row.of(12L, "Ingo"),
-                                DataTypes.ROW(DataTypes.BIGINT(), DataTypes.STRING())));
+                CastTestSpecBuilder.test(CHAR(3), "To CHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef") // "abc"
+                        .testCase(DATE(), LocalDate.parse("2021-30-09"), "2021-30-09") // "202"
+                        .build(),
+                CastTestSpecBuilder.test(VARCHAR(3), "To VARCHAR(3)")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(4), "foo", "foo ")
+                        .testCase(CHAR(4), "foo ", "foo ")
+                        .testCase(VARCHAR(3), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo", "foo")
+                        .testCase(VARCHAR(5), "foo ", "foo ")
+                        // https://issues.apache.org/jira/browse/FLINK-24413 - Trim to precision
+                        // in this case down to 3 chars
+                        .testCase(STRING(), "abcdef", "abcdef")
+                        .build(),
+                CastTestSpecBuilder.test(STRING(), "To STRING")
+                        .testCase(CHAR(3), "foo", "foo")
+                        .testCase(CHAR(5), "foo", "foo  ")
+                        .testCase(VARCHAR(5), "Flink", "Flink")
+                        .testCase(VARCHAR(10), "Flink", "Flink")
+                        .testCase(STRING(), "Apache Flink", "Apache Flink")
+                        .testCase(STRING(), null, null)
+                        .testCase(BOOLEAN(), true, "true")
+                        .testCase(BINARY(2), new byte[] {0, 1}, "\u0000\u0001")
+                        .testCase(BINARY(3), new byte[] {0, 1}, "\u0000\u0001\u0000")
+                        .testCase(VARBINARY(3), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(VARBINARY(5), new byte[] {0, 1, 2}, "\u0000\u0001\u0002")
+                        .testCase(
+                                BYTES(),
+                                new byte[] {0, 1, 2, 3, 4},
+                                "\u0000\u0001\u0002\u0003\u0004")
+                        .testCase(DECIMAL(4, 3), 9.87, "9.870")
+                        // https://issues.apache.org/jira/browse/FLINK-24403 - Left zero padding
+                        // currently not working
+                        // .testCase(DECIMAL(5, 3), 09.87, "09.870")
+                        .testCase(TINYINT(), -125, "-125")
+                        .testCase(SMALLINT(), 32767, "32767")
+                        .testCase(INT(), -12345678, "-12345678")
+                        .testCase(BIGINT(), 1234567891234L, "1234567891234")
+                        .testCase(FLOAT(), -123.456, "-123.456")
+                        .testCase(DOUBLE(), 12345.67890, "12345.6789")
+                        .testCase(DATE(), LocalDate.parse("2021-09-24"), "2021-09-24")
+                        // https://issues.apache.org/jira/browse/FLINK-24415 Currently, fractional
+                        // seconds are lost
+                        .testCase(TIME(5), LocalTime.parse("12:34:56.1234567"), "12:34:56")
+                        .testCase(
+                                TIMESTAMP(),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.123456")
+                        .testCase(
+                                TIMESTAMP(4),
+                                LocalDateTime.parse("2021-09-24T12:34:56.123456"),
+                                "2021-09-24 12:34:56.1234")
+                        // Not supported
+                        // .testCase(TIMESTAMP_WITH_TIME_ZONE(),
+                        // LocalDateTime.parse("2021-09-24T12:34:56.123456"), "2021-09-24
+                        // 12:34:56.123456+0200")
+                        //
+                        // https://issues.apache.org/jira/browse/FLINK-24416 No timezone id/offset
+                        // info

Review comment:
       This is what I mean, no tz info in the result string, I'll rephrase.




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org