Fix milliseconds handling in intervals (#51675) (#52156)

This fixes:

- the parsing of milliseconds in intervals: everything past the . used to be converted as-is to milliseconds, with no normalisation of the unit; thus, a value of .23 ended up as 23 millis in the interval, instead of 230.
- the printing of a trailing .0, in case the interval lacks the fractional part;
- tests generating a random millisecond value used to simply print it in the string about to be evaluated without a necessary front-filling of 0[s], where the amount was below 100/10.

(The combination of first and last issues above, plus statistical "luck" made the incorrect handling pass the tests.)

(cherry picked from commit 4de8c64f63ee37c1bcfdb9b9d3a07d09be243222)
This commit is contained in:
Bogdan Pintea 2020-02-10 19:24:26 +01:00 committed by GitHub
parent d188dda7eb
commit 7b58ed0dd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 50 deletions

View File

@ -26,7 +26,7 @@ import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
//FIXME: Taken from sql-proto. //FIXME: Taken from sql-proto (StringUtils)
//Ideally it should be shared but the dependencies across projects and and SQL-client make it tricky. //Ideally it should be shared but the dependencies across projects and and SQL-client make it tricky.
// Maybe a gradle task would fix that... // Maybe a gradle task would fix that...
public class DateUtils { public class DateUtils {
@ -136,8 +136,14 @@ public class DateUtils {
sb.append(":"); sb.append(":");
durationInSec = durationInSec % SECONDS_PER_MINUTE; durationInSec = durationInSec % SECONDS_PER_MINUTE;
sb.append(indent(durationInSec)); sb.append(indent(durationInSec));
long millis = TimeUnit.NANOSECONDS.toMillis(d.getNano());
if (millis > 0) {
sb.append("."); sb.append(".");
sb.append(TimeUnit.NANOSECONDS.toMillis(d.getNano())); while (millis % 10 == 0) {
millis /= 10;
}
sb.append(millis);
}
return sb.toString(); return sb.toString();
} }

View File

@ -95,22 +95,22 @@ public abstract class SqlProtocolTestCase extends ESRestTestCase {
public void testDateTimeIntervals() throws IOException { public void testDateTimeIntervals() throws IOException {
assertQuery("SELECT INTERVAL '326' YEAR", "INTERVAL '326' YEAR", "interval_year", "P326Y", "+326-0", 7); assertQuery("SELECT INTERVAL '326' YEAR", "INTERVAL '326' YEAR", "interval_year", "P326Y", "+326-0", 7);
assertQuery("SELECT INTERVAL '50' MONTH", "INTERVAL '50' MONTH", "interval_month", "P50M", "+0-50", 7); assertQuery("SELECT INTERVAL '50' MONTH", "INTERVAL '50' MONTH", "interval_month", "P50M", "+0-50", 7);
assertQuery("SELECT INTERVAL '520' DAY", "INTERVAL '520' DAY", "interval_day", "PT12480H", "+520 00:00:00.0", 23); assertQuery("SELECT INTERVAL '520' DAY", "INTERVAL '520' DAY", "interval_day", "PT12480H", "+520 00:00:00", 23);
assertQuery("SELECT INTERVAL '163' HOUR", "INTERVAL '163' HOUR", "interval_hour", "PT163H", "+6 19:00:00.0", 23); assertQuery("SELECT INTERVAL '163' HOUR", "INTERVAL '163' HOUR", "interval_hour", "PT163H", "+6 19:00:00", 23);
assertQuery("SELECT INTERVAL '163' MINUTE", "INTERVAL '163' MINUTE", "interval_minute", "PT2H43M", "+0 02:43:00.0", 23); assertQuery("SELECT INTERVAL '163' MINUTE", "INTERVAL '163' MINUTE", "interval_minute", "PT2H43M", "+0 02:43:00", 23);
assertQuery("SELECT INTERVAL '223.16' SECOND", "INTERVAL '223.16' SECOND", "interval_second", "PT3M43.016S", "+0 00:03:43.16", 23); assertQuery("SELECT INTERVAL '223.16' SECOND", "INTERVAL '223.16' SECOND", "interval_second", "PT3M43.16S", "+0 00:03:43.16", 23);
assertQuery("SELECT INTERVAL '163-11' YEAR TO MONTH", "INTERVAL '163-11' YEAR TO MONTH", "interval_year_to_month", "P163Y11M", assertQuery("SELECT INTERVAL '163-11' YEAR TO MONTH", "INTERVAL '163-11' YEAR TO MONTH", "interval_year_to_month", "P163Y11M",
"+163-11", 7); "+163-11", 7);
assertQuery("SELECT INTERVAL '163 12' DAY TO HOUR", "INTERVAL '163 12' DAY TO HOUR", "interval_day_to_hour", "PT3924H", assertQuery("SELECT INTERVAL '163 12' DAY TO HOUR", "INTERVAL '163 12' DAY TO HOUR", "interval_day_to_hour", "PT3924H",
"+163 12:00:00.0", 23); "+163 12:00:00", 23);
assertQuery("SELECT INTERVAL '163 12:39' DAY TO MINUTE", "INTERVAL '163 12:39' DAY TO MINUTE", "interval_day_to_minute", assertQuery("SELECT INTERVAL '163 12:39' DAY TO MINUTE", "INTERVAL '163 12:39' DAY TO MINUTE", "interval_day_to_minute",
"PT3924H39M", "+163 12:39:00.0", 23); "PT3924H39M", "+163 12:39:00", 23);
assertQuery("SELECT INTERVAL '163 12:39:59.163' DAY TO SECOND", "INTERVAL '163 12:39:59.163' DAY TO SECOND", assertQuery("SELECT INTERVAL '163 12:39:59.163' DAY TO SECOND", "INTERVAL '163 12:39:59.163' DAY TO SECOND",
"interval_day_to_second", "PT3924H39M59.163S", "+163 12:39:59.163", 23); "interval_day_to_second", "PT3924H39M59.163S", "+163 12:39:59.163", 23);
assertQuery("SELECT INTERVAL -'163 23:39:56.23' DAY TO SECOND", "INTERVAL -'163 23:39:56.23' DAY TO SECOND", assertQuery("SELECT INTERVAL -'163 23:39:56.23' DAY TO SECOND", "INTERVAL -'163 23:39:56.23' DAY TO SECOND",
"interval_day_to_second", "PT-3935H-39M-56.023S", "-163 23:39:56.23", 23); "interval_day_to_second", "PT-3935H-39M-56.23S", "-163 23:39:56.23", 23);
assertQuery("SELECT INTERVAL '163:39' HOUR TO MINUTE", "INTERVAL '163:39' HOUR TO MINUTE", "interval_hour_to_minute", assertQuery("SELECT INTERVAL '163:39' HOUR TO MINUTE", "INTERVAL '163:39' HOUR TO MINUTE", "interval_hour_to_minute",
"PT163H39M", "+6 19:39:00.0", 23); "PT163H39M", "+6 19:39:00", 23);
assertQuery("SELECT INTERVAL '163:39:59.163' HOUR TO SECOND", "INTERVAL '163:39:59.163' HOUR TO SECOND", "interval_hour_to_second", assertQuery("SELECT INTERVAL '163:39:59.163' HOUR TO SECOND", "INTERVAL '163:39:59.163' HOUR TO SECOND", "interval_hour_to_second",
"PT163H39M59.163S", "+6 19:39:59.163", 23); "PT163H39M59.163S", "+6 19:39:59.163", 23);
assertQuery("SELECT INTERVAL '163:59.163' MINUTE TO SECOND", "INTERVAL '163:59.163' MINUTE TO SECOND", "interval_minute_to_second", assertQuery("SELECT INTERVAL '163:59.163' MINUTE TO SECOND", "INTERVAL '163:59.163' MINUTE TO SECOND", "interval_minute_to_second",

View File

@ -9,16 +9,16 @@ exactIntervals
SELECT INTERVAL 1 YEAR AS y, INTERVAL 2 MONTH AS m, INTERVAL 3 DAY AS d, INTERVAL 4 HOUR AS h, INTERVAL 5 MINUTE AS mm, INTERVAL 6 SECOND AS s; SELECT INTERVAL 1 YEAR AS y, INTERVAL 2 MONTH AS m, INTERVAL 3 DAY AS d, INTERVAL 4 HOUR AS h, INTERVAL 5 MINUTE AS mm, INTERVAL 6 SECOND AS s;
y | m | d | h | mm | s y | m | d | h | mm | s
---------------+---------------+---------------+---------------+---------------+--------------- ---------------+---------------+-------------+-------------+-------------+-------------
+1-0 |+0-2 |+3 00:00:00.0 |+0 04:00:00.0 |+0 00:05:00.0 |+0 00:00:06.0 +1-0 |+0-2 |+3 00:00:00 |+0 04:00:00 |+0 00:05:00 |+0 00:00:06
; ;
testExactIntervalPlural testExactIntervalPlural
SELECT INTERVAL 1 YEARS AS y, INTERVAL 2 MONTHS AS m, INTERVAL 3 DAYS AS d, INTERVAL 4 HOURS AS h, INTERVAL 5 MINUTES AS mm, INTERVAL 6 SECONDS AS s; SELECT INTERVAL 1 YEARS AS y, INTERVAL 2 MONTHS AS m, INTERVAL 3 DAYS AS d, INTERVAL 4 HOURS AS h, INTERVAL 5 MINUTES AS mm, INTERVAL 6 SECONDS AS s;
y | m | d | h | mm | s y | m | d | h | mm | s
---------------+---------------+---------------+---------------+---------------+--------------- ---------------+---------------+-------------+-------------+-------------+-------------
+1-0 |+0-2 |+3 00:00:00.0 |+0 04:00:00.0 |+0 00:05:00.0 |+0 00:00:06.0 +1-0 |+0-2 |+3 00:00:00 |+0 04:00:00 |+0 00:05:00 |+0 00:00:06
; ;
// take the examples from https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/interval-literals?view=sql-server-2017 // take the examples from https://docs.microsoft.com/en-us/sql/odbc/reference/appendixes/interval-literals?view=sql-server-2017
@ -43,7 +43,7 @@ SELECT INTERVAL '3261' DAY;
INTERVAL '3261' DAY INTERVAL '3261' DAY
------------------- -------------------
+3261 00:00:00.0 +3261 00:00:00
; ;
hour hour
@ -51,7 +51,7 @@ SELECT INTERVAL '163' HOUR;
INTERVAL '163' HOUR INTERVAL '163' HOUR
------------------- -------------------
+6 19:00:00.0 +6 19:00:00
; ;
minute minute
@ -59,7 +59,7 @@ SELECT INTERVAL '163' MINUTE;
INTERVAL '163' MINUTE INTERVAL '163' MINUTE
--------------------- ---------------------
+0 02:43:00.0 +0 02:43:00
; ;
second second
@ -83,7 +83,7 @@ SELECT INTERVAL '163 12' DAY TO HOUR;
INTERVAL '163 12' DAY TO HOUR INTERVAL '163 12' DAY TO HOUR
----------------------------- -----------------------------
+163 12:00:00.0 +163 12:00:00
; ;
dayMinute dayMinute
@ -91,7 +91,7 @@ SELECT INTERVAL '163 12:39' DAY TO MINUTE AS interval;
interval interval
--------------- ---------------
+163 12:39:00.0 +163 12:39:00
; ;
daySecond daySecond
@ -115,7 +115,7 @@ SELECT INTERVAL '163:39' HOUR TO MINUTE AS interval;
interval interval
--------------- ---------------
+6 19:39:00.0 +6 19:39:00
; ;
hourSecond hourSecond
@ -139,7 +139,7 @@ SELECT INTERVAL 1 DAY + INTERVAL 53 MINUTES;
INTERVAL 1 DAY + INTERVAL 53 MINUTES INTERVAL 1 DAY + INTERVAL 53 MINUTES
------------------------------------ ------------------------------------
+1 00:53:00.0 +1 00:53:00
; ;
datePlusIntervalInline datePlusIntervalInline
@ -163,8 +163,8 @@ intervalMinusInterval
SELECT INTERVAL '1' DAY - INTERVAL '2' HOURS AS result; SELECT INTERVAL '1' DAY - INTERVAL '2' HOURS AS result;
result result
--------------- -------------
+0 22:00:00.0 +0 22:00:00
; ;
@ -180,15 +180,15 @@ intervalDayMultiply
SELECT -2 * INTERVAL '1 23:45' DAY TO MINUTES AS result; SELECT -2 * INTERVAL '1 23:45' DAY TO MINUTES AS result;
result result
--------------- -------------
-3 23:30:00.0 -3 23:30:00
; ;
intervalHoursMultiply intervalHoursMultiply
SELECT 4 * -INTERVAL '2' HOURS AS result1, -5 * -INTERVAL '3' HOURS AS result2; SELECT 4 * -INTERVAL '2' HOURS AS result1, -5 * -INTERVAL '3' HOURS AS result2;
result1 | result2 result1 | result2
---------------+-------------- -------------+------------
-0 08:00:00.0 | +0 15:00:00.0 -0 08:00:00 | +0 15:00:00
; ;
intervalNullMath intervalNullMath
@ -206,11 +206,11 @@ SELECT languages, CAST (languages * INTERVAL '1 10:30' DAY TO MINUTES AS string)
languages | result languages | result
---------------+--------------------------------------------- ---------------+---------------------------------------------
2 | +2 21:00:00.0 2 | +2 21:00:00
5 | +7 04:30:00.0 5 | +7 04:30:00
4 | +5 18:00:00.0 4 | +5 18:00:00
5 | +7 04:30:00.0 5 | +7 04:30:00
1 | +1 10:30:00.0 1 | +1 10:30:00
; ;
dateMinusInterval dateMinusInterval

View File

@ -878,7 +878,7 @@ SELECT INTERVAL 1 DAY + INTERVAL 53 MINUTES AS result;
result result
--------------- ---------------
+1 00:53:00.0 +1 00:53:00
// end::dtIntervalPlusInterval // end::dtIntervalPlusInterval
; ;
@ -911,7 +911,7 @@ SELECT INTERVAL '1' DAY - INTERVAL '2' HOURS AS result;
result result
--------------- ---------------
+0 22:00:00.0 +0 22:00:00
// end::dtIntervalMinusInterval // end::dtIntervalMinusInterval
; ;

View File

@ -129,8 +129,14 @@ public final class StringUtils {
sb.append(":"); sb.append(":");
durationInSec = durationInSec % SECONDS_PER_MINUTE; durationInSec = durationInSec % SECONDS_PER_MINUTE;
sb.append(indent(durationInSec)); sb.append(indent(durationInSec));
long millis = TimeUnit.NANOSECONDS.toMillis(d.getNano());
if (millis > 0) {
sb.append("."); sb.append(".");
sb.append(TimeUnit.NANOSECONDS.toMillis(d.getNano())); while (millis % 10 == 0) {
millis /= 10;
}
sb.append(millis);
}
return sb.toString(); return sb.toString();
} }

View File

@ -341,6 +341,10 @@ public final class Intervals {
+ ": negative value [{}] not allowed (negate the entire interval instead)", + ": negative value [{}] not allowed (negate the entire interval instead)",
v); v);
} }
if (units.get(unitIndex) == TimeUnit.MILLISECOND && number.length() < 3) {
// normalize the number past DOT to millis
v *= number.length() < 2 ? 100 : 10;
}
values[unitIndex++] = v; values[unitIndex++] = v;
} catch (QlIllegalArgumentException siae) { } catch (QlIllegalArgumentException siae) {
throw new ParsingException(source, invalidIntervalMessage(string), siae.getMessage()); throw new ParsingException(source, invalidIntervalMessage(string), siae.getMessage());

View File

@ -84,7 +84,8 @@ public class IntervalsTests extends ESTestCase {
public void testSecondInterval() throws Exception { public void testSecondInterval() throws Exception {
int randomSeconds = randomNonNegativeInt(); int randomSeconds = randomNonNegativeInt();
int randomMillis = randomBoolean() ? (randomBoolean() ? 0 : 999) : randomInt(999); int randomMillis = randomBoolean() ? (randomBoolean() ? 0 : 999) : randomInt(999);
String value = format(Locale.ROOT, "%s%d.%d", sign, randomSeconds, randomMillis); String value = format(Locale.ROOT, "%s%d", sign, randomSeconds);
value += randomMillis > 0 ? format(Locale.ROOT, ".%03d", randomMillis) : "";
TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_SECOND); TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_SECOND);
assertEquals(maybeNegate(sign, Duration.ofSeconds(randomSeconds).plusMillis(randomMillis)), amount); assertEquals(maybeNegate(sign, Duration.ofSeconds(randomSeconds).plusMillis(randomMillis)), amount);
} }
@ -129,7 +130,7 @@ public class IntervalsTests extends ESTestCase {
boolean withMillis = randomBoolean(); boolean withMillis = randomBoolean();
int randomMilli = withMillis ? randomInt(999) : 0; int randomMilli = withMillis ? randomInt(999) : 0;
String millisString = withMillis ? "." + randomMilli : ""; String millisString = withMillis && randomMilli > 0 ? format(Locale.ROOT, ".%03d", randomMilli) : "";
String value = format(Locale.ROOT, "%s%d %d:%d:%d%s", sign, randomDay, randomHour, randomMinute, randomSecond, millisString); String value = format(Locale.ROOT, "%s%d %d:%d:%d%s", sign, randomDay, randomHour, randomMinute, randomSecond, millisString);
TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_DAY_TO_SECOND); TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_DAY_TO_SECOND);
@ -152,7 +153,7 @@ public class IntervalsTests extends ESTestCase {
boolean withMillis = randomBoolean(); boolean withMillis = randomBoolean();
int randomMilli = withMillis ? randomInt(999) : 0; int randomMilli = withMillis ? randomInt(999) : 0;
String millisString = withMillis ? "." + randomMilli : ""; String millisString = withMillis && randomMilli > 0 ? format(Locale.ROOT, ".%03d", randomMilli) : "";
String value = format(Locale.ROOT, "%s%d:%d:%d%s", sign, randomHour, randomMinute, randomSecond, millisString); String value = format(Locale.ROOT, "%s%d:%d:%d%s", sign, randomHour, randomMinute, randomSecond, millisString);
TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_HOUR_TO_SECOND); TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_HOUR_TO_SECOND);
@ -166,7 +167,7 @@ public class IntervalsTests extends ESTestCase {
boolean withMillis = randomBoolean(); boolean withMillis = randomBoolean();
int randomMilli = withMillis ? randomInt(999) : 0; int randomMilli = withMillis ? randomInt(999) : 0;
String millisString = withMillis ? "." + randomMilli : ""; String millisString = withMillis && randomMilli > 0 ? format(Locale.ROOT, ".%03d", randomMilli) : "";
String value = format(Locale.ROOT, "%s%d:%d%s", sign, randomMinute, randomSecond, millisString); String value = format(Locale.ROOT, "%s%d:%d%s", sign, randomMinute, randomSecond, millisString);
TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_MINUTE_TO_SECOND); TemporalAmount amount = parseInterval(EMPTY, value, INTERVAL_MINUTE_TO_SECOND);

