use of io.questdb.cairo.sql.RecordCursor 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.RecordCursor 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.RecordCursor 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) {
}
}
}
}
use of io.questdb.cairo.sql.RecordCursor 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);
}
use of io.questdb.cairo.sql.RecordCursor 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);
}
Aggregations