This is an automated email from the ASF dual-hosted git repository. jhyde pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git
commit 72bbbfc964e7805e1b09bfdb47f5472d37050c39 Author: Julian Hyde <[email protected]> AuthorDate: Thu Oct 17 01:37:40 2019 -0700 [CALCITE-3412] FLOOR(timestamp TO WEEK) gives wrong result Fix DateTimeUtils.julianDateFloor so that unixDateFloor etc. give the right result. --- .../apache/calcite/avatica/util/DateTimeUtils.java | 24 ++++++++++++++++++ .../calcite/avatica/util/DateTimeUtilsTest.java | 29 ++++++++++++++++++++++ 2 files changed, 53 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 5efa210..6969f77 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 @@ -926,11 +926,35 @@ public class DateTimeUtils { ++year; } return ymdToUnixDate(year, 1, 1); + case QUARTER: + final int q = (month - 1) / 3; + if (!floor) { + if (month - 1 > q * 3 || day > 1) { + if (q == 3) { + ++year; + month = 1; + } else { + month = q * 3 + 4; + } + } + } else { + month = q * 3 + 1; + } + return ymdToUnixDate(year, month, 1); case MONTH: if (!floor && day > 1) { ++month; } return ymdToUnixDate(year, month, 1); + case WEEK: + final int dow = (int) floorMod(julian + 1, 7); // sun=0, sat=6 + int offset = dow; + if (!floor && offset > 0) { + offset -= 7; + } + return ymdToUnixDate(year, month, day) - offset; + case DAY: + return ymdToUnixDate(year, month, day); default: throw new AssertionError(range); } 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 271de2c..5043467 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 @@ -907,12 +907,41 @@ public class DateTimeUtilsTest { final long y1900_0102 = y1900 + 1; final long y1899 = y1900 - 365; final long y1901 = y1900 + 365; + final long y1900_0506 = y1900 - 1 + 31 + 28 + 31 + 30 + 6; // sunday + final long y1900_0512 = y1900 - 1 + 31 + 28 + 31 + 30 + 12; // saturday + final long y1900_0513 = y1900 - 1 + 31 + 28 + 31 + 30 + 13; // sunday + final long y1900_0514 = y1900 - 1 + 31 + 28 + 31 + 30 + 14; // monday + final long y1900_0520 = y1900 - 1 + 31 + 28 + 31 + 30 + 20; // sunday + final long y1900_0401 = y1900 - 1 + 31 + 28 + 31 + 1; + final long y1900_0501 = y1900 - 1 + 31 + 28 + 31 + 30 + 1; + final long y1900_0601 = y1900 - 1 + 31 + 28 + 31 + 30 + 31 + 1; + 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("1900-01-01", (int) y1900); checkDateString("1900-01-02", (int) y1900_0102); checkDateString("1899-01-01", (int) y1899); checkDateString("1901-01-01", (int) y1901); 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)); + assertThat(unixDateCeil(TimeUnitRange.QUARTER, y1900_0514), is(y1900_0701)); + assertThat(unixDateFloor(TimeUnitRange.QUARTER, y1900_1001), is(y1900_1001)); + assertThat(unixDateCeil(TimeUnitRange.QUARTER, y1900_1001), is(y1900_1001)); + assertThat(unixDateFloor(TimeUnitRange.QUARTER, y1900_1002), is(y1900_1001)); + assertThat(unixDateCeil(TimeUnitRange.QUARTER, y1900_1002), is(y1901)); + assertThat(unixDateFloor(TimeUnitRange.MONTH, y1900_0514), is(y1900_0501)); + assertThat(unixDateCeil(TimeUnitRange.MONTH, y1900_0514), is(y1900_0601)); + assertThat(unixDateFloor(TimeUnitRange.WEEK, y1900_0514), is(y1900_0513)); + assertThat(unixDateCeil(TimeUnitRange.WEEK, y1900_0514), is(y1900_0520)); + assertThat(unixDateFloor(TimeUnitRange.WEEK, y1900_0514), is(y1900_0513)); + assertThat(unixDateCeil(TimeUnitRange.WEEK, y1900_0514), is(y1900_0520)); + assertThat(unixDateFloor(TimeUnitRange.WEEK, y1900_0513), is(y1900_0513)); + assertThat(unixDateCeil(TimeUnitRange.WEEK, y1900_0513), is(y1900_0513)); + assertThat(unixDateFloor(TimeUnitRange.WEEK, y1900_0512), is(y1900_0506)); + assertThat(unixDateCeil(TimeUnitRange.WEEK, y1900_0512), is(y1900_0513)); + assertThat(unixDateFloor(TimeUnitRange.DAY, y1900_0514), is(y1900_0514)); + assertThat(unixDateCeil(TimeUnitRange.DAY, y1900_0514), is(y1900_0514)); } }
