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 2023/04/01 23:55:28 UTC
[calcite] branch main updated: [CALCITE-5538] Allow creating TimestampString with fractional seconds ending in 0
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.git
The following commit(s) were added to refs/heads/main by this push:
new 2d0b3acb11 [CALCITE-5538] Allow creating TimestampString with fractional seconds ending in 0
2d0b3acb11 is described below
commit 2d0b3acb11169b307dc165c1fae8b7c92b888ae9
Author: Julian Hyde <jh...@apache.org>
AuthorDate: Sat Apr 1 11:58:55 2023 -0700
[CALCITE-5538] Allow creating TimestampString with fractional seconds ending in 0
Add unit test for TimestampString.
---
.../org/apache/calcite/util/TimestampString.java | 43 ++++++++++---
.../org/apache/calcite/rex/RexBuilderTest.java | 73 +++++++++++++++++++++-
2 files changed, 106 insertions(+), 10 deletions(-)
diff --git a/core/src/main/java/org/apache/calcite/util/TimestampString.java b/core/src/main/java/org/apache/calcite/util/TimestampString.java
index b6248351be..559df8805d 100644
--- a/core/src/main/java/org/apache/calcite/util/TimestampString.java
+++ b/core/src/main/java/org/apache/calcite/util/TimestampString.java
@@ -18,7 +18,6 @@ package org.apache.calcite.util;
import org.apache.calcite.avatica.util.DateTimeUtils;
-import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -26,6 +25,10 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Calendar;
import java.util.regex.Pattern;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import static org.apache.calcite.util.DateTimeStringUtils.ymdhms;
+
import static java.lang.Math.floorMod;
/**
@@ -40,6 +43,14 @@ public class TimestampString implements Comparable<TimestampString> {
+ " "
+ "[0-9][0-9]:[0-9][0-9]:[0-9][0-9](\\.[0-9]*[1-9])?");
+ /** The allowed format of input strings is slightly more flexible than
+ * normalized strings. Input strings can have trailing zeros in the fractional
+ * seconds. */
+ private static final Pattern INPUT_PATTERN =
+ Pattern.compile("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]"
+ + " "
+ + "[0-9][0-9]:[0-9][0-9]:[0-9][0-9](\\.[0-9]+)?");
+
/** The Unix epoch. */
public static final TimestampString EPOCH =
new TimestampString(1970, 1, 1, 0, 0, 0);
@@ -48,14 +59,13 @@ public class TimestampString implements Comparable<TimestampString> {
/** Creates a TimeString. */
public TimestampString(String v) {
- this.v = v;
- Preconditions.checkArgument(PATTERN.matcher(v).matches(), v);
+ this.v = normalize(v);
}
/** Creates a TimestampString for year, month, day, hour, minute, second,
* millisecond values. */
public TimestampString(int year, int month, int day, int h, int m, int s) {
- this(DateTimeStringUtils.ymdhms(new StringBuilder(), year, month, day, h, m, s).toString());
+ this(ymdhms(new StringBuilder(), year, month, day, h, m, s).toString());
}
/** Sets the fraction field of a {@code TimestampString} to a given number
@@ -65,7 +75,7 @@ public class TimestampString implements Comparable<TimestampString> {
* {@code new TimestampString(1970, 1, 1, 2, 3, 4).withMillis(56)}
* yields {@code TIMESTAMP '1970-01-01 02:03:04.056'}. */
public TimestampString withMillis(int millis) {
- Preconditions.checkArgument(millis >= 0 && millis < 1000);
+ checkArgument(millis >= 0 && millis < 1000);
return withFraction(DateTimeStringUtils.pad(3, millis));
}
@@ -76,7 +86,7 @@ public class TimestampString implements Comparable<TimestampString> {
* {@code new TimestampString(1970, 1, 1, 2, 3, 4).withNanos(56789)}
* yields {@code TIMESTAMP '1970-01-01 02:03:04.000056789'}. */
public TimestampString withNanos(int nanos) {
- Preconditions.checkArgument(nanos >= 0 && nanos < 1000000000);
+ checkArgument(nanos >= 0 && nanos < 1000000000);
return withFraction(DateTimeStringUtils.pad(9, nanos));
}
@@ -102,6 +112,19 @@ public class TimestampString implements Comparable<TimestampString> {
return new TimestampString(v);
}
+ private static String normalize(String v) {
+ checkArgument(INPUT_PATTERN.matcher(v).matches(), v);
+
+ // Remove trailing zeros in the fractional seconds
+ if (v.indexOf('.') >= 0) {
+ while (v.endsWith("0")) {
+ v = v.substring(0, v.length() - 1);
+ }
+ }
+ checkArgument(PATTERN.matcher(v).matches(), v);
+ return v;
+ }
+
@Override public String toString() {
return v;
}
@@ -133,8 +156,12 @@ public class TimestampString implements Comparable<TimestampString> {
.withMillis(calendar.get(Calendar.MILLISECOND));
}
+ /** Returns this value rounded to {@code precision} decimal digits after the
+ * point.
+ *
+ * <p>Uses rounding mode {@link java.math.RoundingMode#DOWN}. */
public TimestampString round(int precision) {
- Preconditions.checkArgument(precision >= 0);
+ checkArgument(precision >= 0);
int targetLength = 20 + precision;
if (v.length() <= targetLength) {
return this;
@@ -191,7 +218,7 @@ public class TimestampString implements Comparable<TimestampString> {
/** Converts this TimestampString to a string, truncated or padded with
* zeros to a given precision. */
public String toString(int precision) {
- Preconditions.checkArgument(precision >= 0);
+ checkArgument(precision >= 0);
final int p = precision();
if (precision < p) {
return round(precision).toString(precision);
diff --git a/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java b/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
index 8741147bda..41e52b185e 100644
--- a/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
+++ b/core/src/test/java/org/apache/calcite/rex/RexBuilderTest.java
@@ -200,6 +200,16 @@ class RexBuilderTest {
final RexLiteral literal4 = builder.makeLiteral(ts4, timestampType18);
assertThat(literal4.getValueAs(TimestampString.class).toString(),
is("1969-07-21 02:56:15.102"));
+ }
+
+ @Test void testTimestampString() {
+ final TimestampString ts = new TimestampString(1969, 7, 21, 2, 56, 15);
+ assertThat(ts.toString(), is("1969-07-21 02:56:15"));
+ assertThat(ts.round(1), is(ts));
+
+ // Now with milliseconds
+ final TimestampString ts2 = ts.withMillis(56);
+ assertThat(ts2.toString(), is("1969-07-21 02:56:15.056"));
// toString
assertThat(ts2.round(1).toString(), is("1969-07-21 02:56:15"));
@@ -216,8 +226,67 @@ class RexBuilderTest {
assertThat(ts2.round(0).toString(1), is("1969-07-21 02:56:15.0"));
assertThat(ts2.round(0).toString(2), is("1969-07-21 02:56:15.00"));
- assertThat(TimestampString.fromMillisSinceEpoch(1456513560123L).toString(),
- is("2016-02-26 19:06:00.123"));
+ // Now with milliseconds ending in zero (3 equivalent strings).
+ final TimestampString ts3 = ts.withMillis(10);
+ assertThat(ts3.toString(), is("1969-07-21 02:56:15.01"));
+
+ final TimestampString ts3b = new TimestampString("1969-07-21 02:56:15.01");
+ assertThat(ts3b.toString(), is("1969-07-21 02:56:15.01"));
+ assertThat(ts3b, is(ts3));
+
+ final TimestampString ts3c = new TimestampString("1969-07-21 02:56:15.010");
+ assertThat(ts3c.toString(), is("1969-07-21 02:56:15.01"));
+ assertThat(ts3c, is(ts3));
+
+ // Now with nanoseconds
+ final TimestampString ts4 = ts.withNanos(56);
+ assertThat(ts4.toString(), is("1969-07-21 02:56:15.000000056"));
+
+ // Check rounding; uses RoundingMode.DOWN
+ final TimestampString ts5 = ts.withNanos(2345670);
+ assertThat(ts5.toString(), is("1969-07-21 02:56:15.00234567"));
+ assertThat(ts5.round(0).toString(), is("1969-07-21 02:56:15"));
+ assertThat(ts5.round(1).toString(), is("1969-07-21 02:56:15"));
+ assertThat(ts5.round(2).toString(), is("1969-07-21 02:56:15"));
+ assertThat(ts5.round(3).toString(), is("1969-07-21 02:56:15.002"));
+ assertThat(ts5.round(4).toString(), is("1969-07-21 02:56:15.0023"));
+ assertThat(ts5.round(5).toString(), is("1969-07-21 02:56:15.00234"));
+ assertThat(ts5.round(6).toString(), is("1969-07-21 02:56:15.002345"));
+ assertThat(ts5.round(600).toString(), is("1969-07-21 02:56:15.00234567"));
+
+ // Now with a very long fraction
+ final TimestampString ts6 = ts.withFraction("102030405060708090102");
+ assertThat(ts6.toString(), is("1969-07-21 02:56:15.102030405060708090102"));
+
+ // From milliseconds
+ final TimestampString ts7 =
+ TimestampString.fromMillisSinceEpoch(1456513560123L);
+ assertThat(ts7.toString(), is("2016-02-26 19:06:00.123"));
+
+ final TimestampString ts8 =
+ TimestampString.fromMillisSinceEpoch(1456513560120L);
+ assertThat(ts8.toString(), is("2016-02-26 19:06:00.12"));
+
+ final TimestampString ts9 = ts8.withFraction("9876543210");
+ assertThat(ts9.toString(), is("2016-02-26 19:06:00.987654321"));
+
+ // TimestampString.toCalendar
+ final Calendar c = ts9.toCalendar();
+ assertThat(c.get(Calendar.ERA), is(1)); // CE
+ assertThat(c.get(Calendar.YEAR), is(2016));
+ assertThat(c.get(Calendar.MONTH), is(1)); // February
+ assertThat(c.get(Calendar.DATE), is(26));
+ assertThat(c.get(Calendar.HOUR_OF_DAY), is(19));
+ assertThat(c.get(Calendar.MINUTE), is(6));
+ assertThat(c.get(Calendar.SECOND), is(0));
+ assertThat(c.get(Calendar.MILLISECOND), is(987)); // RoundingMode.DOWN
+ assertThat(ts9.getMillisSinceEpoch(), is(c.getTimeInMillis()));
+
+ // TimestampString.fromCalendarFields
+ c.set(Calendar.YEAR, 1969);
+ final TimestampString ts10 = TimestampString.fromCalendarFields(c);
+ assertThat(ts10.toString(), is("1969-02-26 19:06:00.987"));
+ assertThat(ts10.getMillisSinceEpoch(), is(c.getTimeInMillis()));
}
private void checkTimestamp(RexLiteral literal) {