Search in sources :

Example 6 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class AbstractOptimiserTest method assertThat0.

private void assertThat0(String expected, String query, boolean header) throws ParserException, IOException {
    RecordSource rs = cache.peek(query);
    if (rs == null) {
        cache.put(query, rs = compiler.compile(FACTORY_CONTAINER.getFactory(), query));
    }
    assertThat(expected, rs, header);
}
Also used : RecordSource(com.questdb.ql.RecordSource)

Example 7 with RecordSource

use of com.questdb.ql.RecordSource 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 8 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class JoinQueryTest method testJoinNoRowid.

@Test
public void testJoinNoRowid() throws Exception {
    final String expected = "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t104281903\t100\t1138\tKWK\t2015-07-10T00:00:14.518Z\tFBLGGTZEN\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1191623531\t100\t1210\tSR\t2015-07-10T00:00:18.175Z\tYM\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1662742408\t100\t1828\tQH\t2015-07-10T00:00:19.509Z\tEYBI\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t220389\t100\t1293\tDGEEWB\t2015-07-10T00:00:56.196Z\tEYBI\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t798408721\t100\t803\tZIHLGS\t2015-07-10T00:00:56.977Z\tVTNNKVOLHLLNN\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t966974434\t100\t870\tJEOBQ\t2015-07-10T00:00:57.981Z\tW\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t258318715\t100\t1036\tOPWOGS\t2015-07-10T00:01:00.608Z\tEYBI\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1528068156\t100\t400\tYBQE\t2015-07-10T00:01:20.643Z\tQXOLEEXZ\n" + "100\tPJFSREKEUNMKWOF\tUVKWCCVTJSKMXVEGPIG\t\tVMY\tRT\tEYYPDVRGRQG\t2015-07-10T00:00:00.100Z\t1935884354\t100\t1503\tD\t2015-07-10T00:01:43.507Z\tRZVZJQRNYSRKZSJ\n";
    final RecordSource m = compileSource("customers where customerName ~ 'PJFSREKEUNMKWOF'");
    final RecordSource s = new NoRowIdRecordSource().of(compileSource("orders"));
    RecordSource r = new HashJoinRecordSource(m, new IntList() {

        {
            add(m.getMetadata().getColumnIndex("customerId"));
        }
    }, s, new IntList() {

        {
            add(s.getMetadata().getColumnIndex("customerId"));
        }
    }, false, 4 * 1024 * 1024, 4 * 1024 * 1024, 1024 * 1024, new RecordKeyCopierCompiler(new BytecodeAssembler()));
    sink.clear();
    printer.print(r, FACTORY_CONTAINER.getFactory());
    TestUtils.assertEquals(expected, sink);
    assertThat(expected, "customers c join orders o on c.customerId = o.customerId where customerName ~ 'PJFSREKEUNMKWOF'");
}
Also used : NoRowIdRecordSource(com.questdb.ql.NoRowIdRecordSource) HashJoinRecordSource(com.questdb.ql.join.HashJoinRecordSource) RecordSource(com.questdb.ql.RecordSource) RecordKeyCopierCompiler(com.questdb.ql.map.RecordKeyCopierCompiler) HashJoinRecordSource(com.questdb.ql.join.HashJoinRecordSource) NoRowIdRecordSource(com.questdb.ql.NoRowIdRecordSource) Test(org.junit.Test)

Example 9 with RecordSource

use of com.questdb.ql.RecordSource 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 10 with RecordSource

use of com.questdb.ql.RecordSource in project questdb by bluestreak01.

the class SQLExportToTextFile method main.

public static void main(String[] args) throws JournalException, IOException, ParserException {
    if (args.length < 1) {
        System.out.println("Usage: SQLExportToTextFile <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, "select year, count() count from (select title, match('\\(([0-9]*?)\\)', title) year from 'movies.csv' order by year desc)")) {
            ExportManager.export(rs, factory, new File(args[0], "export.csv"), ',');
        }
    }
}
Also used : RecordSource(com.questdb.ql.RecordSource) Factory(com.questdb.store.factory.Factory) QueryCompiler(com.questdb.parser.sql.QueryCompiler) File(java.io.File)

Aggregations

RecordSource (com.questdb.ql.RecordSource)30 Test (org.junit.Test)18 AbstractTest (com.questdb.test.tools.AbstractTest)12 RecordCursor (com.questdb.common.RecordCursor)11 Record (com.questdb.common.Record)10 QueryCompiler (com.questdb.parser.sql.QueryCompiler)7 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)7 StringSink (com.questdb.std.str.StringSink)5 JournalWriter (com.questdb.store.JournalWriter)5 JournalEntryWriter (com.questdb.store.JournalEntryWriter)4 Factory (com.questdb.store.factory.Factory)4 BootstrapEnv (com.questdb.BootstrapEnv)3 ServerConfiguration (com.questdb.ServerConfiguration)3 Quote (com.questdb.model.Quote)3 AbstractJournalTest (com.questdb.net.ha.AbstractJournalTest)3 ImportHandler (com.questdb.net.http.handlers.ImportHandler)3 File (java.io.File)3 SymbolTable (com.questdb.common.SymbolTable)2 ClientConfig (com.questdb.net.ha.config.ClientConfig)2 ServerConfig (com.questdb.net.ha.config.ServerConfig)2