chenboat commented on code in PR #10867:
URL: https://github.com/apache/pinot/pull/10867#discussion_r1232868278


##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/FunnelCountAggregationFunction.java:
##########
@@ -0,0 +1,512 @@
+/**
+ * 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.longs.LongArrayList;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import javax.annotation.concurrent.ThreadSafe;
+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.segment.spi.index.reader.Dictionary;
+import org.roaringbitmap.RoaringBitmap;
+
+
+/**
+ * The {@code FunnelCountAggregationFunction} calculates the number of step 
conversions for a given partition column and
+ * a list of boolean expressions.
+ * <p>IMPORTANT: This function relies on the partition column being 
partitioned for each segment, where there are no
+ * common values across different segments.
+ * <p>This function calculates the exact number of step matches per partition 
key within the segment, then sums up the
+ * results from different segments.
+ *
+ * Example:
+ *   SELECT
+ *    dateTrunc('day', timestamp) AS ts,
+ *    FUNNEL_COUNT(
+ *      STEPS(url = '/addToCart', url = '/checkout', url = 
'/orderConfirmation'),
+ *      CORRELATED_BY(user)
+ *    ) as step_counts
+ *    FROM user_log
+ *    WHERE url in ('/addToCart', '/checkout', '/orderConfirmation')
+ *    GROUP BY 1
+ */
+public class FunnelCountAggregationFunction implements 
AggregationFunction<List<Long>, LongArrayList> {
+  final List<ExpressionContext> _expressions;
+  final List<ExpressionContext> _stepExpressions;
+  final List<ExpressionContext> _correlateByExpressions;
+  final ExpressionContext _primaryCorrelationCol;
+  final int _numSteps;
+
+  final SegmentAggregationStrategy<?, List<Long>> _sortedAggregationStrategy;
+  final SegmentAggregationStrategy<?, List<Long>> _bitmapAggregationStrategy;
+
+  public FunnelCountAggregationFunction(List<ExpressionContext> expressions) {
+    _expressions = expressions;
+    _correlateByExpressions = 
Option.CORRELATE_BY.getInputExpressions(expressions);
+    _primaryCorrelationCol = 
Option.CORRELATE_BY.getFirstInputExpression(expressions);
+    _stepExpressions = Option.STEPS.getInputExpressions(expressions);
+    _numSteps = _stepExpressions.size();
+    _sortedAggregationStrategy = new SortedAggregationStrategy();
+    _bitmapAggregationStrategy = new BitmapAggregationStrategy();
+  }
+
+  @Override
+  public String getResultColumnName() {
+    return getType().getName().toLowerCase() + "(" + 
_expressions.stream().map(ExpressionContext::toString)
+        .collect(Collectors.joining(",")) + ")";
+  }
+
+  @Override
+  public List<ExpressionContext> getInputExpressions() {
+    final List<ExpressionContext> inputs = new ArrayList<>();
+    inputs.addAll(_correlateByExpressions);
+    inputs.addAll(_stepExpressions);
+    return inputs;
+  }
+
+  @Override
+  public AggregationFunctionType getType() {
+    return AggregationFunctionType.FUNNELCOUNT;
+  }
+
+  @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) {
+    getAggregationStrategyByBlockValSetMap(blockValSetMap).aggregate(length, 
aggregationResultHolder, blockValSetMap);
+  }
+
+  @Override
+  public void aggregateGroupBySV(int length, int[] groupKeyArray, 
GroupByResultHolder groupByResultHolder,
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    
getAggregationStrategyByBlockValSetMap(blockValSetMap).aggregateGroupBySV(length,
 groupKeyArray,
+        groupByResultHolder, blockValSetMap);
+  }
+
+  @Override
+  public void aggregateGroupByMV(int length, int[][] groupKeysArray, 
GroupByResultHolder groupByResultHolder,
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    
getAggregationStrategyByBlockValSetMap(blockValSetMap).aggregateGroupByMV(length,
 groupKeysArray,
+        groupByResultHolder, blockValSetMap);
+  }
+
+  @Override
+  public List<Long> extractAggregationResult(AggregationResultHolder 
aggregationResultHolder) {
+    return 
getAggregationStrategyByAggregationResult(aggregationResultHolder.getResult()).extractAggregationResult(
+        aggregationResultHolder);
+  }
+
+  @Override
+  public List<Long> extractGroupByResult(GroupByResultHolder 
groupByResultHolder, int groupKey) {
+    return 
getAggregationStrategyByAggregationResult(groupByResultHolder.getResult(groupKey)).extractGroupByResult(
+        groupByResultHolder, groupKey);
+  }
+
+  @Override
+  public List<Long> merge(List<Long> a, List<Long> b) {
+    int length = a.size();
+    Preconditions.checkState(length == b.size(), "The two operand arrays are 
not of the same size! provided %s, %s",
+        length, b.size());
+
+    LongArrayList result = toLongArrayList(a);
+    long[] elements = result.elements();
+    for (int i = 0; i < length; i++) {
+      elements[i] += b.get(i);
+    }
+    return result;
+  }
+
+  @Override
+  public ColumnDataType getIntermediateResultColumnType() {
+    return ColumnDataType.OBJECT;
+  }
+
+  @Override
+  public ColumnDataType getFinalResultColumnType() {
+    return ColumnDataType.LONG_ARRAY;
+  }
+
+  @Override
+  public LongArrayList extractFinalResult(List<Long> result) {
+    return toLongArrayList(result);
+  }
+
+  @Override
+  public String toExplainString() {
+    StringBuilder stringBuilder = new 
StringBuilder(getType().getName()).append('(');
+    int numArguments = getInputExpressions().size();
+    if (numArguments > 0) {
+      stringBuilder.append(getInputExpressions().get(0).toString());
+      for (int i = 1; i < numArguments; i++) {
+        stringBuilder.append(", 
").append(getInputExpressions().get(i).toString());
+      }
+    }
+    return stringBuilder.append(')').toString();
+  }
+
+  private static LongArrayList toLongArrayList(List<Long> longList) {
+    return longList instanceof LongArrayList ? ((LongArrayList) 
longList).clone() : new LongArrayList(longList);
+  }
+
+  private int[] getCorrelationIds(Map<ExpressionContext, BlockValSet> 
blockValSetMap) {
+    return blockValSetMap.get(_primaryCorrelationCol).getDictionaryIdsSV();
+  }
+
+  private int[][] getSteps(Map<ExpressionContext, BlockValSet> blockValSetMap) 
{
+    final int[][] steps = new int[_numSteps][];
+    for (int n = 0; n < _numSteps; n++) {
+      final BlockValSet stepBlockValSet = 
blockValSetMap.get(_stepExpressions.get(n));
+      steps[n] = stepBlockValSet.getIntValuesSV();
+    }
+    return steps;
+  }
+
+  private boolean isSorted(Map<ExpressionContext, BlockValSet> blockValSetMap) 
{
+    final Dictionary primaryCorrelationDictionary = 
blockValSetMap.get(_primaryCorrelationCol).getDictionary();
+    if (primaryCorrelationDictionary == null) {
+      throw new IllegalArgumentException(
+          "CORRELATE_BY column in FUNNELCOUNT aggregation function not 
supported, please use a dictionary encoded "
+              + "column.");
+    }
+    return primaryCorrelationDictionary.isSorted();
+  }
+
+  private SegmentAggregationStrategy<?, List<Long>> 
getAggregationStrategyByBlockValSetMap(
+      Map<ExpressionContext, BlockValSet> blockValSetMap) {
+    return isSorted(blockValSetMap) ? _sortedAggregationStrategy : 
_bitmapAggregationStrategy;
+  }
+
+  private SegmentAggregationStrategy<?, List<Long>> 
getAggregationStrategyByAggregationResult(Object aggResult) {
+    return aggResult instanceof SortedAggregationResult ? 
_sortedAggregationStrategy : _bitmapAggregationStrategy;
+  }
+
+  enum Option {
+    STEPS("steps"),
+    //SEQUENCE_BY("sequenceby"),

Review Comment:
   remove commented line.



-- 
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

Reply via email to