This is an automated email from the ASF dual-hosted git repository.
snuyanzin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git
The following commit(s) were added to refs/heads/master by this push:
new e462b09 [CALCITE-4837] FLOOR/CEIL for DECADE, CENTURY, MILLENIUM
return wrong results
e462b09 is described below
commit e462b09d4d8b6dd64a9a9c0562a6d11868751a06
Author: snuyanzin <[email protected]>
AuthorDate: Thu Oct 7 09:11:58 2021 +0200
[CALCITE-4837] FLOOR/CEIL for DECADE, CENTURY, MILLENIUM return wrong
results
Close apache/calcite-avatica#158
---
.../org/apache/calcite/avatica/util/DateTimeUtils.java | 12 ++++++++++++
.../apache/calcite/avatica/util/DateTimeUtilsTest.java | 18 ++++++++++++++++++
2 files changed, 30 insertions(+)
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 7e5e109..6f08114 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
@@ -921,6 +921,18 @@ public class DateTimeUtils {
int month = (m + 2) % 12 + 1;
int day = d + 1;
switch (range) {
+ case MILLENNIUM:
+ return floor
+ ? ymdToUnixDate(1000 * ((year + 999) / 1000) - 999, 1, 1)
+ : ymdToUnixDate(1000 * ((year + 999) / 1000) + 1, 1, 1);
+ case CENTURY:
+ return floor
+ ? ymdToUnixDate(100 * ((year + 99) / 100) - 99, 1, 1)
+ : ymdToUnixDate(100 * ((year + 99) / 100) + 1, 1, 1);
+ case DECADE:
+ return floor
+ ? ymdToUnixDate(10 * (year / 10), 1, 1)
+ : ymdToUnixDate(10 * (1 + year / 10), 1, 1);
case YEAR:
if (!floor && (month > 1 || day > 1)) {
++year;
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 6a33adf..b8d1453 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
@@ -911,7 +911,13 @@ public class DateTimeUtilsTest {
}
@Test public void testUnixDateFloorCeil() {
+ final long y1001 = ymdToUnixDate(1001, 1, 1);
+ final long y1801 = -(169 * 365 + 169 / 4 - 1);
+ final long y1890 = -(80 * 365 + 80 / 4 - 1);
final long y1900 = -(70 * 365 + 70 / 4);
+ final long y1910 = -(60 * 365 + 60 / 4);
+ final long y1907 = -(63 * 365 + 63 / 4);
+ final long y2001 = 31 * 365 + 31 / 4 + 1;
final long y1900_0102 = y1900 + 1;
final long y1899 = y1900 - 365;
final long y1901 = y1900 + 365;
@@ -926,10 +932,22 @@ public class DateTimeUtilsTest {
final long y1900_0701 = y1900 - 1 + 31 + 28 + 31 + 30 + 31 + 30 + 1;
final long y1900_1001 = y1900 - 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31
+ 30 + 1;
final long y1900_1002 = y1900 - 1 + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31
+ 30 + 2;
+ checkDateString("1801-01-01", (int) y1801);
checkDateString("1900-01-01", (int) y1900);
checkDateString("1900-01-02", (int) y1900_0102);
checkDateString("1899-01-01", (int) y1899);
checkDateString("1901-01-01", (int) y1901);
+ checkDateString("2001-01-01", (int) y2001);
+ assertThat(unixDateFloor(TimeUnitRange.MILLENNIUM, y1907), is(y1001));
+ assertThat(unixDateCeil(TimeUnitRange.MILLENNIUM, y1907), is(y2001));
+ assertThat(unixDateFloor(TimeUnitRange.CENTURY, y1899), is(y1801));
+ assertThat(unixDateCeil(TimeUnitRange.CENTURY, y1899), is(y1901));
+ assertThat(unixDateFloor(TimeUnitRange.DECADE, y1899), is(y1890));
+ assertThat(unixDateFloor(TimeUnitRange.DECADE, y1900_0701), is(y1900));
+ assertThat(unixDateFloor(TimeUnitRange.DECADE, y1907), is(y1900));
+ assertThat(unixDateCeil(TimeUnitRange.DECADE, y1899), is(y1900));
+ assertThat(unixDateCeil(TimeUnitRange.DECADE, y1900_0701), is(y1910));
+ assertThat(unixDateCeil(TimeUnitRange.DECADE, y1907), is(y1910));
assertThat(unixDateFloor(TimeUnitRange.YEAR, y1900_0102), is(y1900));
assertThat(unixDateCeil(TimeUnitRange.YEAR, y1900_0102), is(y1901));
assertThat(unixDateFloor(TimeUnitRange.QUARTER, y1900_0514),
is(y1900_0401));