raminqaf commented on code in PR #30214:
URL: https://github.com/apache/flink/pull/30214#discussion_r4753592921
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java:
##########
@@ -689,6 +724,46 @@ Stream<CastTestSpecBuilder> testCases() {
STRING(),
fromString("23:45"),
DateTimeUtils.toInternal(LocalTime.of(23, 45,
0)))
+ .fromCase(
+ STRING(),
+ fromString(" 12 : 30 : 45 "),
+ DateTimeUtils.toInternal(LocalTime.of(12, 30,
45)))
+ .fromCase(
+ STRING(),
+ fromString("12:30:45+02"),
+ DateTimeUtils.toInternal(LocalTime.of(14, 30,
45)))
+ .fromCase(
+ STRING(),
+ fromString("12:30:45+02:00"),
+ DateTimeUtils.toInternal(LocalTime.of(14, 30,
45)))
+ .fromCase(
+ STRING(),
+ fromString("12:30:45-02:30"),
+ DateTimeUtils.toInternal(LocalTime.of(10, 0,
45)))
Review Comment:
These three cases pin the sign of the offset. `12:30:45+02:00` gives
`14:30:45`, but in ISO 8601 that time is `10:30:45 UTC`.
The offset is also applied after the range check, so `23:00:00+02:00` gives
`25:00:00`.
Both come from the old code, not from this PR. In a refactor, pinning them
makes them look intended. Could we drop these three cases and open a ticket for
the offset handling?
##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/functions/casting/CastRulesTest.java:
##########
@@ -2338,6 +2413,19 @@ Stream<CastTestSpecBuilder> testCases() {
// A variant keeps microseconds for TIME, so fractional
seconds beyond the target
// precision are truncated, matching a regular cast into a
narrower TIME.
CastTestSpecBuilder.testCastTo(TIME(3))
+ .fromCase(
+ STRING(),
+ fromString("12:30:45.123"),
+ DateTimeUtils.toInternal(LocalTime.of(12, 30,
45, 123_000_000)))
+ // the digit after the milliseconds rounds them
+ .fromCase(
+ STRING(),
+ fromString("12:30:45.1235"),
+ DateTimeUtils.toInternal(LocalTime.of(12, 30,
45, 124_000_000)))
+ .fromCase(
+ STRING(),
+ fromString("12:30:45. 123 "),
+ DateTimeUtils.toInternal(LocalTime.of(12, 30,
45, 123_000_000)))
Review Comment:
Should we move this down?
The comment above this spec is about a VARIANT and says extra digits are
truncated.
Or move the comment
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/DateTimeUtils.java:
##########
@@ -764,23 +770,61 @@ private static boolean isValidTime(int hour, int minute,
int second) {
&& second <= 59;
}
+ /**
+ * The number in {@code v[from, to)}, without the whitespace around it
that {@link String#trim}
+ * removes, or -1 when it is empty or has a character other than a digit.
Reads it in place, so
+ * that parsing a date or a time makes no strings.
+ */
+ private static int parseDateTimePart(String v, int from, int to) {
+ while (from < to && v.charAt(from) <= ' ') {
+ from++;
+ }
+ while (to > from && v.charAt(to - 1) <= ' ') {
+ to--;
+ }
+ if (from == to) {
+ return -1;
+ }
+ long value = 0;
+ for (int i = from; i < to; i++) {
+ final char c = v.charAt(i);
+ if (c < '0' || c > '9') {
+ return -1;
+ }
+ // stops growing once too large, as only digits are checked from
there on
+ value = Math.min(value * 10 + (c - '0'), Integer.MAX_VALUE + 1L);
+ }
+ if (value > Integer.MAX_VALUE) {
+ // as Integer.parseInt, for a number of digits only
+ throw new NumberFormatException("For input string: \"" +
v.substring(from, to) + "\"");
+ }
Review Comment:
in other bad scenarios like if `c` is not a number or if `from` and `to` are
equal we return `-1`. Should we do the same for an overflow?
Then `99999999999:00` fails with the same message as `99999999999a:00`
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/utils/DateTimeUtils.java:
##########
@@ -764,23 +770,61 @@ private static boolean isValidTime(int hour, int minute,
int second) {
&& second <= 59;
}
+ /**
+ * The number in {@code v[from, to)}, without the whitespace around it
that {@link String#trim}
+ * removes, or -1 when it is empty or has a character other than a digit.
Reads it in place, so
+ * that parsing a date or a time makes no strings.
+ */
Review Comment:
This is so much text I read and don't understand 😄 Can we instead have a
example and simplify this?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]