Search in sources :

Example 1 with TestBinarySequence

use of io.questdb.griffin.engine.TestBinarySequence in project questdb by bluestreak01.

the class MemoryCARWImplTest method testBinSequence0.

static void testBinSequence0(long mem1Size, long mem2Size) {
    Rnd rnd = new Rnd();
    int n = 999;
    final TestBinarySequence binarySequence = new TestBinarySequence();
    final byte[] buffer = new byte[600];
    final long bufAddr = Unsafe.malloc(buffer.length, MemoryTag.NATIVE_DEFAULT);
    binarySequence.of(buffer);
    try (MemoryARW mem = new MemoryCARWImpl(mem1Size, Integer.MAX_VALUE, MemoryTag.NATIVE_DEFAULT)) {
        Assert.assertEquals(Numbers.ceilPow2(mem1Size), mem.size());
        long offset1 = 8;
        for (int i = 0; i < n; i++) {
            long o;
            if (rnd.nextPositiveInt() % 16 == 0) {
                o = mem.putBin(null);
                Assert.assertEquals(offset1, o);
                offset1 += 8;
                continue;
            }
            int sz = buffer.length;
            for (int j = 0; j < sz; j++) {
                buffer[j] = rnd.nextByte();
                Unsafe.getUnsafe().putByte(bufAddr + j, buffer[j]);
            }
            o = mem.putBin(binarySequence);
            Assert.assertEquals(offset1 + sz, o);
            offset1 += 8 + sz;
            o = mem.putBin(bufAddr, sz);
            Assert.assertEquals(offset1 + sz, o);
            offset1 += 8 + sz;
        }
        try (MemoryARW mem2 = new MemoryCARWImpl(mem2Size, Integer.MAX_VALUE, MemoryTag.NATIVE_DEFAULT)) {
            Assert.assertEquals(Numbers.ceilPow2(mem2Size), mem2.size());
            offset1 = 0;
            for (int i = 0; i < n; i++) {
                BinarySequence sequence = mem.getBin(offset1);
                if (sequence == null) {
                    offset1 += 8;
                } else {
                    offset1 += 2 * (sequence.length() + 8);
                }
                mem2.putBin(sequence);
            }
            offset1 = 0;
            long offset2 = 0;
            // compare
            for (int i = 0; i < n; i++) {
                BinarySequence sequence1 = mem.getBin(offset1);
                BinarySequence sequence2 = mem2.getBin(offset2);
                if (sequence1 == null) {
                    assertNull(sequence2);
                    Assert.assertEquals(TableUtils.NULL_LEN, mem2.getBinLen(offset2));
                    offset1 += 8;
                    offset2 += 8;
                } else {
                    assertNotNull(sequence2);
                    assertEquals(mem.getBinLen(offset1), mem2.getBinLen(offset2));
                    assertEquals(sequence1.length(), sequence2.length());
                    for (long l = 0, len = sequence1.length(); l < len; l++) {
                        assertEquals(sequence1.byteAt(l), sequence2.byteAt(l));
                    }
                    offset1 += sequence1.length() + 8;
                    sequence1 = mem.getBin(offset1);
                    assertNotNull(sequence1);
                    assertEquals(sequence1.length(), sequence2.length());
                    for (long l = 0, len = sequence1.length(); l < len; l++) {
                        assertEquals(sequence1.byteAt(l), sequence2.byteAt(l));
                    }
                    offset1 += sequence1.length() + 8;
                    offset2 += sequence1.length() + 8;
                }
            }
        }
    }
    Unsafe.free(bufAddr, buffer.length, MemoryTag.NATIVE_DEFAULT);
}
Also used : TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) MemoryARW(io.questdb.cairo.vm.api.MemoryARW)

Example 2 with TestBinarySequence

use of io.questdb.griffin.engine.TestBinarySequence in project questdb by bluestreak01.

the class MemoryCARWImplTest method testNullBin0.

