# tests adapted from logictest -- order_by

exec-ddl
CREATE TABLE t (
  a INT PRIMARY KEY,
  b INT,
  c BOOLEAN
)
----
TABLE t
 ├── a int not null
 ├── b int
 ├── c bool
 └── INDEX primary
      └── a int not null

build
SELECT c FROM t ORDER BY c
----
sort
 ├── columns: c:3(bool)
 ├── ordering: +3
 └── project
      ├── columns: c:3(bool)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT c FROM t ORDER BY c DESC
----
sort
 ├── columns: c:3(bool)
 ├── ordering: -3
 └── project
      ├── columns: c:3(bool)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT a, b FROM t ORDER BY b
----
sort
 ├── columns: a:1(int!null) b:2(int)
 ├── ordering: +2
 └── project
      ├── columns: a:1(int!null) b:2(int)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT a, b FROM t ORDER BY b DESC
----
sort
 ├── columns: a:1(int!null) b:2(int)
 ├── ordering: -2
 └── project
      ├── columns: a:1(int!null) b:2(int)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT a, b FROM t ORDER BY b DESC LIMIT 2
----
limit
 ├── columns: a:1(int!null) b:2(int)
 ├── internal-ordering: -2
 ├── ordering: -2
 ├── sort
 │    ├── columns: a:1(int!null) b:2(int)
 │    ├── ordering: -2
 │    └── project
 │         ├── columns: a:1(int!null) b:2(int)
 │         └── scan t
 │              └── columns: a:1(int!null) b:2(int) c:3(bool)
 └── const: 2 [type=int]

build
SELECT a FROM t ORDER BY 1 DESC
----
project
 ├── columns: a:1(int!null)
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: -1

# This query causes an error in Postgres, and the optimizer has followed
# that lead. However, it is supported by the heuristic planner in CockroachDB
# with the semantics:
#   SELECT c FROM t GROUP BY c ORDER BY max(b) DESC;
build
SELECT DISTINCT c FROM t ORDER BY b DESC
----
error (42P10): for SELECT DISTINCT, ORDER BY expressions must appear in select list

build
SELECT a AS foo, b FROM t ORDER BY foo DESC
----
project
 ├── columns: foo:1(int!null) b:2(int)
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: -1

# Check that ambiguous references to renders are properly reported.
build
SELECT a AS foo, b AS foo FROM t ORDER BY foo
----
error (42P09): ORDER BY "foo" is ambiguous

# Check that no ambiguity is reported if the ORDER BY name refers
# to two or more equivalent renders (special case in SQL92).
build
SELECT a AS foo, (a) AS foo FROM t ORDER BY foo LIMIT 1
----
limit
 ├── columns: foo:1(int!null) foo:1(int!null)
 ├── internal-ordering: +1
 ├── ordering: +1
 ├── project
 │    ├── columns: a:1(int!null)
 │    ├── ordering: +1
 │    └── scan t
 │         ├── columns: a:1(int!null) b:2(int) c:3(bool)
 │         └── ordering: +1
 └── const: 1 [type=int]

# Check that this orders by the aliased column b (i.e., column a), not the
# original column b.
build
SELECT a AS b, b AS c FROM t ORDER BY b
----
project
 ├── columns: b:1(int!null) c:2(int)
 ├── ordering: +1
 └── scan t
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: +1

build
SELECT a AS "foo.bar", b FROM t ORDER BY "foo.bar" DESC
----
project
 ├── columns: foo.bar:1(int!null) b:2(int)
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: -1

build
SELECT a AS foo, b FROM t ORDER BY a DESC
----
project
 ├── columns: foo:1(int!null) b:2(int)
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: -1

build
SELECT b FROM t ORDER BY a DESC
----
project
 ├── columns: b:2(int)
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: -1

build
SELECT b FROM t ORDER BY a LIMIT 1
----
limit
 ├── columns: b:2(int)
 ├── internal-ordering: +1
 ├── ordering: +1
 ├── project
 │    ├── columns: a:1(int!null) b:2(int)
 │    ├── ordering: +1
 │    └── scan t
 │         ├── columns: a:1(int!null) b:2(int) c:3(bool)
 │         └── ordering: +1
 └── const: 1 [type=int]

