dongjoon-hyun opened a new pull request, #60470:
URL: https://github.com/apache/spark/pull/60470

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue. The following aggregate functions return 
a non-NULL value on empty input, but they do not override 
`AggregateFunction.defaultResult`. This PR overrides it with the value that 
`eval` returns for a fresh aggregation buffer, as `CountMinSketchAgg` has done 
since SPARK-34876.
   
   | Function | Since |
   |---|---|
   | `hll_sketch_agg`, `hll_union_agg` | 3.5.0 |
   | `schema_of_variant_agg` | 4.0.0 |
   | `theta_sketch_agg`, `theta_union_agg`, `theta_intersection_agg` | 4.1.0 |
   | `kll_sketch_agg_{bigint,float,double}`, 
`kll_merge_agg_{bigint,float,double}` | 4.1.0 |
   | `approx_top_k`, `approx_top_k_accumulate`, `approx_top_k_combine` | 4.1.0 |
   | `tuple_sketch_agg_{double,integer}`, `tuple_union_agg_{double,integer}`, 
`tuple_intersection_agg_{double,integer}` | 4.2.0 |
   
   ```scala
   override def defaultResult: Option[Literal] =
     Option(Literal.create(eval(createAggregationBuffer()), dataType))
   ```
   
   For the KLL merge and tuple sketch aggregates, the override goes into the 
shared base classes (`KllMergeAggBase`, `TupleSketchAggBase`, 
`TupleUnionAggBase`, and `TupleIntersectionAggBase`).
   
   ### Why are the changes needed?
   
   `RewriteCorrelatedScalarSubquery` and `DecorrelateInnerQuery` use 
`defaultResult` to compute what a correlated scalar subquery returns when no 
inner row matches (the COUNT bug handling). `RewriteDistinctAggregates` uses it 
as the result of a regular aggregate when the input of a rewritten distinct 
aggregation is empty. Without `defaultResult`, these rules assume the result is 
NULL, which is wrong for the functions above. For example, `hll_sketch_agg` 
returns an empty sketch, and `hll_sketch_estimate` of it is `0`.
   
   ```sql
   CREATE TEMP VIEW a(k) AS VALUES (1), (2);
   CREATE TEMP VIEW b(k, v) AS VALUES (1, 100), (1, 200);
   
   -- Uncorrelated query over empty input returns 0.
   SELECT hll_sketch_estimate(hll_sketch_agg(v)) FROM b WHERE b.k = 2;
   
   -- Issue 1: It should return 0 for k = 2, but returns NULL.
   SELECT a.k, (SELECT hll_sketch_estimate(hll_sketch_agg(b.v)) FROM b WHERE 
b.k = a.k) FROM a;
   
   -- Issue 2: It should return 1 for k = 2, but returns 0.
   SELECT a.k, (SELECT COUNT(s) FROM (SELECT hll_sketch_agg(b.v) s FROM b WHERE 
b.k = a.k)) FROM a;
   
   -- Issue 3: It should return 0 and VOID, but returns NULL and NULL.
   SELECT count(DISTINCT k), count(DISTINCT v),
     hll_sketch_estimate(hll_sketch_agg(v)),
     schema_of_variant_agg(parse_json(CAST(v AS STRING)))
   FROM b WHERE k = 3;
   ```
   
   **Apache Spark 4.3.0**
   
   ```
   spark-sql (default)> SELECT a.k, (SELECT 
hll_sketch_estimate(hll_sketch_agg(b.v)) FROM b WHERE b.k = a.k) FROM a;
   1    2
   2    NULL
   spark-sql (default)> SELECT a.k, (SELECT COUNT(s) FROM (SELECT 
hll_sketch_agg(b.v) s FROM b WHERE b.k = a.k)) FROM a;
   1    1
   2    0
   spark-sql (default)> SELECT count(DISTINCT k), count(DISTINCT v),
                      >   hll_sketch_estimate(hll_sketch_agg(v)),
                      >   schema_of_variant_agg(parse_json(CAST(v AS STRING)))
                      > FROM b WHERE k = 3;
   0    0       NULL    NULL
   ```
   
   The same happens with every function in the table above in Apache Spark 
3.5.9, 4.0.5, 4.1.4, 4.2.1, and 4.3.0, as far as each release has the function.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The queries above return the correct results.
   
   In addition, when one of these functions has an invalid parameter (for 
example, `hll_sketch_agg(v, 30)`) in a correlated scalar subquery, the query 
now fails with the same error (`HLL_INVALID_LG_K`) even when no inner row 
matches any outer row. Previously such a query returned NULL without an error, 
while the equivalent uncorrelated aggregation over empty input already failed.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test queries in 
`scalar-subquery-select.sql`, one query for each function family.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


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