Yan Yan created SPARK-62373:
-------------------------------

             Summary: Return NULL from KLL quantile getters for an empty sketch
                 Key: SPARK-62373
                 URL: https://issues.apache.org/jira/browse/SPARK-62373
             Project: Spark
          Issue Type: Improvement
          Components: SQL
    Affects Versions: 4.1.0
            Reporter: Yan Yan
             Fix For: 4.5.0


KLL quantile getters currently throw KLL_INVALID_INPUT_SKETCH_BUFFER for a 
valid empty sketch. For example, kll_sketch_agg_bigint returns a valid sketch 
with n = 0 for a group whose values are all NULL, but querying its quantile 
fails the query.

Reproduction:
{code:sql}
SELECT k,
       kll_sketch_get_n_bigint(kll_sketch_agg_bigint(v)) AS n,
       kll_sketch_get_quantile_bigint(kll_sketch_agg_bigint(v), 0.5D) AS median
FROM VALUES ('a', -10L), ('a', 0L), ('a', 10L),
            ('b', CAST(NULL AS BIGINT)) AS t(k, v)
GROUP BY k
ORDER BY k;
{code}

Current behavior: the query fails with KLL_INVALID_INPUT_SKETCH_BUFFER when the 
getter reads the valid empty sketch for group b.

Proposed behavior:
{code}
k  n  median
a  3  0
b  0  NULL
{code}

Return NULL for a valid empty sketch from kll_sketch_get_quantile_bigint, 
kll_sketch_get_quantile_float and kll_sketch_get_quantile_double, for both 
scalar ranks and rank arrays. For an empty sketch and a valid rank array, 
return NULL for the whole result. These quantiles are undefined, and 
percentile_approx also returns NULL when there are no non-NULL input values.

Preserve the existing handling of NULL inputs, NULL rank-array elements, 
unreadable sketch buffers, and non-empty sketches. Invalid ranks must still 
fail with KLL_SKETCH_INVALID_QUANTILE_RANGE for a valid empty sketch. The three 
kll_sketch_get_rank_* functions are outside this change and retain their 
existing behavior.

The quantile expressions must also be marked nullable, because the new behavior 
permits a NULL result even when both inputs are non-NULL. This metadata change 
is required so optimization and generated code handle the new NULL results 
correctly.

This is a user-visible behavior change from an error to NULL, targeted at Spark 
4.5. Existing callers can guard the getter with kll_sketch_get_n_* when they 
need to avoid errors for empty sketches on older versions.

Pull request: https://github.com/apache/spark/pull/60832



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