Search in sources :

Example 11 with SqlCompiler

use of io.questdb.griffin.SqlCompiler in project questdb by bluestreak01.

the class LineTcpReceiverTest method testWithColumnAsReservedKeyword.

@Test
public void testWithColumnAsReservedKeyword() throws Exception {
    try (SqlCompiler compiler = new SqlCompiler(engine);
        SqlExecutionContext sqlExecutionContext = new SqlExecutionContextImpl(engine, 1).with(AllowAllCairoSecurityContext.INSTANCE, new BindVariableServiceImpl(configuration), null, -1, null)) {
        String expected = "out\ttimestamp\tin\n" + "1.0\t1989-12-31T23:26:40.000000Z\tNaN\n" + "NaN\t1990-01-01T00:00:00.000000Z\t2.0\n" + "NaN\t1990-01-01T02:13:20.000000Z\t3.0\n" + "NaN\t1990-01-01T05:00:00.000000Z\t4.0\n";
        runInContext((receiver) -> {
            String lineData = "up out=1.0 631150000000000000\n" + "up in=2.0 631152000000000000\n" + "up in=3.0 631160000000000000\n" + "up in=4.0 631170000000000000\n";
            sendLinger(receiver, lineData, "up");
        });
        TestUtils.assertSql(compiler, sqlExecutionContext, "up", sink, expected);
    }
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlCompiler(io.questdb.griffin.SqlCompiler) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) BindVariableServiceImpl(io.questdb.griffin.engine.functions.bind.BindVariableServiceImpl) Test(org.junit.Test)

Example 12 with SqlCompiler

use of io.questdb.griffin.SqlCompiler in project questdb by bluestreak01.

the class LineTcpReceiverTest method testTableTableIdChangedOnRecreate.

@Test
public void testTableTableIdChangedOnRecreate() throws Exception {
    try (SqlCompiler compiler = new SqlCompiler(engine);
        SqlExecutionContext sqlExecutionContext = new SqlExecutionContextImpl(engine, 1).with(AllowAllCairoSecurityContext.INSTANCE, new BindVariableServiceImpl(configuration), null, -1, null)) {
        compiler.compile("create table weather as (" + "select x as windspeed," + "x*2 as timetocycle, " + "cast(x as timestamp) as ts " + "from long_sequence(2)) timestamp(ts) ", sqlExecutionContext);
        CompiledQuery cq = compiler.compile("weather", sqlExecutionContext);
        try (RecordCursorFactory cursorFactory = cq.getRecordCursorFactory()) {
            try (RecordCursor cursor = cursorFactory.getCursor(sqlExecutionContext)) {
                TestUtils.printCursor(cursor, cursorFactory.getMetadata(), true, sink, printer);
                TestUtils.assertEquals("windspeed\ttimetocycle\tts\n" + "1\t2\t1970-01-01T00:00:00.000001Z\n" + "2\t4\t1970-01-01T00:00:00.000002Z\n", sink);
            }
            compiler.compile("drop table weather", sqlExecutionContext);
            runInContext((receiver) -> {
                String lineData = "weather windspeed=1.0 631150000000000000\n" + "weather windspeed=2.0 631152000000000000\n" + "weather timetocycle=0.0,windspeed=3.0 631160000000000000\n" + "weather windspeed=4.0 631170000000000000\n";
                sendLinger(receiver, lineData, "weather");
            });
            try (RecordCursor cursor = cursorFactory.getCursor(sqlExecutionContext)) {
                TestUtils.printCursor(cursor, cursorFactory.getMetadata(), true, sink, printer);
                Assert.fail();
            } catch (ReaderOutOfDateException ignored) {
            }
        }
    }
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlCompiler(io.questdb.griffin.SqlCompiler) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) BindVariableServiceImpl(io.questdb.griffin.engine.functions.bind.BindVariableServiceImpl) ReaderOutOfDateException(io.questdb.cairo.sql.ReaderOutOfDateException) CompiledQuery(io.questdb.griffin.CompiledQuery) Test(org.junit.Test)

Example 13 with SqlCompiler

use of io.questdb.griffin.SqlCompiler in project questdb by bluestreak01.

the class LineTcpO3Test method test.