View File

@ -159,7 +159,7 @@ public class ExpressionTests extends ESTestCase {
int randomSecond = randomInt(59); int randomSecond = randomInt(59);
int randomMilli = randomInt(999); int randomMilli = randomInt(999);
String value = format(Locale.ROOT, "INTERVAL '%d %d:%d:%d.%d' DAY TO SECOND", randomDay, randomHour, randomMinute, randomSecond, String value = format(Locale.ROOT, "INTERVAL '%d %d:%d:%d.%03d' DAY TO SECOND", randomDay, randomHour, randomMinute, randomSecond,
randomMilli); randomMilli);
assertEquals(Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond) assertEquals(Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond)
.plusMillis(randomMilli), intervalOf(value)); .plusMillis(randomMilli), intervalOf(value));
@ -172,7 +172,7 @@ public class ExpressionTests extends ESTestCase {
int randomSecond = randomInt(59); int randomSecond = randomInt(59);
int randomMilli = randomInt(999); int randomMilli = randomInt(999);
String value = format(Locale.ROOT, "INTERVAL -'%d %d:%d:%d.%d' DAY TO SECOND", randomDay, randomHour, randomMinute, randomSecond, String value = format(Locale.ROOT, "INTERVAL -'%d %d:%d:%d.%03d' DAY TO SECOND", randomDay, randomHour, randomMinute, randomSecond,
randomMilli); randomMilli);
assertEquals(Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond) assertEquals(Duration.ofDays(randomDay).plusHours(randomHour).plusMinutes(randomMinute).plusSeconds(randomSecond)
.plusMillis(randomMilli).negated(), intervalOf(value)); .plusMillis(randomMilli).negated(), intervalOf(value));