Search in sources :

Example 11 with Record

use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.

the class TableReaderTest method testCharAsString.

@Test
public void testCharAsString() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "char_test", PartitionBy.NONE).col("cc", ColumnType.STRING)) {
            CairoTestUtils.create(model);
        }
        char[] data = { 'a', 'b', 'f', 'g' };
        try (TableWriter writer = new TableWriter(configuration, "char_test")) {
            for (int i = 0, n = data.length; i < n; i++) {
                TableWriter.Row r = writer.newRow();
                r.putStr(0, data[i]);
                r.append();
            }
            writer.commit();
        }
        try (TableReader reader = new TableReader(configuration, "char_test")) {
            final RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            int index = 0;
            while (cursor.hasNext()) {
                Assert.assertTrue(index < data.length);
                CharSequence value = record.getStr(0);
                Assert.assertEquals(1, value.length());
                Assert.assertEquals(data[index], value.charAt(0));
                index++;
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 12 with Record

use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.

the class TableReaderTest method testUnsuccessfulRemoveAndReloadSym.

@Test
public void testUnsuccessfulRemoveAndReloadSym() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).col("b", ColumnType.SYMBOL)) {
            CairoTestUtils.create(model);
        }
        Rnd rnd = new Rnd();
        final int N = 1000;
        // make sure we forbid deleting column "b" files
        TestFilesFacade ff = new TestFilesFacade() {

            int counter = 5;

            @Override
            public boolean remove(LPSZ name) {
                if (counter > 0 && ((Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d") || Chars.endsWith(name, "b.o") || Chars.endsWith(name, "b.k") || Chars.endsWith(name, "b.c") || Chars.endsWith(name, "b.v")))) {
                    counter--;
                    return false;
                }
                return super.remove(name);
            }

            @Override
            public boolean wasCalled() {
                return counter < 1;
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            appendTwoSymbols(writer, rnd);
            writer.commit();
            try (TableReader reader = new TableReader(configuration, "x")) {
                long counter = 0;
                rnd.reset();
                RecordCursor cursor = reader.getCursor();
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getSym(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.removeColumn("b");
                if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                    reader.closeColumnForRemove("b");
                }
                // reader.reload();
                // now when we add new column by same name it must not pick up files we failed to delete previously
                writer.addColumn("b", ColumnType.SYMBOL);
                // SymbolMap must be cleared when we try to do add values to new column
                appendTwoSymbols(writer, rnd);
                writer.commit();
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N * 2, reader.size());
                rnd.reset();
                cursor.toTop();
                counter = 0;
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    if (counter < N) {
                        // roll random generator to make sure it returns same values
                        rnd.nextChars(15);
                        Assert.assertNull(record.getSym(1));
                    } else {
                        Assert.assertEquals(rnd.nextChars(15), record.getSym(1));
                    }
                    counter++;
                }
                Assert.assertEquals(N * 2, counter);
            }
        }
        Assert.assertTrue(ff.wasCalled());
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) LPSZ(io.questdb.std.str.LPSZ) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 13 with Record

use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.

the class TableReaderTest method testUnsuccessfulFileRemoveAndReloadStr.

@Test
public void testUnsuccessfulFileRemoveAndReloadStr() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        // create table with two string columns
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.SYMBOL).col("b", ColumnType.STRING)) {
            CairoTestUtils.create(model);
        }
        Rnd rnd = new Rnd();
        final int N = 1000;
        // make sure we forbid deleting column "b" files
        TestFilesFacade ff = new TestFilesFacade() {

            int counter = 2;

            @Override
            public boolean remove(LPSZ name) {
                if (counter > 0 && ((Chars.endsWith(name, "b.i") || Chars.endsWith(name, "b.d") || Chars.endsWith(name, "b.o") || Chars.endsWith(name, "b.k") || Chars.endsWith(name, "b.c") || Chars.endsWith(name, "b.v")))) {
                    counter--;
                    return false;
                }
                return super.remove(name);
            }

            @Override
            public boolean wasCalled() {
                return counter < 1;
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        // populate table and delete column
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row row = writer.newRow();
                row.putSym(0, rnd.nextChars(10));
                row.putStr(1, rnd.nextChars(15));
                row.append();
            }
            writer.commit();
            try (TableReader reader = new TableReader(configuration, "x")) {
                long counter = 0;
                rnd.reset();
                RecordCursor cursor = reader.getCursor();
                final Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    Assert.assertEquals(rnd.nextChars(15), record.getStr(1));
                    counter++;
                }
                Assert.assertEquals(N, counter);
                // this should write metadata without column "b" but will ignore
                // file delete failures
                writer.removeColumn("b");
                if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                    reader.closeColumnForRemove("b");
                }
                // now when we add new column by same name it must not pick up files we failed to delete previously
                writer.addColumn("b", ColumnType.STRING);
                for (int i = 0; i < N; i++) {
                    TableWriter.Row row = writer.newRow();
                    row.putSym(0, rnd.nextChars(10));
                    row.putStr(1, rnd.nextChars(15));
                    row.append();
                }
                writer.commit();
                // now assert what reader sees
                Assert.assertTrue(reader.reload());
                Assert.assertEquals(N * 2, reader.size());
                rnd.reset();
                cursor.toTop();
                counter = 0;
                while (cursor.hasNext()) {
                    Assert.assertEquals(rnd.nextChars(10), record.getSym(0));
                    if (counter < N) {
                        // roll random generator to make sure it returns same values
                        rnd.nextChars(15);
                        Assert.assertNull(record.getStr(1));
                    } else {
                        Assert.assertEquals(rnd.nextChars(15), record.getStr(1));
                    }
                    counter++;
                }
                Assert.assertEquals(N * 2, counter);
            }
        }
        Assert.assertTrue(ff.wasCalled());
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) LPSZ(io.questdb.std.str.LPSZ) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 14 with Record

