exec-ddl
CREATE TABLE a (k INT PRIMARY KEY, i INT, f FLOAT, s STRING, j JSON, arr int[])
----

# --------------------------------------------------
# EliminateAggDistinct
# --------------------------------------------------

norm expect=EliminateAggDistinct
SELECT min(DISTINCT i), max(DISTINCT i), bool_and(DISTINCT i>f), bool_or(DISTINCT i>f), corr(DISTINCT k, i) FROM a
----
scalar-group-by
 ├── columns: min:9 max:10 bool_and:12 bool_or:13 corr:14
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(9,10,12-14)
 ├── project
 │    ├── columns: column11:11 k:1!null i:2
 │    ├── key: (1)
 │    ├── fd: (1)-->(2,11)
 │    ├── scan a
 │    │    ├── columns: k:1!null i:2 f:3
 │    │    ├── key: (1)
 │    │    └── fd: (1)-->(2,3)
 │    └── projections
 │         └── i:2 > f:3 [as=column11:11, outer=(2,3)]
 └── aggregations
      ├── min [as=min:9, outer=(2)]
      │    └── i:2
      ├── max [as=max:10, outer=(2)]
      │    └── i:2
      ├── bool-and [as=bool_and:12, outer=(11)]
      │    └── column11:11
      ├── bool-or [as=bool_or:13, outer=(11)]
      │    └── column11:11
      └── corr [as=corr:14, outer=(1,2)]
           ├── k:1
           └── i:2

# The rule should still work when FILTER is present.
norm expect=EliminateAggDistinct
SELECT
    min(DISTINCT i) FILTER (WHERE i > 5),
    max(DISTINCT i) FILTER (WHERE i > 5),
    bool_and(DISTINCT i>f) FILTER (WHERE f > 0.0),
    bool_or(DISTINCT i>f) FILTER (WHERE f > 1.0),
    corr(DISTINCT k, i) FILTER(WHERE k > 5 AND i > 5)
FROM a
----
scalar-group-by
 ├── columns: min:10 max:11 bool_and:14 bool_or:16 corr:18
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(10,11,14,16,18)
 ├── project
 │    ├── columns: column9:9 column12:12 column13:13 column15:15 column17:17 k:1!null i:2
 │    ├── key: (1)
 │    ├── fd: (1)-->(2,9,12,13,15,17), (2)-->(9)
 │    ├── scan a
 │    │    ├── columns: k:1!null i:2 f:3
 │    │    ├── key: (1)
 │    │    └── fd: (1)-->(2,3)
 │    └── projections
 │         ├── i:2 > 5 [as=column9:9, outer=(2)]
 │         ├── i:2 > f:3 [as=column12:12, outer=(2,3)]
 │         ├── f:3 > 0.0 [as=column13:13, outer=(3)]
 │         ├── f:3 > 1.0 [as=column15:15, outer=(3)]
 │         └── (k:1 > 5) AND (i:2 > 5) [as=column17:17, outer=(1,2)]
 └── aggregations
      ├── agg-filter [as=min:10, outer=(2,9)]
      │    ├── min
      │    │    └── i:2
      │    └── column9:9
      ├── agg-filter [as=max:11, outer=(2,9)]
      │    ├── max
      │    │    └── i:2
      │    └── column9:9
      ├── agg-filter [as=bool_and:14, outer=(12,13)]
      │    ├── bool-and
      │    │    └── column12:12
      │    └── column13:13
      ├── agg-filter [as=bool_or:16, outer=(12,15)]
      │    ├── bool-or
      │    │    └── column12:12
      │    └── column15:15
      └── agg-filter [as=corr:18, outer=(1,2,17)]
           ├── corr
           │    ├── k:1
           │    └── i:2
           └── column17:17

# The rule should not apply to these aggregations.
norm expect-not=EliminateAggDistinct
SELECT
    count(DISTINCT i),
    sum(DISTINCT i) FILTER (WHERE i > 5),
    sum_int(DISTINCT i),
    avg(DISTINCT i),
    stddev(DISTINCT f),
    variance(DISTINCT f),
    xor_agg(DISTINCT s::BYTES),
    array_agg(DISTINCT i),
    array_cat_agg(DISTINCT arr),
    json_agg(DISTINCT j),
    regr_count(DISTINCT i, f)
FROM a
----
scalar-group-by
 ├── columns: count:9!null sum:11 sum_int:12 avg:13 stddev:14 variance:15 xor_agg:17 array_agg:18 array_cat_agg:19 json_agg:20 regr_count:21!null
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── key: ()
 ├── fd: ()-->(9,11-15,17-21)
 ├── project
 │    ├── columns: column10:10 column16:16 i:2 f:3 j:5 arr:6
 │    ├── immutable
 │    ├── fd: (2)-->(10)
 │    ├── scan a
 │    │    └── columns: i:2 f:3 s:4 j:5 arr:6
 │    └── projections
 │         ├── i:2 > 5 [as=column10:10, outer=(2)]
 │         └── s:4::BYTES [as=column16:16, outer=(4), immutable]
 └── aggregations
      ├── agg-distinct [as=count:9, outer=(2)]
      │    └── count
      │         └── i:2
      ├── agg-filter [as=sum:11, outer=(2,10)]
      │    ├── agg-distinct
      │    │    └── sum
      │    │         └── i:2
      │    └── column10:10
      ├── agg-distinct [as=sum_int:12, outer=(2)]
      │    └── sum-int
      │         └── i:2
      ├── agg-distinct [as=avg:13, outer=(2)]
      │    └── avg
      │         └── i:2
      ├── agg-distinct [as=stddev:14, outer=(3)]
      │    └── std-dev
      │         └── f:3
      ├── agg-distinct [as=variance:15, outer=(3)]
      │    └── variance
      │         └── f:3
      ├── agg-distinct [as=xor_agg:17, outer=(16)]
      │    └── xor-agg
      │         └── column16:16
      ├── agg-distinct [as=array_agg:18, outer=(2)]
      │    └── array-agg
      │         └── i:2
      ├── agg-distinct [as=array_cat_agg:19, outer=(6)]
      │    └── array-cat-agg
      │         └── arr:6
      ├── agg-distinct [as=json_agg:20, outer=(5)]
      │    └── json-agg
      │         └── j:5
      └── agg-distinct [as=regr_count:21, outer=(2,3)]
           └── regression-count
                ├── i:2
                └── f:3
