# tests adapted from logictest -- aggregate

exec-ddl
CREATE TABLE kv (
  k INT PRIMARY KEY,
  v INT,
  w INT,
  s STRING
)
----

exec-ddl
CREATE TABLE abxy (
  a INT,
  b INT,
  x INT,
  y INT,
  PRIMARY KEY(a,b)
)
----

build
SELECT min(1), max(1), count(1), sum_int(1), avg(1), sum(1), stddev(1),
  variance(1), bool_and(true), bool_or(false), xor_agg(b'\x01') FROM kv
----
scalar-group-by
 ├── columns: min:8 max:9 count:10!null sum_int:11 avg:12 sum:13 stddev:14 variance:15 bool_and:17 bool_or:19 xor_agg:21
 ├── project
 │    ├── columns: column7:7!null column16:16!null column18:18!null column20:20!null
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         ├── 1 [as=column7:7]
 │         ├── true [as=column16:16]
 │         ├── false [as=column18:18]
 │         └── '\x01' [as=column20:20]
 └── aggregations
      ├── min [as=min:8]
      │    └── column7:7
      ├── max [as=max:9]
      │    └── column7:7
      ├── count [as=count:10]
      │    └── column7:7
      ├── sum-int [as=sum_int:11]
      │    └── column7:7
      ├── avg [as=avg:12]
      │    └── column7:7
      ├── sum [as=sum:13]
      │    └── column7:7
      ├── std-dev [as=stddev:14]
      │    └── column7:7
      ├── variance [as=variance:15]
      │    └── column7:7
      ├── bool-and [as=bool_and:17]
      │    └── column16:16
      ├── bool-or [as=bool_or:19]
      │    └── column18:18
      └── xor-agg [as=xor_agg:21]
           └── column20:20

build
SELECT min(v), max(v), count(v), sum_int(1), avg(v), sum(v), stddev(v),
  variance(v), bool_and(v = 1), bool_and(v = 1), xor_agg(s::bytes) FROM kv
----
scalar-group-by
 ├── columns: min:7 max:8 count:9!null sum_int:11 avg:12 sum:13 stddev:14 variance:15 bool_and:17 bool_and:17 xor_agg:19
 ├── project
 │    ├── columns: column10:10!null column16:16 column18:18 v:2
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         ├── 1 [as=column10:10]
 │         ├── v:2 = 1 [as=column16:16]
 │         └── s:4::BYTES [as=column18:18]
 └── aggregations
      ├── min [as=min:7]
      │    └── v:2
      ├── max [as=max:8]
      │    └── v:2
      ├── count [as=count:9]
      │    └── v:2
      ├── sum-int [as=sum_int:11]
      │    └── column10:10
      ├── avg [as=avg:12]
      │    └── v:2
      ├── sum [as=sum:13]
      │    └── v:2
      ├── std-dev [as=stddev:14]
      │    └── v:2
      ├── variance [as=variance:15]
      │    └── v:2
      ├── bool-and [as=bool_and:17]
      │    └── column16:16
      └── xor-agg [as=xor_agg:19]
           └── column18:18

build
SELECT min(1, 2)
----
error (42883): unknown signature: min(int, int)

build
SELECT min(1), count(1), max(1), sum_int(1), avg(1)::float, sum(1), stddev(1),
  variance(1)::float, bool_and(true), bool_or(true), to_hex(xor_agg(b'\x01'))
----
project
 ├── columns: min:2 count:3!null max:4 sum_int:5 avg:15 sum:7 stddev:8 variance:16 bool_and:11 bool_or:12 to_hex:17
 ├── scalar-group-by
 │    ├── columns: min:2 count:3!null max:4 sum_int:5 avg:6 sum:7 stddev:8 variance:9 bool_and:11 bool_or:12 xor_agg:14
 │    ├── project
 │    │    ├── columns: column1:1!null column10:10!null column13:13!null
 │    │    ├── values
 │    │    │    └── ()
 │    │    └── projections
 │    │         ├── 1 [as=column1:1]
 │    │         ├── true [as=column10:10]
 │    │         └── '\x01' [as=column13:13]
 │    └── aggregations
 │         ├── min [as=min:2]
 │         │    └── column1:1
 │         ├── count [as=count:3]
 │         │    └── column1:1
 │         ├── max [as=max:4]
 │         │    └── column1:1
 │         ├── sum-int [as=sum_int:5]
 │         │    └── column1:1
 │         ├── avg [as=avg:6]
 │         │    └── column1:1
 │         ├── sum [as=sum:7]
 │         │    └── column1:1
 │         ├── std-dev [as=stddev:8]
 │         │    └── column1:1
 │         ├── variance [as=variance:9]
 │         │    └── column1:1
 │         ├── bool-and [as=bool_and:11]
 │         │    └── column10:10
 │         ├── bool-or [as=bool_or:12]
 │         │    └── column10:10
 │         └── xor-agg [as=xor_agg:14]
 │              └── column13:13
 └── projections
      ├── avg:6::FLOAT8 [as=avg:15]
      ├── variance:9::FLOAT8 [as=variance:16]
      └── to_hex(xor_agg:14) [as=to_hex:17]

build
SELECT array_agg(1) FROM kv
----
scalar-group-by
 ├── columns: array_agg:8
 ├── project
 │    ├── columns: column7:7!null
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── 1 [as=column7:7]
 └── aggregations
      └── array-agg [as=array_agg:8]
           └── column7:7

build
SELECT json_agg(v) FROM kv
----
scalar-group-by
 ├── columns: json_agg:7
 ├── project
 │    ├── columns: v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── json-agg [as=json_agg:7]
           └── v:2

build
SELECT jsonb_agg(1)
----
scalar-group-by
 ├── columns: jsonb_agg:2
 ├── project
 │    ├── columns: column1:1!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         └── 1 [as=column1:1]
 └── aggregations
      └── jsonb-agg [as=jsonb_agg:2]
           └── column1:1

# Even with no aggregate functions, grouping occurs in the presence of GROUP BY.
build
SELECT 1 r FROM kv GROUP BY v
----
project
 ├── columns: r:7!null
 ├── group-by (hash)
 │    ├── columns: v:2
 │    ├── grouping columns: v:2
 │    └── project
 │         ├── columns: v:2
 │         └── scan kv
 │              └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── projections
      └── 1 [as=r:7]

# This should ideally return {NULL}, but this is a pathological case, and
# Postgres has the same behavior, so it's sufficient for now.
build
SELECT array_agg(NULL)
----
error (42725): ambiguous call: array_agg(unknown), candidates are:
array_agg(bool) -> bool[]
array_agg(bool[]) -> bool[][]
array_agg(box2d) -> box2d[]
array_agg(box2d[]) -> box2d[][]
array_agg(int) -> int[]
array_agg(int[]) -> int[][]
array_agg(float) -> float[]
array_agg(float[]) -> float[][]
array_agg(decimal) -> decimal[]
array_agg(decimal[]) -> decimal[][]
array_agg(date) -> date[]
array_agg(date[]) -> date[][]
array_agg(timestamp) -> timestamp[]
array_agg(timestamp[]) -> timestamp[][]
array_agg(interval) -> interval[]
array_agg(interval[]) -> interval[][]
array_agg(geography) -> geography[]
array_agg(geography[]) -> geography[][]
array_agg(geometry) -> geometry[]
array_agg(geometry[]) -> geometry[][]
array_agg(string) -> string[]
array_agg(string[]) -> string[][]
array_agg(bytes) -> bytes[]
array_agg(bytes[]) -> bytes[][]
array_agg(timestamptz) -> timestamptz[]
array_agg(timestamptz[]) -> timestamptz[][]
array_agg(oid) -> oid[]
array_agg(oid[]) -> oid[][]
array_agg(uuid) -> uuid[]
array_agg(uuid[]) -> uuid[][]
array_agg(inet) -> inet[]
array_agg(inet[]) -> inet[][]
array_agg(pg_lsn) -> pg_lsn[]
array_agg(pg_lsn[]) -> pg_lsn[][]
array_agg(refcursor) -> refcursor[]
array_agg(refcursor[]) -> refcursor[][]
array_agg(time) -> time[]
array_agg(time[]) -> time[][]
array_agg(timetz) -> timetz[]
array_agg(timetz[]) -> timetz[][]
array_agg(jsonb) -> jsonb[]
array_agg(jsonb[]) -> jsonb[][]
array_agg(varbit) -> varbit[]
array_agg(varbit[]) -> varbit[][]
array_agg(anyenum) -> anyenum[]
array_agg(anyenum[]) -> anyenum[][]
array_agg(tuple) -> tuple[]
array_agg(tuple[]) -> tuple[][]

# With an explicit cast, this works as expected.
build
SELECT array_agg(NULL::TEXT)
----
scalar-group-by
 ├── columns: array_agg:2
 ├── project
 │    ├── columns: column1:1
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         └── NULL::STRING [as=column1:1]
 └── aggregations
      └── array-agg [as=array_agg:2]
           └── column1:1

build
SELECT (SELECT COALESCE(max(1), 0) FROM kv)
----
project
 ├── columns: coalesce:10
 ├── values
 │    └── ()
 └── projections
      └── subquery [as=coalesce:10]
           └── max1-row
                ├── columns: coalesce:9
                └── project
                     ├── columns: coalesce:9
                     ├── scalar-group-by
                     │    ├── columns: max:8
                     │    ├── project
                     │    │    ├── columns: column7:7!null
                     │    │    ├── scan kv
                     │    │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
                     │    │    └── projections
                     │    │         └── 1 [as=column7:7]
                     │    └── aggregations
                     │         └── max [as=max:8]
                     │              └── column7:7
                     └── projections
                          └── COALESCE(max:8, 0) [as=coalesce:9]

build
SELECT count(*), k FROM kv
----
error (42803): column "k" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT count(*) FROM kv GROUP BY s < 5
----
error (22023): unsupported comparison operator: <string> < <int>

build
SELECT count(*), k FROM kv GROUP BY k
----
group-by (hash)
 ├── columns: count:7!null k:1!null
 ├── grouping columns: k:1!null
 ├── project
 │    ├── columns: k:1!null
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

# GROUP BY specified using column index works.
build
SELECT count(*), k FROM kv GROUP BY 2
----
group-by (hash)
 ├── columns: count:7!null k:1!null
 ├── grouping columns: k:1!null
 ├── project
 │    ├── columns: k:1!null
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

build
SELECT * FROM kv GROUP BY v, count(w)
----
error (42803): count(): aggregate functions are not allowed in GROUP BY

build
SELECT count(w) FROM kv GROUP BY 1
----
error (42803): count(): aggregate functions are not allowed in GROUP BY

build
SELECT sum(v) FROM kv GROUP BY k LIMIT sum(v)
----
error (42703): column "v" does not exist

build
SELECT sum(v) FROM kv GROUP BY k LIMIT 1 OFFSET sum(v)
----
error (42703): column "v" does not exist

build
VALUES (99, count(1))
----
error (42803): count(): aggregate functions are not allowed in VALUES

build
SELECT count(*), k FROM kv GROUP BY 5
----
error (42P10): GROUP BY position 5 is not in select list

build
SELECT count(*), k FROM kv GROUP BY 0
----
error (42P10): GROUP BY position 0 is not in select list

build
SELECT 1 GROUP BY 'a'
----
error (42601): non-integer constant in GROUP BY: 'a'

# Qualifying a name in the SELECT, the GROUP BY, both or neither should not affect validation.
build
SELECT count(*), kv.s FROM kv GROUP BY s
----
group-by (hash)
 ├── columns: count:7!null s:4
 ├── grouping columns: s:4
 ├── project
 │    ├── columns: s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

build
SELECT count(*), s FROM kv GROUP BY kv.s
----
group-by (hash)
 ├── columns: count:7!null s:4
 ├── grouping columns: s:4
 ├── project
 │    ├── columns: s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

build
SELECT count(*), kv.s FROM kv GROUP BY kv.s
----
group-by (hash)
 ├── columns: count:7!null s:4
 ├── grouping columns: s:4
 ├── project
 │    ├── columns: s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

build
SELECT count(*), s FROM kv GROUP BY s
----
group-by (hash)
 ├── columns: count:7!null s:4
 ├── grouping columns: s:4
 ├── project
 │    ├── columns: s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

# Grouping by more than one column works.
build
SELECT v, count(*), w FROM kv GROUP BY v, w
----
group-by (hash)
 ├── columns: v:2 count:7!null w:3
 ├── grouping columns: v:2 w:3
 ├── project
 │    ├── columns: v:2 w:3
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

# Grouping by more than one column using column numbers works.
build
SELECT v, count(*), w FROM kv GROUP BY 1, 3
----
group-by (hash)
 ├── columns: v:2 count:7!null w:3
 ├── grouping columns: v:2 w:3
 ├── project
 │    ├── columns: v:2 w:3
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count-rows [as=count_rows:7]

# Selecting and grouping on a function expression works.
build
SELECT count(*), upper(s) FROM kv GROUP BY upper(s)
----
group-by (hash)
 ├── columns: count:7!null upper:8
 ├── grouping columns: column8:8
 ├── project
 │    ├── columns: column8:8
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── upper(s:4) [as=column8:8]
 └── aggregations
      └── count-rows [as=count_rows:7]

# Selecting and grouping on a constant works.
build
SELECT count(*) FROM kv GROUP BY 1+2
----
project
 ├── columns: count:7!null
 └── group-by (hash)
      ├── columns: count_rows:7!null column8:8!null
      ├── grouping columns: column8:8!null
      ├── project
      │    ├── columns: column8:8!null
      │    ├── scan kv
      │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         └── 1 + 2 [as=column8:8]
      └── aggregations
           └── count-rows [as=count_rows:7]

build
SELECT count(*) FROM kv GROUP BY length('abc')
----
project
 ├── columns: count:7!null
 └── group-by (hash)
      ├── columns: count_rows:7!null column8:8
      ├── grouping columns: column8:8
      ├── project
      │    ├── columns: column8:8
      │    ├── scan kv
      │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         └── length('abc') [as=column8:8]
      └── aggregations
           └── count-rows [as=count_rows:7]

# Selecting a function of something which is grouped works.
build
SELECT count(*), upper(s) FROM kv GROUP BY s
----
project
 ├── columns: count:7!null upper:8
 ├── group-by (hash)
 │    ├── columns: s:4 count_rows:7!null
 │    ├── grouping columns: s:4
 │    ├── project
 │    │    ├── columns: s:4
 │    │    └── scan kv
 │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         └── count-rows [as=count_rows:7]
 └── projections
      └── upper(s:4) [as=upper:8]

# Selecting a value that is not grouped, even if a function of it it, does not work.
build
SELECT count(*), s FROM kv GROUP BY upper(s)
----
error (42803): column "s" must appear in the GROUP BY clause or be used in an aggregate function

# Selecting and grouping on a more complex expression works.
build
SELECT count(*), k+v AS r FROM kv GROUP BY k+v
----
group-by (hash)
 ├── columns: count:7!null r:8
 ├── grouping columns: column8:8
 ├── project
 │    ├── columns: column8:8
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── k:1 + v:2 [as=column8:8]
 └── aggregations
      └── count-rows [as=count_rows:7]


# Selecting a more complex expression, made up of things which are each grouped, works.
build
SELECT count(*), k+v AS r FROM kv GROUP BY k, v
----
project
 ├── columns: count:7!null r:8
 ├── group-by (hash)
 │    ├── columns: k:1!null v:2 count_rows:7!null
 │    ├── grouping columns: k:1!null v:2
 │    ├── project
 │    │    ├── columns: k:1!null v:2
 │    │    └── scan kv
 │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         └── count-rows [as=count_rows:7]
 └── projections
      └── k:1 + v:2 [as=r:8]

build
SELECT count(*), k+v FROM kv GROUP BY v
----
error (42803): column "k" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT count(*), v/(k+v) FROM kv GROUP BY k+v
----
error (42803): column "v" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT k FROM kv WHERE avg(k) > 1
----
error (42803): aggregate functions are not allowed in WHERE

build
SELECT max(avg(k)) FROM kv
----
error (42803): max(): avg(): aggregate function calls cannot be nested

