# tests adapted from logictest -- order_by

exec-ddl
CREATE TABLE t (
  a INT PRIMARY KEY,
  b INT,
  c BOOLEAN
)
----

build
SELECT c FROM t ORDER BY c
----
sort
 ├── columns: c:3
 ├── ordering: +3
 └── project
      ├── columns: c:3
      └── scan t
           └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5

build
SELECT c FROM t ORDER BY c DESC
----
sort
 ├── columns: c:3
 ├── ordering: -3
 └── project
      ├── columns: c:3
      └── scan t
           └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5

build
SELECT a, b FROM t ORDER BY b
----
sort
 ├── columns: a:1!null b:2
 ├── ordering: +2
 └── project
      ├── columns: a:1!null b:2
      └── scan t
           └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5

build
SELECT a, b FROM t ORDER BY b DESC
----
sort
 ├── columns: a:1!null b:2
 ├── ordering: -2
 └── project
      ├── columns: a:1!null b:2
      └── scan t
           └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5

build
SELECT a, b FROM t ORDER BY b DESC LIMIT 2
----
limit
 ├── columns: a:1!null b:2
 ├── internal-ordering: -2
 ├── ordering: -2
 ├── sort
 │    ├── columns: a:1!null b:2
 │    ├── ordering: -2
 │    ├── limit hint: 2.00
 │    └── project
 │         ├── columns: a:1!null b:2
 │         └── scan t
 │              └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 └── 2

build
SELECT a FROM t ORDER BY 1 DESC
----
project
 ├── columns: a:1!null
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: -1

# This query causes an error in Postgres, and the optimizer has followed
# that lead.
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!null b:2
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── 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!null foo:1!null
 ├── internal-ordering: +1
 ├── ordering: +1
 ├── project
 │    ├── columns: a:1!null
 │    ├── ordering: +1
 │    ├── limit hint: 1.00
 │    └── scan t
 │         ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── ordering: +1
 │         └── limit hint: 1.00
 └── 1

# 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!null c:2
 ├── ordering: +1
 └── scan t
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: +1

build
SELECT a AS "foo.bar", b FROM t ORDER BY "foo.bar" DESC
----
project
 ├── columns: foo.bar:1!null b:2
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: -1

build
SELECT a AS foo, b FROM t ORDER BY a DESC
----
project
 ├── columns: foo:1!null b:2
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: -1

build
SELECT b FROM t ORDER BY a DESC
----
project
 ├── columns: b:2  [hidden: a:1!null]
 ├── ordering: -1
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: -1

build
SELECT b FROM t ORDER BY a LIMIT 1
----
limit
 ├── columns: b:2  [hidden: a:1!null]
 ├── internal-ordering: +1
 ├── ordering: +1
 ├── project
 │    ├── columns: a:1!null b:2
 │    ├── ordering: +1
 │    ├── limit hint: 1.00
 │    └── scan t
 │         ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── ordering: +1
 │         └── limit hint: 1.00
 └── 1

build
SELECT b FROM t ORDER BY a DESC, b ASC
----
project
 ├── columns: b:2  [hidden: a:1!null]
 ├── ordering: -1,+2
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: -1

build
SELECT b FROM t ORDER BY a DESC, b DESC
----
project
 ├── columns: b:2  [hidden: a:1!null]
 ├── ordering: -1,-2
 └── scan t,rev
      ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── ordering: -1

# both presentation and ordering
build
SELECT a, b, b FROM t ORDER BY c
----
sort
 ├── columns: a:1!null b:2 b:2  [hidden: c:3]
 ├── ordering: +3
 └── project
      ├── columns: a:1!null b:2 c:3
      └── scan t
           └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5

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

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

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

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

build
SELECT a, b FROM t WHERE b = 7 ORDER BY b, a
----
project
 ├── columns: a:1!null b:2!null
 ├── ordering: +2,+1
 └── select
      ├── columns: a:1!null b:2!null c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      ├── ordering: +1 opt(2)
      ├── scan t
      │    ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      │    └── ordering: +1
      └── filters
           └── b:2 = 7

build
SELECT a, b FROM t ORDER BY b, a DESC
----
sort
 ├── columns: a:1!null b:2
 ├── ordering: +2,-1
 └── project
      ├── columns: a:1!null b:2
      └── scan t
           └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5

