alex-plekhanov commented on code in PR #11822:
URL: https://github.com/apache/ignite/pull/11822#discussion_r1973068820


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DateTimeTest.java:
##########
@@ -202,6 +203,67 @@ public void testDstShift() throws Exception {
         }
     }
 
+    /** */
+    @Test
+    public void testOldDateLiterals() {
+        assertQuery("SELECT DATE 
'1582-10-20'").returns(java.sql.Date.valueOf("1582-10-20")).check();
+        assertQuery("SELECT DATE 
'1582-10-15'").returns(java.sql.Date.valueOf("1582-10-15")).check();
+        assertQuery("SELECT DATE 
'1582-10-01'").returns(java.sql.Date.valueOf("1582-10-01")).check();
+        assertQuery("SELECT DATE 
'1582-09-30'").returns(java.sql.Date.valueOf("1582-09-30")).check();
+        assertQuery("SELECT DATE 
'1000-01-01'").returns(java.sql.Date.valueOf("1000-01-01")).check();
+        assertQuery("SELECT DATE 
'0550-05-05'").returns(java.sql.Date.valueOf("0550-05-05")).check();
+
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1582-10-20")).withParams(java.sql.Date.valueOf("1582-10-20")).check();
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1582-10-01")).withParams(java.sql.Date.valueOf("1582-10-01")).check();
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1000-01-01")).withParams(java.sql.Date.valueOf("1000-01-01")).check();
+
+        assertQuery("SELECT TIMESTAMP '1582-10-20 
17:12:47.111'").returns(Timestamp.valueOf("1582-10-20 17:12:47.111"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-10-15 
00:00:00.001'").returns(Timestamp.valueOf("1582-10-15 00:00:00.001"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-10-01 
01:01:15.555'").returns(Timestamp.valueOf("1582-10-01 01:01:15.555"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-09-30 
23:23:59.999'").returns(Timestamp.valueOf("1582-09-30 23:23:59.999"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1000-01-01 
23:23:59.999'").returns(Timestamp.valueOf("1000-01-01 23:23:59.999"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '0550-05-05 
04:04:31.015'").returns(Timestamp.valueOf("0550-05-05 04:04:31.015"))
+            .check();
+
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("1582-10-20 
17:12:47.111"))
+            .returns(Timestamp.valueOf("1582-10-20 17:12:47.111")).check();
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("1582-10-15 
00:00:00.001"))
+            .returns(Timestamp.valueOf("1582-10-15 00:00:00.001")).check();
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("0550-05-05 
04:04:31.015"))
+            .returns(Timestamp.valueOf("0550-05-05 04:04:31.015")).check();
+    }
+
+    /** */
+    @Test
+    public void testOldDateLiteralsWithTable() {
+        assumeTrue(sqlTxMode == SqlTransactionMode.NONE);

Review Comment:
   Use already created `datetimetable` to avoid hang on table creation for 
transactional modes. Something like:
   ```
           sql(client, "INSERT INTO datetimetable (ID, SQLDATE, SQLTIMESTAMP) 
VALUES(?, ?, ? )", 10, sqlDate("1582-10-04"), sqlTimestamp("1582-10-04 
15:31:47.381"));
           sql(client, "INSERT INTO datetimetable (ID, SQLDATE, SQLTIMESTAMP) 
VALUES(11, DATE '1582-10-04', TIMESTAMP '1582-10-04 15:31:47.381')");
   ```



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DateTimeTest.java:
##########
@@ -202,6 +203,67 @@ public void testDstShift() throws Exception {
         }
     }
 
+    /** */
+    @Test
+    public void testOldDateLiterals() {
+        assertQuery("SELECT DATE 
'1582-10-20'").returns(java.sql.Date.valueOf("1582-10-20")).check();
+        assertQuery("SELECT DATE 
'1582-10-15'").returns(java.sql.Date.valueOf("1582-10-15")).check();
+        assertQuery("SELECT DATE 
'1582-10-01'").returns(java.sql.Date.valueOf("1582-10-01")).check();
+        assertQuery("SELECT DATE 
'1582-09-30'").returns(java.sql.Date.valueOf("1582-09-30")).check();
+        assertQuery("SELECT DATE 
'1000-01-01'").returns(java.sql.Date.valueOf("1000-01-01")).check();
+        assertQuery("SELECT DATE 
'0550-05-05'").returns(java.sql.Date.valueOf("0550-05-05")).check();
+
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1582-10-20")).withParams(java.sql.Date.valueOf("1582-10-20")).check();
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1582-10-01")).withParams(java.sql.Date.valueOf("1582-10-01")).check();
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1000-01-01")).withParams(java.sql.Date.valueOf("1000-01-01")).check();
+
+        assertQuery("SELECT TIMESTAMP '1582-10-20 
17:12:47.111'").returns(Timestamp.valueOf("1582-10-20 17:12:47.111"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-10-15 
00:00:00.001'").returns(Timestamp.valueOf("1582-10-15 00:00:00.001"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-10-01 
01:01:15.555'").returns(Timestamp.valueOf("1582-10-01 01:01:15.555"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-09-30 
23:23:59.999'").returns(Timestamp.valueOf("1582-09-30 23:23:59.999"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1000-01-01 
23:23:59.999'").returns(Timestamp.valueOf("1000-01-01 23:23:59.999"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '0550-05-05 
04:04:31.015'").returns(Timestamp.valueOf("0550-05-05 04:04:31.015"))
+            .check();
+
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("1582-10-20 
17:12:47.111"))
+            .returns(Timestamp.valueOf("1582-10-20 17:12:47.111")).check();
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("1582-10-15 
00:00:00.001"))
+            .returns(Timestamp.valueOf("1582-10-15 00:00:00.001")).check();
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("0550-05-05 
04:04:31.015"))
+            .returns(Timestamp.valueOf("0550-05-05 04:04:31.015")).check();

Review Comment:
   `java.sql.Date.valueOf` -> `sqlDate`
   `Timestamp.valueOf(` -> `sqlTimestamp`



##########
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/exp/IgniteRexBuilder.java:
##########
@@ -62,6 +74,97 @@ public IgniteRexBuilder(RelDataTypeFactory typeFactory) {
                 return super.makeLiteral(bd.setScale(type.getScale(), 
RoundingMode.HALF_UP), type, typeName);
         }
 
+        // Adjust temporal literals so that dates before 15.10.1582 would 
match internal long representation.
+        // `RexLiteral#getValueAs(Class type)` uses 
`DateTimeUtils#ymdToUnixDate(int year, int month, int day)` directly
+        // calculating epoch days. We use `new Date(long)` and `new 
Timestampt(long)` in `TypeUtils#fromInternal(...)` and
+        // and `Date#getTime()` in `TypeUtils#toInternal(...)`. Classic 
temporal types process the long value differently
+        // for dates before Gregorian calendar. `DateTimeUtils` does not. This 
may cause +several weeks for old date literals.
+        if (o instanceof DateString)

Review Comment:
   Only literals are affected by this problem? Maybe TypeUtils.toInternal - is 
the right place to fix it?
   Also, we have literal convertation for DEFAULT table column value (see 
TypeUtils.fromLiteral)



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/DateTimeTest.java:
##########
@@ -202,6 +203,67 @@ public void testDstShift() throws Exception {
         }
     }
 
+    /** */
+    @Test
+    public void testOldDateLiterals() {
+        assertQuery("SELECT DATE 
'1582-10-20'").returns(java.sql.Date.valueOf("1582-10-20")).check();
+        assertQuery("SELECT DATE 
'1582-10-15'").returns(java.sql.Date.valueOf("1582-10-15")).check();
+        assertQuery("SELECT DATE 
'1582-10-01'").returns(java.sql.Date.valueOf("1582-10-01")).check();
+        assertQuery("SELECT DATE 
'1582-09-30'").returns(java.sql.Date.valueOf("1582-09-30")).check();
+        assertQuery("SELECT DATE 
'1000-01-01'").returns(java.sql.Date.valueOf("1000-01-01")).check();
+        assertQuery("SELECT DATE 
'0550-05-05'").returns(java.sql.Date.valueOf("0550-05-05")).check();
+
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1582-10-20")).withParams(java.sql.Date.valueOf("1582-10-20")).check();
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1582-10-01")).withParams(java.sql.Date.valueOf("1582-10-01")).check();
+        assertQuery("SELECT 
?").returns(java.sql.Date.valueOf("1000-01-01")).withParams(java.sql.Date.valueOf("1000-01-01")).check();
+
+        assertQuery("SELECT TIMESTAMP '1582-10-20 
17:12:47.111'").returns(Timestamp.valueOf("1582-10-20 17:12:47.111"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-10-15 
00:00:00.001'").returns(Timestamp.valueOf("1582-10-15 00:00:00.001"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-10-01 
01:01:15.555'").returns(Timestamp.valueOf("1582-10-01 01:01:15.555"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1582-09-30 
23:23:59.999'").returns(Timestamp.valueOf("1582-09-30 23:23:59.999"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '1000-01-01 
23:23:59.999'").returns(Timestamp.valueOf("1000-01-01 23:23:59.999"))
+            .check();
+        assertQuery("SELECT TIMESTAMP '0550-05-05 
04:04:31.015'").returns(Timestamp.valueOf("0550-05-05 04:04:31.015"))
+            .check();
+
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("1582-10-20 
17:12:47.111"))
+            .returns(Timestamp.valueOf("1582-10-20 17:12:47.111")).check();
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("1582-10-15 
00:00:00.001"))
+            .returns(Timestamp.valueOf("1582-10-15 00:00:00.001")).check();
+        assertQuery("SELECT ?").withParams(Timestamp.valueOf("0550-05-05 
04:04:31.015"))
+            .returns(Timestamp.valueOf("0550-05-05 04:04:31.015")).check();
+    }
+
+    /** */
+    @Test
+    public void testOldDateLiteralsWithTable() {
+        assumeTrue(sqlTxMode == SqlTransactionMode.NONE);
+
+        sql(client, "CREATE TABLE T1(ID INTEGER PRIMARY KEY, DT DATE, TS 
TIMESTAMP)");
+
+        sql(client, "INSERT INTO T1 VALUES(?, ?, ? )", 1, 
java.sql.Date.valueOf("1582-10-04"),
+            java.sql.Timestamp.valueOf("1582-10-04 15:31:47.381"));
+        sql(client, "INSERT INTO T1 VALUES(2, DATE '1582-10-04', TIMESTAMP 
'1582-10-04 15:31:47.381')");
+
+        assertQuery("SELECT DT from T1 WHERE 
ID=1").returns(java.sql.Date.valueOf("1582-10-04")).check();
+        assertQuery("SELECT DT from T1 WHERE 
ID=2").returns(java.sql.Date.valueOf("1582-10-04")).check();
+        assertQuery("SELECT DT + INTERVAL 1 DAYS from T1 WHERE 
ID=1").returns(java.sql.Date.valueOf("1582-10-05"))

Review Comment:
   Is it correct? 1582-10-04 was followed by 1582-10-15, so there is not such 
dates as 1582-10-05.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to