xiangfu0 commented on code in PR #20061:
URL: https://github.com/apache/pinot/pull/20061#discussion_r4497191328


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MvKeyExpander.java:
##########
@@ -0,0 +1,151 @@
+/**
+ * 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.query.runtime.operator;
+
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.utils.ByteArray;
+
+
+/// Expands the value of a multi-value (MV) group key into the scalar elements 
that form its groups.
+///
+/// The value is what an MV column holds in a multi-stage block: a primitive 
array (`int[]`, `long[]`, `float[]`,
+/// `double[]`), a `String[]`, a `byte[][]`, any other `Object[]` (for example 
`BigDecimal[]`), or a [List] (scalar
+/// functions return these). The rules follow the single-stage engine 
semantics of a group by on an MV column:
+/// - A `null` value expands to one `null` element, so the row forms the 
`null` group.
+/// - An empty array expands to no element, so the row contributes to no group.
+/// - A repeated element stays repeated, so the row counts once per copy.
+///
+/// A `byte[]` element is wrapped in a [ByteArray], the representation of a 
`BYTES` key in the multi-stage engine,
+/// so that equal byte sequences map to the same group.
+///
+/// The class is stateless and thread-safe.
+final class MvKeyExpander {
+  private static final Object[] NULL_ELEMENT = new Object[]{null};
+  private static final Object[] NO_ELEMENT = new Object[0];
+
+  private MvKeyExpander() {
+  }
+
+  /// Returns the elements of `value`. The caller must not modify the returned 
array.
+  ///
+  /// @throws IllegalArgumentException when `value` is not an array or a [List]
+  static Object[] elements(@Nullable Object value) {
+    if (value == null) {

Review Comment:
   **Blocker: wrong results in the default config.** This puts a null into a 
key that `ARRAY_TO_MV` types as NOT NULL, so Calcite has already folded away 
null checks on it. Repro at this SHA (in-process runtime, null handling off, 
`auto`): `s` has 600 rows, `d` matches 300 of them, and each `d.dmv` is `[dN, 
dx]`.
   - `SELECT COALESCE(ARRAY_TO_MV(d.dmv), 'none'), COUNT(*) FROM s LEFT JOIN d 
ON s.k = d.k GROUP BY 1` returns `null=300`, not `none=300`.
   - `CASE WHEN ARRAY_TO_MV(d.dmv) IS NULL THEN 'none' ELSE ... END` gives the 
same result.
   
   With `useMvAggregate=false`, as on master, both queries fail with the 
`ARRAY_TO_MV` validation error, so `auto` turns an error into a silent wrong 
result. One fix: skip the MV aggregate, and the `auto` fallback, when an MV 
key's array operand is nullable, until the type follow-up lands.



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