Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions mysql-test/r/txsql_convert_to_count_star.result
Original file line number Diff line number Diff line change
@@ -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 <temporary>
-> 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 <temporary>
-> 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: 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;
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: 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;
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;
109 changes: 109 additions & 0 deletions mysql-test/t/txsql_convert_to_count_star.test
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions sql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,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
Expand Down
2 changes: 2 additions & 0 deletions sql/item_sum.h
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,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 */
Expand Down
8 changes: 8 additions & 0 deletions sql/join_optimizer/explain_access_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,14 @@ ExplainData ExplainAccessPath(const AccessPath *path, JOIN *join,
} else {
ret += ItemToString(*item);
}
/*
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";
}
}
description.push_back(move(ret));
children.push_back({path->aggregate().child});
Expand Down
Loading