prestodb / presto

The official home of the Presto distributed SQL query engine for big data
http://prestodb.io
Apache License 2.0
16.05k stars 5.38k forks source link

quantile_at_value returning incorrect value for tdigest #23677

Open ericGanbat opened 1 month ago

ericGanbat commented 1 month ago

quantile_at_value returning incorrect value for tdigest, however qdigest returning correct value

50% of value is 1. Tdigest returns quantile 0.75 for 1, and qdigest returns correct 0.5 value

WITH test AS (
    SELECT
        *
    FROM (
        VALUES
            (0.1),(0.2),(0.2),(0.3),(0.4),(0.5),(0.5),(0.5),(0.6),(0.6),
            (1.0),(1.0),(1.0),(1.0),(1.0),(1.0),(1.0),(1.0),(1.0),(1.0)
    ) AS t(val)
)

SELECT
    QUANTILE_AT_VALUE(
        CAST(CAST(TDIGEST_AGG(val) AS VARBINARY) AS TDIGEST(DOUBLE)),
        1.0
    ) AS p100,
    QUANTILE_AT_VALUE(
        CAST(CAST(TDIGEST_AGG(val) AS VARBINARY) AS TDIGEST(DOUBLE)),
        0.9999
    ) AS p9999,
        QUANTILE_AT_VALUE(
        CAST(CAST(qdigest_agg(val) AS VARBINARY) AS QDIGEST(DOUBLE)),
        1.0
    ) AS q_digest
FROM test
Screenshot 2024-09-18 at 11 47 43 AM
tdcmeehan commented 1 month ago

I'm a little confused. Isn't the quantile at value for 1 both 0.75 and 0.5 in your example?