You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "sunchao (via GitHub)" <gi...@apache.org> on 2026/10/23 15:27:23 UTC

[PR] fix: merge all Bloom filter states and respect sliced offsets [datafusion-comet]

sunchao opened a new pull request, #7173:
URL: https://github.com/apache/datafusion-comet/pull/7173

   ## Which issue does this PR close?
   
   Closes #7172.
   
   ## Rationale for this change
   
   Grouped Bloom aggregates can receive several partial states for one group during spill replay. `merge_batch` currently asserts that there is exactly one row, so replay can panic. It also reads the binary array's complete backing buffer, which can include rows excluded by an Arrow slice.
   
   For example, merging partial filters containing 11 and 22 should retain both values. Passing a one-row slice containing the filter for 11 should merge only that row, even if the original buffer also contains a filter for 99.
   
   ## What changes are included in this PR?
   
   Merge each non-null binary row using its own slice. Empty batches and null states are no-ops. The existing version, hash-count, seed, and bit-array compatibility checks still apply to every merged state.
   
   This change contains only the merge fix and regression tests. The accounting changes from #6370 are already on main.
   
   ## How are these changes tested?
   
   Four unit regressions cover multi-row merges, sliced offsets, empty/null batches, and incompatible states. Multi-row and slice coverage exercise both Bloom serialization versions.
   
   A native integration test runs grouped final aggregation with an 8 MiB pool and two passes over 1,024 groups. It explicitly selects DataFusion's legacy sorted-spill path, requires actual spilling, verifies every group's exact merged bytes, rejects duplicate/missing groups, and checks that reservations are released. The repository's existing Cargo nextest job discovers this test automatically.
   
   Local `rustfmt --check` for both changed files and `git diff --check` pass. The attempted focused command was:
   
   ```sh
   cargo test --manifest-path native/Cargo.toml --locked   -p datafusion-comet-spark-expr --lib --test bloom_filter_spill bloom_filter
   ```
   
   Local test execution is blocked before compilation: the configured dependency mirror does not provide `aws-config 1.13.0`, pinned by the current upstream workspace lockfile. The new tests have not been executed locally on this upstream revision; CI results will provide runtime validation. No JVM tests or performance benchmarks were run.
   


-- 
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: merge all Bloom filter states and respect sliced offsets [datafusion-comet]

Posted by "sunchao (via GitHub) via github" <gi...@datafusion.apache.org>.
sunchao merged PR #7173:
URL: https://github.com/apache/datafusion-comet/pull/7173


-- 
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: merge all Bloom filter states and respect sliced offsets [datafusion-comet]

Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #7173:
URL: https://github.com/apache/datafusion-comet/pull/7173#discussion_r4361443553


