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;
}
}
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;
}
}
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");
}
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());
}
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()).$();
}
}
}
Aggregations