private void test(String ilpResourceName) throws Exception {
    assertMemoryLeak(() -> {
        long clientFd = Net.socketTcp(true);
        Assert.assertTrue(clientFd >= 0);
        long ilpSockAddr = Net.sockaddr(Net.parseIPv4("127.0.0.1"), lineConfiguration.getNetDispatcherConfiguration().getBindPort());
        WorkerPool sharedWorkerPool = new WorkerPool(sharedWorkerPoolConfiguration);
        try (LineTcpReceiver ignored = LineTcpReceiver.create(lineConfiguration, sharedWorkerPool, LOG, engine);
            SqlCompiler compiler = new SqlCompiler(engine);
            SqlExecutionContext sqlExecutionContext = new SqlExecutionContextImpl(engine, 1)) {
            SOCountDownLatch haltLatch = new SOCountDownLatch(1);
            engine.setPoolListener((factoryType, thread, name, event, segment, position) -> {
                if (factoryType == PoolListener.SRC_WRITER && event == PoolListener.EV_RETURN && Chars.equals(name, "cpu")) {
                    haltLatch.countDown();
                }
            });
            sharedWorkerPool.assignCleaner(Path.CLEANER);
            sharedWorkerPool.start(LOG);
            TestUtils.assertConnect(clientFd, ilpSockAddr);
            readGzResource(ilpResourceName);
            Net.send(clientFd, resourceAddress, resourceSize);
            Unsafe.free(resourceAddress, resourceSize, MemoryTag.NATIVE_DEFAULT);
            haltLatch.await();
            TestUtils.printSql(compiler, sqlExecutionContext, "select * from " + "cpu", sink);
            readGzResource("selectAll1");
            DirectUnboundedByteSink expectedSink = new DirectUnboundedByteSink(resourceAddress);
            expectedSink.clear(resourceSize);
            TestUtils.assertEquals(expectedSink.toString(), sink);
            Unsafe.free(resourceAddress, resourceSize, MemoryTag.NATIVE_DEFAULT);
        } finally {
            engine.setPoolListener(null);
            Net.close(clientFd);
            Net.freeSockAddr(ilpSockAddr);
            sharedWorkerPool.halt();
        }
    });
}
Also used : SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) WorkerPool(io.questdb.mp.WorkerPool) SqlCompiler(io.questdb.griffin.SqlCompiler) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) DirectUnboundedByteSink(io.questdb.std.str.DirectUnboundedByteSink) SOCountDownLatch(io.questdb.mp.SOCountDownLatch)

Example 14 with SqlCompiler

use of io.questdb.griffin.SqlCompiler in project invesdwin-context-persistence by subes.

the class QuestDBPerformanceTest method testQuestDbPerformance.

@Test
public void testQuestDbPerformance() throws InterruptedException, SqlException, IOException {
    final File directory = new File(ContextProperties.getCacheDirectory(), QuestDBPerformanceTest.class.getSimpleName());
    Files.deleteNative(directory);
    Files.forceMkdir(directory);
    final CairoConfiguration configuration = new DefaultCairoConfiguration(directory.getAbsolutePath());
    final Instant writesStart = new Instant();
    int i = 0;
    final CairoEngine engine = new CairoEngine(configuration);
    final SqlExecutionContextImpl ctx = new SqlExecutionContextImpl(engine, 1);
    final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
    try (SqlCompiler compiler = new SqlCompiler(engine)) {
        compiler.compile("create table abc (value long, key timestamp) timestamp(key)", ctx);
        try (TableWriter writer = engine.getWriter(ctx.getCairoSecurityContext(), "abc", "insert")) {
            for (final FDate date : newValues()) {
                final TableWriter.Row row = writer.newRow(date.millisValue());
                row.putLong(0, date.millisValue());
                row.append();
                i++;
                if (i % FLUSH_INTERVAL == 0) {
                    if (loopCheck.check()) {
                        printProgress("Writes", writesStart, i, VALUES);
                    }
                    writer.commit();
                }
            }
            writer.commit();
        }
        printProgress("WritesFinished", writesStart, VALUES, VALUES);
    }
    readIterator(engine);
    readGet(engine);
    readGetLatest(engine);
    try (SqlCompiler compiler = new SqlCompiler(engine)) {
        compiler.compile("dtop table 'abc';", ctx);
    }
    engine.close();
    Files.deleteNative(directory);
}
Also used : SqlCompiler(io.questdb.griffin.SqlCompiler) Instant(de.invesdwin.util.time.Instant) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate) SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) TableWriter(io.questdb.cairo.TableWriter) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoEngine(io.questdb.cairo.CairoEngine) File(java.io.File) DefaultCairoConfiguration(io.questdb.cairo.DefaultCairoConfiguration) CairoConfiguration(io.questdb.cairo.CairoConfiguration) Test(org.junit.jupiter.api.Test)

Example 15 with SqlCompiler

use of io.questdb.griffin.SqlCompiler 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)

Aggregations

SqlCompiler (io.questdb.griffin.SqlCompiler)25 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)21 Test (org.junit.Test)11 SqlExecutionContext (io.questdb.griffin.SqlExecutionContext)10 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)8 CairoEngine (io.questdb.cairo.CairoEngine)7 RecordCursor (io.questdb.cairo.sql.RecordCursor)6 SqlException (io.questdb.griffin.SqlException)6 CairoConfiguration (io.questdb.cairo.CairoConfiguration)5 DefaultCairoConfiguration (io.questdb.cairo.DefaultCairoConfiguration)5 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)4 Instant (de.invesdwin.util.time.Instant)4 FDate (de.invesdwin.util.time.date.FDate)4 BindVariableServiceImpl (io.questdb.griffin.engine.functions.bind.BindVariableServiceImpl)4 AbstractCairoTest (io.questdb.cairo.AbstractCairoTest)3 TableWriter (io.questdb.cairo.TableWriter)3 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)3 FilesFacade (io.questdb.std.FilesFacade)3 FilesFacadeImpl (io.questdb.std.FilesFacadeImpl)3 Record (io.questdb.cairo.sql.Record)2