Search in sources :

Example 96 with RecordCursorFactory

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);
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) SqlException(io.questdb.griffin.SqlException) Test(org.junit.Test) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest)

Example 97 with RecordCursorFactory

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));
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 98 with RecordCursorFactory

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));
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 99 with RecordCursorFactory

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));
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 100 with RecordCursorFactory

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);
        }
    }
}
Also used : TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Aggregations

RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)136 Test (org.junit.Test)118 RecordCursor (io.questdb.cairo.sql.RecordCursor)108 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)89 Record (io.questdb.cairo.sql.Record)79 TableWriter (io.questdb.cairo.TableWriter)68 Rnd (io.questdb.std.Rnd)24 SqlCompiler (io.questdb.griffin.SqlCompiler)8 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)6 ReaderOutOfDateException (io.questdb.cairo.sql.ReaderOutOfDateException)4 SqlException (io.questdb.griffin.SqlException)4 BaseConnection (org.postgresql.core.BaseConnection)4 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)3 Instant (de.invesdwin.util.time.Instant)3 FDate (de.invesdwin.util.time.date.FDate)3 FilesFacade (io.questdb.std.FilesFacade)3 FilesFacadeImpl (io.questdb.std.FilesFacadeImpl)3 CairoConfiguration (io.questdb.cairo.CairoConfiguration)2 CairoEngine (io.questdb.cairo.CairoEngine)2 CairoException (io.questdb.cairo.CairoException)2