Mihaly Szjatinya has uploaded a new patch set (#5). ( 
https://gerrit.cloudera.org/24986 )

Change subject: IMPALA-15004: Write NDV stats to Iceberg Puffin files in 
COMPUTE STATS
......................................................................

IMPALA-15004: Write NDV stats to Iceberg Puffin files in COMPUTE STATS

COMPUTE STATS on an Iceberg table previously stored column NDV
statistics only in the Hive Metastore (HMS). This change also writes
them to an Iceberg Puffin statistics file as standard
apache-datasketches-theta-v1 blobs, so the NDVs live with the table
metadata and interoperate with other engines such as Trino and Spark.
Impala's existing Puffin read path already consumes such files.

The new query option ICEBERG_WRITE_PUFFIN_STATS (default: true)
controls the behaviour:
- true: the column stats child query no longer computes NDV(); a
  separate child query computes a theta sketch per column with
  ds_theta_sketch_iceberg instead. catalogd writes the sketches to a
  Puffin file committed for the snapshot the stats were computed for,
  and writes their estimates as the NDVs to HMS, so HMS and Puffin
  agree.
- false: the previous behaviour (HyperLogLog NDVs, HMS only).
COMPUTE STATS with TABLESAMPLE keeps using SAMPLED_NDV() and writes no
Puffin stats, as theta sketches can't extrapolate from a sample.
COMPUTE INCREMENTAL STATS is not affected. No Puffin file is written
for a table without a snapshot.

The theta child query runs with TIMEZONE=UTC: Iceberg hashes timestamp
values as stored, and Impala converts timestamptz values read from
Iceberg tables to the session time zone. ChildQuery now supports
per-child query option overrides for this.

Writing follows Trino: the new statistics file replaces only the file
of its own snapshot, and carries over all blobs of the latest
statistics file of that snapshot or its nearest ancestor, except theta
sketches of the recomputed columns. Blobs written by other engines are
kept, with their snapshot ids and properties unchanged. If the
previous file can't be read, the new file contains only the new
blobs. A failed Puffin write only logs a warning, as HMS has the same
NDVs.

DROP STATS now also removes all statistics files of the table from
the Iceberg metadata, like Trino's drop_extended_stats. So does
TRUNCATE when it deletes the stats (DELETE_STATS_IN_TRUNCATE=true),
as the Puffin stats of older snapshots would otherwise still be read
for the empty table. The files themselves are left for
REMOVE_ORPHAN_FILES, as older metadata versions may still reference
them.

The Iceberg docs describe Puffin writing and the new DROP STATS and
TRUNCATE behaviour; the query option gets its own page.

This change depends on IMPALA-15775, which makes HMS stats win over
Puffin stats of the same snapshot.

Testing:
- AnalyzeDDLTest#TestComputeStatsIcebergPuffin: child queries
  generated with and without the query option and with TABLESAMPLE.
- IcebergPuffinStatsWriterTest: carry-over of blobs from the previous
  statistics file, unreadable previous file, ancestor lookup.
- test_iceberg.py::TestIcebergTableWithPuffinStats:
  - test_compute_stats_writes_puffin: COMPUTE STATS writes Puffin NDVs
    that are read back; HMS wins on the same snapshot.
  - test_compute_stats_write_puffin_stats_option: query option values,
    TABLESAMPLE, table without snapshot.
  - test_compute_stats_puffin_timestamps_in_utc: timestamptz sketches
    match Iceberg's bytes regardless of the session time zone.
  - test_compute_stats_puffin_keeps_other_stats: foreign blobs are
    carried over to the new statistics file.
  - test_drop_stats_removes_puffin_stats: DROP STATS unreferences the
    statistics files of HMS-integrated and hadoop.tables tables.
- test_iceberg_trino_puffin_sketches.py: byte-level and NDV interop
  between Impala and Trino Puffin theta sketches.

Regression:
- test_iceberg.py::TestIcebergTable::test_compute_stats: COMPUTE and
  DROP STATS on Iceberg tables, manual column stats.
- test_iceberg.py::TestIcebergTable::test_truncate_iceberg_tables:
  TRUNCATE clears the column stats.
- test_iceberg.py::TestIcebergTableWithPuffinStats: Puffin read path.
- test_iceberg_with_puffin.py: Puffin reading with startup flags.
- test_datasketches.py: ds_theta_sketch functions used by the child
  query.
- tests/metadata/test_compute_stats.py: COMPUTE STATS child queries.
- tests/metadata/test_stats_extrapolation.py: Iceberg NDVs with stats
  extrapolation.

Assisted-by: Claude Opus 4.8 (GitHub Copilot)
Change-Id: I0112492a9fb156b5317d8e0a6bb3dcf18ae6eeec
---
M be/src/exec/catalog-op-executor.cc
M be/src/exec/catalog-op-executor.h
M be/src/service/child-query.cc
M be/src/service/child-query.h
M be/src/service/client-request-state.cc
M be/src/service/query-options.cc
M be/src/service/query-options.h
M common/thrift/ImpalaService.thrift
M common/thrift/JniCatalog.thrift
M common/thrift/Query.thrift
M docs/impala.ditamap
M docs/topics/impala_delete_stats_in_truncate.xml
M docs/topics/impala_drop_stats.xml
M docs/topics/impala_iceberg.xml
A docs/topics/impala_iceberg_write_puffin_stats.xml
M fe/src/main/java/org/apache/impala/analysis/ComputeStatsStmt.java
M fe/src/main/java/org/apache/impala/service/CatalogOpExecutor.java
A fe/src/main/java/org/apache/impala/service/IcebergPuffinStatsWriter.java
M fe/src/test/java/org/apache/impala/analysis/AnalyzeDDLTest.java
A fe/src/test/java/org/apache/impala/service/IcebergPuffinStatsWriterTest.java
A 
testdata/workloads/functional-query/queries/QueryTest/iceberg-trino-interop-puffin-stats.test
A tests/custom_cluster/test_iceberg_trino_puffin_sketches.py
M tests/query_test/test_iceberg.py
23 files changed, 1,461 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.cloudera.org:29418/Impala-ASF refs/changes/86/24986/5
--
To view, visit https://gerrit.cloudera.org/24986
To unsubscribe, visit https://gerrit.cloudera.org/settings

Gerrit-Project: Impala-ASF
Gerrit-Branch: master
Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I0112492a9fb156b5317d8e0a6bb3dcf18ae6eeec
Gerrit-Change-Number: 24986
Gerrit-PatchSet: 5
Gerrit-Owner: Mihaly Szjatinya <[email protected]>
Gerrit-Reviewer: Csaba Ringhofer <[email protected]>
Gerrit-Reviewer: Impala Public Jenkins <[email protected]>
Gerrit-Reviewer: Mihaly Szjatinya <[email protected]>
Gerrit-Reviewer: Peter Rozsa <[email protected]>

Reply via email to