Search in sources :

Example 71 with RecordCursorFactory

use of io.questdb.cairo.sql.RecordCursorFactory in project invesdwin-context-persistence by subes.

the class QuestDBPerformanceTest method readGet.

private void readGet(final CairoEngine engine) throws InterruptedException, SqlException {
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    final List<FDate> values = Lists.toList(newValues());
    final Instant readsStart = new Instant();
    final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 1);
    try (SqlCompiler compiler = new SqlCompiler(engine)) {
        for (int reads = 0; reads < READS; reads++) {
            FDate prevValue = null;
            int count = 0;
            for (int i = 0; i < values.size(); i++) {
                try (RecordCursorFactory factory = compiler.compile("abc WHERE key = cast(" + values.get(i).millisValue() + " AS TIMESTAMP) LIMIT 1", ctx).getRecordCursorFactory()) {
                    try (RecordCursor cursor = factory.getCursor(ctx)) {
                        final io.questdb.cairo.sql.Record record = cursor.getRecord();
                        Assertions.checkTrue(cursor.hasNext());
                        final FDate value = new FDate(record.getLong(0));
                        if (prevValue != null) {
                            Assertions.checkTrue(prevValue.isBefore(value));
                        }
                        prevValue = value;
                        count++;
                    }
                }
            }
            Assertions.checkEquals(count, VALUES);
            if (loopCheck.check()) {
                printProgress("Gets", readsStart, VALUES * reads, VALUES * READS);
            }
        }
    }
    printProgress("GetsFinished", readsStart, VALUES * READS, VALUES * READS);
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlCompiler(io.questdb.griffin.SqlCompiler) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Instant(de.invesdwin.util.time.Instant) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate)

Example 72 with RecordCursorFactory

use of io.questdb.cairo.sql.RecordCursorFactory in project invesdwin-context-persistence by subes.

the class QuestDBPerformanceTest method readGetLatest.

private void readGetLatest(final CairoEngine engine) throws InterruptedException, SqlException {
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    final List<FDate> values = Lists.toList(newValues());
    final Instant readsStart = new Instant();
    final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 1);
    try (SqlCompiler compiler = new SqlCompiler(engine)) {
        for (int reads = 0; reads < READS; reads++) {
            FDate prevValue = null;
            int count = 0;
            for (int i = 0; i < values.size(); i++) {
                // ORDER BY key DESC is horribly slow (90/s), SELECT max works better
                try (RecordCursorFactory factory = compiler.compile("SELECT max(value) FROM abc WHERE key <= cast(" + (values.get(i).millisValue()) + " AS TIMESTAMP) LIMIT 1", ctx).getRecordCursorFactory()) {
                    try (RecordCursor cursor = factory.getCursor(ctx)) {
                        final io.questdb.cairo.sql.Record record = cursor.getRecord();
                        Assertions.checkTrue(cursor.hasNext());
                        final FDate value = new FDate(record.getLong(0));
                        if (prevValue != null) {
                            Assertions.checkTrue(prevValue.isBefore(value));
                        }
                        prevValue = value;
                        count++;
                        if (loopCheck.check()) {
                            printProgress("GetLatests", readsStart, VALUES * reads + count, VALUES * READS);
                        }
                    }
                }
            }
            Assertions.checkEquals(count, VALUES);
        }
    }
    printProgress("GetLatestsFinished", readsStart, VALUES * READS, VALUES * READS);
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlCompiler(io.questdb.griffin.SqlCompiler) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Instant(de.invesdwin.util.time.Instant) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate)

Example 73 with RecordCursorFactory

use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.

the class JsonQueryProcessor method executeNewSelect.

private void executeNewSelect(JsonQueryProcessorState state, CompiledQuery cc, CharSequence keepAliveHeader) throws PeerDisconnectedException, PeerIsSlowToReadException, SqlException {
    state.logExecuteNew();
    final RecordCursorFactory factory = cc.getRecordCursorFactory();
    executeSelect(state, factory, keepAliveHeader);
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory)

Example 74 with RecordCursorFactory

use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.

the class PGJobContextTest method testInsertAllTypes.

