Search in sources :

Example 31 with RecordCursor

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

the class EmbeddedApiTest method testReadWrite.

@Test
public void testReadWrite() throws Exception {
    final CairoConfiguration configuration = new DefaultCairoConfiguration(temp.getRoot().getAbsolutePath());
    TestUtils.assertMemoryLeak(() -> {
        // write part
        try (final CairoEngine engine = new CairoEngine(configuration);
            final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 1);
            final SqlCompiler compiler = new SqlCompiler(engine)) {
            compiler.compile("create table abc (a int, b byte, c short, d long, e float, g double, h date, i symbol, j string, k boolean, ts timestamp) timestamp(ts)", ctx);
            try (TableWriter writer = engine.getWriter(ctx.getCairoSecurityContext(), "abc", "testing")) {
                for (int i = 0; i < 10; i++) {
                    TableWriter.Row row = writer.newRow(Os.currentTimeMicros());
                    row.putInt(0, 123);
                    row.putByte(1, (byte) 1111);
                    row.putShort(2, (short) 222);
                    row.putLong(3, 333);
                    row.putFloat(4, 4.44f);
                    row.putDouble(5, 5.55);
                    row.putDate(6, System.currentTimeMillis());
                    row.putSym(7, "xyz");
                    row.putStr(8, "abc");
                    row.putBool(9, true);
                    row.append();
                }
                writer.commit();
            }
            try (RecordCursorFactory factory = compiler.compile("abc", ctx).getRecordCursorFactory()) {
                try (RecordCursor cursor = factory.getCursor(ctx)) {
                    final Record record = cursor.getRecord();
                    // noinspection StatementWithEmptyBody
                    while (cursor.hasNext()) {
                    // access 'record' instance for field values
                    }
                }
            }
        }
    });
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlCompiler(io.questdb.griffin.SqlCompiler) TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) Record(io.questdb.cairo.sql.Record) CairoEngine(io.questdb.cairo.CairoEngine) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Test(org.junit.Test)

Example 32 with RecordCursor

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

the class EmbeddedApiTest method testConcurrentSQLExec.

@Test
public void testConcurrentSQLExec() throws Exception {
    final CairoConfiguration configuration = new DefaultCairoConfiguration(temp.getRoot().getAbsolutePath());
    final Log log = LogFactory.getLog("testConcurrentSQLExec");
    TestUtils.assertMemoryLeak(() -> {
        WorkerPool workerPool = new WorkerPool(new WorkerPoolConfiguration() {

            @Override
            public int[] getWorkerAffinity() {
                return new int[] { -1, -1 };
            }

            @Override
            public int getWorkerCount() {
                return 2;
            }

            @Override
            public boolean haltOnError() {
                return false;
            }
        });
        Rnd rnd = new Rnd();
        try (final CairoEngine engine = new CairoEngine(configuration)) {
            workerPool.assign(new GroupByJob(engine.getMessageBus()));
            workerPool.start(log);
            try {
                // number of cores is current thread + workers in the pool
                final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 2);
                try (SqlCompiler compiler = new SqlCompiler(engine)) {
                    compiler.compile("create table abc (g double, ts timestamp) timestamp(ts) partition by DAY", ctx);
                    long timestamp = 0;
                    try (TableWriter writer = engine.getWriter(ctx.getCairoSecurityContext(), "abc", "testing")) {
                        for (int i = 0; i < 10_000_000; i++) {
                            TableWriter.Row row = writer.newRow(timestamp);
                            row.putDouble(0, rnd.nextDouble());
                            row.append();
                            timestamp += 1_000_000;
                        }
                        writer.commit();
                    }
                    try (RecordCursorFactory factory = compiler.compile("select sum(g) from abc", ctx).getRecordCursorFactory()) {
                        try (RecordCursor cursor = factory.getCursor(ctx)) {
                            final Record ignored = cursor.getRecord();
                            // noinspection StatementWithEmptyBody
                            while (cursor.hasNext()) {
                            // access 'record' instance for field values
                            }
                        }
                    }
                }
            } finally {
                workerPool.halt();
            }
        }
    });
}
Also used : SqlCompiler(io.questdb.griffin.SqlCompiler) RecordCursor(io.questdb.cairo.sql.RecordCursor) Log(io.questdb.log.Log) Rnd(io.questdb.std.Rnd) GroupByJob(io.questdb.griffin.engine.groupby.vect.GroupByJob) SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) WorkerPool(io.questdb.mp.WorkerPool) TableWriter(io.questdb.cairo.TableWriter) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) Record(io.questdb.cairo.sql.Record) CairoEngine(io.questdb.cairo.CairoEngine) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoConfiguration(io.questdb.cairo.CairoConfiguration) WorkerPoolConfiguration(io.questdb.mp.WorkerPoolConfiguration) Test(org.junit.Test)

