Impala Public Jenkins has submitted this change and it was merged. ( 
https://gerrit.cloudera.org/24982 )

Change subject: IMPALA-15459: Add Iceberg-encoded theta sketch flavor for 
Puffin NDV stats
......................................................................

IMPALA-15459: Add Iceberg-encoded theta sketch flavor for Puffin NDV stats

COMPUTE STATS will write column NDV statistics as Iceberg Puffin
apache-datasketches-theta-v1 blobs (IMPALA-15004). For cross-engine
interop (Trino, Spark) each value must be hashed into the sketch using
Iceberg Appendix D single-value serialization.

Impala's ds_theta_sketch() hashes values via the DataSketches typed
update() overloads, which widen INT to an 8-byte integer and FLOAT to a
double, and canonicalize DOUBLE -0.0 and NaN values. This matches Hive's
datasketches UDF but not the Iceberg Puffin spec. ds_theta_sketch() also
does not support BOOLEAN, DATE, TIMESTAMP, DECIMAL and UUID, so it
cannot be used for those column types at all.

This patch adds an undocumented aggregate flavor,
ds_theta_sketch_iceberg(), that hashes every Iceberg column type
Impala computes NDV for with the Appendix D encoding. COMPUTE STATS
(IMPALA-15004) uses it when writing Puffin NDV blobs. The user-facing
ds_theta_sketch() is unchanged.

  Type      | ds_theta_sketch()     | ds_theta_sketch_iceberg()
  ----------+-----------------------+---------------------------
  BOOLEAN   | not supported         | 1 byte
  INT       | widened to 8 bytes    | 4-byte LE
  BIGINT    | 8-byte LE             | 8-byte LE (same symbol)
  FLOAT     | widened to double     | 4-byte LE
  DOUBLE    | 8-byte LE, -0.0 and   | 8-byte LE raw bits
            | NaNs canonicalized    |
  STRING    | raw bytes             | raw bytes (same symbol)
  DATE      | not supported         | 4-byte LE day count
  TIMESTAMP | not supported         | 8-byte LE micros since epoch
  DECIMAL   | not supported         | unscaled, BE two's-complement,
            |                       | minimum bytes
  UUID      | not supported         | 16 bytes BE (STRING symbol)

TINYINT and SMALLINT have no ds_theta_sketch_iceberg() overload of their
own, as Iceberg has no such column types; they are implicitly cast to
INT and get its encoding.

TIMESTAMP is the only lossy conversion: Impala timestamps have
nanosecond precision and are floored to microseconds, so two timestamps
that differ only below the microsecond are counted as one value. This
does not affect Iceberg, whose timestamp type has microsecond precision.

TIMESTAMP values are hashed without time zone conversion. COMPUTE
STATS (IMPALA-15004) runs the sketch query with TIMEZONE=UTC, so that
'timestamptz' columns, which the scanner converts to the session time
zone, are hashed in UTC as well.

UUID columns are not covered by COMPUTE STATS yet, but registering
ds_theta_sketch_iceberg() for them makes it trivial to add them later.

Changes:
- BE: added AggregateFunctions::DsThetaUpdateIceberg<T> with Appendix D
  specialisations for BOOLEAN, INT, FLOAT, DOUBLE, DATE, TIMESTAMP and
  DECIMAL, sharing a DsThetaUpdateRaw() helper. DECIMAL handles all
  three backing types (val4/val8/val16) based on the byte size of the
  argument type, which codegen turns into a constant.
- BE: extracted the minimal two's-complement trimming from
  ColumnStats<DecimalValue>::SerializeIcebergSingleValue (Parquet
  Iceberg stats) into BitUtil::TrimSignExtension() and reused it for
  DECIMAL.
- FE: registered ds_theta_sketch_iceberg() in BuiltinsDb.java via a
  new DS_THETA_ICEBERG_UPDATE_SYMBOL map; BIGINT, STRING and UUID reuse
  the ds_theta_sketch() update symbols for BIGINT and STRING.

Testing:
- datasketches-theta.test: distinct counts and exact-byte checks
  against the expected Appendix D encoding for every supported type,
  DOUBLE -0.0/NaN handling, and a comparison with ds_theta_sketch().
  The exact-byte checks force codegen, as COMPUTE STATS uses it.
- test_datasketches.py: test_theta lets the .test file set
  disable_codegen_rows_threshold.
- aggregate-functions-test.cc:
  DsThetaSketch.IcebergDataToSketchTimestamp for the TIMESTAMP
  conversion, including pre-epoch and sub-microsecond values.

Regression:
- test_datasketches.py::TestDatasketches::test_theta: existing
  ds_theta_sketch() behaviour, including Hive-written sketches.
- aggregate-functions-test.cc DsThetaSketch.*: theta UDA lifecycle.
- serialize-single-value-test.cc: Parquet Iceberg single-value
  serialization, refactored to use BitUtil::TrimSignExtension().
- bit-util-test.cc: BitUtil helpers.

Assisted-by: Claude Opus 4.8 (GitHub Copilot)
Change-Id: I78c76c38dffa9807eb13ad465afab0b80f31984a
Reviewed-on: https://gerrit.cloudera.org/24982
Reviewed-by: Impala Public Jenkins <[email protected]>
Tested-by: Impala Public Jenkins <[email protected]>
---
M be/src/exec/parquet/parquet-column-stats.inline.h
M be/src/exprs/aggregate-functions-ir.cc
M be/src/exprs/aggregate-functions-test.cc
M be/src/exprs/aggregate-functions.h
M be/src/util/bit-util.h
M fe/src/main/java/org/apache/impala/catalog/BuiltinsDb.java
M testdata/workloads/functional-query/queries/QueryTest/datasketches-theta.test
M tests/query_test/test_datasketches.py
8 files changed, 494 insertions(+), 12 deletions(-)

Approvals:
  Impala Public Jenkins: Looks good to me, approved; Verified

--
To view, visit https://gerrit.cloudera.org/24982
To unsubscribe, visit https://gerrit.cloudera.org/settings

Gerrit-Project: Impala-ASF
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I78c76c38dffa9807eb13ad465afab0b80f31984a
Gerrit-Change-Number: 24982
Gerrit-PatchSet: 9
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]>
Gerrit-Reviewer: Zoltan Borok-Nagy <[email protected]>

Reply via email to