build
SELECT b FROM t ORDER BY a DESC, b ASC
----
sort
 ├── columns: b:2(int)
 ├── ordering: -1,+2
 └── project
      ├── columns: a:1(int!null) b:2(int)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT b FROM t ORDER BY a DESC, b DESC
----
sort
 ├── columns: b:2(int)
 ├── ordering: -1,-2
 └── project
      ├── columns: a:1(int!null) b:2(int)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

# both presentation and ordering
build
SELECT a, b, b FROM t ORDER BY c
----
sort
 ├── columns: a:1(int!null) b:2(int) b:2(int)
 ├── ordering: +3
 └── scan t
      └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT * FROM t ORDER BY (b, t.*)
----
sort
 ├── columns: a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +2,+1,+2,+3
 └── scan t
      └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT * FROM t ORDER BY (b, a), c
----
sort
 ├── columns: a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +2,+1,+3
 └── scan t
      └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT * FROM t ORDER BY b, (a, c)
----
sort
 ├── columns: a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +2,+1,+3
 └── scan t
      └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT * FROM t ORDER BY (b, (a, c))
----
sort
 ├── columns: a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +2,+1,+3
 └── scan t
      └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT a, b FROM t WHERE b = 7 ORDER BY b, a
----
sort
 ├── columns: a:1(int!null) b:2(int!null)
 ├── ordering: +2,+1
 └── project
      ├── columns: a:1(int!null) b:2(int!null)
      └── select
           ├── columns: a:1(int!null) b:2(int!null) c:3(bool)
           ├── scan t
           │    └── columns: a:1(int!null) b:2(int) c:3(bool)
           └── filters [type=bool]
                └── eq [type=bool]
                     ├── variable: b [type=int]
                     └── const: 7 [type=int]

build
SELECT a, b FROM t ORDER BY b, a DESC
----
sort
 ├── columns: a:1(int!null) b:2(int)
 ├── ordering: +2,-1
 └── project
      ├── columns: a:1(int!null) b:2(int)
      └── scan t
           └── columns: a:1(int!null) b:2(int) c:3(bool)

build
SELECT a, b, a+b AS ab FROM t WHERE b = 7 ORDER BY ab DESC, a
----
sort
 ├── columns: a:1(int!null) b:2(int!null) ab:4(int)
 ├── ordering: -4,+1
 └── project
      ├── columns: ab:4(int) a:1(int!null) b:2(int!null)
      ├── select
      │    ├── columns: a:1(int!null) b:2(int!null) c:3(bool)
      │    ├── scan t
      │    │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      │    └── filters [type=bool]
      │         └── eq [type=bool]
      │              ├── variable: b [type=int]
      │              └── const: 7 [type=int]
      └── projections
           └── plus [type=int]
                ├── variable: a [type=int]
                └── variable: b [type=int]

build
SELECT a FROM t ORDER BY a+b DESC, a
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: -4,+1
 └── project
      ├── columns: column4:4(int) a:1(int!null)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── plus [type=int]
                ├── variable: a [type=int]
                └── variable: b [type=int]

build
SELECT a FROM t ORDER BY (((a)))
----
project
 ├── columns: a:1(int!null)
 ├── ordering: +1
 └── scan t
      ├── columns: a:1(int!null) b:2(int) c:3(bool)
      └── ordering: +1

build
(((SELECT a FROM t))) ORDER BY a DESC LIMIT 4
----
limit
 ├── columns: a:1(int!null)
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1(int!null)
 │    ├── ordering: -1
 │    └── scan t,rev
 │         ├── columns: a:1(int!null) b:2(int) c:3(bool)
 │         └── ordering: -1
 └── const: 4 [type=int]

build
(((SELECT a FROM t ORDER BY a DESC LIMIT 4)))
----
limit
 ├── columns: a:1(int!null)
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1(int!null)
 │    ├── ordering: -1
 │    └── scan t,rev
 │         ├── columns: a:1(int!null) b:2(int) c:3(bool)
 │         └── ordering: -1
 └── const: 4 [type=int]

build
((SELECT a FROM t ORDER BY a)) ORDER BY a
----
error (42601): multiple ORDER BY clauses not allowed

build
SELECT CASE a WHEN 1 THEN b ELSE c END as val FROM t ORDER BY val
----
error: incompatible value type: expected c to be of type int, found type bool