##########
native/spark-expr/tests/bloom_filter_spill.rs:
##########
@@ -0,0 +1,160 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use arrow::array::{Array, BinaryArray, BinaryBuilder, Int64Array, RecordBatch};
+use arrow::datatypes::{DataType, Field, Schema};
+use datafusion::common::{Result, ScalarValue};
+use datafusion::datasource::memory::MemorySourceConfig;
+use datafusion::datasource::source::DataSourceExec;
+use datafusion::execution::memory_pool::{GreedyMemoryPool, MemoryPool};
+use datafusion::execution::runtime_env::RuntimeEnvBuilder;
+use datafusion::execution::TaskContext;
+use datafusion::logical_expr::AggregateUDF;
+use datafusion::physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr};
+use datafusion::physical_expr::expressions::{Column, Literal};
+use datafusion::physical_plan::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};
+use datafusion::physical_plan::{common::collect, ExecutionPlan};
+use datafusion::prelude::SessionConfig;
+use datafusion_comet_spark_expr::{BloomFilterAgg, SparkBloomFilterVersion};
+
+fn bloom_state(aggregate: &AggregateFunctionExpr, values: &[i64]) -> Result<Vec<u8>> {
+    let mut accumulator = aggregate.create_accumulator()?;
+    accumulator.update_batch(&[Arc::new(Int64Array::from_iter_values(
+        values.iter().copied(),
+    ))])?;
+    let ScalarValue::Binary(Some(state)) = accumulator.state()?.remove(0) else {
+        panic!("a nonempty Bloom filter must have a binary state");
+    };
+    Ok(state)
+}
+
+#[tokio::test]
+async fn grouped_bloom_filter_merges_spilled_states() -> Result<()> {
+    const NUM_GROUPS: usize = 1024;
+    const BATCH_SIZE: usize = 8;
+    const NUM_BITS: i64 = 131_072;
+
+    let value_schema = Arc::new(Schema::new(vec![
+        Field::new("key", DataType::Int64, false),
+        Field::new("value", DataType::Int64, false),
+    ]));
+    let aggregate_udf = Arc::new(AggregateUDF::new_from_impl(BloomFilterAgg::new(
+        Arc::new(Literal::new(ScalarValue::Int64(Some(1024)))),
+        Arc::new(Literal::new(ScalarValue::Int64(Some(NUM_BITS)))),
+        DataType::Binary,
+        SparkBloomFilterVersion::V1,
+    )));
+    let aggregate_expr = Arc::new(
+        AggregateExprBuilder::new(aggregate_udf, vec![Arc::new(Column::new("value", 1))])
+            .schema(Arc::clone(&value_schema))
+            .alias("bloom")
+            .build()?,
+    );
+    let state_schema = Arc::new(Schema::new(vec![
+        Field::new("key", DataType::Int64, false),
+        Field::new("bits", DataType::Binary, false),
+    ]));
+
+    // Each pass has one valid, nonempty partial state per group. The complete set
+    // of 16 KiB filters exceeds the pool, so the two passes meet during sorted
+    // spill replay. Distinct values detect lost states as well as merge panics.
+    let mut batches = Vec::new();
+    for pass in 0..2 {
+        for start in (0..NUM_GROUPS).step_by(BATCH_SIZE) {
+            let mut states = BinaryBuilder::new();
+            for key in start..start + BATCH_SIZE {
+                states.append_value(bloom_state(
+                    &aggregate_expr,
+                    &[(pass * NUM_GROUPS + key) as i64],
+                )?);
+            }
+            batches.push(RecordBatch::try_new(
+                Arc::clone(&state_schema),
+                vec![
+                    Arc::new(Int64Array::from_iter_values(
+                        (start..start + BATCH_SIZE).map(|key| key as i64),
+                    )),
+                    Arc::new(states.finish()),
+                ],
+            )?);
+        }
+    }
+    let scan = Arc::new(DataSourceExec::new(Arc::new(MemorySourceConfig::try_new(
+        &[batches],
+        state_schema,
+        None,
+    )?)));
+    let aggregate = AggregateExec::try_new(
+        AggregateMode::Final,
+        PhysicalGroupBy::new_single(vec![(Arc::new(Column::new("key", 0)), "key".into())]),
+        vec![Arc::clone(&aggregate_expr)],
+        vec![None],
+        scan,
+        value_schema,
+    )?;
+    let pool = Arc::new(GreedyMemoryPool::new(8 * 1024 * 1024));
+    let runtime = RuntimeEnvBuilder::new()
+        .with_memory_pool(Arc::clone(&pool) as _)
+        .build_arc()?;
+    let context = Arc::new(
+        TaskContext::default()
+            .with_runtime(runtime)
+            .with_session_config(
+                // Exercise the sorted-spill replay path explicitly.
+                SessionConfig::new()
+                    .with_batch_size(BATCH_SIZE)
+                    .set_bool("datafusion.execution.enable_migration_aggregate", false),

Review Comment:
   This test selects the legacy sorted-spill path with `enable_migration_aggregate=false`, but DataFusion 55.2 defaults it to `true` and I could not find that flag set anywhere in `native` or `spark/src/main`, so Comet runs the default path. As written, the only end-to-end regression exercises a path Comet does not use. Could the test run both settings, or could the description say why only the legacy path hands `merge_batch` multiple state rows? The test also depends on the pool size and the `spill_count() > 0` heuristic, so a DataFusion bump could break it without a Comet change. A Final aggregate over one batch holding two states, with no pool or spill, would cover the same contract through the real operator more cheaply. I did not run any of this.



##########
native/spark-expr/src/bloom_filter/bloom_filter_agg.rs:
##########
@@ -190,16 +190,83 @@ impl Accumulator for SparkBloomFilter {
             "Expect one element in 'states' but found {}",
             states.len()
         );
-        assert_eq!(states[0].len(), 1);
-        let state_sv = downcast_value!(states[0], BinaryArray);
-        self.merge_filter(state_sv.value_data())
+        let states = downcast_value!(states[0], BinaryArray);
+        // Spill replay can supply multiple states for a group. Use each row's
+        // slice so that sliced arrays also honor their offsets.

Review Comment:
   The comment says spill replay can supply multiple states, but `merge_batch` can receive any number of rows. As far as I can tell DataFusion's ungrouped path passes the whole input batch (`aggregate_stream.rs`) and the grouped path goes through `GroupsAccumulatorAdapter`, which slices a taken array per group, so neither needs a spill. If that is right, the failure is broader than spill replay and the description could say so. The comment could say why per-row slices are used instead: the array may be a slice, and `value_data()` would expose the whole backing buffer. I did not verify how many state rows a Comet native Final sees per batch.



-- 
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: merge all Bloom filter states and respect sliced offsets [datafusion-comet]

Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #7173:
URL: https://github.com/apache/datafusion-comet/pull/7173#discussion_r4365203392


##########
native/spark-expr/tests/bloom_filter_spill.rs:
##########
@@ -0,0 +1,160 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::Arc;
+
+use arrow::array::{Array, BinaryArray, BinaryBuilder, Int64Array, RecordBatch};
+use arrow::datatypes::{DataType, Field, Schema};
+use datafusion::common::{Result, ScalarValue};
+use datafusion::datasource::memory::MemorySourceConfig;
+use datafusion::datasource::source::DataSourceExec;
+use datafusion::execution::memory_pool::{GreedyMemoryPool, MemoryPool};
+use datafusion::execution::runtime_env::RuntimeEnvBuilder;
+use datafusion::execution::TaskContext;
+use datafusion::logical_expr::AggregateUDF;
+use datafusion::physical_expr::aggregate::{AggregateExprBuilder, AggregateFunctionExpr};
+use datafusion::physical_expr::expressions::{Column, Literal};
+use datafusion::physical_plan::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};
+use datafusion::physical_plan::{common::collect, ExecutionPlan};
+use datafusion::prelude::SessionConfig;
+use datafusion_comet_spark_expr::{BloomFilterAgg, SparkBloomFilterVersion};
+
+fn bloom_state(aggregate: &AggregateFunctionExpr, values: &[i64]) -> Result<Vec<u8>> {
+    let mut accumulator = aggregate.create_accumulator()?;
+    accumulator.update_batch(&[Arc::new(Int64Array::from_iter_values(
+        values.iter().copied(),
+    ))])?;
+    let ScalarValue::Binary(Some(state)) = accumulator.state()?.remove(0) else {
+        panic!("a nonempty Bloom filter must have a binary state");
+    };
+    Ok(state)
+}
+
+#[tokio::test]
+async fn grouped_bloom_filter_merges_spilled_states() -> Result<()> {
+    const NUM_GROUPS: usize = 1024;
+    const BATCH_SIZE: usize = 8;
+    const NUM_BITS: i64 = 131_072;
+
+    let value_schema = Arc::new(Schema::new(vec![
+        Field::new("key", DataType::Int64, false),
+        Field::new("value", DataType::Int64, false),
+    ]));
+    let aggregate_udf = Arc::new(AggregateUDF::new_from_impl(BloomFilterAgg::new(
+        Arc::new(Literal::new(ScalarValue::Int64(Some(1024)))),
+        Arc::new(Literal::new(ScalarValue::Int64(Some(NUM_BITS)))),
+        DataType::Binary,
+        SparkBloomFilterVersion::V1,
+    )));
+    let aggregate_expr = Arc::new(
+        AggregateExprBuilder::new(aggregate_udf, vec![Arc::new(Column::new("value", 1))])
+            .schema(Arc::clone(&value_schema))
+            .alias("bloom")
+            .build()?,
+    );
+    let state_schema = Arc::new(Schema::new(vec![
+        Field::new("key", DataType::Int64, false),
+        Field::new("bits", DataType::Binary, false),
+    ]));
+
+    // Each pass has one valid, nonempty partial state per group. The complete set
+    // of 16 KiB filters exceeds the pool, so the two passes meet during sorted
+    // spill replay. Distinct values detect lost states as well as merge panics.
+    let mut batches = Vec::new();
+    for pass in 0..2 {
+        for start in (0..NUM_GROUPS).step_by(BATCH_SIZE) {
+            let mut states = BinaryBuilder::new();
+            for key in start..start + BATCH_SIZE {
+                states.append_value(bloom_state(
+                    &aggregate_expr,
+                    &[(pass * NUM_GROUPS + key) as i64],
+                )?);
+            }
+            batches.push(RecordBatch::try_new(
+                Arc::clone(&state_schema),
+                vec![
+                    Arc::new(Int64Array::from_iter_values(
+                        (start..start + BATCH_SIZE).map(|key| key as i64),
+                    )),
+                    Arc::new(states.finish()),
+                ],
+            )?);
+        }
+    }
+    let scan = Arc::new(DataSourceExec::new(Arc::new(MemorySourceConfig::try_new(
+        &[batches],
+        state_schema,
+        None,
+    )?)));
+    let aggregate = AggregateExec::try_new(
+        AggregateMode::Final,
+        PhysicalGroupBy::new_single(vec![(Arc::new(Column::new("key", 0)), "key".into())]),
+        vec![Arc::clone(&aggregate_expr)],
+        vec![None],
+        scan,
+        value_schema,
+    )?;
+    let pool = Arc::new(GreedyMemoryPool::new(8 * 1024 * 1024));
+    let runtime = RuntimeEnvBuilder::new()
+        .with_memory_pool(Arc::clone(&pool) as _)
+        .build_arc()?;
+    let context = Arc::new(
+        TaskContext::default()
+            .with_runtime(runtime)
+            .with_session_config(
+                // Exercise the sorted-spill replay path explicitly.
+                SessionConfig::new()
+                    .with_batch_size(BATCH_SIZE)
+                    .set_bool("datafusion.execution.enable_migration_aggregate", false),

Review Comment:
   I replaced the forced-spill fixture in 9a83585f6 with a grouped `Final` aggregate using DataFusion’s default settings. One input batch gives group 1 partial filters for 11 and 22 and group 2 a filter for 99. The test checks the exact bytes for both groups. It passed in the focused harness and failed against the original accumulator at the one-row assertion (`2` rows received, `1` expected).
   



-- 
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: merge all Bloom filter states and respect sliced offsets [datafusion-comet]

Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #7173:
URL: https://github.com/apache/datafusion-comet/pull/7173#discussion_r4365206512


##########
native/spark-expr/src/bloom_filter/bloom_filter_agg.rs:
##########
@@ -190,16 +190,83 @@ impl Accumulator for SparkBloomFilter {
             "Expect one element in 'states' but found {}",
             states.len()
         );
-        assert_eq!(states[0].len(), 1);
-        let state_sv = downcast_value!(states[0], BinaryArray);
-        self.merge_filter(state_sv.value_data())
+        let states = downcast_value!(states[0], BinaryArray);
+        // Spill replay can supply multiple states for a group. Use each row's
+        // slice so that sliced arrays also honor their offsets.

Review Comment:
   The comment in 9a83585f6 and the PR rationale now describe ordinary final aggregation. A group’s Arrow slice can share its backing buffer with other groups, which is why `merge_batch` uses each row’s logical bytes. I also checked this by restoring `value_data()` only for one-row groups. The two-state group still merged correctly, and the new regression caught the wrong filter bytes for the other group.
   



-- 
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