Example 33 with RecordCursor

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

the class JoinTest method testCrossTripleOverflow.

@Test
public void testCrossTripleOverflow() throws Exception {
    assertMemoryLeak(() -> {
        final CompiledQuery cq = compiler.compile("select * from long_sequence(1000000000) a cross join long_sequence(1000000000) b cross join long_sequence(1000000000) c", sqlExecutionContext);
        final RecordCursorFactory factory = cq.getRecordCursorFactory();
        try {
            Assert.assertNotNull(factory);
            sink.clear();
            printer.printHeader(factory.getMetadata(), sink);
            TestUtils.assertEquals("x\tx1\tx2\n", sink);
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                Assert.assertEquals(Long.MAX_VALUE, cursor.size());
            }
        } finally {
            Misc.free(factory);
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Test(org.junit.Test)

Example 34 with RecordCursor

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

the class LatestByParallelTest method assertQuery.

private static void assertQuery(SqlCompiler compiler, SqlExecutionContext sqlExecutionContext, String expected, String ddl, String query) throws SqlException {
    compiler.compile(ddl, sqlExecutionContext);
    CompiledQuery cc = compiler.compile(query, sqlExecutionContext);
    RecordCursorFactory factory = cc.getRecordCursorFactory();
    try {
        try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
            TestUtils.assertCursor(expected, cursor, factory.getMetadata(), true, sink);
        }
    } finally {
        Misc.free(factory);
    }
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor)

Example 35 with RecordCursor

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

the class O3CommitLagTest method insertUncommitted.

private void insertUncommitted(SqlCompiler compiler, SqlExecutionContext sqlExecutionContext, String sql, TableWriter writer) throws SqlException {
    minTimestamp = Long.MAX_VALUE;
    maxTimestamp = Long.MIN_VALUE;
    try (RecordCursorFactory factory = compiler.compile(sql, sqlExecutionContext).getRecordCursorFactory()) {
        RecordMetadata metadata = factory.getMetadata();
        int timestampIndex = writer.getMetadata().getTimestampIndex();
        EntityColumnFilter toColumnFilter = new EntityColumnFilter();
        toColumnFilter.of(metadata.getColumnCount());
        if (null == copier) {
            copier = SqlCompiler.assembleRecordToRowCopier(new BytecodeAssembler(), metadata, writer.getMetadata(), toColumnFilter);
        }
        try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                long timestamp = record.getTimestamp(timestampIndex);
                if (timestamp > maxTimestamp) {
                    maxTimestamp = timestamp;
                }
                if (timestamp < minTimestamp) {
                    minTimestamp = timestamp;
                }
                Row row = writer.newRow(timestamp);
                copier.copy(record, row);
                row.append();
            }
        }
    }
}
Also used : RecordMetadata(io.questdb.cairo.sql.RecordMetadata) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Row(io.questdb.cairo.TableWriter.Row) BytecodeAssembler(io.questdb.std.BytecodeAssembler)

Aggregations

RecordCursor (io.questdb.cairo.sql.RecordCursor)174 Test (org.junit.Test)137 Record (io.questdb.cairo.sql.Record)123 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)108 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)87 TableWriter (io.questdb.cairo.TableWriter)71 Rnd (io.questdb.std.Rnd)29 LPSZ (io.questdb.std.str.LPSZ)10 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)7 SqlCompiler (io.questdb.griffin.SqlCompiler)6 DateFormat (io.questdb.std.datetime.DateFormat)6 Path (io.questdb.std.str.Path)6 StringSink (io.questdb.std.str.StringSink)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 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