1
0
mirror of https://github.com/MariaDB/server.git synced 2026-10-23 18:49:29 +03:00
Files
mariadb/mysql-test/main/win_bit.test
DerZc f49e838d36 MDEV-40689 Wrong result: BIT_AND/BIT_OR/BIT_XOR in WINDOW functions over a frame containing NULL
BIT_AND, BIT_OR, and BIT_XOR window functions can return incorrect
values as a sliding frame moves past NULL input rows.

Adding a NULL argument leaves the bit-aggregate state unchanged, but
removing that row unconditionally calls remove_as_window() with
val_int()'s value. The removal path therefore changes state for a row
that never contributed to the aggregate.

Evaluate the departing window argument once and retain its unsigned
value. Call remove_as_window() only when the evaluated argument is
non-NULL. Leave the existing incremental window algorithm and non-window
aggregation path in place.

The regression checks all three bit aggregates over ROWS BETWEEN 1
PRECEDING AND CURRENT ROW with interleaved NULL and non-NULL values,
including removal of a real zero and restoration of the neutral values
after the frame becomes all-NULL.

Bug report: https://jira.mariadb.org/browse/MDEV-40689
2026-09-28 14:44:23 +03:00

99 lines
2.8 KiB
Plaintext

create table t1 (
pk int primary key,
a int,
b int
);
create table t2 (
pk int primary key,
a int,
b int
);
insert into t1 values
( 1 , 0, 1),
( 2 , 0, 2),
( 3 , 1, 4),
( 4 , 1, 8),
( 5 , 2, 32),
( 6 , 2, 64),
( 7 , 2, 128),
( 8 , 2, 16);
insert into t2 values
( 1 , 0, 2),
( 2 , 0, 2),
( 3 , 1, 4),
( 4 , 1, 4),
( 5 , 2, 16),
( 6 , 2, 64),
( 7 , 2, 128),
( 8 , 2, 16);
--echo # Test bit functions on only one partition.
select pk, a, b,
bit_or(b) over (order by pk) as bit_or,
bit_and(b) over (order by pk) as bit_and,
bit_xor(b) over (order by pk) as bit_xor
from t1;
select pk, a, b,
bit_or(b) over (order by pk) as bit_or,
bit_and(b) over (order by pk) as bit_and,
bit_xor(b) over (order by pk) as bit_xor
from t2;
--echo # Test multiple partitions with bit functions.
select pk, a, b,
bit_or(b) over (partition by a order by pk) as bit_or,
bit_and(b) over (partition by a order by pk) as bit_and,
bit_xor(b) over (partition by a order by pk) as bit_xor
from t1;
select pk, a, b,
bit_or(b) over (partition by a order by pk) as bit_or,
bit_and(b) over (partition by a order by pk) as bit_and,
bit_xor(b) over (partition by a order by pk) as bit_xor
from t2;
--echo # Test remove function for bit functions using a sliding window.
select pk, a, b,
bit_or(b) over (partition by a order by pk ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) as bit_or,
bit_and(b) over (partition by a order by pk) as bit_and,
bit_xor(b) over (partition by a order by pk) as bit_xor
from t1;
select pk, a, b,
bit_or(b) over (partition by a order by pk) as bit_or,
bit_and(b) over (partition by a order by pk) as bit_and,
bit_xor(b) over (partition by a order by pk) as bit_xor
from t2;
#select pk, a, b, bit_or(b) over (order by a) as count from t1 order by a, pk;
#select pk, a, b, bit_and(b) over (order by a ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as count from t1 order by a, pk;
#select pk, a, b, bit_xor(b) over (order by a, pk ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as count from t2 order by pk;
#select pk, a, b, bit_or(b) over (order by a, pk ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as count from t2 order by pk;
#select pk, a, b, bit_and(b) over (order by a, pk ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) as count from t3 order by pk;
drop table t1;
drop table t2;
--echo #
--echo # MDEV-40689: Sliding bit aggregates with NULL inputs
--echo #
CREATE TABLE t1 (pk INT PRIMARY KEY,a INT,b INT);
INSERT INTO t1 VALUES (1,0,NULL),(2,0,1),(3,0,2),(4,0,NULL),(5,0,3),(6,1,7),(7,1,NULL),(8,1,6),(9,1,0),(10,1,5),(11,1,NULL),(12,1,NULL);
SELECT pk,b,BIT_AND(b) OVER w AS bit_and,BIT_OR(b) OVER w AS bit_or,BIT_XOR(b) OVER w AS bit_xor FROM t1 WINDOW w AS (ORDER BY pk ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) ORDER BY pk;
DROP TABLE t1;
--echo # End of 11.4 tests