Search in sources :

Example 66 with RecordCursor

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

the class SampleByTest method testSampleFillValueAllTypesAndTruncate.

@Test
public void testSampleFillValueAllTypesAndTruncate() throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table x as " + "(" + "select" + " rnd_double(0)*100 a," + " rnd_symbol(5,4,4,1) b," + " rnd_float(0)*100 c," + " abs(rnd_int()) d," + " rnd_short() e," + " rnd_byte(3,10) f," + " rnd_long() g," + " timestamp_sequence(172800000000, 3600000000) k" + " from" + " long_sequence(20)" + ") timestamp(k) partition by NONE", sqlExecutionContext);
        try (final RecordCursorFactory factory = compiler.compile("select b, sum(a), sum(c), sum(d), sum(e), sum(f), sum(g), k from x sample by 3h fill(20.56, 0, 0, 0, 0, 0)", sqlExecutionContext).getRecordCursorFactory()) {
            assertTimestamp("k", factory);
            String expected = "b\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk\n" + "\t74.19752505948932\t113.1213\t2557447177\t868\t12\t-6307312481136788016\t1970-01-03T00:00:00.000000Z\n" + "CPSW\t0.35983672154330515\t76.7567\t113506296\t27809\t9\t-8889930662239044040\t1970-01-03T00:00:00.000000Z\n" + "PEHN\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T00:00:00.000000Z\n" + "RXGZ\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T00:00:00.000000Z\n" + "HYRX\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T00:00:00.000000Z\n" + "\t76.64256753596138\t55.2249\t326010667\t-5741\t8\t7392877322819819290\t1970-01-03T03:00:00.000000Z\n" + "CPSW\t13.450170570900255\t34.3569\t410717394\t18229\t10\t6820495939660535106\t1970-01-03T03:00:00.000000Z\n" + "PEHN\t15.786635599554755\t12.5030\t264240638\t-7976\t6\t-8480005421611953360\t1970-01-03T03:00:00.000000Z\n" + "RXGZ\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T03:00:00.000000Z\n" + "HYRX\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T03:00:00.000000Z\n" + "\t85.05940141744613\t92.1608\t301655269\t-14676\t12\t-2937111954994403426\t1970-01-03T06:00:00.000000Z\n" + "CPSW\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T06:00:00.000000Z\n" + "PEHN\t86.64158914718531\t88.3742\t1566901076\t-3017\t3\t-5028301966399563827\t1970-01-03T06:00:00.000000Z\n" + "RXGZ\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T06:00:00.000000Z\n" + "HYRX\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T06:00:00.000000Z\n" + "\t106.78118249687527\t103.1198\t3029605432\t-2372\t12\t-1162868573414266742\t1970-01-03T09:00:00.000000Z\n" + "CPSW\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T09:00:00.000000Z\n" + "PEHN\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T09:00:00.000000Z\n" + "RXGZ\t3.831785863680992\t42.0204\t1254404167\t1756\t5\t8702525427024484485\t1970-01-03T09:00:00.000000Z\n" + "HYRX\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T09:00:00.000000Z\n" + "\t117.60937843256664\t189.8173\t3717804370\t-27064\t17\t2215137494070785317\t1970-01-03T12:00:00.000000Z\n" + "CPSW\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T12:00:00.000000Z\n" + "PEHN\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T12:00:00.000000Z\n" + "RXGZ\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T12:00:00.000000Z\n" + "HYRX\t24.008362859107102\t76.5784\t2111250190\t-13252\t8\t7973684666911773753\t1970-01-03T12:00:00.000000Z\n" + "\t28.087836621126815\t139.3070\t2587989045\t11751\t17\t-8594661640328306402\t1970-01-03T15:00:00.000000Z\n" + "CPSW\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T15:00:00.000000Z\n" + "PEHN\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T15:00:00.000000Z\n" + "RXGZ\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T15:00:00.000000Z\n" + "HYRX\t2.6836863013701473\t10.6430\t502711083\t-8221\t9\t-7709579215942154242\t1970-01-03T15:00:00.000000Z\n" + "\t75.17160551750754\t120.5189\t2362241402\t514\t11\t-2863260545700031392\t1970-01-03T18:00:00.000000Z\n" + "CPSW\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T18:00:00.000000Z\n" + "PEHN\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T18:00:00.000000Z\n" + "RXGZ\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T18:00:00.000000Z\n" + "HYRX\t20.56\t0.0000\t0\t0\t0\t0\t1970-01-03T18:00:00.000000Z\n";
            assertCursor(expected, factory, false, true, false, false);
            // make sure we get the same outcome when we get factory to create new cursor
            assertCursor(expected, factory, false, true, false, false);
            // make sure strings, binary fields and symbols are compliant with expected record behaviour
            assertVariableColumns(factory, true);
            compiler.compile("truncate table x", sqlExecutionContext);
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                sink.clear();
                printer.print(cursor, factory.getMetadata(), true, sink);
                TestUtils.assertEquals("b\tsum\tsum1\tsum2\tsum3\tsum4\tsum5\tk\n", sink);
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Test(org.junit.Test) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest)

Example 67 with RecordCursor

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

the class DataFrameRecordCursorFactoryTest method testFactory.

