use of com.xiaomi.linden.thrift.common.Aggregation in project linden by XiaoMi.
the class BQLCompilerAnalyzer method exitAggregation_spec.
@Override
public void exitAggregation_spec(BQLParser.Aggregation_specContext ctx) {
String col = unescapeColumnName(ctx.column_name());
Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
LindenType type = fieldNameAndType.getValue();
if (type != LindenType.INTEGER && type != LindenType.LONG && type != LindenType.DOUBLE) {
throw new ParseCancellationException(new SemanticException(ctx.column_name(), "Aggregation doesn't support the type of the field \"" + col + "\"."));
}
col = fieldNameAndType.getKey();
Aggregation aggregation = new Aggregation();
aggregation.setField(col);
aggregation.setType(type);
for (BQLParser.Bucket_specContext specContext : ctx.bucket_spec()) {
Bucket bucket = (Bucket) valProperty.get(specContext);
if (bucket != null) {
aggregation.addToBuckets(bucket);
}
}
facetRequest.addToAggregations(aggregation);
}
use of com.xiaomi.linden.thrift.common.Aggregation in project linden by XiaoMi.
the class LindenResultParser method parseFacets.
private void parseFacets(LindenResult result, Facets facets, FacetsCollector facetsCollector) throws IOException {
// Set facets
if (request.isSetFacet()) {
if (request.getFacet().isSetFacetParams() && facets != null) {
List<LindenFacetParam> facetParams = request.getFacet().getFacetParams();
for (LindenFacetParam facetParam : facetParams) {
LindenFacetResult lindenFacetResult = new LindenFacetResult();
lindenFacetResult.setDim(facetParam.facetDimAndPath.dim);
lindenFacetResult.setPath(facetParam.facetDimAndPath.path);
FacetResult facetResult;
if (facetParam.facetDimAndPath.path != null) {
facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim, facetParam.facetDimAndPath.path.split("/"));
} else {
facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim);
}
if (facetResult != null) {
lindenFacetResult.setValue(facetResult.value.intValue());
lindenFacetResult.setChildCount(facetResult.childCount);
int sumValue = 0;
for (int j = 0; j < facetResult.labelValues.length; ++j) {
LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
labelAndValue.setLabel(facetResult.labelValues[j].label);
int value = facetResult.labelValues[j].value.intValue();
labelAndValue.setValue(value);
sumValue += value;
lindenFacetResult.addToLabelValues(labelAndValue);
}
if (sumValue > lindenFacetResult.getValue() || facetResult.labelValues.length < facetParam.topN) {
lindenFacetResult.setValue(sumValue);
}
}
result.addToFacetResults(lindenFacetResult);
}
} else if (request.getFacet().isSetAggregations() && facetsCollector != null) {
List<Aggregation> aggregations = request.getFacet().getAggregations();
for (int i = 0; i < aggregations.size(); ++i) {
Aggregation aggregation = aggregations.get(i);
String fieldName = aggregation.getField();
LindenType type = aggregation.getType();
AggregationResult aggregationResult = new AggregationResult();
aggregationResult.setField(fieldName);
Facets aggFacets;
if (type == LindenType.INTEGER || type == LindenType.LONG) {
LongRange[] ranges = new LongRange[aggregation.getBucketsSize()];
for (int j = 0; j < ranges.length; ++j) {
Bucket bucket = aggregation.getBuckets().get(j);
String label = generateBucketLabel(bucket);
long minValue = bucket.getStartValue().equals("*") ? Long.MIN_VALUE : Long.parseLong(bucket.getStartValue());
long maxValue = bucket.getEndValue().equals("*") ? Long.MAX_VALUE : Long.parseLong(bucket.getEndValue());
ranges[j] = new LongRange(label, minValue, bucket.isStartClosed(), maxValue, bucket.isEndClosed());
}
aggFacets = new LongRangeFacetCounts(fieldName, facetsCollector, ranges);
} else if (type == LindenType.DOUBLE) {
DoubleRange[] ranges = new DoubleRange[aggregation.getBucketsSize()];
for (int j = 0; j < ranges.length; ++j) {
Bucket bucket = aggregation.getBuckets().get(j);
String label = generateBucketLabel(bucket);
double minValue = bucket.getStartValue().equals("*") ? -Double.MAX_VALUE : Double.parseDouble(bucket.getStartValue());
double maxValue = bucket.getEndValue().equals("*") ? Double.MAX_VALUE : Double.parseDouble(bucket.getEndValue());
ranges[j] = new DoubleRange(label, minValue, bucket.isStartClosed(), maxValue, bucket.isEndClosed());
}
aggFacets = new DoubleRangeFacetCounts(fieldName, facetsCollector, ranges);
} else {
throw new IOException(type + " type is not supported in aggregation");
}
FacetResult facetResult = aggFacets.getTopChildren(aggregation.getBucketsSize(), fieldName);
for (int j = 0; j < facetResult.labelValues.length; ++j) {
LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
labelAndValue.setLabel(facetResult.labelValues[j].label);
labelAndValue.setValue(facetResult.labelValues[j].value.intValue());
aggregationResult.addToLabelValues(labelAndValue);
}
result.addToAggregationResults(aggregationResult);
}
}
}
}
Aggregations