calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [3/3] calcite git commit: [CALCITE-1639] TIMESTAMPADD(MONTH, ...) should return last day of month if the day overflows (Hongbin Ma)
Date Wed, 19 Apr 2017 19:18:14 GMT
[CALCITE-1639] TIMESTAMPADD(MONTH, ...) should return last day of month if the day overflows
(Hongbin Ma)

Close apache/calcite#431


Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/0d2d11f3
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/0d2d11f3
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/0d2d11f3

Branch: refs/heads/master
Commit: 0d2d11f3b8ab4a11279b4f73d0cd913902872880
Parents: cd13698
Author: Julian Hyde <jhyde@apache.org>
Authored: Tue Apr 18 13:40:16 2017 -0700
Committer: Julian Hyde <jhyde@apache.org>
Committed: Wed Apr 19 10:39:17 2017 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/calcite/runtime/SqlFunctions.java   | 6 +-----
 .../org/apache/calcite/sql/test/SqlOperatorBaseTest.java     | 8 ++++++++
 .../test/java/org/apache/calcite/test/SqlFunctionsTest.java  | 8 ++++----
 3 files changed, 13 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/0d2d11f3/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index baa2c6f..fec73c6 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -2089,11 +2089,7 @@ public class SqlFunctions {
     m0 += m - y * 12;
     int last = lastDay(y0, m0);
     if (d0 > last) {
-      d0 = 1;
-      if (++m0 > 12) {
-        m0 = 1;
-        ++y0;
-      }
+      d0 = last;
     }
     return DateTimeUtils.ymdToUnixDate(y0, m0, d0);
   }

http://git-wip-us.apache.org/repos/asf/calcite/blob/0d2d11f3/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
index 3e8cbd2..2ba92ac 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
@@ -5709,6 +5709,14 @@ public abstract class SqlOperatorBaseTest {
         null, "TIMESTAMP(0)");
     tester.checkScalar("timestampadd(DAY, 1, cast(null as date))",
         null, "DATE");
+
+    // Round to the last day of previous month
+    tester.checkScalar("timestampadd(MONTH, 1, date '2016-05-31')",
+        "2016-06-30", "DATE NOT NULL");
+    tester.checkScalar("timestampadd(MONTH, 5, date '2016-01-31')",
+        "2016-06-30", "DATE NOT NULL");
+    tester.checkScalar("timestampadd(MONTH, -1, date '2016-03-31')",
+        "2016-02-29", "DATE NOT NULL");
   }
 
   @Test public void testTimestampDiff() {

http://git-wip-us.apache.org/repos/asf/calcite/blob/0d2d11f3/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
index 12ef35b..4293654 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
@@ -365,10 +365,10 @@ public class SqlFunctionsTest {
     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);
   }


Mime
View raw message