build
SELECT a, b, a+b AS ab FROM t WHERE b = 7 ORDER BY ab DESC, a
----
sort
 ├── columns: a:1!null b:2!null ab:6!null
 ├── ordering: -6,+1
 └── project
      ├── columns: ab:6!null a:1!null b:2!null
      ├── select
      │    ├── columns: a:1!null b:2!null c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      │    ├── scan t
      │    │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      │    └── filters
      │         └── b:2 = 7
      └── projections
           └── a:1 + b:2 [as=ab:6]

build
SELECT a FROM t ORDER BY a+b DESC, a
----
sort
 ├── columns: a:1!null  [hidden: column6:6]
 ├── ordering: -6,+1
 └── project
      ├── columns: column6:6 a:1!null
      ├── scan t
      │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── projections
           └── a:1 + b:2 [as=column6:6]

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

build
(((SELECT a FROM t))) ORDER BY a DESC LIMIT 4
----
limit
 ├── columns: a:1!null
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1!null
 │    ├── ordering: -1
 │    ├── limit hint: 4.00
 │    └── scan t,rev
 │         ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── ordering: -1
 │         └── limit hint: 4.00
 └── 4

build
(((SELECT a FROM t ORDER BY a DESC LIMIT 4)))
----
limit
 ├── columns: a:1!null
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1!null
 │    ├── ordering: -1
 │    ├── limit hint: 4.00
 │    └── scan t,rev
 │         ├── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 │         ├── ordering: -1
 │         └── limit hint: 4.00
 └── 4

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 (22023): incompatible value type: expected b to be of type bool, found type int

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 in this context

build
SELECT generate_series FROM generate_series(1, 100) ORDER BY ARRAY[generate_series]
----
sort
 ├── columns: generate_series:1  [hidden: column2:2]
 ├── ordering: +2
 └── project
      ├── columns: column2:2 generate_series:1
      ├── project-set
      │    ├── columns: generate_series:1
      │    ├── values
      │    │    └── ()
      │    └── zip
      │         └── generate_series(1, 100)
      └── projections
           └── ARRAY[generate_series:1] [as=column2:2]

build
SELECT ARRAY[generate_series] FROM generate_series(1, 100) ORDER BY ARRAY[generate_series]
----
sort
 ├── columns: array:2
 ├── ordering: +2
 └── project
      ├── columns: array:2
      ├── project-set
      │    ├── columns: generate_series:1
      │    ├── values
      │    │    └── ()
      │    └── zip
      │         └── generate_series(1, 100)
      └── projections
           └── ARRAY[generate_series:1] [as=array:2]

build
SELECT ARRAY[generate_series] FROM generate_series(1, 100) ORDER BY 1
----
sort
 ├── columns: array:2
 ├── ordering: +2
 └── project
      ├── columns: array:2
      ├── project-set
      │    ├── columns: generate_series:1
      │    ├── values
      │    │    └── ()
      │    └── zip
      │         └── generate_series(1, 100)
      └── projections
           └── ARRAY[generate_series:1] [as=array:2]

build
SELECT ARRAY[generate_series] AS a FROM generate_series(1, 100) ORDER BY a
----
sort
 ├── columns: a:2
 ├── ordering: +2
 └── project
      ├── columns: a:2
      ├── project-set
      │    ├── columns: generate_series:1
      │    ├── values
      │    │    └── ()
      │    └── zip
      │         └── generate_series(1, 100)
      └── projections
           └── ARRAY[generate_series:1] [as=a:2]

build
SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY 1
----
project
 ├── columns: generate_series:1 array:2
 ├── ordering: +1
 ├── sort
 │    ├── columns: generate_series:1
 │    ├── ordering: +1
 │    └── project-set
 │         ├── columns: generate_series:1
 │         ├── values
 │         │    └── ()
 │         └── zip
 │              └── generate_series(1, 1)
 └── projections
      └── ARRAY[generate_series:1] [as=array:2]

build
SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY generate_series
----
project
 ├── columns: generate_series:1 array:2
 ├── ordering: +1
 ├── sort
 │    ├── columns: generate_series:1
 │    ├── ordering: +1
 │    └── project-set
 │         ├── columns: generate_series:1
 │         ├── values
 │         │    └── ()
 │         └── zip
 │              └── generate_series(1, 1)
 └── projections
      └── ARRAY[generate_series:1] [as=array:2]

