Search in sources :

Example 6 with ParserException

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

the class QueryCompiler method selectColumns0.

private RecordSource selectColumns0(final RecordSource recordSource, QueryModel model) throws ParserException {
    final ObjList<QueryColumn> columns = model.getColumns();
    final CharSequenceIntHashMap columnNameHistogram = model.getColumnNameHistogram();
    final RecordMetadata meta = recordSource.getMetadata();
    this.outerVirtualColumns.clear();
    this.innerVirtualColumn.clear();
    this.aggregators.clear();
    this.selectedColumns.clear();
    this.selectedColumnAliases.clear();
    this.groupKeyColumns.clear();
    this.aggregateColumnSequence = 0;
    this.analyticColumns.clear();
    RecordSource rs = recordSource;
    try {
        // create virtual columns from select list
        for (int i = 0, k = columns.size(); i < k; i++) {
            final QueryColumn qc = columns.getQuick(i);
            final ExprNode node = qc.getAst();
            final boolean analytic = qc instanceof AnalyticColumn;
            if (!analytic && node.type == ExprNode.LITERAL) {
                // check literal column validity
                if (meta.getColumnIndexQuiet(node.token) == -1) {
                    throw QueryError.invalidColumn(node.position, node.token);
                }
                // check if this column is exists in more than one journal/result set
                if (columnNameHistogram.get(node.token) > 0) {
                    throw QueryError.ambiguousColumn(node.position);
                }
                // add to selected columns
                selectedColumns.add(node.token);
                addAlias(node.position, qc.getName());
                groupKeyColumns.add(node.token);
                continue;
            }
            // generate missing alias for everything else
            if (qc.getAlias() == null) {
                qc.of(createAlias(aggregateColumnSequence++), node.position, node);
            }
            selectedColumns.add(qc.getAlias());
            addAlias(node.position, qc.getAlias());
            // outright aggregate
            if (!analytic && node.type == ExprNode.FUNCTION && FunctionFactories.isAggregate(node.token)) {
                aggregators.add(qc);
                continue;
            }
            // check if this expression references aggregate function
            if (node.type == ExprNode.OPERATION || node.type == ExprNode.FUNCTION) {
                int beforeSplit = aggregators.size();
                splitAggregates(node, aggregators);
                if (beforeSplit < aggregators.size()) {
                    outerVirtualColumns.add(qc);
                    continue;
                }
            }
            if (analytic) {
                if (qc.getAst().type != ExprNode.FUNCTION) {
                    // todo: not hit by test
                    throw QueryError.$(qc.getAst().position, "Analytic function expected");
                }
                if (aggregators.size() > 0) {
                    throw QueryError.$(qc.getAst().position, "Analytic function is not allowed in context of aggregation. Use sub-query.");
                }
                AnalyticColumn ac = (AnalyticColumn) qc;
                analyticColumns.add(ac);
            } else {
                // this is either a constant or non-aggregate expression
                // either case is a virtual column
                innerVirtualColumn.add(qc);
            }
        }
        // if virtual columns are present, create record source to calculate them
        if (innerVirtualColumn.size() > 0) {
            // this is either a constant or non-aggregate expression
            // either case is a virtual column
            ObjList<VirtualColumn> virtualColumns = new ObjList<>();
            final String timestampName;
            if (model.getSampleBy() != null) {
                timestampName = meta.getColumnName(getTimestampIndex(model, model.getTimestamp(), meta));
            } else {
                timestampName = null;
            }
            for (int i = 0, n = innerVirtualColumn.size(); i < n; i++) {
                QueryColumn qc = innerVirtualColumn.getQuick(i);
                if (Chars.equalsNc(qc.getAlias(), timestampName)) {
                    throw QueryError.$(qc.getAliasPosition() == -1 ? qc.getAst().position : qc.getAliasPosition(), "Alias clashes with implicit sample column name");
                }
                VirtualColumn vc = virtualColumnBuilder.createVirtualColumn(model, qc.getAst(), meta);
                vc.setName(qc.getAlias());
                virtualColumns.add(vc);
                groupKeyColumns.add(qc.getAlias());
            }
            rs = new VirtualColumnRecordSource(rs, virtualColumns);
        }
        // if aggregators present, wrap record source into group-by source
        if (aggregators.size() > 0) {
            rs = compileAggregates(rs, model);
        } else {
            ExprNode sampleBy = model.getSampleBy();
            if (sampleBy != null) {
                throw QueryError.$(sampleBy.position, "There are no aggregation columns");
            }
        }
        if (outerVirtualColumns.size() > 0) {
            rs = compileOuterVirtualColumns(rs, model);
        }
        if (analyticColumns.size() > 0) {
            rs = compileAnalytic(rs, model);
        }
        if (selectedColumns.size() > 0) {
            // wrap underlying record source into selected columns source.
            rs = new SelectedColumnsRecordSource(rs, selectedColumns, selectedColumnAliases);
        }
        return rs;
    } catch (ParserException e) {
        Misc.free(rs);
        throw e;
    }
}
Also used : ParserException(com.questdb.ex.ParserException) VirtualColumnRecordSource(com.questdb.ql.virtual.VirtualColumnRecordSource) 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) SelectedColumnsRecordSource(com.questdb.ql.select.SelectedColumnsRecordSource) VirtualColumn(com.questdb.ql.ops.VirtualColumn)

Example 7 with ParserException

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

the class QueryCompiler method compileNoOptimise.

private RecordSource compileNoOptimise(QueryModel model, Factory factory) throws ParserException {
    RecordSource rs;
    try {
        if (model.getJoinModels().size() > 1) {
            optimiseJoins(model, factory);
            rs = compileJoins(model, factory);
        } else if (model.getJournalName() != null) {
            rs = compileJournal(model, factory);
        } else {
            rs = compileSubQuery(model, factory);
            QueryModel nm = model.getNestedModel();
            if (nm != null) {
                nm.setRecordSource(rs);
            }
        }
        return limit(timestamp(order(selectColumns(rs, model), model), model), model);
    } catch (ParserException e) {
        freeModelRecordSources(model);
        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)

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