deemoliu commented on code in PR #11346: URL: https://github.com/apache/pinot/pull/11346#discussion_r1330687546
########## pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/DistinctCountHLLPlusAggregationFunction.java: ########## @@ -0,0 +1,473 @@ +/** + * 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.clearspring.analytics.stream.cardinality.HyperLogLogPlus; +import com.google.common.base.Preconditions; +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.common.ObjectSerDeUtils; +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.segment.spi.index.reader.Dictionary; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.apache.pinot.spi.utils.CommonConstants; +import org.roaringbitmap.PeekableIntIterator; +import org.roaringbitmap.RoaringBitmap; + + +public class DistinctCountHLLPlusAggregationFunction extends BaseSingleInputAggregationFunction<HyperLogLogPlus, Long> { + // The parameter "p" determines the precision of the sparse list in HyperLogLogPlus. + protected final int _p; + // The "sp" parameter specifies the number of standard deviations that the sparse list's precision should be set to. + protected final int _sp; + + public DistinctCountHLLPlusAggregationFunction(List<ExpressionContext> arguments) { + super(arguments.get(0)); + int numExpressions = arguments.size(); + // This function expects 1 or 2 or 3 arguments. + Preconditions.checkArgument(numExpressions <= 3, "DistinctCountHLLPlus expects 2 or 3 arguments, got: %s", + numExpressions); + if (arguments.size() == 2) { + _p = arguments.get(1).getLiteral().getIntValue(); + _sp = CommonConstants.Helix.DEFAULT_HYPERLOGLOG_PLUS_SP; + } else if (arguments.size() == 3) { + _p = arguments.get(1).getLiteral().getIntValue(); + _sp = arguments.get(2).getLiteral().getIntValue(); + } else { + _p = CommonConstants.Helix.DEFAULT_HYPERLOGLOG_PLUS_P; + _sp = CommonConstants.Helix.DEFAULT_HYPERLOGLOG_PLUS_SP; + } + } + + public int getP() { + return _p; + } + + public int getSp() { + return _sp; + } + + @Override + public AggregationFunctionType getType() { + return AggregationFunctionType.DISTINCTCOUNTHLLPLUS; + } + + @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); + + // Treat BYTES value as serialized HyperLogLogPlus + DataType storedType = blockValSet.getValueType().getStoredType(); + if (storedType == DataType.BYTES) { + byte[][] bytesValues = blockValSet.getBytesValuesSV(); + try { + HyperLogLogPlus hyperLogLogPlus = aggregationResultHolder.getResult(); + if (hyperLogLogPlus != null) { + for (int i = 0; i < length; i++) { Review Comment: thanks for code review! addressed the code comment, please take a look -- 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