build
SELECT * FROM t ORDER BY 0
----
error (42P10): ORDER BY position 0 is not in select list

build
SELECT * FROM t ORDER BY true
----
error (42601): non-integer constant in ORDER BY: true

build
SELECT * FROM t ORDER BY 'a'
----
error (42601): non-integer constant in ORDER BY: 'a'

build
SELECT * FROM t ORDER BY 2.5
----
error (42601): non-integer constant in ORDER BY: 2.5

build
SELECT * FROM t ORDER BY foo
----
error (42703): column "foo" does not exist

build
SELECT a FROM t ORDER BY a.b
----
error (42P01): no data source matches prefix: a

build
SELECT generate_series FROM generate_series(1, 100) ORDER BY ARRAY[generate_series]
----
error (0A000): can't order by column type int[]

build
SELECT ARRAY[generate_series] FROM generate_series(1, 100) ORDER BY ARRAY[generate_series]
----
error (0A000): can't order by column type int[]

build
SELECT ARRAY[generate_series] FROM generate_series(1, 100) ORDER BY 1
----
error (0A000): can't order by column type int[]

build
SELECT ARRAY[generate_series] AS a FROM generate_series(1, 100) ORDER BY a
----
error (0A000): can't order by column type int[]

build
SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY 1
----
sort
 ├── columns: generate_series:1(int) array:2(int[])
 ├── ordering: +1
 └── project
      ├── columns: array:2(int[]) generate_series:1(int)
      ├── zip
      │    ├── columns: generate_series:1(int)
      │    └── function: generate_series [type=int]
      │         ├── const: 1 [type=int]
      │         └── const: 1 [type=int]
      └── projections
           └── array: [type=int[]]
                └── variable: generate_series [type=int]

build
SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY generate_series
----
sort
 ├── columns: generate_series:1(int) array:2(int[])
 ├── ordering: +1
 └── project
      ├── columns: array:2(int[]) generate_series:1(int)
      ├── zip
      │    ├── columns: generate_series:1(int)
      │    └── function: generate_series [type=int]
      │         ├── const: 1 [type=int]
      │         └── const: 1 [type=int]
      └── projections
           └── array: [type=int[]]
                └── variable: generate_series [type=int]

build
SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY -generate_series
----
sort
 ├── columns: generate_series:1(int) array:2(int[])
 ├── ordering: +3
 └── project
      ├── columns: array:2(int[]) column3:3(int) generate_series:1(int)
      ├── zip
      │    ├── columns: generate_series:1(int)
      │    └── function: generate_series [type=int]
      │         ├── const: 1 [type=int]
      │         └── const: 1 [type=int]
      └── projections
           ├── array: [type=int[]]
           │    └── variable: generate_series [type=int]
           └── unary-minus [type=int]
                └── variable: generate_series [type=int]


# Sort should be skipped if the ORDER BY clause is constant.
# (This skipping will probably happen later during optimization. The below
# tests should just show that the ordering column is constant.)
build
SELECT * FROM t ORDER BY 1+2
----
sort
 ├── columns: a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +4
 └── project
      ├── columns: column4:4(int!null) a:1(int!null) b:2(int) c:3(bool)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── const: 3 [type=int]

build
SELECT 1 AS r, * FROM t ORDER BY 1
----
sort
 ├── columns: r:4(int!null) a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +4
 └── project
      ├── columns: r:4(int!null) a:1(int!null) b:2(int) c:3(bool)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── const: 1 [type=int]

build
SELECT * FROM t ORDER BY length('abc')
----
sort
 ├── columns: a:1(int!null) b:2(int) c:3(bool)
 ├── ordering: +4
 └── project
      ├── columns: column4:4(int) a:1(int!null) b:2(int) c:3(bool)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── function: length [type=int]
                └── const: 'abc' [type=string]

build
SELECT b+2 AS r FROM t ORDER BY b+2
----
sort
 ├── columns: r:4(int)
 ├── ordering: +4
 └── project
      ├── columns: r:4(int)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── plus [type=int]
                ├── variable: b [type=int]
                └── const: 2 [type=int]

# Check that the sort picks up a renamed render properly.
build
SELECT b+2 AS y FROM t ORDER BY y
----
sort
 ├── columns: y:4(int)
 ├── ordering: +4
 └── project
      ├── columns: y:4(int)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── plus [type=int]
                ├── variable: b [type=int]
                └── const: 2 [type=int]

