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

   ### 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` + `NestedColumnPruning` already prune scans down to 
accessed scalar subpaths. However, the statistics subsystem treated `VARIANT` 
and `STRUCT` columns as opaque unsupported complex types.
   
   ### Current behavior
   
   1. `ANALYZE TABLE` skips `VARIANT` and `STRUCT` columns completely, 
collecting no subcolumn NDV, null count, data size, min/max, or hot values.
   2. Nereids CBO (`StatsCalculator` and `ExpressionEstimation`) returns 
`ColumnStatistic.UNKNOWN` for pruned `VARIANT` subcolumn `SlotReference`s 
(`v['uid']`), `STRUCT` field accesses (`s.addr.zip`), and string/date 
`CAST(v['city'] AS STRING)` predicates, falling back to heuristic selectivities 
and setting `planned with unknown column statistics` (`hasUnknownColStats = 
true`).
   
   ### Expected behavior
   
   1. `ANALYZE TABLE` (full, block-sampled, partition-scoped, histogram, and 
external lakehouse batched-sample) automatically discovers and collects 
statistics for exact predefined scalar `VariantField`s and nested scalar 
`StructField`s (up to depth `4`, `64` subcolumns per root column), while also 
supporting explicit ``ANALYZE TABLE tbl(v, `s.addr.zip`)``, `SHOW COLUMN 
STATS`, `DROP STATS`, and `ALTER TABLE ... MODIFY COLUMN ... SET STATS`.
   2. Nereids CBO resolves subcolumn statistics across `StatsCalculator`, 
`ExpressionEstimation.visitElementAt` (by field name or 1-based ordinal), and 
`ExpressionEstimation.castMinMax`, and avoids poisoning `hasUnknownColStats` 
when all accessed subcolumns of a `VARIANT` or `STRUCT` container slot have 
known statistics.
   
   ### Proposed design
   
   1. Add `SubcolumnStatisticsUtil` to deterministically discover addressable 
scalar subcolumns from `VariantType.getPredefinedFields()` (exact paths without 
`*`, `?`, `[`, `]`, respecting `variant_enable_flatten_nested` / dot-escaping) 
and `StructType.getFields()`, encoding canonical IDs as `rootCol + "." + 
PropertyAnalyzer.encodeVariantPathComponent(seg)...` (`%` -> `%25`, `.` -> 
`%2E`) so IDs never contain SQL quotes or backslashes and require zero 
`__internal_schema` DDL changes.
   2. Wire subcolumn SQL expressions (`CAST(element_at(...) AS <type>)` for 
`VARIANT`, `element_at(...)` for `STRUCT`) into `BaseAnalysisTask`, 
`OlapAnalysisTask` (`prefersInSampleBounds = true`), `HistogramTask`, 
`ColumnSampleBatchCollector`, `PartitionSampleBatchCollector`, 
`ExternalSampleBatchCollector`, and stats DDL/show/cleaner commands, giving 
physical top-level columns precedence on any name collision.
   3. Update `StatsCalculator` (`statsColumnKey` and 
`isContainerSlotCoveredBySubcolumnStats` using symbolic `displayAllAccessPaths` 
/ `displayPredicateAccessPaths` across OLAP and field-id external scans) and 
`ExpressionEstimation` (`visitElementAt`, `castMinMax`).
   - Rejected alternatives: (a) collecting dynamic unindexed `VARIANT` keys by 
scanning JSON metadata, which causes unbounded cardinality explosion in 
`column_statistics`; (b) adding a separate subcolumn metadata column to 
`column_statistics`, which requires an internal schema migration when canonical 
percent-encoded `col_id`s are already collision-free.
   
   ### Impact
   
   OLAP and external tables containing `VARIANT` with predefined scalar fields 
or `STRUCT` with scalar fields (`fe-core`). `ARRAY`, `MAP`, `JSON`, and untyped 
`VARIANT` columns remain unchanged.
   
   ### Reproduction or evidence
   
   Verified via 87 FE unit tests (`SubcolumnStatisticsTest`, `AnalyzeTest`, 
`OlapAnalysisTaskTest`, `ExpressionEstimationTest`, `StatsCalculatorTest`, 
etc.) and live-cluster `test_subcolumn_stats.groovy` + Iceberg `STRUCT` 
subcolumn `EXPLAIN` verification (`stats=10`, `stats=50`, `stats=36`, 
`stats=3.04`, without `planned with unknown column statistics`).
   
   ### Scope and non-goals
   
   Covers predefined scalar `VariantField`s (non-wildcard) and nested scalar 
`StructField`s up to depth `4` (`<= 64` per root column). Wildcard `VARIANT` 
patterns, unindexed dynamic `VARIANT` keys, `ARRAY`, `MAP`, and `JSON` remain 
out of scope.
   
   ### Acceptance criteria
   
   - Full, block-sampled, partition-scoped, and external sampled `ANALYZE` 
populate canonical subcolumn stats rows (`v.uid`, `v.profile.city`, 
`s.addr.zip`).
   - `SHOW COLUMN STATS`, `DROP STATS`, and `ALTER TABLE ... MODIFY COLUMN ... 
SET STATS` work on both root container names and backticked subcolumn IDs.
   - Nereids `EXPLAIN PHYSICAL PLAN` estimates exact subcolumn filter 
selectivities without marking `planned with unknown column statistics`.
   
   ### Risks and compatibility
   
   Pure FE change; zero Thrift, Protobuf, EditLog, or `__internal_schema` DDL 
changes. Rolling upgrades and existing statistics rows remain fully compatible.
   
   ### Related
   
   Completes complex-type subcolumn coverage for the OLAP & Lakehouse 
statistics series.
   


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