use of io.druid.query.aggregation.PostAggregator in project hive by apache.
the class DruidGroupByQueryRecordReader method next.
@Override
public boolean next(NullWritable key, DruidWritable value) {
if (nextKeyValue()) {
// Update value
value.getValue().clear();
// 1) The timestamp column
value.getValue().put(DruidTable.DEFAULT_TIMESTAMP_COLUMN, current.getTimestamp().getMillis());
// 2) The dimension columns
for (int i = 0; i < query.getDimensions().size(); i++) {
DimensionSpec ds = query.getDimensions().get(i);
List<String> dims = current.getDimension(ds.getDimension());
if (dims.size() == 0) {
// NULL value for dimension
value.getValue().put(ds.getOutputName(), null);
} else {
int pos = dims.size() - indexes[i] - 1;
value.getValue().put(ds.getOutputName(), dims.get(pos));
}
}
int counter = 0;
// 3) The aggregation columns
for (AggregatorFactory af : query.getAggregatorSpecs()) {
switch(extractors[counter++]) {
case FLOAT:
value.getValue().put(af.getName(), current.getFloatMetric(af.getName()));
break;
case LONG:
value.getValue().put(af.getName(), current.getLongMetric(af.getName()));
break;
}
}
// 4) The post-aggregation columns
for (PostAggregator pa : query.getPostAggregatorSpecs()) {
assert extractors[counter++] == Extract.FLOAT;
value.getValue().put(pa.getName(), current.getFloatMetric(pa.getName()));
}
return true;
}
return false;
}
use of io.druid.query.aggregation.PostAggregator in project hive by apache.
the class DruidGroupByQueryRecordReader method getCurrentValue.
@Override
public DruidWritable getCurrentValue() throws IOException, InterruptedException {
// Create new value
DruidWritable value = new DruidWritable();
// 1) The timestamp column
value.getValue().put(DruidTable.DEFAULT_TIMESTAMP_COLUMN, current.getTimestamp().getMillis());
// 2) The dimension columns
for (int i = 0; i < query.getDimensions().size(); i++) {
DimensionSpec ds = query.getDimensions().get(i);
List<String> dims = current.getDimension(ds.getDimension());
if (dims.size() == 0) {
// NULL value for dimension
value.getValue().put(ds.getOutputName(), null);
} else {
int pos = dims.size() - indexes[i] - 1;
value.getValue().put(ds.getOutputName(), dims.get(pos));
}
}
int counter = 0;
// 3) The aggregation columns
for (AggregatorFactory af : query.getAggregatorSpecs()) {
switch(extractors[counter++]) {
case FLOAT:
value.getValue().put(af.getName(), current.getFloatMetric(af.getName()));
break;
case LONG:
value.getValue().put(af.getName(), current.getLongMetric(af.getName()));
break;
}
}
// 4) The post-aggregation columns
for (PostAggregator pa : query.getPostAggregatorSpecs()) {
assert extractors[counter++] == Extract.FLOAT;
value.getValue().put(pa.getName(), current.getFloatMetric(pa.getName()));
}
return value;
}
use of io.druid.query.aggregation.PostAggregator in project hive by apache.
the class DruidSerDe method inferSchema.
/* Timeseries query */
private void inferSchema(TimeseriesQuery query, List<String> columnNames, List<PrimitiveTypeInfo> columnTypes) {
// Timestamp column
columnNames.add(DruidTable.DEFAULT_TIMESTAMP_COLUMN);
columnTypes.add(TypeInfoFactory.timestampTypeInfo);
// Aggregator columns
for (AggregatorFactory af : query.getAggregatorSpecs()) {
columnNames.add(af.getName());
columnTypes.add(DruidSerDeUtils.convertDruidToHiveType(af.getTypeName()));
}
// different types for post-aggregation functions
for (PostAggregator pa : query.getPostAggregatorSpecs()) {
columnNames.add(pa.getName());
columnTypes.add(TypeInfoFactory.floatTypeInfo);
}
}
use of io.druid.query.aggregation.PostAggregator in project hive by apache.
the class DruidSerDe method inferSchema.
/* TopN query */
private void inferSchema(TopNQuery query, List<String> columnNames, List<PrimitiveTypeInfo> columnTypes) {
// Timestamp column
columnNames.add(DruidTable.DEFAULT_TIMESTAMP_COLUMN);
columnTypes.add(TypeInfoFactory.timestampTypeInfo);
// Dimension column
columnNames.add(query.getDimensionSpec().getOutputName());
columnTypes.add(TypeInfoFactory.stringTypeInfo);
// Aggregator columns
for (AggregatorFactory af : query.getAggregatorSpecs()) {
columnNames.add(af.getName());
columnTypes.add(DruidSerDeUtils.convertDruidToHiveType(af.getTypeName()));
}
// different types for post-aggregation functions
for (PostAggregator pa : query.getPostAggregatorSpecs()) {
columnNames.add(pa.getName());
columnTypes.add(TypeInfoFactory.floatTypeInfo);
}
}
use of io.druid.query.aggregation.PostAggregator in project druid by druid-io.
the class TopNQueryQueryToolChestTest method testCacheStrategy.
@Test
public void testCacheStrategy() throws Exception {
CacheStrategy<Result<TopNResultValue>, Object, TopNQuery> strategy = new TopNQueryQueryToolChest(null, null).getCacheStrategy(new TopNQuery(new TableDataSource("dummy"), VirtualColumns.EMPTY, new DefaultDimensionSpec("test", "test"), new NumericTopNMetricSpec("metric1"), 3, new MultipleIntervalSegmentSpec(ImmutableList.of(new Interval("2015-01-01/2015-01-02"))), null, Granularities.ALL, ImmutableList.<AggregatorFactory>of(new CountAggregatorFactory("metric1")), ImmutableList.<PostAggregator>of(new ConstantPostAggregator("post", 10)), null));
final Result<TopNResultValue> result = new Result<>(// test timestamps that result in integer size millis
new DateTime(123L), new TopNResultValue(Arrays.asList(ImmutableMap.<String, Object>of("test", "val1", "metric1", 2))));
Object preparedValue = strategy.prepareForCache().apply(result);
ObjectMapper objectMapper = new DefaultObjectMapper();
Object fromCacheValue = objectMapper.readValue(objectMapper.writeValueAsBytes(preparedValue), strategy.getCacheObjectClazz());
Result<TopNResultValue> fromCacheResult = strategy.pullFromCache().apply(fromCacheValue);
Assert.assertEquals(result, fromCacheResult);
}
Aggregations