build
SELECT b+2 AS y FROM t ORDER BY b+2
----
sort
 ├── columns: y:4(int)
 ├── ordering: +4
 └── project
      ├── columns: y:4(int)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── plus [type=int]
                ├── variable: b [type=int]
                └── const: 2 [type=int]

build
SELECT b, c FROM t ORDER BY @2
----
sort
 ├── columns: b:2(int) c:3(bool)
 ├── ordering: +4
 └── project
      ├── columns: column4:4(int) b:2(int) c:3(bool)
      ├── scan t
      │    └── columns: a:1(int!null) b:2(int) c:3(bool)
      └── projections
           └── variable: b [type=int]

build
SELECT b, c FROM t ORDER BY @4
----
error (42703): invalid column ordinal: @4

exec-ddl
CREATE TABLE abc (
  a INT,
  b INT,
  c INT,
  d CHAR,
  PRIMARY KEY (a, b, c),
  UNIQUE INDEX bc (b, c),
  INDEX ba (b, a),
  FAMILY (a, b, c),
  FAMILY (d)
)
----
TABLE abc
 ├── a int not null
 ├── b int not null
 ├── c int not null
 ├── d string
 ├── INDEX primary
 │    ├── a int not null
 │    ├── b int not null
 │    └── c int not null
 ├── INDEX bc
 │    ├── b int not null
 │    ├── c int not null
 │    └── a int not null (storing)
 └── INDEX ba
      ├── b int not null
      ├── a int not null
      └── c int not null

exec-ddl
CREATE VIEW abcview AS SELECT * FROM abc
----
VIEW abcview
 └── SELECT * FROM abc

build
SELECT d FROM abc ORDER BY lower(d)
----
sort
 ├── columns: d:4(string)
 ├── ordering: +5
 └── project
      ├── columns: column5:5(string) d:4(string)
      ├── scan abc
      │    └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
      └── projections
           └── function: lower [type=string]
                └── variable: d [type=string]

build
SELECT * FROM abc ORDER BY a
----
scan abc
 ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
 └── ordering: +1

build
SELECT a, b FROM abc ORDER BY b, a
----
sort
 ├── columns: a:1(int!null) b:2(int!null)
 ├── ordering: +2,+1
 └── project
      ├── columns: a:1(int!null) b:2(int!null)
      └── scan abc
           └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)

build
SELECT a, b FROM abc ORDER BY b, c
----
sort
 ├── columns: a:1(int!null) b:2(int!null)
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1(int!null) b:2(int!null) c:3(int!null)
      └── scan abc
           └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)

build
SELECT a, b FROM abc ORDER BY b, c, a DESC
----
sort
 ├── columns: a:1(int!null) b:2(int!null)
 ├── ordering: +2,+3,-1
 └── project
      ├── columns: a:1(int!null) b:2(int!null) c:3(int!null)
      └── scan abc
           └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)

build
SELECT a FROM abc ORDER BY a DESC
----
project
 ├── columns: a:1(int!null)
 ├── ordering: -1
 └── scan abc,rev
      ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
      └── ordering: -1

build
SELECT a FROM abc ORDER BY a DESC LIMIT 1
----
limit
 ├── columns: a:1(int!null)
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1(int!null)
 │    ├── ordering: -1
 │    └── scan abc,rev
 │         ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
 │         └── ordering: -1
 └── const: 1 [type=int]

build
SELECT a FROM abc ORDER BY a DESC OFFSET 1
----
offset
 ├── columns: a:1(int!null)
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1(int!null)
 │    ├── ordering: -1
 │    └── scan abc,rev
 │         ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
 │         └── ordering: -1
 └── const: 1 [type=int]

build
SELECT c FROM abc WHERE b = 2 ORDER BY c
----
sort
 ├── columns: c:3(int!null)
 ├── ordering: +3
 └── project
      ├── columns: c:3(int!null)
      └── select
           ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
           ├── scan abc
           │    └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
           └── filters [type=bool]
                └── eq [type=bool]
                     ├── variable: b [type=int]
                     └── const: 2 [type=int]

