You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2023/05/10 20:59:44 UTC
[calcite-avatica] branch main updated: [CALCITE-5678] Validate date, time and timestamp literals per ISO-8601
This is an automated email from the ASF dual-hosted git repository.
jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git
The following commit(s) were added to refs/heads/main by this push:
new 05e07c151 [CALCITE-5678] Validate date, time and timestamp literals per ISO-8601
05e07c151 is described below
commit 05e07c151a47e5225b67084abb2e2b9fe161a30d
Author: zstan <st...@gmail.com>
AuthorDate: Thu Apr 27 13:17:55 2023 +0300
[CALCITE-5678] Validate date, time and timestamp literals per ISO-8601
Close apache/calcite-avatica#218
---
.../apache/calcite/avatica/util/DateTimeUtils.java | 99 +++++++++++++++++++-
.../calcite/avatica/util/DateTimeUtilsTest.java | 102 ++++++++++++++++++++-
.../util/TimestampFromNumberAccessorTest.java | 6 ++
3 files changed, 202 insertions(+), 5 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java b/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
index 144d024ce..3c988a140 100644
--- a/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
+++ b/core/src/main/java/org/apache/calcite/avatica/util/DateTimeUtils.java
@@ -30,6 +30,8 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
/**
* Utility functions for datetime types: date, time, timestamp.
@@ -57,6 +59,14 @@ public class DateTimeUtils {
public static final String TIMESTAMP_FORMAT_STRING =
DATE_FORMAT_STRING + " " + TIME_FORMAT_STRING;
+ /** Regex for date, YYYY-MM-DD. */
+ private static final Pattern ISO_DATE_PATTERN =
+ Pattern.compile("^(\\d{4})-([0]\\d|1[0-2])-([0-2]\\d|3[01])$");
+
+ /** Regex for time, HH:MM:SS. */
+ private static final Pattern ISO_TIME_PATTERN =
+ Pattern.compile("^([0-2]\\d):[0-5]\\d:[0-5]\\d(\\.\\d*)*$");
+
/** The GMT time zone.
*
* @deprecated Use {@link #UTC_ZONE} */
@@ -729,15 +739,100 @@ public class DateTimeUtils {
return r;
}
+ private static void validateDate(String s, String full) {
+ Matcher matcher = ISO_DATE_PATTERN.matcher(s);
+ if (matcher.find()) {
+ int year = Integer.parseInt(matcher.group(1));
+ int month = Integer.parseInt(matcher.group(2));
+ int day = Integer.parseInt(matcher.group(3));
+ if (day > daysInMonth(year, month)) {
+ throw fieldOutOfRange("DAY", full);
+ }
+ } else {
+ throw invalidType("DATE", full);
+ }
+ }
+
+ /** Returns the number of days in a month in the proleptic Gregorian calendar
+ * used by ISO-8601.
+ *
+ * <p>"Proleptic" means that we apply the calendar to dates before the
+ * Gregorian calendar was invented (in 1582). Thus, years 0 and 1200 are
+ * considered leap years, and 1500 is not. */
+ private static int daysInMonth(int year, int month) {
+ switch (month) {
+ case 9:
+ case 4:
+ case 6:
+ case 11:
+ // Thirty days hath September,
+ // April, June, and November,
+ return 30;
+
+ default:
+ // All the rest have thirty-one,
+ return 31;
+
+ case 2:
+ // Except February, twenty-eight days clear,
+ // And twenty-nine in each leap year.
+ return isLeapYear(year) ? 29 : 28;
+ }
+ }
+
+ /** Whether a year is considered a leap year in the proleptic Gregorian
+ * calendar. */
+ private static boolean isLeapYear(int year) {
+ return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
+ }
+
+ private static void validateTime(String time, String full) {
+ Matcher matcher = ISO_TIME_PATTERN.matcher(time);
+ if (matcher.find()) {
+ int hour = Integer.parseInt(matcher.group(1));
+ if (hour > 23) {
+ throw fieldOutOfRange("HOUR", full);
+ }
+ } else {
+ throw invalidType("TIME", full);
+ }
+ }
+
+ private static IllegalArgumentException fieldOutOfRange(String field,
+ String full) {
+ return new IllegalArgumentException("Value of " + field
+ + " field is out of range in '" + full + "'");
+ }
+
+ private static IllegalArgumentException invalidType(String type,
+ String full) {
+ return new IllegalArgumentException("Invalid " + type + " value, '"
+ + full + "'");
+ }
+
public static long timestampStringToUnixDate(String s) {
+ try {
+ return timestampStringToUnixDate0(s);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(e.getMessage());
+ }
+ }
+
+ private static long timestampStringToUnixDate0(String s) {
final long d;
final long t;
s = s.trim();
int space = s.indexOf(' ');
if (space >= 0) {
- d = dateStringToUnixDate(s.substring(0, space));
- t = timeStringToUnixDate(s, space + 1);
+ String datePart = s.substring(0, space);
+ validateDate(datePart, s);
+ d = dateStringToUnixDate(datePart);
+
+ String timePart = s.substring(space + 1);
+ validateTime(timePart, s);
+ t = timeStringToUnixDate(timePart);
} else {
+ validateDate(s, s);
d = dateStringToUnixDate(s);
t = 0;
}
diff --git a/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java b/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java
index 7b03a64ac..65b77e00d 100644
--- a/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java
+++ b/core/src/test/java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.calcite.avatica.util;
+import org.hamcrest.Matcher;
import org.junit.Test;
import java.math.BigDecimal;
@@ -1574,15 +1575,18 @@ public class DateTimeUtilsTest {
* proleptic Gregorian calendar used by unix timestamps.
*/
@Test public void testUnixTimestampToSqlTimestampWithGregorianShift() {
- assertThat(unixTimestampToSqlTimestamp(
+ assertThat(
+ unixTimestampToSqlTimestamp(
timestampStringToUnixDate("1582-10-04 00:00:00"),
CALENDAR),
is(Timestamp.valueOf("1582-10-04 00:00:00.0")));
- assertThat(unixTimestampToSqlTimestamp(
+ assertThat(
+ unixTimestampToSqlTimestamp(
timestampStringToUnixDate("1582-10-05 00:00:00"),
CALENDAR),
is(Timestamp.valueOf("1582-10-15 00:00:00.0")));
- assertThat(unixTimestampToSqlTimestamp(
+ assertThat(
+ unixTimestampToSqlTimestamp(
timestampStringToUnixDate("1582-10-15 00:00:00"),
CALENDAR),
is(Timestamp.valueOf("1582-10-15 00:00:00.0")));
@@ -1605,6 +1609,98 @@ public class DateTimeUtilsTest {
unixTimestampToSqlTimestamp(timestampStringToUnixDate("9999-12-31 00:00:00"), CALENDAR),
is(Timestamp.valueOf("9999-12-31 00:00:00")));
}
+
+ /** Until we upgrade to Junit5. */
+ private static void assertThrows(Runnable runnable,
+ Class<? extends Throwable> throwableClass,
+ Matcher<String> messageMatcher) {
+ try {
+ runnable.run();
+ throw new AssertionError("Exception not raised");
+ } catch (Throwable e) {
+ assertThat(throwableClass.isInstance(e), is(true));
+ assertThat(e.getMessage(), messageMatcher);
+ }
+ }
+
+ /**
+ * Test exception is raised if date in inappropriate meaning.
+ */
+ @Test public void testBrokenDate() {
+ // 2023 is not a leap year
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-02-29 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Value of DAY field is out of range in '2023-02-29 12:00:00.123'"));
+
+ // 2000 is a leap year
+ long x = timestampStringToUnixDate("2000-02-29 12:00:00.123");
+ assertThat(x, is(951825600123L));
+
+ // 1900 is not a leap year
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("1900-02-29 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Value of DAY field is out of range in '1900-02-29 12:00:00.123'"));
+
+ // 2100 is not a leap year
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2100-02-29 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Value of DAY field is out of range in '2100-02-29 12:00:00.123'"));
+
+ // 1600 is a leap year
+ long x2 = timestampStringToUnixDate("1600-02-29 12:00:00.123");
+ assertThat(x2, is(-11670955199877L));
+
+ // 1500 is not a leap year
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("1500-02-29 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Value of DAY field is out of range in '1500-02-29 12:00:00.123'"));
+
+ // April has only 30 days
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-04-31 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Value of DAY field is out of range in '2023-04-31 12:00:00.123'"));
+
+ // Month 13 is invalid
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-13-29 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Invalid DATE value, '2023-13-29 12:00:00.123'"));
+
+ // Month -3 is invalid
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023--3-29 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Invalid DATE value, '2023--3-29 12:00:00.123'"));
+
+ // '123-1' is an invalid fractional second
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-02-27 12:00:00.123-1"),
+ IllegalArgumentException.class,
+ is("Invalid TIME value, '2023-02-27 12:00:00.123-1'"));
+
+ // Invalid day 270
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-02-270 12:00:00.123"),
+ IllegalArgumentException.class,
+ is("Invalid DATE value, '2023-02-270 12:00:00.123'"));
+
+ // Invalid fractional seconds '123-1'
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-02-27 12:00:00.123-1"),
+ IllegalArgumentException.class,
+ is("Invalid TIME value, '2023-02-27 12:00:00.123-1'"));
+
+ // Hour 24 is invalid
+ assertThrows(() ->
+ DateTimeUtils.timestampStringToUnixDate("2023-02-28 24:00:00.123"),
+ IllegalArgumentException.class,
+ is("Value of HOUR field is out of range in '2023-02-28 24:00:00.123'"));
+ }
}
// End DateTimeUtilsTest.java
diff --git a/core/src/test/java/org/apache/calcite/avatica/util/TimestampFromNumberAccessorTest.java b/core/src/test/java/org/apache/calcite/avatica/util/TimestampFromNumberAccessorTest.java
index f4c06aa93..0b548b940 100644
--- a/core/src/test/java/org/apache/calcite/avatica/util/TimestampFromNumberAccessorTest.java
+++ b/core/src/test/java/org/apache/calcite/avatica/util/TimestampFromNumberAccessorTest.java
@@ -103,6 +103,12 @@ public class TimestampFromNumberAccessorTest {
value = DateTimeUtils.timestampStringToUnixDate("1500-04-30 12:00:00.123");
assertThat(instance.getString(), is("1500-04-30 12:00:00"));
+
+ value = DateTimeUtils.timestampStringToUnixDate("2023-09-30 15:28:27.6789012345");
+ assertThat(instance.getString(), is("2023-09-30 15:28:27"));
+
+ value = DateTimeUtils.timestampStringToUnixDate("2023-09-30 15:28:27.");
+ assertThat(instance.getString(), is("2023-09-30 15:28:27"));
}
/**