Details
-
Bug
-
Status: Confirmed (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.11, 11.4, 11.8, 12.3, 12.3.3
-
MariaDB 12.3.3 @2883bccc
Can also be reproduced on 12.1.2-MariaDB-ubu2404
Description
When a SELECT has two window functions and the second one uses OVER (PARTITION BY <constant> ORDER BY …), MariaDB's window-function computation coalesces both windows into a single filesort keyed only by the second window's (constant) ORDER BY and drops the first window's own ORDER BY. This might be unexpected behavior.
Please see the repro below:
CREATE TABLE t (id BIGINT); |
INSERT INTO t VALUES (-3),(-1),(0),(1),(2),(2),(NULL),(7); |
|
|
-- (1) THE BUG: window A = SUM(41) OVER (ORDER BY id<1 DESC) is corrupted by the presence of window B.
|
-- Correct (RANGE peers over key id<1: 3 rows key=1 ->123, 4 rows key=0 ->287, 1 NULL ->328):
|
-- {123,123,123, 287,287,287,287, 328} <- MySQL returns exactly this.
|
-- MariaDB actual: wrong AND run-order-dependent, e.g. {41,82,123, 246,246,246, 287, 328}.
|
SELECT id, |
SUM(41) OVER (ORDER BY id < 1 DESC) AS a, |
MAX(id) OVER (PARTITION BY '3' ORDER BY '3' DESC) AS b |
FROM t; |
-- Expected column a multiset = {123 x3, 287 x4, 328}; MariaDB gives garbage (frame sizes 1..8).
|
|
|
-- (2) CONTROL -- window A ALONE is correct (and equals MySQL):
|
SELECT id, SUM(41) OVER (ORDER BY id < 1 DESC) AS a FROM t; |
-- a = {123 x3, 287 x4, 328} ✓
|
|
|
-- (3) CONTROL -- window B PARTITION BY a REAL column (not a constant): window A is correct again:
|
SELECT id, |
SUM(41) OVER (ORDER BY id < 1 DESC) AS a, |
MAX(id) OVER (PARTITION BY id ORDER BY '3' DESC) AS b |
FROM t; |
-- a = {123 x3, 287 x4, 328} ✓ (the constant partition is the trigger)
|
|
|
-- (4) CONTROL -- window B with NO ORDER BY: window A is correct again:
|
SELECT id, |
SUM(41) OVER (ORDER BY id < 1 DESC) AS a, |
MAX(id) OVER (PARTITION BY '3') AS b |
FROM t; |
-- a = {123 x3, 287 x4, 328} ✓ (window B needs both PARTITION-BY-constant AND its own ORDER BY) |