# Test case from #2761.
build
SELECT count(kv.k) AS count_1, kv.v + kv.w AS lx FROM kv GROUP BY kv.v + kv.w
----
group-by (hash)
 ├── columns: count_1:7!null lx:8
 ├── grouping columns: column8:8
 ├── project
 │    ├── columns: column8:8 k:1!null
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── v:2 + w:3 [as=column8:8]
 └── aggregations
      └── count [as=count:7]
           └── k:1

build
SELECT count(*)
----
scalar-group-by
 ├── columns: count:1!null
 ├── values
 │    └── ()
 └── aggregations
      └── count-rows [as=count_rows:1]

build
SELECT count(k) from kv
----
scalar-group-by
 ├── columns: count:7!null
 ├── project
 │    ├── columns: k:1!null
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count [as=count:7]
           └── k:1

build
SELECT count(1)
----
scalar-group-by
 ├── columns: count:2!null
 ├── project
 │    ├── columns: column1:1!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         └── 1 [as=column1:1]
 └── aggregations
      └── count [as=count:2]
           └── column1:1

build
SELECT count(1) from kv
----
scalar-group-by
 ├── columns: count:8!null
 ├── project
 │    ├── columns: column7:7!null
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── 1 [as=column7:7]
 └── aggregations
      └── count [as=count:8]
           └── column7:7

build
SELECT count(k, v) FROM kv
----
error (42883): unknown signature: count(int, int)

build
SELECT v, count(k) FROM kv GROUP BY v ORDER BY v
----
sort
 ├── columns: v:2 count:7!null
 ├── ordering: +2
 └── group-by (hash)
      ├── columns: v:2 count:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: k:1!null v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count [as=count:7]
                └── k:1

build
SELECT v, count(k) FROM kv GROUP BY v ORDER BY v DESC
----
sort
 ├── columns: v:2 count:7!null
 ├── ordering: -2
 └── group-by (hash)
      ├── columns: v:2 count:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: k:1!null v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count [as=count:7]
                └── k:1

build
SELECT v, count(k) FROM kv GROUP BY v ORDER BY count(k) DESC
----
sort
 ├── columns: v:2 count:7!null
 ├── ordering: -7
 └── group-by (hash)
      ├── columns: v:2 count:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: k:1!null v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count [as=count:7]
                └── k:1

build
SELECT v, count(k) FROM kv GROUP BY v ORDER BY v-count(k)
----
sort
 ├── columns: v:2 count:7!null  [hidden: column8:8]
 ├── ordering: +8
 └── project
      ├── columns: column8:8 v:2 count:7!null
      ├── group-by (hash)
      │    ├── columns: v:2 count:7!null
      │    ├── grouping columns: v:2
      │    ├── project
      │    │    ├── columns: k:1!null v:2
      │    │    └── scan kv
      │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── aggregations
      │         └── count [as=count:7]
      │              └── k:1
      └── projections
           └── v:2 - count:7 [as=column8:8]

build
SELECT v FROM kv GROUP BY v ORDER BY sum(k)
----
sort
 ├── columns: v:2  [hidden: sum:7!null]
 ├── ordering: +7
 └── group-by (hash)
      ├── columns: v:2 sum:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: k:1!null v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── sum [as=sum:7]
                └── k:1

build
SELECT v, count(k) FROM kv GROUP BY v ORDER BY 1 DESC
----
sort
 ├── columns: v:2 count:7!null
 ├── ordering: -2
 └── group-by (hash)
      ├── columns: v:2 count:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: k:1!null v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count [as=count:7]
                └── k:1

build
SELECT count(*), count(k), count(kv.v) FROM kv
----
scalar-group-by
 ├── columns: count:7!null count:8!null count:9!null
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── count-rows [as=count_rows:7]
      ├── count [as=count:8]
      │    └── k:1
      └── count [as=count:9]
           └── v:2

build
SELECT count(kv.*) FROM kv
----
scalar-group-by
 ├── columns: count:8!null
 ├── project
 │    ├── columns: column7:7
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── ((k:1, v:2, w:3, s:4) AS k, v, w, s) [as=column7:7]
 └── aggregations
      └── count [as=count:8]
           └── column7:7

build
SELECT count(DISTINCT k), count(DISTINCT v), count(DISTINCT (v)) FROM kv
----
scalar-group-by
 ├── columns: count:7!null count:8!null count:8!null
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── agg-distinct [as=count:7]
      │    └── count
      │         └── k:1
      └── agg-distinct [as=count:8]
           └── count
                └── v:2

build
SELECT upper(s), count(DISTINCT k), count(DISTINCT v), count(DISTINCT (v)) FROM kv GROUP BY upper(s)
----
group-by (hash)
 ├── columns: upper:9 count:7!null count:8!null count:8!null
 ├── grouping columns: column9:9
 ├── project
 │    ├── columns: column9:9 k:1!null v:2
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── upper(s:4) [as=column9:9]
 └── aggregations
      ├── agg-distinct [as=count:7]
      │    └── count
      │         └── k:1
      └── agg-distinct [as=count:8]
           └── count
                └── v:2

build
SELECT count((k, v)) FROM kv
----
scalar-group-by
 ├── columns: count:8!null
 ├── project
 │    ├── columns: column7:7
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── (k:1, v:2) [as=column7:7]
 └── aggregations
      └── count [as=count:8]
           └── column7:7

build
SELECT count(DISTINCT (k, v)) FROM kv
----
scalar-group-by
 ├── columns: count:8!null
 ├── project
 │    ├── columns: column7:7
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── (k:1, v:2) [as=column7:7]
 └── aggregations
      └── agg-distinct [as=count:8]
           └── count
                └── column7:7

build
SELECT count(DISTINCT (k, (v))) FROM kv
----
scalar-group-by
 ├── columns: count:8!null
 ├── project
 │    ├── columns: column7:7
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── (k:1, v:2) [as=column7:7]
 └── aggregations
      └── agg-distinct [as=count:8]
           └── count
                └── column7:7

build
SELECT count(*) FROM kv a, kv b
----
scalar-group-by
 ├── columns: count:13!null
 ├── project
 │    └── inner-join (cross)
 │         ├── columns: a.k:1!null a.v:2 a.w:3 a.s:4 a.crdb_internal_mvcc_timestamp:5 a.tableoid:6 b.k:7!null b.v:8 b.w:9 b.s:10 b.crdb_internal_mvcc_timestamp:11 b.tableoid:12
 │         ├── scan kv [as=a]
 │         │    └── columns: a.k:1!null a.v:2 a.w:3 a.s:4 a.crdb_internal_mvcc_timestamp:5 a.tableoid:6
 │         ├── scan kv [as=b]
 │         │    └── columns: b.k:7!null b.v:8 b.w:9 b.s:10 b.crdb_internal_mvcc_timestamp:11 b.tableoid:12
 │         └── filters (true)
 └── aggregations
      └── count-rows [as=count_rows:13]

build
SELECT count((k, v)) FROM kv LIMIT 1
----
limit
 ├── columns: count:8!null
 ├── scalar-group-by
 │    ├── columns: count:8!null
 │    ├── limit hint: 1.00
 │    ├── project
 │    │    ├── columns: column7:7
 │    │    ├── scan kv
 │    │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── (k:1, v:2) [as=column7:7]
 │    └── aggregations
 │         └── count [as=count:8]
 │              └── column7:7
 └── 1

build
SELECT count((k, v)) FROM kv OFFSET 1
----
offset
 ├── columns: count:8!null
 ├── scalar-group-by
 │    ├── columns: count:8!null
 │    ├── project
 │    │    ├── columns: column7:7
 │    │    ├── scan kv
 │    │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── (k:1, v:2) [as=column7:7]
 │    └── aggregations
 │         └── count [as=count:8]
 │              └── column7:7
 └── 1

build
SELECT count(k)+count(kv.v) AS r FROM kv
----
project
 ├── columns: r:9!null
 ├── scalar-group-by
 │    ├── columns: count:7!null count:8!null
 │    ├── project
 │    │    ├── columns: k:1!null v:2
 │    │    └── scan kv
 │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         ├── count [as=count:7]
 │         │    └── k:1
 │         └── count [as=count:8]
 │              └── v:2
 └── projections
      └── count:7 + count:8 [as=r:9]

build
SELECT count(NULL::int), count((NULL, NULL))
----
scalar-group-by
 ├── columns: count:2!null count:4!null
 ├── project
 │    ├── columns: column1:1 column3:3
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         ├── NULL::INT8 [as=column1:1]
 │         └── (NULL, NULL) [as=column3:3]
 └── aggregations
      ├── count [as=count:2]
      │    └── column1:1
      └── count [as=count:4]
           └── column3:3

build
SELECT min(k), max(k), min(v), max(v) FROM kv
----
scalar-group-by
 ├── columns: min:7 max:8 min:9 max:10
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── min [as=min:7]
      │    └── k:1
      ├── max [as=max:8]
      │    └── k:1
      ├── min [as=min:9]
      │    └── v:2
      └── max [as=max:10]
           └── v:2

build
SELECT min(k), max(k), min(v), max(v) FROM kv WHERE k > 8
----
scalar-group-by
 ├── columns: min:7 max:8 min:9 max:10
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── select
 │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── filters
 │              └── k:1 > 8
 └── aggregations
      ├── min [as=min:7]
      │    └── k:1
      ├── max [as=max:8]
      │    └── k:1
      ├── min [as=min:9]
      │    └── v:2
      └── max [as=max:10]
           └── v:2

build
SELECT array_agg(k), array_agg(s) FROM (SELECT k, s FROM kv ORDER BY k)
----
scalar-group-by
 ├── columns: array_agg:7 array_agg:8
 ├── internal-ordering: +1
 ├── project
 │    ├── columns: k:1!null s:4
 │    ├── ordering: +1
 │    └── scan kv
 │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── ordering: +1
 └── aggregations
      ├── array-agg [as=array_agg:7]
      │    └── k:1
      └── array-agg [as=array_agg:8]
           └── s:4

build
SELECT array_agg(k) FROM (SELECT k FROM kv ORDER BY s)
----
scalar-group-by
 ├── columns: array_agg:7
 ├── internal-ordering: +4
 ├── sort
 │    ├── columns: k:1!null s:4
 │    ├── ordering: +4
 │    └── project
 │         ├── columns: k:1!null s:4
 │         └── scan kv
 │              └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── array-agg [as=array_agg:7]
           └── k:1

build
SELECT max(k) FROM (SELECT k FROM kv ORDER BY s)
----
scalar-group-by
 ├── columns: max:7
 ├── project
 │    ├── columns: k:1!null
 │    └── project
 │         ├── columns: k:1!null s:4
 │         └── scan kv
 │              └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── max [as=max:7]
           └── k:1


build
SELECT array_agg(k) || 1 FROM (SELECT k FROM kv ORDER BY s)
----
project
 ├── columns: "?column?":8
 ├── scalar-group-by
 │    ├── columns: array_agg:7
 │    ├── internal-ordering: +4
 │    ├── sort
 │    │    ├── columns: k:1!null s:4
 │    │    ├── ordering: +4
 │    │    └── project
 │    │         ├── columns: k:1!null s:4
 │    │         └── scan kv
 │    │              └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         └── array-agg [as=array_agg:7]
 │              └── k:1
 └── projections
      └── array_agg:7 || 1 [as="?column?":8]

build
SELECT array_agg(s) FROM kv WHERE s IS NULL
----
scalar-group-by
 ├── columns: array_agg:7
 ├── project
 │    ├── columns: s:4
 │    └── select
 │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── filters
 │              └── s:4 IS NULL
 └── aggregations
      └── array-agg [as=array_agg:7]
           └── s:4

build
SELECT avg(k), avg(v), sum(k), sum(v) FROM kv
----
scalar-group-by
 ├── columns: avg:7 avg:8 sum:9 sum:10
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── avg [as=avg:7]
      │    └── k:1
      ├── avg [as=avg:8]
      │    └── v:2
      ├── sum [as=sum:9]
      │    └── k:1
      └── sum [as=sum:10]
           └── v:2

build
SELECT avg(k::decimal), avg(v::decimal), sum(k::decimal), sum(v::decimal) FROM kv
----
scalar-group-by
 ├── columns: avg:8 avg:10 sum:11 sum:12
 ├── project
 │    ├── columns: column7:7!null column9:9
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         ├── k:1::DECIMAL [as=column7:7]
 │         └── v:2::DECIMAL [as=column9:9]
 └── aggregations
      ├── avg [as=avg:8]
      │    └── column7:7
      ├── avg [as=avg:10]
      │    └── column9:9
      ├── sum [as=sum:11]
      │    └── column7:7
      └── sum [as=sum:12]
           └── column9:9

build
SELECT avg(DISTINCT k), avg(DISTINCT v), sum(DISTINCT k), sum(DISTINCT v) FROM kv
----
scalar-group-by
 ├── columns: avg:7 avg:8 sum:9 sum:10
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── agg-distinct [as=avg:7]
      │    └── avg
      │         └── k:1
      ├── agg-distinct [as=avg:8]
      │    └── avg
      │         └── v:2
      ├── agg-distinct [as=sum:9]
      │    └── sum
      │         └── k:1
      └── agg-distinct [as=sum:10]
           └── sum
                └── v:2

build
SELECT avg(k) * 2.0 + max(v)::DECIMAL AS r FROM kv
----
project
 ├── columns: r:9
 ├── scalar-group-by
 │    ├── columns: avg:7 max:8
 │    ├── project
 │    │    ├── columns: k:1!null v:2
 │    │    └── scan kv
 │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         ├── avg [as=avg:7]
 │         │    └── k:1
 │         └── max [as=max:8]
 │              └── v:2
 └── projections
      └── (avg:7 * 2.0) + max:8::DECIMAL [as=r:9]

build
SELECT avg(k) * 2.0 + max(v)::DECIMAL AS r FROM kv WHERE w*2 = k
----
project
 ├── columns: r:9
 ├── scalar-group-by
 │    ├── columns: avg:7 max:8
 │    ├── project
 │    │    ├── columns: k:1!null v:2
 │    │    └── select
 │    │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │         ├── scan kv
 │    │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │         └── filters
 │    │              └── (w:3 * 2) = k:1
 │    └── aggregations
 │         ├── avg [as=avg:7]
 │         │    └── k:1
 │         └── max [as=max:8]
 │              └── v:2
 └── projections
      └── (avg:7 * 2.0) + max:8::DECIMAL [as=r:9]

exec-ddl
CREATE TABLE abc (
  a CHAR PRIMARY KEY,
  b FLOAT,
  c BOOLEAN,
  d DECIMAL
)
----

build
SELECT min(a), min(b), min(c), min(d) FROM abc
----
scalar-group-by
 ├── columns: min:7 min:8 min:9 min:10
 ├── project
 │    ├── columns: a:1!null b:2 c:3 d:4
 │    └── scan abc
 │         └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── min [as=min:7]
      │    └── a:1
      ├── min [as=min:8]
      │    └── b:2
      ├── min [as=min:9]
      │    └── c:3
      └── min [as=min:10]
           └── d:4

build
SELECT max(a), max(b), max(c), max(d) FROM abc
----
scalar-group-by
 ├── columns: max:7 max:8 max:9 max:10
 ├── project
 │    ├── columns: a:1!null b:2 c:3 d:4
 │    └── scan abc
 │         └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── a:1
      ├── max [as=max:8]
      │    └── b:2
      ├── max [as=max:9]
      │    └── c:3
      └── max [as=max:10]
           └── d:4

build
SELECT avg(b), sum(b), avg(d), sum(d) FROM abc
----
scalar-group-by
 ├── columns: avg:7 sum:8 avg:9 sum:10
 ├── project
 │    ├── columns: b:2 d:4
 │    └── scan abc
 │         └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── avg [as=avg:7]
      │    └── b:2
      ├── sum [as=sum:8]
      │    └── b:2
      ├── avg [as=avg:9]
      │    └── d:4
      └── sum [as=sum:10]
           └── d:4

# Verify summing of intervals
exec-ddl
CREATE TABLE intervals (
  a INTERVAL PRIMARY KEY
)
----

build
SELECT sum(a) FROM intervals
----
scalar-group-by
 ├── columns: sum:4
 ├── project
 │    ├── columns: a:1!null
 │    └── scan intervals
 │         └── columns: a:1!null crdb_internal_mvcc_timestamp:2 tableoid:3
 └── aggregations
      └── sum [as=sum:4]
           └── a:1

build
SELECT avg(a) FROM abc
----
error (42883): unknown signature: avg(char)

