924060929 opened a new issue, #70991:
URL: https://github.com/apache/doris/issues/70991

   ### Background
   
   Apache Doris stores per-partition OLAP column statistics in 
`__internal_schema.partition_statistics` using an `HLL` sketch (`ndv`) so 
partition-pruned queries in `StatsCalculator` and cross-partition merges 
(`MERGE_PARTITION_TEMPLATE`) can deduplicate distinct values across partitions. 
However, partition-scoped OLAP sampling was unsupported, leaving users unable 
to bound `ANALYZE` cost on large individual partitions.
   
   ### Current behavior
   
   1. Explicit partition-scoped sampling on OLAP tables fails:
      ```sql
      ANALYZE TABLE part_tbl PARTITION(p1) WITH SAMPLE ROWS 400000 WITH SYNC;
      -- AnalysisException: Partition-scoped sample analyze is not supported 
for OLAP table ...
      ```
   2. When `enable_partition_analyze = true`, partitions larger than 
`huge_partition_lower_bound_rows` (`100,000,000`) are skipped during automatic 
partition analysis, leaving `partition_statistics` missing for the largest 
partitions.
   
   ### Expected behavior
   
   `ANALYZE TABLE tbl PARTITION(p1, ...) WITH SAMPLE ...`, `PARTITION(*) WITH 
SAMPLE ...`, and automatic partition analysis of huge partitions execute 
partition-scoped `FIXED_BLOCK` / `KEY_RANGE_BLOCK` sampling batched across 
columns directly into `__internal_schema.partition_statistics`, preserving both 
cross-partition `HLL` deduplication for low-cardinality values and DUJ1 scaling 
for high-cardinality columns.
   
   ### Proposed design
   
   1. Decompose partition NDV into `(ndv HLL, unseen_ndv BIGINT NULL)` on 
`__internal_schema.partition_statistics` (applied automatically via light 
schema change in `InternalSchemaInitializer`):
      - `ndv` stores `HLL_HASH` over observed sample rows so low-cardinality 
values shared across partitions deduplicate via `HLL_UNION`.
      - `unseen_ndv` stores `GREATEST(0, est_ndv - HLL_CARDINALITY(ndv))` so 
high-cardinality columns are not capped at sample size `n`.
      - Cross-partition merges (`MERGE_PARTITION_TEMPLATE` and 
`PartitionColumnStatisticBuilder.merge`, cloning `Hll128.copy()` to prevent 
in-place cache mutation) evaluate `LEAST(non_null_rows, 
HLL_CARDINALITY(HLL_UNION(ndv)) + SUM(unseen_ndv))`.
   2. Add `PartitionSampleBatchCollector` to batch up to 
`statistics_sample_batch_columns` (`16`) columns per sampled partition into a 
single `INSERT INTO __internal_schema.partition_statistics ... WITH sample_base 
AS (...)` query so `HLL` sketches never round-trip through FE SQL literals.
   3. Fix `LogicalPlanBuilder.visitAnalyzeTable` to pass `ctx.star != null` 
into `PartitionNamesInfo`, validate OLAP `PARTITION(...)` commands cleanly, and 
allow `needAnalyzePartition` to schedule huge partitions when 
`enable_fixed_block_stats_sampling = true`.
   - Rejected alternatives: (a) storing only sampled `HLL` without 
`unseen_ndv`, which caps NDV at sample row count; (b) replacing `ndv HLL` with 
a scalar `BIGINT`, which sums overlapping dictionary values across partitions.
   
   ### Impact
   
   Partitioned OLAP tables across `DUP_KEYS`, `UNIQUE_KEYS` (MOW/MOR), 
`AGG_KEYS` (HASH/RANDOM), rollups, and synchronous MVs (`fe-core` and 
`__internal_schema.partition_statistics`).
   
   ### Reproduction or evidence
   
   Verified via 120 FE unit tests (`OlapPartitionSampleTest`, `AnalyzeTest`, 
`InternalSchemaInitializerTest`, `InternalSchemaAlterTest`, 
`StatsCalculatorTest`) and live-cluster `test_fixed_block_analyze.groovy`.
   
   ### Scope and non-goals
   
   Covers permanent OLAP partition block sampling (`PARTITION(p1, ...)`, 
`PARTITION(*)`, `force_sample_analyze`, and `enable_partition_analyze` huge 
partitions). Temporary partitions (`TEMPORARY PARTITION(...)`) remain rejected.
   
   ### Acceptance criteria
   
   - `ANALYZE TABLE tbl PARTITION(p1) WITH SAMPLE ...` and `PARTITION(*) WITH 
SAMPLE ...` populate `partition_statistics` across DUP/MOW/MOR/AGG/MV indexes.
   - `InternalSchemaInitializer` adds `unseen_ndv BIGINT NULL` automatically on 
existing clusters; legacy `NULL` rows behave identically to `0`.
   - Cross-partition merges deduplicate shared low-cardinality values via 
`HLL_UNION` while scaling high-cardinality `unseen_ndv`.
   
   ### Risks and compatibility
   
   No Thrift or Protobuf changes. Light schema change (`ADD COLUMN unseen_ndv 
BIGINT NULL`) is non-blocking and backward-compatible with existing 
`partition_statistics` data.
   
   ### Related
   
   Builds on OLAP multi-column batched block sampling (`FIXED_BLOCK` and 
`KEY_RANGE_BLOCK`).
   


-- 
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]

Reply via email to