use of io.questdb.griffin.engine.functions.GroupByFunction in project questdb by bluestreak01.
the class SampleByFillValueRecordCursorFactory method createPlaceholderFunctions.
@NotNull
public static ObjList<Function> createPlaceholderFunctions(ObjList<Function> recordFunctions, @Transient IntList recordFunctionPositions, @NotNull @Transient ObjList<ExpressionNode> fillValues) throws SqlException {
final ObjList<Function> placeholderFunctions = new ObjList<>();
int fillIndex = 0;
final int fillValueCount = fillValues.size();
for (int i = 0, n = recordFunctions.size(); i < n; i++) {
Function function = recordFunctions.getQuick(i);
if (function instanceof GroupByFunction) {
if (fillIndex == fillValueCount) {
throw SqlException.position(0).put("not enough values");
}
ExpressionNode fillNode = fillValues.getQuick(fillIndex++);
try {
switch(ColumnType.tagOf(function.getType())) {
case ColumnType.INT:
placeholderFunctions.add(IntConstant.newInstance(Numbers.parseInt(fillNode.token)));
break;
case ColumnType.LONG:
placeholderFunctions.add(LongConstant.newInstance(Numbers.parseLong(fillNode.token)));
break;
case ColumnType.FLOAT:
placeholderFunctions.add(FloatConstant.newInstance(Numbers.parseFloat(fillNode.token)));
break;
case ColumnType.DOUBLE:
placeholderFunctions.add(DoubleConstant.newInstance(Numbers.parseDouble(fillNode.token)));
break;
case ColumnType.SHORT:
placeholderFunctions.add(ShortConstant.newInstance((short) Numbers.parseInt(fillNode.token)));
break;
case ColumnType.BYTE:
placeholderFunctions.add(ByteConstant.newInstance((byte) Numbers.parseInt(fillNode.token)));
break;
default:
throw SqlException.$(recordFunctionPositions.getQuick(i), "Unsupported type: ").put(ColumnType.nameOf(function.getType()));
}
} catch (NumericException e) {
throw SqlException.position(fillNode.position).put("invalid number: ").put(fillNode.token);
}
} else {
placeholderFunctions.add(function);
}
}
return placeholderFunctions;
}
use of io.questdb.griffin.engine.functions.GroupByFunction in project questdb by bluestreak01.
the class SampleByInterpolateRecordCursorFactory method interpolate.
private void interpolate(long lo, long hi, Record mapRecord, long x1, long x2, MapValue x1Value, MapValue x2value) {
computeYPoints(x1Value, x2value);
for (long x = lo; x < hi; x = sampler.nextTimestamp(x)) {
final MapValue result = findDataMapValue3(mapRecord, x);
assert result != null && result.getByte(0) == 1;
for (int i = 0; i < groupByTwoPointFunctionCount; i++) {
GroupByFunction function = groupByTwoPointFunctions.getQuick(i);
InterpolationUtil.interpolateGap(function, result, sampler.getBucketSize(), x1Value, x2value);
}
for (int i = 0; i < groupByScalarFunctionCount; i++) {
GroupByFunction function = groupByScalarFunctions.getQuick(i);
interpolatorFunctions.getQuick(i).interpolateAndStore(function, result, x, x1, x2, yData + i * 16L, yData + i * 16L + 8);
}
// fill the value, change flag from 'gap' to 'fill'
result.putByte(0, (byte) 0);
}
}
use of io.questdb.griffin.engine.functions.GroupByFunction in project questdb by bluestreak01.
the class SampleByInterpolateRecordCursorFactory method computeYPoints.
private void computeYPoints(MapValue x1Value, MapValue x2value) {
for (int i = 0; i < groupByScalarFunctionCount; i++) {
InterpolationUtil.StoreYFunction storeYFunction = storeYFunctions.getQuick(i);
GroupByFunction groupByFunction = groupByScalarFunctions.getQuick(i);
storeYFunction.store(groupByFunction, x1Value, yData + i * 16L);
storeYFunction.store(groupByFunction, x2value, yData + i * 16L + 8);
}
}
use of io.questdb.griffin.engine.functions.GroupByFunction in project questdb by bluestreak01.
the class GroupByUtils method prepareGroupByRecordFunctions.
public static void prepareGroupByRecordFunctions(@NotNull QueryModel model, RecordMetadata metadata, @NotNull ListColumnFilter listColumnFilter, ObjList<GroupByFunction> groupByFunctions, @Transient IntList groupByFunctionPositions, ObjList<Function> recordFunctions, @Transient IntList recordFunctionPositions, GenericRecordMetadata groupByMetadata, ArrayColumnTypes keyTypes, int keyColumnIndex, boolean timestampUnimportant, int timestampIndex) throws SqlException {
recordFunctionPositions.clear();
// Process group-by functions first to get the idea of
// how many map values we will have.
// Map value count is needed to calculate offsets for
// map key columns.
ObjList<QueryColumn> columns = model.getColumns();
int valueColumnIndex = 0;
int inferredKeyColumnCount = 0;
// when we have same column several times in a row
// we only add it once to map keys
int lastIndex = -1;
for (int i = 0, n = columns.size(); i < n; i++) {
final QueryColumn column = columns.getQuick(i);
final ExpressionNode node = column.getAst();
final int type;
if (node.type == ExpressionNode.LITERAL) {
// this is key
int index = metadata.getColumnIndexQuiet(node.token);
if (index == -1) {
throw SqlException.invalidColumn(node.position, node.token);
}
type = metadata.getColumnType(index);
if (index != timestampIndex || timestampUnimportant) {
if (lastIndex != index) {
listColumnFilter.add(index + 1);
keyTypes.add(type);
keyColumnIndex++;
lastIndex = index;
}
final Function fun;
switch(ColumnType.tagOf(type)) {
case ColumnType.BOOLEAN:
fun = BooleanColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.BYTE:
fun = ByteColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.SHORT:
fun = ShortColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.CHAR:
fun = CharColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.INT:
fun = IntColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.LONG:
fun = LongColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.FLOAT:
fun = FloatColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.DOUBLE:
fun = DoubleColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.STRING:
fun = StrColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.SYMBOL:
fun = new MapSymbolColumn(keyColumnIndex - 1, index, metadata.isSymbolTableStatic(index));
break;
case ColumnType.DATE:
fun = DateColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.TIMESTAMP:
fun = TimestampColumn.newInstance(keyColumnIndex - 1);
break;
case ColumnType.LONG256:
fun = Long256Column.newInstance(keyColumnIndex - 1);
break;
default:
fun = BinColumn.newInstance(keyColumnIndex - 1);
break;
}
recordFunctions.add(fun);
recordFunctionPositions.add(node.position);
} else {
// set this function to null, cursor will replace it with an instance class
// timestamp function returns value of class member which makes it impossible
// to create these columns in advance of cursor instantiation
recordFunctions.add(null);
groupByFunctionPositions.add(0);
if (groupByMetadata.getTimestampIndex() == -1) {
groupByMetadata.setTimestampIndex(i);
}
assert ColumnType.tagOf(type) == ColumnType.TIMESTAMP;
}
// and finish with populating metadata for this factory
if (column.getAlias() == null) {
groupByMetadata.add(BaseRecordMetadata.copyOf(metadata, index));
} else {
groupByMetadata.add(new TableColumnMetadata(Chars.toString(column.getAlias()), metadata.getColumnHash(index), type, metadata.isColumnIndexed(index), metadata.getIndexValueBlockCapacity(index), metadata.isSymbolTableStatic(index), metadata.getMetadata(index)));
}
inferredKeyColumnCount++;
} else {
// add group-by function as a record function as well
// so it can produce column values
final GroupByFunction groupByFunction = groupByFunctions.getQuick(valueColumnIndex);
recordFunctions.add(groupByFunction);
recordFunctionPositions.add(groupByFunctionPositions.getQuick(valueColumnIndex++));
type = groupByFunction.getType();
// and finish with populating metadata for this factory
groupByMetadata.add(new TableColumnMetadata(Chars.toString(column.getName()), 0, type, false, 0, groupByFunction instanceof SymbolFunction && (((SymbolFunction) groupByFunction).isSymbolTableStatic()), groupByFunction.getMetadata()));
}
}
validateGroupByColumns(model, inferredKeyColumnCount);
}
use of io.questdb.griffin.engine.functions.GroupByFunction in project questdb by bluestreak01.
the class GroupByUtils method prepareGroupByFunctions.
public static void prepareGroupByFunctions(QueryModel model, RecordMetadata metadata, FunctionParser functionParser, SqlExecutionContext executionContext, ObjList<GroupByFunction> groupByFunctions, @Transient IntList groupByFunctionPositions, ArrayColumnTypes valueTypes) throws SqlException {
groupByFunctionPositions.clear();
final ObjList<QueryColumn> columns = model.getColumns();
for (int i = 0, n = columns.size(); i < n; i++) {
final QueryColumn column = columns.getQuick(i);
final ExpressionNode node = column.getAst();
if (node.type != ExpressionNode.LITERAL) {
// this can fail
ExpressionNode columnAst = column.getAst();
final Function function = functionParser.parseFunction(columnAst, metadata, executionContext);
// so we have them do all the work
assert function instanceof GroupByFunction;
GroupByFunction func = (GroupByFunction) function;
func.pushValueTypes(valueTypes);
groupByFunctions.add(func);
groupByFunctionPositions.add(columnAst.position);
}
}
}
Aggregations