@Test
public void testFactory() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        final int N = 100;
        // separate two symbol columns with primitive. It will make problems apparent if index does not shift correctly
        try (TableModel model = new TableModel(configuration, "x", PartitionBy.DAY).col("a", ColumnType.STRING).col("b", ColumnType.SYMBOL).indexed(true, N / 4).col("i", ColumnType.INT).col("c", ColumnType.SYMBOL).indexed(true, N / 4).timestamp()) {
            CairoTestUtils.create(model);
        }
        final Rnd rnd = new Rnd();
        final String[] symbols = new String[N];
        final int M = 1000;
        final long increment = 1000000 * 60L * 4;
        for (int i = 0; i < N; i++) {
            symbols[i] = rnd.nextChars(8).toString();
        }
        rnd.reset();
        // prepare the data
        long timestamp = 0;
        try (TableWriter writer = new TableWriter(configuration, "x")) {
            for (int i = 0; i < M; i++) {
                TableWriter.Row row = writer.newRow(timestamp += increment);
                row.putStr(0, rnd.nextChars(20));
                row.putSym(1, symbols[rnd.nextPositiveInt() % N]);
                row.putInt(2, rnd.nextInt());
                row.putSym(3, symbols[rnd.nextPositiveInt() % N]);
                row.append();
            }
            writer.commit();
        }
        try (CairoEngine engine = new CairoEngine(configuration)) {
            String value = symbols[N - 10];
            int columnIndex;
            int symbolKey;
            RecordMetadata metadata;
            try (TableReader reader = engine.getReader(AllowAllCairoSecurityContext.INSTANCE, "x", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION)) {
                columnIndex = reader.getMetadata().getColumnIndexQuiet("b");
                symbolKey = reader.getSymbolMapReader(columnIndex).keyOf(value);
                metadata = GenericRecordMetadata.copyOf(reader.getMetadata());
            }
            SymbolIndexRowCursorFactory symbolIndexRowCursorFactory = new SymbolIndexRowCursorFactory(columnIndex, symbolKey, true, BitmapIndexReader.DIR_FORWARD, null);
            FullFwdDataFrameCursorFactory dataFrameFactory = new FullFwdDataFrameCursorFactory(engine, "x", TableUtils.ANY_TABLE_ID, TableUtils.ANY_TABLE_VERSION);
            // entity index
            final IntList columnIndexes = new IntList();
            for (int i = 0, n = metadata.getColumnCount(); i < n; i++) {
                columnIndexes.add(i);
            }
            DataFrameRecordCursorFactory factory = new DataFrameRecordCursorFactory(metadata, dataFrameFactory, symbolIndexRowCursorFactory, false, null, false, columnIndexes, null);
            SqlExecutionContext sqlExecutionContext = new SqlExecutionContextImpl(engine, 1).with(AllowAllCairoSecurityContext.INSTANCE, null, null, -1, null);
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                Record record = cursor.getRecord();
                while (cursor.hasNext()) {
                    TestUtils.assertEquals(value, record.getSym(1));
                }
            }
        }
    });
}
Also used : RecordCursor(io.questdb.cairo.sql.RecordCursor) Rnd(io.questdb.std.Rnd) IntList(io.questdb.std.IntList) RecordMetadata(io.questdb.cairo.sql.RecordMetadata) SqlExecutionContextImpl(io.questdb.griffin.SqlExecutionContextImpl) SqlExecutionContext(io.questdb.griffin.SqlExecutionContext) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 68 with RecordCursor

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

the class LikeFunctionFactoryTest method testEmptyLikeString.

@Test
public void testEmptyLikeString() throws Exception {
    assertMemoryLeak(() -> {
        String sql = "create table x as (\n" + "select cast('ABCGE' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('SBDHDJ' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('BDGDGGG' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('AAAAVVV' as string) as name from long_sequence(1)\n" + ")";
        compiler.compile(sql, sqlExecutionContext);
        try (RecordCursorFactory factory = compiler.compile("select * from x where name like ''", sqlExecutionContext).getRecordCursorFactory()) {
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                sink.clear();
                printer.print(cursor, factory.getMetadata(), false, sink);
                Assert.assertEquals(sink.toString(), "");
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 69 with RecordCursor

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

the class LikeFunctionFactoryTest method testLikeStringPercentageAtStart.

@Test
public void testLikeStringPercentageAtStart() throws Exception {
    assertMemoryLeak(() -> {
        String sql = "create table x as (\n" + "select cast('ABCGE' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('SBDHDJ' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('BDGDGGG' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('AAAAVVV' as string) as name from long_sequence(1)\n" + ")";
        compiler.compile(sql, sqlExecutionContext);
        try (RecordCursorFactory factory = compiler.compile("select * from x where name like '%GGG'", sqlExecutionContext).getRecordCursorFactory()) {
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                sink.clear();
                printer.print(cursor, factory.getMetadata(), false, sink);
                Assert.assertEquals(sink.toString().replace("\n", ""), "BDGDGGG");
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 70 with RecordCursor

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

the class LikeFunctionFactoryTest method testLikeStringUnderscoreAndPercentage.

@Test
public void testLikeStringUnderscoreAndPercentage() throws Exception {
    assertMemoryLeak(() -> {
        String sql = "create table x as (\n" + "select cast('ABCGE' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('SBDHDJ' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('BDGDGGG' as string) as name from long_sequence(1)\n" + "union\n" + "select cast('AAAAVVV' as string) as name from long_sequence(1)\n" + ")";
        compiler.compile(sql, sqlExecutionContext);
        try (RecordCursorFactory factory = compiler.compile("select * from x where name like '_B%'", sqlExecutionContext).getRecordCursorFactory()) {
            try (RecordCursor cursor = factory.getCursor(sqlExecutionContext)) {
                sink.clear();
                printer.print(cursor, factory.getMetadata(), false, sink);
                Assert.assertEquals(sink.toString().split("\n").length, 2);
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

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