build
SELECT avg(c) FROM abc
----
error (42883): unknown signature: avg(bool)

build
SELECT avg((a,c)) FROM abc
----
error (42883): unknown signature: avg(tuple{char, bool})

build
SELECT sum(a) FROM abc
----
error (42883): unknown signature: sum(char)

build
SELECT sum(c) FROM abc
----
error (42883): unknown signature: sum(bool)

build
SELECT sum((a,c)) FROM abc
----
error (42883): unknown signature: sum(tuple{char, bool})

exec-ddl
CREATE TABLE xyz (
  x INT PRIMARY KEY,
  y INT,
  z FLOAT,
  INDEX xy (x, y),
  INDEX zyx (z, y, x),
  FAMILY (x),
  FAMILY (y),
  FAMILY (z)
)
----

build
SELECT min(x) FROM xyz
----
scalar-group-by
 ├── columns: min:6
 ├── project
 │    ├── columns: x:1!null
 │    └── scan xyz
 │         └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 └── aggregations
      └── min [as=min:6]
           └── x:1

build
SELECT min(x) FROM xyz WHERE x in (0, 4, 7)
----
scalar-group-by
 ├── columns: min:6
 ├── project
 │    ├── columns: x:1!null
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── x:1 IN (0, 4, 7)
 └── aggregations
      └── min [as=min:6]
           └── x:1

build
SELECT max(x) FROM xyz
----
scalar-group-by
 ├── columns: max:6
 ├── project
 │    ├── columns: x:1!null
 │    └── scan xyz
 │         └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 └── aggregations
      └── max [as=max:6]
           └── x:1

build
SELECT max(y) FROM xyz WHERE x = 1
----
scalar-group-by
 ├── columns: max:6
 ├── project
 │    ├── columns: y:2
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── x:1 = 1
 └── aggregations
      └── max [as=max:6]
           └── y:2

build
SELECT min(y) FROM xyz WHERE x = 7
----
scalar-group-by
 ├── columns: min:6
 ├── project
 │    ├── columns: y:2
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── x:1 = 7
 └── aggregations
      └── min [as=min:6]
           └── y:2

build
SELECT min(x) FROM xyz WHERE (y, z) = (2, 3.0)
----
scalar-group-by
 ├── columns: min:6
 ├── project
 │    ├── columns: x:1!null
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── (y:2, z:3) = (2, 3.0)
 └── aggregations
      └── min [as=min:6]
           └── x:1

build
SELECT max(x) FROM xyz WHERE (z, y) = (3.0, 2)
----
scalar-group-by
 ├── columns: max:6
 ├── project
 │    ├── columns: x:1!null
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── (z:3, y:2) = (3.0, 2)
 └── aggregations
      └── max [as=max:6]
           └── x:1


# VARIANCE/STDDEV

build
SELECT variance(x), variance(y::decimal), round(variance(z), 14) FROM xyz
----
project
 ├── columns: variance:6 variance:8 round:10
 ├── scalar-group-by
 │    ├── columns: variance:6 variance:8 variance:9
 │    ├── project
 │    │    ├── columns: column7:7 x:1!null z:3
 │    │    ├── scan xyz
 │    │    │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    │    └── projections
 │    │         └── y:2::DECIMAL [as=column7:7]
 │    └── aggregations
 │         ├── variance [as=variance:6]
 │         │    └── x:1
 │         ├── variance [as=variance:8]
 │         │    └── column7:7
 │         └── variance [as=variance:9]
 │              └── z:3
 └── projections
      └── round(variance:9, 14) [as=round:10]

build
SELECT variance(x) FROM xyz WHERE x = 10
----
scalar-group-by
 ├── columns: variance:6
 ├── project
 │    ├── columns: x:1!null
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── x:1 = 10
 └── aggregations
      └── variance [as=variance:6]
           └── x:1

build
SELECT stddev(x), stddev(y::decimal), round(stddev(z), 14) FROM xyz
----
project
 ├── columns: stddev:6 stddev:8 round:10
 ├── scalar-group-by
 │    ├── columns: stddev:6 stddev:8 stddev:9
 │    ├── project
 │    │    ├── columns: column7:7 x:1!null z:3
 │    │    ├── scan xyz
 │    │    │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    │    └── projections
 │    │         └── y:2::DECIMAL [as=column7:7]
 │    └── aggregations
 │         ├── std-dev [as=stddev:6]
 │         │    └── x:1
 │         ├── std-dev [as=stddev:8]
 │         │    └── column7:7
 │         └── std-dev [as=stddev:9]
 │              └── z:3
 └── projections
      └── round(stddev:9, 14) [as=round:10]

build
SELECT stddev(x) FROM xyz WHERE x = 1
----
scalar-group-by
 ├── columns: stddev:6
 ├── project
 │    ├── columns: x:1!null
 │    └── select
 │         ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── scan xyz
 │         │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         └── filters
 │              └── x:1 = 1
 └── aggregations
      └── std-dev [as=stddev:6]
           └── x:1

build
SELECT avg(1::int)::float, avg(2::float)::float, avg(3::decimal)::float
----
project
 ├── columns: avg:7 avg:4 avg:8
 ├── scalar-group-by
 │    ├── columns: avg:2 avg:4 avg:6
 │    ├── project
 │    │    ├── columns: column1:1!null column3:3!null column5:5!null
 │    │    ├── values
 │    │    │    └── ()
 │    │    └── projections
 │    │         ├── 1 [as=column1:1]
 │    │         ├── 2.0 [as=column3:3]
 │    │         └── 3 [as=column5:5]
 │    └── aggregations
 │         ├── avg [as=avg:2]
 │         │    └── column1:1
 │         ├── avg [as=avg:4]
 │         │    └── column3:3
 │         └── avg [as=avg:6]
 │              └── column5:5
 └── projections
      ├── avg:2::FLOAT8 [as=avg:7]
      └── avg:6::FLOAT8 [as=avg:8]

build
SELECT count(2::int), count(3::float), count(4::decimal)
----
scalar-group-by
 ├── columns: count:2!null count:4!null count:6!null
 ├── project
 │    ├── columns: column1:1!null column3:3!null column5:5!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         ├── 2 [as=column1:1]
 │         ├── 3.0 [as=column3:3]
 │         └── 4 [as=column5:5]
 └── aggregations
      ├── count [as=count:2]
      │    └── column1:1
      ├── count [as=count:4]
      │    └── column3:3
      └── count [as=count:6]
           └── column5:5

build
SELECT sum(1::int), sum(2::float), sum(3::decimal)
----
scalar-group-by
 ├── columns: sum:2 sum:4 sum:6
 ├── project
 │    ├── columns: column1:1!null column3:3!null column5:5!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         ├── 1 [as=column1:1]
 │         ├── 2.0 [as=column3:3]
 │         └── 3 [as=column5:5]
 └── aggregations
      ├── sum [as=sum:2]
      │    └── column1:1
      ├── sum [as=sum:4]
      │    └── column3:3
      └── sum [as=sum:6]
           └── column5:5

build
SELECT variance(1::int), variance(1::float), variance(1::decimal)
----
scalar-group-by
 ├── columns: variance:2 variance:4 variance:6
 ├── project
 │    ├── columns: column1:1!null column3:3!null column5:5!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         ├── 1 [as=column1:1]
 │         ├── 1.0 [as=column3:3]
 │         └── 1 [as=column5:5]
 └── aggregations
      ├── variance [as=variance:2]
      │    └── column1:1
      ├── variance [as=variance:4]
      │    └── column3:3
      └── variance [as=variance:6]
           └── column5:5

build
SELECT stddev(1::int), stddev(1::float), stddev(1::decimal)
----
scalar-group-by
 ├── columns: stddev:2 stddev:4 stddev:6
 ├── project
 │    ├── columns: column1:1!null column3:3!null column5:5!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         ├── 1 [as=column1:1]
 │         ├── 1.0 [as=column3:3]
 │         └── 1 [as=column5:5]
 └── aggregations
      ├── std-dev [as=stddev:2]
      │    └── column1:1
      ├── std-dev [as=stddev:4]
      │    └── column3:3
      └── std-dev [as=stddev:6]
           └── column5:5

# Ensure subqueries don't trigger aggregation.
build
SELECT x > (SELECT avg(0)) AS r FROM xyz LIMIT 1
----
limit
 ├── columns: r:8
 ├── project
 │    ├── columns: r:8
 │    ├── limit hint: 1.00
 │    ├── scan xyz
 │    │    ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    │    └── limit hint: 1.00
 │    └── projections
 │         └── gt [as=r:8]
 │              ├── x:1
 │              └── subquery
 │                   └── max1-row
 │                        ├── columns: avg:7
 │                        └── scalar-group-by
 │                             ├── columns: avg:7
 │                             ├── project
 │                             │    ├── columns: column6:6!null
 │                             │    ├── values
 │                             │    │    └── ()
 │                             │    └── projections
 │                             │         └── 0 [as=column6:6]
 │                             └── aggregations
 │                                  └── avg [as=avg:7]
 │                                       └── column6:6
 └── 1

build
SELECT x > (SELECT avg(y) FROM xyz) AS r FROM xyz LIMIT 1
----
limit
 ├── columns: r:12
 ├── project
 │    ├── columns: r:12
 │    ├── limit hint: 1.00
 │    ├── scan xyz
 │    │    ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    │    └── limit hint: 1.00
 │    └── projections
 │         └── gt [as=r:12]
 │              ├── x:1
 │              └── subquery
 │                   └── max1-row
 │                        ├── columns: avg:11
 │                        └── scalar-group-by
 │                             ├── columns: avg:11
 │                             ├── project
 │                             │    ├── columns: y:7
 │                             │    └── scan xyz
 │                             │         └── columns: x:6!null y:7 z:8 crdb_internal_mvcc_timestamp:9 tableoid:10
 │                             └── aggregations
 │                                  └── avg [as=avg:11]
 │                                       └── y:7
 └── 1

exec-ddl
CREATE TABLE bools (b BOOL)
----

build
SELECT bool_and(b), bool_or(b) FROM bools
----
scalar-group-by
 ├── columns: bool_and:5 bool_or:6
 ├── project
 │    ├── columns: b:1
 │    └── scan bools
 │         └── columns: b:1 rowid:2!null crdb_internal_mvcc_timestamp:3 tableoid:4
 └── aggregations
      ├── bool-and [as=bool_and:5]
      │    └── b:1
      └── bool-or [as=bool_or:6]
           └── b:1


# Tests with * inside GROUP BY.
build
SELECT 1 r FROM kv GROUP BY kv.*;
----
project
 ├── columns: r:7!null
 ├── group-by (hash)
 │    ├── columns: k:1!null v:2 w:3 s:4
 │    ├── grouping columns: k:1!null v:2 w:3 s:4
 │    └── project
 │         ├── columns: k:1!null v:2 w:3 s:4
 │         └── scan kv
 │              └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── projections
      └── 1 [as=r:7]

exec-ddl
CREATE TABLE xor_bytes (a bytes, b int, c int)
----

build
SELECT to_hex(xor_agg(a)), xor_agg(c) FROM xor_bytes
----
project
 ├── columns: to_hex:9 xor_agg:8
 ├── scalar-group-by
 │    ├── columns: xor_agg:7 xor_agg:8
 │    ├── project
 │    │    ├── columns: a:1 c:3
 │    │    └── scan xor_bytes
 │    │         └── columns: a:1 b:2 c:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         ├── xor-agg [as=xor_agg:7]
 │         │    └── a:1
 │         └── xor-agg [as=xor_agg:8]
 │              └── c:3
 └── projections
      └── to_hex(xor_agg:7) [as=to_hex:9]

build
SELECT to_hex(xor_agg(a)), b, xor_agg(c) FROM xor_bytes GROUP BY b ORDER BY b
----
sort
 ├── columns: to_hex:9 b:2 xor_agg:8
 ├── ordering: +2
 └── project
      ├── columns: to_hex:9 b:2 xor_agg:8
      ├── group-by (hash)
      │    ├── columns: b:2 xor_agg:7 xor_agg:8
      │    ├── grouping columns: b:2
      │    ├── project
      │    │    ├── columns: a:1 b:2 c:3
      │    │    └── scan xor_bytes
      │    │         └── columns: a:1 b:2 c:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── aggregations
      │         ├── xor-agg [as=xor_agg:7]
      │         │    └── a:1
      │         └── xor-agg [as=xor_agg:8]
      │              └── c:3
      └── projections
           └── to_hex(xor_agg:7) [as=to_hex:9]

# At execution time, this query will cause the error:
# "arguments to xor must all be the same length"
build
SELECT xor_agg(i) FROM (VALUES (b'\x01'), (b'\x01\x01')) AS a(i)
----
scalar-group-by
 ├── columns: xor_agg:2
 ├── values
 │    ├── columns: column1:1!null
 │    ├── ('\x01',)
 │    └── ('\x0101',)
 └── aggregations
      └── xor-agg [as=xor_agg:2]
           └── column1:1

build
SELECT max(true), min(true)
----
scalar-group-by
 ├── columns: max:2 min:3
 ├── project
 │    ├── columns: column1:1!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         └── true [as=column1:1]
 └── aggregations
      ├── max [as=max:2]
      │    └── column1:1
      └── min [as=min:3]
           └── column1:1

build
SELECT concat_agg(s) FROM (SELECT s FROM kv ORDER BY k)
----
scalar-group-by
 ├── columns: concat_agg:7
 ├── internal-ordering: +1
 ├── project
 │    ├── columns: k:1!null s:4
 │    ├── ordering: +1
 │    └── scan kv
 │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── ordering: +1
 └── aggregations
      └── concat-agg [as=concat_agg:7]
           └── s:4

build
SELECT json_agg(s) FROM (SELECT s FROM kv ORDER BY k)
----
scalar-group-by
 ├── columns: json_agg:7
 ├── internal-ordering: +1
 ├── project
 │    ├── columns: k:1!null s:4
 │    ├── ordering: +1
 │    └── scan kv
 │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── ordering: +1
 └── aggregations
      └── json-agg [as=json_agg:7]
           └── s:4

build
SELECT jsonb_agg(s) FROM (SELECT s FROM kv ORDER BY k)
----
scalar-group-by
 ├── columns: jsonb_agg:7
 ├── internal-ordering: +1
 ├── project
 │    ├── columns: k:1!null s:4
 │    ├── ordering: +1
 │    └── scan kv
 │         ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── ordering: +1
 └── aggregations
      └── jsonb-agg [as=jsonb_agg:7]
           └── s:4

exec-ddl
CREATE TABLE ab (
  a INT PRIMARY KEY,
  b INT,
  FAMILY (a),
  FAMILY (b)
)
----

exec-ddl
CREATE TABLE xy(x STRING, y STRING);
----

# Grouping and rendering tuples.
build
SELECT (b, a) AS r FROM ab GROUP BY (b, a)
----
project
 ├── columns: r:5
 ├── group-by (hash)
 │    ├── columns: a:1!null b:2
 │    ├── grouping columns: a:1!null b:2
 │    └── project
 │         ├── columns: a:1!null b:2
 │         └── scan ab
 │              └── columns: a:1!null b:2 crdb_internal_mvcc_timestamp:3 tableoid:4
 └── projections
      └── (b:2, a:1) [as=r:5]

build
SELECT min(y), (b, a) AS r
 FROM ab, xy GROUP BY (x, (a, b))
----
project
 ├── columns: min:10 r:11
 ├── group-by (hash)
 │    ├── columns: a:1!null b:2 x:5 min:10
 │    ├── grouping columns: a:1!null b:2 x:5
 │    ├── project
 │    │    ├── columns: a:1!null b:2 x:5 y:6
 │    │    └── inner-join (cross)
 │    │         ├── columns: a:1!null b:2 ab.crdb_internal_mvcc_timestamp:3 ab.tableoid:4 x:5 y:6 rowid:7!null xy.crdb_internal_mvcc_timestamp:8 xy.tableoid:9
 │    │         ├── scan ab
 │    │         │    └── columns: a:1!null b:2 ab.crdb_internal_mvcc_timestamp:3 ab.tableoid:4
 │    │         ├── scan xy
 │    │         │    └── columns: x:5 y:6 rowid:7!null xy.crdb_internal_mvcc_timestamp:8 xy.tableoid:9
 │    │         └── filters (true)
 │    └── aggregations
 │         └── min [as=min:10]
 │              └── y:6
 └── projections
      └── (b:2, a:1) [as=r:11]