static void testNullBin0(MemoryARW mem) {
    final TestBinarySequence binarySequence = new TestBinarySequence();
    final byte[] buf = new byte[0];
    binarySequence.of(buf);
    mem.putBin(null);
    mem.putBin(0, 0);
    long o1 = mem.putBin(binarySequence);
    mem.putNullBin();
    assertNull(mem.getBin(0));
    assertNull(mem.getBin(8));
    BinarySequence bsview = mem.getBin(16);
    assertNotNull(bsview);
    assertEquals(0, bsview.length());
    assertNull(mem.getBin(o1));
}
Also used : TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence)

Example 3 with TestBinarySequence

use of io.questdb.griffin.engine.TestBinarySequence in project questdb by bluestreak01.

the class BindVariablesTest method testBinIndexed.

@Test
public void testBinIndexed() throws SqlException {
    Rnd rnd = new Rnd();
    TestBinarySequence sequence = new TestBinarySequence();
    sequence.of(rnd.nextBytes(256));
    bindVariableService.setBin(1, null);
    bindVariableService.setBin(0, sequence);
    Function func = expr("to_char($1)").withFunction(new ToCharBinFunctionFactory()).withFunction(new ToStrTimestampFunctionFactory()).$();
    func.init(null, sqlExecutionContext);
    Function func2 = expr("length($1)").withFunction(new LengthBinFunctionFactory()).$();
    func2.init(null, sqlExecutionContext);
    TestUtils.assertEquals("00000000 56 54 4a 57 43 50 53 57 48 59 52 58 50 45 48 4e\n" + "00000010 52 58 47 5a 53 58 55 58 49 42 42 54 47 50 47 57\n" + "00000020 46 46 59 55 44 45 59 59 51 45 48 42 48 46 4f 57\n" + "00000030 4c 50 44 58 59 53 42 45 4f 55 4f 4a 53 48 52 55\n" + "00000040 45 44 52 51 51 55 4c 4f 46 4a 47 45 54 4a 52 53\n" + "00000050 5a 53 52 59 52 46 42 56 54 4d 48 47 4f 4f 5a 5a\n" + "00000060 56 44 5a 4a 4d 59 49 43 43 58 5a 4f 55 49 43 57\n" + "00000070 45 4b 47 48 56 55 56 53 44 4f 54 53 45 44 59 59\n" + "00000080 43 54 47 51 4f 4c 59 58 57 43 4b 59 4c 53 55 57\n" + "00000090 44 53 57 55 47 53 48 4f 4c 4e 56 54 49 51 42 5a\n" + "000000a0 58 49 4f 56 49 4b 4a 53 4d 53 53 55 51 53 52 4c\n" + "000000b0 54 4b 56 56 53 4a 4f 4a 49 50 48 5a 45 50 49 48\n" + "000000c0 56 4c 54 4f 56 4c 4a 55 4d 4c 47 4c 48 4d 4c 4c\n" + "000000d0 45 4f 59 50 48 52 49 50 5a 49 4d 4e 5a 5a 52 4d\n" + "000000e0 46 4d 42 45 5a 47 48 57 56 44 4b 46 4c 4f 50 4a\n" + "000000f0 4f 58 50 4b 52 47 49 49 48 59 48 42 4f 51 4d 59", func.getStr(builder.getRecord()));
    // check that bin bind variable length is accurate
    Assert.assertEquals(256, func2.getLong(builder.getRecord()));
    sequence.of(rnd.nextBytes(16));
    bindVariableService.setBin(0, sequence);
    TestUtils.assertEquals("00000000 53 53 4d 50 47 4c 55 4f 48 4e 5a 48 5a 53 51 4c", func.getStr(builder.getRecord()));
    bindVariableService.setBin(0, new TestBinarySequence().of(rnd.nextBytes(24)));
    TestUtils.assertEquals("00000000 44 47 4c 4f 47 49 46 4f 55 53 5a 4d 5a 56 51 45\n" + "00000010 42 4e 44 43 51 43 45 48", func.getStr(builder.getRecord()));
}
Also used : ToStrTimestampFunctionFactory(io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory) TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) Function(io.questdb.cairo.sql.Function) Test(org.junit.Test)

Example 4 with TestBinarySequence

use of io.questdb.griffin.engine.TestBinarySequence in project questdb by bluestreak01.

