nooneuse opened a new issue, #71068: URL: https://github.com/apache/doris/issues/71068
### Search before asking - [x] I had searched in the [issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no similar issues. ### Description ### Background Apache Doris provides `datasketches_hll_union_agg(sketch [, lg_max_k])` for cardinality estimation (#63143, #67469) and `datasketches_hll_downsample(sketch, lg_k [, tgt_type])` for scalar sketch downsampling (#69257), but lacks an aggregate function returning a merged serialized DataSketches HLL sketch for asynchronous materialized views (MTMVs). ### Current behavior `datasketches_hll_union_agg` outputs a `DOUBLE` cardinality estimate (`canRollUp() == false`), so Nereids MTMVs cannot pre-aggregate raw HLL sketch columns into coarser sketch summaries and transparently roll them up for `datasketches_hll_union_agg` or sketch-union queries. ### Expected behavior Users can aggregate serialized DataSketches HLL sketches via `datasketches_hll_union(sketch [, lg_max_k [, tgt_type]])` (alias `ds_hll_union`) in MTMVs and roll them up transparently at query time: ```sql -- Minute-level MTMV: SELECT date_trunc(event_ts, 'hour') AS hour_ts, date_trunc(event_ts, 'minute') AS min_ts, k, datasketches_hll_union(sk, 12, 'HLL_8') AS sk_union FROM ds_hll_mv_events GROUP BY date_trunc(event_ts, 'hour'), date_trunc(event_ts, 'minute'), k; -- Hourly query rewritten to datasketches_hll_union_agg(sk_union, 8) since 8 <= 12: SELECT date_trunc(event_ts, 'hour') AS hour_ts, k, CAST(ROUND(datasketches_hll_union_agg(sk, 8)) AS BIGINT) AS uv FROM ds_hll_mv_events GROUP BY date_trunc(event_ts, 'hour'), k; ``` ### Proposed design 1. **Aggregate function `datasketches_hll_union` (alias `ds_hll_union`):** Accepts 1–3 arguments (`STRING`/`VARCHAR`/`VARBINARY` sketch, optional constant `lg_max_k` in `[7, 21]`, optional constant `tgt_type` in `{HLL_4, HLL_6, HLL_8}` defaulting to `HLL_4`) and preserves arg0's type to avoid implicit casts breaking MV matching. Uses `NullableAggregateFunction` semantics (returns `NULL` on empty or all-NULL groups; outputs an empty compact sketch of `lg_max_k` or `12` when all non-NULL inputs are empty sketches). 2. **Nereids MTMV Rollup Handler (`DataSketchesHllRollupHandler`):** Placed between `DirectRollupHandler` and `MappingRollupHandler`, supporting `datasketches_hll_union_agg -> datasketches_hll_union` and cascading `datasketches_hll_union -> datasketches_hll_union`. Verifies `child(0)` equality and precision compatibility `Kq_eff <= Kmv_eff` (omitted `lg_max_k` treated as `21`, non-literal conservatively rejected), while leaving `MappingRollupHandler`'s single-argument matching untouched. Alternatives rejected: - Fixed `STRING` or `VARBINARY` return type: implicit casts break Nereids MV expression matching. - Non-nullable return emitting empty sketches on all-NULL groups: contradicts SQL nullability expectations for empty groups. - Modifying `MappingRollupHandler` directly: couples multi-argument precision semantics into single-argument `Any.INSTANCE` rewriting. ### Impact Users running hourly or multi-stage DataSketches HLL aggregations over minute-level MTMVs gain transparent rollup support. Existing `datasketches_hll_union_agg` and `datasketches_hll_downsample` queries are unaffected. ### Reproduction or evidence Verified via new BE unit tests (`agg_datasketches_hll_union_agg_test.cpp`), FE unit tests (`DataSketchesHllUnionTest`, `DataSketchesHllRollupHandlerTest`, `InferAggNotNullTest`), and regression suites (`test_datasketches_hll_union.groovy`, `datasketches_hll_union_roll_up.groovy`) against a 1-FE/1-BE ASAN cluster. ### Scope and non-goals - In scope: `datasketches_hll_union` / `ds_hll_union` in FE/BE and Nereids async MTMV rollups. - Out of scope (follow-up): synchronous MVs (`CREATE MATERIALIZED VIEW`), native storage aggregate column types, and specialized `_state`/`_merge`/`_union` combinator MV rollup handlers. ### Acceptance criteria - `datasketches_hll_union` preserves arg0 type (`STRING`/`VARCHAR`/`VARBINARY`), enforces `[7, 21]` and `{HLL_4, HLL_6, HLL_8}`, and matches `datasketches_hll_union_agg` cardinality estimates after unioning. - Empty and all-NULL groups return `NULL`; all-empty-sketch groups return a valid compact empty sketch. - Async MTMV rewrite succeeds for `Kq_eff <= Kmv_eff` (including cascading rollups) and conservatively rejects `Kq_eff > Kmv_eff` or MVs using `datasketches_hll_union_agg`. ### Risks and compatibility - Rolling upgrade: queries or MTMVs calling `datasketches_hll_union` require BE nodes carrying the new aggregate function (older BEs reject the unknown function with an error). - No Thrift/Protobuf, storage format, or EditLog/image changes; downgrade is safe when no MTMV uses `datasketches_hll_union`. ### Related - Related PRs: #63143, #67469, #69257 ### Use case CREATE MATERIALIZED VIEW mv_min_hll BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL DISTRIBUTED BY RANDOM BUCKETS 2 PROPERTIES ('replication_num' = '1') AS SELECT date_trunc(event_ts, 'hour') AS hour_ts, date_trunc(event_ts, 'minute') AS min_ts, k, datasketches_hll_union(sk, 12, 'HLL_8') AS sk_union FROM ds_hll_mv_events GROUP BY date_trunc(event_ts, 'hour'), date_trunc(event_ts, 'minute'), k; -- Rewritten to datasketches_hll_union_agg(sk_union, 8) since 8 <= 12: SELECT date_trunc(event_ts, 'hour') AS hour_ts, k, CAST(ROUND(datasketches_hll_union_agg(sk, 8)) AS BIGINT) AS uv FROM ds_hll_mv_events GROUP BY date_trunc(event_ts, 'hour'), k; ### Related issues _No response_ ### Are you willing to submit PR? - [x] Yes I am willing to submit a PR! ### Code of Conduct - [x] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct) -- 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]
