Skip to content

feat: support Spark HyperLogLog sketch functions (hll_sketch_agg, hll_union_agg, hll_sketch_estimate, hll_union)#4802

Open
andygrove wants to merge 12 commits into
apache:mainfrom
andygrove:feat/hll-sketch-functions
Open

feat: support Spark HyperLogLog sketch functions (hll_sketch_agg, hll_union_agg, hll_sketch_estimate, hll_union)#4802
andygrove wants to merge 12 commits into
apache:mainfrom
andygrove:feat/hll-sketch-functions

Conversation

@andygrove

@andygrove andygrove commented Jul 3, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes #.

Rationale for this change

Spark 3.5+ exposes Apache DataSketches HyperLogLog functions (hll_sketch_agg, hll_union_agg, hll_sketch_estimate, hll_union) for approximate distinct counting with a persistable, mergeable sketch. This PR adds native Comet acceleration for these functions on Spark 4.0+.

The native implementation is backed by the pure-Rust datasketches crate (Apache DataSketches), so no C++ toolchain is added to the build. Cross-engine verification confirmed the crate is DataSketches-Java compatible: it uses MurmurHash3-x64-128 with the standard seed (9001) and byte-exact input hashing, so Spark can read Comet-produced sketches and vice versa. For a high-cardinality HLL-array sketch, Comet's output is byte-identical to Spark's.

Because HLL is inherently approximate, the point estimate is not guaranteed bit-identical to Spark: after a partial/final merge the sketch is flagged out-of-order and estimated via a composite estimator whose interpolation/bias tables differ slightly between the Rust crate and datasketches-java (observed ~0.7%, well within HLL's ~1.6% standard error at the default lgConfigK). The functions are therefore reported as Incompatible: by default Comet falls back to Spark for exact results, and users opt in per expression via spark.comet.expression.<name>.allowIncompatible=true to accept the small difference in exchange for native execution.

What changes are included in this PR?

Spark 4.0+ only (the functions do not exist in 3.4, and this scopes the feature to 4.x to keep the shared serde tree compiling on older versions).

  • A SparkHllSketch / SparkHllUnion wrapper over the datasketches crate that fixes the target type to HLL_8 and routes input hashing through the crate's byte-exact hash_value wrappers to match Spark.
  • Native aggregate accumulators for hll_sketch_agg and hll_union_agg, and native scalar functions for hll_sketch_estimate and hll_union, wired through protobuf and the DataFusion planner.
  • A new sparkVersionSpecificAggregates shim hook so a Spark-4.0+-only aggregate can register without breaking the 3.4/3.5 builds.
  • Scala serde for all four expressions in the spark-4.x source tree, each reported as Incompatible (opt-in).

All four functions are implemented:

  • hll_sketch_agg (native + serde)
  • hll_sketch_estimate (native + serde)
  • hll_union_agg (native + serde)
  • hll_union (native + serde)
  • End-to-end, error-behavior, and cross-engine round-trip tests

How are these changes tested?

  • Rust unit tests for the sketch wrapper, including a cross-engine regression fixture: a sketch produced by Spark's hll_sketch_agg is deserialized and estimated by Comet.
  • End-to-end Scala tests under -Pspark-4.0 that opt into the incompatible path, assert the query runs fully natively (no fallback), and assert the estimate is within HLL error of the true distinct count, across default and non-default lgConfigK and integer/string inputs.

andygrove added 11 commits July 2, 2026 17:39
Wrap the pure-Rust datasketches crate's HLL_8 sketch/union behind
SparkHllSketch/SparkHllUnion, hashing inputs via the crate's hash_value
wrappers (raw_bytes, sign_extend) so MurmurHash3-x64-128 input matches
DataSketches-Java. Verified cross-engine: Comet-produced sketches are
byte-identical to Spark hll_sketch_agg output for HLL-array mode and
mutually readable for low-cardinality List/Set mode.
Adds the two protobuf messages needed to serialize Spark's
hll_sketch_agg and hll_union_agg aggregate functions, wired into
the AggExpr oneof as field numbers 19 and 20.
Wire up HllSketchAgg as an AggregateUDFImpl backed by SparkHllSketch,
accepting Int8/16/32/64, Utf8, and Binary inputs and returning a
serialized HLL sketch as Binary. Null groups evaluate to NULL, matching
Spark's HllSketchAgg. Wires the new AggExprStruct::HllSketchAgg arm into
the native planner's create_agg_expr.

Also add a placeholder HllUnionAgg planner arm returning a clear
"not yet supported" error, since that oneof variant already exists in
the proto but its native accumulator lands in a follow-on task.
@andygrove andygrove changed the title feat: support Spark HLL sketch functions (hll_sketch_agg, hll_union_agg, hll_sketch_estimate, hll_union) feat: support Spark HyperLogLog sketch functions (hll_sketch_agg, hll_union_agg, hll_sketch_estimate, hll_union) Jul 3, 2026
@andygrove andygrove marked this pull request as ready for review July 3, 2026 04:23
@andygrove andygrove requested a review from comphead July 3, 2026 04:24
@andygrove andygrove added this to the 1.0.0 milestone Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant