This is an automated email from the ASF dual-hosted git repository.
twalthr pushed a commit to branch release-1.15
in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/release-1.15 by this push:
new b1b58ee4c2e [FLINK-27368][table-planner] Trim casts from character
string to numeric
b1b58ee4c2e is described below
commit b1b58ee4c2e61bc6f8e9c4d356708c1e080b3ee9
Author: Timo Walther <[email protected]>
AuthorDate: Mon Apr 25 09:39:29 2022 +0200
[FLINK-27368][table-planner] Trim casts from character string to numeric
This closes #19565.
---
.../casting/StringToNumericPrimitiveCastRule.java | 14 ++++++++------
.../table/planner/functions/casting/CastRulesTest.java | 15 +++++++++++++++
2 files changed, 23 insertions(+), 6 deletions(-)
diff --git
a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToNumericPrimitiveCastRule.java
b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToNumericPrimitiveCastRule.java
index 314930247a0..a0ba3698bae 100644
---
a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToNumericPrimitiveCastRule.java
+++
b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToNumericPrimitiveCastRule.java
@@ -28,6 +28,7 @@ import static
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING
import static
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_INT;
import static
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_LONG;
import static
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_SHORT;
+import static
org.apache.flink.table.planner.functions.casting.CastRuleUtils.methodCall;
import static
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
/**
@@ -54,19 +55,20 @@ class StringToNumericPrimitiveCastRule
String inputTerm,
LogicalType inputLogicalType,
LogicalType targetLogicalType) {
+ final String trimmedInputTerm = methodCall(inputTerm, "trim");
switch (targetLogicalType.getTypeRoot()) {
case TINYINT:
- return staticCall(STRING_DATA_TO_BYTE(), inputTerm);
+ return staticCall(STRING_DATA_TO_BYTE(), trimmedInputTerm);
case SMALLINT:
- return staticCall(STRING_DATA_TO_SHORT(), inputTerm);
+ return staticCall(STRING_DATA_TO_SHORT(), trimmedInputTerm);
case INTEGER:
- return staticCall(STRING_DATA_TO_INT(), inputTerm);
+ return staticCall(STRING_DATA_TO_INT(), trimmedInputTerm);
case BIGINT:
- return staticCall(STRING_DATA_TO_LONG(), inputTerm);
+ return staticCall(STRING_DATA_TO_LONG(), trimmedInputTerm);
case FLOAT:
- return staticCall(STRING_DATA_TO_FLOAT(), inputTerm);
+ return staticCall(STRING_DATA_TO_FLOAT(), trimmedInputTerm);
case DOUBLE:
- return staticCall(STRING_DATA_TO_DOUBLE(), inputTerm);
+ return staticCall(STRING_DATA_TO_DOUBLE(), trimmedInputTerm);
}
throw new IllegalArgumentException("This is a bug. Please file an
issue.");
}
diff --git
a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java
b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java
index f676f88ff8f..6b69667b2a6 100644
---
a/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java
+++
b/flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java
@@ -169,6 +169,7 @@ class CastRulesTest {
.fail(STRING(), fromString("Apache"),
TableException.class)
.fromCase(STRING(), fromString("1.234"), (byte) 1)
.fromCase(STRING(), fromString("123"), (byte) 123)
+ .fromCase(STRING(), fromString(" 123 "), (byte) 123)
.fail(STRING(), fromString("-130"),
TableException.class)
.fromCase(
DECIMAL(4, 3),
@@ -203,6 +204,7 @@ class CastRulesTest {
.fail(STRING(), fromString("Apache"),
TableException.class)
.fromCase(STRING(), fromString("1.234"), (short) 1)
.fromCase(STRING(), fromString("123"), (short) 123)
+ .fromCase(STRING(), fromString(" 123 "), (short) 123)
.fail(STRING(), fromString("-32769"),
TableException.class)
.fromCase(
DECIMAL(4, 3),
@@ -247,6 +249,7 @@ class CastRulesTest {
.fail(STRING(), fromString("Apache"),
TableException.class)
.fromCase(STRING(), fromString("1.234"), 1)
.fromCase(STRING(), fromString("123"), 123)
+ .fromCase(STRING(), fromString(" 123 "), 123)
.fail(STRING(), fromString("-3276913443134"),
TableException.class)
.fromCase(DECIMAL(4, 3), fromBigDecimal(new
BigDecimal("9.87"), 4, 3), 9)
// https://issues.apache.org/jira/browse/FLINK-24420 -
Check out of range
@@ -293,6 +296,7 @@ class CastRulesTest {
.fail(STRING(), fromString("Apache"),
TableException.class)
.fromCase(STRING(), fromString("1.234"), 1L)
.fromCase(STRING(), fromString("123"), 123L)
+ .fromCase(STRING(), fromString(" 123 "), 123L)
.fromCase(STRING(), fromString("-3276913443134"),
-3276913443134L)
.fromCase(DECIMAL(4, 3), fromBigDecimal(new
BigDecimal("9.87"), 4, 3), 9L)
.fromCase(
@@ -334,6 +338,7 @@ class CastRulesTest {
.fail(STRING(), fromString("Apache"),
TableException.class)
.fromCase(STRING(), fromString("1.234"), 1.234f)
.fromCase(STRING(), fromString("123"), 123.0f)
+ .fromCase(STRING(), fromString(" 123 "), 123.0f)
.fromCase(STRING(), fromString("-3276913443134"),
-3.27691351E12f)
.fromCase(
DECIMAL(4, 3), fromBigDecimal(new
BigDecimal("9.87"), 4, 3), 9.87f)
@@ -380,6 +385,8 @@ class CastRulesTest {
.fail(STRING(), fromString("Apache"),
TableException.class)
.fromCase(STRING(), fromString("1.234"), 1.234d)
.fromCase(STRING(), fromString("123"), 123.0d)
+ .fromCase(STRING(), fromString(" 123 "), 123.0d)
+ .fromCase(STRING(), fromString(" .123 "), 0.123d)
.fromCase(STRING(), fromString("-3276913443134"),
-3.276913443134E12d)
.fromCase(
DECIMAL(4, 3), fromBigDecimal(new
BigDecimal("9.87"), 4, 3), 9.87d)
@@ -1251,6 +1258,14 @@ class CastRulesTest {
STRING(),
fromString("1.2"),
fromBigDecimal(new BigDecimal("1.200"), 5, 3))
+ .fromCase(
+ STRING(),
+ fromString(" 1.2 "),
+ fromBigDecimal(new BigDecimal("1.200"), 5, 3))
+ .fromCase(
+ STRING(),
+ fromString(" .2 "),
+ fromBigDecimal(new BigDecimal("0.200"), 5, 3))
.fromCase(
DECIMAL(4, 3),
fromBigDecimal(new BigDecimal("9.87"), 4, 3),