Skip to content

Commit 6012ed9

Browse files
author
Dag Wanvik
committed
Bug#36746818 Sorting (PARTITION BY, ORDER BY) no longer stable for window functions: MacOS
Also fixes Bug#36795676 Test main.window_functions failing Also fixes Bug#36795679 Test main.window_jsonaggs failing A query with a window function in window_functions.test was non-deterministic. The window specification had a PARTITION clause, but no ORDER BY clause, so we saw different results on Linux than on MacOS. Ordering in MySQL doesn't necessarily do a stable sort, so we cannot rely on a specific row ordering inside a window partition unless we have a sufficient ORDER BY clause in the window spcification. Solution: remove non-deterministic query. Change-Id: I72f5a213119f51429e7eac58fd8c6aacef71a6d8
1 parent 57a551a commit 6012ed9

File tree

4 files changed

+23
-46
lines changed

4 files changed

+23
-46
lines changed

mysql-test/r/window_functions.result

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3716,32 +3716,6 @@ t FIRST_VALUE(t) OVER w
37163716
20:30:00 22:30:00
37173717
21:30:00 NULL
37183718
22:30:00 NULL
3719-
SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p );
3720-
p t FIRST_VALUE(t) OVER w
3721-
1 00:30:00 12:30:00
3722-
1 00:31:00 12:30:00
3723-
1 01:30:00 12:30:00
3724-
1 04:30:00 12:30:00
3725-
1 06:30:00 12:30:00
3726-
1 12:30:00 12:30:00
3727-
1 13:30:00 12:30:00
3728-
1 15:30:00 12:30:00
3729-
1 18:30:00 12:30:00
3730-
1 20:30:00 12:30:00
3731-
1 21:30:00 12:30:00
3732-
1 22:30:00 12:30:00
3733-
2 00:30:00 12:30:00
3734-
2 00:31:00 12:30:00
3735-
2 01:30:00 12:30:00
3736-
2 04:30:00 12:30:00
3737-
2 06:30:00 12:30:00
3738-
2 12:30:00 12:30:00
3739-
2 13:30:00 12:30:00
3740-
2 15:30:00 12:30:00
3741-
2 18:30:00 12:30:00
3742-
2 20:30:00 12:30:00
3743-
2 21:30:00 12:30:00
3744-
2 22:30:00 12:30:00
37453719
SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t);
37463720
p t FIRST_VALUE(t) OVER w
37473721
1 00:30:00 00:30:00

mysql-test/r/window_jsonaggs.result

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,27 @@ zxczcx 39 [null, "24324", "poipo", "efghi", "fsfd", "ffhfg", "efghi", "efghi", "
159159
uwrteu 43 [null, "24324", "poipo", "efghi", "fsfd", "ffhfg", "efghi", "efghi", "dfgdf", "dkfhk", null, "sdfsd", "wqweq", "zxczcx", "uwrteu"] [0, 1, 6, 10, 10, 13, 20, 21, 23, 28, 29, 30, 32, 39, 43]
160160
NULL 100 [null, "24324", "poipo", "efghi", "fsfd", "ffhfg", "efghi", "efghi", "dfgdf", "dkfhk", null, "sdfsd", "wqweq", "zxczcx", "uwrteu", null] [0, 1, 6, 10, 10, 13, 20, 21, 23, 28, 29, 30, 32, 39, 43, 100]
161161
# With ROLLUP
162+
# Originally, this query had just (ORDER BY f) - and all f
163+
# values are NULL - so it was non-deterministic and showed
164+
# differences across platforms due to unstable sorting. The new
165+
# frame spec captures the same data but deterministically
162166
SELECT i, f, JSON_ARRAYAGG(i) OVER w jarray_i, JSON_ARRAYAGG(f)
163167
OVER w jarray_f FROM t1 GROUP BY i,f WITH ROLLUP HAVING GROUPING(f)=1
164-
WINDOW w AS (ORDER BY f);
168+
WINDOW w AS (ORDER BY f, i ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
165169
i f jarray_i jarray_f
166-
1 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
167-
10 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
168-
11 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
169-
13 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
170-
15 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
171-
2 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
172-
3 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
173-
4 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
174-
5 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
175-
8 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
176-
9 NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
177-
NULL NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
178-
NULL NULL [null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null] [null, null, null, null, null, null, null, null, null, null, null, null, null]
170+
NULL NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
171+
NULL NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
172+
1 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
173+
2 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
174+
3 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
175+
4 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
176+
5 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
177+
8 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
178+
9 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
179+
10 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
180+
11 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
181+
13 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
182+
15 NULL [null, null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15] [null, null, null, null, null, null, null, null, null, null, null, null, null]
179183
# With other window functions
180184
SELECT i, f, RANK() OVER w rank1, NTH_VALUE(i,3)
181185
OVER w nth, LEAD(f, 2) OVER w lead1, LAG(f,2) OVER w lag1, JSON_ARRAYAGG(i)

mysql-test/t/window_functions.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,8 +1242,6 @@ SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t RANGE BETWEE
12421242
SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t ROWS BETWEEN 2 FOLLOWING AND 3 FOLLOWING);
12431243
SELECT t, FIRST_VALUE(t) OVER w FROM t_time WINDOW w AS (ORDER BY t RANGE BETWEEN INTERVAL 2 HOUR FOLLOWING AND INTERVAL 3 HOUR FOLLOWING);
12441244

1245-
--sorted_result
1246-
SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p );
12471245
SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t);
12481246
SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t DESC);
12491247
SELECT p, t, FIRST_VALUE(t) OVER w FROM t_time2 WINDOW w AS (PARTITION by p ORDER BY t ROWS 2 PRECEDING);

mysql-test/t/window_jsonaggs.test

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ SELECT s, j, JSON_ARRAYAGG(s) OVER w jarray_s, JSON_ARRAYAGG(j)
5151

5252
--echo # With ROLLUP
5353

54-
# The order here is undefined and implementation-dependent.
55-
--sorted_result
56-
--replace_regex /9, 10, 8, 11, 5, 4, 3, 13, 2, 1, 15, null, null/null, 1, 2, 3, 4, 5, 8, 9, 10, 11, 13, 15, null/
54+
--echo # Originally, this query had just (ORDER BY f) - and all f
55+
--echo # values are NULL - so it was non-deterministic and showed
56+
--echo # differences across platforms due to unstable sorting. The new
57+
--echo # frame spec captures the same data but deterministically
5758
SELECT i, f, JSON_ARRAYAGG(i) OVER w jarray_i, JSON_ARRAYAGG(f)
5859
OVER w jarray_f FROM t1 GROUP BY i,f WITH ROLLUP HAVING GROUPING(f)=1
59-
WINDOW w AS (ORDER BY f);
60+
WINDOW w AS (ORDER BY f, i ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING);
6061

6162
--echo # With other window functions
6263

0 commit comments

Comments
 (0)