use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.

the class TableWriterTest method testO3AferRowCancel.

@Test
public void testO3AferRowCancel() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "weather", PartitionBy.DAY).col("windspeed", ColumnType.DOUBLE).timestamp()) {
            CairoTestUtils.create(model);
        }
        try (TableWriter writer = new TableWriter(configuration, "weather")) {
            TableWriter.Row r;
            r = writer.newRow(IntervalUtils.parseFloorPartialDate("2021-01-31"));
            r.putDouble(0, 1.0);
            r.append();
            // Out of order
            r = writer.newRow(IntervalUtils.parseFloorPartialDate("2021-01-30"));
            r.putDouble(0, 1.0);
            r.cancel();
            // Back in order
            r = writer.newRow(IntervalUtils.parseFloorPartialDate("2021-02-01"));
            r.putDouble(0, 1.0);
            r.append();
            Assert.assertEquals(2, writer.size());
            writer.commit();
        }
        long[] expectedTs = new long[] { IntervalUtils.parseFloorPartialDate("2021-01-31"), IntervalUtils.parseFloorPartialDate("2021-02-01") };
        try (TableReader reader = new TableReader(configuration, "weather")) {
            int col = reader.getMetadata().getColumnIndex("timestamp");
            RecordCursor cursor = reader.getCursor();
            final Record r = cursor.getRecord();
            int i = 0;
            while (cursor.hasNext()) {
                Assert.assertEquals("Row " + i, expectedTs[i++], r.getTimestamp(col));
            }
            Assert.assertEquals(expectedTs.length, i);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 15 with Record

use of io.questdb.cairo.sql.Record in project questdb by bluestreak01.

the class CompactMapTest method assertMap2.

private void assertMap2(Rnd rnd, TestRecord.ArrayBinarySequence binarySequence, int keyColumnOffset, Rnd rnd2, RecordCursor mapCursor) {
    long c = 0;
    rnd.reset();
    rnd2.reset();
    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));
        } 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));
        }
        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);
}
Also used : Record(io.questdb.cairo.sql.Record)

Aggregations

Record (io.questdb.cairo.sql.Record)171 Test (org.junit.Test)130 RecordCursor (io.questdb.cairo.sql.RecordCursor)121 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)79 TableWriter (io.questdb.cairo.TableWriter)70 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)66 Rnd (io.questdb.std.Rnd)32 Function (io.questdb.cairo.sql.Function)28 InStrFunctionFactory (io.questdb.griffin.engine.functions.bool.InStrFunctionFactory)16 NotFunctionFactory (io.questdb.griffin.engine.functions.bool.NotFunctionFactory)15 OrFunctionFactory (io.questdb.griffin.engine.functions.bool.OrFunctionFactory)15 IntList (io.questdb.std.IntList)15 CastStrToGeoHashFunctionFactory (io.questdb.griffin.engine.functions.cast.CastStrToGeoHashFunctionFactory)14 ToStrDateFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrDateFunctionFactory)14 ToStrTimestampFunctionFactory (io.questdb.griffin.engine.functions.date.ToStrTimestampFunctionFactory)14 LengthStrFunctionFactory (io.questdb.griffin.engine.functions.str.LengthStrFunctionFactory)14 LengthSymbolFunctionFactory (io.questdb.griffin.engine.functions.str.LengthSymbolFunctionFactory)14 ToCharBinFunctionFactory (io.questdb.griffin.engine.functions.str.ToCharBinFunctionFactory)14 CursorDereferenceFunctionFactory (io.questdb.griffin.engine.functions.catalogue.CursorDereferenceFunctionFactory)13 SwitchFunctionFactory (io.questdb.griffin.engine.functions.conditional.SwitchFunctionFactory)13