Search in sources :

Example 1 with Timestamps

use of io.questdb.std.datetime.microtime.Timestamps in project questdb by bluestreak01.

the class TimestampQueryTest method testNowPerformsBinarySearchOnTimestamp.

@Test
public void testNowPerformsBinarySearchOnTimestamp() throws Exception {
    try {
        currentMicros = 0;
        assertMemoryLeak(() -> {
            // create table
            // One hour step timestamps from epoch for 2000 steps
            final int count = 200;
            String createStmt = "create table xts as (select timestamp_sequence(0, 3600L * 1000 * 1000) ts from long_sequence(" + count + ")) timestamp(ts) partition by DAY";
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.000000Z'");
            compiler.compile(createStmt, sqlExecutionContext);
            formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
            Stream<Object[]> dates = LongStream.rangeClosed(0, count - 1).map(i -> i * 3600L * 1000).mapToObj(ts -> new Object[] { ts * 1000L, formatter.format(new Date(ts)) });
            List<Object[]> datesArr = dates.collect(Collectors.toList());
            final long hour = Timestamps.HOUR_MICROS;
            final long day = 24 * hour;
            compareNowRange("select * FROM xts WHERE ts >= '1970' and ts <= '2021'", datesArr, ts -> true);
            // Scroll now to the end
            currentMicros = 200L * hour;
            compareNowRange("select ts FROM xts WHERE ts >= now() - 3600 * 1000 * 1000L", datesArr, ts -> ts >= currentMicros - hour);
            compareNowRange("select ts FROM xts WHERE ts >= now() + 3600 * 1000 * 1000L", datesArr, ts -> ts >= currentMicros + hour);
            for (currentMicros = hour; currentMicros < count * hour; currentMicros += day) {
                compareNowRange("select ts FROM xts WHERE ts < now()", datesArr, ts -> ts < currentMicros);
            }
            for (currentMicros = hour; currentMicros < count * hour; currentMicros += 12 * hour) {
                compareNowRange("select ts FROM xts WHERE ts >= now()", datesArr, ts -> ts >= currentMicros);
            }
            for (currentMicros = 0; currentMicros < count * hour + 4 * day; currentMicros += 5 * hour) {
                compareNowRange("select ts FROM xts WHERE ts <= dateadd('d', -1, now()) and ts >= dateadd('d', -2, now())", datesArr, ts -> ts >= (currentMicros - 2 * day) && (ts <= currentMicros - day));
            }
            currentMicros = 100L * hour;
            compareNowRange("WITH temp AS (SELECT ts FROM xts WHERE ts > dateadd('y', -1, now())) " + "SELECT ts FROM temp WHERE ts < now()", datesArr, ts -> ts < currentMicros);
        });
    } finally {
        currentMicros = -1;
    }
}
Also used : ColumnType(io.questdb.cairo.ColumnType) LongStream(java.util.stream.LongStream) Date(java.util.Date) TimeZone(java.util.TimeZone) TableModel(io.questdb.cairo.TableModel) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) PartitionBy(io.questdb.cairo.PartitionBy) TestUtils(io.questdb.test.tools.TestUtils) Timestamps(io.questdb.std.datetime.microtime.Timestamps) LongPredicate(java.util.function.LongPredicate) Assert(org.junit.Assert) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Aggregations

ColumnType (io.questdb.cairo.ColumnType)1 PartitionBy (io.questdb.cairo.PartitionBy)1 TableModel (io.questdb.cairo.TableModel)1 Timestamps (io.questdb.std.datetime.microtime.Timestamps)1 TestUtils (io.questdb.test.tools.TestUtils)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 TimeZone (java.util.TimeZone)1 LongPredicate (java.util.function.LongPredicate)1 Collectors (java.util.stream.Collectors)1 LongStream (java.util.stream.LongStream)1 Stream (java.util.stream.Stream)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1