jasperjiaguo commented on code in PR #8724: URL: https://github.com/apache/pinot/pull/8724#discussion_r878565911
########## pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/HistogramAggregationFunction.java: ########## @@ -0,0 +1,358 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.core.query.aggregation.function; + +import com.google.common.base.Preconditions; +import it.unimi.dsi.fastutil.doubles.DoubleArrayList; +import java.util.List; +import java.util.Map; +import org.apache.pinot.common.request.context.ExpressionContext; +import org.apache.pinot.common.utils.DataSchema.ColumnDataType; +import org.apache.pinot.core.common.BlockValSet; +import org.apache.pinot.core.query.aggregation.AggregationResultHolder; +import org.apache.pinot.core.query.aggregation.ObjectAggregationResultHolder; +import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder; +import org.apache.pinot.core.query.aggregation.groupby.ObjectGroupByResultHolder; +import org.apache.pinot.core.query.aggregation.utils.DoubleVectorOpUtils; +import org.apache.pinot.segment.spi.AggregationFunctionType; + + +/** + * Histogram for single-value numerical columns + * usage example: + * `Histogram(columnName, ARRAY[0,1,10,100])` to specify bins (0,1], (1,10], (10,1000] or + * `Histogram(columnName, 0, 1000, 10)` to specify 10 equal-length bins (0,100], (100,200], ..., (900,1000] + */ +public class HistogramAggregationFunction extends BaseSingleInputAggregationFunction<DoubleArrayList, DoubleArrayList> { + + private static final String ARRAY_CONSTRUCTOR = "arrayvalueconstructor"; + private static final int INVALID_BIN = -1; + double[] _bucketEdges; + boolean _isEqualLength = false; + double _lower; + double _upper; + double _binLength; + + public HistogramAggregationFunction(List<ExpressionContext> arguments) { + super(arguments.get(0)); + int numArguments = arguments.size(); + Preconditions.checkArgument(numArguments == 4 || numArguments == 2, "Histogram expects 2 or 4 arguments, got: %s;" + + "usage example: `Histogram(columnName, ARRAY[0,1,10,100])` to specify bins (0,1], (1,10], (10,1000] or " + + "`Histogram(columnName, 0, 1000, 10)` to specify 10 equal-length bins " + + "(0,100], (100,200], ..., (900,1000]", numArguments); + if (numArguments == 2) { + ExpressionContext arrayExpression = arguments.get(1); + Preconditions.checkArgument( + (arrayExpression.getType() == ExpressionContext.Type.FUNCTION) && (arrayExpression.getFunction() + .getFunctionName().equals(ARRAY_CONSTRUCTOR)), + "Please use the format of `Histogram(columnName, ARRAY[1,10,100])` to specify the bin edges"); + _bucketEdges = parseVector(arrayExpression.getFunction().getArguments()); + _lower = _bucketEdges[0]; + _upper = _bucketEdges[_bucketEdges.length - 1]; + } else { + _isEqualLength = true; + _lower = Double.parseDouble(arguments.get(1).getLiteral()); + _upper = Double.parseDouble(arguments.get(2).getLiteral()); + int numBins = Integer.parseInt(arguments.get(3).getLiteral()); + Preconditions.checkArgument(_upper > _lower, + "The right most edge must be greater than left most edge, given %s and %s", _lower, _upper); + Preconditions.checkArgument(numBins > 0, "The number of bins must be greater than zero, given %s", numBins); + _bucketEdges = new double[numBins + 1]; + _bucketEdges[0] = _lower; + _bucketEdges[numBins] = _upper; + _binLength = (_upper - _lower) / numBins; + for (int i = 1; i < numBins; i++) { + _bucketEdges[i] = i * _binLength + _lower; + } + } + } + + int getNumBins() { + return _bucketEdges.length - 1; + } + + int getNumEdges() { + return _bucketEdges.length; + } + + private double[] parseVector(List<ExpressionContext> arrayStr) { + int len = arrayStr.size(); + Preconditions.checkArgument(len > 1, "The number of bin edges must be greater than 1"); + double[] ret = new double[len]; + for (int i = 0; i < len; i++) { + ret[i] = Double.parseDouble(arrayStr.get(i).getLiteral()); + } + for (int i = 1; i < len; i++) { + Preconditions.checkState(ret[i] > ret[i - 1], "the bin edges must be strictly increasing"); + } Review Comment: Resolved -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org