Repository navigation
[feature] Convert count on not null column to count(0) - #91
Merged
litianfeng1998 merged 6 commits intoJun 5, 2026
Merged
Conversation
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
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.
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.
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<uint> allocated from the TABLE mem_root so it is compatible with MySQL memory management.
- 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).
|
|
Collaborator
Author
|
@copilot resolve the merge conflicts in this pull request |
flyfox20
pushed a commit
that referenced
this pull request
Jun 23, 2026
flyfox20
pushed a commit
that referenced
this pull request
Jun 23, 2026
* [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 * [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. * [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. * [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<uint> allocated from the TABLE mem_root so it is compatible with MySQL memory management. * [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). --------- Co-authored-by: litianfeng1998 <litianfeng1998@users.noreply.github.com>
flyfox20
pushed a commit
that referenced
this pull request
Jun 24, 2026
* [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 * [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. * [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. * [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<uint> allocated from the TABLE mem_root so it is compatible with MySQL memory management. * [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). --------- Co-authored-by: litianfeng1998 <litianfeng1998@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Convert
count(col)tocount(0)when the column is guaranteed NOT NULL (e.g. a NOT NULL column not on the inner side of an outer join). In that casecount(col)is equivalent tocount(0), and the per-row column processing overhead can be saved when the column is not referenced elsewhere.Controlled by a new session sysvar
txsql_count_conversion_enabled(default ON). Two new status vars expose the effect:Txsql_total_count_functionandTxsql_implicit_convert_to_count_star.Commits
TABLE::fields_use_count) + count(0) conversion during optimization.real_sum_func()in ExplainAccessPath soItem_rollup_sum_switcheris handled correctly.fields_use_countafter query finishes.fields_use_countas aMem_root_array<uint>allocated from the TABLE mem_root.Testing
main.txsql_convert_to_count_starpasses (both--recordand normal runs) on a debug server.