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

   ### Background
   
   Apache Doris stores typed `VARIANT` predefined subcolumns (`VARIANT<'uid': 
INT, 'profile.city': STRING>`) and `STRUCT` scalar fields (`STRUCT<age: INT, 
addr: STRUCT<city: VARCHAR(32), zip: INT>>`) as columnar sub-streams, and 
`VariantSubPathPruning` plus `NestedColumnPruning` prune scans to accessed 
scalar subpaths. However, the statistics subsystem treated `VARIANT` and 
`STRUCT` columns as opaque unsupported complex types, skipping them during 
`ANALYZE` and returning `ColumnStatistic.UNKNOWN` during Nereids cardinality 
estimation.
   
   ### What problem does this PR solve?
   
   Issue Number: close #70993
   Related PR: #71003 (depends on)
   PR Type: Feature
   
   Problem Summary:
   
   Queries filtering, joining, or grouping on `VARIANT` predefined scalar 
subcolumns or `STRUCT` scalar fields fell back to heuristic selectivities and 
set `hasUnknownColStats = true` (`planned with unknown column statistics`) 
because `ANALYZE TABLE` skipped complex columns and `StatsCalculator` / 
`ExpressionEstimation` could not resolve subcolumn paths. Discovering 
addressable scalar subcolumns from `VariantType` and `StructType` schemas 
allows full, block-sampled, partition-scoped, and external sampled `ANALYZE` to 
persist subcolumn statistics under collision-free canonical `col_id`s without 
changing internal statistics table schemas, while Nereids resolves those 
statistics across scan, `element_at`, and `CAST` estimation. Existing tables 
upgrade seamlessly and collect subcolumn statistics on the next `ANALYZE`.
   
   ### Details
   
   - **Change map:** 29 files (+1,582/-109): FE production code +932/-107 
across 26 files (59%), unit and regression tests +650/-2 across 3 files (41%):
     1. Deterministic subcolumn discovery, percent-encoded canonical `col_id`s, 
and SQL extraction expressions (`SubcolumnStatisticsUtil`, `StatisticsUtil`, 
`OlapTable`, `ExternalTable`, ~435 lines).
     2. Full, batched-sample, partition-sample, external-sample, histogram, and 
stats DDL/show/cleaner integration (`AnalyzeTableCommand`, 
`ShowColumnStatsCommand`, `DropStatsCommand`, `AlterColumnStatsCommand`, 
`OlapAnalysisTask`, `BaseAnalysisTask`, `TableStatisticsSqlBuilder`, 
`ColumnSampleBatchCollector`, `PartitionSampleBatchCollector`, 
`ExternalSampleBatchCollector`, ~245 lines).
     3. Nereids subcolumn lookup, `element_at` / `CAST` estimation, and 
container-slot unknown-stats suppression across OLAP and field-id external 
scans (`StatsCalculator`, `ExpressionEstimation`, ~250 lines).
   - **Scope:** Supports exact predefined scalar `VariantField`s (excluding 
glob wildcards `*`, `?`, `[`, `]`) and nested scalar `StructField`s up to depth 
`4` (`64` subcolumns per root column) across OLAP 
full/sample/partition/histogram analyze, external table full/batched-sample 
analyze, `SHOW COLUMN STATS`, `DROP STATS`, `ALTER TABLE ... MODIFY COLUMN ... 
SET STATS`, auto-analyze, and Nereids CBO. `ARRAY`, `MAP`, `JSON`, and untyped 
`VARIANT` columns remain unsupported.
   - **Design:** `SubcolumnStatisticsUtil` encodes subcolumn IDs as `rootCol + 
"." + PropertyAnalyzer.encodeVariantPathComponent(seg)...` (`%` -> `%25`, `.` 
-> `%2E`) in deterministic base-then-index schema order so IDs never contain 
SQL quotes or backslashes and never collide between nested paths and escaped 
literal-dot keys. Extraction expressions (`CAST(element_at(...) AS <type>)` for 
`VARIANT`, `element_at(...)` for `STRUCT`) evaluate once in scan/CTE 
projections with in-sample min/max bounds (`prefersInSampleBounds = true`). 
`StatsCalculator` resolves pruned `SlotReference` `subPath`s and checks 
symbolic `displayAllAccessPaths` / `displayPredicateAccessPaths` so 
`hasUnknownColStats` is not poisoned when all accessed subcolumns have known 
statistics; `ExpressionEstimation` resolves nested `element_at` chains (by 
field name or 1-based ordinal) and preserves `StringLiteral` and matching 
scalar literal bounds across `CAST`.
   - **Usage:**
     ```sql
     ANALYZE TABLE subcol_stats_tbl WITH SYNC;
     ANALYZE TABLE subcol_stats_tbl(v, `s.addr.zip`) WITH SAMPLE ROWS 200 WITH 
SYNC;
     SHOW COLUMN STATS subcol_stats_tbl(`v.uid`, `s.addr.zip`);
     ```
   - **Default behavior:** Active automatically whenever `ANALYZE` or 
auto-analyze runs on tables declaring `VARIANT` predefined scalar fields or 
`STRUCT` scalar fields; physical top-level columns always take precedence on 
name collisions.
   - **Compatibility:** No Thrift, Protobuf, EditLog, or `__internal_schema` 
DDL changes; rolling BE-then-FE upgrades and existing statistics rows remain 
fully compatible.
   - **Behavior change:** Full and automatic `ANALYZE` on tables with `VARIANT` 
predefined scalar fields or `STRUCT` scalar fields now populate subcolumn rows 
in `column_statistics` / `partition_statistics` (for example, `SHOW COLUMN 
STATS` includes `v.uid` and `s.addr.zip`), `DROP STATS tbl(v)` cascades to 
`v.*` subcolumns, and `EXPLAIN` no longer reports `planned with unknown column 
statistics` when all accessed subcolumns of a `VARIANT` or `STRUCT` predicate 
have collected statistics.
   
   ### Testing
   
   - Locally ran FE unit tests (`SubcolumnStatisticsTest`, `AnalyzeTest`, 
`OlapAnalysisTaskTest`, `TableStatisticsSqlBuilderTest`, 
`ColumnSampleBatchCollectorTest`, `OlapPartitionSampleTest`, 
`ExternalSampleBatchCollectorTest`, `ExpressionEstimationTest`, 
`StatsCalculatorTest`): 87/87 passed.
   - Ran `regression-test/suites/statistics/test_subcolumn_stats.groovy` and 
local Hadoop Iceberg `STRUCT` subcolumn `ANALYZE ... WITH SAMPLE` + `EXPLAIN` 
verification on a local FE+BE cluster (all passed); updated 
`regression-test/suites/statistics/analyze_stats.groovy` for `STRUCT` subcolumn 
row counts.
   
   ### Notes for Reviewers
   
   - Stacked on #71003; review top commit `cfb7107862c` for the standalone diff 
until #71003 merges.
   
   ### Release note
   
   Support statistics collection (full, block-sampled, and partition-scoped) 
and Nereids CBO cardinality estimation for predefined scalar `VARIANT` 
subcolumns and `STRUCT` scalar subfields.
   
   ### Check List (For Author)
   
   - Behavior changed: Yes (see Details)
   - Does this need documentation: Yes (plan to document `VARIANT` and `STRUCT` 
subcolumn statistics in `apache/doris-website`)
   - 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