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;
}
}
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;
}
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);
}
}
}
});
}
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);
}
}
}
}
});
}
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) {
}
}
}
}
Aggregations