Search in sources :

Example 1 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class AbstractOptimiserTest method assertRowId.

protected static void assertRowId(String query, String longColumn) throws ParserException {
    RecordSource src = compiler.compile(FACTORY_CONTAINER.getFactory(), query);
    try {
        RecordCursor cursor = src.prepareCursor(FACTORY_CONTAINER.getFactory());
        try {
            int dateIndex = src.getMetadata().getColumnIndex(longColumn);
            HashMap<Long, Long> map = new HashMap<>();
            long count = 0;
            while (cursor.hasNext()) {
                Record record = cursor.next();
                map.put(record.getRowId(), record.getLong(dateIndex));
                count++;
            }
            Assert.assertTrue(count > 0);
            Record record = cursor.newRecord();
            for (Map.Entry<Long, Long> e : map.entrySet()) {
                Assert.assertEquals((long) e.getValue(), cursor.recordAt(e.getKey()).getLong(dateIndex));
                cursor.recordAt(record, e.getKey());
                Assert.assertEquals((long) e.getValue(), record.getLong(dateIndex));
            }
        } finally {
            cursor.releaseCursor();
        }
    } finally {
        Misc.free(src);
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) RecordCursor(com.questdb.common.RecordCursor) HashMap(java.util.HashMap) Record(com.questdb.common.Record) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class SQLExamples method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLExamples <path>");
        System.exit(1);
    }
    try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLExamples.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try (RecordSource rs = compiler.compile(factory, "'movies.csv'")) {
            // Execute query and fetch results
            RecordCursor cursor = rs.prepareCursor(factory);
            try {
                while (cursor.hasNext()) {
                    Record record = cursor.next();
                }
            } finally {
                cursor.releaseCursor();
            }
        }
        // to simplify query demonstration we have generic record source printer
        RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
        printer.print(compiler.compile(factory, "'movies.csv'"), factory);
        System.out.println("---------");
        // find movie by ID
        printer.print(compiler.compile(factory, "'movies.csv' where movieId = 62198"), factory);
        System.out.println("---------");
        // extract year from movie title
        printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' where movieId = 62198"), factory);
        System.out.println("---------");
        // order by movie year descending
        printer.print(compiler.compile(factory, "select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc"), factory);
        System.out.println("---------");
        // count titles by year
        printer.print(compiler.compile(factory, "select year, count() from (select title, pluck('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)"), factory);
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) StdoutSink(com.questdb.std.str.StdoutSink) RecordCursor(com.questdb.common.RecordCursor) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) Factory(com.questdb.store.factory.Factory) Record(com.questdb.common.Record) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

Example 3 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class VarAggregator method computeVar.

protected double computeVar(DirectMapValues values) {
    long ref = values.getLong(oPartialHead);
    double total;
    // calculate mean
    double mean;
    if (ref == -1) {
        total = (double) values.getLong(oPartialTotal);
        mean = values.getDouble(oPartialSum) / total;
    } else {
        total = (double) values.getLong(oTotal);
        double count = (double) values.getLong(oPartialTotal);
        mean = (count / total) * (values.getDouble(oPartialSum) / count);
        meanPartials.of(ref);
        while (meanPartials.hasNext()) {
            Record r = meanPartials.next();
            double sum = r.getDouble(0);
            count = r.getLong(1);
            mean += (count / total) * (sum / count);
        }
    }
    // 
    double variance = 0;
    double partialSum = 0;
    long partialCount = 0;
    srcRecords.of(values.getLong(oValuesHead));
    while (srcRecords.hasNext()) {
        Record r = srcRecords.next();
        double d = r.getDouble(0);
        double x = (mean - d) * (mean - d);
        double y = x + partialSum;
        if (y == Double.POSITIVE_INFINITY || y == Double.NEGATIVE_INFINITY) {
            variance += (partialCount / total) * (partialSum / (double) partialCount);
            partialSum = x;
            partialCount = 1;
        } else {
            partialSum = y;
            partialCount++;
        }
    }
    variance += (partialCount / total) * (partialSum / (double) partialCount);
    return variance;
}
Also used : Record(com.questdb.common.Record)

Example 4 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class GenericInteropTest method testObjectWriteGenericRead.

