Search in sources :

Example 11 with QueryCompiler

use of com.questdb.parser.sql.QueryCompiler 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)

Example 12 with QueryCompiler

use of com.questdb.parser.sql.QueryCompiler in project questdb by bluestreak01.

the class SQLParameters method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLParameters <path>");
        System.exit(1);
    }
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLParameters.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try (RecordSource rs = compiler.compile(factory, "'movies.csv' where movieId = :id")) {
            RecordSourcePrinter printer = new RecordSourcePrinter(new StdoutSink());
            // use of parameters avoids expensive query compilation
            rs.getParam(":id").set(62198);
            printer.print(rs, factory);
            System.out.println("----------------");
            rs.getParam(":id").set(125);
            printer.print(rs, factory);
        }
    }
}
Also used : JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) RecordSource(com.questdb.ql.RecordSource) StdoutSink(com.questdb.std.str.StdoutSink) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

Example 13 with QueryCompiler

use of com.questdb.parser.sql.QueryCompiler in project questdb by bluestreak01.

the class PerformanceTest method testAllBySymbolValueOverIntervalNew.

@SuppressWarnings("StatementWithEmptyBody")
@Test
public void testAllBySymbolValueOverIntervalNew() throws JournalException, ParserException, NumericException {
    try (JournalWriter<Quote> w = getFactory().writer(Quote.class, "quote", TEST_DATA_SIZE)) {
        TestUtils.generateQuoteData(w, TEST_DATA_SIZE, DateFormatUtils.parseDateTime("2013-10-05T10:00:00.000Z"), 1000);
        w.commit();
    }
    QueryCompiler compiler = new QueryCompiler();
    Factory factory = getFactory();
    try (RecordSource src = compiler.compile(factory, "quote where timestamp = '2013-10-05T10:00:00.000Z;10d' and sym = 'LLOY.L'")) {
        int count = 1000;
        long t = 0;
        for (int i = -count; i < count; i++) {
            if (i == 0) {
                t = System.nanoTime();
            }
            RecordCursor c = src.prepareCursor(factory);
            try {
                for (; c.hasNext(); ) {
                    c.next();
                }
            } finally {
                c.releaseCursor();
            }
        }
        LOG.info().$("NEW journal.query().all().withKeys(\"LLOY.L\").slice(interval) (query only) latency: ").$((System.nanoTime() - t) / count / 1000).$("μs").$();
    }
}
Also used : Quote(com.questdb.model.Quote) RecordSource(com.questdb.ql.RecordSource) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) ReaderFactory(com.questdb.store.factory.ReaderFactory) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 14 with QueryCompiler

use of com.questdb.parser.sql.QueryCompiler in project questdb by bluestreak01.

the class HttpServerTest method testImportNumberPrefixedColumn.

@Test
public void testImportNumberPrefixedColumn() throws Exception {
    final ServerConfiguration configuration = new ServerConfiguration();
    BootstrapEnv env = new BootstrapEnv();
    env.configuration = configuration;
    env.factory = getFactory();
    env.typeProbeCollection = TYPE_PROBE_COLLECTION;
    env.matcher = new SimpleUrlMatcher() {

        {
            put("/imp", new ImportHandler(env));
        }
    };
    HttpServer server = new HttpServer(env);
    server.start();
    try {
        Assert.assertEquals(200, HttpTestUtils.upload("/csv/test-import-num-prefix.csv", "http://localhost:9000/imp?fmt=json", null, null));
        StringSink sink = new StringSink();
        RecordSourcePrinter printer = new RecordSourcePrinter(sink);
        QueryCompiler qc = new QueryCompiler(env);
        try (RecordSource rs = qc.compile(env.factory, "select count(StrSym), count(IntSym), count(_1IntCol), count(long), count() from 'test-import-num-prefix.csv'")) {
            printer.print(rs, env.factory);
        }
        TestUtils.assertEquals("126\t126\t128\t129\t129\n", sink);
    } finally {
        server.halt();
    }
}
Also used : BootstrapEnv(com.questdb.BootstrapEnv) RecordSource(com.questdb.ql.RecordSource) ServerConfiguration(com.questdb.ServerConfiguration) RecordSourcePrinter(com.questdb.ql.RecordSourcePrinter) ImportHandler(com.questdb.net.http.handlers.ImportHandler) StringSink(com.questdb.std.str.StringSink) QueryCompiler(com.questdb.parser.sql.QueryCompiler) AbstractJournalTest(com.questdb.net.ha.AbstractJournalTest) Test(org.junit.Test)

Example 15 with QueryCompiler

use of com.questdb.parser.sql.QueryCompiler in project questdb by bluestreak01.

the class SQLErrorHandling method main.

public static void main(String[] args) throws JournalException, ParserException, IOException {
    if (args.length < 1) {
        System.out.println("Usage: SQLErrorHandling <path>");
        System.exit(1);
    }
    try (Factory factory = new Factory(args[0], 1000, 1, 0)) {
        // import movies data to query
        ImportManager.importFile(factory, SQLErrorHandling.class.getResource("/movies.csv").getFile(), ',', null, false);
        // Create SQL engine instance.
        QueryCompiler compiler = new QueryCompiler();
        try {
            // in case of exception all allocated resources are freed automatically
            compiler.compile(factory, "'movies.csv' where movieIds = :id");
        } catch (ParserException e) {
            LOG.error().$("At (").$(QueryError.getPosition()).$(") : ").$(QueryError.getMessage()).$();
        }
    }
}
Also used : ParserException(com.questdb.ex.ParserException) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) QueryCompiler(com.questdb.parser.sql.QueryCompiler)

Aggregations

QueryCompiler (com.questdb.parser.sql.QueryCompiler)15 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)11 Test (org.junit.Test)10 BootstrapEnv (com.questdb.BootstrapEnv)9 ServerConfiguration (com.questdb.ServerConfiguration)9 AbstractJournalTest (com.questdb.net.ha.AbstractJournalTest)9 ImportHandler (com.questdb.net.http.handlers.ImportHandler)9 StringSink (com.questdb.std.str.StringSink)9 RecordSource (com.questdb.ql.RecordSource)7 Factory (com.questdb.store.factory.Factory)5 LogFactory (com.questdb.log.LogFactory)2 StdoutSink (com.questdb.std.str.StdoutSink)2 Record (com.questdb.common.Record)1 RecordCursor (com.questdb.common.RecordCursor)1 ParserException (com.questdb.ex.ParserException)1 Quote (com.questdb.model.Quote)1 ParsedModel (com.questdb.parser.sql.model.ParsedModel)1 JournalException (com.questdb.std.ex.JournalException)1 ReaderFactory (com.questdb.store.factory.ReaderFactory)1 JournalConfiguration (com.questdb.store.factory.configuration.JournalConfiguration)1