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 2022/10/20 20:14:28 UTC

[calcite-avatica] branch main updated (a111295d6 -> e2ba4a7c3)

This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/calcite-avatica.git


    from a111295d6 Prepare for next development iteration
     new e47b43ee8 [CALCITE-1639] TIMESTAMPADD(MONTH, ...) should return last day of month if the day overflows
     new e2ba4a7c3 [CALCITE-5338] In DateTimeUtils, deprecate floorMod and floorDiv, and use equivalents in java.lang.Math

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/calcite/avatica/remote/TypedValue.java  |  4 +-
 .../calcite/avatica/util/AbstractCursor.java       |  6 +--
 .../apache/calcite/avatica/util/DateTimeUtils.java | 57 +++++++++++-----------
 .../calcite/avatica/util/DateTimeUtilsTest.java    | 48 +++++++++---------
 4 files changed, 58 insertions(+), 57 deletions(-)


[calcite-avatica] 02/02: [CALCITE-5338] In DateTimeUtils, deprecate floorMod and floorDiv, and use equivalents in java.lang.Math

Posted by jh...@apache.org.
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

commit e2ba4a7c313dcfbfc002aac4f5e228a47954fa78
Author: Julian Hyde <jh...@apache.org>
AuthorDate: Wed Oct 19 13:59:47 2022 -0700

    [CALCITE-5338] In DateTimeUtils, deprecate floorMod and floorDiv, and use equivalents in java.lang.Math
    
    In Avatica's DateTimeUtils, replace floorDiv and floorMod
    with equivalents in java.lang.Math.
    
    The JDK equivalents were introduced in Java 8, probably have
    superior performance (due to intrinsics), and we can use
    them now that Avatica is JDK 8 and above.
    
    java.lang.Math.floorMod(long, int), which was introduced in
    JDK 9, is still off-limits.
---
 .../apache/calcite/avatica/remote/TypedValue.java  |  4 +-
 .../calcite/avatica/util/AbstractCursor.java       |  6 +--
 .../apache/calcite/avatica/util/DateTimeUtils.java | 51 ++++++++++++----------
 .../calcite/avatica/util/DateTimeUtilsTest.java    | 40 ++++++++---------
 4 files changed, 53 insertions(+), 48 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/avatica/remote/TypedValue.java b/core/src/main/java/org/apache/calcite/avatica/remote/TypedValue.java
index 29b7487db..969172fdf 100644
--- a/core/src/main/java/org/apache/calcite/avatica/remote/TypedValue.java
+++ b/core/src/main/java/org/apache/calcite/avatica/remote/TypedValue.java
@@ -406,9 +406,9 @@ public class TypedValue {
       }
       switch (rep) {
       case JAVA_SQL_DATE:
-        return (int) DateTimeUtils.floorDiv(t, DateTimeUtils.MILLIS_PER_DAY);
+        return (int) Math.floorDiv(t, DateTimeUtils.MILLIS_PER_DAY);
       case JAVA_SQL_TIME:
-        return (int) DateTimeUtils.floorMod(t, DateTimeUtils.MILLIS_PER_DAY);
+        return (int) Math.floorMod(t, DateTimeUtils.MILLIS_PER_DAY);
       default:
         return t;
       }
diff --git a/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java b/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java
index f05cbcc6d..b3f68020f 100644
--- a/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java
+++ b/core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java
@@ -1052,7 +1052,7 @@ public abstract class AbstractCursor implements Cursor {
         return null;
       }
       return new Time(
-          DateTimeUtils.floorMod(timestamp.getTime(),
+          Math.floorMod(timestamp.getTime(),
               DateTimeUtils.MILLIS_PER_DAY));
     }
 
@@ -1190,7 +1190,7 @@ public abstract class AbstractCursor implements Cursor {
         return null;
       }
       return new Time(
-          DateTimeUtils.floorMod(timestamp.getTime(),
+          Math.floorMod(timestamp.getTime(),
               DateTimeUtils.MILLIS_PER_DAY));
     }
 
