Search in sources :

Example 1 with RecordCursor

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

the class TableWriterTest method assertGeoStr.

private void assertGeoStr(String hash, int tableBits, long expected) {
    final String tableName = "geo1";
    try (TableModel model = new TableModel(configuration, tableName, PartitionBy.NONE)) {
        model.col("g", ColumnType.getGeoHashTypeWithBits(tableBits));
        CairoTestUtils.createTable(model);
    }
    try (TableWriter writer = new TableWriter(configuration, tableName)) {
        TableWriter.Row r = writer.newRow();
        r.putGeoStr(0, hash);
        r.append();
        writer.commit();
    }
    try (TableReader r = new TableReader(configuration, tableName)) {
        final RecordCursor cursor = r.getCursor();
        final Record record = cursor.getRecord();
        final int type = r.getMetadata().getColumnType(0);
        Assert.assertTrue(cursor.hasNext());
        final long actual;
        switch(ColumnType.tagOf(type)) {
            case ColumnType.GEOBYTE:
                actual = record.getGeoByte(0);
                break;
            case ColumnType.GEOSHORT:
                actual = record.getGeoShort(0);
                break;
            case ColumnType.GEOINT:
                actual = record.getGeoInt(0);
                break;
            default:
                actual = record.getGeoLong(0);
                break;
        }
        Assert.assertEquals(expected, actual);
    }
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record)

Example 2 with RecordCursor

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

the class TableWriterTest method testO3WithCancelRow.

@Test
public void testO3WithCancelRow() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "weather", PartitionBy.DAY).col("windspeed", ColumnType.DOUBLE).timestamp()) {
            CairoTestUtils.create(model);
        }
        long[] tss = new long[] { 631150000000000L, 631152000000000L, 631160000000000L };
        try (TableWriter writer = new TableWriter(configuration, "weather")) {
            TableWriter.Row r = writer.newRow(tss[1]);
            r.putDouble(0, 1.0);
            r.append();
            // Out of order
            r = writer.newRow(tss[0]);
            r.putDouble(0, 2.0);
            r.append();
            r = writer.newRow(tss[2]);
            r.putDouble(0, 3.0);
            r.cancel();
            // Implicit commit
            writer.addColumn("timetocycle", ColumnType.DOUBLE);
            writer.newRow(tss[2]);
            r.putDouble(0, 3.0);
            r.putDouble(2, -1.0);
            r.append();
            writer.commit();
        }
        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, tss[i++], r.getTimestamp(col));
            }
            Assert.assertEquals(tss.length, i);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 3 with RecordCursor

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

the class TableReaderTest method testReload.

