Skip to content

Commit

Permalink
[FLINK-27368][table-planner] Trim casts from character string to numeric
Browse files Browse the repository at this point in the history
This closes apache#19565.
  • Loading branch information
twalthr committed May 4, 2022
1 parent 90e98ba commit 7128a60
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

/**
Expand All @@ -54,19 +55,20 @@ public String generateExpression(
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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Stream<CastTestSpecBuilder> testCases() {
.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),
Expand Down Expand Up @@ -203,6 +204,7 @@ Stream<CastTestSpecBuilder> testCases() {
.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),
Expand Down Expand Up @@ -247,6 +249,7 @@ Stream<CastTestSpecBuilder> testCases() {
.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
Expand Down Expand Up @@ -293,6 +296,7 @@ Stream<CastTestSpecBuilder> testCases() {
.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(
Expand Down Expand Up @@ -334,6 +338,7 @@ Stream<CastTestSpecBuilder> testCases() {
.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)
Expand Down Expand Up @@ -380,6 +385,8 @@ Stream<CastTestSpecBuilder> testCases() {
.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)
Expand Down Expand Up @@ -1251,6 +1258,14 @@ Stream<CastTestSpecBuilder> testCases() {
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),
Expand Down

0 comments on commit 7128a60

Please sign in to comment.