From b8e46bcb3324a7f6788cf39f54d08cb2d07a2493 Mon Sep 17 00:00:00 2001 From: jaugsburger Date: Tue, 26 May 2020 21:42:13 +0100 Subject: [PATCH] fix(griffin) - timestamp search issue with partial date. Equality fix #323 (#331) --- .../AbstractIntervalDataFrameCursor.java | 2 +- .../questdb/griffin/TimestampQueryTest.java | 125 ++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/io/questdb/griffin/TimestampQueryTest.java diff --git a/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java b/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java index 0e70546b6..1d5cbdde3 100644 --- a/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java +++ b/core/src/main/java/io/questdb/cairo/AbstractIntervalDataFrameCursor.java @@ -158,7 +158,7 @@ public abstract class AbstractIntervalDataFrameCursor implements DataFrameCursor } else { intervalLo = reader.floorToPartitionTimestamp(lo); } - this.initialPartitionLo = reader.getPartitionCountBetweenTimestamps(reader.getMinTimestamp(), intervalLo); + this.initialPartitionLo = reader.getMinTimestamp() < intervalLo ? reader.getPartitionCountBetweenTimestamps(reader.getMinTimestamp(), intervalLo) : 0; long intervalHi = reader.floorToPartitionTimestamp(intervals.getQuick((initialIntervalsHi - 1) * 2 + 1)); this.initialPartitionHi = Math.min(reader.getPartitionCount(), reader.getPartitionCountBetweenTimestamps(reader.getMinTimestamp(), intervalHi) + 1); } diff --git a/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java b/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java new file mode 100644 index 000000000..709076553 --- /dev/null +++ b/core/src/test/java/io/questdb/griffin/TimestampQueryTest.java @@ -0,0 +1,125 @@ +/******************************************************************************* + * ___ _ ____ ____ + * / _ \ _ _ ___ ___| |_| _ \| __ ) + * | | | | | | |/ _ \/ __| __| | | | _ \ + * | |_| | |_| | __/\__ \ |_| |_| | |_) | + * \__\_\\__,_|\___||___/\__|____/|____/ + * + * Copyright (c) 2014-2019 Appsicle + * Copyright (c) 2019-2020 QuestDB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************************/ + +package io.questdb.griffin; + +import io.questdb.griffin.engine.functions.rnd.SharedRandom; +import io.questdb.std.Rnd; +import org.junit.Before; +import org.junit.Test; + +public class TimestampQueryTest extends AbstractGriffinTest { + + @Before + public void setUp3() { + SharedRandom.RANDOM.set(new Rnd()); + } + + @Test + public void testEqualityTimestampFormatYearOnlyPositiveTest() 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"; + String expected = "symbol\tme_seq_num\ttimestamp\n"; + compiler.compile(createStmt, sqlExecutionContext); + //insert + executeInsert("INSERT INTO ob_mem_snapshot VALUES(1, 1, 1609459199000000)"); + 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 where ts ='2020' + 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'"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + }); + } + + + @Test + public void testEqualityTimestampFormatYearOnlyNegativeTest() 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"; + String expected = "symbol\tme_seq_num\ttimestamp\n"; + compiler.compile(createStmt, sqlExecutionContext); + //insert + executeInsert("INSERT INTO ob_mem_snapshot VALUES(1, 1, 1609459199000000)"); + 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 where ts ='2021' + expected = "symbol\tme_seq_num\ttimestamp\n"; + query = "SELECT * FROM ob_mem_snapshot where timestamp ='2021'"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + }); + } + + @Test + public void testEqualityTimestampFormatYearAndMonthPositiveTest() 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"; + String expected = "symbol\tme_seq_num\ttimestamp\n"; + compiler.compile(createStmt, sqlExecutionContext); + //insert + executeInsert("INSERT INTO ob_mem_snapshot VALUES(1, 1, 1609459199000000)"); + 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 where ts ='2020-12' + 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'"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + }); + } + + @Test + public void testEqualityTimestampFormatYearAndMonthNegativeTest() 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"; + String expected = "symbol\tme_seq_num\ttimestamp\n"; + compiler.compile(createStmt, sqlExecutionContext); + //insert + executeInsert("INSERT INTO ob_mem_snapshot VALUES(1, 1, 1609459199000000)"); + 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 where ts ='2021-01' + expected = "symbol\tme_seq_num\ttimestamp\n"; + query = "SELECT * FROM ob_mem_snapshot where timestamp ='2021-01'"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + // test where ts ='2020-11' + expected = "symbol\tme_seq_num\ttimestamp\n"; + query = "SELECT * FROM ob_mem_snapshot where timestamp ='2020-11'"; + printSqlResult(expected, query, "timestamp", null, null, true, true); + }); + } +} -- GitLab