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;
}
}
Aggregations