Search in sources :

Example 1 with ParserException

use of com.questdb.ex.ParserException in project questdb by bluestreak01.

the class QueryCompiler method filter.

private RecordSource filter(QueryModel model, RecordSource rs) throws ParserException {
    try {
        if (model.getWhereClause() == null) {
            return rs;
        }
        RecordMetadata m = rs.getMetadata();
        if (model.getAlias() != null) {
            m.setAlias(model.getAlias().token);
        }
        int timestampIndex = getTimestampIndexQuiet(model.getTimestamp(), m);
        IntrinsicModel im = queryFilterAnalyser.extract(model, model.getWhereClause(), m, null, timestampIndex);
        if (im.intrinsicValue == IntrinsicValue.FALSE) {
            return new NoOpJournalRecordSource(rs);
        }
        if (im.intervals != null) {
            rs = new IntervalRecordSource(rs, im.intervals, timestampIndex);
        }
        if (im.filter != null) {
            VirtualColumn vc = virtualColumnBuilder.createVirtualColumn(model, im.filter, m);
            if (vc.isConstant()) {
                // todo: not hit by test
                if (vc.getBool(null)) {
                    return rs;
                } else {
                    return new NoOpJournalRecordSource(rs);
                }
            }
            return new FilteredRecordSource(rs, vc, im.filter);
        } else {
            return rs;
        }
    } catch (ParserException e) {
        Misc.free(rs);
        throw e;
    }
}
Also used : IntervalRecordSource(com.questdb.ql.interval.IntervalRecordSource) ParserException(com.questdb.ex.ParserException) VirtualColumn(com.questdb.ql.ops.VirtualColumn)

Example 2 with ParserException

use of com.questdb.ex.ParserException in project questdb by bluestreak01.

the class QueryCompiler method compileJoins.

private RecordSource compileJoins(QueryModel model, Factory factory) throws ParserException {
    ObjList<QueryModel> joinModels = model.getJoinModels();
    IntList ordered = model.getOrderedJoinModels();
    RecordSource master = null;
    try {
        boolean needColumnNameHistogram = model.getColumns().size() > 0;
        model.getColumnNameHistogram().clear();
        for (int i = 0, n = ordered.size(); i < n; i++) {
            int index = ordered.getQuick(i);
            QueryModel m = joinModels.getQuick(index);
            // compile
            RecordSource slave = m.getRecordSource();
            if (slave == null) {
                slave = compileJournal(m, factory);
            } else {
                slave = filter(m, slave);
            }
            if (m.getAlias() != null) {
                slave.getMetadata().setAlias(m.getAlias().token);
            }
            if (needColumnNameHistogram) {
                model.createColumnNameHistogram(slave);
            }
            // check if this is the root of joins
            if (master == null) {
                // This is an opportunistic check of order by clause
                // to determine if we can get away ordering main record source only
                // Ordering main record source could benefit from rowid access thus
                // making it faster compared to ordering of join record source that
                // doesn't allow rowid access.
                master = analyseAndCompileOrderBy(model, slave);
            } else {
                // not the root, join to "master"
                switch(m.getJoinType()) {
                    case QueryModel.JOIN_CROSS:
                        // there are fields to analyse
                        master = new CrossJoinRecordSource(master, slave);
                        break;
                    case QueryModel.JOIN_ASOF:
                        master = createAsOfJoin(model.getTimestamp(), m, master, slave);
                        break;
                    default:
                        master = createHashJoin(m, master, slave);
                        break;
                }
            }
            // check if there are post-filters
            ExprNode filter = m.getPostJoinWhereClause();
            if (filter != null) {
                master = new FilteredRecordSource(master, virtualColumnBuilder.createVirtualColumn(model, filter, master.getMetadata()), filter);
            }
        }
        if (joinModelIsFalse(model)) {
            return new NoOpJournalRecordSource(master);
        }
        return master;
    } catch (ParserException e) {
        Misc.free(master);
        throw e;
    }
}
Also used : ParserException(com.questdb.ex.ParserException) CrossJoinRecordSource(com.questdb.ql.join.CrossJoinRecordSource) RBTreeSortedRecordSource(com.questdb.ql.sort.RBTreeSortedRecordSource) IntervalRecordSource(com.questdb.ql.interval.IntervalRecordSource) AsOfJoinRecordSource(com.questdb.ql.join.AsOfJoinRecordSource) SelectedColumnsRecordSource(com.questdb.ql.select.SelectedColumnsRecordSource) HashJoinRecordSource(com.questdb.ql.join.HashJoinRecordSource) VirtualColumnRecordSource(com.questdb.ql.virtual.VirtualColumnRecordSource) AsOfPartitionedJoinRecordSource(com.questdb.ql.join.AsOfPartitionedJoinRecordSource) CrossJoinRecordSource(com.questdb.ql.join.CrossJoinRecordSource)

