From 6b5b7b0c933452a3fab9bb13d9fef4fa9eb4337e Mon Sep 17 00:00:00 2001 From: litianfeng1998 Date: Fri, 5 Jun 2026 10:59:00 +0800 Subject: [PATCH 1/5] [feature] Convert count on not null column to count(0) Problem: ======= If a counted column is guaranteed NOT NULL (e.g. a NOT NULL column not on the inner side of an outer join), count(col) is equivalent to count(0), and the column processing overhead can be saved when it is not referenced elsewhere. Solution: ======= Add a per-field reference count (TABLE::fields_use_count) and clear the read_set bit of fields no longer referenced; convert count(not_null_col) to count(0) during optimization. New sysvar: txsql_count_conversion_enabled, default ON New statvar: Txsql_implicit_convert_to_count_star, Txsql_total_count_function --- mysql-test/r/txsql_convert_to_count_star.pq | 294 ++++++++++++++++++ .../r/txsql_convert_to_count_star.result | 250 +++++++++++++++ mysql-test/t/txsql_convert_to_count_star.test | 109 +++++++ sql/CMakeLists.txt | 1 + sql/item_sum.h | 2 + sql/join_optimizer/explain_access_path.cc | 5 + sql/mysqld.cc | 6 + sql/parallel_execution/px_item.cc | 10 +- sql/sql_count_conversion.cc | 66 ++++ sql/sql_count_conversion.h | 14 + sql/sql_optimizer.cc | 12 + sql/sys_vars.cc | 7 + sql/system_variables.h | 5 + sql/table.cc | 25 +- sql/table.h | 26 ++ unittest/gunit/fake_table.h | 1 + 16 files changed, 830 insertions(+), 3 deletions(-) create mode 100644 mysql-test/r/txsql_convert_to_count_star.pq create mode 100644 mysql-test/r/txsql_convert_to_count_star.result create mode 100644 mysql-test/t/txsql_convert_to_count_star.test create mode 100644 sql/sql_count_conversion.cc create mode 100644 sql/sql_count_conversion.h diff --git a/mysql-test/r/txsql_convert_to_count_star.pq b/mysql-test/r/txsql_convert_to_count_star.pq new file mode 100644 index 00000000000..94381c289d2 --- /dev/null +++ b/mysql-test/r/txsql_convert_to_count_star.pq @@ -0,0 +1,294 @@ +create table t1(a int, b int); +create table t2(a int not null, b int not null); +INSERT INTO t1 VALUES (1, 1), (2, NULL), (3, 3); +INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3); +ANALYZE TABLE t1,t2; +Table Op Msg_type Msg_text +test.t1 analyze status OK +test.t2 analyze status OK +set @saved_value=@@txsql_count_conversion_enabled; +set @@txsql_count_conversion_enabled = 0; +flush status; +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(*) from t1 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t1 (cost=0.55 rows=3) + +select count(*) from t1 where a > 1; +count(*) +2 +EXPLAIN format=tree select count(b) from t1 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(t1.b) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t1 (cost=0.55 rows=3) + +select count(b) from t1 where a > 1; +count(b) +1 +EXPLAIN format=tree select count(*) from t2 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select count(*) from t2 where a > 1; +count(*) +2 +EXPLAIN format=tree select count(b) from t2 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(t2.b) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 where a > 1; +count(b) +2 +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +EXPLAIN +-> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) + -> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) + -> Parallel table scan on t2 (cost=0.55 rows=3) + -> Hash + -> Table scan on t1 (cost=0.18 rows=3) + +select count(t2.b) from t2 left join t1 on t1.a = t2.a; +count(t2.b) +3 +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +EXPLAIN +-> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) + -> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) + -> Parallel table scan on t1 (cost=0.55 rows=3) + -> Hash + -> Table scan on t2 (cost=0.18 rows=3) + +select count(t2.b) from t1 left join t2 on t1.a = t2.a; +count(t2.b) +3 +EXPLAIN format=tree select count(b) from t2 group by a; +EXPLAIN +-> Table scan on + -> Final Aggregate using temporary table + -> PX Receiver (slice: 0; workers: 1) + -> PX Sender (slice: 1; workers: 3) + -> Table scan on + -> Aggregate using temporary table + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 group by a; +count(b) +1 +1 +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +set @@txsql_count_conversion_enabled = 1; +EXPLAIN format=tree select count(*) from t1 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t1 (cost=0.55 rows=3) + +select count(*) from t1 where a > 1; +count(*) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 5 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(b) from t1 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(t1.b) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t1 (cost=0.55 rows=3) + +select count(b) from t1 where a > 1; +count(b) +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 10 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(*) from t2 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select count(*) from t2 where a > 1; +count(*) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 15 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(b) from t2 where a > 1; +EXPLAIN +-> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) + -> Aggregate: count(0) using conversion for count of not null column (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 where a > 1; +count(b) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 20 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 5 +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +EXPLAIN +-> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) + -> Aggregate: count(0) using conversion for count of not null column (cost=2.23 rows=1) + -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) + -> Parallel table scan on t2 (cost=0.55 rows=3) + -> Hash + -> Table scan on t1 (cost=0.18 rows=3) + +select count(t2.b) from t2 left join t1 on t1.a = t2.a; +count(t2.b) +3 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 25 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 10 +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +EXPLAIN +-> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) + -> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) + -> Parallel table scan on t1 (cost=0.55 rows=3) + -> Hash + -> Table scan on t2 (cost=0.18 rows=3) + +select count(t2.b) from t1 left join t2 on t1.a = t2.a; +count(t2.b) +3 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 30 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 10 +EXPLAIN format=tree select count(b) from t2 group by a; +EXPLAIN +-> Table scan on + -> Final Aggregate using temporary table + -> PX Receiver (slice: 0; workers: 1) + -> PX Sender (slice: 1; workers: 3) + -> Table scan on + -> Aggregate using temporary table + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 group by a; +count(b) +1 +1 +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 35 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 15 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 35 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 15 +# --include save_and_set_px.inc +set @@txsql_count_conversion_enabled=0; +EXPLAIN format=tree select avg(b) from t2; +EXPLAIN +-> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) + -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select avg(b) from t2; +avg(b) +2.0000 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +set @@txsql_count_conversion_enabled=1; +EXPLAIN format=tree select avg(b) from t2; +EXPLAIN +-> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) + -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select avg(b) from t2; +avg(b) +2.0000 +# --include set_px_to_saved.inc +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 35 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 15 +set @@txsql_count_conversion_enabled=@saved_value; +DROP TABLE t1,t2; diff --git a/mysql-test/r/txsql_convert_to_count_star.result b/mysql-test/r/txsql_convert_to_count_star.result new file mode 100644 index 00000000000..6f6e4b561ea --- /dev/null +++ b/mysql-test/r/txsql_convert_to_count_star.result @@ -0,0 +1,250 @@ +create table t1(a int, b int); +create table t2(a int not null, b int not null); +INSERT INTO t1 VALUES (1, 1), (2, NULL), (3, 3); +INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3); +ANALYZE TABLE t1,t2; +Table Op Msg_type Msg_text +test.t1 analyze status OK +test.t2 analyze status OK +set @saved_value=@@txsql_count_conversion_enabled; +set @@txsql_count_conversion_enabled = 0; +flush status; +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(*) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(*) from t1 where a > 1; +count(*) +2 +EXPLAIN format=tree select count(b) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(t1.b) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(b) from t1 where a > 1; +count(b) +1 +EXPLAIN format=tree select count(*) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(*) from t2 where a > 1; +count(*) +2 +EXPLAIN format=tree select count(b) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(t2.b) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 where a > 1; +count(b) +2 +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) + -> Table scan on t2 (cost=0.55 rows=3) + -> Hash + -> Table scan on t1 (cost=0.18 rows=3) + +select count(t2.b) from t2 left join t1 on t1.a = t2.a; +count(t2.b) +3 +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) + -> Table scan on t1 (cost=0.55 rows=3) + -> Hash + -> Table scan on t2 (cost=0.18 rows=3) + +select count(t2.b) from t1 left join t2 on t1.a = t2.a; +count(t2.b) +3 +EXPLAIN format=tree select count(b) from t2 group by a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 group by a; +count(b) +1 +1 +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +set @@txsql_count_conversion_enabled = 1; +EXPLAIN format=tree select count(*) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(*) from t1 where a > 1; +count(*) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 2 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(b) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(t1.b) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(b) from t1 where a > 1; +count(b) +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 4 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(*) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(*) from t2 where a > 1; +count(*) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 6 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(b) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(0) using conversion for count of not null column (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 where a > 1; +count(b) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 8 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 2 +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(0) using conversion for count of not null column (cost=2.23 rows=1) + -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) + -> Table scan on t2 (cost=0.55 rows=3) + -> Hash + -> Table scan on t1 (cost=0.18 rows=3) + +select count(t2.b) from t2 left join t1 on t1.a = t2.a; +count(t2.b) +3 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 10 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 4 +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) + -> Table scan on t1 (cost=0.55 rows=3) + -> Hash + -> Table scan on t2 (cost=0.18 rows=3) + +select count(t2.b) from t1 left join t2 on t1.a = t2.a; +count(t2.b) +3 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 12 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 4 +EXPLAIN format=tree select count(b) from t2 group by a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 group by a; +count(b) +1 +1 +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 14 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 6 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 14 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 6 +# --include save_and_set_px.inc +set @@txsql_count_conversion_enabled=0; +EXPLAIN format=tree select avg(b) from t2; +EXPLAIN +-> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) + -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select avg(b) from t2; +avg(b) +2.0000 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +set @@txsql_count_conversion_enabled=1; +EXPLAIN format=tree select avg(b) from t2; +EXPLAIN +-> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) + -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) + -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select avg(b) from t2; +avg(b) +2.0000 +# --include set_px_to_saved.inc +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 14 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 6 +set @@txsql_count_conversion_enabled=@saved_value; +DROP TABLE t1,t2; diff --git a/mysql-test/t/txsql_convert_to_count_star.test b/mysql-test/t/txsql_convert_to_count_star.test new file mode 100644 index 00000000000..6101a39802d --- /dev/null +++ b/mysql-test/t/txsql_convert_to_count_star.test @@ -0,0 +1,109 @@ + +create table t1(a int, b int); +create table t2(a int not null, b int not null); + +INSERT INTO t1 VALUES (1, 1), (2, NULL), (3, 3); +INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3); + +ANALYZE TABLE t1,t2; + +set @saved_value=@@txsql_count_conversion_enabled; +set @@txsql_count_conversion_enabled = 0; +flush status; +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(*) from t1 where a > 1; +select count(*) from t1 where a > 1; + +EXPLAIN format=tree select count(b) from t1 where a > 1; +select count(b) from t1 where a > 1; + +EXPLAIN format=tree select count(*) from t2 where a > 1; +select count(*) from t2 where a > 1; + +EXPLAIN format=tree select count(b) from t2 where a > 1; +select count(b) from t2 where a > 1; + +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +select count(t2.b) from t2 left join t1 on t1.a = t2.a; + +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +select count(t2.b) from t1 left join t2 on t1.a = t2.a; + +EXPLAIN format=tree select count(b) from t2 group by a; +select count(b) from t2 group by a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +set @@txsql_count_conversion_enabled = 1; + +EXPLAIN format=tree select count(*) from t1 where a > 1; +select count(*) from t1 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(b) from t1 where a > 1; +select count(b) from t1 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(*) from t2 where a > 1; +select count(*) from t2 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(b) from t2 where a > 1; +select count(b) from t2 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +select count(t2.b) from t2 left join t1 on t1.a = t2.a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +select count(t2.b) from t1 left join t2 on t1.a = t2.a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(b) from t2 group by a; +select count(b) from t2 group by a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +--source suite/parallel_execution/inc/save_and_set_px.inc +connect(conn1,localhost,root,,); +set @@txsql_count_conversion_enabled=0; +connection conn1; +EXPLAIN format=tree select avg(b) from t2; +select avg(b) from t2; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +set @@txsql_count_conversion_enabled=1; +EXPLAIN format=tree select avg(b) from t2; +select avg(b) from t2; +disconnect conn1; + +connection default; +--source suite/parallel_execution/inc/set_px_to_saved.inc + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +set @@txsql_count_conversion_enabled=@saved_value; +DROP TABLE t1,t2; diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt index bc08aedf1ae..1e4cfb72d6a 100644 --- a/sql/CMakeLists.txt +++ b/sql/CMakeLists.txt @@ -594,6 +594,7 @@ SET(SQL_SHARED_SOURCES sql_cmd_srs.cc sql_connect.cc sql_constraint.cc + sql_count_conversion.cc sql_cursor.cc sql_data_change.cc sql_db.cc diff --git a/sql/item_sum.h b/sql/item_sum.h index 804efe47fb0..ad417009b2a 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -1155,6 +1155,8 @@ class Item_sum_count : public Item_sum_int { return (sum_func() == COUNT_DISTINCT_FUNC) ? false : true; } #endif /* defined(HAVE_PX) */ + // True if it is count(0) converted from count(non_null_field) + bool is_coverted_count_zero{false}; }; /* Item to get the value of a stored sum function */ diff --git a/sql/join_optimizer/explain_access_path.cc b/sql/join_optimizer/explain_access_path.cc index 0ad169c27e4..17857dcdfdc 100644 --- a/sql/join_optimizer/explain_access_path.cc +++ b/sql/join_optimizer/explain_access_path.cc @@ -999,6 +999,11 @@ ExplainData ExplainAccessPath(const AccessPath *path, JOIN *join, } else { ret += ItemToString(*item); } + + if ((*item)->sum_func() == Item_sum::COUNT_FUNC && + ((Item_sum_count *)(*item))->is_coverted_count_zero) { + ret += " using conversion for count of not null column"; + } } description.push_back(move(ret)); children.push_back({path->aggregate().child}); diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 8b8fbf65682..d04ec00669e 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -10392,6 +10392,12 @@ SHOW_VAR status_vars[] = { {"Handler_read_rnd_next", (char *)offsetof(System_status_var, ha_read_rnd_next_count), SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, + {"Txsql_implicit_convert_to_count_star", + (char *)offsetof(System_status_var, txsql_implicit_convert_to_count_zero), + SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, + {"Txsql_total_count_function", + (char *)offsetof(System_status_var, txsql_total_count_function), + SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, {"Handler_rollback", (char *)offsetof(System_status_var, ha_rollback_count), SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, {"Handler_savepoint", diff --git a/sql/parallel_execution/px_item.cc b/sql/parallel_execution/px_item.cc index 120f918c617..e4328c6ea01 100644 --- a/sql/parallel_execution/px_item.cc +++ b/sql/parallel_execution/px_item.cc @@ -34,6 +34,7 @@ #include "sql/filesort.h" #include "sql/item.h" #include "sql/item_sum.h" +#include "sql/sql_count_conversion.h" #include "sql/sql_optimizer.h" #include "sql/sql_executor.h" #include "sql/log.h" @@ -219,7 +220,14 @@ Item_sum **Item_sum_avg::pq_rebuild_item(THD *thd, Query_block *select) { } Item_sum *new_sum_item = new (thd->mem_root) Item_sum_sum(POS(), args[0], has_with_distinct(), m_window); - Item_sum *new_count_item = new (thd->mem_root) Item_sum_count(POS(), args[0], m_window); + Item_sum *new_count_item = nullptr; + if (thd->variables.txsql_count_conversion_enabled && can_convert_to_zero(args[0])) { + Item *zero_args = new (thd->mem_root) Item_int(int32{0}, 1); + if (zero_args == nullptr) return nullptr; + new_count_item = new (thd->mem_root) Item_sum_count(POS(), zero_args, m_window); + } else { + new_count_item = new (thd->mem_root) Item_sum_count(POS(), args[0], m_window); + } if (new_sum_item == nullptr || new_sum_item->pq_copy_item(thd, select, this)) { return nullptr; } diff --git a/sql/sql_count_conversion.cc b/sql/sql_count_conversion.cc new file mode 100644 index 00000000000..0d7d84a802f --- /dev/null +++ b/sql/sql_count_conversion.cc @@ -0,0 +1,66 @@ +#include "sql/sql_count_conversion.h" +#include "sql/item.h" +#include "sql/item_sum.h" +#include "sql/parallel_execution/px_interface.h" +#include "sql/opt_trace.h" // Opt_trace_object +#include "sql/opt_trace_context.h" + +void inc_txsql_implicit_convert_to_count_zero() { + current_thd->status_var.txsql_implicit_convert_to_count_zero++; +} + +void inc_txsql_total_count_function() { + current_thd->status_var.txsql_total_count_function++; +} + +bool can_convert_to_zero(Item *item) { + return (item && item->type() == Item::FIELD_ITEM && !item->is_nullable()); +} + +bool count_col_conversion(THD *thd, Item_sum_count *item_count) { + inc_txsql_total_count_function(); + Opt_trace_context *const trace = &thd->opt_trace; + + if (can_convert_to_zero(item_count->get_arg(0))) { + Field *field = ((Item_field *)item_count->get_arg(0))->field; + TABLE *table = field->table; + table->release_field(field->field_index()); + item_count->set_arg(thd, 0, new (thd->mem_root) Item_int(int32{0}, 1)); + item_count->is_coverted_count_zero = true; + inc_txsql_implicit_convert_to_count_zero(); + Opt_trace_object trace_cnt_conversion(trace); + trace_cnt_conversion.add("using count conversion", true); + } + + return false; +} + +bool count_col_conversion(THD *thd, const mem_root_deque &fields) { + DBUG_TRACE; + + assert(thd && thd->variables.txsql_count_conversion_enabled); + for (Item *item : fields) { + if (item->type() == Item::SUM_FUNC_ITEM && !item->m_is_window_function) { + if (item->is_outer_reference()) { + continue; + } + + /* + MySQL will convert count(*) to count(0), and 0 has two meanings:the field + value is not considered and it will not be NULL.Therefore, count(*) counts + all values that contain NULL. Count(col), will add col to read_set, that is, + need to copy col to TABLE::record[0], and will not count the null value in + col. However, if col can ensure that it is not NULL, for example, there is + a not null restriction in the table definition, and col does not belong to + the inner table of left join. Then count(col) is equivalent to count(0). + */ + Item_sum *item_sum = down_cast(item); + if (item_sum->real_sum_func() == Item_sum::COUNT_FUNC && + count_col_conversion(thd, (Item_sum_count *)item_sum)) { + return true; + } + } + if (thd->is_error()) return true; + } + return false; +} \ No newline at end of file diff --git a/sql/sql_count_conversion.h b/sql/sql_count_conversion.h new file mode 100644 index 00000000000..d6dcfd19bd5 --- /dev/null +++ b/sql/sql_count_conversion.h @@ -0,0 +1,14 @@ +#ifndef SQL_COUNT_CONVERSION_INCLUDED +#define SQL_COUNT_CONVERSION_INCLUDED + +class Item; +class Item_sum_count; +class THD; +template +class mem_root_deque; + +extern bool can_convert_to_zero(Item *item); +extern bool count_col_conversion(THD *thd, const mem_root_deque &fields); +extern bool count_col_conversion(THD *thd, Item_sum_count *item_count); + +#endif /* SQL_SELECT_INCLUDED */ \ No newline at end of file diff --git a/sql/sql_optimizer.cc b/sql/sql_optimizer.cc index 8cf83799a28..f92c43ff858 100644 --- a/sql/sql_optimizer.cc +++ b/sql/sql_optimizer.cc @@ -97,6 +97,7 @@ #include "sql/sql_class.h" #include "sql/sql_const.h" #include "sql/sql_const_folding.h" +#include "sql/sql_count_conversion.h" #include "sql/sql_error.h" #include "sql/sql_join_buffer.h" // JOIN_CACHE #include "sql/sql_planner.h" // calculate_condition_filter @@ -446,6 +447,17 @@ bool JOIN::optimize(bool finalize_access_paths) { return true; } + /* + Try to optimize count(col) to count(0) if possible. + */ + if (thd->variables.txsql_count_conversion_enabled && + thd->lex->sql_command == SQLCOM_SELECT && + count_col_conversion(thd, *fields)) { + error = 1; + DBUG_PRINT("error", ("Error from count_col_conversion")); + return true; + } + /* Try to optimize count(*), min() and max() to const fields if there is implicit grouping (aggregate functions but no diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc index 8f4c7e45693..2c2548cc083 100644 --- a/sql/sys_vars.cc +++ b/sql/sys_vars.cc @@ -8908,6 +8908,13 @@ static Sys_var_bool Sys_txsql_pread_count_enabled( CMD_LINE(OPT_ARG), DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr)); +static Sys_var_bool Sys_txsql_count_conversion_enabled( + "txsql_count_conversion_enabled", + "Allow implicit conversion of count on not null column to count(0)", + HINT_UPDATEABLE SESSION_VAR(txsql_count_conversion_enabled), + CMD_LINE(OPT_ARG), DEFAULT(true), + NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr)); + #ifdef HAVE_TDSQL static Sys_var_bool Sys_g_sqlAsyn( "sqlasyn", diff --git a/sql/system_variables.h b/sql/system_variables.h index 3a431fb8a2e..c6d1686aec3 100644 --- a/sql/system_variables.h +++ b/sql/system_variables.h @@ -552,6 +552,8 @@ struct System_variables { #endif /* HAVE_PX */ /// @sa Sys_txsql_pread_count_enabled bool txsql_pread_count_enabled; + /// @sa Sys_txsql_count_conversion_enabled + bool txsql_count_conversion_enabled; }; /** @@ -624,6 +626,9 @@ struct System_status_var { /* Number of statements sent from the client. */ ulonglong questions; + ulonglong txsql_implicit_convert_to_count_zero; + ulonglong txsql_total_count_function; + /// How many queries have been executed on a secondary storage engine. ulonglong secondary_engine_execution_count; diff --git a/sql/table.cc b/sql/table.cc index 77bdb9386b6..77a10a27666 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -3247,6 +3247,9 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, outparam->no_replicate = false; } + outparam->fields_use_count.clear(); + outparam->fields_use_count.resize(share->fields); + /* Increment the opened_tables counter, only when open flags set. */ if (db_stat) thd->status_var.opened_tables++; @@ -4127,6 +4130,9 @@ void TABLE::init(THD *thd, TABLE_LIST *tl) { if (!pos_in_table_list->prelocking_placeholder) { bind_value_generators_to_fields(); } + + fields_use_count.clear(); + fields_use_count.resize(s->fields); } /** @@ -4163,6 +4169,8 @@ void TABLE::reset() { m_parallel_scan = false; m_parallel_workers = 0; #endif /* defined(HAVE_PX) */ + fields_use_count.clear(); + fields_use_count.resize(s->fields); } /** @@ -4229,6 +4237,8 @@ bool TABLE::init_tmp_table(THD *thd, TABLE_SHARE *share, MEM_ROOT *m_root, #ifndef NDEBUG set_tmp_table_seq_id(thd->get_tmp_table_seq_id()); #endif + fields_use_count.clear(); + fields_use_count.resize(s->fields); return false; } @@ -5454,6 +5464,7 @@ void TABLE::mark_column_used(Field *field, enum enum_mark_columns mark) { case MARK_COLUMNS_READ: { Key_map part_of_key = field->part_of_key; bitmap_set_bit(read_set, field->field_index()); + use_field(field->field_index()); part_of_key.merge(field->part_of_prefixkey); covering_keys.intersect(part_of_key); @@ -5471,6 +5482,7 @@ void TABLE::mark_column_used(Field *field, enum enum_mark_columns mark) { case MARK_COLUMNS_TEMP: bitmap_set_bit(read_set, field->field_index()); + use_field(field->field_index()); if (field->is_virtual_gcol()) mark_gcol_in_maps(field); break; } @@ -5552,6 +5564,7 @@ void TABLE::mark_auto_increment_column() { store() to check overflow of auto_increment values */ bitmap_set_bit(read_set, found_next_number_field->field_index()); + use_field(found_next_number_field->field_index()); bitmap_set_bit(write_set, found_next_number_field->field_index()); if (s->next_number_keypart) mark_columns_used_by_index_no_reset(s->next_number_index, read_set); @@ -5586,6 +5599,7 @@ void TABLE::mark_columns_needed_for_delete(THD *thd) { for (reg_field = field; *reg_field; reg_field++) { if ((*reg_field)->is_flag_set(PART_KEY_FLAG)) bitmap_set_bit(read_set, (*reg_field)->field_index()); + use_field((*reg_field)->field_index()); } file->column_bitmaps_signal(); } @@ -5663,6 +5677,7 @@ void TABLE::mark_columns_needed_for_update(THD *thd, bool mark_binlog_columns) { /* Merge keys is all keys that had a column referred to in the query */ if (merge_keys.is_overlapping((*reg_field)->part_of_key)) bitmap_set_bit(read_set, (*reg_field)->field_index()); + use_field((*reg_field)->field_index()); } file->column_bitmaps_signal(); } @@ -5758,8 +5773,10 @@ void TABLE::mark_columns_per_binlog_row_image(THD *thd) { */ if ((s->primary_key < MAX_KEY) && (my_field->is_flag_set(PRI_KEY_FLAG) || - (my_field->type() != MYSQL_TYPE_BLOB))) + (my_field->type() != MYSQL_TYPE_BLOB))) { bitmap_set_bit(read_set, my_field->field_index()); + use_field(my_field->field_index()); + } if (my_field->type() != MYSQL_TYPE_BLOB) bitmap_set_bit(write_set, my_field->field_index()); @@ -7320,7 +7337,10 @@ void TABLE::mark_gcol_in_maps(const Field *field) { Typed array fields internally are using a conversion field, it needs to marked as readable in order to do conversions. */ - if (field->is_array()) bitmap_set_bit(read_set, field->field_index()); + if (field->is_array()) { + bitmap_set_bit(read_set, field->field_index()); + use_field(field->field_index()); + } /* Note that underlying base columns are here added to read_set but not added @@ -7338,6 +7358,7 @@ void TABLE::mark_gcol_in_maps(const Field *field) { for (uint i = 0; i < s->fields; i++) { if (bitmap_is_set(&field->gcol_info->base_columns_map, i)) { bitmap_set_bit(read_set, i); + use_field(i); if (this->field[i]->is_virtual_gcol()) bitmap_set_bit(write_set, i); } } diff --git a/sql/table.h b/sql/table.h index d1cd2e5a27c..5fb85856f26 100644 --- a/sql/table.h +++ b/sql/table.h @@ -1661,6 +1661,32 @@ struct TABLE { // List of table check constraints. Sql_table_check_constraint_list *table_check_constraint_list{nullptr}; + public: + /** + Reference count of each field in read operation (read_set). Valid only + for SELECT statemnt. + */ + std::vector fields_use_count; + + /** + In the prepare phase, every time Field appears in fields and conditions, + read_set will be set once. Introduce TABLE::fields_use_count to record + the number of times each field is referenced. If use_count is 0, remove + it from TABLE::read_set. + */ + void release_field(uint16 field_index) { + if (fields_use_count.size() > field_index && + --fields_use_count[field_index] == 0) { + bitmap_clear_bit(read_set, field_index); + } + } + + void use_field(uint16 field_index) { + if (fields_use_count.size() > field_index) { + ++fields_use_count[field_index]; + } + } + private: /** If true, this table is inner w.r.t. some outer join operation, all columns diff --git a/unittest/gunit/fake_table.h b/unittest/gunit/fake_table.h index 7c8bb650d1c..fa3c54787dd 100644 --- a/unittest/gunit/fake_table.h +++ b/unittest/gunit/fake_table.h @@ -300,6 +300,7 @@ class Fake_TABLE : public TABLE { new_field->table_name = &table_name; new_field->set_field_index(pos); bitmap_set_bit(read_set, pos); + use_field(pos); const ptrdiff_t field_offset = pos * MAX_FIELD_WIDTH; new_field->set_field_ptr(record[0] + field_offset + 1); if (new_field->get_null_ptr() != nullptr) From 9fdad65b4d4737461c42f16854cf4fb5ae310b64 Mon Sep 17 00:00:00 2001 From: litianfeng1998 Date: Fri, 5 Jun 2026 10:59:36 +0800 Subject: [PATCH 2/5] [bugfix] Convert count on not null column to count(0) postfix Use real_sum_func() instead of sum_func() when deciding whether to print the count-conversion info in ExplainAccessPath, so that special items such as Item_rollup_sum_switcher (whose sum_func() returns the child aggregate type) do not cause an incorrect cast/print. --- sql/join_optimizer/explain_access_path.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sql/join_optimizer/explain_access_path.cc b/sql/join_optimizer/explain_access_path.cc index 17857dcdfdc..04d92ef2029 100644 --- a/sql/join_optimizer/explain_access_path.cc +++ b/sql/join_optimizer/explain_access_path.cc @@ -999,8 +999,11 @@ ExplainData ExplainAccessPath(const AccessPath *path, JOIN *join, } else { ret += ItemToString(*item); } - - if ((*item)->sum_func() == Item_sum::COUNT_FUNC && + /* + Note: use real_sum_func(), i.e, Item_rollup_sum_switcher will return + master()->sum_func() in sum_func. + */ + if ((*item)->real_sum_func() == Item_sum::COUNT_FUNC && ((Item_sum_count *)(*item))->is_coverted_count_zero) { ret += " using conversion for count of not null column"; } From 0584dd2ba5863ee4a93d6951d9d94c3ef07d6568 Mon Sep 17 00:00:00 2001 From: litianfeng1998 Date: Fri, 5 Jun 2026 10:59:55 +0800 Subject: [PATCH 3/5] [bugfix] count rewrite asan mem leak postfix The count conversion feature added TABLE::fields_use_count. Since MySQL TABLE objects are recycled without explicitly calling the destructor, the vector did not release its memory. Explicitly clear fields_use_count for every open table after the query finishes. --- sql/sql_parse.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index a307f6263e2..c510c047f9e 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -5950,6 +5950,12 @@ int mysql_execute_command(THD *thd, bool first_level) { } if (unlikely(thd->has_backquery())) ha_end_backquery(thd); + for (TABLE *table = thd->open_tables; table; table = table->next) { + DBUG_PRINT("cnt_conversion", + ("clear fields_use_count of table: '%s' query_id: %lu", + table->s->table_name.str, (ulong)table->query_id)); + if (!table->fields_use_count.empty()) table->fields_use_count.clear(); + } lex->cleanup(thd, true); From 3190f9f7a9f4194498e7469fd3719e55928f38d9 Mon Sep 17 00:00:00 2001 From: litianfeng1998 Date: Fri, 5 Jun 2026 11:02:07 +0800 Subject: [PATCH 4/5] [bugfix] cnt rule asan leak postfix The count conversion feature introduced a std::vector member in TABLE which is not allocated from mem_root; since TABLE is managed by the mem_root mechanism and its destructor is not called, the vector leaked. Replace fields_use_count with a Mem_root_array allocated from the TABLE mem_root so it is compatible with MySQL memory management. --- sql/sql_parse.cc | 4 +++- sql/table.cc | 20 ++++++++++++-------- sql/table.h | 21 ++++++++++++++++----- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index c510c047f9e..f840b4bcb5c 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -5954,7 +5954,9 @@ int mysql_execute_command(THD *thd, bool first_level) { DBUG_PRINT("cnt_conversion", ("clear fields_use_count of table: '%s' query_id: %lu", table->s->table_name.str, (ulong)table->query_id)); - if (!table->fields_use_count.empty()) table->fields_use_count.clear(); + if (table->fields_use_count && !table->fields_use_count->empty()) { + table->fields_use_count->clear(); + } } lex->cleanup(thd, true); diff --git a/sql/table.cc b/sql/table.cc index 77a10a27666..6cf13051e14 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -3247,8 +3247,12 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, outparam->no_replicate = false; } - outparam->fields_use_count.clear(); - outparam->fields_use_count.resize(share->fields); + if (!outparam->fields_use_count) { + outparam->fields_use_count = new (root) Mem_root_array(root, share->fields); + if (outparam->fields_use_count == nullptr) goto err; // OOM + } else { + outparam->reset_fields_use_count(); + } /* Increment the opened_tables counter, only when open flags set. */ if (db_stat) thd->status_var.opened_tables++; @@ -3272,6 +3276,9 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, free_items(table_cc.value_generator()->item_list); } } + if (outparam->fields_use_count) { + outparam->fields_use_count->clear(); + } outparam->file = nullptr; // For easier error checking outparam->db_stat = 0; if (!internal_tmp) root->Clear(); @@ -4131,8 +4138,7 @@ void TABLE::init(THD *thd, TABLE_LIST *tl) { bind_value_generators_to_fields(); } - fields_use_count.clear(); - fields_use_count.resize(s->fields); + reset_fields_use_count(); } /** @@ -4169,8 +4175,7 @@ void TABLE::reset() { m_parallel_scan = false; m_parallel_workers = 0; #endif /* defined(HAVE_PX) */ - fields_use_count.clear(); - fields_use_count.resize(s->fields); + reset_fields_use_count(); } /** @@ -4237,8 +4242,7 @@ bool TABLE::init_tmp_table(THD *thd, TABLE_SHARE *share, MEM_ROOT *m_root, #ifndef NDEBUG set_tmp_table_seq_id(thd->get_tmp_table_seq_id()); #endif - fields_use_count.clear(); - fields_use_count.resize(s->fields); + reset_fields_use_count(); return false; } diff --git a/sql/table.h b/sql/table.h index 5fb85856f26..6d0e6f0e2e9 100644 --- a/sql/table.h +++ b/sql/table.h @@ -1666,7 +1666,7 @@ struct TABLE { Reference count of each field in read operation (read_set). Valid only for SELECT statemnt. */ - std::vector fields_use_count; + Mem_root_array *fields_use_count{nullptr}; /** In the prepare phase, every time Field appears in fields and conditions, @@ -1675,15 +1675,26 @@ struct TABLE { it from TABLE::read_set. */ void release_field(uint16 field_index) { - if (fields_use_count.size() > field_index && - --fields_use_count[field_index] == 0) { + if (fields_use_count == nullptr) return; + if (fields_use_count->empty()) return; + if (fields_use_count->size() > field_index && + --((*fields_use_count)[field_index]) == 0) { bitmap_clear_bit(read_set, field_index); } } void use_field(uint16 field_index) { - if (fields_use_count.size() > field_index) { - ++fields_use_count[field_index]; + if (fields_use_count == nullptr) return; + if (fields_use_count->empty()) return; + if (fields_use_count->size() > field_index) { + ++(*fields_use_count)[field_index]; + } + } + + void reset_fields_use_count() { + if (fields_use_count) { + fields_use_count->clear(); + fields_use_count->resize(s->fields); } } From 0b117ad17ca1a7645c368a011efa406bdabd4b25 Mon Sep 17 00:00:00 2001 From: litianfeng1998 Date: Fri, 5 Jun 2026 12:02:18 +0800 Subject: [PATCH 5/5] [port] adapt count conversion for OpenTenBase/TXSQL 8.0.30 - table.cc: brace the PART_KEY_FLAG / merge_keys branches so the added use_field() calls are correctly guarded (fixes -Werror=misleading- indentation under debug build with gcc-toolset-10). - regenerate mysql-test/r/txsql_convert_to_count_star.result against this repo via mtr --record (test passes in both record and normal runs). - drop the .pq result variant carried over from the source repo; it is environment-specific and not validated here (default .result is used). --- mysql-test/r/txsql_convert_to_count_star.pq | 294 ------------------ .../r/txsql_convert_to_count_star.result | 16 +- sql/table.cc | 6 +- 3 files changed, 12 insertions(+), 304 deletions(-) delete mode 100644 mysql-test/r/txsql_convert_to_count_star.pq diff --git a/mysql-test/r/txsql_convert_to_count_star.pq b/mysql-test/r/txsql_convert_to_count_star.pq deleted file mode 100644 index 94381c289d2..00000000000 --- a/mysql-test/r/txsql_convert_to_count_star.pq +++ /dev/null @@ -1,294 +0,0 @@ -create table t1(a int, b int); -create table t2(a int not null, b int not null); -INSERT INTO t1 VALUES (1, 1), (2, NULL), (3, 3); -INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3); -ANALYZE TABLE t1,t2; -Table Op Msg_type Msg_text -test.t1 analyze status OK -test.t2 analyze status OK -set @saved_value=@@txsql_count_conversion_enabled; -set @@txsql_count_conversion_enabled = 0; -flush status; -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 0 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 0 -EXPLAIN format=tree select count(*) from t1 where a > 1; -EXPLAIN --> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(0) (cost=0.65 rows=1) - -> Filter: (t1.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t1 (cost=0.55 rows=3) - -select count(*) from t1 where a > 1; -count(*) -2 -EXPLAIN format=tree select count(b) from t1 where a > 1; -EXPLAIN --> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(t1.b) (cost=0.65 rows=1) - -> Filter: (t1.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t1 (cost=0.55 rows=3) - -select count(b) from t1 where a > 1; -count(b) -1 -EXPLAIN format=tree select count(*) from t2 where a > 1; -EXPLAIN --> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(0) (cost=0.65 rows=1) - -> Filter: (t2.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select count(*) from t2 where a > 1; -count(*) -2 -EXPLAIN format=tree select count(b) from t2 where a > 1; -EXPLAIN --> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(t2.b) (cost=0.65 rows=1) - -> Filter: (t2.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select count(b) from t2 where a > 1; -count(b) -2 -EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; -EXPLAIN --> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) - -> Aggregate: count(t2.b) (cost=2.23 rows=1) - -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -> Hash - -> Table scan on t1 (cost=0.18 rows=3) - -select count(t2.b) from t2 left join t1 on t1.a = t2.a; -count(t2.b) -3 -EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; -EXPLAIN --> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) - -> Aggregate: count(t2.b) (cost=2.23 rows=1) - -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) - -> Parallel table scan on t1 (cost=0.55 rows=3) - -> Hash - -> Table scan on t2 (cost=0.18 rows=3) - -select count(t2.b) from t1 left join t2 on t1.a = t2.a; -count(t2.b) -3 -EXPLAIN format=tree select count(b) from t2 group by a; -EXPLAIN --> Table scan on - -> Final Aggregate using temporary table - -> PX Receiver (slice: 0; workers: 1) - -> PX Sender (slice: 1; workers: 3) - -> Table scan on - -> Aggregate using temporary table - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select count(b) from t2 group by a; -count(b) -1 -1 -1 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 0 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 0 -set @@txsql_count_conversion_enabled = 1; -EXPLAIN format=tree select count(*) from t1 where a > 1; -EXPLAIN --> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(0) (cost=0.65 rows=1) - -> Filter: (t1.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t1 (cost=0.55 rows=3) - -select count(*) from t1 where a > 1; -count(*) -2 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 5 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 0 -EXPLAIN format=tree select count(b) from t1 where a > 1; -EXPLAIN --> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(t1.b) (cost=0.65 rows=1) - -> Filter: (t1.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t1 (cost=0.55 rows=3) - -select count(b) from t1 where a > 1; -count(b) -1 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 10 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 0 -EXPLAIN format=tree select count(*) from t2 where a > 1; -EXPLAIN --> Aggregate: sum(`count(*)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(0) (cost=0.65 rows=1) - -> Filter: (t2.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select count(*) from t2 where a > 1; -count(*) -2 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 15 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 0 -EXPLAIN format=tree select count(b) from t2 where a > 1; -EXPLAIN --> Aggregate: sum(`count(b)`) (cost=0.65 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.65 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.65 rows=1) - -> Aggregate: count(0) using conversion for count of not null column (cost=0.65 rows=1) - -> Filter: (t2.a > 1) (cost=0.55 rows=1) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select count(b) from t2 where a > 1; -count(b) -2 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 20 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 5 -EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; -EXPLAIN --> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) - -> Aggregate: count(0) using conversion for count of not null column (cost=2.23 rows=1) - -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -> Hash - -> Table scan on t1 (cost=0.18 rows=3) - -select count(t2.b) from t2 left join t1 on t1.a = t2.a; -count(t2.b) -3 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 25 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 10 -EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; -EXPLAIN --> Aggregate: sum(`count(t2.b)`) (cost=2.23 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=2.23 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=2.23 rows=1) - -> Aggregate: count(t2.b) (cost=2.23 rows=1) - -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) - -> Parallel table scan on t1 (cost=0.55 rows=3) - -> Hash - -> Table scan on t2 (cost=0.18 rows=3) - -select count(t2.b) from t1 left join t2 on t1.a = t2.a; -count(t2.b) -3 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 30 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 10 -EXPLAIN format=tree select count(b) from t2 group by a; -EXPLAIN --> Table scan on - -> Final Aggregate using temporary table - -> PX Receiver (slice: 0; workers: 1) - -> PX Sender (slice: 1; workers: 3) - -> Table scan on - -> Aggregate using temporary table - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select count(b) from t2 group by a; -count(b) -1 -1 -1 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 35 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 15 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 35 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 15 -# --include save_and_set_px.inc -set @@txsql_count_conversion_enabled=0; -EXPLAIN format=tree select avg(b) from t2; -EXPLAIN --> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) - -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select avg(b) from t2; -avg(b) -2.0000 -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 0 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 0 -set @@txsql_count_conversion_enabled=1; -EXPLAIN format=tree select avg(b) from t2; -EXPLAIN --> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) - -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) - -> Parallel table scan on t2 (cost=0.55 rows=3) - -select avg(b) from t2; -avg(b) -2.0000 -# --include set_px_to_saved.inc -show status like "%total_count_function%"; -Variable_name Value -Txsql_total_count_function 35 -show status like "%implicit_convert_to_count_star%"; -Variable_name Value -Txsql_implicit_convert_to_count_star 15 -set @@txsql_count_conversion_enabled=@saved_value; -DROP TABLE t1,t2; diff --git a/mysql-test/r/txsql_convert_to_count_star.result b/mysql-test/r/txsql_convert_to_count_star.result index 6f6e4b561ea..d62eaea1822 100644 --- a/mysql-test/r/txsql_convert_to_count_star.result +++ b/mysql-test/r/txsql_convert_to_count_star.result @@ -212,10 +212,10 @@ Txsql_implicit_convert_to_count_star 6 set @@txsql_count_conversion_enabled=0; EXPLAIN format=tree select avg(b) from t2; EXPLAIN --> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) - -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) +-> Aggregate: sum(t2.b), count(t2.b) + -> PX Receiver (slice: 0; workers: 1) + -> PX Sender (slice: 1; workers: 3) + -> Aggregate: sum(t2.b), count(t2.b) (cost=0.85 rows=1) -> Parallel table scan on t2 (cost=0.55 rows=3) select avg(b) from t2; @@ -230,10 +230,10 @@ Txsql_implicit_convert_to_count_star 0 set @@txsql_count_conversion_enabled=1; EXPLAIN format=tree select avg(b) from t2; EXPLAIN --> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) - -> PX Receiver (slice: 0; workers: 1) (cost=0.85 rows=1) - -> PX Sender (slice: 1; workers: 3) (cost=0.85 rows=1) - -> Aggregate: px_sum_switcher(avg(t2.b)) (cost=0.85 rows=1) +-> Aggregate: sum(t2.b), count(0) + -> PX Receiver (slice: 0; workers: 1) + -> PX Sender (slice: 1; workers: 3) + -> Aggregate: sum(t2.b), count(0) (cost=0.85 rows=1) -> Parallel table scan on t2 (cost=0.55 rows=3) select avg(b) from t2; diff --git a/sql/table.cc b/sql/table.cc index 6cf13051e14..88f4396ad54 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -5601,9 +5601,10 @@ void TABLE::mark_columns_needed_for_delete(THD *thd) { if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE) { Field **reg_field; for (reg_field = field; *reg_field; reg_field++) { - if ((*reg_field)->is_flag_set(PART_KEY_FLAG)) + if ((*reg_field)->is_flag_set(PART_KEY_FLAG)) { bitmap_set_bit(read_set, (*reg_field)->field_index()); use_field((*reg_field)->field_index()); + } } file->column_bitmaps_signal(); } @@ -5679,9 +5680,10 @@ void TABLE::mark_columns_needed_for_update(THD *thd, bool mark_binlog_columns) { Field **reg_field; for (reg_field = field; *reg_field; reg_field++) { /* Merge keys is all keys that had a column referred to in the query */ - if (merge_keys.is_overlapping((*reg_field)->part_of_key)) + if (merge_keys.is_overlapping((*reg_field)->part_of_key)) { bitmap_set_bit(read_set, (*reg_field)->field_index()); use_field((*reg_field)->field_index()); + } } file->column_bitmaps_signal(); }