924060929 opened a new pull request, #71002:
URL: https://github.com/apache/doris/pull/71002

   ### 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 full-table 
`MERGE_PARTITION_TEMPLATE` queries can deduplicate distinct values across 
partitions. Previously, `ANALYZE TABLE ... PARTITION(p1, ...) WITH SAMPLE` was 
rejected on OLAP tables, and when `enable_partition_analyze = true`, partitions 
exceeding `huge_partition_lower_bound_rows` were skipped in 
`partition_statistics` during automatic analysis.
   
   ### What problem does this PR solve?
   
   Issue Number: close #70991
   Related PR: #71001 (depends on)
   PR Type: Feature
   
   Problem Summary:
   Users with large partitioned OLAP tables could not sample individual 
partitions (`ANALYZE TABLE t PARTITION(p1) WITH SAMPLE ...`), and automatic 
partition analysis skipped partitions larger than 
`huge_partition_lower_bound_rows`, leaving `partition_statistics` incomplete 
for partition-pruned queries. Simply inserting sampled values into `ndv HLL` 
undercounts high-cardinality columns because an `HLL` over a 400K-row sample 
cannot exceed 400K distinct values on a 100M-row partition, whereas storing 
only a scalar NDV without `HLL` overcounts low-cardinality values shared across 
partitions. This PR decomposes partition NDV into `(ndv HLL, unseen_ndv 
BIGINT)`—preserving exact `HLL_UNION` deduplication for observed sample values 
while scaling unobserved distinct values via DUJ1—and executes partition-scoped 
fixed-block / key-range-block sampling batched across columns directly into 
`partition_statistics`. Existing clusters automatically add the nullable 
`unseen_ndv` column via light
  schema change on startup, and existing full-partition rows (`unseen_ndv IS 
NULL` or `0`) retain identical behavior.
   
   ### Details
   
   - **Change map:** 27 files (+1,471/-77): FE production code +1,176/-62 
across 20 files, unit and regression tests +295/-15 across 7 files:
     1. Partition schema & model (~130 lines: `InternalSchema`, 
`InternalSchemaInitializer`, `StatisticConstants`, `StatisticsRepository`, 
`Hll128`, `PartitionColumnStatistic`, `PartitionColumnStatisticBuilder`, 
`ShowColumnStatsCommand`): add nullable `unseen_ndv BIGINT`, light schema 
evolution, defensive `Hll128.copy()` on partition merge, and effective NDV 
queries/merges.
     2. Partition-scoped sampling & multi-column batching (~500 lines: 
`IndexSamplingSnapshot`, `TableStatisticsSqlBuilder`, 
`PartitionSampleBatchCollector`, `AnalysisJob`, `BaseAnalysisTask`, 
`OlapAnalysisTask`): build single-partition `TabletSamplingScope`, batch up to 
16 columns per sampled partition into a single `INSERT INTO 
partition_statistics`, and route explicit `PARTITION(...) WITH SAMPLE` plus 
huge partitions in `doPartitionTable()`.
     3. Command validation & auto-analyze (~50 lines: `LogicalPlanBuilder`, 
`AnalyzeTableCommand`, `AnalysisManager`, `StatisticsUtil`): fix `PARTITION(*)` 
star flag in `visitAnalyzeTable`, allow OLAP `PARTITION(...) WITH SAMPLE` when 
`enable_fixed_block_stats_sampling = true`, reject `TEMPORARY PARTITION`, scope 
`partitionUpdateRows` to analyzed partitions, and track huge partitions in 
`needAnalyzePartition`.
   - **Scope:** Supports `ANALYZE TABLE tbl PARTITION(p1, ...) WITH SAMPLE 
ROWS/PERCENT ...`, `PARTITION(*) WITH SAMPLE ...`, `force_sample_analyze = 
true` on `PARTITION(...)`, and automatic partition sampling of huge partitions 
(`> huge_partition_lower_bound_rows`) when `enable_fixed_block_stats_sampling = 
true`, across `DUP_KEYS`, `UNIQUE_KEYS` (MOW and MOR), `AGG_KEYS` (hash and 
random distribution), rollups, and synchronous materialized views. Temporary 
partitions (`TEMPORARY PARTITION(...)`), unpartitioned OLAP tables with 
`PARTITION(...)`, and partition-scoped OLAP sampling with 
`enable_fixed_block_stats_sampling = false` are rejected with an 
`AnalysisException`.
   - **Design:** Stores observed sample values in `partition_statistics.ndv` 
