use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class CompactMapTest method testValueRandomWrite.
@Test
public void testValueRandomWrite() throws Exception {
TestUtils.assertMemoryLeak(() -> {
final int N = 1000;
final Rnd rnd = new Rnd();
TestRecord.ArrayBinarySequence binarySequence = new TestRecord.ArrayBinarySequence();
CairoTestUtils.createTestTable(N, rnd, binarySequence);
BytecodeAssembler asm = new BytecodeAssembler();
try (TableReader reader = new TableReader(configuration, "x")) {
EntityColumnFilter entityColumnFilter = new EntityColumnFilter();
entityColumnFilter.of(reader.getMetadata().getColumnCount());
try (CompactMap map = new CompactMap(1024 * 1024, 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.9, 1, Integer.MAX_VALUE)) {
RecordSink sink = RecordSinkFactory.getInstance(asm, reader.getMetadata(), entityColumnFilter, false);
// this random will be populating values
Rnd rnd2 = new Rnd();
RecordCursor cursor = reader.getCursor();
Record record = cursor.getRecord();
long counter = 0;
while (cursor.hasNext()) {
MapKey key = map.withKey();
key.put(record, sink);
MapValue value1 = key.createValue();
Assert.assertTrue(value1.isNew());
value1.putFloat(4, rnd2.nextFloat());
value1.putDouble(5, rnd2.nextDouble());
value1.putDate(6, rnd2.nextLong());
value1.putTimestamp(7, rnd2.nextLong());
value1.putBool(8, rnd2.nextBoolean());
value1.putLong(0, ++counter);
value1.putInt(1, rnd2.nextInt());
value1.putShort(2, rnd2.nextShort());
value1.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));
}
}
}
});
}
use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class TableWriterTest method testSymbolCacheFlag.
private void testSymbolCacheFlag(boolean cacheFlag) {
try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).cached(cacheFlag).col("b", ColumnType.STRING).col("c", ColumnType.SYMBOL).cached(!cacheFlag).timestamp()) {
CairoTestUtils.create(model);
}
int N = 1000;
Rnd rnd = new Rnd();
try (TableWriter writer = new TableWriter(configuration, "x")) {
Assert.assertEquals(cacheFlag, writer.isSymbolMapWriterCached(0));
Assert.assertNotEquals(cacheFlag, writer.isSymbolMapWriterCached(2));
for (int i = 0; i < N; i++) {
TableWriter.Row r = writer.newRow();
r.putSym(0, rnd.nextChars(5));
r.putStr(1, rnd.nextChars(10));
r.append();
}
writer.commit();
}
try (TableReader reader = new TableReader(configuration, "x")) {
rnd.reset();
int count = 0;
Assert.assertEquals(cacheFlag, reader.isColumnCached(0));
Assert.assertNotEquals(cacheFlag, reader.isColumnCached(2));
RecordCursor cursor = reader.getCursor();
final Record record = cursor.getRecord();
while (cursor.hasNext()) {
TestUtils.assertEquals(rnd.nextChars(5), record.getSym(0));
TestUtils.assertEquals(rnd.nextChars(10), record.getStr(1));
count++;
}
Assert.assertEquals(N, count);
}
}
use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class TableWriterTest method testTwoByteUtf8.
@Test
public void testTwoByteUtf8() {
String name = "соотечественник";
try (TableModel model = new TableModel(configuration, name, PartitionBy.NONE).col("секьюрити", ColumnType.STRING).timestamp()) {
CairoTestUtils.create(model);
}
Rnd rnd = new Rnd();
try (TableWriter writer = new TableWriter(configuration, name)) {
for (int i = 0; i < 1000000; i++) {
TableWriter.Row r = writer.newRow();
r.putStr(0, rnd.nextChars(5));
r.append();
}
writer.commit(CommitMode.ASYNC);
writer.addColumn("митинг", ColumnType.INT);
Assert.assertEquals(0, writer.getColumnIndex("секьюрити"));
Assert.assertEquals(2, writer.getColumnIndex("митинг"));
}
rnd.reset();
try (TableReader reader = new TableReader(configuration, name)) {
int col = reader.getMetadata().getColumnIndex("секьюрити");
RecordCursor cursor = reader.getCursor();
final Record r = cursor.getRecord();
while (cursor.hasNext()) {
TestUtils.assertEquals(rnd.nextChars(5), r.getStr(col));
}
}
}
use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.
the class SwitchFunctionFactory method getLongKeyedFunction.
private Function getLongKeyedFunction(ObjList<Function> args, IntList argPositions, int position, int n, Function keyFunction, int valueType, Function elseBranch, LongMethod longMethod) throws SqlException {
final LongObjHashMap<Function> map = new LongObjHashMap<>();
final ObjList<Function> argsToPoke = new ObjList<>();
for (int i = 1; i < n; i += 2) {
final Function fun = args.getQuick(i);
final long key = longMethod.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(longMethod.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 testInsertAllTypes.
private void testInsertAllTypes(boolean binary) throws Exception {
assertMemoryLeak(() -> {
compiler.compile("create table xyz (" + "a byte," + "b char," + "c short," + "d int," + "e long," + "f float," + "g double," + "h string," + "i symbol," + "j boolean," + "k long256" + ")", sqlExecutionContext);
try (final PGWireServer ignored = createPGServer(2);
final Connection connection = getConnection(false, binary);
final PreparedStatement insert = connection.prepareStatement("insert into xyz values (?,?,?,?,?,?,?,?,?,?,?)")) {
final Rnd rnd = new Rnd();
connection.setAutoCommit(false);
for (int i = 0; i < 10_000; i++) {
if (rnd.nextInt() % 4 > 0) {
insert.setByte(1, rnd.nextByte());
} else {
insert.setNull(1, Types.SMALLINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setByte(2, (byte) rnd.nextChar());
} else {
insert.setNull(2, Types.SMALLINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setShort(3, rnd.nextShort());
} else {
insert.setNull(3, Types.SMALLINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setInt(4, rnd.nextInt());
} else {
insert.setNull(4, Types.INTEGER);
}
if (rnd.nextInt() % 4 > 0) {
insert.setLong(5, rnd.nextLong());
} else {
insert.setNull(5, Types.BIGINT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setFloat(6, rnd.nextFloat());
} else {
insert.setNull(6, Types.REAL);
}
if (rnd.nextInt() % 4 > 0) {
insert.setDouble(7, rnd.nextDouble());
} else {
insert.setNull(7, Types.FLOAT);
}
if (rnd.nextInt() % 4 > 0) {
insert.setString(8, "hello21");
} else {
insert.setNull(8, Types.VARCHAR);
}
if (rnd.nextInt() % 4 > 0) {
insert.setString(9, "bus");
} else {
insert.setNull(9, Types.VARCHAR);
}
if (rnd.nextInt() % 4 > 0) {
insert.setBoolean(10, true);
} else {
insert.setNull(10, Types.BOOLEAN);
}
if (rnd.nextInt() % 4 > 0) {
insert.setString(11, "05a9796963abad00001e5f6bbdb38");
} else {
insert.setNull(11, Types.VARCHAR);
}
insert.execute();
Assert.assertEquals(1, insert.getUpdateCount());
}
connection.commit();
rnd.reset();
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()) {
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextByte(), record.getByte(0));
} else {
Assert.assertEquals(0, record.getByte(0));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextChar(), record.getChar(1));
} else {
Assert.assertEquals(0, record.getChar(1));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextShort(), record.getShort(2));
} else {
Assert.assertEquals(0, record.getShort(2));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextInt(), record.getInt(3));
} else {
Assert.assertEquals(Numbers.INT_NaN, record.getInt(3));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextLong(), record.getLong(4));
} else {
Assert.assertEquals(Numbers.LONG_NaN, record.getLong(4));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextFloat(), record.getFloat(5), 0.0001f);
} else {
Assert.assertTrue(record.getFloat(5) != record.getFloat(5));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertEquals(rnd.nextDouble(), record.getDouble(6), 0.000001);
} else {
Assert.assertTrue(record.getDouble(6) != record.getDouble(6));
}
if (rnd.nextInt() % 4 > 0) {
TestUtils.assertEquals("hello21", record.getStr(7));
} else {
Assert.assertNull(record.getStr(7));
}
if (rnd.nextInt() % 4 > 0) {
TestUtils.assertEquals("bus", record.getSym(8));
} else {
Assert.assertNull(record.getSym(8));
}
if (rnd.nextInt() % 4 > 0) {
Assert.assertTrue(record.getBool(9));
} else {
Assert.assertFalse(record.getBool(9));
}
sink.clear();
record.getLong256(10, sink);
if (rnd.nextInt() % 4 > 0) {
TestUtils.assertEquals("0x5a9796963abad00001e5f6bbdb38", sink);
} else {
Assert.assertEquals(0, sink.length());
}
count++;
}
Assert.assertEquals(10_000, count);
}
}
}
});
}
Aggregations