@Test
public void testObjectWriteGenericRead() throws Exception {
    try (JournalWriter<Data> writer = getFactory().writer(new JournalMetadataBuilder<Data>(Data.class, "test") {

        {
            $sym("sym").index();
            $int("id").index();
            $str("rateId").index();
        }
    })) {
        Data d = new Data();
        d.sym = "GBPUSD";
        d.created = 30000;
        d.bid = 0.65;
        d.ask = 0.66;
        d.bidSize = 1000;
        d.askSize = 1100;
        d.id = 1;
        d.status = "OK";
        d.user = "system";
        d.rateId = "GBPUSD:GLOBAL";
        d.active = true;
        d.nullable = null;
        d.ticks = 12345678;
        d.modulo = 425;
        writer.append(d);
        writer.commit();
    }
    try (RecordSource rs = compile("test")) {
        RecordCursor cursor = rs.prepareCursor(getFactory());
        try {
            Record e;
            Assert.assertTrue(cursor.hasNext());
            Assert.assertNotNull(e = cursor.next());
            Assert.assertEquals("GBPUSD", e.getSym(0));
            Assert.assertEquals(30000, e.getDate(1));
            Assert.assertEquals(0.65, e.getDouble(2), 0.000001);
            Assert.assertEquals(0.66, e.getDouble(3), 0.000001);
            Assert.assertEquals(1000, e.getInt(4));
            Assert.assertEquals(1100, e.getInt(5));
            Assert.assertEquals(1, e.getByte(6));
            TestUtils.assertEquals("OK", e.getFlyweightStr(7));
            TestUtils.assertEquals("system", e.getFlyweightStr(8));
            TestUtils.assertEquals("GBPUSD:GLOBAL", e.getFlyweightStr(9));
            Assert.assertTrue(e.getBool(10));
            Assert.assertNull(e.getFlyweightStr(11));
            Assert.assertEquals(12345678, e.getLong(12));
            Assert.assertEquals(425, e.getShort(13));
            Assert.assertFalse(cursor.hasNext());
        } finally {
            cursor.releaseCursor();
        }
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) RecordCursor(com.questdb.common.RecordCursor) Record(com.questdb.common.Record) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 5 with Record

use of com.questdb.common.Record in project questdb by bluestreak01.

the class DDLTests method testCreateAsSelectBin.

@Test
public void testCreateAsSelectBin() throws Exception {
    int N = 10000;
    int SZ = 4096;
    ByteBuffer buf = ByteBuffer.allocateDirect(SZ);
    try {
        long addr = ByteBuffers.getAddress(buf);
        try (JournalWriter w = compiler.createWriter(getFactory(), "create table x (a INT, b BINARY)")) {
            Rnd rnd = new Rnd();
            for (int i = 0; i < N; i++) {
                long p = addr;
                int n = (rnd.nextPositiveInt() % (SZ - 1)) / 8;
                for (int j = 0; j < n; j++) {
                    Unsafe.getUnsafe().putLong(p, rnd.nextLong());
                    p += 8;
                }
                buf.limit(n * 8);
                JournalEntryWriter ew = w.entryWriter();
                ew.putInt(0, i);
                ew.putBin(1, buf);
                ew.append();
                buf.clear();
            }
            w.commit();
        }
        exec("create table y as (x)");
        int count = 0;
        try (RecordSource rs = compiler.compile(getFactory(), "y")) {
            RecordCursor cursor = rs.prepareCursor(getFactory());
            try {
                Rnd rnd = new Rnd();
                while (cursor.hasNext()) {
                    Record rec = cursor.next();
                    Assert.assertEquals(count, rec.getInt(0));
                    long len = rec.getBinLen(1);
                    DirectInputStream is = rec.getBin(1);
                    is.copyTo(addr, 0, len);
                    long p = addr;
                    int n = (rnd.nextPositiveInt() % (SZ - 1)) / 8;
                    for (int j = 0; j < n; j++) {
                        Assert.assertEquals(rnd.nextLong(), Unsafe.getUnsafe().getLong(p));
                        p += 8;
                    }
                    count++;
                }
            } finally {
                cursor.releaseCursor();
            }
        }
        Assert.assertEquals(N, count);
    } finally {
        ByteBuffers.release(buf);
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) RecordCursor(com.questdb.common.RecordCursor) Record(com.questdb.common.Record) ByteBuffer(java.nio.ByteBuffer) JournalEntryWriter(com.questdb.store.JournalEntryWriter) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

Record (com.questdb.common.Record)29 RecordCursor (com.questdb.common.RecordCursor)19 Test (org.junit.Test)13 RecordSource (com.questdb.ql.RecordSource)10 JournalWriter (com.questdb.store.JournalWriter)5 AbstractTest (com.questdb.test.tools.AbstractTest)5 FakeRecord (com.questdb.ql.join.hash.FakeRecord)4 JournalEntryWriter (com.questdb.store.JournalEntryWriter)4 NullableRecord (com.questdb.ql.NullableRecord)3 Rnd (com.questdb.std.Rnd)3 SymbolTable (com.questdb.common.SymbolTable)2 AbstractOptimiserTest (com.questdb.parser.sql.AbstractOptimiserTest)2 LongList (com.questdb.std.LongList)2 RedBlackTree (com.questdb.std.RedBlackTree)2 ArrayList (java.util.ArrayList)2 RecordCursorFactory (com.questdb.cairo.sql.RecordCursorFactory)1 QueryCompiler (com.questdb.parser.sql.QueryCompiler)1 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)1 AsOfPartitionedJoinRecordSource (com.questdb.ql.join.AsOfPartitionedJoinRecordSource)1 BytecodeAssembler (com.questdb.std.BytecodeAssembler)1