private void testReload(int partitionBy, int count, long inct, final int testPartitionSwitch) throws Exception {
    final long increment = inct * 1000;
    CairoTestUtils.createAllTable(configuration, partitionBy);
    TestUtils.assertMemoryLeak(() -> {
        Rnd rnd = new Rnd();
        long ts = TimestampFormatUtils.parseTimestamp("2013-03-04T00:00:00.000Z");
        long blob = allocBlob();
        try {
            try (TableReader reader = new TableReader(configuration, "all")) {
                // can we reload empty table?
                Assert.assertFalse(reader.reload());
                // reader can see all the rows ? Meaning none?
                assertCursor(reader.getCursor(), ts, increment, blob, 0, null);
            }
            try (TableReader reader = new TableReader(configuration, "all")) {
                RecordCursor cursor = reader.getCursor();
                // this combination of reload/iterate/reload is deliberate
                // we make sure that reload() behavior is not affected by
                // iterating empty result set
                Assert.assertFalse(reader.reload());
                assertCursor(cursor, ts, increment, blob, 0, null);
                Assert.assertFalse(reader.reload());
                // create table with first batch populating all columns (there could be null values too)
                long nextTs = testAppend(rnd, configuration, ts, count, increment, blob, 0);
                // can we reload from empty to first batch?
                Assert.assertTrue(reader.reload());
                // make sure we can see first batch right after table is open
                assertCursor(cursor, ts, increment, blob, count, BATCH1_ASSERTER);
                // create another reader to make sure it can load data from constructor
                try (TableReader reader2 = new TableReader(configuration, "all")) {
                    // make sure we can see first batch right after table is open
                    assertCursor(reader2.getCursor(), ts, increment, blob, count, BATCH1_ASSERTER);
                }
                // try to reload when table hasn't changed
                Assert.assertFalse(reader.reload());
                // add second batch to test if reload of open table will pick it up
                nextTs = testAppend(rnd, configuration, nextTs, count, increment, blob, testPartitionSwitch);
                // if we don't reload reader it should still see first batch
                // reader can see all the rows ?
                cursor.toTop();
                assertPartialCursor(cursor, new Rnd(), ts, increment, blob, count / 4, BATCH1_ASSERTER);
                // reload should be successful because we have new data in the table
                Assert.assertTrue(reader.reload());
                // check if we can see second batch after reader was reloaded
                assertCursor(cursor, ts, increment, blob, 2L * count, BATCH1_ASSERTER);
                // reader must be able to cope with that
                try (TableWriter writer = new TableWriter(configuration, "all")) {
                    // this is a bit of paranoid check, but make sure our reader doesn't flinch when new writer is open
                    assertCursor(cursor, ts, increment, blob, 2L * count, BATCH1_ASSERTER);
                    // also make sure that there is nothing to reload, we've not done anything to data after all
                    Assert.assertFalse(reader.reload());
                    // check that we can still see two batches after no-op reload
                    // we rule out possibility of reload() corrupting table state
                    assertCursor(cursor, ts, increment, blob, 2L * count, BATCH1_ASSERTER);
                    // just for no reason add third batch
                    nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH1_GENERATOR);
                    // table must be able to reload now
                    Assert.assertTrue(reader.reload());
                    // and we should see three batches of data
                    assertCursor(cursor, ts, increment, blob, 3L * count, BATCH1_ASSERTER);
                    // this is where things get interesting
                    // add single column
                    writer.addColumn("str2", ColumnType.STRING);
                    // populate table with fourth batch, this time we also populate new column
                    // we expect that values of new column will be NULL for first three batches and non-NULL for fourth
                    nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH2_GENERATOR);
                    // reload table, check if it was positive effort
                    Assert.assertTrue(reader.reload());
                    // two-step assert checks 3/4 rows checking that new column is NUL
                    // the last 1/3 is checked including new column
                    // this is why we need to use same random state and timestamp
                    assertBatch2(count, increment, ts, blob, reader);
                    // good job we got as far as this
                    // lets now add another column and populate fifth batch, including new column
                    // reading this table will ensure tops are preserved
                    writer.addColumn("int2", ColumnType.INT);
                    nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH3_GENERATOR);
                    Assert.assertTrue(reader.reload());
                    assertBatch3(count, increment, ts, blob, reader);
                    // now append more columns that would overflow column buffer and force table to use different
                    // algo when retaining resources
                    writer.addColumn("short2", ColumnType.SHORT);
                    writer.addColumn("bool2", ColumnType.BOOLEAN);
                    writer.addColumn("byte2", ColumnType.BYTE);
                    writer.addColumn("float2", ColumnType.FLOAT);
                    writer.addColumn("double2", ColumnType.DOUBLE);
                    writer.addColumn("sym2", ColumnType.SYMBOL);
                    writer.addColumn("long2", ColumnType.LONG);
                    writer.addColumn("date2", ColumnType.DATE);
                    writer.addColumn("bin2", ColumnType.BINARY);
                    // populate new columns and start asserting batches, which would assert that new columns are
                    // retrospectively "null" in existing records
                    nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH4_GENERATOR);
                    Assert.assertTrue(reader.reload());
                    assertBatch4(count, increment, ts, blob, reader);
                    if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                        reader.closeColumnForRemove("bin2");
                    }
                    writer.removeColumn("bin2");
                    Assert.assertTrue(reader.reload());
                    // and assert that all columns that have not been deleted contain correct values
                    assertBatch5(count, increment, ts, blob, cursor, new Rnd());
                    // append all columns excluding the one we just deleted
                    nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH6_GENERATOR);
                    Assert.assertTrue(reader.reload());
                    // and assert that all columns that have not been deleted contain correct values
                    assertBatch6(count, increment, ts, blob, cursor);
                    if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                        reader.closeColumnForRemove("int");
                    }
                    // remove first column and add new column by same name
                    writer.removeColumn("int");
                    writer.addColumn("int", ColumnType.INT);
                    Assert.assertTrue(reader.reload());
                    assertBatch7(count, increment, ts, blob, cursor);
                    Assert.assertFalse(reader.reload());
                    nextTs = testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH8_GENERATOR);
                    Assert.assertTrue(reader.reload());
                    assertBatch8(count, increment, ts, blob, cursor);
                    if (configuration.getFilesFacade().isRestrictedFileSystem()) {
                        reader.closeColumnForRemove("sym");
                    }
                    writer.removeColumn("sym");
                    writer.addColumn("sym", ColumnType.SYMBOL);
                    Assert.assertTrue(reader.reload());
                    testAppend(writer, rnd, nextTs, count, increment, blob, 0, BATCH9_GENERATOR);
                    Assert.assertTrue(reader.reload());
                    assertBatch9(count, increment, ts, blob, cursor);
                }
            }
        } finally {
            freeBlob(blob);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor)

