Search in sources :

Example 16 with RecordCursorFactory

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

the class KeyedAggregationTest method testIntSymbolSumTimeRange.

@Test
public void testIntSymbolSumTimeRange() throws Exception {
    assertMemoryLeak(() -> {
        compiler.compile("create table tab as (select rnd_symbol('s1','s2','s3', null) s1, rnd_double(2) val, timestamp_sequence(0, 1000000) t from long_sequence(1000000)) timestamp(t) partition by DAY", sqlExecutionContext);
        compiler.compile("alter table tab add column s2 symbol cache", sqlExecutionContext);
        compiler.compile("insert into tab select rnd_symbol('s1','s2','s3', null), rnd_double(2), timestamp_sequence(cast('1970-01-13T00:00:00.000000Z' as timestamp), 1000000), rnd_symbol('a1','a2','a3', null) s2 from long_sequence(1000000)", sqlExecutionContext);
        // test with key falling within null columns
        try (RecordCursorFactory factory = compiler.compile("select s2, sum(val) from tab order by s2", sqlExecutionContext).getRecordCursorFactory()) {
            Record[] expected = new Record[] { new Record() {

                @Override
                public CharSequence getSym(int col) {
                    return null;
                }

                @Override
                public double getDouble(int col) {
                    return 520447.6629968692;
                }
            }, new Record() {

                @Override
                public CharSequence getSym(int col) {
                    return "a1";
                }

                @Override
                public double getDouble(int col) {
                    return 104308.65839619662;
                }
            }, new Record() {

                @Override
                public CharSequence getSym(int col) {
                    return "a2";
                }

                @Override
                public double getDouble(int col) {
                    return 104559.28674751727;
                }
            }, new Record() {

                @Override
                public CharSequence getSym(int col) {
                    return "a3";
                }

                @Override
                public double getDouble(int col) {
                    return 104044.11326997768;
                }
            } };
            assertCursorRawRecords(expected, factory, false, true);
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) Record(io.questdb.cairo.sql.Record) Test(org.junit.Test)

Example 17 with RecordCursorFactory

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

the class SqlCodeGeneratorTest method testBindVariableInWhere.

@Test
public void testBindVariableInWhere() throws Exception {
    assertMemoryLeak(() -> {
        final CairoConfiguration configuration = new DefaultCairoConfiguration(root);
        try (CairoEngine engine = new CairoEngine(configuration);
            SqlCompiler compiler = new SqlCompiler(engine)) {
            bindVariableService.clear();
            bindVariableService.setLong(0, 10);
            try (RecordCursorFactory factory = compiler.compile("select x from long_sequence(100) where x = $1", sqlExecutionContext).getRecordCursorFactory()) {
                assertCursor("x\n" + "10\n", factory, true, true, false);
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) Test(org.junit.Test)

Example 18 with RecordCursorFactory

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

the class SqlCodeGeneratorTest method testBindVariableInSelect2.

@Test
public void testBindVariableInSelect2() throws Exception {
    assertMemoryLeak(() -> {
        final CairoConfiguration configuration = new DefaultCairoConfiguration(root);
        try (CairoEngine engine = new CairoEngine(configuration);
            SqlCompiler compiler = new SqlCompiler(engine)) {
            bindVariableService.clear();
            bindVariableService.setLong("y", 10);
            try (RecordCursorFactory factory = compiler.compile("select x, :y from long_sequence(2)", sqlExecutionContext).getRecordCursorFactory()) {
                assertCursor("x\t:y\n" + "1\t10\n" + "2\t10\n", factory, true, true, true);
            }
        }
    });
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) Test(org.junit.Test)

Example 19 with RecordCursorFactory

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

the class SqlCodeGeneratorTest method testLatestByIOFailure.

@Test
public void testLatestByIOFailure() throws Exception {
    assertMemoryLeak(() -> {
        FilesFacade ff = new FilesFacadeImpl() {

            @Override
            public long openRO(LPSZ name) {
                if (Chars.endsWith(name, "b.d")) {
                    return -1;
                }
                return super.openRO(name);
            }
        };
        CairoConfiguration configuration = new DefaultCairoConfiguration(root) {

            @Override
            public FilesFacade getFilesFacade() {
                return ff;
            }
        };
        try (CairoEngine engine = new CairoEngine(configuration);
            SqlCompiler compiler = new SqlCompiler(engine)) {
            try {
                compiler.compile(("create table x as " + "(" + "select rnd_double(0)*100 a, rnd_symbol(5,4,4,1) b, timestamp_sequence(0, 100000000000) k from" + " long_sequence(200)" + ") timestamp(k) partition by DAY"), sqlExecutionContext);
                try (final RecordCursorFactory factory = compiler.compile("select * from x latest by b where b = 'PEHN' and a < 22", sqlExecutionContext).getRecordCursorFactory()) {
                    try {
                        assertCursor("a\tb\tk\n" + "5.942010834028\tPEHN\t1970-08-03T02:53:20.000000Z\n", factory, true, true, false);
                        Assert.fail();
                    } catch (CairoException e) {
                        TestUtils.assertContains(e.getFlyweightMessage(), "could not open");
                    }
                }
                Assert.assertEquals(0, engine.getBusyReaderCount());
                Assert.assertEquals(0, engine.getBusyWriterCount());
            } finally {
                engine.clear();
            }
        }
    });
}
Also used : FilesFacade(io.questdb.std.FilesFacade) RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) LPSZ(io.questdb.std.str.LPSZ) FilesFacadeImpl(io.questdb.std.FilesFacadeImpl) Test(org.junit.Test)

Example 20 with RecordCursorFactory

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

the class O3Test method testAppendOrderStability.

private static void testAppendOrderStability(CairoEngine engine, SqlCompiler compiler, SqlExecutionContext sqlExecutionContext) throws SqlException {
    compiler.compile("create table x (" + "seq long, " + "sym symbol, " + "ts timestamp" + "), index(sym) timestamp (ts) partition by DAY", sqlExecutionContext);
    String[] symbols = { "AA", "BB" };
    long[] seq = new long[symbols.length];
    // insert some records in order
    final Rnd rnd = new Rnd();
    try (TableWriter w = engine.getWriter(sqlExecutionContext.getCairoSecurityContext(), "x", "testing")) {
        long t = 0;
        for (int i = 0; i < 1000; i++) {
            TableWriter.Row r = w.newRow(t++);
            int index = rnd.nextInt(1);
            r.putLong(0, seq[index]++);
            r.putSym(1, symbols[index]);
            r.append();
        }
        w.commitWithLag();
        // now do out of order
        for (int i = 0; i < 100_000; i++) {
            TableWriter.Row r;
            // symbol 0
            r = w.newRow(t + 1);
            r.putLong(0, seq[0]++);
            r.putSym(1, symbols[0]);
            r.append();
            r = w.newRow(t + 1);
            r.putLong(0, seq[0]++);
            r.putSym(1, symbols[0]);
            r.append();
            // symbol 1
            r = w.newRow(t);
            r.putLong(0, seq[1]++);
            r.putSym(1, symbols[1]);
            r.append();
            r = w.newRow(t);
            r.putLong(0, seq[1]++);
            r.putSym(1, symbols[1]);
            r.append();
            t += 2;
        }
        w.commit();
    }
    // now verify that sequence did not get mixed up in the table
    long[] actualSeq = new long[symbols.length];
    try (RecordCursorFactory f = compiler.compile("x", sqlExecutionContext).getRecordCursorFactory();
        RecordCursor cursor = f.getCursor(sqlExecutionContext)) {
        final Record record = cursor.getRecord();
        while (cursor.hasNext()) {
            int index = record.getInt(1);
            Assert.assertEquals(record.getLong(0), actualSeq[index]++);
        }
    }
}
Also used : RecordCursorFactory(io.questdb.cairo.sql.RecordCursorFactory) RecordCursor(io.questdb.cairo.sql.RecordCursor) Record(io.questdb.cairo.sql.Record)

Aggregations

RecordCursorFactory (io.questdb.cairo.sql.RecordCursorFactory)136 Test (org.junit.Test)118 RecordCursor (io.questdb.cairo.sql.RecordCursor)108 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)89 Record (io.questdb.cairo.sql.Record)79 TableWriter (io.questdb.cairo.TableWriter)68 Rnd (io.questdb.std.Rnd)24 SqlCompiler (io.questdb.griffin.SqlCompiler)8 SqlExecutionContextImpl (io.questdb.griffin.SqlExecutionContextImpl)6 ReaderOutOfDateException (io.questdb.cairo.sql.ReaderOutOfDateException)4 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 FDate (de.invesdwin.util.time.date.FDate)3 FilesFacade (io.questdb.std.FilesFacade)3 FilesFacadeImpl (io.questdb.std.FilesFacadeImpl)3 CairoConfiguration (io.questdb.cairo.CairoConfiguration)2 CairoEngine (io.questdb.cairo.CairoEngine)2 CairoException (io.questdb.cairo.CairoException)2