Search in sources :

Example 76 with Rnd

use of io.questdb.std.Rnd in project questdb by bluestreak01.

the class InsertTest method testBindVariableInsert.

private void testBindVariableInsert(int partitionBy, TimestampFunction timestampFunction, boolean initBindVariables, boolean columnSet) throws Exception {
    assertMemoryLeak(() -> {
        CairoTestUtils.createAllTableWithNewTypes(configuration, partitionBy);
        // this is BLOB
        byte[] blob = new byte[500];
        TestBinarySequence bs = new TestBinarySequence();
        bs.of(blob);
        Rnd rnd = new Rnd();
        if (initBindVariables) {
            // this is type declaration to have query compile correctly
            bindVariableService.setInt(0, 0);
            bindVariableService.setShort(1, (short) 10);
            bindVariableService.setByte(2, (byte) 91);
            bindVariableService.setDouble(3, 9.2);
            bindVariableService.setFloat(4, 5.6f);
            bindVariableService.setLong(5, 99901);
            bindVariableService.setStr(6, "hello kitty");
            bindVariableService.setStr(7, "sym?");
            bindVariableService.setBoolean(8, true);
            bindVariableService.setBin(9, bs);
            bindVariableService.setDate(10, 1234L);
            bindVariableService.setLong256(11, 1, 2, 3, 4);
            bindVariableService.setChar(12, 'A');
            bindVariableService.setTimestamp(13, timestampFunction.getTimestamp());
        }
        final String sql;
        if (columnSet) {
            sql = "insert into all2 (" + "int, " + "short, " + "byte, " + "double, " + "float, " + "long, " + "str, " + "sym, " + "bool, " + "bin, " + "date, " + "long256, " + "chr, " + "timestamp" + ") values (" + "$1, " + "$2, " + "$3, " + "$4, " + "$5, " + "$6, " + "$7, " + "$8, " + "$9, " + "$10, " + "$11, " + "$12, " + "$13, " + "$14)";
        } else {
            sql = "insert into all2 values (" + "$1, " + "$2, " + "$3, " + "$4, " + "$5, " + "$6, " + "$7, " + "$8, " + "$9, " + "$10, " + "$11, " + "$12, " + "$13, " + "$14)";
        }
        final CompiledQuery cq = compiler.compile(sql, sqlExecutionContext);
        Assert.assertEquals(CompiledQuery.INSERT, cq.getType());
        InsertStatement insert = cq.getInsertStatement();
        try (InsertMethod method = insert.createMethod(sqlExecutionContext)) {
            for (int i = 0; i < 10_000; i++) {
                bindVariableService.setInt(0, rnd.nextInt());
                bindVariableService.setShort(1, rnd.nextShort());
                bindVariableService.setByte(2, rnd.nextByte());
                bindVariableService.setDouble(3, rnd.nextDouble());
                bindVariableService.setFloat(4, rnd.nextFloat());
                bindVariableService.setLong(5, rnd.nextLong());
                bindVariableService.setStr(6, rnd.nextChars(6));
                bindVariableService.setStr(7, rnd.nextChars(1));
                bindVariableService.setBoolean(8, rnd.nextBoolean());
                rnd.nextBytes(blob);
                bindVariableService.setBin(9, bs);
                bindVariableService.setDate(10, rnd.nextLong());
                bindVariableService.setLong256(11, rnd.nextLong(), rnd.nextLong(), rnd.nextLong(), rnd.nextLong());
                bindVariableService.setChar(12, rnd.nextChar());
                bindVariableService.setTimestamp(13, timestampFunction.getTimestamp());
                method.execute();
            }
            method.commit();
        }
        rnd.reset();
        try (TableReader reader = engine.getReader(sqlExecutionContext.getCairoSecurityContext(), "all2")) {
            final TableReaderRecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                Assert.assertEquals(rnd.nextInt(), record.getInt(0));
                Assert.assertEquals(rnd.nextShort(), record.getShort(1));
                Assert.assertEquals(rnd.nextByte(), record.getByte(2));
                Assert.assertEquals(rnd.nextDouble(), record.getDouble(3), 0.0001);
                Assert.assertEquals(rnd.nextFloat(), record.getFloat(4), 0.000001);
                Assert.assertEquals(rnd.nextLong(), record.getLong(5));
                TestUtils.assertEquals(rnd.nextChars(6), record.getStr(6));
                TestUtils.assertEquals(rnd.nextChars(1), record.getSym(7));
                Assert.assertEquals(rnd.nextBoolean(), record.getBool(8));
                rnd.nextBytes(blob);
                BinarySequence binarySequence = record.getBin(9);
                Assert.assertEquals(blob.length, binarySequence.length());
                for (int j = 0, m = blob.length; j < m; j++) {
                    Assert.assertEquals(blob[j], binarySequence.byteAt(j));
                }
                Assert.assertEquals(rnd.nextLong(), record.getDate(10));
                Long256 long256 = record.getLong256A(11);
                Assert.assertEquals(rnd.nextLong(), long256.getLong0());
                Assert.assertEquals(rnd.nextLong(), long256.getLong1());
                Assert.assertEquals(rnd.nextLong(), long256.getLong2());
                Assert.assertEquals(rnd.nextLong(), long256.getLong3());
                Assert.assertEquals(rnd.nextChar(), record.getChar(12));
            }
        }
    });
}
Also used : TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) BinarySequence(io.questdb.std.BinarySequence) TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) Rnd(io.questdb.std.Rnd) Long256(io.questdb.std.Long256)

Aggregations

Rnd (io.questdb.std.Rnd)76 Test (org.junit.Test)49 Record (io.questdb.cairo.sql.Record)32 RecordCursor (io.questdb.cairo.sql.RecordCursor)29 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)28 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)24 TableWriter (io.questdb.cairo.TableWriter)23 Path (io.questdb.std.str.Path)10 RuntimeIntervalModel (io.questdb.griffin.model.RuntimeIntervalModel)4 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 DataFrameCursor (io.questdb.cairo.sql.DataFrameCursor)2 ReaderOutOfDateException (io.questdb.cairo.sql.ReaderOutOfDateException)2 RowCursor (io.questdb.cairo.sql.RowCursor)2 StaticSymbolTable (io.questdb.cairo.sql.StaticSymbolTable)2 LineTcpSender (io.questdb.cutlass.line.LineTcpSender)2 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)2 AbstractFunctionFactoryTest (io.questdb.griffin.engine.AbstractFunctionFactoryTest)2 BinarySequence (io.questdb.std.BinarySequence)2 BytecodeAssembler (io.questdb.std.BytecodeAssembler)2 FilesFacade (io.questdb.std.FilesFacade)2