build
SELECT generate_series, ARRAY[generate_series] FROM generate_series(1, 1) ORDER BY -generate_series
----
sort
 ├── columns: generate_series:1 array:2  [hidden: column3:3]
 ├── ordering: +3
 └── project
      ├── columns: array:2 column3:3 generate_series:1
      ├── project-set
      │    ├── columns: generate_series:1
      │    ├── values
      │    │    └── ()
      │    └── zip
      │         └── generate_series(1, 1)
      └── projections
           ├── ARRAY[generate_series:1] [as=array:2]
           └── -generate_series:1 [as=column3:3]


# Sort should be skipped if the ORDER BY clause is constant.
build
SELECT * FROM t ORDER BY 1+2
----
project
 ├── columns: a:1!null b:2 c:3  [hidden: column6:6!null]
 ├── ordering: +6
 ├── scan t
 │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 └── projections
      └── 1 + 2 [as=column6:6]

build
SELECT 1 AS r, * FROM t ORDER BY 1
----
project
 ├── columns: r:6!null a:1!null b:2 c:3
 ├── ordering: +6
 ├── scan t
 │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 └── projections
      └── 1 [as=r:6]

build
SELECT * FROM t ORDER BY length('abc')
----
project
 ├── columns: a:1!null b:2 c:3  [hidden: column6:6]
 ├── ordering: +6
 ├── scan t
 │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
 └── projections
      └── length('abc') [as=column6:6]

build
SELECT b+2 AS r FROM t ORDER BY b+2
----
sort
 ├── columns: r:6
 ├── ordering: +6
 └── project
      ├── columns: r:6
      ├── scan t
      │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── projections
           └── b:2 + 2 [as=r:6]

# Check that the sort picks up a renamed render properly.
build
SELECT b+2 AS y FROM t ORDER BY y
----
sort
 ├── columns: y:6
 ├── ordering: +6
 └── project
      ├── columns: y:6
      ├── scan t
      │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── projections
           └── b:2 + 2 [as=y:6]

build
SELECT b+2 AS y FROM t ORDER BY b+2
----
sort
 ├── columns: y:6
 ├── ordering: +6
 └── project
      ├── columns: y:6
      ├── scan t
      │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── projections
           └── b:2 + 2 [as=y:6]

build
SELECT b, c FROM t ORDER BY @2
----
error (01P01): invalid syntax @2

build
SELECT b, c FROM t ORDER BY @4
----
error (01P01): invalid syntax @4

build set=allow_ordinal_column_references=true
SELECT b, c FROM t ORDER BY @2
----
sort
 ├── columns: b:2 c:3  [hidden: column6:6]
 ├── ordering: +6
 └── project
      ├── columns: column6:6 b:2 c:3
      ├── scan t
      │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── projections
           └── b:2 [as=column6:6]

build set=allow_ordinal_column_references=true
SELECT b, c FROM t ORDER BY @4
----
sort
 ├── columns: b:2 c:3  [hidden: column6:6]
 ├── ordering: +6
 └── project
      ├── columns: column6:6 b:2 c:3
      ├── scan t
      │    └── columns: a:1!null b:2 c:3 crdb_internal_mvcc_timestamp:4 tableoid:5
      └── projections
           └── crdb_internal_mvcc_timestamp:4 [as=column6:6]

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)
)
----

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

build
SELECT d FROM abc ORDER BY lower(d)
----
sort
 ├── columns: d:4  [hidden: column7:7]
 ├── ordering: +7
 └── project
      ├── columns: column7:7 d:4
      ├── scan abc
      │    └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── lower(d:4) [as=column7:7]

build
SELECT * FROM abc ORDER BY a
----
project
 ├── columns: a:1!null b:2!null c:3!null d:4
 ├── ordering: +1
 └── scan abc
      ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── ordering: +1

build
SELECT a, b FROM abc ORDER BY b, a
----
sort
 ├── columns: a:1!null b:2!null
 ├── ordering: +2,+1
 └── project
      ├── columns: a:1!null b:2!null
      └── scan abc
           └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a, b FROM abc ORDER BY b, c
----
sort
 ├── columns: a:1!null b:2!null  [hidden: c:3!null]
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1!null b:2!null c:3!null
      └── scan abc
           └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a, b FROM abc ORDER BY b, c, a DESC
