This is an automated email from the ASF dual-hosted git repository.
tanner pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new 72e689e22c [CALCITE-6703] RelJson cannot handle timestamps prior to
1970-01-25 20:31:23.648
72e689e22c is described below
commit 72e689e22c288789c322e498f0eacd45bebb375a
Author: Tanner Clary <[email protected]>
AuthorDate: Fri Nov 22 10:50:28 2024 -0800
[CALCITE-6703] RelJson cannot handle timestamps prior to 1970-01-25
20:31:23.648
---
.../apache/calcite/rel/externalize/RelJson.java | 9 +++-
.../org/apache/calcite/plan/RelWriterTest.java | 55 ++++++++++++++++++++++
2 files changed, 63 insertions(+), 1 deletion(-)
diff --git a/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
b/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
index 41262c065b..c7539a427b 100644
--- a/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
+++ b/core/src/main/java/org/apache/calcite/rel/externalize/RelJson.java
@@ -830,9 +830,16 @@ public class RelJson {
Sarg sarg = sargFromJson((Map) literal, type);
return rexBuilder.makeSearchArgumentLiteral(sarg, type);
}
- if (type.getSqlTypeName() == SqlTypeName.SYMBOL) {
+ SqlTypeName sqlTypeName = type.getSqlTypeName();
+ if (sqlTypeName == SqlTypeName.SYMBOL) {
literal = RelEnumTypes.toEnum((String) literal);
}
+ if (sqlTypeName == SqlTypeName.TIMESTAMP
+ || sqlTypeName == SqlTypeName.TIMESTAMP_WITH_LOCAL_TIME_ZONE) {
+ if (literal instanceof Integer) {
+ literal = ((Integer) literal).longValue();
+ }
+ }
return rexBuilder.makeLiteral(literal, type);
}
if (map.containsKey("sargLiteral")) {
diff --git a/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
b/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
index 501b6c0bde..32252f8964 100644
--- a/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
+++ b/core/src/test/java/org/apache/calcite/plan/RelWriterTest.java
@@ -740,6 +740,61 @@ class RelWriterTest {
.assertThatPlan(isLinux(expected));
}
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6703">[CALCITE-6703]
+ * RelJson cannot handle timestamps prior to 1970-01-25 20:31:23.648</a>. */
+ @Test void testJsonToRexForTimestamp() {
+ // Below Integer.MAX_VALUE
+ final String timestampRepresentedAsInt = "{\n"
+ + " \"literal\": 2129400000,\n"
+ + " \"type\": {\n"
+ + " \"type\": \"TIMESTAMP\",\n"
+ + " \"nullable\": false\n"
+ + " }\n"
+ + "}\n";
+ // Above Integer.MAX_VALUE
+ final String timestampRepresentedAsLong = "{\n"
+ + " \"literal\": 3129400000,\n"
+ + " \"type\": {\n"
+ + " \"type\": \"TIMESTAMP\",\n"
+ + " \"nullable\": false\n"
+ + " }\n"
+ + "}\n";
+
+ // These timestamps were verified using BigQuery's UNIX_MILLIS function.
+ assertThatReadExpressionResult(timestampRepresentedAsInt, is("1970-01-25
15:30:00"));
+ assertThatReadExpressionResult(timestampRepresentedAsLong, is("1970-02-06
05:16:40"));
+ }
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-6703">[CALCITE-6703]
+ * RelJson cannot handle timestamps prior to 1970-01-25 20:31:23.648</a>. */
+ @Test void testJsonToRexForTimestampWithLocalTimeZone() {
+ // Below Integer.MAX_VALUE
+ final String timestampWithLocalTzRepresentedAsInt = "{\n"
+ + " \"literal\": 2129400000,\n"
+ + " \"type\": {\n"
+ + " \"type\": \"TIMESTAMP_WITH_LOCAL_TIME_ZONE\",\n"
+ + " \"nullable\": false\n"
+ + " }\n"
+ + "}\n";
+ // Above Integer.MAX_VALUE
+ final String timestampWithLocalTzRepresentedAsLong = "{\n"
+ + " \"literal\": 3129400000,\n"
+ + " \"type\": {\n"
+ + " \"type\": \"TIMESTAMP_WITH_LOCAL_TIME_ZONE\",\n"
+ + " \"nullable\": false\n"
+ + " }\n"
+ + "}\n";
+
+ // These timestamps were verified using BigQuery's UNIX_MILLIS function.
+ assertThatReadExpressionResult(timestampWithLocalTzRepresentedAsInt,
+ is("1970-01-25 15:30:00:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)"));
+ assertThatReadExpressionResult(timestampWithLocalTzRepresentedAsLong,
+ is("1970-02-06 05:16:40:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0)"));
+ }
+
+
@Test void testJsonToRex() {
// Test simple literal without inputs
final String jsonString1 = "{\n"