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

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

Problem - all regarding {{kll_merge_agg_bigint}}, {{kll_merge_agg_float}} and 
{{kll_merge_agg_double}} without k:
 * The result takes the k of the first input sketch that the aggregate reads.
 * This can cause high instability in terms of memory, performance, and output 
accuracy: if the first sketch chosen is k=8, it will have high error bounds and 
use very little memory, but it's also possible that the very same query uses 
k=65000. Which k is chosen is non-deterministic (depends on first sketch 
encountered, which is undefined)

Proposal:
 * Add an internal SQL conf, on by default: 
{{spark.sql.function.kllMerge.lowestK.enabled}}.
 * When it is on, a merge without k returns the smallest k among the non-empty 
input sketches, compacting or exact. If every input sketch is empty, it returns 
the smallest k of them. With no non-NULL input, it returns an empty sketch with 
k = 200. An explicit k does not change.
 * So the k does not depend on the order or the partitioning of the inputs.
 * Trade-off: a small sketch that never compacted can lower the k, so the 
result can be less exact than the larger input. Pass k to keep a larger k.
 * The scalar {{kll_sketch_merge_*}} functions do not change (SPARK-61981 only 
documents them): they keep the k of the left sketch. So for the same two 
sketches, the scalar and the aggregate merge can return a different k.

h3. Why this rule
 * A larger k than the smallest compacted k would claim more accuracy than the 
result has. For example, a k = 65000 sketch that took in a compacted k = 8 
sketch is only as accurate as k = 8, and the larger k only costs memory.
 * A smaller k would throw away accuracy that the inputs still have.
 * Considered first: let only inputs that compacted lower the k, and otherwise 
keep the k of the first input. This is what BigQuery does 
([https://cloud.google.com/bigquery/docs/sketches#precision_kll]). It is not 
deterministic in Spark, because whether a buffer compacts depends on the merge 
order and the partitioning. For example, (k = 10, 7 values) merged with (k = 
20, 4 values) compacts at k = 10 when the k = 10 buffer is the target, and does 
not compact when the k = 20 buffer is the target.
 * Chosen: the smallest k among the non-empty inputs, compacted or not. A 
minimum does not depend on the order, so every plan gives the same k. Empty 
inputs count only when every input is empty. With no non-NULL input, the result 
has k = 200.


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

Problem - all regarding {{kll_merge_agg_bigint}}, {{kll_merge_agg_float}} and 
{{kll_merge_agg_double}} without k:
 * The result takes the k of the first input sketch that the aggregate reads.
 * This can cause high instability in terms of memory, performance, and output 
accuracy: if the first sketch chosen is k=8, it will have high error bounds and 
use very little memory, but it's also possible that the very same query uses 
k=65000. Which k is chosen is non-deterministic (depends on first sketch 
encountered, which is undefined)

Proposal:
 * Add an internal SQL conf, on by default: 
{{spark.sql.function.kllMerge.lowestK.enabled}}.
 * When it is on, a merge without k returns the smallest k among the non-empty 
input sketches, compacting or exact. If every input sketch is empty, it returns 
the smallest k of them. With no non-NULL input, it returns an empty sketch with 
k = 200. An explicit k does not change.
 * So the k does not depend on the order or the partitioning of the inputs.
 * Trade-off: a small sketch that never compacted can lower the k, so the 
result can be less exact than the larger input. Pass k to keep a larger k.
 * The scalar {{kll_sketch_merge_*}} functions do not change (SPARK-61981 only 
documents them): they keep the k of the left sketch. So for the same two 
sketches, the scalar and the aggregate merge can return a different k.



> kll_merge_agg without k: add a conf to take the lowest k among inputs that 
> have started compacting
> --------------------------------------------------------------------------------------------------
>
>                 Key: SPARK-61368
>                 URL: https://issues.apache.org/jira/browse/SPARK-61368
>             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/60301]
> Problem - all regarding {{kll_merge_agg_bigint}}, {{kll_merge_agg_float}} and 
> {{kll_merge_agg_double}} without k:
>  * The result takes the k of the first input sketch that the aggregate reads.
>  * This can cause high instability in terms of memory, performance, and 
> output accuracy: if the first sketch chosen is k=8, it will have high error 
> bounds and use very little memory, but it's also possible that the very same 
> query uses k=65000. Which k is chosen is non-deterministic (depends on first 
> sketch encountered, which is undefined)
> Proposal:
>  * Add an internal SQL conf, on by default: 
> {{spark.sql.function.kllMerge.lowestK.enabled}}.
>  * When it is on, a merge without k returns the smallest k among the 
> non-empty input sketches, compacting or exact. If every input sketch is 
> empty, it returns the smallest k of them. With no non-NULL input, it returns 
> an empty sketch with k = 200. An explicit k does not change.
>  * So the k does not depend on the order or the partitioning of the inputs.
>  * Trade-off: a small sketch that never compacted can lower the k, so the 
> result can be less exact than the larger input. Pass k to keep a larger k.
>  * The scalar {{kll_sketch_merge_*}} functions do not change (SPARK-61981 
> only documents them): they keep the k of the left sketch. So for the same two 
> sketches, the scalar and the aggregate merge can return a different k.
> h3. Why this rule
>  * A larger k than the smallest compacted k would claim more accuracy than 
> the result has. For example, a k = 65000 sketch that took in a compacted k = 
> 8 sketch is only as accurate as k = 8, and the larger k only costs memory.
>  * A smaller k would throw away accuracy that the inputs still have.
>  * Considered first: let only inputs that compacted lower the k, and 
> otherwise keep the k of the first input. This is what BigQuery does 
> ([https://cloud.google.com/bigquery/docs/sketches#precision_kll]). It is not 
> deterministic in Spark, because whether a buffer compacts depends on the 
> merge order and the partitioning. For example, (k = 10, 7 values) merged with 
> (k = 20, 4 values) compacts at k = 10 when the k = 10 buffer is the target, 
> and does not compact when the k = 20 buffer is the target.
>  * Chosen: the smallest k among the non-empty inputs, compacted or not. A 
> minimum does not depend on the order, so every plan gives the same k. Empty 
> inputs count only when every input is empty. With no non-NULL input, the 
> result has k = 200.



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