build
SELECT v, count(k) FROM kv GROUP BY v ORDER BY count(k)
----
sort
 ├── columns: v:2 count:7!null
 ├── ordering: +7
 └── group-by (hash)
      ├── columns: v:2 count:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: k:1!null v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count [as=count:7]
                └── k:1

build
SELECT v, count(*) FROM kv GROUP BY v ORDER BY count(*)
----
sort
 ├── columns: v:2 count:7!null
 ├── ordering: +7
 └── group-by (hash)
      ├── columns: v:2 count_rows:7!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: v:2
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count-rows [as=count_rows:7]

build
SELECT v, count(1) FROM kv GROUP BY v ORDER BY count(1)
----
sort
 ├── columns: v:2 count:8!null
 ├── ordering: +8
 └── group-by (hash)
      ├── columns: v:2 count:8!null
      ├── grouping columns: v:2
      ├── project
      │    ├── columns: column7:7!null v:2
      │    ├── scan kv
      │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         └── 1 [as=column7:7]
      └── aggregations
           └── count [as=count:8]
                └── column7:7

build
SELECT (k+v)/(v+w) AS r FROM kv GROUP BY k+v, v+w;
----
project
 ├── columns: r:9
 ├── group-by (hash)
 │    ├── columns: column7:7 column8:8
 │    ├── grouping columns: column7:7 column8:8
 │    └── project
 │         ├── columns: column7:7 column8:8
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── projections
 │              ├── k:1 + v:2 [as=column7:7]
 │              └── v:2 + w:3 [as=column8:8]
 └── projections
      └── column7:7 / column8:8 [as=r:9]

# Check that everything still works with differently qualified names
build fully-qualify-names
SELECT sum(t.kv.w), t.kv.v FROM t.kv GROUP BY v, kv.k * w
----
project
 ├── columns: sum:7 v:2
 └── group-by (hash)
      ├── columns: t.public.kv.v:2 sum:7 column8:8
      ├── grouping columns: t.public.kv.v:2 column8:8
      ├── project
      │    ├── columns: column8:8 t.public.kv.v:2 t.public.kv.w:3
      │    ├── scan t.public.kv
      │    │    └── columns: t.public.kv.k:1!null t.public.kv.v:2 t.public.kv.w:3 t.public.kv.s:4 t.public.kv.crdb_internal_mvcc_timestamp:5 t.public.kv.tableoid:6
      │    └── projections
      │         └── t.public.kv.k:1 * t.public.kv.w:3 [as=column8:8]
      └── aggregations
           └── sum [as=sum:7]
                └── t.public.kv.w:3

build fully-qualify-names
SELECT sum(t.kv.w), lower(s), t.kv.v + k * t.kv.w AS r, t.kv.v FROM t.kv GROUP BY v, lower(kv.s), kv.k * w
----
project
 ├── columns: sum:7 lower:8 r:10 v:2
 ├── group-by (hash)
 │    ├── columns: t.public.kv.v:2 sum:7 column8:8 column9:9
 │    ├── grouping columns: t.public.kv.v:2 column8:8 column9:9
 │    ├── project
 │    │    ├── columns: column8:8 column9:9 t.public.kv.v:2 t.public.kv.w:3
 │    │    ├── scan t.public.kv
 │    │    │    └── columns: t.public.kv.k:1!null t.public.kv.v:2 t.public.kv.w:3 t.public.kv.s:4 t.public.kv.crdb_internal_mvcc_timestamp:5 t.public.kv.tableoid:6
 │    │    └── projections
 │    │         ├── lower(t.public.kv.s:4) [as=column8:8]
 │    │         └── t.public.kv.k:1 * t.public.kv.w:3 [as=column9:9]
 │    └── aggregations
 │         └── sum [as=sum:7]
 │              └── t.public.kv.w:3
 └── projections
      └── t.public.kv.v:2 + column9:9 [as=r:10]

# Check all the different types of scalar expressions as group by columns
build
SELECT b1.b AND abc.c AND b2.b AS r FROM bools b1, bools b2, abc GROUP BY b1.b AND abc.c, b2.b
----
project
 ├── columns: r:16
 ├── group-by (hash)
 │    ├── columns: b2.b:5 column15:15
 │    ├── grouping columns: b2.b:5 column15:15
 │    └── project
 │         ├── columns: column15:15 b2.b:5
 │         ├── inner-join (cross)
 │         │    ├── columns: b1.b:1 b1.rowid:2!null b1.crdb_internal_mvcc_timestamp:3 b1.tableoid:4 b2.b:5 b2.rowid:6!null b2.crdb_internal_mvcc_timestamp:7 b2.tableoid:8 a:9!null abc.b:10 c:11 d:12 abc.crdb_internal_mvcc_timestamp:13 abc.tableoid:14
 │         │    ├── scan bools [as=b1]
 │         │    │    └── columns: b1.b:1 b1.rowid:2!null b1.crdb_internal_mvcc_timestamp:3 b1.tableoid:4
 │         │    ├── inner-join (cross)
 │         │    │    ├── columns: b2.b:5 b2.rowid:6!null b2.crdb_internal_mvcc_timestamp:7 b2.tableoid:8 a:9!null abc.b:10 c:11 d:12 abc.crdb_internal_mvcc_timestamp:13 abc.tableoid:14
 │         │    │    ├── scan bools [as=b2]
 │         │    │    │    └── columns: b2.b:5 b2.rowid:6!null b2.crdb_internal_mvcc_timestamp:7 b2.tableoid:8
 │         │    │    ├── scan abc
 │         │    │    │    └── columns: a:9!null abc.b:10 c:11 d:12 abc.crdb_internal_mvcc_timestamp:13 abc.tableoid:14
 │         │    │    └── filters (true)
 │         │    └── filters (true)
 │         └── projections
 │              └── b1.b:1 AND c:11 [as=column15:15]
 └── projections
      └── column15:15 AND b2.b:5 [as=r:16]

build
SELECT b1.b AND abc.c AND abc.c FROM bools b1, bools b2, abc GROUP BY b1.b AND abc.c, b2.b
----
error (42803): column "c" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT b1.b OR abc.c OR b2.b AS r FROM bools b1, bools b2, abc GROUP BY b1.b OR abc.c, b2.b
----
project
 ├── columns: r:16
 ├── group-by (hash)
 │    ├── columns: b2.b:5 column15:15
 │    ├── grouping columns: b2.b:5 column15:15
 │    └── project
 │         ├── columns: column15:15 b2.b:5
 │         ├── inner-join (cross)
 │         │    ├── columns: b1.b:1 b1.rowid:2!null b1.crdb_internal_mvcc_timestamp:3 b1.tableoid:4 b2.b:5 b2.rowid:6!null b2.crdb_internal_mvcc_timestamp:7 b2.tableoid:8 a:9!null abc.b:10 c:11 d:12 abc.crdb_internal_mvcc_timestamp:13 abc.tableoid:14
 │         │    ├── scan bools [as=b1]
 │         │    │    └── columns: b1.b:1 b1.rowid:2!null b1.crdb_internal_mvcc_timestamp:3 b1.tableoid:4
 │         │    ├── inner-join (cross)
 │         │    │    ├── columns: b2.b:5 b2.rowid:6!null b2.crdb_internal_mvcc_timestamp:7 b2.tableoid:8 a:9!null abc.b:10 c:11 d:12 abc.crdb_internal_mvcc_timestamp:13 abc.tableoid:14
 │         │    │    ├── scan bools [as=b2]
 │         │    │    │    └── columns: b2.b:5 b2.rowid:6!null b2.crdb_internal_mvcc_timestamp:7 b2.tableoid:8
 │         │    │    ├── scan abc
 │         │    │    │    └── columns: a:9!null abc.b:10 c:11 d:12 abc.crdb_internal_mvcc_timestamp:13 abc.tableoid:14
 │         │    │    └── filters (true)
 │         │    └── filters (true)
 │         └── projections
 │              └── b1.b:1 OR c:11 [as=column15:15]
 └── projections
      └── column15:15 OR b2.b:5 [as=r:16]

build
SELECT b1.b OR abc.c OR abc.c FROM bools b1, bools b2, abc GROUP BY b1.b OR abc.c, b2.b
----
error (42803): column "c" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT k % w % v AS r FROM kv GROUP BY k % w, v
----
project
 ├── columns: r:8
 ├── group-by (hash)
 │    ├── columns: v:2 column7:7
 │    ├── grouping columns: v:2 column7:7
 │    └── project
 │         ├── columns: column7:7 v:2
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── projections
 │              └── k:1 % w:3 [as=column7:7]
 └── projections
      └── column7:7 % v:2 [as=r:8]

build
SELECT concat(concat(s, a), a) FROM kv, abc GROUP BY concat(s, a), a
----
project
 ├── columns: concat:14
 ├── group-by (hash)
 │    ├── columns: a:7!null column13:13
 │    ├── grouping columns: a:7!null column13:13
 │    └── project
 │         ├── columns: column13:13 a:7!null
 │         ├── inner-join (cross)
 │         │    ├── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6 a:7!null b:8 c:9 d:10 abc.crdb_internal_mvcc_timestamp:11 abc.tableoid:12
 │         │    ├── scan kv
 │         │    │    └── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6
 │         │    ├── scan abc
 │         │    │    └── columns: a:7!null b:8 c:9 d:10 abc.crdb_internal_mvcc_timestamp:11 abc.tableoid:12
 │         │    └── filters (true)
 │         └── projections
 │              └── concat(s:4, a:7) [as=column13:13]
 └── projections
      └── concat(column13:13, a:7) [as=concat:14]

build
SELECT concat(concat(s, a), s) FROM kv, abc GROUP BY concat(s, a), a
----
error (42803): column "s" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT k < w AND v != 5 AS r FROM kv GROUP BY k < w, v
----
project
 ├── columns: r:8
 ├── group-by (hash)
 │    ├── columns: v:2 column7:7
 │    ├── grouping columns: v:2 column7:7
 │    └── project
 │         ├── columns: column7:7 v:2
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── projections
 │              └── k:1 < w:3 [as=column7:7]
 └── projections
      └── column7:7 AND (v:2 != 5) [as=r:8]

build
SELECT k < w AND k < v FROM kv GROUP BY k < w, v
----
error (42803): column "k" must appear in the GROUP BY clause or be used in an aggregate function

exec-ddl
CREATE TABLE foo (bar JSON, baz JSON)
----

build
SELECT a.bar @> b.baz AND b.baz @> b.baz AS r FROM foo AS a, foo AS b GROUP BY a.bar @> b.baz, b.baz
----
project
 ├── columns: r:12
 ├── group-by (hash)
 │    ├── columns: b.baz:7 column11:11
 │    ├── grouping columns: b.baz:7 column11:11
 │    └── project
 │         ├── columns: column11:11 b.baz:7
 │         ├── inner-join (cross)
 │         │    ├── columns: a.bar:1 a.baz:2 a.rowid:3!null a.crdb_internal_mvcc_timestamp:4 a.tableoid:5 b.bar:6 b.baz:7 b.rowid:8!null b.crdb_internal_mvcc_timestamp:9 b.tableoid:10
 │         │    ├── scan foo [as=a]
 │         │    │    └── columns: a.bar:1 a.baz:2 a.rowid:3!null a.crdb_internal_mvcc_timestamp:4 a.tableoid:5
 │         │    ├── scan foo [as=b]
 │         │    │    └── columns: b.bar:6 b.baz:7 b.rowid:8!null b.crdb_internal_mvcc_timestamp:9 b.tableoid:10
 │         │    └── filters (true)
 │         └── projections
 │              └── a.bar:1 @> b.baz:7 [as=column11:11]
 └── projections
      └── column11:11 AND (b.baz:7 @> b.baz:7) [as=r:12]

build
SELECT a.bar @> b.baz AND b.baz @> b.baz FROM foo AS a, foo AS b GROUP BY b.baz <@ a.bar, b.baz
----
error (42803): column "bar" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT b.baz <@ a.bar AND b.baz <@ b.baz AS r FROM foo AS a, foo AS b GROUP BY b.baz <@ a.bar, b.baz
----
project
 ├── columns: r:12
 ├── group-by (hash)
 │    ├── columns: b.baz:7 column11:11
 │    ├── grouping columns: b.baz:7 column11:11
 │    └── project
 │         ├── columns: column11:11 b.baz:7
 │         ├── inner-join (cross)
 │         │    ├── columns: a.bar:1 a.baz:2 a.rowid:3!null a.crdb_internal_mvcc_timestamp:4 a.tableoid:5 b.bar:6 b.baz:7 b.rowid:8!null b.crdb_internal_mvcc_timestamp:9 b.tableoid:10
 │         │    ├── scan foo [as=a]
 │         │    │    └── columns: a.bar:1 a.baz:2 a.rowid:3!null a.crdb_internal_mvcc_timestamp:4 a.tableoid:5
 │         │    ├── scan foo [as=b]
 │         │    │    └── columns: b.bar:6 b.baz:7 b.rowid:8!null b.crdb_internal_mvcc_timestamp:9 b.tableoid:10
 │         │    └── filters (true)
 │         └── projections
 │              └── b.baz:7 <@ a.bar:1 [as=column11:11]
 └── projections
      └── column11:11 AND (b.baz:7 <@ b.baz:7) [as=r:12]

exec-ddl
CREATE TABLE times (t time PRIMARY KEY)
----

build
SELECT date_trunc('second', a.t) - date_trunc('minute', b.t) AS r FROM times a, times b
  GROUP BY date_trunc('second', a.t), date_trunc('minute', b.t)
----
project
 ├── columns: r:9
 ├── group-by (hash)
 │    ├── columns: column7:7 column8:8
 │    ├── grouping columns: column7:7 column8:8
 │    └── project
 │         ├── columns: column7:7 column8:8
 │         ├── inner-join (cross)
 │         │    ├── columns: a.t:1!null a.crdb_internal_mvcc_timestamp:2 a.tableoid:3 b.t:4!null b.crdb_internal_mvcc_timestamp:5 b.tableoid:6
 │         │    ├── scan times [as=a]
 │         │    │    └── columns: a.t:1!null a.crdb_internal_mvcc_timestamp:2 a.tableoid:3
 │         │    ├── scan times [as=b]
 │         │    │    └── columns: b.t:4!null b.crdb_internal_mvcc_timestamp:5 b.tableoid:6
 │         │    └── filters (true)
 │         └── projections
 │              ├── date_trunc('second', a.t:1) [as=column7:7]
 │              └── date_trunc('minute', b.t:4) [as=column8:8]
 └── projections
      └── column7:7 - column8:8 [as=r:9]

build
SELECT date_trunc('second', a.t) - date_trunc('second', b.t) FROM times a, times b
  GROUP BY date_trunc('second', a.t), date_trunc('minute', b.t)
----
error (42803): column "t" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT NOT b AS r FROM bools GROUP BY NOT b
----
group-by (hash)
 ├── columns: r:5
 ├── grouping columns: column5:5
 └── project
      ├── columns: column5:5
      ├── scan bools
      │    └── columns: b:1 rowid:2!null crdb_internal_mvcc_timestamp:3 tableoid:4
      └── projections
           └── NOT b:1 [as=column5:5]

build
SELECT b FROM bools GROUP BY NOT b
----
error (42803): column "b" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT NOT b AS r FROM bools GROUP BY b
----
project
 ├── columns: r:5
 ├── group-by (hash)
 │    ├── columns: b:1
 │    ├── grouping columns: b:1
 │    └── project
 │         ├── columns: b:1
 │         └── scan bools
 │              └── columns: b:1 rowid:2!null crdb_internal_mvcc_timestamp:3 tableoid:4
 └── projections
      └── NOT b:1 [as=r:5]

build
SELECT +k * (-w) AS r FROM kv GROUP BY +k, -w
----
project
 ├── columns: r:8
 ├── group-by (hash)
 │    ├── columns: k:1!null column7:7
 │    ├── grouping columns: k:1!null column7:7
 │    └── project
 │         ├── columns: column7:7 k:1!null
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── projections
 │              └── -w:3 [as=column7:7]
 └── projections
      └── k:1 * column7:7 [as=r:8]