Example 3 with ParserException

use of com.questdb.ex.ParserException in project questdb by bluestreak01.

the class RenameJournalTest method testJournalAlreadyOpenButIdle.

@Test
@SuppressWarnings("unchecked")
public void testJournalAlreadyOpenButIdle() throws Exception {
    createX();
    Factory factory = getFactory();
    assertJournal(factory, "x");
    sink.clear();
    compiler.execute(factory, "rename table x to y");
    assertJournal(factory, "y");
    // make sure caching readerFactory doesn't return old journal
    try {
        factory.reader(new JournalKey("x"));
        Assert.fail();
    } catch (JournalException e) {
        Assert.assertEquals("Journal does not exist", e.getMessage());
    }
    // make sure compile doesn't pick up old journal
    try {
        compiler.compile(factory, "x");
        Assert.fail("still exists");
    } catch (ParserException e) {
        Assert.assertEquals(0, QueryError.getPosition());
        TestUtils.assertEquals("Journal does not exist", QueryError.getMessage());
    }
    sink.clear();
    createX();
    assertJournal(factory, "x");
}
Also used : ParserException(com.questdb.ex.ParserException) JournalException(com.questdb.std.ex.JournalException) Factory(com.questdb.store.factory.Factory) JournalKey(com.questdb.store.JournalKey) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 4 with ParserException

use of com.questdb.ex.ParserException in project questdb by bluestreak01.

the class AbstractTest method assertThat.

protected void assertThat(String expected, String query, boolean header) throws ParserException, IOException {
    long memUsed = Unsafe.getMemUsed();
    try (RecordSource src = compiler.compile(getFactory(), query)) {
        RecordCursor cursor = src.prepareCursor(getFactory());
        try {
            sink.clear();
            printer.print(cursor, header, src.getMetadata());
            TestUtils.assertEquals(expected, sink);
            cursor.toTop();
            sink.clear();
            printer.print(cursor, header, src.getMetadata());
            TestUtils.assertEquals(expected, sink);
        } finally {
            cursor.releaseCursor();
        }
        TestUtils.assertStrings(src, getFactory());
    } catch (ParserException e) {
        System.out.println(QueryError.getMessage());
        System.out.println(QueryError.getPosition());
        throw e;
    }
    Assert.assertEquals(memUsed, Unsafe.getMemUsed());
}
Also used : ParserException(com.questdb.ex.ParserException) RecordSource(com.questdb.ql.RecordSource) RecordCursor(com.questdb.common.RecordCursor)

Example 5 with ParserException

use of com.questdb.ex.ParserException 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

ParserException (com.questdb.ex.ParserException)7 IntervalRecordSource (com.questdb.ql.interval.IntervalRecordSource)4 AsOfJoinRecordSource (com.questdb.ql.join.AsOfJoinRecordSource)3 AsOfPartitionedJoinRecordSource (com.questdb.ql.join.AsOfPartitionedJoinRecordSource)3 CrossJoinRecordSource (com.questdb.ql.join.CrossJoinRecordSource)3 HashJoinRecordSource (com.questdb.ql.join.HashJoinRecordSource)3 SelectedColumnsRecordSource (com.questdb.ql.select.SelectedColumnsRecordSource)3 RBTreeSortedRecordSource (com.questdb.ql.sort.RBTreeSortedRecordSource)3 VirtualColumnRecordSource (com.questdb.ql.virtual.VirtualColumnRecordSource)3 VirtualColumn (com.questdb.ql.ops.VirtualColumn)2 Factory (com.questdb.store.factory.Factory)2 RecordCursor (com.questdb.common.RecordCursor)1 LogFactory (com.questdb.log.LogFactory)1 QueryCompiler (com.questdb.parser.sql.QueryCompiler)1 RecordSource (com.questdb.ql.RecordSource)1 JournalException (com.questdb.std.ex.JournalException)1 JournalKey (com.questdb.store.JournalKey)1 AbstractTest (com.questdb.test.tools.AbstractTest)1 Test (org.junit.Test)1