use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.
the class SampleByTest method testIndexSampleByAlignToCalendarBindVariables.
@Test
public void testIndexSampleByAlignToCalendarBindVariables() throws Exception {
assertMemoryLeak(() -> {
compiler.compile("create table x as " + "(" + "select" + " rnd_double(1)*180 lat," + " rnd_double(1)*180 lon," + " rnd_symbol('a') s," + " timestamp_sequence('2021-03-28T00:59:00.00000Z', 60*1000000L) k" + " from" + " long_sequence(100)" + "), index(s) timestamp(k) partition by DAY", sqlExecutionContext);
try (RecordCursorFactory factory = compiler.compile("select k, s, first(lat) lat, last(lon) lon " + "from x " + "where s in ('a') " + "sample by 1h align to calendar time zone $1 with offset $2", sqlExecutionContext).getRecordCursorFactory()) {
String expectedMoscow = "k\ts\tlat\tlon\n" + "2021-03-28T00:15:00.000000Z\ta\t144.77803379943109\tNaN\n" + "2021-03-28T01:15:00.000000Z\ta\t31.267026583720984\tNaN\n" + "2021-03-28T02:15:00.000000Z\ta\t103.7167928478985\t128.42101395467057\n";
String expectedPrague = "k\ts\tlat\tlon\n" + "2021-03-28T00:10:00.000000Z\ta\t144.77803379943109\tNaN\n" + "2021-03-28T01:10:00.000000Z\ta\t137.95662156473048\tNaN\n" + "2021-03-28T02:10:00.000000Z\ta\tNaN\t128.42101395467057\n";
sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Moscow");
sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
assertCursor(expectedMoscow, cursor, factory.getMetadata(), true);
}
// invalid timezone
sqlExecutionContext.getBindVariableService().setStr(0, "Oopsie");
sqlExecutionContext.getBindVariableService().setStr(1, "00:15");
try {
factory.getCursor(sqlExecutionContext);
Assert.fail();
} catch (SqlException e) {
Assert.assertEquals(108, e.getPosition());
TestUtils.assertContains(e.getFlyweightMessage(), "invalid timezone: Oopsie");
}
sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
sqlExecutionContext.getBindVariableService().setStr(1, "uggs");
try {
factory.getCursor(sqlExecutionContext);
Assert.fail();
} catch (SqlException e) {
Assert.assertEquals(123, e.getPosition());
TestUtils.assertContains(e.getFlyweightMessage(), "invalid offset: uggs");
}
sqlExecutionContext.getBindVariableService().setStr(0, "Europe/Prague");
sqlExecutionContext.getBindVariableService().setStr(1, "00:10");
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
assertCursor(expectedPrague, cursor, factory.getMetadata(), true);
}
sqlExecutionContext.getBindVariableService().setStr(0, null);
sqlExecutionContext.getBindVariableService().setStr(1, "00:10");
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
assertCursor(expectedPrague, cursor, factory.getMetadata(), true);
}
}
});
}
use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.
the class LastLongGroupByFunctionFactoryTest method testNonNull.
@Test
public void testNonNull() throws SqlException {
compiler.compile("create table tab (f long)", sqlExecutionContext);
final Rnd rnd = new Rnd();
try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
for (int i = 100; i > 10; i--) {
TableWriter.Row r = w.newRow();
r.putLong(0, rnd.nextLong());
r.append();
}
w.commit();
}
try (RecordCursorFactory factory = compiler.compile("select last(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
Record record = cursor.getRecord();
Assert.assertEquals(1, cursor.size());
Assert.assertTrue(cursor.hasNext());
Assert.assertEquals(-6919361415374675248L, record.getLong(0));
}
}
}
use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.
the class LastTimestampGroupByFunctionFactoryTest method testSomeNull.
@Test
public void testSomeNull() throws SqlException {
compiler.compile("create table tab (f timestamp)", sqlExecutionContext);
try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
for (int i = 100; i > 10; i--) {
TableWriter.Row r = w.newRow();
if (i % 4 == 0) {
r.putLong(0, i);
}
r.append();
}
w.commit();
}
try (RecordCursorFactory factory = compiler.compile("select last(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
Record record = cursor.getRecord();
Assert.assertEquals(1, cursor.size());
Assert.assertTrue(cursor.hasNext());
Assert.assertEquals(Numbers.LONG_NaN, record.getTimestamp(0));
}
}
}
use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.
the class LastTimestampGroupByFunctionFactoryTest method testNonNull.
@Test
public void testNonNull() throws SqlException {
compiler.compile("create table tab (f timestamp)", sqlExecutionContext);
final Rnd rnd = new Rnd();
try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
for (int i = 100; i > 10; i--) {
TableWriter.Row r = w.newRow();
r.putLong(0, rnd.nextLong());
r.append();
}
w.commit();
}
try (RecordCursorFactory factory = compiler.compile("select last(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
Record record = cursor.getRecord();
Assert.assertEquals(1, cursor.size());
Assert.assertTrue(cursor.hasNext());
Assert.assertEquals(-6919361415374675248L, record.getTimestamp(0));
}
}
}
use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.
the class MaxDoubleGroupByFunctionFactoryTest method testFirstNull.
@Test
public void testFirstNull() throws SqlException {
compiler.compile("create table tab (f double)", sqlExecutionContext);
final Rnd rnd = new Rnd();
try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "tab", "testing")) {
TableWriter.Row r = w.newRow();
r.append();
for (int i = 100; i > 10; i--) {
r = w.newRow();
r.putDouble(0, rnd.nextDouble());
r.append();
}
w.commit();
}
try (RecordCursorFactory factory = compiler.compile("select max(f) from tab", sqlExecutionContext).getRecordCursorFactory()) {
try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
Record record = cursor.getRecord();
Assert.assertEquals(1, cursor.size());
Assert.assertTrue(cursor.hasNext());
Assert.assertEquals(0.9856290845874263, record.getDouble(0), 0.0001);
}
}
}
Aggregations