You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "andygrove (via GitHub)" <gi...@apache.org> on 2026/05/21 17:28:52 UTC
[PR] fix: make BloomFilter intermediate buffer Spark-compatible [datafusion-comet]
andygrove opened a new pull request, #4390:
URL: https://github.com/apache/datafusion-comet/pull/4390
## Which issue does this PR close?
Closes #2889.
Stacks on #4015.
## Rationale for this change
Comet's `SparkBloomFilter::state_as_bytes` emitted only the raw bit array in native endianness, with no header. Spark's `BloomFilterImpl.writeTo` emits a header (version, num_hash_functions, optional seed for V2, num_words) followed by big-endian `u64` bit words. The mismatch crashed mixed Spark-Partial / Comet-Final (and the inverse) execution with `Cannot merge SparkBloomFilters with different lengths.` (#2889).
#4015 hides the crash by tagging `BloomFilterAggregate` as not `supportsMixedPartialFinal`. This PR fixes the root cause: Comet's intermediate buffer now matches Spark's `writeTo` output byte-for-byte, and `merge_filter` parses + validates the header before merging. With buffers wire-compatible, `BloomFilterAggregate` joins the `supportsMixedPartialFinal = true` set.
## What changes are included in this PR?
- `SparkBloomFilter::state_as_bytes` delegates to the existing `spark_serialization()`.
- `SparkBloomFilter::merge_filter` parses the Spark header, asserts `version` / `num_hash_functions` / `seed` / `num_words` match `self`, and OR-merges big-endian `u64` words.
- `CometBloomFilterAggregate` overrides `supportsMixedPartialFinal = true`.
- `CometObjectHashAggregateExec` gains a `getSupportLevel` override that mirrors `CometHashAggregateExec`'s `COMET_ENABLE_{PARTIAL,FINAL}_HASH_AGGREGATE` test-knob handling, so `ObjectHashAggregateExec` plans can be exercised in the same way.
- Native unit tests for V1 / V2 round-trip and explicit mismatch rejection cases.
- `CometExecRuleSuite` cases for both mixed-direction BloomFilter plans, mirroring the MIN/MAX coverage added in #4015.
This PR was scaffolded with the project's `superpowers:writing-plans` / `superpowers:subagent-driven-development` skills.
## How are these changes tested?
- Native: `cargo test -p datafusion-comet-spark-expr bloom_filter::` covers V1 / V2 round-trip and four mismatch-rejection cases.
- JVM: `CometExecRuleSuite` mixed BloomFilter tests (both directions).
- End-to-end: Spark's `BloomFilterAggregateQuerySuite` now exercises the mixed path via CI (previously surfaced #2889).
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: make BloomFilter intermediate buffer Spark-compatible [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #4390:
URL: https://github.com/apache/datafusion-comet/pull/4390#issuecomment-4522144948
> @mbutrovich This got merged without running CI on the most recent commit - not sure how that was possible
It was intentional that CI did not run on the commit, but GitHub should not have allowed it to be merged.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: make BloomFilter intermediate buffer Spark-compatible [datafusion-comet]
Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on PR #4390:
URL: https://github.com/apache/datafusion-comet/pull/4390#issuecomment-4522137785
@mbutrovich This got merged without running CI on the most recent commit - not sure how that was possible
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: make BloomFilter intermediate buffer Spark-compatible [datafusion-comet]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on PR #4390:
URL: https://github.com/apache/datafusion-comet/pull/4390#issuecomment-4522156874
> It was intentional that CI did not run on the commit, but GitHub should not have allowed it to be merged.
My bad, I saw the ready to merge button when I should have checked that it actually ran the actions.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: make BloomFilter intermediate buffer Spark-compatible [datafusion-comet]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich merged PR #4390:
URL: https://github.com/apache/datafusion-comet/pull/4390
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: make BloomFilter intermediate buffer Spark-compatible [datafusion-comet]
Posted by "mbutrovich (via GitHub)" <gi...@apache.org>.
mbutrovich commented on code in PR #4390:
URL: https://github.com/apache/datafusion-comet/pull/4390#discussion_r3284098242
##########
native/spark-expr/src/bloom_filter/spark_bloom_filter.rs:
##########
@@ -272,16 +272,59 @@ impl SparkBloomFilter {
}
pub fn state_as_bytes(&self) -> Vec<u8> {
- self.bits.to_bytes()
+ self.spark_serialization()
}
pub fn merge_filter(&mut self, other: &[u8]) {
Review Comment:
`merge_filter` is `pub fn ... -> ()` and panics via `assert_eq!` on every header mismatch. Its only caller is `Accumulator::merge_batch` in `bloom_filter_agg.rs:176`, which already returns `Result`. Threading these through as `DataFusionError::Internal` would let a corrupt or truncated intermediate buffer surface as a query failure rather than crashing the executor process.
##########
native/spark-expr/src/bloom_filter/spark_bloom_filter.rs:
##########
@@ -272,16 +272,59 @@ impl SparkBloomFilter {
}
pub fn state_as_bytes(&self) -> Vec<u8> {
- self.bits.to_bytes()
+ self.spark_serialization()
}
pub fn merge_filter(&mut self, other: &[u8]) {
+ let mut offset = 0;
+
+ let version_int = read_num_be_bytes!(i32, 4, other[offset..]);
+ offset += 4;
+ assert_eq!(
+ version_int,
+ self.version.to_int(),
+ "BloomFilter merge: version mismatch (got {}, expected {})",
+ version_int,
+ self.version.to_int(),
+ );
+
+ let num_hash = read_num_be_bytes!(i32, 4, other[offset..]) as u32;
+ offset += 4;
+ assert_eq!(
+ num_hash, self.num_hash_functions,
+ "BloomFilter merge: num_hash_functions mismatch (got {}, expected {})",
+ num_hash, self.num_hash_functions,
+ );
+
+ if let SparkBloomFilterVersion::V2 = self.version {
+ let seed = read_num_be_bytes!(i32, 4, other[offset..]);
+ offset += 4;
+ assert_eq!(
+ seed, self.seed,
+ "BloomFilter merge: seed mismatch (got {}, expected {})",
+ seed, self.seed,
+ );
+ }
+
+ let num_words = read_num_be_bytes!(i32, 4, other[offset..]) as usize;
+ offset += 4;
assert_eq!(
- other.len(),
- self.bits.byte_size(),
- "Cannot merge SparkBloomFilters with different lengths."
+ num_words,
+ self.bits.word_size(),
+ "BloomFilter merge: num_words mismatch (got {}, expected {})",
+ num_words,
+ self.bits.word_size(),
);
- self.bits.merge_bits(other);
+
+ let words = self.bits.data();
Review Comment:
This clones `self.bits.data()` into `words`, builds a new `Vec` of the same size, and then constructs a fresh `SparkBitArray` whose `new` re-scans the words to recompute `bit_count`. Two allocations and two passes per merge. An in-place variant that ORs into `self.bits.data` directly and accumulates `bit_count` in the same loop would avoid both.
##########
spark/src/main/scala/org/apache/spark/sql/comet/operators.scala:
##########
@@ -1697,6 +1697,20 @@ object CometObjectHashAggregateExec
override def enabledConfig: Option[ConfigEntry[Boolean]] = Some(
CometConf.COMET_EXEC_AGGREGATE_ENABLED)
+ override def getSupportLevel(op: ObjectHashAggregateExec): SupportLevel = {
Review Comment:
The body here is identical to `CometHashAggregateExec.getSupportLevel` at `operators.scala:1658-1670`, including the conf names. That is fine for the test-knob purpose called out in the comment, but `COMET_ENABLE_PARTIAL_HASH_AGGREGATE` and `COMET_ENABLE_FINAL_HASH_AGGREGATE` now gate both `HashAggregateExec` and `ObjectHashAggregateExec`. As a follow-up, consider renaming to `COMET_ENABLE_PARTIAL_AGGREGATE` / `COMET_ENABLE_FINAL_AGGREGATE` so the conf names match the scope.
##########
native/spark-expr/src/bloom_filter/spark_bloom_filter.rs:
##########
@@ -396,4 +439,96 @@ mod tests {
buf.extend_from_slice(&[0u8; 32]); // 4 words * 8 bytes
let _ = SparkBloomFilter::from(buf.as_slice());
}
+
+ /// Two V1 filters with identical parameters. Populate the first, serialize via
+ /// state_as_bytes, merge into the empty second, and verify the second contains
+ /// everything the first did. Exercises the aggregator state → merge_batch path.
+ #[test]
+ fn state_round_trip_v1_merge() {
+ let num_bits = 1024;
+ let num_hash = optimal_num_hash_functions(100, num_bits);
+ let mut a = SparkBloomFilter::new(SparkBloomFilterVersion::V1, num_hash, num_bits, 0);
+ for v in [1_i64, 7, 42, 99, -3, i64::MAX] {
+ a.put_long(v);
+ }
+
+ let mut b = SparkBloomFilter::new(SparkBloomFilterVersion::V1, num_hash, num_bits, 0);
+ b.merge_filter(&a.state_as_bytes());
Review Comment:
Nice that these go through `state_as_bytes` then `merge_filter`. The pre-existing `v1_round_trip` and `v2_round_trip` tests above use `SparkBloomFilter::from`, which was always header-aware, so they would not have caught the bug this PR fixes. The new cases are exactly the right round-trip path
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org