summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorDavid Rowley2023-04-14 04:23:11 +0000
committerDavid Rowley2023-04-14 04:23:11 +0000
commit1c19e2863994ec15e496d21a9320aff95a389d0b (patch)
tree74b6fcc85947b476cdf974cf0c3b1997aaf92143 /src/test
parent9517e6e1dd60f38d55f3941d8b5182555c165ede (diff)
Fix incorrect partition pruning logic for boolean partitioned tables
The partition pruning logic assumed that "b IS NOT true" was exactly the same as "b IS FALSE". This is not the case when considering NULL values. Fix this so we correctly include any partition which could hold NULL values for the NOT case. Additionally, this fixes a bug in the partition pruning code which handles partitioned tables partitioned like ((NOT boolcol)). This is a seemingly unlikely schema design, and it was untested and also broken. Here we add tests for the ((NOT boolcol)) case and insert some actual data into those tables and verify we do get the correct rows back when running queries. I've also adjusted the existing boolpart tests to include some data and verify we get the correct results too. Both the bugs being fixed here could lead to incorrect query results with fewer rows being returned than expected. No additional rows could have been returned accidentally. In passing, remove needless ternary expression. It's more simple just to pass !is_not_clause to makeBoolConst(). It makes sense to do this so the code is consistent with the bug fix in the "else if" condition just below. David Kimura did submit a patch to fix the first of the issues here, but that's not what's being committed here. Reported-by: David Kimura Reviewed-by: Richard Guo, David Kimura Discussion: https://postgr.es/m/CAHnPFjQ5qxs6J_p+g8=ww7GQvfn71_JE+Tygj0S7RdRci1uwPw@mail.gmail.com Backpatch-through: 11, all supported versions
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/partition_prune.out227
-rw-r--r--src/test/regress/sql/partition_prune.sql38
2 files changed, 254 insertions, 11 deletions
diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out
index 28ce692eea2..83148a5d14e 100644
--- a/src/test/regress/expected/partition_prune.out
+++ b/src/test/regress/expected/partition_prune.out
@@ -1043,6 +1043,7 @@ create table boolpart (a bool) partition by list (a);
create table boolpart_default partition of boolpart default;
create table boolpart_t partition of boolpart for values in ('true');
create table boolpart_f partition of boolpart for values in ('false');
+insert into boolpart values (true), (false), (null);
explain (costs off) select * from boolpart where a in (true, false);
QUERY PLAN
------------------------------------------------
@@ -1077,22 +1078,27 @@ explain (costs off) select * from boolpart where a is true or a is not true;
Filter: ((a IS TRUE) OR (a IS NOT TRUE))
-> Seq Scan on boolpart_t
Filter: ((a IS TRUE) OR (a IS NOT TRUE))
-(5 rows)
+ -> Seq Scan on boolpart_default
+ Filter: ((a IS TRUE) OR (a IS NOT TRUE))
+(7 rows)
explain (costs off) select * from boolpart where a is not true;
- QUERY PLAN
----------------------------------
+ QUERY PLAN
+------------------------------------
Append
-> Seq Scan on boolpart_f
Filter: (a IS NOT TRUE)
-(3 rows)
+ -> Seq Scan on boolpart_default
+ Filter: (a IS NOT TRUE)
+(5 rows)
explain (costs off) select * from boolpart where a is not true and a is not false;
- QUERY PLAN
---------------------------
- Result
- One-Time Filter: false
-(2 rows)
+ QUERY PLAN
+--------------------------------------------------------
+ Append
+ -> Seq Scan on boolpart_default
+ Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE))
+(3 rows)
explain (costs off) select * from boolpart where a is unknown;
QUERY PLAN
@@ -1118,6 +1124,207 @@ explain (costs off) select * from boolpart where a is not unknown;
Filter: (a IS NOT UNKNOWN)
(7 rows)
+select * from boolpart where a in (true, false);
+ a
+---
+ f
+ t
+(2 rows)
+
+select * from boolpart where a = false;
+ a
+---
+ f
+(1 row)
+
+select * from boolpart where not a = false;
+ a
+---
+ t
+(1 row)
+
+select * from boolpart where a is true or a is not true;
+ a
+---
+ f
+ t
+
+(3 rows)
+
+select * from boolpart where a is not true;
+ a
+---
+ f
+
+(2 rows)
+
+select * from boolpart where a is not true and a is not false;
+ a
+---
+
+(1 row)
+
+select * from boolpart where a is unknown;
+ a
+---
+
+(1 row)
+
+select * from boolpart where a is not unknown;
+ a
+---
+ f
+ t
+(2 rows)
+
+-- inverse boolean partitioning - a seemingly unlikely design, but we've got
+-- code for it, so we'd better test it.
+create table iboolpart (a bool) partition by list ((not a));
+create table iboolpart_default partition of iboolpart default;
+create table iboolpart_f partition of iboolpart for values in ('true');
+create table iboolpart_t partition of iboolpart for values in ('false');
+insert into iboolpart values (true), (false), (null);
+explain (costs off) select * from iboolpart where a in (true, false);
+ QUERY PLAN
+------------------------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: (a = ANY ('{t,f}'::boolean[]))
+ -> Seq Scan on iboolpart_f
+ Filter: (a = ANY ('{t,f}'::boolean[]))
+ -> Seq Scan on iboolpart_default
+ Filter: (a = ANY ('{t,f}'::boolean[]))
+(7 rows)
+
+explain (costs off) select * from iboolpart where a = false;
+ QUERY PLAN
+-------------------------------
+ Append
+ -> Seq Scan on iboolpart_f
+ Filter: (NOT a)
+(3 rows)
+
+explain (costs off) select * from iboolpart where not a = false;
+ QUERY PLAN
+-------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: a
+(3 rows)
+
+explain (costs off) select * from iboolpart where a is true or a is not true;
+ QUERY PLAN
+--------------------------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: ((a IS TRUE) OR (a IS NOT TRUE))
+ -> Seq Scan on iboolpart_f
+ Filter: ((a IS TRUE) OR (a IS NOT TRUE))
+ -> Seq Scan on iboolpart_default
+ Filter: ((a IS TRUE) OR (a IS NOT TRUE))
+(7 rows)
+
+explain (costs off) select * from iboolpart where a is not true;
+ QUERY PLAN
+-------------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: (a IS NOT TRUE)
+ -> Seq Scan on iboolpart_f
+ Filter: (a IS NOT TRUE)
+ -> Seq Scan on iboolpart_default
+ Filter: (a IS NOT TRUE)
+(7 rows)
+
+explain (costs off) select * from iboolpart where a is not true and a is not false;
+ QUERY PLAN
+--------------------------------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE))
+ -> Seq Scan on iboolpart_f
+ Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE))
+ -> Seq Scan on iboolpart_default
+ Filter: ((a IS NOT TRUE) AND (a IS NOT FALSE))
+(7 rows)
+
+explain (costs off) select * from iboolpart where a is unknown;
+ QUERY PLAN
+-------------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: (a IS UNKNOWN)
+ -> Seq Scan on iboolpart_f
+ Filter: (a IS UNKNOWN)
+ -> Seq Scan on iboolpart_default
+ Filter: (a IS UNKNOWN)
+(7 rows)
+
+explain (costs off) select * from iboolpart where a is not unknown;
+ QUERY PLAN
+-------------------------------------
+ Append
+ -> Seq Scan on iboolpart_t
+ Filter: (a IS NOT UNKNOWN)
+ -> Seq Scan on iboolpart_f
+ Filter: (a IS NOT UNKNOWN)
+ -> Seq Scan on iboolpart_default
+ Filter: (a IS NOT UNKNOWN)
+(7 rows)
+
+select * from iboolpart where a in (true, false);
+ a
+---
+ t
+ f
+(2 rows)
+
+select * from iboolpart where a = false;
+ a
+---
+ f
+(1 row)
+
+select * from iboolpart where not a = false;
+ a
+---
+ t
+(1 row)
+
+select * from iboolpart where a is true or a is not true;
+ a
+---
+ t
+ f
+
+(3 rows)
+
+select * from iboolpart where a is not true;
+ a
+---
+ f
+
+(2 rows)
+
+select * from iboolpart where a is not true and a is not false;
+ a
+---
+
+(1 row)
+
+select * from iboolpart where a is unknown;
+ a
+---
+
+(1 row)
+
+select * from iboolpart where a is not unknown;
+ a
+---
+ t
+ f
+(2 rows)
+
create table boolrangep (a bool, b bool, c int) partition by range (a,b,c);
create table boolrangep_tf partition of boolrangep for values from ('true', 'false', 0) to ('true', 'false', 100);
create table boolrangep_ft partition of boolrangep for values from ('false', 'true', 0) to ('false', 'true', 100);
@@ -1524,7 +1731,7 @@ explain (costs off) select * from rparted_by_int2 where a > 100000000000000;
Filter: (a > '100000000000000'::bigint)
(3 rows)
-drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
+drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, iboolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
--
-- Test Partition pruning for HASH partitioning
--
diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql
index 9b94ac50af5..a1ad757bfbd 100644
--- a/src/test/regress/sql/partition_prune.sql
+++ b/src/test/regress/sql/partition_prune.sql
@@ -150,6 +150,7 @@ create table boolpart (a bool) partition by list (a);
create table boolpart_default partition of boolpart default;
create table boolpart_t partition of boolpart for values in ('true');
create table boolpart_f partition of boolpart for values in ('false');
+insert into boolpart values (true), (false), (null);
explain (costs off) select * from boolpart where a in (true, false);
explain (costs off) select * from boolpart where a = false;
@@ -160,6 +161,41 @@ explain (costs off) select * from boolpart where a is not true and a is not fals
explain (costs off) select * from boolpart where a is unknown;
explain (costs off) select * from boolpart where a is not unknown;
+select * from boolpart where a in (true, false);
+select * from boolpart where a = false;
+select * from boolpart where not a = false;
+select * from boolpart where a is true or a is not true;
+select * from boolpart where a is not true;
+select * from boolpart where a is not true and a is not false;
+select * from boolpart where a is unknown;
+select * from boolpart where a is not unknown;
+
+-- inverse boolean partitioning - a seemingly unlikely design, but we've got
+-- code for it, so we'd better test it.
+create table iboolpart (a bool) partition by list ((not a));
+create table iboolpart_default partition of iboolpart default;
+create table iboolpart_f partition of iboolpart for values in ('true');
+create table iboolpart_t partition of iboolpart for values in ('false');
+insert into iboolpart values (true), (false), (null);
+
+explain (costs off) select * from iboolpart where a in (true, false);
+explain (costs off) select * from iboolpart where a = false;
+explain (costs off) select * from iboolpart where not a = false;
+explain (costs off) select * from iboolpart where a is true or a is not true;
+explain (costs off) select * from iboolpart where a is not true;
+explain (costs off) select * from iboolpart where a is not true and a is not false;
+explain (costs off) select * from iboolpart where a is unknown;
+explain (costs off) select * from iboolpart where a is not unknown;
+
+select * from iboolpart where a in (true, false);
+select * from iboolpart where a = false;
+select * from iboolpart where not a = false;
+select * from iboolpart where a is true or a is not true;
+select * from iboolpart where a is not true;
+select * from iboolpart where a is not true and a is not false;
+select * from iboolpart where a is unknown;
+select * from iboolpart where a is not unknown;
+
create table boolrangep (a bool, b bool, c int) partition by range (a,b,c);
create table boolrangep_tf partition of boolrangep for values from ('true', 'false', 0) to ('true', 'false', 100);
create table boolrangep_ft partition of boolrangep for values from ('false', 'true', 0) to ('false', 'true', 100);
@@ -281,7 +317,7 @@ create table rparted_by_int2_maxvalue partition of rparted_by_int2 for values fr
-- all partitions but rparted_by_int2_maxvalue pruned
explain (costs off) select * from rparted_by_int2 where a > 100000000000000;
-drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
+drop table lp, coll_pruning, rlp, mc3p, mc2p, boolpart, iboolpart, boolrangep, rp, coll_pruning_multi, like_op_noprune, lparted_by_int2, rparted_by_int2;
--
-- Test Partition pruning for HASH partitioning