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;
}
}
Aggregations