use of org.apache.druid.java.util.common.HumanReadableBytes in project druid by druid-io.
the class ArrayConcatSqlAggregator method toDruidAggregation.
@Nullable
@Override
public Aggregation toDruidAggregation(PlannerContext plannerContext, RowSignature rowSignature, VirtualColumnRegistry virtualColumnRegistry, RexBuilder rexBuilder, String name, AggregateCall aggregateCall, Project project, List<Aggregation> existingAggregations, boolean finalizeAggregations) {
final List<RexNode> arguments = aggregateCall.getArgList().stream().map(i -> Expressions.fromFieldAccess(rowSignature, project, i)).collect(Collectors.toList());
Integer maxSizeBytes = null;
if (arguments.size() > 1) {
RexNode maxBytes = arguments.get(1);
if (!maxBytes.isA(SqlKind.LITERAL)) {
// maxBytes must be a literal
return null;
}
maxSizeBytes = ((Number) RexLiteral.value(maxBytes)).intValue();
}
final DruidExpression arg = Expressions.toDruidExpression(plannerContext, rowSignature, arguments.get(0));
final ExprMacroTable macroTable = plannerContext.getExprMacroTable();
final String fieldName;
final ColumnType druidType = Calcites.getValueTypeForRelDataTypeFull(aggregateCall.getType());
if (druidType == null || !druidType.isArray()) {
// must be an array
return null;
}
final String initialvalue = ExpressionType.fromColumnTypeStrict(druidType).asTypeString() + "[]";
if (arg.isDirectColumnAccess()) {
fieldName = arg.getDirectColumn();
} else {
VirtualColumn vc = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(plannerContext, arg, druidType);
fieldName = vc.getOutputName();
}
if (aggregateCall.isDistinct()) {
return Aggregation.create(new ExpressionLambdaAggregatorFactory(name, ImmutableSet.of(fieldName), null, initialvalue, null, true, false, false, StringUtils.format("array_set_add_all(\"__acc\", \"%s\")", fieldName), StringUtils.format("array_set_add_all(\"__acc\", \"%s\")", name), null, null, maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes) : null, macroTable));
} else {
return Aggregation.create(new ExpressionLambdaAggregatorFactory(name, ImmutableSet.of(fieldName), null, initialvalue, null, true, false, false, StringUtils.format("array_concat(\"__acc\", \"%s\")", fieldName), StringUtils.format("array_concat(\"__acc\", \"%s\")", name), null, null, maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes) : null, macroTable));
}
}
use of org.apache.druid.java.util.common.HumanReadableBytes in project druid by druid-io.
the class ExpressionLambdaAggregatorFactoryTest method testComplexTypeFinalized.
@Test
public void testComplexTypeFinalized() {
ExpressionLambdaAggregatorFactory agg = new ExpressionLambdaAggregatorFactory("expr_agg_name", ImmutableSet.of("some_column"), null, "hyper_unique()", null, null, false, false, "hyper_unique_add(some_column, __acc)", "hyper_unique_add(__acc, expr_agg_name)", null, "hyper_unique_estimate(o)", new HumanReadableBytes(2048), TestExprMacroTable.INSTANCE);
Assert.assertEquals(HyperUniquesAggregatorFactory.TYPE, agg.getIntermediateType());
Assert.assertEquals(HyperUniquesAggregatorFactory.TYPE, agg.getCombiningFactory().getIntermediateType());
Assert.assertEquals(ColumnType.DOUBLE, agg.getResultType());
}
use of org.apache.druid.java.util.common.HumanReadableBytes in project druid by druid-io.
the class ExpressionLambdaAggregatorFactoryTest method testLongArrayTypeFinalized.
@Test
public void testLongArrayTypeFinalized() {
ExpressionLambdaAggregatorFactory agg = new ExpressionLambdaAggregatorFactory("expr_agg_name", ImmutableSet.of("some_column", "some_other_column"), null, "0", "ARRAY<LONG>[]", null, false, false, "__acc + some_column + some_other_column", "array_set_add(__acc, expr_agg_name)", null, "array_to_string(o, ';')", new HumanReadableBytes(2048), TestExprMacroTable.INSTANCE);
Assert.assertEquals(ColumnType.LONG, agg.getIntermediateType());
Assert.assertEquals(ColumnType.LONG_ARRAY, agg.getCombiningFactory().getIntermediateType());
Assert.assertEquals(ColumnType.STRING, agg.getResultType());
}
use of org.apache.druid.java.util.common.HumanReadableBytes in project druid by druid-io.
the class ExpressionLambdaAggregatorFactoryTest method testStringType.
@Test
public void testStringType() {
ExpressionLambdaAggregatorFactory agg = new ExpressionLambdaAggregatorFactory("expr_agg_name", ImmutableSet.of("some_column", "some_other_column"), null, "''", "''", true, true, true, "concat(__acc, some_column, some_other_column)", "concat(__acc, expr_agg_name)", null, null, new HumanReadableBytes(2048), TestExprMacroTable.INSTANCE);
Assert.assertEquals(ColumnType.STRING, agg.getIntermediateType());
Assert.assertEquals(ColumnType.STRING, agg.getCombiningFactory().getIntermediateType());
Assert.assertEquals(ColumnType.STRING, agg.getResultType());
}
use of org.apache.druid.java.util.common.HumanReadableBytes in project druid by druid-io.
the class ExpressionLambdaAggregatorFactoryTest method testInitialValueMustBeConstant.
@Test
public void testInitialValueMustBeConstant() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("initial value must be constant");
ExpressionLambdaAggregatorFactory agg = new ExpressionLambdaAggregatorFactory("expr_agg_name", ImmutableSet.of("some_column", "some_other_column"), null, "x + y", null, true, false, false, "__acc + some_column + some_other_column", "__acc + expr_agg_name", null, null, new HumanReadableBytes(2048), TestExprMacroTable.INSTANCE);
agg.getIntermediateType();
}
Aggregations