use of org.apache.druid.math.expr.ExpressionType in project druid by druid-io.
the class ExpressionLambdaAggregatorFactory method getResultType.
@Override
public ColumnType getResultType() {
Expr finalizeExpr = finalizeExpression.get();
ExprEval<?> initialVal = initialCombineValue.get();
if (finalizeExpr != null) {
ExpressionType type = finalizeExpr.getOutputType(finalizeInspector.get());
if (type == null) {
type = initialVal.type();
}
return ExpressionType.toColumnType(type);
}
return ExpressionType.toColumnType(initialVal.type());
}
use of org.apache.druid.math.expr.ExpressionType in project druid by druid-io.
the class TimestampShiftMacroTest method testDynamicExpression.
@Test
public void testDynamicExpression() {
// step parameter is not a literal expression
Expr expr = apply(ImmutableList.of(ExprEval.of(timestamp.getMillis()).toExpr(), ExprEval.of("P1Y").toExpr(), new NotLiteralExpr("step"), ExprEval.of("America/Los_Angeles").toExpr()));
final int step = 3;
Assert.assertEquals(timestamp.toDateTime(DateTimes.inferTzFromString("America/Los_Angeles")).withPeriodAdded(Years.ONE, step).getMillis(), expr.eval(new Expr.ObjectBinding() {
@Nullable
@Override
public ExpressionType getType(String name) {
return null;
}
@Nullable
@Override
public Object get(String name) {
if ("step".equals(name)) {
return step;
} else {
throw new IAE("Invalid bindings");
}
}
}).asLong());
}
use of org.apache.druid.math.expr.ExpressionType in project druid by druid-io.
the class BaseFilterTest method selectColumnValuesMatchingFilterUsingVectorVirtualColumnCursor.
private List<String> selectColumnValuesMatchingFilterUsingVectorVirtualColumnCursor(final DimFilter filter, final String virtualColumn, final String selectColumn) {
final Expr parsedIdentifier = Parser.parse(selectColumn, TestExprMacroTable.INSTANCE);
try (final VectorCursor cursor = makeVectorCursor(makeFilter(filter))) {
final ExpressionType outputType = parsedIdentifier.getOutputType(cursor.getColumnSelectorFactory());
final List<String> values = new ArrayList<>();
if (outputType.is(ExprType.STRING)) {
final VectorObjectSelector objectSelector = cursor.getColumnSelectorFactory().makeObjectSelector(virtualColumn);
while (!cursor.isDone()) {
final Object[] rowVector = objectSelector.getObjectVector();
for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
values.add((String) rowVector[i]);
}
cursor.advance();
}
} else {
final VectorValueSelector valueSelector = cursor.getColumnSelectorFactory().makeValueSelector(virtualColumn);
while (!cursor.isDone()) {
final boolean[] nulls = valueSelector.getNullVector();
if (outputType.is(ExprType.DOUBLE)) {
final double[] doubles = valueSelector.getDoubleVector();
for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
if (nulls != null && nulls[i]) {
values.add(null);
} else {
values.add(String.valueOf(doubles[i]));
}
}
} else {
final long[] longs = valueSelector.getLongVector();
for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
if (nulls != null && nulls[i]) {
values.add(null);
} else {
values.add(String.valueOf(longs[i]));
}
}
}
cursor.advance();
}
}
return values;
}
}
use of org.apache.druid.math.expr.ExpressionType in project druid by apache.
the class ExpressionLambdaAggregatorFactory method getResultType.
@Override
public ColumnType getResultType() {
Expr finalizeExpr = finalizeExpression.get();
ExprEval<?> initialVal = initialCombineValue.get();
if (finalizeExpr != null) {
ExpressionType type = finalizeExpr.getOutputType(finalizeInspector.get());
if (type == null) {
type = initialVal.type();
}
return ExpressionType.toColumnType(type);
}
return ExpressionType.toColumnType(initialVal.type());
}
use of org.apache.druid.math.expr.ExpressionType in project druid by apache.
the class ExpressionVectorSelectorsTest method sanityTestVectorizedExpressionSelectors.
public static void sanityTestVectorizedExpressionSelectors(String expression, @Nullable ExpressionType outputType, QueryableIndex index, Closer closer, int rowsPerSegment) {
final List<Object> results = new ArrayList<>(rowsPerSegment);
final VirtualColumns virtualColumns = VirtualColumns.create(ImmutableList.of(new ExpressionVirtualColumn("v", expression, ExpressionType.toColumnType(outputType), TestExprMacroTable.INSTANCE)));
final QueryableIndexStorageAdapter storageAdapter = new QueryableIndexStorageAdapter(index);
VectorCursor cursor = storageAdapter.makeVectorCursor(null, index.getDataInterval(), virtualColumns, false, 512, null);
ColumnCapabilities capabilities = virtualColumns.getColumnCapabilities(storageAdapter, "v");
int rowCount = 0;
if (capabilities.isDictionaryEncoded().isTrue()) {
SingleValueDimensionVectorSelector selector = cursor.getColumnSelectorFactory().makeSingleValueDimensionSelector(DefaultDimensionSpec.of("v"));
while (!cursor.isDone()) {
int[] row = selector.getRowVector();
for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
results.add(selector.lookupName(row[i]));
}
cursor.advance();
}
} else {
VectorValueSelector selector = null;
VectorObjectSelector objectSelector = null;
if (outputType != null && outputType.isNumeric()) {
selector = cursor.getColumnSelectorFactory().makeValueSelector("v");
} else {
objectSelector = cursor.getColumnSelectorFactory().makeObjectSelector("v");
}
while (!cursor.isDone()) {
boolean[] nulls;
switch(outputType.getType()) {
case LONG:
nulls = selector.getNullVector();
long[] longs = selector.getLongVector();
for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
results.add(nulls != null && nulls[i] ? null : longs[i]);
}
break;
case DOUBLE:
// special case to test floats just to get coverage on getFloatVector
if ("float2".equals(expression)) {
nulls = selector.getNullVector();
float[] floats = selector.getFloatVector();
for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
results.add(nulls != null && nulls[i] ? null : (double) floats[i]);
}
} else {
nulls = selector.getNullVector();
double[] doubles = selector.getDoubleVector();
for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
results.add(nulls != null && nulls[i] ? null : doubles[i]);
}
}
break;
case STRING:
Object[] objects = objectSelector.getObjectVector();
for (int i = 0; i < objectSelector.getCurrentVectorSize(); i++, rowCount++) {
results.add(objects[i]);
}
break;
}
cursor.advance();
}
}
closer.register(cursor);
Sequence<Cursor> cursors = new QueryableIndexStorageAdapter(index).makeCursors(null, index.getDataInterval(), virtualColumns, Granularities.ALL, false, null);
int rowCountCursor = cursors.map(nonVectorized -> {
final ColumnValueSelector nonSelector = nonVectorized.getColumnSelectorFactory().makeColumnValueSelector("v");
int rows = 0;
while (!nonVectorized.isDone()) {
Assert.assertEquals(StringUtils.format("Failed at row %s", rows), nonSelector.getObject(), results.get(rows));
rows++;
nonVectorized.advance();
}
return rows;
}).accumulate(0, (acc, in) -> acc + in);
Assert.assertTrue(rowCountCursor > 0);
Assert.assertEquals(rowCountCursor, rowCount);
}
Aggregations