build
SELECT k * (-w) FROM kv GROUP BY +k, -w
----
project
 ├── columns: "?column?":8
 ├── group-by (hash)
 │    ├── columns: k:1!null column7:7
 │    ├── grouping columns: k:1!null column7:7
 │    └── project
 │         ├── columns: column7:7 k:1!null
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── projections
 │              └── -w:3 [as=column7:7]
 └── projections
      └── k:1 * column7:7 [as="?column?":8]

build
SELECT +k * (-w) AS r FROM kv GROUP BY k, w
----
project
 ├── columns: r:7
 ├── group-by (hash)
 │    ├── columns: k:1!null w:3
 │    ├── grouping columns: k:1!null w:3
 │    └── project
 │         ├── columns: k:1!null w:3
 │         └── scan kv
 │              └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── projections
      └── k:1 * (-w:3) [as=r:7]

build
SELECT 1 + min(v*2) AS r FROM kv GROUP BY k+3
----
project
 ├── columns: r:10
 ├── group-by (hash)
 │    ├── columns: min:8 column9:9!null
 │    ├── grouping columns: column9:9!null
 │    ├── project
 │    │    ├── columns: column7:7 column9:9!null
 │    │    ├── scan kv
 │    │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         ├── v:2 * 2 [as=column7:7]
 │    │         └── k:1 + 3 [as=column9:9]
 │    └── aggregations
 │         └── min [as=min:8]
 │              └── column7:7
 └── projections
      └── 1 + min:8 [as=r:10]

build
SELECT count(*) FROM kv GROUP BY k, k
----
project
 ├── columns: count:7!null
 └── group-by (hash)
      ├── columns: k:1!null count_rows:7!null
      ├── grouping columns: k:1!null
      ├── project
      │    ├── columns: k:1!null
      │    └── scan kv
      │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── aggregations
           └── count-rows [as=count_rows:7]

build
SELECT count(upper(s)) FROM kv GROUP BY upper(s)
----
project
 ├── columns: count:8!null
 └── group-by (hash)
      ├── columns: column7:7 count:8!null
      ├── grouping columns: column7:7
      ├── project
      │    ├── columns: column7:7
      │    ├── scan kv
      │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         └── upper(s:4) [as=column7:7]
      └── aggregations
           └── count [as=count:8]
                └── column7:7

build
SELECT sum(abc.d) FROM kv JOIN abc ON kv.k >= abc.d GROUP BY kv.*
----
project
 ├── columns: sum:13!null
 └── group-by (hash)
      ├── columns: k:1!null v:2 w:3 s:4 sum:13!null
      ├── grouping columns: k:1!null v:2 w:3 s:4
      ├── project
      │    ├── columns: k:1!null v:2 w:3 s:4 d:10!null
      │    └── inner-join (cross)
      │         ├── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6 a:7!null b:8 c:9 d:10!null abc.crdb_internal_mvcc_timestamp:11 abc.tableoid:12
      │         ├── scan kv
      │         │    └── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6
      │         ├── scan abc
      │         │    └── columns: a:7!null b:8 c:9 d:10 abc.crdb_internal_mvcc_timestamp:11 abc.tableoid:12
      │         └── filters
      │              └── k:1 >= d:10
      └── aggregations
           └── sum [as=sum:13]
                └── d:10

build
SELECT sum(DISTINCT abc.d) FROM abc
----
scalar-group-by
 ├── columns: sum:7
 ├── project
 │    ├── columns: d:4
 │    └── scan abc
 │         └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=sum:7]
           └── sum
                └── d:4

# FILTER.

build
SELECT sum(abc.d) FILTER (WHERE abc.d > 0) FROM abc
----
scalar-group-by
 ├── columns: sum:8
 ├── project
 │    ├── columns: column7:7 d:4
 │    ├── scan abc
 │    │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── d:4 > 0 [as=column7:7]
 └── aggregations
      └── agg-filter [as=sum:8]
           ├── sum
           │    └── d:4
           └── column7:7

# Ensure aggregates with FILTER coexist properly with non-FILTER aggregates.
build
SELECT
    sum(x) FILTER (WHERE y > 0),
    avg(DISTINCT z),
    avg(DISTINCT z) FILTER (WHERE y > 0)
FROM xyz
----
scalar-group-by
 ├── columns: sum:7 avg:8 avg:9
 ├── project
 │    ├── columns: column6:6 x:1!null z:3
 │    ├── scan xyz
 │    │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    └── projections
 │         └── y:2 > 0 [as=column6:6]
 └── aggregations
      ├── agg-filter [as=sum:7]
      │    ├── sum
      │    │    └── x:1
      │    └── column6:6
      ├── agg-distinct [as=avg:8]
      │    └── avg
      │         └── z:3
      └── agg-filter [as=avg:9]
           ├── agg-distinct
           │    └── avg
           │         └── z:3
           └── column6:6

# Ensure aggregates involving FILTER are deduplicated.
build
SELECT
    avg(DISTINCT x),
    avg(DISTINCT x),
    avg(DISTINCT x) FILTER (WHERE y > 0),
    avg(DISTINCT x) FILTER (WHERE y > 0)
FROM xyz
----
scalar-group-by
 ├── columns: avg:6 avg:6 avg:8 avg:8
 ├── project
 │    ├── columns: column7:7 x:1!null
 │    ├── scan xyz
 │    │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    └── projections
 │         └── y:2 > 0 [as=column7:7]
 └── aggregations
      ├── agg-distinct [as=avg:6]
      │    └── avg
      │         └── x:1
      └── agg-filter [as=avg:8]
           ├── agg-distinct
           │    └── avg
           │         └── x:1
           └── column7:7

build
SELECT
    string_agg(x::string, 'foo') FILTER (WHERE y > 0)
FROM xyz
----
scalar-group-by
 ├── columns: string_agg:9
 ├── project
 │    ├── columns: column6:6!null column7:7!null column8:8
 │    ├── scan xyz
 │    │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    └── projections
 │         ├── x:1::STRING [as=column6:6]
 │         ├── 'foo' [as=column7:7]
 │         └── y:2 > 0 [as=column8:8]
 └── aggregations
      └── agg-filter [as=string_agg:9]
           ├── string-agg
           │    ├── column6:6
           │    └── column7:7
           └── column8:8

build
SELECT y, count(*) FILTER (WHERE x > 5) FROM xyz GROUP BY y
----
group-by (hash)
 ├── columns: y:2 count:8!null
 ├── grouping columns: y:2
 ├── project
 │    ├── columns: column6:6!null column7:7!null y:2
 │    ├── scan xyz
 │    │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │    └── projections
 │         ├── true [as=column6:6]
 │         └── x:1 > 5 [as=column7:7]
 └── aggregations
      └── agg-filter [as=count:8]
           ├── count
           │    └── column6:6
           └── column7:7

build
SELECT y, count(*) FILTER (WHERE count(*) > 5) FROM xyz GROUP BY y
----
error (42803): count_rows(): aggregate functions are not allowed in FILTER


# Check that ordering by an alias of an aggregate works.
build
SELECT max(k) AS mk FROM kv GROUP BY v ORDER BY mk
----
sort
 ├── columns: mk:7!null
 ├── ordering: +7
 └── project
      ├── columns: max:7!null
      └── group-by (hash)
           ├── columns: v:2 max:7!null
           ├── grouping columns: v:2
           ├── project
           │    ├── columns: k:1!null v:2
           │    └── scan kv
           │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           └── aggregations
                └── max [as=max:7]
                     └── k:1

build
SELECT max(k) AS mk FROM kv GROUP BY v ORDER BY max(k)
----
sort
 ├── columns: mk:7!null
 ├── ordering: +7
 └── project
      ├── columns: max:7!null
      └── group-by (hash)
           ├── columns: v:2 max:7!null
           ├── grouping columns: v:2
           ├── project
           │    ├── columns: k:1!null v:2
           │    └── scan kv
           │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           └── aggregations
                └── max [as=max:7]
                     └── k:1

build
SELECT max(k) AS mk1, max(k) AS mk2 FROM kv GROUP BY v ORDER BY mk1
----
sort
 ├── columns: mk1:7!null mk2:7!null
 ├── ordering: +7
 └── project
      ├── columns: max:7!null
      └── group-by (hash)
           ├── columns: v:2 max:7!null
           ├── grouping columns: v:2
           ├── project
           │    ├── columns: k:1!null v:2
           │    └── scan kv
           │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           └── aggregations
                └── max [as=max:7]
                     └── k:1

build
SELECT max(k) AS mk1, max(k) AS mk2 FROM kv GROUP BY v ORDER BY mk2
----
sort
 ├── columns: mk1:7!null mk2:7!null
 ├── ordering: +7
 └── project
      ├── columns: max:7!null
      └── group-by (hash)
           ├── columns: v:2 max:7!null
           ├── grouping columns: v:2
           ├── project
           │    ├── columns: k:1!null v:2
           │    └── scan kv
           │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           └── aggregations
                └── max [as=max:7]
                     └── k:1

build
SELECT max(k) AS mk1, max(k)/5 AS mk2 FROM kv GROUP BY v ORDER BY mk2
----
sort
 ├── columns: mk1:7!null mk2:8!null
 ├── ordering: +8
 └── project
      ├── columns: mk2:8!null max:7!null
      ├── group-by (hash)
      │    ├── columns: v:2 max:7!null
      │    ├── grouping columns: v:2
      │    ├── project
      │    │    ├── columns: k:1!null v:2
      │    │    └── scan kv
      │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── aggregations
      │         └── max [as=max:7]
      │              └── k:1
      └── projections
           └── max:7 / 5 [as=mk2:8]

# Grouping columns cannot be reused inside an aggregate input expression
# because the aggregate input expressions and grouping expressions are
# built as part of the same projection.
build
SELECT max((k+v)/(k-v)) AS r, (k+v)*(k-v) AS s FROM kv GROUP BY k+v, k-v
----
project
 ├── columns: r:8 s:11
 ├── group-by (hash)
 │    ├── columns: max:8 column9:9 column10:10
 │    ├── grouping columns: column9:9 column10:10
 │    ├── project
 │    │    ├── columns: column7:7 column9:9 column10:10
 │    │    ├── scan kv
 │    │    │    └── columns: k:1!null v:2 w:3 kv.s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         ├── (k:1 + v:2) / (k:1 - v:2) [as=column7:7]
 │    │         ├── k:1 + v:2 [as=column9:9]
 │    │         └── k:1 - v:2 [as=column10:10]
 │    └── aggregations
 │         └── max [as=max:8]
 │              └── column7:7
 └── projections
      └── column9:9 * column10:10 [as=s:11]

build
SELECT max((k+v)/(k-v)) AS r, (k+v)*(k-v) AS s FROM kv GROUP BY k+v, (k+v)/(k-v), (k+v)*(k-v)
----
project
 ├── columns: r:8 s:10
 └── group-by (hash)
      ├── columns: column7:7 max:8 column9:9 column10:10
      ├── grouping columns: column7:7 column9:9 column10:10
      ├── project
      │    ├── columns: column7:7 column9:9 column10:10
      │    ├── scan kv
      │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         ├── (k:1 + v:2) / (k:1 - v:2) [as=column7:7]
      │         ├── k:1 + v:2 [as=column9:9]
      │         └── (k:1 + v:2) * (k:1 - v:2) [as=column10:10]
      └── aggregations
           └── max [as=max:8]
                └── column7:7

# Tests for corr.

build
SELECT corr(k, v) FROM kv
----
scalar-group-by
 ├── columns: corr:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── corr [as=corr:7]
           ├── k:1
           └── v:2

build
SELECT corr(k) FROM kv
----
error (42883): unknown signature: corr(int)

