stojkomilos opened a new pull request, #60663:
URL: https://github.com/apache/spark/pull/60663

   ### What changes were proposed in this pull request?
   
   We add an internal SQL conf, 
`spark.sql.function.kllSketchMerge.lowestCompactingK.enabled`, off by default. 
When it is on, `kll_sketch_merge_bigint`, `kll_sketch_merge_float` and 
`kll_sketch_merge_double(left, right)` return a sketch with k = min(k of the 
first non-empty input, k of `right` if `right` has started compacting 
(estimation mode)).
   
   - An exact `right` never lowers k.
   - An empty input counts as absent: `merge(empty, x)` takes the k of `x`. Two 
empty inputs give the default k 200. A NULL input still gives NULL.
   - `KllSketchMergeBase.mergeTargetAndInput` picks the merge target. A 
DataSketches merge keeps the k of its target, so when the result takes the k of 
`right`, `left` is merged into a heap copy of `right`.
   - The rule applies to each call, so `merge(a, merge(b, c))` can get a 
smaller k than `merge(merge(a, b), c)`.
   - The three classes capture the conf when the function is resolved, in a 
constructor parameter. Plan strings do not show it.
   
   This is the rule that SPARK-61368 (#60301) adds to `kll_merge_agg_*`, for 
two inputs. It is also the rule of BigQuery's KLL merge. Its docs say that with 
different precisions, "the precision is downgraded to the lowest precision 
involved in the merge" 
([`KLL_QUANTILES.MERGE_PARTIAL`](https://cloud.google.com/bigquery/docs/reference/standard-sql/kll_functions#kll_quantilesmerge_partial)).
   
   ### Why are the changes needed?
   
   Today `kll_sketch_merge_*(left, right)` returns the k of `left`:
   
   - If `right` has compacted at a smaller k, the result keeps the larger k of 
`left`. It is larger, but not more accurate: DataSketches already uses the 
smaller k for the error bounds.
   - An empty `left` also decides k. For example, the empty k = 200 sketch of 
an all-NULL group makes an exact k = 1000 `right` with 500 values compact at k 
= 200.
   - With SPARK-61368, `kll_merge_agg_*` follows the rule above. Without this 
PR, `kll_sketch_merge_*(a, b)` and `kll_merge_agg_*` over the rows a, b can 
return different k.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The new conf is off by default.
   
   With the conf on, the k of a `kll_sketch_merge_*` result can change, and so 
can its size and quantile estimates. Repro: with the conf off, this query 
returns k = 1000. With the conf on, it returns k = 100.
   
   ```sql
   SET spark.sql.function.kllSketchMerge.lowestCompactingK.enabled = true;
   SELECT regexp_extract(kll_sketch_to_string_bigint(kll_sketch_merge_bigint(l, 
r)), 'K +: ([0-9]+)', 1) AS k
   FROM (SELECT kll_sketch_agg_bigint(id, 1000) AS l FROM range(10)),
        (SELECT kll_sketch_agg_bigint(id, 100) AS r FROM range(100000));
   ```
   
   ### How was this patch tested?
   
   **Added**
   
   `DataFrameAggregateSuite` (5 tests): each test merges sketches with 
different k and sizes, and asserts the k of the result.
   
   - k rule (1 test, 3 types x 2 conf values x 116 input sets): 16 named sets 
and 100 random chains of 2 to 5 inputs. With the conf on, the result has the 
rule's k. With it off, it has the k of `left`. n is always the sum of the 
inputs.
   - Edge cases (3 separate tests): a nested merge counts a compacted inner 
result, a DataFrame keeps the conf value from when it was resolved, and a 
result with the k of `right` keeps the rank of the median within 0.1.
   - NULL input (1 test, 3 types x 2 conf values): a NULL `left`, `right`, or 
both gives NULL.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude Code, Claude Opus 5.5)
   
   This contribution is my original work, and I license the work to the project 
under the project's open source license.
   
   This pull request and its description were written by Isaac.
   


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