matrixorigin / matrixorigin/matrixone
[Compatibility]: SUM and AVG reject MySQL numeric-convertible character and temporal operands
- Dominant language
- Go
- Stars
- 1.9k
- Forks
- 311
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 768
Description
## Description
MatrixOne rejects character and temporal operands for `SUM` and `AVG` during function binding, while MySQL places such operands in numeric context. Equivalent explicit casts work in MatrixOne, so the gap is aggregate type coercion rather than unavailable arithmetic.
`ENUM`/`SET` are intentionally excluded from this report because they are already tracked by #28692.
## Environment
- MatrixOne: latest official `main`, commit `07fdd4ae0f80f287b93fc4b525fd296d5617abc7`
- Deployment: local standalone launch (isolated Log/TN/CN ports and data directory)
- MySQL contract: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html
## Reproduction
```sql
create table strings(s varchar(32));
insert into strings values ('1'),('2.5'),(' 3 '),('-4e0'),(null);
select sum(s), avg(s) from strings;
create table temporal(d date, dt datetime(6), ts timestamp(6), tm time(6));
insert into temporal values
('2026-01-01','2026-01-01 00:00:00.100000','2026-01-01 00:00:00.100000','00:00:00.100000'),
('2026-01-02','2026-01-01 00:00:00.200000','2026-01-01 00:00:00.200000','00:00:00.200000');
select sum(d),avg(d),sum(dt),avg(dt),sum(ts),avg(ts),sum(tm),avg(tm)
from temporal;
```
## MatrixOne behavior
```text
invalid argument aggregate function sum, bad value [VARCHAR]
invalid argument aggregate function sum, bad value [DATE]
```
The rejection reproduced in 3/3 fresh runs. `AVG` is rejected by the same `SumSupportedTypes` check when executed separately.
## MatrixOne controls
Explicit numeric conversion succeeds:
```sql
select sum(cast(s as double)), avg(cast(s as double)) from strings;
-- 2.5, 0.625
select sum(cast(d as decimal(38,0))),
sum(cast(dt as decimal(38,6))),
sum(cast(ts as decimal(38,6))),
sum(cast(tm as decimal(38,6)))
from temporal;
-- 40520203, 40520202000000.300000, 40520202000000.300000, 0.300000
```
The adjacent `VAR_POP`, `VAR_SAMP`, `STDDEV_POP`, and `STDDEV_SAMP` functions already apply MySQL-compatible string/temporal numeric coercion after #28229.
## Expected behavior
For MySQL compatibility, `SUM` and `AVG` should apply the same numeric-context rules to character and temporal operands instead of requiring application-side casts. Ordinary, DISTINCT, grouped, window, prepared-statement, View, and CTAS forms should resolve consistently.
## Code-path analysis
`sumAvgTypeCheck` in `pkg/sql/plan/function/list_agg.go` delegates to `fixedUnaryAggTypeCheck(..., SumSupportedTypes)`, which excludes MySQL string and temporal types. The recently added `mysqlNumericAggTypeCheck` used by variance/stddev already contains the missing coercion categories and demonstrates an adjacent planner pattern.
## Suggested regression coverage
- clean numeric strings, whitespace, exponent, numeric prefix, empty/invalid strings, and NULL;
- DATE/TIME/DATETIME/TIMESTAMP with fractional seconds;
- SUM/AVG, DISTINCT, grouped/window, DOP 1/>1, prepared parameters, View, and CTAS;
- explicit-cast controls and warning/error behavior under SQL modes.
## Duplicate search
Open and closed issues were searched for SUM/AVG VARCHAR/string/temporal/date coercion and aggregate binding errors. #28229 is the already-fixed variance/stddev equivalent; #28692 covers ENUM/SET only. No matching character/temporal SUM/AVG report was found.
Contributor guide
Assessment
This issue has not been assessed yet.