yujun777 opened a new pull request, #68863:
URL: https://github.com/apache/doris/pull/68863
### What problem does this PR solve?
Issue Number: N/A
Related Issue: #57278
Problem Summary:
`Count.constructRollUp` rolled a non-DISTINCT count up to `sum`, so a query
whose
predicate filters out every row of the materialized view aggregated an empty
input.
`SUM` is NULL on empty input, while a scalar `COUNT` without `GROUP BY` must
return
one row of 0.
The rewritten output kept the nullability of the query's `COUNT`, which is a
`NotNullableAggregateFunction`, so
`MaterializedViewUtils.normalizeExpression` wrapped
the rolled-up `sum` in `non_nullable` and the BE reported the NULL as a hard
error
instead of returning 0:
```sql
CREATE TABLE base (id INT, k INT) DUPLICATE KEY(id) DISTRIBUTED BY HASH(id)
BUCKETS 1
PROPERTIES ("replication_num" = "1");
INSERT INTO base VALUES (1,1),(2,1);
CREATE MATERIALIZED VIEW mv
BUILD DEFERRED REFRESH COMPLETE ON MANUAL
DISTRIBUTED BY HASH(k) BUCKETS 1 PROPERTIES ("replication_num" = "1")
AS SELECT k, COUNT(*) n FROM base GROUP BY k;
REFRESH MATERIALIZED VIEW mv COMPLETE; -- SUCCESS, mv holds (1, 2)
SELECT /*+ use_mv(mv) */ COUNT(*) FROM base WHERE k > 999;
-- before: errCode = 2, detailMessage = [INVALID_ARGUMENT]There's NULL value
in
-- column which is illegal for non_nullable
-- after: 0
-- the same query with enable_materialized_view_rewrite=false already
returned 0
```
Fix: roll a non-DISTINCT count up to `sum0`, which is also a
`NotNullableAggregateFunction` and returns 0 on empty input — exactly the
`resultForEmptyInput()` contract `Count` already declares. Its
`ret(BigIntType).args(BigIntType)` signature matches the BIGINT count column
stored in
the MV, so the output slot of the rewrite does not change, and
`Sum0.constructRollUp`
returns `Sum0`, keeping a multi-level rollup self-consistent. A grouped
rollup is
unaffected because a group only appears when at least one MV row survives,
where `sum`
and `sum0` agree row by row. The DISTINCT branch already rolled up to
`BitmapUnionCount`, whose empty-input result is 0, so it stays as is.
### Release note
A query that hits a materialized view through a non-DISTINCT `COUNT` rollup
no longer
fails with `There's NULL value in column ... which is illegal for
non_nullable` when its
predicate filters out every row of the materialized view; it returns 0 like
the same
query planned against the base table.
### Check List (For Author)
- Test
- [x] Regression test
- [ ] Unit Test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason
New suite `nereids_rules_p0/mv/count_rollup_empty_input` asserts the
rewrite is
chosen (`mv_rewrite_success`), pins the mechanism
(`explain { contains "sum0"; notContains "non_nullable" }`) and
golden-files the
empty-input scalar COUNT, the same query with the rewrite disabled, a
partially
matching predicate, a grouped rollup and a full-table rollup.
Re-runs of existing COUNT/rollup suites are unchanged: `mv_p0/count_star`,
`nereids_rules_p0/mv` `aggregate_with_roll_up`,
`aggregate_without_roll_up`,
`any_value_roll_up`, `agg_optimize_when_uniform`, `mv_tpch_test`,
`materialized_view_grouping_sets`.
- Behavior changed:
- [ ] No.
- [x] Yes. The plan of a COUNT rollup over a materialized view now uses
`sum0`
instead of `sum`. Results only differ for the empty-input case, which
previously
raised an error.
- Does this need documentation?
- [x] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR should
merge into -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]