build
SELECT corr(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: corr(int, string, string)

build
SELECT corr(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: corr:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=corr:7]
           └── corr
                ├── k:1
                └── v:2

build
SELECT max(s), corr(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 corr:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── corr [as=corr:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT corr(k, NULL) FROM kv
----
error (42725): ambiguous call: corr(int, unknown), candidates are:
corr(int, decimal) -> float
corr(int, int) -> float
corr(int, float) -> float

build
SELECT corr('foo', v) FROM kv
----
error (22P02): corr(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for covar_pop.

build
SELECT covar_pop(k, v) FROM kv
----
scalar-group-by
 ├── columns: covar_pop:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── covar-pop [as=covar_pop:7]
           ├── k:1
           └── v:2

build
SELECT covar_pop(k) FROM kv
----
error (42883): unknown signature: covar_pop(int)

build
SELECT covar_pop(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: covar_pop(int, string, string)

build
SELECT covar_pop(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: covar_pop:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=covar_pop:7]
           └── covar-pop
                ├── k:1
                └── v:2

build
SELECT max(s), covar_pop(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 covar_pop:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── covar-pop [as=covar_pop:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT covar_pop(k, NULL) FROM kv
----
error (42725): ambiguous call: covar_pop(int, unknown), candidates are:
covar_pop(int, decimal) -> float
covar_pop(int, int) -> float
covar_pop(int, float) -> float

build
SELECT covar_pop('foo', v) FROM kv
----
error (22P02): covar_pop(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_intercept.

build
SELECT regr_intercept(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_intercept:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-intercept [as=regr_intercept:7]
           ├── k:1
           └── v:2

build
SELECT regr_intercept(k) FROM kv
----
error (42883): unknown signature: regr_intercept(int)

build
SELECT regr_intercept(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_intercept(int, string, string)

build
SELECT regr_intercept(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_intercept:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_intercept:7]
           └── regression-intercept
                ├── k:1
                └── v:2

build
SELECT max(s), regr_intercept(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_intercept:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-intercept [as=regr_intercept:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_intercept(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_intercept(int, unknown), candidates are:
regr_intercept(int, decimal) -> float
regr_intercept(int, int) -> float
regr_intercept(int, float) -> float

build
SELECT regr_intercept('foo', v) FROM kv
----
error (22P02): regr_intercept(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_r2.

build
SELECT regr_r2(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_r2:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-r2 [as=regr_r2:7]
           ├── k:1
           └── v:2

build
SELECT regr_r2(k) FROM kv
----
error (42883): unknown signature: regr_r2(int)

build
SELECT regr_r2(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_r2(int, string, string)

build
SELECT regr_r2(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_r2:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_r2:7]
           └── regression-r2
                ├── k:1
                └── v:2

build
SELECT max(s), regr_r2(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_r2:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-r2 [as=regr_r2:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_r2(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_r2(int, unknown), candidates are:
regr_r2(int, decimal) -> float
regr_r2(int, int) -> float
regr_r2(int, float) -> float

build
SELECT regr_r2('foo', v) FROM kv
----
error (22P02): regr_r2(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_slope.

build
SELECT regr_slope(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_slope:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-slope [as=regr_slope:7]
           ├── k:1
           └── v:2

build
SELECT regr_slope(k) FROM kv
----
error (42883): unknown signature: regr_slope(int)

build
SELECT regr_slope(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_slope(int, string, string)

build
SELECT regr_slope(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_slope:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_slope:7]
           └── regression-slope
                ├── k:1
                └── v:2

build
SELECT max(s), regr_slope(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_slope:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-slope [as=regr_slope:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_slope(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_slope(int, unknown), candidates are:
regr_slope(int, decimal) -> float
regr_slope(int, int) -> float
regr_slope(int, float) -> float

build
SELECT regr_slope('foo', v) FROM kv
----
error (22P02): regr_slope(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_sxx.

build
SELECT regr_sxx(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_sxx:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-s-x-x [as=regr_sxx:7]
           ├── k:1
           └── v:2

build
SELECT regr_sxx(k) FROM kv
----
error (42883): unknown signature: regr_sxx(int)


build
SELECT regr_sxx(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_sxx(int, string, string)

build
SELECT regr_sxx(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_sxx:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_sxx:7]
           └── regression-s-x-x
                ├── k:1
                └── v:2

build
SELECT max(s), regr_sxx(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_sxx:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-s-x-x [as=regr_sxx:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_sxx(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_sxx(int, unknown), candidates are:
regr_sxx(int, decimal) -> float
regr_sxx(int, int) -> float
regr_sxx(int, float) -> float

build
SELECT regr_sxx('foo', v) FROM kv
----
error (22P02): regr_sxx(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_sxy.

build
SELECT regr_sxy(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_sxy:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-s-x-y [as=regr_sxy:7]
           ├── k:1
           └── v:2

build
SELECT regr_sxy(k) FROM kv
----
error (42883): unknown signature: regr_sxy(int)

build
SELECT regr_sxy(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_sxy(int, string, string)

build
SELECT regr_sxy(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_sxy:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_sxy:7]
           └── regression-s-x-y
                ├── k:1
                └── v:2

build
SELECT max(s), regr_sxy(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_sxy:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-s-x-y [as=regr_sxy:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_sxy(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_sxy(int, unknown), candidates are:
regr_sxy(int, decimal) -> float
regr_sxy(int, int) -> float
regr_sxy(int, float) -> float

build
SELECT regr_sxy('foo', v) FROM kv
----
error (22P02): regr_sxy(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_syy.

build
SELECT regr_syy(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_syy:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-s-y-y [as=regr_syy:7]
           ├── k:1
           └── v:2

build
SELECT regr_syy(k) FROM kv
----
error (42883): unknown signature: regr_syy(int)

build
SELECT regr_syy(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_syy(int, string, string)

build
SELECT regr_syy(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_syy:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_syy:7]
           └── regression-s-y-y
                ├── k:1
                └── v:2

build
SELECT max(s), regr_syy(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_syy:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-s-y-y [as=regr_syy:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_syy(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_syy(int, unknown), candidates are:
regr_syy(int, decimal) -> float
regr_syy(int, int) -> float
regr_syy(int, float) -> float

build
SELECT regr_syy('foo', v) FROM kv
----
error (22P02): regr_syy(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_count.

build
SELECT regr_count(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_count:7!null
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-count [as=regr_count:7]
           ├── k:1
           └── v:2

build
SELECT regr_count(k) FROM kv
----
error (42883): unknown signature: regr_count(int)

build
SELECT regr_count(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_count(int, string, string)

build
SELECT regr_count(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_count:7!null
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_count:7]
           └── regression-count
                ├── k:1
                └── v:2

build
SELECT max(s), regr_count(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_count:8!null min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-count [as=regr_count:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_count(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_count(int, unknown), candidates are:
regr_count(int, decimal) -> int
regr_count(int, int) -> int
regr_count(int, float) -> int

build
SELECT regr_count('foo', v) FROM kv
----
error (22P02): regr_count(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_avgx.

build
SELECT regr_avgx(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_avgx:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-avg-x [as=regr_avgx:7]
           ├── k:1
           └── v:2

build
SELECT regr_avgx(k) FROM kv
----
error (42883): unknown signature: regr_avgx(int)

build
SELECT regr_avgx(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_avgx(int, string, string)

build
SELECT regr_avgx(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_avgx:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_avgx:7]
           └── regression-avg-x
                ├── k:1
                └── v:2

build
SELECT max(s), regr_avgx(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_avgx:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-avg-x [as=regr_avgx:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_avgx(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_avgx(int, unknown), candidates are:
regr_avgx(int, decimal) -> float
regr_avgx(int, int) -> float
regr_avgx(int, float) -> float

build
SELECT regr_avgx('foo', v) FROM kv
----
error (22P02): regr_avgx(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for regr_avgy.

build
SELECT regr_avgy(k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_avgy:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── regression-avg-y [as=regr_avgy:7]
           ├── k:1
           └── v:2

build
SELECT regr_avgy(k) FROM kv
----
error (42883): unknown signature: regr_avgy(int)

build
SELECT regr_avgy(k, 'x', 'y') FROM kv
----
error (42883): unknown signature: regr_avgy(int, string, string)

build
SELECT regr_avgy(DISTINCT k, v) FROM kv
----
scalar-group-by
 ├── columns: regr_avgy:7
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── agg-distinct [as=regr_avgy:7]
           └── regression-avg-y
                ├── k:1
                └── v:2

build
SELECT max(s), regr_avgy(k, v), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 regr_avgy:8 min:9
 ├── project
 │    ├── columns: k:1!null v:2 s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── regression-avg-y [as=regr_avgy:8]
      │    ├── k:1
      │    └── v:2
      └── min [as=min:9]
           └── s:4

build
SELECT regr_avgy(k, NULL) FROM kv
----
error (42725): ambiguous call: regr_avgy(int, unknown), candidates are:
regr_avgy(int, decimal) -> float
regr_avgy(int, int) -> float
regr_avgy(int, float) -> float

build
SELECT regr_avgy('foo', v) FROM kv
----
error (22P02): regr_avgy(): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax

# Tests for string_agg.

build
SELECT string_agg(s, 'separator') FROM kv
----
scalar-group-by
 ├── columns: string_agg:8
 ├── project
 │    ├── columns: column7:7!null s:4
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── 'separator' [as=column7:7]
 └── aggregations
      └── string-agg [as=string_agg:8]
           ├── s:4
           └── column7:7

build
SELECT string_agg(s) FROM kv
----
error (42883): unknown signature: string_agg(string)

build
SELECT string_agg(s, 'x', 'y') FROM kv
----
error (42883): unknown signature: string_agg(string, string, string)

build
SELECT string_agg(DISTINCT s, 'separator') FROM kv
----
scalar-group-by
 ├── columns: string_agg:8
 ├── project
 │    ├── columns: column7:7!null s:4
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── 'separator' [as=column7:7]
 └── aggregations
      └── agg-distinct [as=string_agg:8]
           └── string-agg
                ├── s:4
                └── column7:7

build
SELECT max(s), string_agg(s, 'sep1'), string_agg(s, 'sep2'), min(s) FROM kv
----
scalar-group-by
 ├── columns: max:7 string_agg:9 string_agg:11 min:12
 ├── project
 │    ├── columns: column8:8!null column10:10!null s:4
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         ├── 'sep1' [as=column8:8]
 │         └── 'sep2' [as=column10:10]
 └── aggregations
      ├── max [as=max:7]
      │    └── s:4
      ├── string-agg [as=string_agg:9]
      │    ├── s:4
      │    └── column8:8
      ├── string-agg [as=string_agg:11]
      │    ├── s:4
      │    └── column10:10
      └── min [as=min:12]
           └── s:4

# The separator must be constant, but it need not be a literal - any constant (as
# determined by tree.IsConst) is valid.
build
SELECT string_agg(s, 'abc' || 'xyz') FROM kv
----
scalar-group-by
 ├── columns: string_agg:8
 ├── project
 │    ├── columns: column7:7!null s:4
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── 'abc' || 'xyz' [as=column7:7]
 └── aggregations
      └── string-agg [as=string_agg:8]
           ├── s:4
           └── column7:7

build
SELECT string_agg(s, NULL) FROM kv
----
scalar-group-by
 ├── columns: string_agg:8
 ├── project
 │    ├── columns: column7:7 s:4
 │    ├── scan kv
 │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── projections
 │         └── NULL [as=column7:7]
 └── aggregations
      └── string-agg [as=string_agg:8]
           ├── s:4
           └── column7:7

build
SELECT string_agg(s, s) FROM kv
----
scalar-group-by
 ├── columns: string_agg:7
 ├── project
 │    ├── columns: s:4
 │    └── scan kv
 │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── string-agg [as=string_agg:7]
           ├── s:4
           └── s:4

# Regression test for #26419
build
SELECT 123 r FROM kv ORDER BY max(v)
----
project
 ├── columns: r:8!null  [hidden: max:7]
 ├── ordering: +7
 ├── scalar-group-by
 │    ├── columns: max:7
 │    ├── project
 │    │    ├── columns: v:2
 │    │    └── scan kv
 │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         └── max [as=max:7]
 │              └── v:2
 └── projections
      └── 123 [as=r:8]

# Check that ordering columns are projected correctly.
build
SELECT array_agg(y) FROM (SELECT * FROM xyz ORDER BY x+y)
----
scalar-group-by
 ├── columns: array_agg:7
 ├── internal-ordering: +6
 ├── sort
 │    ├── columns: y:2 column6:6
 │    ├── ordering: +6
 │    └── project
 │         ├── columns: y:2 column6:6
 │         └── project
 │              ├── columns: column6:6 x:1!null y:2 z:3
 │              ├── scan xyz
 │              │    └── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │              └── projections
 │                   └── x:1 + y:2 [as=column6:6]
 └── aggregations
      └── array-agg [as=array_agg:7]
           └── y:2

build
SELECT array_agg(y) FROM (SELECT * FROM xyz ORDER BY x DESC)
----
scalar-group-by
 ├── columns: array_agg:6
 ├── internal-ordering: -1
 ├── project
 │    ├── columns: x:1!null y:2
 │    ├── ordering: -1
 │    └── project
 │         ├── columns: x:1!null y:2 z:3
 │         ├── ordering: -1
 │         └── scan xyz,rev
 │              ├── columns: x:1!null y:2 z:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │              └── ordering: -1
 └── aggregations
      └── array-agg [as=array_agg:6]
           └── y:2

# Regression test for #30166.
build
SELECT array_agg(generate_series(1, 2))
----
error (0A000): array_agg(): set-returning functions are not allowed in aggregate

# Regression test for #37317: duplicate column in GROUP BY
build format=show-all
SELECT
*
FROM
(
 SELECT
 x AS firstCol,
 y AS secondCol,
 y AS thirdCol
 FROM xyz
)
GROUP BY
firstCol, secondCol, thirdCol;
----
group-by (hash)
 ├── columns: firstcol:1(int!null) secondcol:2(int) thirdcol:2(int)
 ├── grouping columns: t.public.xyz.x:1(int!null) t.public.xyz.y:2(int)
 ├── stats: [rows=1000, distinct(1,2)=1000, null(1,2)=0]
 ├── cost: 1159.01625
 ├── key: (1)
 ├── fd: (1)-->(2)
 ├── interesting orderings: (+1)
 └── project
      ├── columns: t.public.xyz.x:1(int!null) t.public.xyz.y:2(int)
      ├── stats: [rows=1000, distinct(1,2)=1000, null(1,2)=0]
      ├── cost: 1118.84
      ├── key: (1)
      ├── fd: (1)-->(2)
      ├── prune: (1,2)
      ├── interesting orderings: (+1)
      └── scan t.public.xyz
           ├── columns: t.public.xyz.x:1(int!null) t.public.xyz.y:2(int) t.public.xyz.z:3(float) t.public.xyz.crdb_internal_mvcc_timestamp:4(decimal) t.public.xyz.tableoid:5(oid)
           ├── stats: [rows=1000, distinct(1,2)=1000, null(1,2)=0]
           ├── cost: 1108.82
           ├── key: (1)
           ├── fd: (1)-->(2-5)
           ├── prune: (1-5)
           └── interesting orderings: (+1) (+3,+2,+1)

# Testing ordered aggregations.
exec-ddl
CREATE TABLE tab (col1 int NOT NULL, col2 int NOT NULL, col3 string)
----

build
SELECT array_agg(col1 ORDER BY col1) FROM tab
----
scalar-group-by
 ├── columns: array_agg:7
 ├── window partition=() ordering=+1
 │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7
 │    ├── scan tab
 │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── windows
 │         └── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
 │              └── col1:1
 └── aggregations
      └── const-agg [as=array_agg:7]
           └── array_agg:7

# Ignore aggregate orderings for non commutative aggregates.
build
SELECT count(col1 ORDER BY col2) FROM tab
----
scalar-group-by
 ├── columns: count:7!null
 ├── project
 │    ├── columns: col1:1!null col2:2!null
 │    └── scan tab
 │         └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 └── aggregations
      └── count [as=count:7]
           └── col1:1

# Multiple ordered aggregations.
build
SELECT array_agg(col1 ORDER BY col1), array_agg(col1 ORDER BY col2) from tab
----
scalar-group-by
 ├── columns: array_agg:7 array_agg:8
 ├── window partition=() ordering=+2
 │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7 array_agg:8
 │    ├── window partition=() ordering=+1
 │    │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7
 │    │    ├── scan tab
 │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── windows
 │    │         └── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
 │    │              └── col1:1
 │    └── windows
 │         └── array-agg [as=array_agg:8, frame="range from unbounded to unbounded"]
 │              └── col1:1
 └── aggregations
      ├── const-agg [as=array_agg:7]
      │    └── array_agg:7
      └── const-agg [as=array_agg:8]
           └── array_agg:8

build
SELECT concat_agg(col3 ORDER BY col1), array_agg(col1) FROM tab
----
scalar-group-by
 ├── columns: concat_agg:7 array_agg:8
 ├── window partition=()
 │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 concat_agg:7 array_agg:8
 │    ├── window partition=() ordering=+1
 │    │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 concat_agg:7
 │    │    ├── scan tab
 │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── windows
 │    │         └── concat-agg [as=concat_agg:7, frame="range from unbounded to unbounded"]
 │    │              └── col3:3
 │    └── windows
 │         └── array-agg [as=array_agg:8, frame="range from unbounded to unbounded"]
 │              └── col1:1
 └── aggregations
      ├── const-agg [as=concat_agg:7]
      │    └── concat_agg:7
      └── const-agg [as=array_agg:8]
           └── array_agg:8

build
SELECT concat_agg(col3 ORDER BY col1), sum(col1 ORDER BY col2) FROM tab
----
scalar-group-by
 ├── columns: concat_agg:7 sum:8
 ├── window partition=()
 │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 concat_agg:7 sum:8
 │    ├── window partition=() ordering=+1
 │    │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 concat_agg:7
 │    │    ├── scan tab
 │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── windows
 │    │         └── concat-agg [as=concat_agg:7, frame="range from unbounded to unbounded"]
 │    │              └── col3:3
 │    └── windows
 │         └── sum [as=sum:8, frame="range from unbounded to unbounded"]
 │              └── col1:1
 └── aggregations
      ├── const-agg [as=concat_agg:7]
      │    └── concat_agg:7
      └── const-agg [as=sum:8]
           └── sum:8

build
SELECT array_agg(col1 ORDER BY col1) FROM tab GROUP BY col2
----
project
 ├── columns: array_agg:7
 └── group-by (hash)
      ├── columns: col2:2!null array_agg:7
      ├── grouping columns: col2:2!null
      ├── window partition=(2) ordering=+1
      │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7
      │    ├── scan tab
      │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── windows
      │         └── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
      │              └── col1:1
      └── aggregations
           └── const-agg [as=array_agg:7]
                └── array_agg:7

build
SELECT array_agg(col1 ORDER BY col1), array_agg(col3 ORDER BY col1) FROM tab GROUP BY col2
----
project
 ├── columns: array_agg:7 array_agg:8
 └── group-by (hash)
      ├── columns: col2:2!null array_agg:7 array_agg:8
      ├── grouping columns: col2:2!null
      ├── window partition=(2) ordering=+1
      │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7 array_agg:8
      │    ├── scan tab
      │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── windows
      │         ├── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
      │         │    └── col1:1
      │         └── array-agg [as=array_agg:8, frame="range from unbounded to unbounded"]
      │              └── col3:3
      └── aggregations
           ├── const-agg [as=array_agg:7]
           │    └── array_agg:7
           └── const-agg [as=array_agg:8]
                └── array_agg:8

build
SELECT array_agg(col1 ORDER BY col1), array_agg(col3 ORDER BY col1) FROM tab GROUP BY col2 HAVING col2 > 1
----
project
 ├── columns: array_agg:7 array_agg:8
 └── select
      ├── columns: col2:2!null array_agg:7 array_agg:8
      ├── group-by (hash)
      │    ├── columns: col2:2!null array_agg:7 array_agg:8
      │    ├── grouping columns: col2:2!null
      │    ├── window partition=(2) ordering=+1
      │    │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7 array_agg:8
      │    │    ├── scan tab
      │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    │    └── windows
      │    │         ├── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
      │    │         │    └── col1:1
      │    │         └── array-agg [as=array_agg:8, frame="range from unbounded to unbounded"]
      │    │              └── col3:3
      │    └── aggregations
      │         ├── const-agg [as=array_agg:7]
      │         │    └── array_agg:7
      │         └── const-agg [as=array_agg:8]
      │              └── array_agg:8
      └── filters
           └── col2:2 > 1

# Add projection on top to ensure the default NULL values are set correctly.
# Regression test for #57496: Ensure that non-input columns are not passed
# through by the Project.
build
SELECT count(DISTINCT col1), count(*), array_agg(col1 ORDER BY col2) FROM tab
----
project
 ├── columns: count:7 count:8 array_agg:9
 └── project
      ├── columns: count:7 count_rows:8 array_agg:9
      ├── scalar-group-by
      │    ├── columns: array_agg:9 count:10 count_rows:11
      │    ├── window partition=() ordering=+2
      │    │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 count:7 count_rows:8 array_agg:9
      │    │    ├── window partition=()
      │    │    │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 count:7 count_rows:8
      │    │    │    ├── scan tab
      │    │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    │    │    └── windows
      │    │    │         ├── count [as=count:7, frame="range from unbounded to unbounded"]
      │    │    │         │    └── col1:1
      │    │    │         └── count-rows [as=count_rows:8, frame="range from unbounded to unbounded"]
      │    │    └── windows
      │    │         └── array-agg [as=array_agg:9, frame="range from unbounded to unbounded"]
      │    │              └── col1:1
      │    └── aggregations
      │         ├── const-agg [as=count:10]
      │         │    └── count:7
      │         ├── const-agg [as=count_rows:11]
      │         │    └── count_rows:8
      │         └── const-agg [as=array_agg:9]
      │              └── array_agg:9
      └── projections
           ├── CASE WHEN count:10 IS NULL THEN 0 ELSE count:10 END [as=count:7]
           └── CASE WHEN count_rows:11 IS NULL THEN 0 ELSE count_rows:11 END [as=count_rows:8]

# Testing aggregations as window when group by has a projection.
build
SELECT array_agg(col1 ORDER BY col1) FROM tab GROUP BY upper(col3)
----
project
 ├── columns: array_agg:7
 └── group-by (hash)
      ├── columns: array_agg:7 column8:8
      ├── grouping columns: column8:8
      ├── window partition=(8) ordering=+1
      │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7 column8:8
      │    ├── project
      │    │    ├── columns: column8:8 col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    │    ├── scan tab
      │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    │    └── projections
      │    │         └── upper(col3:3) [as=column8:8]
      │    └── windows
      │         └── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
      │              └── col1:1
      └── aggregations
           └── const-agg [as=array_agg:7]
                └── array_agg:7

build
SELECT array_agg(col1 ORDER BY col1), upper(col3) FROM tab GROUP BY upper(col3)
----
group-by (hash)
 ├── columns: array_agg:7 upper:8
 ├── grouping columns: column8:8
 ├── window partition=(8) ordering=+1
 │    ├── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6 array_agg:7 column8:8
 │    ├── project
 │    │    ├── columns: column8:8 col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan tab
 │    │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── upper(col3:3) [as=column8:8]
 │    └── windows
 │         └── array-agg [as=array_agg:7, frame="range from unbounded to unbounded"]
 │              └── col1:1
 └── aggregations
      └── const-agg [as=array_agg:7]
           └── array_agg:7

build
SELECT array_agg(lower(col3)) FROM tab GROUP BY upper(col3)
----
project
 ├── columns: array_agg:8
 └── group-by (hash)
      ├── columns: array_agg:8 column9:9
      ├── grouping columns: column9:9
      ├── project
      │    ├── columns: column7:7 column9:9
      │    ├── scan tab
      │    │    └── columns: col1:1!null col2:2!null col3:3 rowid:4!null crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         ├── lower(col3:3) [as=column7:7]
      │         └── upper(col3:3) [as=column9:9]
      └── aggregations
           └── array-agg [as=array_agg:8]
                └── column7:7

build
SELECT array_agg(v+w ORDER BY w) FROM kv
----
scalar-group-by
 ├── columns: array_agg:8
 ├── window partition=() ordering=+3
 │    ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6 column7:7 array_agg:8
 │    ├── project
 │    │    ├── columns: column7:7 k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan kv
 │    │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── v:2 + w:3 [as=column7:7]
 │    └── windows
 │         └── array-agg [as=array_agg:8, frame="range from unbounded to unbounded"]
 │              └── column7:7
 └── aggregations
      └── const-agg [as=array_agg:8]
           └── array_agg:8

build
SELECT array_agg(v ORDER BY v+w) FROM kv
----
scalar-group-by
 ├── columns: array_agg:8
 ├── window partition=() ordering=+7
 │    ├── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6 column7:7 array_agg:8
 │    ├── project
 │    │    ├── columns: column7:7 k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan kv
 │    │    │    └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── v:2 + w:3 [as=column7:7]
 │    └── windows
 │         └── array-agg [as=array_agg:8, frame="range from unbounded to unbounded"]
 │              └── v:2
 └── aggregations
      └── const-agg [as=array_agg:8]
           └── array_agg:8

build
SELECT array_agg(a ORDER BY b) FROM (SELECT 1 AS a, 2 AS b)
----
scalar-group-by
 ├── columns: array_agg:3
 ├── window partition=() ordering=+2
 │    ├── columns: a:1!null b:2!null array_agg:3
 │    ├── project
 │    │    ├── columns: a:1!null b:2!null
 │    │    ├── values
 │    │    │    └── ()
 │    │    └── projections
 │    │         ├── 1 [as=a:1]
 │    │         └── 2 [as=b:2]
 │    └── windows
 │         └── array-agg [as=array_agg:3, frame="range from unbounded to unbounded"]
 │              └── a:1
 └── aggregations
      └── const-agg [as=array_agg:3]
           └── array_agg:3

# Regression test for #38551.
build
SELECT * FROM ROWS FROM (count(json_each('[]')))
----
error (0A000): count(): json_each(): set-returning functions are not allowed in aggregate

# Tests for projecting non-grouping columns when we group by a PK.
build
SELECT v FROM kv GROUP BY k
----
project
 ├── columns: v:2
 └── group-by (hash)
      ├── columns: k:1!null v:2
      ├── grouping columns: k:1!null v:2
      └── project
           ├── columns: k:1!null v:2
           └── scan kv
                └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6

# This should be equivalent to the query above.
build
SELECT v FROM kv GROUP BY k, v
----
project
 ├── columns: v:2
 └── group-by (hash)
      ├── columns: k:1!null v:2
      ├── grouping columns: k:1!null v:2
      └── project
           ├── columns: k:1!null v:2
           └── scan kv
                └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT count(*), k+v FROM kv GROUP BY k
----
project
 ├── columns: count:7!null "?column?":8
 ├── group-by (hash)
 │    ├── columns: k:1!null v:2 count_rows:7!null
 │    ├── grouping columns: k:1!null v:2
 │    ├── project
 │    │    ├── columns: k:1!null v:2
 │    │    └── scan kv
 │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    └── aggregations
 │         └── count-rows [as=count_rows:7]
 └── projections
      └── k:1 + v:2 [as="?column?":8]

build
SELECT count(*) FROM kv GROUP BY k HAVING v=1
----
project
 ├── columns: count:7!null
 └── select
      ├── columns: k:1!null v:2!null count_rows:7!null
      ├── group-by (hash)
      │    ├── columns: k:1!null v:2 count_rows:7!null
      │    ├── grouping columns: k:1!null v:2
      │    ├── project
      │    │    ├── columns: k:1!null v:2
      │    │    └── scan kv
      │    │         └── columns: k:1!null v:2 w:3 s:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── aggregations
      │         └── count-rows [as=count_rows:7]
      └── filters
           └── v:2 = 1

build
SELECT k, v, count(*) FROM kv JOIN ab ON a=k GROUP BY k
----
group-by (hash)
 ├── columns: k:1!null v:2 count:11!null
 ├── grouping columns: k:1!null v:2
 ├── project
 │    ├── columns: k:1!null v:2
 │    └── inner-join (hash)
 │         ├── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6 a:7!null b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
 │         ├── scan kv
 │         │    └── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6
 │         ├── scan ab
 │         │    └── columns: a:7!null b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
 │         └── filters
 │              └── a:7 = k:1
 └── aggregations
      └── count-rows [as=count_rows:11]

# Not allowed when grouping on a subset of the PK.
build
SELECT x, y FROM abxy GROUP BY a
----
error (42803): column "x" must appear in the GROUP BY clause or be used in an aggregate function

build
SELECT x, y FROM abxy GROUP BY a, b
----
project
 ├── columns: x:3 y:4
 └── group-by (hash)
      ├── columns: a:1!null b:2!null x:3 y:4
      ├── grouping columns: a:1!null b:2!null x:3 y:4
      └── project
           ├── columns: a:1!null b:2!null x:3 y:4
           └── scan abxy
                └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6

# The following two should be equivalent to the one above.
build
SELECT x, y FROM abxy GROUP BY x, a, b
----
project
 ├── columns: x:3 y:4
 └── group-by (hash)
      ├── columns: a:1!null b:2!null x:3 y:4
      ├── grouping columns: a:1!null b:2!null x:3 y:4
      └── project
           ├── columns: a:1!null b:2!null x:3 y:4
           └── scan abxy
                └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT x, y FROM abxy GROUP BY x, y, a, b
----
project
 ├── columns: x:3 y:4
 └── group-by (hash)
      ├── columns: a:1!null b:2!null x:3 y:4
      ├── grouping columns: a:1!null b:2!null x:3 y:4
      └── project
           ├── columns: a:1!null b:2!null x:3 y:4
           └── scan abxy
                └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT x, y FROM abxy NATURAL JOIN ab GROUP BY a, b
----
project
 ├── columns: x:3 y:4
 └── group-by (hash)
      ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4
      ├── grouping columns: abxy.a:1!null abxy.b:2!null x:3 y:4
      └── project
           ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4
           └── inner-join (hash)
                ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6 ab.a:7!null ab.b:8!null ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                ├── scan abxy
                │    └── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6
                ├── scan ab
                │    └── columns: ab.a:7!null ab.b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                └── filters
                     ├── abxy.a:1 = ab.a:7
                     └── abxy.b:2 = ab.b:8

# Should be equivalent to the one above.
build
SELECT x, y FROM abxy NATURAL JOIN ab GROUP BY a, b, x
----
project
 ├── columns: x:3 y:4
 └── group-by (hash)
      ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4
      ├── grouping columns: abxy.a:1!null abxy.b:2!null x:3 y:4
      └── project
           ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4
           └── inner-join (hash)
                ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6 ab.a:7!null ab.b:8!null ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                ├── scan abxy
                │    └── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6
                ├── scan ab
                │    └── columns: ab.a:7!null ab.b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                └── filters
                     ├── abxy.a:1 = ab.a:7
                     └── abxy.b:2 = ab.b:8

build
SELECT abxy.*, ab.* FROM abxy, ab GROUP BY abxy.a, abxy.b, ab.a
----
group-by (hash)
 ├── columns: a:1!null b:2!null x:3 y:4 a:7!null b:8
 ├── grouping columns: abxy.a:1!null abxy.b:2!null x:3 y:4 ab.a:7!null ab.b:8
 └── project
      ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 ab.a:7!null ab.b:8
      └── inner-join (cross)
           ├── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6 ab.a:7!null ab.b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
           ├── scan abxy
           │    └── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6
           ├── scan ab
           │    └── columns: ab.a:7!null ab.b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
           └── filters (true)

# Not allowed with UNION.
build
SELECT x FROM (SELECT a, b, x FROM abxy UNION SELECT a, b, 1 FROM ab) GROUP BY a,b
----
error (42803): column "x" must appear in the GROUP BY clause or be used in an aggregate function

# Allowed with EXCEPT.
build
SELECT x FROM (SELECT a, b, x FROM abxy EXCEPT SELECT a, b, 1 FROM ab) GROUP BY a,b
----
project
 ├── columns: x:3
 └── group-by (hash)
      ├── columns: abxy.a:1!null abxy.b:2 x:3
      ├── grouping columns: abxy.a:1!null abxy.b:2 x:3
      └── except
           ├── columns: abxy.a:1!null abxy.b:2 x:3
           ├── left columns: abxy.a:1!null abxy.b:2 x:3
           ├── right columns: ab.a:7 ab.b:8 "?column?":11
           ├── project
           │    ├── columns: abxy.a:1!null abxy.b:2!null x:3
           │    └── scan abxy
           │         └── columns: abxy.a:1!null abxy.b:2!null x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6
           └── project
                ├── columns: "?column?":11!null ab.a:7!null ab.b:8
                ├── scan ab
                │    └── columns: ab.a:7!null ab.b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                └── projections
                     └── 1 [as="?column?":11]

# Allowed even with outer joins. It's a little subtle why this is correct: the
# PK columns are also non-nullable so any "outer" rows are never in the same
# group with "non-outer" rows.
build
SELECT v, w FROM kv FULL JOIN ab ON k=a GROUP BY k
----
project
 ├── columns: v:2 w:3
 └── group-by (hash)
      ├── columns: k:1 v:2 w:3
      ├── grouping columns: k:1 v:2 w:3
      └── project
           ├── columns: k:1 v:2 w:3
           └── full-join (hash)
                ├── columns: k:1 v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6 a:7 b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                ├── scan kv
                │    └── columns: k:1!null v:2 w:3 s:4 kv.crdb_internal_mvcc_timestamp:5 kv.tableoid:6
                ├── scan ab
                │    └── columns: a:7!null b:8 ab.crdb_internal_mvcc_timestamp:9 ab.tableoid:10
                └── filters
                     └── k:1 = a:7

# Verify that we handle tables with no primary index (#44659).
build
SELECT table_schema FROM information_schema.columns GROUP BY table_name
----
error (42803): column "table_schema" must appear in the GROUP BY clause or be used in an aggregate function

# Tests with aliases (see #28059).
build
SELECT x + 1 AS z FROM abxy GROUP BY z
----
group-by (hash)
 ├── columns: z:7
 ├── grouping columns: z:7
 └── project
      ├── columns: z:7
      ├── scan abxy
      │    └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── x:3 + 1 [as=z:7]

# The FROM column has precedence, we should be grouping by abxy.x, not by x%10.
build
SELECT (x % 10) AS x FROM abxy GROUP BY x
----
project
 ├── columns: x:7
 ├── group-by (hash)
 │    ├── columns: abxy.x:3
 │    ├── grouping columns: abxy.x:3
 │    └── project
 │         ├── columns: abxy.x:3
 │         └── scan abxy
 │              └── columns: a:1!null b:2!null abxy.x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── projections
      └── abxy.x:3 % 10 [as=x:7]

# But aliases have precedence over columns from higher scopes. Here we are
# grouping by v, not by the outer x.
build
SELECT x, (SELECT v AS x FROM kv GROUP BY x) FROM abxy
----
project
 ├── columns: x:3 x:13
 ├── scan abxy
 │    └── columns: a:1!null b:2!null abxy.x:3 y:4 abxy.crdb_internal_mvcc_timestamp:5 abxy.tableoid:6
 └── projections
      └── subquery [as=x:13]
           └── max1-row
                ├── columns: v:8
                └── group-by (hash)
                     ├── columns: v:8
                     ├── grouping columns: v:8
                     └── project
                          ├── columns: v:8
                          └── scan kv
                               └── columns: k:7!null v:8 w:9 s:10 kv.crdb_internal_mvcc_timestamp:11 kv.tableoid:12

build
SELECT sum(x) AS u FROM abxy GROUP BY u
----
error (42803): sum(): aggregate functions are not allowed in GROUP BY

# Implicit aliases should work too.
build
SELECT x + 1 FROM abxy GROUP BY "?column?"
----
group-by (hash)
 ├── columns: "?column?":7
 ├── grouping columns: "?column?":7
 └── project
      ├── columns: "?column?":7
      ├── scan abxy
      │    └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── x:3 + 1 [as="?column?":7]

build
SELECT sum(x) FROM abxy GROUP BY sum
----
error (42803): sum(): aggregate functions are not allowed in GROUP BY

# Ambiguous aliases should error out.
build
SELECT (x + 1) AS u, (y + 1) AS u FROM abxy GROUP BY u
----
error (42702): GROUP BY "u" is ambiguous

# In this case we would have had an outer column if it wasn't for the aliases;
# this should error out just the same.
build
SELECT x, (SELECT v AS x, w AS x FROM kv GROUP BY x) FROM abxy
----
error (42702): GROUP BY "x" is ambiguous

# Duplicate expressions with the same alias are not ambiguous.
build
SELECT (x + 1) AS u, (x + 1) AS u FROM abxy GROUP BY u
----
group-by (hash)
 ├── columns: u:7 u:7
 ├── grouping columns: u:7
 └── project
      ├── columns: u:7
      ├── scan abxy
      │    └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── x:3 + 1 [as=u:7]

build
SELECT (x + 1) AS u, (x + 1) AS u, (y + 1) AS u FROM abxy GROUP BY u
----
error (42702): GROUP BY "u" is ambiguous

# In this case, the FROM column has precedence.
build
SELECT sum(x + 1) AS x, sum(y + 1) AS x FROM abxy GROUP BY x
----
project
 ├── columns: x:8 x:10
 └── group-by (hash)
      ├── columns: x:3 sum:8 sum:10
      ├── grouping columns: x:3
      ├── project
      │    ├── columns: column7:7 column9:9 x:3
      │    ├── scan abxy
      │    │    └── columns: a:1!null b:2!null x:3 y:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      │    └── projections
      │         ├── x:3 + 1 [as=column7:7]
      │         └── y:4 + 1 [as=column9:9]
      └── aggregations
           ├── sum [as=sum:8]
           │    └── column7:7
           └── sum [as=sum:10]
                └── column9:9

# Regression test for #44724.
build
SELECT *
  FROM (SELECT 1 AS one, v FROM kv) AS kv
  JOIN LATERAL (
        SELECT b, sum(one) FROM abxy
       ) AS abxy ON kv.v = abxy.b
----
error (42803): aggregate functions are not allowed in FROM clause of their own query level

# Regression test for #45838. The aggregate should be allowed in the WHERE
# clause since it's scoped at the outer level.
build
  SELECT sum(x)
    FROM abxy AS t
GROUP BY y
  HAVING EXISTS(SELECT 1 FROM abxy AS t2 WHERE sum(t.x) = 1)
----
project
 ├── columns: sum:7
 └── select
      ├── columns: t.y:4 sum:7
      ├── group-by (hash)
      │    ├── columns: t.y:4 sum:7
      │    ├── grouping columns: t.y:4
      │    ├── project
      │    │    ├── columns: t.x:3 t.y:4
      │    │    └── scan abxy [as=t]
      │    │         └── columns: t.a:1!null t.b:2!null t.x:3 t.y:4 t.crdb_internal_mvcc_timestamp:5 t.tableoid:6
      │    └── aggregations
      │         └── sum [as=sum:7]
      │              └── t.x:3
      └── filters
           └── exists
                └── project
                     ├── columns: "?column?":15!null
                     ├── select
                     │    ├── columns: t2.a:8!null t2.b:9!null t2.x:10 t2.y:11 t2.crdb_internal_mvcc_timestamp:12 t2.tableoid:13
                     │    ├── scan abxy [as=t2]
                     │    │    └── columns: t2.a:8!null t2.b:9!null t2.x:10 t2.y:11 t2.crdb_internal_mvcc_timestamp:12 t2.tableoid:13
                     │    └── filters
                     │         └── sum:7 = 1
                     └── projections
                          └── 1 [as="?column?":15]

exec-ddl
CREATE TABLE onek (
        unique1         int,
        unique2         int,
        two                     int,
        four            int,
        ten                     int,
        twenty          int,
        hundred         int,
        thousand        int,
        twothousand     int,
        fivethous       int,
        tenthous        int,
        odd                     int,
        even            int,
        stringu1        string,
        stringu2        string,
        string4         string
)
----

# Regression tests for #30652.
build
  SELECT ten, sum(DISTINCT four)
    FROM onek AS a
GROUP BY ten
  HAVING EXISTS(
            SELECT 1 FROM onek AS b WHERE sum(DISTINCT a.four) = b.four
         )
----
select
 ├── columns: ten:5 sum:20
 ├── group-by (hash)
 │    ├── columns: a.ten:5 sum:20
 │    ├── grouping columns: a.ten:5
 │    ├── project
 │    │    ├── columns: a.four:4 a.ten:5
 │    │    └── scan onek [as=a]
 │    │         └── columns: a.unique1:1 a.unique2:2 a.two:3 a.four:4 a.ten:5 a.twenty:6 a.hundred:7 a.thousand:8 a.twothousand:9 a.fivethous:10 a.tenthous:11 a.odd:12 a.even:13 a.stringu1:14 a.stringu2:15 a.string4:16 a.rowid:17!null a.crdb_internal_mvcc_timestamp:18 a.tableoid:19
 │    └── aggregations
 │         └── agg-distinct [as=sum:20]
 │              └── sum
 │                   └── a.four:4
 └── filters
      └── exists
           └── project
                ├── columns: "?column?":41!null
                ├── select
                │    ├── columns: b.unique1:21 b.unique2:22 b.two:23 b.four:24!null b.ten:25 b.twenty:26 b.hundred:27 b.thousand:28 b.twothousand:29 b.fivethous:30 b.tenthous:31 b.odd:32 b.even:33 b.stringu1:34 b.stringu2:35 b.string4:36 b.rowid:37!null b.crdb_internal_mvcc_timestamp:38 b.tableoid:39
                │    ├── scan onek [as=b]
                │    │    └── columns: b.unique1:21 b.unique2:22 b.two:23 b.four:24 b.ten:25 b.twenty:26 b.hundred:27 b.thousand:28 b.twothousand:29 b.fivethous:30 b.tenthous:31 b.odd:32 b.even:33 b.stringu1:34 b.stringu2:35 b.string4:36 b.rowid:37!null b.crdb_internal_mvcc_timestamp:38 b.tableoid:39
                │    └── filters
                │         └── sum:20 = b.four:24
                └── projections
                     └── 1 [as="?column?":41]

build
  SELECT ten, sum(DISTINCT four)
    FROM onek AS a
GROUP BY ten
  HAVING EXISTS(
            SELECT 1
              FROM onek AS b
             WHERE sum(DISTINCT a.four + b.four) = b.four
         )
----
error (42803): aggregate functions are not allowed in WHERE

build
SELECT (
        SELECT t2.a
          FROM abxy AS t2 JOIN abxy AS t3 ON sum(t1.x) = t3.x
       )
  FROM abxy AS t1
----
project
 ├── columns: a:21
 ├── scalar-group-by
 │    ├── columns: sum:20
 │    ├── project
 │    │    ├── columns: x:19
 │    │    ├── scan abxy [as=t1]
 │    │    │    └── columns: t1.a:1!null t1.b:2!null t1.x:3 t1.y:4 t1.crdb_internal_mvcc_timestamp:5 t1.tableoid:6
 │    │    └── projections
 │    │         └── t1.x:3 [as=x:19]
 │    └── aggregations
 │         └── sum [as=sum:20]
 │              └── x:19
 └── projections
      └── subquery [as=a:21]
           └── max1-row
                ├── columns: t2.a:7!null
                └── project
                     ├── columns: t2.a:7!null
                     └── inner-join (cross)
                          ├── columns: t2.a:7!null t2.b:8!null t2.x:9 t2.y:10 t2.crdb_internal_mvcc_timestamp:11 t2.tableoid:12 t3.a:13!null t3.b:14!null t3.x:15!null t3.y:16 t3.crdb_internal_mvcc_timestamp:17 t3.tableoid:18
                          ├── scan abxy [as=t2]
                          │    └── columns: t2.a:7!null t2.b:8!null t2.x:9 t2.y:10 t2.crdb_internal_mvcc_timestamp:11 t2.tableoid:12
                          ├── scan abxy [as=t3]
                          │    └── columns: t3.a:13!null t3.b:14!null t3.x:15 t3.y:16 t3.crdb_internal_mvcc_timestamp:17 t3.tableoid:18
                          └── filters
                               └── sum:20 = t3.x:15

# Regression test for #45631.
build
SELECT
  (SELECT (max(b), unnest) FROM ab WHERE a = unnest)
FROM
  ROWS FROM (unnest(ARRAY[1, 2]))
----
project
 ├── columns: "?column?":8
 ├── project-set
 │    ├── columns: unnest:1
 │    ├── values
 │    │    └── ()
 │    └── zip
 │         └── unnest(ARRAY[1,2])
 └── projections
      └── subquery [as="?column?":8]
           └── max1-row
                ├── columns: "?column?":7
                └── project
                     ├── columns: "?column?":7
                     ├── scalar-group-by
                     │    ├── columns: max:6
                     │    ├── project
                     │    │    ├── columns: b:3
                     │    │    └── select
                     │    │         ├── columns: a:2!null b:3 crdb_internal_mvcc_timestamp:4 tableoid:5
                     │    │         ├── scan ab
                     │    │         │    └── columns: a:2!null b:3 crdb_internal_mvcc_timestamp:4 tableoid:5
                     │    │         └── filters
                     │    │              └── a:2 = unnest:1
                     │    └── aggregations
                     │         └── max [as=max:6]
                     │              └── b:3
                     └── projections
                          └── (max:6, unnest:1) [as="?column?":7]

# Regression test for #46196. Don't eliminate the scalar group by, and
# default to type string.
build format=show-types
SELECT max(t0.c0) FROM (VALUES (NULL), (NULL)) t0(c0);
----
scalar-group-by
 ├── columns: max:3(string)
 ├── project
 │    ├── columns: column2:2(string)
 │    ├── values
 │    │    ├── columns: column1:1(unknown)
 │    │    ├── (NULL,) [type=tuple{unknown}]
 │    │    └── (NULL,) [type=tuple{unknown}]
 │    └── projections
 │         └── column1:1::STRING [as=column2:2, type=string]
 └── aggregations
      └── max [as=max:3, type=string]
           └── column2:2 [type=string]

build
SELECT percentile_disc(0.95) WITHIN GROUP (ORDER BY b) FROM abc;
----
scalar-group-by
 ├── columns: percentile_disc:8
 ├── window partition=() ordering=+2
 │    ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6 column7:7!null percentile_disc:8
 │    ├── project
 │    │    ├── columns: column7:7!null a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan abc
 │    │    │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── 0.95 [as=column7:7]
 │    └── windows
 │         └── percentile-disc [as=percentile_disc:8, frame="range from unbounded to unbounded"]
 │              ├── column7:7
 │              └── b:2
 └── aggregations
      └── const-agg [as=percentile_disc:8]
           └── percentile_disc:8

build
SELECT percentile_cont(0.95) WITHIN GROUP (ORDER BY b) FROM abc;
----
scalar-group-by
 ├── columns: percentile_cont:8
 ├── window partition=() ordering=+2
 │    ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6 column7:7!null percentile_cont:8
 │    ├── project
 │    │    ├── columns: column7:7!null a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan abc
 │    │    │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── 0.95 [as=column7:7]
 │    └── windows
 │         └── percentile-cont [as=percentile_cont:8, frame="range from unbounded to unbounded"]
 │              ├── column7:7
 │              └── b:2
 └── aggregations
      └── const-agg [as=percentile_cont:8]
           └── percentile_cont:8

build
SELECT percentile_disc(ARRAY[0.90, 0.95]::float[]) WITHIN GROUP (ORDER BY b) FROM abc;
----
scalar-group-by
 ├── columns: percentile_disc:8
 ├── window partition=() ordering=+2
 │    ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6 column7:7!null percentile_disc:8
 │    ├── project
 │    │    ├── columns: column7:7!null a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan abc
 │    │    │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── ARRAY[0.9,0.95] [as=column7:7]
 │    └── windows
 │         └── percentile-disc [as=percentile_disc:8, frame="range from unbounded to unbounded"]
 │              ├── column7:7
 │              └── b:2
 └── aggregations
      └── const-agg [as=percentile_disc:8]
           └── percentile_disc:8

build
SELECT percentile_cont(ARRAY[0.90, 0.95]::float[]) WITHIN GROUP (ORDER BY b) FROM abc;
----
scalar-group-by
 ├── columns: percentile_cont:8
 ├── window partition=() ordering=+2
 │    ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6 column7:7!null percentile_cont:8
 │    ├── project
 │    │    ├── columns: column7:7!null a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    ├── scan abc
 │    │    │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │    │    └── projections
 │    │         └── ARRAY[0.9,0.95] [as=column7:7]
 │    └── windows
 │         └── percentile-cont [as=percentile_cont:8, frame="range from unbounded to unbounded"]
 │              ├── column7:7
 │              └── b:2
 └── aggregations
      └── const-agg [as=percentile_cont:8]
           └── percentile_cont:8

# Regression test for #46914.
exec-ddl
CREATE TABLE t0(c0 INT)
----

build format=(show-types,show-scalars)
SELECT * FROM t0 GROUP BY t0.c0 HAVING min((CASE WHEN false THEN NULL END):::FLOAT) IS NOT NAN
----
project
 ├── columns: c0:1(int)
 └── select
      ├── columns: c0:1(int) min:6(float!null)
      ├── group-by (hash)
      │    ├── columns: c0:1(int) min:6(float)
      │    ├── grouping columns: c0:1(int)
      │    ├── project
      │    │    ├── columns: column5:5(float) c0:1(int)
      │    │    ├── scan t0
      │    │    │    └── columns: c0:1(int) rowid:2(int!null) crdb_internal_mvcc_timestamp:3(decimal) tableoid:4(oid)
      │    │    └── projections
      │    │         └── case [as=column5:5, type=float]
      │    │              ├── true [type=bool]
      │    │              ├── when [type=float]
      │    │              │    ├── false [type=bool]
      │    │              │    └── cast: FLOAT8 [type=float]
      │    │              │         └── null [type=unknown]
      │    │              └── null [type=float]
      │    └── aggregations
      │         └── min [as=min:6, type=float]
      │              └── variable: column5:5 [type=float]
      └── filters
           └── ne [type=bool]
                ├── variable: min:6 [type=float]
                └── const: NaN [type=float]

# Regression test for #51877.
build
SELECT max((SELECT jsonb_agg(v))) FROM kv
----
error (42803): aggregate function calls cannot be nested

# Regression test for #57441.
build
SELECT v FROM kv UNION ALL SELECT count(1) FROM kv ORDER BY count(1)
----
error (42803): count(): aggregate functions are not allowed in ORDER BY

build
SELECT count(1) FROM kv UNION ALL SELECT v FROM kv ORDER BY count(1)
----
error (42803): count(): aggregate functions are not allowed in ORDER BY

# Regression test for #68290: the outer column should not become an implicit
# grouping column.
build
SELECT
  (
    SELECT
      CASE
      WHEN t.arr IS NULL THEN '[]'::JSONB
      ELSE json_agg(elem)
      END
    FROM
      unnest(t.arr) AS elem
  )
FROM
  (SELECT ARRAY[]::STRING[] AS arr) AS t
----
project
 ├── columns: json_agg:5
 ├── project
 │    ├── columns: arr:1!null
 │    ├── values
 │    │    └── ()
 │    └── projections
 │         └── ARRAY[] [as=arr:1]
 └── projections
      └── subquery [as=json_agg:5]
           └── max1-row
                ├── columns: json_agg:4
                └── project
                     ├── columns: json_agg:4
                     ├── scalar-group-by
                     │    ├── columns: json_agg:3
                     │    ├── project-set
                     │    │    ├── columns: unnest:2
                     │    │    ├── values
                     │    │    │    └── ()
                     │    │    └── zip
                     │    │         └── unnest(arr:1)
                     │    └── aggregations
                     │         └── json-agg [as=json_agg:3]
                     │              └── unnest:2
                     └── projections
                          └── CASE WHEN arr:1 IS NULL THEN '[]' ELSE json_agg:3 END [as=json_agg:4]

# Regression test for incorrectly ignoring NULLS LAST in aggregate functions (#91295).
exec-ddl
CREATE TABLE nulls_last_test (
    id INT NULL
)
----

build
SELECT array_agg(id ORDER BY id NULLS LAST) FROM nulls_last_test
----
scalar-group-by
 ├── columns: array_agg:6
 ├── window partition=() ordering=+5,+1
 │    ├── columns: id:1 rowid:2!null crdb_internal_mvcc_timestamp:3 tableoid:4 column5:5!null array_agg:6
 │    ├── project
 │    │    ├── columns: column5:5!null id:1 rowid:2!null crdb_internal_mvcc_timestamp:3 tableoid:4
 │    │    ├── scan nulls_last_test
 │    │    │    └── columns: id:1 rowid:2!null crdb_internal_mvcc_timestamp:3 tableoid:4
 │    │    └── projections
 │    │         └── id:1 IS NULL [as=column5:5]
 │    └── windows
 │         └── array-agg [as=array_agg:6, frame="range from unbounded to unbounded"]
 │              └── id:1
 └── aggregations
      └── const-agg [as=array_agg:6]
           └── array_agg:6
