gortiz commented on code in PR #19905:
URL: https://github.com/apache/pinot/pull/19905#discussion_r4296188582


##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AnyValueAggregationFunction.java:
##########
@@ -197,10 +198,12 @@ private void aggregateHelper(int length, BlockValSet bvs, 
ValueProcessor<Object>
         }
       });
     } else {
-      // Fall back to direct value access based on type
+      // Fall back to direct value access based on type. Read the values once 
per block: for a transform input, each
+      // get*ValuesSV() call evaluates the transform on the whole block again.
+      IntFunction<Object> valueReader = getDirectValueReader(bvs);

Review Comment:
   Right, an all-null or empty block now evaluates the transform once instead 
of zero times. I kept it simple on purpose: one evaluation per block is the 
normal cost for any aggregation over a transform, and making the reader lazy 
adds a branch on the hot path for an edge case.



##########
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/function/AnyValueAggregationFunction.java:
##########
@@ -231,23 +234,38 @@ private Object getDictionaryValue(Dictionary dict, int 
dictId, DataType storedTy
     }
   }
 
-  /// Get value directly from BlockValSet based on data type
-  private Object getDirectValue(BlockValSet bvs, int index) {
+  /// Returns a reader for the values of the BlockValSet based on data type. 
The values are read once, when this method
+  /// is called.
+  private IntFunction<Object> getDirectValueReader(BlockValSet bvs) {

Review Comment:
   Added a BYTES case. It checks that `getBytesValuesSV()` is called once per 
block and that each group gets `new ByteArray(values[i])`.



##########
pinot-core/src/test/java/org/apache/pinot/core/query/aggregation/function/AnyValueGroupByRecomputationTest.java:
##########
@@ -0,0 +1,70 @@
+/**
+ * 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 java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.core.common.BlockValSet;
+import org.apache.pinot.core.query.aggregation.groupby.GroupByResultHolder;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.testng.annotations.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockingDetails;
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+
+
+/// Counts how many times ANY_VALUE group-by reads the values of a 
non-dictionary input per block.
+///
+/// A transform input (for example `jsonExtractScalar`) recomputes the whole 
block on every `get*ValuesSV()` call,
+/// so the number of calls per block multiplies the transform cost.
+public class AnyValueGroupByRecomputationTest {
+  private static final int NUM_DOCS = 1000;
+
+  @Test
+  public void testGroupByReadsNonDictionaryValuesOncePerBlock() {
+    ExpressionContext expression = 
ExpressionContext.forIdentifier("jsonExtractResult");
+    AnyValueAggregationFunction function = new 
AnyValueAggregationFunction(List.of(expression), false);
+
+    String[] values = new String[NUM_DOCS];
+    int[] groupKeys = new int[NUM_DOCS];
+    for (int i = 0; i < NUM_DOCS; i++) {
+      values[i] = "zone-" + (i % 3);
+      groupKeys[i] = i;
+    }
+    BlockValSet valueSet = mock(BlockValSet.class);
+    when(valueSet.getValueType()).thenReturn(DataType.STRING);
+    when(valueSet.isDictionaryEncoded()).thenReturn(false);
+    when(valueSet.isSingleValue()).thenReturn(true);

Review Comment:
   Removed, thanks.



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

Reply via email to