Skip to content

Commit

Permalink
[FLINK-15420][table-planner-blink] Fix precision is lost when casting…
Browse files Browse the repository at this point in the history
… string to timestamp (apache#10727)
  • Loading branch information
docete authored and wuchong committed Jan 2, 2020
1 parent dc0cf3b commit 24c970b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,7 @@ object ScalarOperatorGens {
resultNullable = true) {
operandTerm =>
s"""
|$SQL_TIMESTAMP.fromEpochMillis(
| ${qualifyMethod(BuiltInMethod.STRING_TO_TIMESTAMP.method)}($operandTerm.toString()))
|${qualifyMethod(BuiltInMethods.STRING_TO_TIMESTAMP)}($operandTerm.toString())
""".stripMargin
}

Expand Down Expand Up @@ -2289,8 +2288,7 @@ object ScalarOperatorGens {
s"${qualifyMethod(BuiltInMethods.STRING_TO_TIME)}($operandTerm.toString())"
case TIMESTAMP_WITHOUT_TIME_ZONE =>
s"""
|${SQL_TIMESTAMP}.fromEpochMillis(
| ${qualifyMethod(BuiltInMethod.STRING_TO_TIMESTAMP.method)}($operandTerm.toString()))
|${qualifyMethod(BuiltInMethods.STRING_TO_TIMESTAMP)}($operandTerm.toString())
|""".stripMargin
case _ => throw new UnsupportedOperationException
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ class TemporalTypesTest extends ExpressionTestBase {
testSqlApi(
"TIMESTAMP '1500-04-30 12:00:00.1234'",
"1500-04-30 12:00:00.1234")

testSqlApi(
"CAST('1500-04-30 12:00:00.123456789' AS TIMESTAMP(9))",
"1500-04-30 12:00:00.123456789")

// by default, it's TIMESTAMP(6)
testSqlApi(
"CAST('1500-04-30 12:00:00.123456789' AS TIMESTAMP)",
"1500-04-30 12:00:00.123456")

testSqlApi(
"CAST('1999-9-10 05:20:10.123456' AS TIMESTAMP)",
"1999-09-10 05:20:10.123456"
)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
Expand Down Expand Up @@ -253,7 +254,13 @@ public static SqlTimestamp toSqlTimestamp(String dateStr, String format) {
return SqlTimestamp.fromLocalDateTime(ldt);
}
} catch (DateTimeParseException e) {
return null;
// fall back to support cases like '1999-9-10 05:20:10'
try {
Timestamp ts = Timestamp.valueOf(dateStr);
return SqlTimestamp.fromTimestamp(ts);
} catch (IllegalArgumentException ie) {
return null;
}
}
}

Expand Down

0 comments on commit 24c970b

Please sign in to comment.