Search in sources :

Example 66 with RecordCursorFactory

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

the class JsonQueryProcessor method execute0.

public void execute0(JsonQueryProcessorState state) throws PeerDisconnectedException, PeerIsSlowToReadException, ServerDisconnectException {
    metrics.jsonQuery().markStart();
    state.startExecutionTimer();
    final HttpConnectionContext context = state.getHttpConnectionContext();
    // do not set random for new request to avoid copying random from previous request into next one
    // the only time we need to copy random from state is when we resume request execution
    sqlExecutionContext.with(context.getCairoSecurityContext(), null, null, context.getFd(), interruptor.of(context.getFd()));
    state.info().$("exec [q='").utf8(state.getQuery()).$("']").$();
    final RecordCursorFactory factory = QueryCache.getInstance().poll(state.getQuery());
    try {
        if (factory != null) {
            try {
                sqlExecutionContext.storeTelemetry(CompiledQuery.SELECT, Telemetry.ORIGIN_HTTP_JSON);
                executeCachedSelect(state, factory, configuration.getKeepAliveHeader());
            } catch (ReaderOutOfDateException e) {
                LOG.info().$(e.getFlyweightMessage()).$();
                Misc.free(factory);
                compileQuery(state);
            }
        } else {
            // new query
            compileQuery(state);
        }
    } catch (SqlException e) {
        syntaxError(context.getChunkedResponseSocket(), e, state, configuration.getKeepAliveHeader());
        readyForNextRequest(context);
    } catch (EntryUnavailableException e) {
        LOG.info().$("[fd=").$(context.getFd()).$("] Resource busy, will retry").$();
        throw RetryOperationException.INSTANCE;
    } catch (CairoError | CairoException e) {
        internalError(context.getChunkedResponseSocket(), e.getFlyweightMessage(), e, state);
        readyForNextRequest(context);
    } catch (PeerIsSlowToReadException | PeerDisconnectedException e) {
        // re-throw the exception
        throw e;
    } catch (Throwable e) {
        state.error().$("Uh-oh. Error!").$(e).$();
        throw ServerDisconnectException.INSTANCE;
    }
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) CairoError(io.questdb.cairo.CairoError) PeerIsSlowToReadException(io.questdb.network.PeerIsSlowToReadException) CairoException(io.questdb.cairo.CairoException) EntryUnavailableException(io.questdb.cairo.EntryUnavailableException) ReaderOutOfDateException(io.questdb.cairo.sql.ReaderOutOfDateException) PeerDisconnectedException(io.questdb.network.PeerDisconnectedException)

Example 67 with RecordCursorFactory

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

the class QueryCache method poll.

public RecordCursorFactory poll(CharSequence sql) {
    final RecordCursorFactory factory = cache.poll(sql);
    log(factory == null ? "miss" : "hit", sql);
    return factory;
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory)

Example 68 with RecordCursorFactory

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

the class PGJobContextTest method testInsertBinaryBindVariable.

private void testInsertBinaryBindVariable(boolean binaryProtocol) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (" + "a binary" + ")", sqlExecutionContext);
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, binaryProtocol);
            final PreparedStatement insert = connection.prepareStatement("insert into xyz values (?)")) {
            connection.setAutoCommit(false);
            int totalCount = 10;
            for (int i = 0; i < totalCount; i++) {
                insert.setBytes(1, new byte[] { 1, 2, 3, 4 });
                insert.execute();
            }
            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()) {
                        Assert.assertEquals(4, record.getBinLen(0));
                        count++;
                    }
                    Assert.assertEquals(totalCount, 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)

Example 69 with RecordCursorFactory

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

the class PGJobContextTest method testBinaryInsert.

private void testBinaryInsert(int maxLength, boolean binaryProtocol) throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table xyz (" + "a binary" + ")", sqlExecutionContext);
        try (final PGWireServer ignored = createPGServer(1);
            final Connection connection = getConnection(false, binaryProtocol);
            final PreparedStatement insert = connection.prepareStatement("insert into xyz values (?)")) {
            connection.setAutoCommit(false);
            try (InputStream str = new InputStream() {

                int value = 0;

                @Override
                public int read() {
                    if (maxLength == value)
                        return -1;
                    return value++ % 255;
                }
            }) {
                int totalCount = 1;
                for (int i = 0; i < totalCount; i++) {
                    insert.setBinaryStream(1, str);
                    insert.execute();
                }
                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()) {
                            Assert.assertEquals(maxLength, record.getBinLen(0));
                            BinarySequence bs = record.getBin(0);
                            for (int i = 0; i < maxLength; i++) {
                                Assert.assertEquals(i % 255, // Convert byte to unsigned int
                                bs.byteAt(i) & 0xff);
                            }
                            count++;
                        }
                        Assert.assertEquals(totalCount, count);
                    }
                }
            }
        }
    });
}
Also used : BinarySequence(io.questdb.std.BinarySequence) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) InputStream(java.io.InputStream) BaseConnection(org.postgresql.core.BaseConnection) Record(io.questdb.cairo.sql.Record)

Example 70 with RecordCursorFactory

use of io.questdb.cairo.sql.RecordCursorFactory 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)

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