----
sort
 ├── columns: a:1!null b:2!null  [hidden: c:3!null]
 ├── ordering: +2,+3,-1
 └── project
      ├── columns: a:1!null b:2!null c:3!null
      └── scan abc
           └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM abc ORDER BY a DESC
----
project
 ├── columns: a:1!null
 ├── ordering: -1
 └── scan abc,rev
      ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── ordering: -1

build
SELECT a FROM abc ORDER BY a DESC LIMIT 1
----
limit
 ├── columns: a:1!null
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1!null
 │    ├── ordering: -1
 │    ├── limit hint: 1.00
 │    └── scan abc,rev
 │         ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         ├── ordering: -1
 │         └── limit hint: 1.00
 └── 1

build
SELECT a FROM abc ORDER BY a DESC OFFSET 1
----
offset
 ├── columns: a:1!null
 ├── internal-ordering: -1
 ├── ordering: -1
 ├── project
 │    ├── columns: a:1!null
 │    ├── ordering: -1
 │    └── scan abc,rev
 │         ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         └── ordering: -1
 └── 1

build
SELECT c FROM abc WHERE b = 2 ORDER BY c
----
sort
 ├── columns: c:3!null
 ├── ordering: +3
 └── project
      ├── columns: c:3!null
      └── select
           ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           ├── scan abc
           │    └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           └── filters
                └── b:2 = 2

build
SELECT c FROM abc WHERE b = 2 ORDER BY c DESC
----
sort
 ├── columns: c:3!null
 ├── ordering: -3
 └── project
      ├── columns: c:3!null
      └── select
           ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           ├── scan abc
           │    └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           └── filters
                └── b:2 = 2

build
SELECT * FROM (SELECT b, c FROM abc WHERE a=1 ORDER BY a,b) ORDER BY b,c
----
project
 ├── columns: b:2!null c:3!null
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1!null b:2!null c:3!null
      ├── ordering: +2,+3 opt(1)
      └── select
           ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           ├── ordering: +2,+3 opt(1)
           ├── scan abc
           │    ├── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
           │    └── ordering: +1,+2,+3
           └── filters
                └── a:1 = 1

build
SELECT a FROM abc ORDER BY INDEX abc@bc
----
sort
 ├── columns: a:1!null  [hidden: b:2!null c:3!null]
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1!null b:2!null c:3!null
      └── scan abc
           └── columns: a:1!null b:2!null c:3!null d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM abc ORDER BY PRIMARY KEY a
----
error (42P01): 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))
----

build
SELECT * FROM bar ORDER BY baz, id
----
sort
 ├── columns: id:1!null baz:2
 ├── ordering: +2,+1
 └── project
      ├── columns: id:1!null baz:2
      └── scan bar
           └── columns: id:1!null baz:2 crdb_internal_mvcc_timestamp:3 tableoid:4

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)
)
----

# 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:7
 ├── project
 │    ├── columns: a:1!null b:2 c:3 d:4
 │    └── scan abcd
 │         └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 └── projections
      └── a:1 + b:2 [as=r:7]

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

build
SELECT * FROM (VALUES ('a'), ('b'), ('c')) AS c(x) ORDER BY x
----
sort
 ├── columns: x:1!null
 ├── ordering: +1
 └── values
      ├── columns: column1:1!null
      ├── ('a',)
      ├── ('b',)
      └── ('c',)

build
SELECT * FROM (SELECT * FROM (VALUES ('a'), ('b'), ('c')) AS c(x) ORDER BY x)
----
values
 ├── columns: x:1!null
 ├── ('a',)
 ├── ('b',)
 └── ('c',)

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)
)
----

# 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!null writer_id:2!null block_num:3!null block_id:1!null
 ├── internal-ordering: +1,+2,+3
 ├── ordering: +1,+2,+3
 ├── project
 │    ├── columns: block_id:1!null writer_id:2!null block_num:3!null
 │    ├── ordering: +1,+2,+3
 │    ├── limit hint: 1.00
 │    └── scan blocks
 │         ├── columns: block_id:1!null writer_id:2!null block_num:3!null raw_bytes:4 crdb_internal_mvcc_timestamp:5 tableoid:6
 │         ├── ordering: +1,+2,+3
 │         └── limit hint: 1.00
 └── 1

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