Example 4 with RecordCursor

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

the class TableReaderTest method testOver2GFile.

@Test
public void testOver2GFile() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.NONE).col("a", ColumnType.LONG)) {
            CairoTestUtils.create(model);
        }
        long N = 280000000;
        Rnd rnd = new Rnd();
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < N; i++) {
                TableWriter.Row r = writer.newRow();
                r.putLong(0, rnd.nextLong());
                r.append();
            }
            writer.commit();
        }
        try (TableReader reader = new TableReader(configuration, "x")) {
            int count = 0;
            rnd.reset();
            RecordCursor cursor = reader.getCursor();
            final Record record = cursor.getRecord();
            while (cursor.hasNext()) {
                Assert.assertEquals(rnd.nextLong(), record.getLong(0));
                count++;
            }
            Assert.assertEquals(N, count);
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 5 with RecordCursor

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

the class TableReaderTest method assertBatch3.

private void assertBatch3(int count, long increment, long ts, long blob, TableReader reader) {
    Rnd exp = new Rnd();
    long ts2;
    RecordCursor cursor = reader.getCursor();
    ts2 = assertPartialCursor(cursor, exp, ts, increment, blob, 3L * count, (r, rnd1, ts1, blob1) -> {
        BATCH1_ASSERTER.assertRecord(r, rnd1, ts1, blob1);
        BATCH2_BEFORE_ASSERTER.assertRecord(r, rnd1, ts1, blob1);
        BATCH3_BEFORE_ASSERTER.assertRecord(r, rnd1, ts1, blob1);
    });
    ts2 = assertPartialCursor(cursor, exp, ts2, increment, blob, count, (r, rnd12, ts12, blob12) -> {
        BATCH2_ASSERTER.assertRecord(r, rnd12, ts12, blob12);
        BATCH3_BEFORE_ASSERTER.assertRecord(r, rnd12, ts12, blob12);
    });
    assertPartialCursor(cursor, exp, ts2, increment, blob, count, BATCH3_ASSERTER);
}
Also used : DateFormat(io.questdb.std.datetime.DateFormat) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test) RecordCursor(io.questdb.cairo.sql.RecordCursor) TimeUnit(java.util.concurrent.TimeUnit) StringSink(io.questdb.std.str.StringSink) CountDownLatch(java.util.concurrent.CountDownLatch) Path(io.questdb.std.str.Path) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestUtils(io.questdb.test.tools.TestUtils) Record(io.questdb.cairo.sql.Record) Timestamps(io.questdb.std.datetime.microtime.Timestamps) TimestampFormatUtils(io.questdb.std.datetime.microtime.TimestampFormatUtils) Assert(org.junit.Assert) io.questdb.std(io.questdb.std) LPSZ(io.questdb.std.str.LPSZ) RecordCursor(io.questdb.cairo.sql.RecordCursor)

Aggregations

RecordCursor (io.questdb.cairo.sql.RecordCursor)174 Test (org.junit.Test)137 Record (io.questdb.cairo.sql.Record)123 RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)108 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)87 TableWriter (io.questdb.cairo.TableWriter)71 Rnd (io.questdb.std.Rnd)29 LPSZ (io.questdb.std.str.LPSZ)10 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)7 SqlCompiler (io.questdb.griffin.SqlCompiler)6 DateFormat (io.questdb.std.datetime.DateFormat)6 Path (io.questdb.std.str.Path)6 StringSink (io.questdb.std.str.StringSink)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 CyclicBarrier (java.util.concurrent.CyclicBarrier)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 SqlException (io.questdb.griffin.SqlException)4 BaseConnection (org.postgresql.core.BaseConnection)4 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)3 Instant (de.invesdwin.util.time.Instant)3