build
SELECT c FROM abc WHERE b = 2 ORDER BY c DESC
----
sort
 ├── columns: c:3(int!null)
 ├── ordering: -3
 └── project
      ├── columns: c:3(int!null)
      └── select
           ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
           ├── scan abc
           │    └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
           └── filters [type=bool]
                └── eq [type=bool]
                     ├── variable: b [type=int]
                     └── const: 2 [type=int]

build
SELECT * FROM (SELECT b, c FROM abc WHERE a=1 ORDER BY a,b) ORDER BY b,c
----
sort
 ├── columns: b:2(int!null) c:3(int!null)
 ├── ordering: +2,+3
 └── project
      ├── columns: b:2(int!null) c:3(int!null)
      └── project
           ├── columns: a:1(int!null) b:2(int!null) c:3(int!null)
           └── select
                ├── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
                ├── scan abc
                │    └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)
                └── filters [type=bool]
                     └── eq [type=bool]
                          ├── variable: a [type=int]
                          └── const: 1 [type=int]

build
SELECT a FROM abc ORDER BY INDEX abc@bc
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1(int!null) b:2(int!null) c:3(int!null)
      └── scan abc
           └── columns: a:1(int!null) b:2(int!null) c:3(int!null) d:4(string)

build
SELECT a FROM abc ORDER BY PRIMARY KEY a
----
error: no data source matches prefix: "a"

build
SELECT a FROM abcview ORDER BY INDEX abcview@bc
----
error (42809): "abcview" is not a table

exec-ddl
CREATE TABLE bar (id INT PRIMARY KEY, baz STRING, UNIQUE INDEX i_bar (baz))
----
TABLE bar
 ├── id int not null
 ├── baz string
 ├── INDEX primary
 │    └── id int not null
 └── INDEX i_bar
      ├── baz string
      └── id int not null (storing)

build
SELECT * FROM bar ORDER BY baz, id
----
sort
 ├── columns: id:1(int!null) baz:2(string)
 ├── ordering: +2,+1
 └── scan bar
      └── columns: id:1(int!null) baz:2(string)

exec-ddl
CREATE TABLE abcd (
  a INT PRIMARY KEY,
  b INT,
  c INT,
  d INT,
  INDEX abc (a, b, c),
  INDEX bcd (b, c DESC, d)
)
----
TABLE abcd
 ├── a int not null
 ├── b int
 ├── c int
 ├── d int
 ├── INDEX primary
 │    └── a int not null
 ├── INDEX abc
 │    ├── a int not null
 │    ├── b int
 │    └── c int
 └── INDEX bcd
      ├── b int
      ├── c int desc
      ├── d int
      └── a int not null

# Verify that projections after ORDER BY perform correctly (i.e., the outer
# expression does not guarantee it will apply the ORDER BY).

build
SELECT a+b AS r FROM (SELECT * FROM abcd ORDER BY d)
----
project
 ├── columns: r:5(int)
 ├── scan abcd
 │    └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)
 └── projections
      └── plus [type=int]
           ├── variable: a [type=int]
           └── variable: b [type=int]

build
SELECT b+d AS r FROM (SELECT * FROM abcd ORDER BY a,d)
----
project
 ├── columns: r:5(int)
 ├── scan abcd
 │    └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)
 └── projections
      └── plus [type=int]
           ├── variable: b [type=int]
           └── variable: d [type=int]

build
SELECT * FROM (VALUES ('a'), ('b'), ('c')) AS c(x) ORDER BY x
----
sort
 ├── columns: x:1(string)
 ├── ordering: +1
 └── values
      ├── columns: column1:1(string)
      ├── tuple [type=tuple{string}]
      │    └── const: 'a' [type=string]
      ├── tuple [type=tuple{string}]
      │    └── const: 'b' [type=string]
      └── tuple [type=tuple{string}]
           └── const: 'c' [type=string]

build
SELECT * FROM (SELECT * FROM (VALUES ('a'), ('b'), ('c')) AS c(x) ORDER BY x)
----
values
 ├── columns: x:1(string)
 ├── tuple [type=tuple{string}]
 │    └── const: 'a' [type=string]
 ├── tuple [type=tuple{string}]
 │    └── const: 'b' [type=string]
 └── tuple [type=tuple{string}]
      └── const: 'c' [type=string]

