@@ -1517,3 +1517,40 @@ SELECT address, zip FROM addresses ORDER BY ALL;
15171517111 Duck Duck Goose Ln 11111
15181518111 Duck Duck Goose Ln 11111-0001
15191519123 Quack Blvd 11111
1520+
1521+ # Create a table with an order clause that's not a simple column reference
1522+ statement ok
1523+ CREATE EXTERNAL TABLE ordered (
1524+ a BIGINT NOT NULL,
1525+ b BIGINT NOT NULL
1526+ )
1527+ STORED AS CSV
1528+ LOCATION 'data/composite_order.csv'
1529+ OPTIONS ('format.has_header' 'true')
1530+ WITH ORDER (a + b);
1531+
1532+ # Simple query should be just a table scan
1533+ query TT
1534+ EXPLAIN SELECT * from ordered;
1535+ ----
1536+ physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/data/composite_order.csv]]}, projection=[a, b], output_ordering=[a@0 + b@1 ASC NULLS LAST], file_type=csv, has_header=true
1537+
1538+ # Query ordered by the declared order should be just a table scan
1539+ query TT
1540+ EXPLAIN SELECT * from ordered ORDER BY (a + b);
1541+ ----
1542+ physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/data/composite_order.csv]]}, projection=[a, b], output_ordering=[a@0 + b@1 ASC NULLS LAST], file_type=csv, has_header=true
1543+
1544+ # Order equivalence handling should make this query a simple table scan
1545+ query TT
1546+ EXPLAIN SELECT * from ordered ORDER BY -(a + b) desc nulls last;
1547+ ----
1548+ physical_plan DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/data/composite_order.csv]]}, projection=[a, b], output_ordering=[a@0 + b@1 ASC NULLS LAST], file_type=csv, has_header=true
1549+
1550+ # Ordering by another column requires a sort
1551+ query TT
1552+ EXPLAIN SELECT * from ordered ORDER BY a;
1553+ ----
1554+ physical_plan
1555+ 01)SortExec: expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false]
1556+ 02)--DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/sqllogictest/data/composite_order.csv]]}, projection=[a, b], output_ordering=[a@0 + b@1 ASC NULLS LAST], file_type=csv, has_header=true
0 commit comments