From 1bd1a13b3b72a06d9cf6ae67c819cabe9d4ea2dd Mon Sep 17 00:00:00 2001 From: jaugsburger Date: Wed, 27 May 2020 17:45:49 +0100 Subject: [PATCH] fix(griffin) - microsecond timestamp (#335) --- .../AbstractIntervalDataFrameCursor.java | 6 +++++ .../std/microtime/TimestampFormatUtils.java | 6 ++++- .../java/io/questdb/griffin/InsertTest.java | 6 ++--- .../questdb/griffin/TimestampQueryTest.java | 22 ++++++++++++++++++- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java b/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java index 1d5cbdde3..6ab03fdd3 100644 --- a/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java +++ b/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java @@ -138,6 +138,12 @@ public abstract class AbstractIntervalDataFrameCursor implements DataFrameCursor this.initialIntervalsLo = intervalsLo / 2; int intervalsHi = intervals.binarySearch(reader.getMaxTimestamp()); + if (reader.getMaxTimestamp() == intervals.getQuick(intervals.size() - 1)) { + this.initialIntervalsHi = intervals.size() - 1; + } + if (reader.getMaxTimestamp() == intervals.getQuick(0)) { + this.initialIntervalsHi = 1; + } if (intervalsHi < 0) { intervalsHi = -intervalsHi - 1; // when interval index is "even" we scored just between two interval diff --git a/core/src/main/java/io/questdb/std/microtime/TimestampFormatUtils.java b/core/src/main/java/io/questdb/std/microtime/TimestampFormatUtils.java index d836c22c7..7b86afcb1 100644 --- a/core/src/main/java/io/questdb/std/microtime/TimestampFormatUtils.java +++ b/core/src/main/java/io/questdb/std/microtime/TimestampFormatUtils.java @@ -341,7 +341,11 @@ public class TimestampFormatUtils { } private static long parseDateTime(CharSequence seq, int lo, int lim) throws NumericException { - return UTC_FORMAT.parse(seq, lo, lim, enLocale); + if (lim - lo == 27) { + return USEC_UTC_FORMAT.parse(seq, lo, lim, enLocale); + } else { + return UTC_FORMAT.parse(seq, lo, lim, enLocale); + } } } diff --git a/core/src/test/java/io/questdb/griffin/InsertTest.java b/core/src/test/java/io/questdb/griffin/InsertTest.java index 3a11930c9..429b7b9e4 100644 --- a/core/src/test/java/io/questdb/griffin/InsertTest.java +++ b/core/src/test/java/io/questdb/griffin/InsertTest.java @@ -52,7 +52,7 @@ public class InsertTest extends AbstractGriffinTest { @Test public void testInsertAllByDay() throws Exception { testBindVariableInsert(PartitionBy.DAY, new TimestampFunction() { - private long last = TimestampFormatUtils.parseDateTime("2019-03-10T00:00:00.0000000"); + private long last = TimestampFormatUtils.parseDateTime("2019-03-10T00:00:00.000000Z"); @Override public long getTimestamp() { @@ -64,7 +64,7 @@ public class InsertTest extends AbstractGriffinTest { @Test public void testInsertAllByMonth() throws Exception { testBindVariableInsert(PartitionBy.MONTH, new TimestampFunction() { - private long last = TimestampFormatUtils.parseDateTime("2019-03-10T00:00:00.0000000"); + private long last = TimestampFormatUtils.parseDateTime("2019-03-10T00:00:00.000000Z"); @Override public long getTimestamp() { @@ -81,7 +81,7 @@ public class InsertTest extends AbstractGriffinTest { @Test public void testInsertAllByYear() throws Exception { testBindVariableInsert(PartitionBy.YEAR, new TimestampFunction() { - private long last = TimestampFormatUtils.parseDateTime("2019-03-10T00:00:00.0000000"); + private long last = TimestampFormatUtils.parseDateTime("2019-03-10T00:00:00.000000Z"); @Override public long getTimestamp() { diff --git a/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java b/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java index 64b169475..a4677073b 100644 --- a/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java +++ b/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java @@ -495,6 +495,26 @@ public class TimestampQueryTest extends AbstractGriffinTest { @Test public void testEqualsToTimestampFormatYearMonthDayHourMinuteSecond() throws Exception { + assertMemoryLeak(() -> { + //create table + String createStmt = "create table ob_mem_snapshot (symbol int, me_seq_num long, timestamp timestamp) timestamp(timestamp) partition by DAY"; + compiler.compile(createStmt, sqlExecutionContext); + //insert + executeInsert("INSERT INTO ob_mem_snapshot VALUES(1, 1, 1609459199000000)"); + String expected = "symbol\tme_seq_num\ttimestamp\n" + + "1\t1\t2020-12-31T23:59:59.000000Z\n"; + String query = "select * from ob_mem_snapshot"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + // test + expected = "symbol\tme_seq_num\ttimestamp\n" + + "1\t1\t2020-12-31T23:59:59.000000Z\n"; + query = "SELECT * FROM ob_mem_snapshot where timestamp = '2020-12-31T23:59:59'"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + }); + } + + @Test + public void testEqualsToTimestampWithMicrosecond() throws Exception { assertMemoryLeak(() -> { //create table String createStmt = "create table ob_mem_snapshot (symbol int, me_seq_num long, timestamp timestamp) timestamp(timestamp) partition by DAY"; @@ -508,7 +528,7 @@ public class TimestampQueryTest extends AbstractGriffinTest { // test expected = "symbol\tme_seq_num\ttimestamp\n" + "1\t1\t2020-12-31T23:59:59.000001Z\n"; - query = "SELECT * FROM ob_mem_snapshot where timestamp = '2020-12-31T23:59:59'"; + query = "SELECT * FROM ob_mem_snapshot where timestamp = '2020-12-31T23:59:59.000001Z'"; printSqlResult(expected, query, "timestamp", null, null, true, true); }); } -- GitLab