use of org.apache.druid.query.aggregation.AggregatorFactory in project druid by druid-io.
the class TDigestSketchAggregatorTest method serializeDeserializeFactoryWithFieldName.
// this is to test Json properties and equals
@Test
public void serializeDeserializeFactoryWithFieldName() throws Exception {
ObjectMapper objectMapper = new DefaultObjectMapper();
new TDigestSketchModule().getJacksonModules().forEach(objectMapper::registerModule);
TDigestSketchAggregatorFactory factory = new TDigestSketchAggregatorFactory("name", "filedName", 128);
AggregatorFactory other = objectMapper.readValue(objectMapper.writeValueAsString(factory), AggregatorFactory.class);
Assert.assertEquals(factory, other);
}
use of org.apache.druid.query.aggregation.AggregatorFactory in project druid by druid-io.
the class MaterializedViewSupervisorTest method setUp.
@Before
public void setUp() {
TestDerbyConnector derbyConnector = derbyConnectorRule.getConnector();
derbyConnector.createDataSourceTable();
derbyConnector.createSegmentTable();
taskStorage = EasyMock.createMock(TaskStorage.class);
taskMaster = EasyMock.createMock(TaskMaster.class);
indexerMetadataStorageCoordinator = new IndexerSQLMetadataStorageCoordinator(objectMapper, derbyConnectorRule.metadataTablesConfigSupplier().get(), derbyConnector);
metadataSupervisorManager = EasyMock.createMock(MetadataSupervisorManager.class);
sqlSegmentsMetadataManager = EasyMock.createMock(SqlSegmentsMetadataManager.class);
taskQueue = EasyMock.createMock(TaskQueue.class);
taskQueue.start();
objectMapper.registerSubtypes(new NamedType(HashBasedNumberedShardSpec.class, "hashed"));
spec = new MaterializedViewSupervisorSpec("base", new DimensionsSpec(Collections.singletonList(new StringDimensionSchema("dim"))), new AggregatorFactory[] { new LongSumAggregatorFactory("m1", "m1") }, HadoopTuningConfig.makeDefaultTuningConfig(), null, null, null, null, null, false, objectMapper, taskMaster, taskStorage, metadataSupervisorManager, sqlSegmentsMetadataManager, indexerMetadataStorageCoordinator, new MaterializedViewTaskConfig(), EasyMock.createMock(AuthorizerMapper.class), EasyMock.createMock(ChatHandlerProvider.class), new SupervisorStateManagerConfig());
derivativeDatasourceName = spec.getDataSourceName();
supervisor = (MaterializedViewSupervisor) spec.createSupervisor();
}
use of org.apache.druid.query.aggregation.AggregatorFactory in project druid by druid-io.
the class MovingAverageQueryToolChest method makePostComputeManipulatorFn.
@Override
public Function<Row, Row> makePostComputeManipulatorFn(MovingAverageQuery query, MetricManipulationFn fn) {
return new Function<Row, Row>() {
@Override
public Row apply(Row result) {
MapBasedRow mRow = (MapBasedRow) result;
final Map<String, Object> values = new HashMap<>(mRow.getEvent());
for (AggregatorFactory agg : query.getAggregatorSpecs()) {
Object aggVal = values.get(agg.getName());
if (aggVal != null) {
values.put(agg.getName(), fn.manipulate(agg, aggVal));
} else {
values.put(agg.getName(), null);
}
}
for (AveragerFactory<?, ?> avg : query.getAveragerSpecs()) {
Object aggVal = values.get(avg.getName());
if (aggVal != null) {
values.put(avg.getName(), fn.manipulate(new AveragerFactoryWrapper<>(avg, avg.getName() + "_"), aggVal));
} else {
values.put(avg.getName(), null);
}
}
return new MapBasedRow(result.getTimestamp(), values);
}
};
}
use of org.apache.druid.query.aggregation.AggregatorFactory in project druid by druid-io.
the class BaseAverager method addElement.
/* (non-Javadoc)
* @see Averager#addElement(java.util.Map, java.util.Map)
*/
@SuppressWarnings("unchecked")
@Override
public void addElement(Map<String, Object> e, Map<String, AggregatorFactory> a) {
Object metric = e.get(fieldName);
I finalMetric;
if (a.containsKey(fieldName)) {
AggregatorFactory af = a.get(fieldName);
finalMetric = metric != null ? (I) af.finalizeComputation(metric) : null;
} else {
finalMetric = (I) metric;
}
buckets[index++] = finalMetric;
index %= numBuckets;
}
use of org.apache.druid.query.aggregation.AggregatorFactory in project druid by druid-io.
the class BloomFilterSqlAggregator 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 RexNode inputOperand = Expressions.fromFieldAccess(rowSignature, project, aggregateCall.getArgList().get(0));
final DruidExpression input = Expressions.toDruidExpression(plannerContext, rowSignature, inputOperand);
if (input == null) {
return null;
}
final AggregatorFactory aggregatorFactory;
final String aggName = StringUtils.format("%s:agg", name);
final RexNode maxNumEntriesOperand = Expressions.fromFieldAccess(rowSignature, project, aggregateCall.getArgList().get(1));
if (!maxNumEntriesOperand.isA(SqlKind.LITERAL)) {
// maxNumEntriesOperand must be a literal in order to plan.
return null;
}
final int maxNumEntries = ((Number) RexLiteral.value(maxNumEntriesOperand)).intValue();
// Look for existing matching aggregatorFactory.
for (final Aggregation existing : existingAggregations) {
for (AggregatorFactory factory : existing.getAggregatorFactories()) {
if (factory instanceof BloomFilterAggregatorFactory) {
final BloomFilterAggregatorFactory theFactory = (BloomFilterAggregatorFactory) factory;
// Check input for equivalence.
final boolean inputMatches;
final DruidExpression virtualInput = virtualColumnRegistry.findVirtualColumnExpressions(theFactory.requiredFields()).stream().findFirst().orElse(null);
if (virtualInput == null) {
if (input.isDirectColumnAccess()) {
inputMatches = input.getDirectColumn().equals(theFactory.getField().getDimension());
} else {
inputMatches = input.getSimpleExtraction().getColumn().equals(theFactory.getField().getDimension()) && input.getSimpleExtraction().getExtractionFn().equals(theFactory.getField().getExtractionFn());
}
} else {
inputMatches = virtualInput.equals(input);
}
final boolean matches = inputMatches && theFactory.getMaxNumEntries() == maxNumEntries;
if (matches) {
// Found existing one. Use this.
return Aggregation.create(theFactory);
}
}
}
}
// No existing match found. Create a new one.
ColumnType valueType = Calcites.getColumnTypeForRelDataType(inputOperand.getType());
final DimensionSpec spec;
if (input.isDirectColumnAccess()) {
spec = new DefaultDimensionSpec(input.getSimpleExtraction().getColumn(), StringUtils.format("%s:%s", name, input.getSimpleExtraction().getColumn()), valueType);
} else if (input.isSimpleExtraction()) {
spec = new ExtractionDimensionSpec(input.getSimpleExtraction().getColumn(), StringUtils.format("%s:%s", name, input.getSimpleExtraction().getColumn()), valueType, input.getSimpleExtraction().getExtractionFn());
} else {
String virtualColumnName = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(input, inputOperand.getType());
spec = new DefaultDimensionSpec(virtualColumnName, StringUtils.format("%s:%s", name, virtualColumnName));
}
aggregatorFactory = new BloomFilterAggregatorFactory(aggName, spec, maxNumEntries);
return Aggregation.create(aggregatorFactory);
}
Aggregations