build
SELECT a FROM abcd ORDER BY b, PRIMARY KEY abcd
----
sort
 ├── columns: a:1!null  [hidden: b:2]
 ├── ordering: +2,+1
 └── project
      ├── columns: a:1!null b:2
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM abcd ORDER BY INDEX abcd@abc
----
project
 ├── columns: a:1!null  [hidden: b:2 c:3]
 ├── ordering: +1,+2,+3
 └── scan abcd
      ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── ordering: +1

build
SELECT a FROM abcd ORDER BY INDEX abcd@abc DESC
----
project
 ├── columns: a:1!null  [hidden: b:2 c:3]
 ├── ordering: -1,-2,-3
 └── scan abcd,rev
      ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── ordering: -1

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

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

build
SELECT a FROM abcd ORDER BY INDEX abcd@bcd
----
sort
 ├── columns: a:1!null  [hidden: b:2 c:3 d:4]
 ├── ordering: +2,-3,+4,+1
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM abcd ORDER BY INDEX abcd@bcd DESC
----
sort
 ├── columns: a:1!null  [hidden: b:2 c:3 d:4]
 ├── ordering: -2,+3,-4,-1
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6


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

build
SELECT a FROM t.public.abcd ORDER BY INDEX t.public.abcd@bcd
----
sort
 ├── columns: a:1!null  [hidden: b:2 c:3 d:4]
 ├── ordering: +2,-3,+4,+1
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM t.abcd ORDER BY INDEX t.abcd@bcd
----
sort
 ├── columns: a:1!null  [hidden: b:2 c:3 d:4]
 ├── ordering: +2,-3,+4,+1
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM public.abcd ORDER BY INDEX public.abcd@bcd
----
sort
 ├── columns: a:1!null  [hidden: b:2 c:3 d:4]
 ├── ordering: +2,-3,+4,+1
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

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

# 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
)
----

# Next two plans should be the same.
build
SELECT a, b FROM abcd ORDER BY b, c
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3]
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1!null b:2 c:3
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a, b FROM abcd ORDER BY b, c NULLS FIRST
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3]
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1!null b:2 c:3
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

# Should be different from the plan above (1 null re-ordering).
build
SELECT a, b FROM abcd ORDER BY b NULLS LAST, c
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3 nulls_ordering_b:7!null]
 ├── ordering: +7,+2,+3
 └── project
      ├── columns: nulls_ordering_b:7!null a:1!null b:2 c:3
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── b:2 IS NULL [as=nulls_ordering_b:7]

# Should be different from the two plans above (2 null re-orderings).
build
SELECT a, b FROM abcd ORDER BY b NULLS LAST, c DESC NULLS FIRST
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3 nulls_ordering_b:7!null nulls_ordering_c:8!null]
 ├── ordering: +7,+2,-8,-3
 └── project
      ├── columns: nulls_ordering_b:7!null nulls_ordering_c:8!null a:1!null b:2 c:3
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           ├── b:2 IS NULL [as=nulls_ordering_b:7]
           └── c:3 IS NULL [as=nulls_ordering_c:8]

# Try NULLS ordered last.
# Next two plans should be the same.
build set=null_ordered_last=true
SELECT a, b FROM abcd ORDER BY b, c
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3 nulls_ordering_b:7!null nulls_ordering_c:8!null]
 ├── ordering: +7,+2,+8,+3
 └── project
      ├── columns: nulls_ordering_b:7!null nulls_ordering_c:8!null a:1!null b:2 c:3
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           ├── b:2 IS NULL [as=nulls_ordering_b:7]
           └── c:3 IS NULL [as=nulls_ordering_c:8]

build set=null_ordered_last=true
SELECT a, b FROM abcd ORDER BY b, c NULLS LAST
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3 nulls_ordering_b:7!null nulls_ordering_c:8!null]
 ├── ordering: +7,+2,+8,+3
 └── project
      ├── columns: nulls_ordering_b:7!null nulls_ordering_c:8!null a:1!null b:2 c:3
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           ├── b:2 IS NULL [as=nulls_ordering_b:7]
           └── c:3 IS NULL [as=nulls_ordering_c:8]

