CREATE TABLE test (a INTEGER, b INTEGER, ts TIMESTAMP TIME INDEX);

Affected Rows: 0

INSERT INTO test VALUES (11, 22, 1), (12, 21, 2), (13, 22, 3);

Affected Rows: 3

SELECT a FROM test ORDER BY 2;

Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: Order by column out of bounds, specified: 2, max: 1

SELECT a FROM test ORDER BY 'hello', a;

+----+
| a  |
+----+
| 11 |
| 12 |
| 13 |
+----+

-- Ambiguous reference in union alias, give and error in duckdb, but works in greptimedb
SELECT a AS k, b FROM test UNION SELECT a, b AS k FROM test ORDER BY k;

+----+----+
| k  | b  |
+----+----+
| 11 | 22 |
| 12 | 21 |
| 13 | 22 |
+----+----+

SELECT a AS k, b FROM test UNION SELECT a AS k, b FROM test ORDER BY k;

+----+----+
| k  | b  |
+----+----+
| 11 | 22 |
| 12 | 21 |
| 13 | 22 |
+----+----+

SELECT a % 2, b FROM test UNION SELECT b, a % 2 AS k ORDER BY a % 2;

Error: 3000(PlanQuery), Failed to plan SQL: No field named b.

-- Works duckdb, but not work in greptimedb
SELECT a % 2, b FROM test UNION SELECT a % 2 AS k, b FROM test ORDER BY a % 2;

Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: For SELECT DISTINCT, ORDER BY expressions a must appear in select list

SELECT a % 2, b FROM test UNION SELECT a % 2 AS k, b FROM test ORDER BY 3;

Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: Order by column out of bounds, specified: 3, max: 2

-- "order by -1" is generally an undefined behavior.
-- It's not supported in PostgreSQL 16, error "ORDER BY position -1 is not in select list".
-- But in Mysql 8, it can be executed, just the actual order is ignored.
-- In DataFusion, it behaves like Mysql 8. The "sort" plan node will be eliminated by the physical optimizer
-- "EnforceSorting" because it's sort key is parsed as a constant "-1".
-- We check the "explain" of the "order by -1" query to ensure that.
-- SQLNESS REPLACE (peers.*) REDACTED
-- SQLNESS REPLACE (partitioning.*) REDACTED
EXPLAIN SELECT a % 2, b FROM test UNION SELECT a % 2 AS k, b FROM test ORDER BY -1;

+---------------+------------------------------------------------------------------------------------------------------------+
| plan_type     | plan                                                                                                       |
+---------------+------------------------------------------------------------------------------------------------------------+
| logical_plan  | Sort: Int64(-1) ASC NULLS LAST                                                                             |
|               |   Aggregate: groupBy=[[test.a % Int64(2), test.b]], aggr=[[]]                                              |
|               |     Union                                                                                                  |
|               |       MergeScan [is_placeholder=false]                                                                     |
|               |       MergeScan [is_placeholder=false]                                                                     |
| physical_plan | CoalescePartitionsExec                                                                                     |
|               |   AggregateExec: mode=SinglePartitioned, gby=[test.a % Int64(2)@0 as test.a % Int64(2), b@1 as b], aggr=[] |
|               |     InterleaveExec                                                                                         |
|               |       MergeScanExec: REDACTED
|               |       MergeScanExec: REDACTED
|               |                                                                                                            |
+---------------+------------------------------------------------------------------------------------------------------------+

SELECT a % 2, b FROM test UNION SELECT a % 2 AS k FROM test ORDER BY -1;

Error: 3000(PlanQuery), Failed to plan SQL: Error during planning: UNION queries have different number of columns

DROP TABLE test;

Affected Rows: 0