@@ -1248,7 +1248,7 @@ public abstract class AbstractCursor implements Cursor {
         return null;
       }
       return new Time(
-          DateTimeUtils.floorMod(timestamp.getTime(),
+          Math.floorMod(timestamp.getTime(),
               DateTimeUtils.MILLIS_PER_DAY));
     }
 
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 a6bfe0bc1..5995d22dc 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
@@ -784,9 +784,9 @@ public class DateTimeUtils {
     case DAY:
       return day;
     case DOW:
-      return (int) floorMod(julian + 1, 7) + 1; // sun=1, sat=7
+      return Math.floorMod(julian + 1, 7) + 1; // sun=1, sat=7
     case ISODOW:
-      return (int) floorMod(julian, 7) + 1; // mon=1, sun=7
+      return Math.floorMod(julian, 7) + 1; // mon=1, sun=7
     case WEEK:
       return getIso8601WeekNumber(julian, year, month, day);
     case DOY:
@@ -813,7 +813,7 @@ public class DateTimeUtils {
    * Sometimes it is in the year before the given year. */
   private static long firstMondayOfFirstWeek(int year) {
     final long janFirst = ymdToJulian(year, 1, 1);
-    final long janFirstDow = floorMod(janFirst + 1, 7); // sun=0, sat=6
+    final long janFirstDow = Math.floorMod(janFirst + 1, 7L); // sun=0, sat=6
     return janFirst + (11 - janFirstDow) % 7 - 3;
   }
 
@@ -824,15 +824,15 @@ public class DateTimeUtils {
   private static int getIso8601WeekNumber(int julian, int year, int month, int day) {
     long fmofw = firstMondayOfFirstWeek(year);
     if (month == 12 && day > 28) {
-      if (31 - day + 4 > 7 - ((int) floorMod(julian, 7) + 1)
-          && 31 - day + (int) (floorMod(julian, 7) + 1) >= 4) {
+      if (31 - day + 4 > 7 - (Math.floorMod(julian, 7) + 1)
+          && 31 - day + Math.floorMod(julian, 7) + 1 >= 4) {
         return (int) (julian - fmofw) / 7 + 1;
       } else {
         return 1;
       }
     } else if (month == 1 && day < 5) {
-      if (4 - day <= 7 - ((int) floorMod(julian, 7) + 1)
-          && day - ((int) (floorMod(julian, 7) + 1)) >= -3) {
+      if (4 - day <= 7 - (Math.floorMod(julian, 7) + 1)
+          && day - (Math.floorMod(julian, 7) + 1) >= -3) {
         return 1;
       } else {
         return (int) (julian - firstMondayOfFirstWeek(year - 1)) / 7 + 1;
@@ -844,7 +844,8 @@ public class DateTimeUtils {
   /** Extracts a time unit from a UNIX date (milliseconds since epoch). */
   public static int unixTimestampExtract(TimeUnitRange range,
       long timestamp) {
-    return unixTimeExtract(range, (int) floorMod(timestamp, MILLIS_PER_DAY));
+    return unixTimeExtract(range,
+        (int) Math.floorMod(timestamp, MILLIS_PER_DAY));
   }
 
   /** Extracts a time unit from a time value (milliseconds since midnight). */
@@ -873,7 +874,7 @@ public class DateTimeUtils {
 
   /** Resets to epoch (1970-01-01) the "date" part of a timestamp. */
   public static long resetDate(long timestamp) {
-    return floorMod(timestamp, MILLIS_PER_DAY);
+    return Math.floorMod(timestamp, MILLIS_PER_DAY);
   }
 
   public static long unixTimestampFloor(TimeUnitRange range, long timestamp) {
@@ -959,7 +960,7 @@ public class DateTimeUtils {
       }
       return ymdToUnixDate(year, month, 1);
     case WEEK:
-      final int dow = (int) floorMod(julian + 1, 7); // sun=0, sat=6
+      final int dow = Math.floorMod(julian + 1, 7); // sun=0, sat=6
       int offset = dow;
       if (!floor && offset > 0) {
         offset -= 7;
@@ -1015,7 +1016,7 @@ public class DateTimeUtils {
    * of milliseconds since the epoch. */
   public static long addMonths(long timestamp, int m) {
     final long millis =
-        DateTimeUtils.floorMod(timestamp, DateTimeUtils.MILLIS_PER_DAY);
+        Math.floorMod(timestamp, DateTimeUtils.MILLIS_PER_DAY);
     timestamp -= millis;
     final long x =
         addMonths((int) (timestamp / DateTimeUtils.MILLIS_PER_DAY), m);
@@ -1029,9 +1030,9 @@ public class DateTimeUtils {
     int m0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.MONTH, date);
     int d0 = (int) DateTimeUtils.unixDateExtract(TimeUnitRange.DAY, date);
     m0 += m;
-    int deltaYear = (int) DateTimeUtils.floorDiv(m0, 12);
+    int deltaYear = Math.floorDiv(m0, 12);
     y0 += deltaYear;
-    m0 = (int) DateTimeUtils.floorMod(m0, 12);
+    m0 = Math.floorMod(m0, 12);
     if (m0 == 0) {
       y0 -= 1;
       m0 += 12;
@@ -1084,14 +1085,12 @@ public class DateTimeUtils {
   }
 
   public static int subtractMonths(long t0, long t1) {
-    final long millis0 =
-        DateTimeUtils.floorMod(t0, DateTimeUtils.MILLIS_PER_DAY);
-    final int d0 = (int) DateTimeUtils.floorDiv(t0 - millis0,
-        DateTimeUtils.MILLIS_PER_DAY);
-    final long millis1 =
-        DateTimeUtils.floorMod(t1, DateTimeUtils.MILLIS_PER_DAY);
-    final int d1 = (int) DateTimeUtils.floorDiv(t1 - millis1,
-        DateTimeUtils.MILLIS_PER_DAY);
+    final long millis0 = Math.floorMod(t0, DateTimeUtils.MILLIS_PER_DAY);
+    final int d0 =
+        (int) Math.floorDiv(t0 - millis0, DateTimeUtils.MILLIS_PER_DAY);
+    final long millis1 = Math.floorMod(t1, DateTimeUtils.MILLIS_PER_DAY);
+    final int d1 =
+        (int) Math.floorDiv(t1 - millis1, DateTimeUtils.MILLIS_PER_DAY);
     int x = subtractMonths(d0, d1);
     final long d2 = addMonths(d1, x);
     if (d2 == d0 && millis0 < millis1) {
@@ -1100,7 +1099,10 @@ public class DateTimeUtils {
     return x;
   }
 
-  /** Divide, rounding towards negative infinity. */
+  /** Divide, rounding towards negative infinity.
+   *
+   * @deprecated Use {@link Math#floorDiv(long, long)} */
+  @Deprecated // to be removed before 2.0
   public static long floorDiv(long x, long y) {
     long r = x / y;
     // if the signs are different and modulo not zero, round down
@@ -1110,7 +1112,10 @@ public class DateTimeUtils {
     return r;
   }
 
-  /** Modulo, always returning a non-negative result. */
+  /** Modulo, always returning a non-negative result.
+   *
+   * @deprecated Use {@link Math#floorMod(long, long)} */
+  @Deprecated // to be removed before 2.0
   public static long floorMod(long x, long y) {
     return x - floorDiv(x, y) * y;
   }
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 88066b5a9..bd8d79864 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
@@ -27,8 +27,6 @@ import static org.apache.calcite.avatica.util.DateTimeUtils.EPOCH_JULIAN;
 import static org.apache.calcite.avatica.util.DateTimeUtils.addMonths;
 import static org.apache.calcite.avatica.util.DateTimeUtils.dateStringToUnixDate;
 import static org.apache.calcite.avatica.util.DateTimeUtils.digitCount;
-import static org.apache.calcite.avatica.util.DateTimeUtils.floorDiv;
-import static org.apache.calcite.avatica.util.DateTimeUtils.floorMod;
 import static org.apache.calcite.avatica.util.DateTimeUtils.intervalDayTimeToString;
 import static org.apache.calcite.avatica.util.DateTimeUtils.intervalYearMonthToString;
 import static org.apache.calcite.avatica.util.DateTimeUtils.subtractMonths;
@@ -69,28 +67,30 @@ public class DateTimeUtilsTest {
     assertEquals(3, digitCount(100));
   }
 
+  @SuppressWarnings("deprecation")
   @Test public void testFloorDiv() {
-    assertThat(floorDiv(13, 3), equalTo(4L));
-    assertThat(floorDiv(12, 3), equalTo(4L));
-    assertThat(floorDiv(11, 3), equalTo(3L));
-    assertThat(floorDiv(-13, 3), equalTo(-5L));
-    assertThat(floorDiv(-12, 3), equalTo(-4L));
-    assertThat(floorDiv(-11, 3), equalTo(-4L));
-    assertThat(floorDiv(0, 3), equalTo(0L));
-    assertThat(floorDiv(1, 3), equalTo(0L));
-    assertThat(floorDiv(-1, 3), is(-1L));
+    assertThat(DateTimeUtils.floorDiv(13, 3), equalTo(4L));
+    assertThat(DateTimeUtils.floorDiv(12, 3), equalTo(4L));
+    assertThat(DateTimeUtils.floorDiv(11, 3), equalTo(3L));
+    assertThat(DateTimeUtils.floorDiv(-13, 3), equalTo(-5L));
+    assertThat(DateTimeUtils.floorDiv(-12, 3), equalTo(-4L));
+    assertThat(DateTimeUtils.floorDiv(-11, 3), equalTo(-4L));
+    assertThat(DateTimeUtils.floorDiv(0, 3), equalTo(0L));
+    assertThat(DateTimeUtils.floorDiv(1, 3), equalTo(0L));
+    assertThat(DateTimeUtils.floorDiv(-1, 3), is(-1L));
   }
 
+  @SuppressWarnings("deprecation")
   @Test public void testFloorMod() {
-    assertThat(floorMod(13, 3), is(1L));
-    assertThat(floorMod(12, 3), is(0L));
-    assertThat(floorMod(11, 3), is(2L));
-    assertThat(floorMod(-13, 3), is(2L));
-    assertThat(floorMod(-12, 3), is(0L));
-    assertThat(floorMod(-11, 3), is(1L));
-    assertThat(floorMod(0, 3), is(0L));
-    assertThat(floorMod(1, 3), is(1L));
-    assertThat(floorMod(-1, 3), is(2L));
+    assertThat(DateTimeUtils.floorMod(13, 3), is(1L));
+    assertThat(DateTimeUtils.floorMod(12, 3), is(0L));
+    assertThat(DateTimeUtils.floorMod(11, 3), is(2L));
+    assertThat(DateTimeUtils.floorMod(-13, 3), is(2L));
+    assertThat(DateTimeUtils.floorMod(-12, 3), is(0L));
+    assertThat(DateTimeUtils.floorMod(-11, 3), is(1L));
+    assertThat(DateTimeUtils.floorMod(0, 3), is(0L));
+    assertThat(DateTimeUtils.floorMod(1, 3), is(1L));
+    assertThat(DateTimeUtils.floorMod(-1, 3), is(2L));
   }
 
   @Test public void testTimeUnitRange() {


[calcite-avatica] 01/02: [CALCITE-1639] TIMESTAMPADD(MONTH, ...) should return last day of month if the day overflows

Posted by jh...@apache.org.
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

commit e47b43ee8c818cb2bf18649410cbd6b56f63fe8c
Author: Hongbin Ma <ma...@apache.org>
AuthorDate: Wed Oct 19 13:43:42 2022 -0700

    [CALCITE-1639] TIMESTAMPADD(MONTH, ...) should return last day of month if the day overflows
    
    (Copied from Calcite)
---
 .../main/java/org/apache/calcite/avatica/util/DateTimeUtils.java  | 6 +-----
 .../java/org/apache/calcite/avatica/util/DateTimeUtilsTest.java   | 8 ++++----
 2 files changed, 5 insertions(+), 9 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 6f08114d8..a6bfe0bc1 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
@@ -1039,11 +1039,7 @@ public class DateTimeUtils {
 
     int last = lastDay(y0, m0);
     if (d0 > last) {
-      d0 = 1;
-      if (++m0 > 12) {
-        m0 = 1;
-        ++y0;
-      }
+      d0 = last;
     }
     return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
   }
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 b8d1453c4..88066b5a9 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
@@ -814,10 +814,10 @@ public class DateTimeUtilsTest {
     checkAddMonths(2016, 1, 1, 2017, 2, 1, 13);
     checkAddMonths(2016, 1, 1, 2015, 1, 1, -12);
     checkAddMonths(2016, 1, 1, 2018, 10, 1, 33);
-    checkAddMonths(2016, 1, 31, 2016, 5, 1, 3); // roll up
-    checkAddMonths(2016, 4, 30, 2016, 7, 30, 3); // roll up
-    checkAddMonths(2016, 1, 31, 2016, 3, 1, 1);
-    checkAddMonths(2016, 3, 31, 2016, 3, 1, -1);
+    checkAddMonths(2016, 1, 31, 2016, 4, 30, 3);
+    checkAddMonths(2016, 4, 30, 2016, 7, 30, 3);
+    checkAddMonths(2016, 1, 31, 2016, 2, 29, 1);
+    checkAddMonths(2016, 3, 31, 2016, 2, 29, -1);
     checkAddMonths(2016, 3, 31, 2116, 3, 31, 1200);
     checkAddMonths(2016, 2, 28, 2116, 2, 28, 1200);
     checkAddMonths(2019, 9, 1, 2020, 3, 1, 6);