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
Open
Conversation
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.
[skip ci]
…tch heap [skip ci]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
datasketchescrate (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 viaspark.comet.expression.<name>.allowIncompatible=trueto 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).
SparkHllSketch/SparkHllUnionwrapper over thedatasketchescrate that fixes the target type to HLL_8 and routes input hashing through the crate's byte-exacthash_valuewrappers to match Spark.hll_sketch_aggandhll_union_agg, and native scalar functions forhll_sketch_estimateandhll_union, wired through protobuf and the DataFusion planner.sparkVersionSpecificAggregatesshim hook so a Spark-4.0+-only aggregate can register without breaking the 3.4/3.5 builds.spark-4.xsource tree, each reported asIncompatible(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)How are these changes tested?
hll_sketch_aggis deserialized and estimated by Comet.-Pspark-4.0that 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-defaultlgConfigKand integer/string inputs.