# Should be different from the plan above (1 null re-ordering).
build set=null_ordered_last=true
SELECT a, b FROM abcd ORDER BY b NULLS FIRST, c
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3 nulls_ordering_c:7!null]
 ├── ordering: +2,+7,+3
 └── project
      ├── columns: nulls_ordering_c:7!null a:1!null b:2 c:3
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── c:3 IS NULL [as=nulls_ordering_c:7]

# Should be different from the two plans above (2 null re-orderings).
build set=null_ordered_last=true
SELECT a, b FROM abcd ORDER BY b NULLS FIRST, c DESC NULLS LAST
----
sort
 ├── columns: a:1!null b:2  [hidden: c:3]
 ├── ordering: +2,-3
 └── project
      ├── columns: a:1!null b:2 c:3
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM abcd ORDER BY b, c
----
sort
 ├── columns: a:1!null  [hidden: b:2 c:3]
 ├── ordering: +2,+3
 └── project
      ├── columns: a:1!null b:2 c:3
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT a FROM abcd ORDER BY a, b, c
----
project
 ├── columns: a:1!null  [hidden: b:2 c:3]
 ├── ordering: +1,+2,+3
 └── scan abcd
      ├── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── ordering: +1

build
SELECT ARRAY[a] FROM abcd ORDER BY 1
----
sort
 ├── columns: array:7!null
 ├── ordering: +7
 └── project
      ├── columns: array:7!null
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── ARRAY[a:1] [as=array:7]

# Next two plans should be the same.
build
SELECT * FROM abcd ORDER BY b DESC
----
sort
 ├── columns: a:1!null b:2 c:3 d:4
 ├── ordering: -2
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

build
SELECT * FROM abcd ORDER BY b DESC NULLS LAST
----
sort
 ├── columns: a:1!null b:2 c:3 d:4
 ├── ordering: -2
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

# Should be different from the plan above (1 null re-ordering).
build
SELECT * FROM abcd ORDER BY b DESC NULLS FIRST
----
sort
 ├── columns: a:1!null b:2 c:3 d:4  [hidden: nulls_ordering_b:7!null]
 ├── ordering: -7,-2
 └── project
      ├── columns: nulls_ordering_b:7!null a:1!null b:2 c:3 d:4
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── b:2 IS NULL [as=nulls_ordering_b:7]

# Try with NULL ordered last.
# Next two plans should be the same.
build set=null_ordered_last=true
SELECT * FROM abcd ORDER BY b DESC
----
sort
 ├── columns: a:1!null b:2 c:3 d:4  [hidden: nulls_ordering_b:7!null]
 ├── ordering: -7,-2
 └── project
      ├── columns: nulls_ordering_b:7!null a:1!null b:2 c:3 d:4
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── b:2 IS NULL [as=nulls_ordering_b:7]

build set=null_ordered_last=true
SELECT * FROM abcd ORDER BY b DESC NULLS FIRST
----
sort
 ├── columns: a:1!null b:2 c:3 d:4  [hidden: nulls_ordering_b:7!null]
 ├── ordering: -7,-2
 └── project
      ├── columns: nulls_ordering_b:7!null a:1!null b:2 c:3 d:4
      ├── scan abcd
      │    └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6
      └── projections
           └── b:2 IS NULL [as=nulls_ordering_b:7]

# Should be different from the plan above (1 null re-ordering).
build set=null_ordered_last=true
SELECT * FROM abcd ORDER BY b DESC NULLS LAST
----
sort
 ├── columns: a:1!null b:2 c:3 d:4
 ├── ordering: -2
 └── project
      ├── columns: a:1!null b:2 c:3 d:4
      └── scan abcd
           └── columns: a:1!null b:2 c:3 d:4 crdb_internal_mvcc_timestamp:5 tableoid:6

# TODO(#129956): Handle non-default NULL ordering that references a projected
# expression that contains a subquery.
exec-ddl
CREATE TABLE t129956 (i INT)
----

build
SELECT * FROM t129956 AS t0
WHERE EXISTS(
  SELECT (SELECT t0.i FROM t129956) AS tmp
  ORDER BY tmp NULLS LAST
)
----
error (0A000): subqueries in ORDER BY with non-default NULLS ordering is not supported

# TODO(#92165): Allow ordering by VECTOR columns.
exec-ddl
CREATE TABLE t_vector (v VECTOR)
----

build
SELECT * FROM t_vector ORDER BY v
----
error (0A000): unimplemented: can't order by column type VECTOR
