-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: add cluster setting to preserve subquery and cte ordering
This patch adds an experimental cluster setting `propagate_input_ordering` that indicates whether subqueries and CTEs should propagate their orderings to their parent scopes, when the parent scope is unordered. As an example, the following two queries should produce the following result when the cluster setting is true: ``` select * from (select * from generate_series(1, 10) i order by i % 5 asc, i asc) tmp; with tmp as (select * from generate_series(1, 10) i order by i % 5 asc, i asc) select * from tmp; ---- 5 10 1 6 2 7 3 8 4 9 ``` This allows cockroach to imitate postgres behavior - while postgres does not guarantee to maintain ordering on subqueries, it does in practice. Some existing applications take advantage of this fact, and so the ability to toggle this setting can help resolve incompatibilities in some cases. Fixes #68211 Release note: None
- Loading branch information
1 parent
50ef2fc
commit f983edb
Showing
12 changed files
with
466 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
pkg/sql/logictest/testdata/logic_test/preserve_input_order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
statement ok | ||
SET propagate_input_ordering=true; | ||
|
||
query I | ||
WITH tmp AS (SELECT * FROM generate_series(1, 10) i ORDER BY i % 5 ASC, i ASC) SELECT * FROM tmp; | ||
---- | ||
5 | ||
10 | ||
1 | ||
6 | ||
2 | ||
7 | ||
3 | ||
8 | ||
4 | ||
9 | ||
|
||
query I | ||
WITH tmp AS (SELECT * FROM generate_series(1, 10) i ORDER BY i % 5 ASC, i ASC) SELECT * FROM tmp; | ||
---- | ||
5 | ||
10 | ||
1 | ||
6 | ||
2 | ||
7 | ||
3 | ||
8 | ||
4 | ||
9 | ||
|
||
# The internal ordering column for i%5 should not be present in the output. | ||
query T | ||
SELECT foo FROM (SELECT i, i%2 FROM generate_series(1, 10) i ORDER BY i % 5 ASC, i ASC) AS foo | ||
---- | ||
(5,1) | ||
(10,0) | ||
(1,1) | ||
(6,0) | ||
(2,0) | ||
(7,1) | ||
(3,1) | ||
(8,0) | ||
(4,0) | ||
(9,1) | ||
|
||
# The internal ordering column for i%5 should not be present in the output. | ||
query II | ||
SELECT foo.* FROM (SELECT i, i%2 FROM generate_series(1, 10) i ORDER BY i % 5 ASC, i ASC) AS foo | ||
---- | ||
5 1 | ||
10 0 | ||
1 1 | ||
6 0 | ||
2 0 | ||
7 1 | ||
3 1 | ||
8 0 | ||
4 0 | ||
9 1 | ||
|
||
# The subquery ordering is propagated to the aggregation. | ||
query T | ||
SELECT array_agg(i) FROM (SELECT * FROM generate_series(1, 5) i ORDER BY i%2 DESC, i) | ||
---- | ||
{1,3,5,2,4} | ||
|
||
# The input ordering is not propagated through joins. | ||
query II | ||
WITH tmp AS (SELECT * FROM generate_series(1, 2) x), | ||
tmp2 AS (SELECT * FROM generate_series(8, 12) i ORDER BY i % 5 ASC, i ASC) | ||
SELECT * FROM tmp, tmp2; | ||
---- | ||
1 8 | ||
1 9 | ||
1 10 | ||
1 11 | ||
1 12 | ||
2 8 | ||
2 9 | ||
2 10 | ||
2 11 | ||
2 12 | ||
|
||
# The input ordering is not propagated through joins. | ||
query II | ||
SELECT * | ||
FROM (SELECT * FROM generate_series(1, 2) x) tmp, | ||
(SELECT * FROM generate_series(8, 12) i ORDER BY i % 5 ASC, i ASC) tmp2; | ||
---- | ||
1 8 | ||
1 9 | ||
1 10 | ||
1 11 | ||
1 12 | ||
2 8 | ||
2 9 | ||
2 10 | ||
2 11 | ||
2 12 | ||
|
||
# Do not preserve the subquery ordering because the parent scope has its own | ||
# ordering. | ||
query I | ||
WITH tmp AS (SELECT * FROM generate_series(1, 10) i ORDER BY i % 5 ASC, i ASC) | ||
SELECT * FROM tmp ORDER BY i DESC; | ||
---- | ||
10 | ||
9 | ||
8 | ||
7 | ||
6 | ||
5 | ||
4 | ||
3 | ||
2 | ||
1 | ||
|
||
# Do not preserve the subquery ordering because the parent scope has its own | ||
# ordering. | ||
query I | ||
WITH tmp AS (SELECT * FROM generate_series(1, 10) i ORDER BY i % 5 ASC, i ASC) | ||
SELECT * FROM tmp ORDER BY i DESC; | ||
---- | ||
10 | ||
9 | ||
8 | ||
7 | ||
6 | ||
5 | ||
4 | ||
3 | ||
2 | ||
1 | ||
|
||
statement ok | ||
RESET propagate_input_ordering; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.