private void testInsertAllTypes(boolean binary) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (" + "a byte," + "b char," + "c short," + "d int," + "e long," + "f float," + "g double," + "h string," + "i symbol," + "j boolean," + "k long256" + ")", sqlExecutionContext);
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, binary);
            final PreparedStatement insert = connection.prepareStatement("insert into xyz values (?,?,?,?,?,?,?,?,?,?,?)")) {
            final Rnd rnd = new Rnd();
            connection.setAutoCommit(false);
            for (int i = 0; i < 10_000; i++) {
                if (rnd.nextInt() % 4 > 0) {
                    insert.setByte(1, rnd.nextByte());
                } else {
                    insert.setNull(1, Types.SMALLINT);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setByte(2, (byte) rnd.nextChar());
                } else {
                    insert.setNull(2, Types.SMALLINT);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setShort(3, rnd.nextShort());
                } else {
                    insert.setNull(3, Types.SMALLINT);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setInt(4, rnd.nextInt());
                } else {
                    insert.setNull(4, Types.INTEGER);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setLong(5, rnd.nextLong());
                } else {
                    insert.setNull(5, Types.BIGINT);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setFloat(6, rnd.nextFloat());
                } else {
                    insert.setNull(6, Types.REAL);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setDouble(7, rnd.nextDouble());
                } else {
                    insert.setNull(7, Types.FLOAT);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setString(8, "hello21");
                } else {
                    insert.setNull(8, Types.VARCHAR);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setString(9, "bus");
                } else {
                    insert.setNull(9, Types.VARCHAR);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setBoolean(10, true);
                } else {
                    insert.setNull(10, Types.BOOLEAN);
                }
                if (rnd.nextInt() % 4 > 0) {
                    insert.setString(11, "05a9796963abad00001e5f6bbdb38");
                } else {
                    insert.setNull(11, Types.VARCHAR);
                }
                insert.execute();
                Assert.assertEquals(1, insert.getUpdateCount());
            }
            connection.commit();
            rnd.reset();
            try (RecordCursorFactory factory = compiler.compile("xyz", sqlExecutionContext).getRecordCursorFactory()) {
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    final Record record = cursor.getRecord();
                    int count = 0;
                    while (cursor.hasNext()) {
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextByte(), record.getByte(0));
                        } else {
                            Assert.assertEquals(0, record.getByte(0));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextChar(), record.getChar(1));
                        } else {
                            Assert.assertEquals(0, record.getChar(1));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextShort(), record.getShort(2));
                        } else {
                            Assert.assertEquals(0, record.getShort(2));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextInt(), record.getInt(3));
                        } else {
                            Assert.assertEquals(Numbers.INT_NaN, record.getInt(3));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextLong(), record.getLong(4));
                        } else {
                            Assert.assertEquals(Numbers.LONG_NaN, record.getLong(4));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextFloat(), record.getFloat(5), 0.0001f);
                        } else {
                            Assert.assertTrue(record.getFloat(5) != record.getFloat(5));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertEquals(rnd.nextDouble(), record.getDouble(6), 0.000001);
                        } else {
                            Assert.assertTrue(record.getDouble(6) != record.getDouble(6));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            TestUtils.assertEquals("hello21", record.getStr(7));
                        } else {
                            Assert.assertNull(record.getStr(7));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            TestUtils.assertEquals("bus", record.getSym(8));
                        } else {
                            Assert.assertNull(record.getSym(8));
                        }
                        if (rnd.nextInt() % 4 > 0) {
                            Assert.assertTrue(record.getBool(9));
                        } else {
                            Assert.assertFalse(record.getBool(9));
                        }
                        sink.clear();
                        record.getLong256(10, sink);
                        if (rnd.nextInt() % 4 > 0) {
                            TestUtils.assertEquals("0x5a9796963abad00001e5f6bbdb38", sink);
                        } else {
                            Assert.assertEquals(0, sink.length());
                        }
                        count++;
                    }
                    Assert.assertEquals(10_000, count);
                }
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) BaseConnection(org.postgresql.core.BaseConnection) Rnd(io.questdb.std.Rnd) Record(io.questdb.cairo.sql.Record)

Example 75 with RecordCursorFactory

use of io.questdb.cairo.sql.RecordCursorFactory in project questdb by bluestreak01.

the class PGJobContextTest method insertAllGeoHashTypes.

private void insertAllGeoHashTypes(boolean binary) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (" + "a geohash(1b)," + "b geohash(2b)," + "c geohash(3b)," + "d geohash(1c)," + "e geohash(2c)," + "f geohash(4c)," + "g geohash(8c)" + ")", sqlExecutionContext);
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, binary);
            final PreparedStatement insert = connection.prepareStatement("insert into xyz values (" + "cast(? as geohash(1b))," + "cast(? as geohash(2b))," + "cast(? as geohash(3b))," + "cast(? as geohash(1c))," + "cast(? as geohash(2c))," + "cast(? as geohash(4c))," + "cast(? as geohash(8c)))")) {
            connection.setAutoCommit(false);
            for (int i = 0; i < 100; i++) {
                insert.setString(1, "0");
                insert.setString(2, "10");
                insert.setString(3, "010");
                insert.setString(4, "x");
                insert.setString(5, "xy");
                insert.setString(6, "xyzw");
                insert.setString(7, "xyzwzvxq");
                insert.execute();
                Assert.assertEquals(1, insert.getUpdateCount());
            }
            connection.commit();
            try (RecordCursorFactory factory = compiler.compile("xyz", sqlExecutionContext).getRecordCursorFactory()) {
                try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                    final Record record = cursor.getRecord();
                    int count = 0;
                    while (cursor.hasNext()) {
                        // TODO: bits geohash lliteral
                        // Assert.assertEquals((byte)GeoHashes.fromBitString("0"), record.getGeoHashByte(0));
                        // Assert.assertEquals((byte)GeoHashes.fromBitString("01"), record.getGeoHashByte(1));
                        // Assert.assertEquals((byte)GeoHashes.fromBitString("010"), record.getGeoHashByte(2));
                        Assert.assertEquals(GeoHashes.fromString("x", 0, 1), record.getGeoByte(3));
                        Assert.assertEquals(GeoHashes.fromString("xy", 0, 2), record.getGeoShort(4));
                        Assert.assertEquals(GeoHashes.fromString("xyzw", 0, 4), record.getGeoInt(5));
                        Assert.assertEquals(GeoHashes.fromString("xyzwzvxq", 0, 8), record.getGeoLong(6));
                        count++;
                    }
                    Assert.assertEquals(100, count);
                }
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) BaseConnection(org.postgresql.core.BaseConnection) Record(io.questdb.cairo.sql.Record)

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