-- Make sure the non-STRICT transition functions correctly handle NULLs passed
-- to them. NULL percentiles/values should be rejected, because the aggregate
-- can't return anything sensible.

\set VERBOSITY terse

-- The array is only read while the aggregate state is still NULL, i.e. on the
-- first row with a non-NULL value, so a single row is enough.

-- tdigest_add_double_array(internal, double precision, int, double precision[])
SELECT tdigest_percentile(v, 100, NULL::double precision[])
  FROM (VALUES (1.0::double precision), (2.0)) s(v);

-- tdigest_add_double_array_count(internal, double precision, bigint, int, double precision[])
SELECT tdigest_percentile(v, 2, 100, NULL::double precision[])
  FROM (VALUES (1.0::double precision), (2.0)) s(v);

-- tdigest_add_digest_array(internal, tdigest, double precision[])
SELECT tdigest_percentile(d, NULL::double precision[])
  FROM (SELECT tdigest(v, 100) AS d
          FROM (VALUES (1.0::double precision), (2.0)) s(v)) t;

-- tdigest_add_double_array_values(internal, double precision, int, double precision[])
SELECT tdigest_percentile_of(v, 100, NULL::double precision[])
  FROM (VALUES (1.0::double precision), (2.0)) s(v);

-- tdigest_add_double_array_values_count(internal, double precision, bigint, int, double precision[])
SELECT tdigest_percentile_of(v, 2, 100, NULL::double precision[])
  FROM (VALUES (1.0::double precision), (2.0)) s(v);

-- tdigest_add_digest_array_values(internal, tdigest, double precision[])
SELECT tdigest_percentile_of(d, NULL::double precision[])
  FROM (SELECT tdigest(v, 100) AS d
          FROM (VALUES (1.0::double precision), (2.0)) s(v)) t;

-- A NULL array on a later row is harmless (the state already exists), and
-- tdigest_add_double_array_increment() guards the same call correctly. Both
-- work before and after the fix.
SELECT tdigest_percentile(v, 100, CASE WHEN v = 1.0 THEN ARRAY[0.5] END)
  FROM (VALUES (1.0::double precision), (2.0)) s(v);

SELECT tdigest_count(tdigest_add(NULL::tdigest, NULL::double precision[], 100));
