Search in sources :

Example 6 with SymbolFunction

use of io.questdb.griffin.engine.functions.SymbolFunction in project questdb by bluestreak01.

the class SqlCodeGenerator method generateSelectVirtual.

private RecordCursorFactory generateSelectVirtual(QueryModel model, SqlExecutionContext executionContext) throws SqlException {
    final RecordCursorFactory factory = generateSubQuery(model, executionContext);
    try {
        final ObjList<QueryColumn> columns = model.getColumns();
        final int columnCount = columns.size();
        final RecordMetadata metadata = factory.getMetadata();
        final ObjList<Function> functions = new ObjList<>(columnCount);
        final GenericRecordMetadata virtualMetadata = new GenericRecordMetadata();
        // attempt to preserve timestamp on new data set
        CharSequence timestampColumn;
        final int timestampIndex = metadata.getTimestampIndex();
        if (timestampIndex > -1) {
            timestampColumn = metadata.getColumnName(timestampIndex);
        } else {
            timestampColumn = null;
        }
        for (int i = 0; i < columnCount; i++) {
            final QueryColumn column = columns.getQuick(i);
            final ExpressionNode node = column.getAst();
            if (node.type == ExpressionNode.LITERAL && Chars.equalsNc(node.token, timestampColumn)) {
                virtualMetadata.setTimestampIndex(i);
            }
            final Function function = functionParser.parseFunction(column.getAst(), metadata, executionContext);
            // define "undefined" functions as string
            if (function.isUndefined()) {
                function.assignType(ColumnType.STRING, executionContext.getBindVariableService());
            }
            functions.add(function);
            if (function instanceof SymbolFunction) {
                virtualMetadata.add(new TableColumnMetadata(Chars.toString(column.getAlias()), configuration.getRandom().nextLong(), function.getType(), false, 0, ((SymbolFunction) function).isSymbolTableStatic(), function.getMetadata()));
            } else {
                virtualMetadata.add(new TableColumnMetadata(Chars.toString(column.getAlias()), configuration.getRandom().nextLong(), function.getType(), function.getMetadata()));
            }
        }
        // not selected, we will need to add it
        if (executionContext.isTimestampRequired() && timestampColumn != null && virtualMetadata.getTimestampIndex() == -1) {
            final Function timestampFunction = FunctionParser.createColumn(0, timestampColumn, metadata);
            functions.add(timestampFunction);
            for (int i = 0, n = model.getBottomUpColumns().size(); i < n; i++) {
                QueryColumn qc = model.getBottomUpColumns().getQuick(i);
                if (qc.getAst().type == LITERAL && Chars.equals(timestampColumn, qc.getAst().token)) {
                    virtualMetadata.setTimestampIndex(virtualMetadata.getColumnCount());
                    virtualMetadata.add(new TableColumnMetadata(Chars.toString(qc.getAlias()), 0, timestampFunction.getType(), timestampFunction.getMetadata()));
                    break;
                }
            }
        }
        return new VirtualRecordCursorFactory(virtualMetadata, functions, factory);
    } catch (SqlException | CairoException e) {
        factory.close();
        throw e;
    }
}
Also used : SymbolFunction(io.questdb.griffin.engine.functions.SymbolFunction) GroupByFunction(io.questdb.griffin.engine.functions.GroupByFunction) AnalyticFunction(io.questdb.griffin.engine.analytic.AnalyticFunction) SortedLightRecordCursorFactory(io.questdb.griffin.engine.orderby.SortedLightRecordCursorFactory) SortedRecordCursorFactory(io.questdb.griffin.engine.orderby.SortedRecordCursorFactory) LimitRecordCursorFactory(io.questdb.griffin.engine.LimitRecordCursorFactory) GroupByRecordCursorFactory(io.questdb.griffin.engine.groupby.vect.GroupByRecordCursorFactory) EmptyTableRecordCursorFactory(io.questdb.griffin.engine.EmptyTableRecordCursorFactory) CachedAnalyticRecordCursorFactory(io.questdb.griffin.engine.analytic.CachedAnalyticRecordCursorFactory) SymbolFunction(io.questdb.griffin.engine.functions.SymbolFunction)

Aggregations

SymbolFunction (io.questdb.griffin.engine.functions.SymbolFunction)6 Function (io.questdb.cairo.sql.Function)3 BinaryFunction (io.questdb.griffin.engine.functions.BinaryFunction)2 BooleanFunction (io.questdb.griffin.engine.functions.BooleanFunction)2 GroupByFunction (io.questdb.griffin.engine.functions.GroupByFunction)2 UnaryFunction (io.questdb.griffin.engine.functions.UnaryFunction)2 EmptyTableRecordCursorFactory (io.questdb.griffin.engine.EmptyTableRecordCursorFactory)1 LimitRecordCursorFactory (io.questdb.griffin.engine.LimitRecordCursorFactory)1 AnalyticFunction (io.questdb.griffin.engine.analytic.AnalyticFunction)1 CachedAnalyticRecordCursorFactory (io.questdb.griffin.engine.analytic.CachedAnalyticRecordCursorFactory)1 NegatableBooleanFunction (io.questdb.griffin.engine.functions.NegatableBooleanFunction)1 GroupByRecordCursorFactory (io.questdb.griffin.engine.groupby.vect.GroupByRecordCursorFactory)1 SortedLightRecordCursorFactory (io.questdb.griffin.engine.orderby.SortedLightRecordCursorFactory)1 SortedRecordCursorFactory (io.questdb.griffin.engine.orderby.SortedRecordCursorFactory)1 ExpressionNode (io.questdb.griffin.model.ExpressionNode)1 QueryColumn (io.questdb.griffin.model.QueryColumn)1