the class BindVariablesTest method testBin.

@Test
public void testBin() throws SqlException {
    Rnd rnd = new Rnd();
    TestBinarySequence sequence = new TestBinarySequence();
    sequence.of(rnd.nextBytes(256));
    bindVariableService.setBin("x", sequence);
    Function func = expr("to_char(:x)").withFunction(new ToCharBinFunctionFactory()).withFunction(new ToStrTimestampFunctionFactory()).$();
    func.init(null, sqlExecutionContext);
    Function func2 = expr("length(:x)").withFunction(new LengthBinFunctionFactory()).$();
    func2.init(null, sqlExecutionContext);
    TestUtils.assertEquals("00000000 56 54 4a 57 43 50 53 57 48 59 52 58 50 45 48 4e\n" + "00000010 52 58 47 5a 53 58 55 58 49 42 42 54 47 50 47 57\n" + "00000020 46 46 59 55 44 45 59 59 51 45 48 42 48 46 4f 57\n" + "00000030 4c 50 44 58 59 53 42 45 4f 55 4f 4a 53 48 52 55\n" + "00000040 45 44 52 51 51 55 4c 4f 46 4a 47 45 54 4a 52 53\n" + "00000050 5a 53 52 59 52 46 42 56 54 4d 48 47 4f 4f 5a 5a\n" + "00000060 56 44 5a 4a 4d 59 49 43 43 58 5a 4f 55 49 43 57\n" + "00000070 45 4b 47 48 56 55 56 53 44 4f 54 53 45 44 59 59\n" + "00000080 43 54 47 51 4f 4c 59 58 57 43 4b 59 4c 53 55 57\n" + "00000090 44 53 57 55 47 53 48 4f 4c 4e 56 54 49 51 42 5a\n" + "000000a0 58 49 4f 56 49 4b 4a 53 4d 53 53 55 51 53 52 4c\n" + "000000b0 54 4b 56 56 53 4a 4f 4a 49 50 48 5a 45 50 49 48\n" + "000000c0 56 4c 54 4f 56 4c 4a 55 4d 4c 47 4c 48 4d 4c 4c\n" + "000000d0 45 4f 59 50 48 52 49 50 5a 49 4d 4e 5a 5a 52 4d\n" + "000000e0 46 4d 42 45 5a 47 48 57 56 44 4b 46 4c 4f 50 4a\n" + "000000f0 4f 58 50 4b 52 47 49 49 48 59 48 42 4f 51 4d 59", func.getStr(builder.getRecord()));
    // check that bin bind variable length is accurate
    Assert.assertEquals(256, func2.getLong(builder.getRecord()));
    sequence.of(rnd.nextBytes(16));
    bindVariableService.setBin("x", sequence);
    TestUtils.assertEquals("00000000 53 53 4d 50 47 4c 55 4f 48 4e 5a 48 5a 53 51 4c", func.getStr(builder.getRecord()));
    bindVariableService.setBin("x", new TestBinarySequence().of(rnd.nextBytes(24)));
    TestUtils.assertEquals("00000000 44 47 4c 4f 47 49 46 4f 55 53 5a 4d 5a 56 51 45\n" + "00000010 42 4e 44 43 51 43 45 48", func.getStr(builder.getRecord()));
}
Also used : ToStrTimestampFunctionFactory(io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory) TestBinarySequence(io.questdb.griffin.engine.TestBinarySequence) Function(io.questdb.cairo.sql.Function) Test(org.junit.Test)

Example 5 with TestBinarySequence

use of io.questdb.griffin.engine.TestBinarySequence 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

TestBinarySequence (io.questdb.griffin.engine.TestBinarySequence)6 Test (org.junit.Test)3 Function (io.questdb.cairo.sql.Function)2 ToStrTimestampFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory)2 MemoryARW (io.questdb.cairo.vm.api.MemoryARW)1 BinarySequence (io.questdb.std.BinarySequence)1 Long256 (io.questdb.std.Long256)1 Rnd (io.questdb.std.Rnd)1 StringSink (io.questdb.std.str.StringSink)1 Random (java.util.Random)1