(`HLL`) and the non-negative residual `GREATEST(0, est_ndv - 
HLL_CARDINALITY(ndv))` in `partition_statistics.unseen_ndv` (`BIGINT NULL`). 
Cross-partition merges (`MERGE_PARTITION_TEMPLATE` and 
`PartitionColumnStatisticBuilder.merge`) compute `LEAST(non_null_rows, 
HLL_CARDINALITY(HLL_UNION(ndv)) + SUM(unseen_ndv))` when any merged partition 
has `unseen_ndv > 0` (and pure `HLL_CARDINALITY(HLL_UNION(ndv))` when all 
partitions are full scans). Batches up to `statistics_sample_batch_columns` 
(default `16`) columns per sampled partition into a single `INSERT INTO 
__internal_schema.partition_statistics ... WITH sample_base AS (...)` query so 
`HLL` values stay inside BE vector execution without base64 round-trips to FE.
   - **Usage:**
     ```sql
     ANALYZE TABLE lineitem PARTITION(p_202611) WITH SAMPLE ROWS 400000 WITH 
SYNC;
     SHOW COLUMN STATS lineitem PARTITION(p_202611);
     ```
   - **Default behavior:** Enabled by default whenever 
`enable_fixed_block_stats_sampling = true` (default `true`). Table-wide 
`ANALYZE TABLE tbl WITH SAMPLE ...` with `enable_partition_analyze = false` 
(default `false`) continues to run a single table-wide sample scan.
   - **Compatibility:** `__internal_schema.partition_statistics` adds nullable 
value column `unseen_ndv BIGINT NULL`; `InternalSchemaInitializer` applies this 
light schema change in place on upgraded clusters. Legacy rows with `unseen_ndv 
= NULL` evaluate to `0`. No Thrift or Protobuf changes.
   - **Persistence and failure handling:** Schema evolution uses standard 
`AlterTableCommand` (`AddColumnOp`) persisted via FE EditLog. Each sampled 
partition batch inserts directly into `partition_statistics` and refreshes 
`StatisticsCache`; empty sample draws (`n = 0`) emit zero rows so existing 
partition statistics are preserved.
   - **Behavior change:** `__internal_schema.partition_statistics` adds 
nullable column `unseen_ndv BIGINT NULL` via light schema change on startup, 
`ANALYZE TABLE ... PARTITION(...) WITH SAMPLE` is now accepted on partitioned 
OLAP tables when `enable_fixed_block_stats_sampling = true`, and huge 
partitions (`> huge_partition_lower_bound_rows`) under 
`enable_partition_analyze = true` are sampled into `partition_statistics` 
instead of being skipped.
   
   ### Testing
   
   - FE Checkstyle (`mvn checkstyle:check -pl fe-core`) and 120 FE unit tests 
across `OlapPartitionSampleTest`, `AnalyzeTest`, `OlapAnalysisTaskTest`, 
`ColumnMinMaxBatchCollectorTest`, `ColumnSampleBatchCollectorTest`, 
`InternalSchemaInitializerTest`, `InternalSchemaAlterTest`, and 
`StatsCalculatorTest` passed locally.
   - Ran `regression-test/suites/statistics/test_fixed_block_analyze.groovy` 
(covering `PARTITION(p1)` and `PARTITION(*)` block sampling across `DUP_KEYS`, 
MOW, MOR, `AGG_KEYS`, `RANDOM` `AGG_KEYS`, and synchronous MVs) on a local 
FE+BE cluster — passed.
   
   ### Notes for Reviewers
   
   - Stacked on #71001; review top commit `74e8f41cd1e` for the standalone diff 
until #71001 merges.
   
   ### Release note
   
   Support block-sampled partition statistics (`ANALYZE TABLE ... 
PARTITION(...) WITH SAMPLE`) and automatic block sampling of huge partitions on 
OLAP tables, preserving both cross-partition HLL deduplication and 
high-cardinality NDV scaling.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - Does this need documentation: Yes (update `ANALYZE` syntax documentation 
in `apache/doris-website` to note OLAP `PARTITION(...) WITH SAMPLE` support)
   - Pre-submission status: Ready
   
   ### AI Disclosure
   
   - Tool: Antigravity (Claude Opus 4.6)
   - Generated: code, tests, this description
   - Human review: not stated
   


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