exec-ddl
CREATE TABLE blocks (
  block_id  INT,
  writer_id STRING,
  block_num INT,
  raw_bytes BYTES,
  PRIMARY KEY (block_id, writer_id, block_num)
)
----
TABLE blocks
 ├── block_id int not null
 ├── writer_id string not null
 ├── block_num int not null
 ├── raw_bytes bytes
 └── INDEX primary
      ├── block_id int not null
      ├── writer_id string not null
      └── block_num int not null

# Regression test for #13696.
build
SELECT block_id,writer_id,block_num,block_id FROM blocks ORDER BY block_id, writer_id, block_num LIMIT 1
----
limit
 ├── columns: block_id:1(int!null) writer_id:2(string!null) block_num:3(int!null) block_id:1(int!null)
 ├── internal-ordering: +1,+2,+3
 ├── ordering: +1,+2,+3
 ├── project
 │    ├── columns: block_id:1(int!null) writer_id:2(string!null) block_num:3(int!null)
 │    ├── ordering: +1,+2,+3
 │    └── scan blocks
 │         ├── columns: block_id:1(int!null) writer_id:2(string!null) block_num:3(int!null) raw_bytes:4(bytes)
 │         └── ordering: +1,+2,+3
 └── const: 1 [type=int]

build
SELECT a FROM abcd ORDER BY PRIMARY KEY abcd
----
project
 ├── columns: a:1(int!null)
 ├── ordering: +1
 └── scan abcd
      ├── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)
      └── ordering: +1

build
SELECT a FROM abcd ORDER BY b, PRIMARY KEY abcd
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,+1
 └── project
      ├── columns: a:1(int!null) b:2(int)
      └── scan abcd
           └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM abcd ORDER BY INDEX abcd@abc
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +1,+2,+3
 └── project
      ├── columns: a:1(int!null) b:2(int) c:3(int)
      └── scan abcd
           └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM abcd ORDER BY INDEX abcd@abc DESC
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: -1,-2,-3
 └── project
      ├── columns: a:1(int!null) b:2(int) c:3(int)
      └── scan abcd
           └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM abcd AS foo ORDER BY INDEX abcd@abc DESC
----
error (42P01): no data source matches prefix: t.public.abcd

build
SELECT a FROM abcd AS foo ORDER BY INDEX foo@abc DESC
----
error: no data source matches prefix: "foo"

build
SELECT a FROM abcd ORDER BY INDEX abcd@bcd
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,-3,+4,+1
 └── scan abcd
      └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM abcd ORDER BY INDEX abcd@bcd DESC
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: -2,+3,-4,-1
 └── scan abcd
      └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)


build
SELECT a FROM abcd ORDER BY INDEX abcd@nonexistent
----
error: index "nonexistent" not found

build
SELECT a FROM t.public.abcd ORDER BY INDEX t.public.abcd@bcd
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,-3,+4,+1
 └── scan abcd
      └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM t.abcd ORDER BY INDEX t.abcd@bcd
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,-3,+4,+1
 └── scan abcd
      └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM public.abcd ORDER BY INDEX public.abcd@bcd
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,-3,+4,+1
 └── scan abcd
      └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM (SELECT a FROM abcd) ORDER BY INDEX abcd@bcd
----
error (42P01): no data source matches prefix: t.public.abcd

# Drop previous table with same name, but different schema.
exec-ddl
DROP TABLE abcd
----

exec-ddl
CREATE TABLE abcd (
  a INT PRIMARY KEY,
  b INT,
  c INT,
  d INT
)
----
TABLE abcd
 ├── a int not null
 ├── b int
 ├── c int
 ├── d int
 └── INDEX primary
      └── a int not null

build
SELECT a, b FROM abcd ORDER BY b, c
----
sort
 ├── columns: a:1(int!null) b:2(int)
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1(int!null) b:2(int) c:3(int)
      └── scan abcd
           └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM abcd ORDER BY b, c
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1(int!null) b:2(int) c:3(int)
      └── scan abcd
           └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT a FROM abcd ORDER BY a, b, c
----
sort
 ├── columns: a:1(int!null)
 ├── ordering: +1,+2,+3
 └── project
      ├── columns: a:1(int!null) b:2(int) c:3(int)
      └── scan abcd
           └── columns: a:1(int!null) b:2(int) c:3(int) d:4(int)

build
SELECT ARRAY[a] FROM abcd ORDER BY 1
----
error (0A000): can't order by column type int[]
