[ 
https://issues.apache.org/jira/browse/SPARK-61981?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Milos Stojko updated SPARK-61981:
---------------------------------
    Description: 
PR that adds this - [https://github.com/apache/spark/pull/60663]

Problem - The docs of {{kll_sketch_merge_bigint}}, {{kll_sketch_merge_float}} 
and {{kll_sketch_merge_double}} do not say which k the result has. They also 
say that incompatible sketch types throw an error. That is not always true: a 
bigint and a double sketch can merge without an error and give wrong values, 
because the sketch bytes do not record the item type.

Change - Document the behavior that master already has: the result has the k of 
{{left}}. The reported error bound follows the smallest k that {{left}}, or a 
compacted {{right}}, has used, so the result can show a larger k than its 
accuracy. An empty {{right}} changes nothing, a NULL input gives NULL, and a 
fold takes the k of its seed. The usage text, the API docs and the SQL 
reference say so. The error claim is narrowed to invalid buffers and mixed 
types.

Behavior - No change.


  was:
PR that adds this - [https://github.com/apache/spark/pull/60663]

Problem - {{kll_sketch_merge_bigint}}, {{kll_sketch_merge_float}} and 
{{kll_sketch_merge_double}} return a sketch with the k of {{left}}, whatever 
the k of {{right}} is:
 * {{merge(a, b)}} and {{merge(b, a)}} can return a different k, and so can two 
nestings of the same merges.
 * If {{right}} has started compacting at a smaller k, the result keeps the 
larger k of {{left}}. The result 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 sketch of an 
all-NULL group has k = 200. Merged with an exact k = 1000 sketch of 500 values, 
the result has k = 200 and compacts, so it loses accuracy that the data had.
 * SPARK-61368 adds a rule for k to {{kll_merge_agg_*}}. Without the same rule 
here, {{kll_sketch_merge_*(a, b)}} and {{kll_merge_agg_*}} over the rows a, b 
can return different k.

Proposal:
 * Add an internal SQL conf, on by default: 
{{spark.sql.function.kllMerge.lowestCompactingK.enabled}}. Set it to false to 
get the k of {{left}} again.
 * When it is on, {{kll_sketch_merge_*(left, right)}} returns the smaller k of 
the two inputs, compacting or exact. An empty input counts only if both inputs 
are empty. A NULL input still gives NULL.
 * So the k does not depend on the order of the inputs or on how nested merges 
are grouped. This is the rule of SPARK-61368 for two inputs.

Repro: this query returns k = 100 with the conf on (the default), and k = 1000 
with it off.
{code:sql}
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));
{code}



> kll_sketch_merge_*: add a conf to take the lowest k among the first non-empty 
> input and a compacting right input
> ----------------------------------------------------------------------------------------------------------------
>
>                 Key: SPARK-61981
>                 URL: https://issues.apache.org/jira/browse/SPARK-61981
>             Project: Spark
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 5.0.0
>            Reporter: Milos Stojko
>            Priority: Major
>              Labels: pull-request-available
>
> PR that adds this - [https://github.com/apache/spark/pull/60663]
> Problem - The docs of {{kll_sketch_merge_bigint}}, {{kll_sketch_merge_float}} 
> and {{kll_sketch_merge_double}} do not say which k the result has. They also 
> say that incompatible sketch types throw an error. That is not always true: a 
> bigint and a double sketch can merge without an error and give wrong values, 
> because the sketch bytes do not record the item type.
> Change - Document the behavior that master already has: the result has the k 
> of {{left}}. The reported error bound follows the smallest k that {{left}}, 
> or a compacted {{right}}, has used, so the result can show a larger k than 
> its accuracy. An empty {{right}} changes nothing, a NULL input gives NULL, 
> and a fold takes the k of its seed. The usage text, the API docs and the SQL 
> reference say so. The error claim is narrowed to invalid buffers and mixed 
> types.
> Behavior - No change.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to