use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class SwitchFunctionFactory method getIntKeyedFunction.
private Function getIntKeyedFunction(ObjList<Function> args, IntList argPositions, int position, int n, Function keyFunction, int valueType, Function elseBranch, IntMethod intMethod) throws SqlException {
final IntObjHashMap<Function> map = new IntObjHashMap<>();
final ObjList<Function> argsToPoke = new ObjList<>();
for (int i = 1; i < n; i += 2) {
final Function fun = args.getQuick(i);
final int key = intMethod.getKey(fun, null);
final int index = map.keyIndex(key);
if (index < 0) {
throw SqlException.$(argPositions.getQuick(i), "duplicate branch");
}
map.putAt(index, key, args.getQuick(i + 1));
argsToPoke.add(args.getQuick(i + 1));
}
final Function elseB = getElseFunction(valueType, elseBranch);
final CaseFunctionPicker picker = record -> {
final int index = map.keyIndex(intMethod.getKey(keyFunction, record));
if (index < 0) {
return map.valueAtQuick(index);
}
return elseB;
};
argsToPoke.add(elseB);
argsToPoke.add(keyFunction);
return CaseCommon.getCaseFunction(position, valueType, picker, argsToPoke);
}
use of io.questdb.cairo.sql.Record 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.Record 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.Record in project questdb by bluestreak01.
the class FastMapTest method assertCursor2.
private void assertCursor2(Rnd rnd, TestRecord.ArrayBinarySequence binarySequence, int keyColumnOffset, Rnd rnd2, RecordCursor mapCursor) {
long c = 0;
rnd.reset();
rnd2.reset();
final Record record = mapCursor.getRecord();
while (mapCursor.hasNext()) {
// value
Assert.assertEquals(++c, record.getLong(0));
Assert.assertEquals(rnd2.nextInt(), record.getInt(1));
Assert.assertEquals(rnd2.nextShort(), record.getShort(2));
Assert.assertEquals(rnd2.nextByte(), record.getByte(3));
Assert.assertEquals(rnd2.nextFloat(), record.getFloat(4), 0.000001f);
Assert.assertEquals(rnd2.nextDouble(), record.getDouble(5), 0.000000001);
Assert.assertEquals(rnd2.nextLong(), record.getDate(6));
Assert.assertEquals(rnd2.nextLong(), record.getTimestamp(7));
Assert.assertEquals(rnd2.nextBoolean(), record.getBool(8));
// key fields
Assert.assertEquals(rnd.nextByte(), record.getByte(keyColumnOffset));
Assert.assertEquals(rnd.nextShort(), record.getShort(keyColumnOffset + 1));
if (rnd.nextInt() % 4 == 0) {
Assert.assertEquals(Numbers.INT_NaN, record.getInt(keyColumnOffset + 2));
} else {
Assert.assertEquals(rnd.nextInt(), record.getInt(keyColumnOffset + 2));
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertEquals(Numbers.LONG_NaN, record.getLong(keyColumnOffset + 3));
} else {
Assert.assertEquals(rnd.nextLong(), record.getLong(keyColumnOffset + 3));
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertEquals(Numbers.LONG_NaN, record.getDate(keyColumnOffset + 4));
} else {
Assert.assertEquals(rnd.nextLong(), record.getDate(keyColumnOffset + 4));
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertEquals(Numbers.LONG_NaN, record.getTimestamp(keyColumnOffset + 5));
} else {
Assert.assertEquals(rnd.nextLong(), record.getTimestamp(keyColumnOffset + 5));
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertTrue(Float.isNaN(record.getFloat(keyColumnOffset + 6)));
} else {
Assert.assertEquals(rnd.nextFloat(), record.getFloat(keyColumnOffset + 6), 0.00000001f);
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertTrue(Double.isNaN(record.getDouble(keyColumnOffset + 7)));
} else {
Assert.assertEquals(rnd.nextDouble(), record.getDouble(keyColumnOffset + 7), 0.0000000001d);
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertNull(record.getStr(keyColumnOffset + 8));
Assert.assertNull(record.getStrB(keyColumnOffset + 8));
Assert.assertEquals(-1, record.getStrLen(keyColumnOffset + 8));
AbstractCairoTest.sink.clear();
record.getStr(keyColumnOffset + 8, AbstractCairoTest.sink);
Assert.assertEquals(0, AbstractCairoTest.sink.length());
} else {
CharSequence tmp = rnd.nextChars(5);
TestUtils.assertEquals(tmp, record.getStr(keyColumnOffset + 8));
TestUtils.assertEquals(tmp, record.getStrB(keyColumnOffset + 8));
Assert.assertEquals(tmp.length(), record.getStrLen(keyColumnOffset + 8));
AbstractCairoTest.sink.clear();
record.getStr(keyColumnOffset + 8, AbstractCairoTest.sink);
TestUtils.assertEquals(tmp, AbstractCairoTest.sink);
}
if (rnd.nextInt() % 4 == 0) {
Assert.assertNull(record.getStr(keyColumnOffset + 9));
} else {
TestUtils.assertEquals(rnd.nextChars(3), record.getStr(keyColumnOffset + 9));
}
Assert.assertEquals(rnd.nextBoolean(), record.getBool(keyColumnOffset + 10));
if (rnd.nextInt() % 4 == 0) {
TestUtils.assertEquals(null, record.getBin(keyColumnOffset + 11), record.getBinLen(keyColumnOffset + 11));
} else {
binarySequence.of(rnd.nextBytes(25));
TestUtils.assertEquals(binarySequence, record.getBin(keyColumnOffset + 11), record.getBinLen(keyColumnOffset + 11));
}
}
Assert.assertEquals(5000, c);
}
use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class FastMapTest method testValueRandomWrite.
@Test
public void testValueRandomWrite() throws Exception {
TestUtils.assertMemoryLeak(() -> {
final int N = 10000;
final Rnd rnd = new Rnd();
TestRecord.ArrayBinarySequence binarySequence = new TestRecord.ArrayBinarySequence();
createTestTable(N, rnd, binarySequence);
BytecodeAssembler asm = new BytecodeAssembler();
try (TableReader reader = new TableReader(configuration, "x")) {
ListColumnFilter listColumnFilter = new ListColumnFilter();
for (int i = 0, n = reader.getMetadata().getColumnCount(); i < n; i++) {
listColumnFilter.add(i + 1);
}
try (FastMap map = new FastMap(Numbers.SIZE_1MB, new SymbolAsIntTypes().of(reader.getMetadata()), new ArrayColumnTypes().add(ColumnType.LONG).add(ColumnType.INT).add(ColumnType.SHORT).add(ColumnType.BYTE).add(ColumnType.FLOAT).add(ColumnType.DOUBLE).add(ColumnType.DATE).add(ColumnType.TIMESTAMP).add(ColumnType.BOOLEAN), N, 0.9f, 1)) {
RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), listColumnFilter, false);
// this random will be populating values
Rnd rnd2 = new Rnd();
RecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
long counter = 0;
while (cursor.hasNext()) {
MapKey key = map.withKey();
key.put(record, sink);
MapValue value = key.createValue();
Assert.assertTrue(value.isNew());
value.putFloat(4, rnd2.nextFloat());
value.putDouble(5, rnd2.nextDouble());
value.putDate(6, rnd2.nextLong());
value.putTimestamp(7, rnd2.nextLong());
value.putBool(8, rnd2.nextBoolean());
value.putLong(0, ++counter);
value.putInt(1, rnd2.nextInt());
value.putShort(2, rnd2.nextShort());
value.putByte(3, rnd2.nextByte());
}
cursor.toTop();
rnd2.reset();
long c = 0;
while (cursor.hasNext()) {
MapKey key = map.withKey();
key.put(record, sink);
MapValue value = key.findValue();
Assert.assertNotNull(value);
Assert.assertEquals(rnd2.nextFloat(), value.getFloat(4), 0.000001f);
Assert.assertEquals(rnd2.nextDouble(), value.getDouble(5), 0.000000001);
Assert.assertEquals(rnd2.nextLong(), value.getDate(6));
Assert.assertEquals(rnd2.nextLong(), value.getTimestamp(7));
Assert.assertEquals(rnd2.nextBoolean(), value.getBool(8));
Assert.assertEquals(++c, value.getLong(0));
Assert.assertEquals(rnd2.nextInt(), value.getInt(1));
Assert.assertEquals(rnd2.nextShort(), value.getShort(2));
Assert.assertEquals(rnd2.nextByte(), value.getByte(3));
}
}
}
});
}
Aggregations