siddharthteotia commented on code in PR #8565: URL: https://github.com/apache/pinot/pull/8565#discussion_r854382148
########## pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/PercentileSmartTDigestAggregationFunction.java: ########## @@ -0,0 +1,346 @@ +/** + * 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 com.tdunning.math.stats.TDigest; +import it.unimi.dsi.fastutil.doubles.DoubleArrayList; +import it.unimi.dsi.fastutil.doubles.DoubleListIterator; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +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.segment.spi.AggregationFunctionType; +import org.apache.pinot.spi.data.FieldSpec.DataType; + + +/** + * The {@code PercentileSmartTDigestAggregationFunction} calculates the percentile of the values for a given expression + * (both single-valued and multi-valued are supported). + * + * For aggregation-only queries, the values are stored in a {@link DoubleArrayList} initially. Once the number of values + * exceeds a threshold, the list will be converted into a {@link TDigest}, and approximate result will be returned. + * + * The function takes an optional third argument for parameters: + * - threshold: Threshold of the number of values to trigger the conversion, 100_000 by default. Non-positive value + * means never convert. + * - compression: Compression for the converted TDigest, 100 by default. + * Example of third argument: 'threshold=10000;compression=50' + */ +public class PercentileSmartTDigestAggregationFunction extends BaseSingleInputAggregationFunction<Object, Double> { + private static final double DEFAULT_FINAL_RESULT = Double.NEGATIVE_INFINITY; + + private final double _percentile; + private final int _threshold; + private final int _compression; + + public PercentileSmartTDigestAggregationFunction(List<ExpressionContext> arguments) { + super(arguments.get(0)); + try { + _percentile = Double.parseDouble(arguments.get(1).getLiteral()); + } catch (Exception e) { + throw new IllegalArgumentException( + "Second argument of PERCENTILE_SMART_TDIGEST aggregation function must be a double literal (percentile)"); + } + Preconditions.checkArgument(_percentile >= 0 && _percentile <= 100, "Invalid percentile: %s", _percentile); + if (arguments.size() > 2) { + Parameters parameters = new Parameters(arguments.get(2).getLiteral()); + _compression = parameters._compression; + _threshold = parameters._threshold; + } else { + _threshold = Parameters.DEFAULT_THRESHOLD; + _compression = Parameters.DEFAULT_COMPRESSION; + } + } + + public double getPercentile() { + return _percentile; + } + + public int getThreshold() { + return _threshold; + } + + public int getCompression() { + return _compression; + } + + @Override + public AggregationFunctionType getType() { + return AggregationFunctionType.PERCENTILESMARTTDIGEST; + } + + @Override + public String getColumnName() { + return AggregationFunctionType.PERCENTILESMARTTDIGEST.getName() + _percentile + "_" + _expression; + } + + @Override + public String getResultColumnName() { + return AggregationFunctionType.PERCENTILESMARTTDIGEST.getName().toLowerCase() + "(" + _expression + ", " + + _percentile + ")"; + } + + @Override + public AggregationResultHolder createAggregationResultHolder() { + return new ObjectAggregationResultHolder(); + } + + @Override + public GroupByResultHolder createGroupByResultHolder(int initialCapacity, int maxCapacity) { + return new ObjectGroupByResultHolder(initialCapacity, maxCapacity); + } + + @Override + public void aggregate(int length, AggregationResultHolder aggregationResultHolder, + Map<ExpressionContext, BlockValSet> blockValSetMap) { + BlockValSet blockValSet = blockValSetMap.get(_expression); + validateValueType(blockValSet); Review Comment: Doing this once (upon first call to aggregate) should be enough ? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
