Repository navigation
Description
Description
GROUP BY, SELECT DISTINCT, and distinct aggregates can split fixed-width CHAR values that differ only in trailing spaces, even though MatrixOne's comparison and unique-key paths treat those values as equal.
For the four stored values 'a', 'a ', 'a ', and 'a ' in a CHAR(4) column:
c = 'a'is true for all four rows;WHERE c = 'a'returns all four rows;- a unique index rejects the second spelling as a duplicate;
COUNT(DISTINCT c),APPROX_COUNT_DISTINCT(c), andHLL_ADD_AGG(c)each return cardinality4instead of1;GROUP BY candSELECT DISTINCT cproduce four keys instead of one.
This violates the SQL equivalence invariant inside MatrixOne: values that compare equal and conflict in a unique key must not become separate grouping/distinct keys.
Environment
- MatrixOne: latest official
main, commit07fdd4ae0f80f287b93fc4b525fd296d5617abc7 - Deployment: local standalone launch (isolated Log/TN/CN ports and data directory)
Reproduction
drop database if exists char_distinct_group_repro;
create database char_distinct_group_repro;
use char_distinct_group_repro;
create table t(id int, c char(4));
insert into t values
(1, 'a'),
(2, 'a '),
(3, 'a '),
(4, 'a ');
select id, c = 'a' as equals_a from t order by id;
select count(*) from t where c = 'a';
select count(distinct c) from t;
select count(*) from (select distinct c from t) d;
select count(*) from (select c from t group by c) g;
create table u(c char(4), unique key uk(c));
insert into u values ('a');
insert into u values ('a ');Actual behavior
equals_a: 1, 1, 1, 1
WHERE count: 4
COUNT DISTINCT: 4
SELECT DISTINCT groups: 4
GROUP BY groups: 4
second unique insert: Duplicate entry 'a ' for key 'uk'
The result was reproduced three times from freshly recreated tables on the same main build.
A width matrix further isolates two affected paths:
type COUNT(DISTINCT c) GROUP BY groups rows matching c = 'a'
CHAR(4) 4 4 4
CHAR(8) 4 1 4
CHAR(16) 4 1 4
CHAR(32) 4 1 4
Thus the short-key grouping path is affected, while the distinct-aggregate path is affected independently of these tested widths.
For both CHAR(4) and CHAR(32), APPROX_COUNT_DISTINCT(c) and HLL_CARDINALITY(HLL_ADD_AGG(c)) also return 4. A VARCHAR(4) control returns four across all grouping/distinct paths, as expected because its trailing spaces remain significant. COUNT(DISTINCT c, 7) returns four for both CHAR widths, so multi-argument exact DISTINCT is affected too.
Expected behavior
All three distinct/grouping forms should produce one key, consistent with =, WHERE, and the unique index.
Code-path analysis
The behavior depends on the internal hash-map representation rather than SQL semantics:
pkg/common/hashmap/strhashmap.gocallscanonicalVarlenaHashValue, which trims trailing ASCII spaces fortypes.T_char;pkg/common/hashmap/inthashmap.go:fillVarlenaKeycopies raw varlena bytes directly and does not apply the sameCHARcanonicalization; short grouping keys can therefore receive different group IDs;- exact distinct-aggregate admission and retained-key logic in
pkg/sql/colexec/aggexeccompares/copies raw argument bytes, with special canonicalization only for floating signed zero; it has no PAD SPACE canonicalization forCHAR; - HLL/approximate distinct aggregation hashes raw argument bytes and likewise lacks typed
CHARcanonicalization.
This explains why a StrHashMap-only unit test protects longer SQL grouping keys but not the IntHashMap or distinct-aggregate paths.
Regression context
#28023 recorded a related PAD SPACE grouping defect and was closed after a prior main build grouped the values together. The current official main has regressed the SQL grouping/distinct behavior through a different short-key hash path, so this report is new rather than reopening the closed issue.
Suggested regression coverage
IntHashMapunit coverage forCHARvalues with zero through full-width trailing spaces, including nullable keys;- SQL
GROUP BY,SELECT DISTINCT, exact/approximate/HLL distinct aggregates for both short and longCHARwidths; - DOP 1/DOP >1 and resident/spill aggregate paths;
- controls for
VARCHAR, whose trailing spaces remain significant.
Activity
fix: canonicalize CHAR and JSON HLL_ADD_AGG keys (#29123)
Ariznawlll commentedon Sep 21, 2026
Verified on current main 57c280e.\n\nFor CHAR values differing only by trailing spaces, exact DISTINCT, APPROX_COUNT_DISTINCT, and HLL cardinality all return 1. The canonical SQL equality key is therefore shared by the aggregate paths. The result was stable across three runs.\n\nMOTR coverage was added in matrixorigin/motr#193:\nsuites/14_issue_regression/t/issue_28864_28859_28860_28854_aggregates.test.\n\nClosing as fixed.