diff --git a/.cargo/mutants.toml b/.cargo/mutants.toml new file mode 100644 index 0000000000000..18351304562e9 --- /dev/null +++ b/.cargo/mutants.toml @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +# Configuration for cargo-mutants. See the "Mutation testing" section of +# docs/source/contributor-guide/testing.md. + +profile = "ci" diff --git a/.github/workflows/mutants.yml b/.github/workflows/mutants.yml new file mode 100644 index 0000000000000..154887325dfc2 --- /dev/null +++ b/.github/workflows/mutants.yml @@ -0,0 +1,126 @@ +# SPDX-License-Identifier: Apache-2.0 +# SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership +# SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements + +# Mutation testing of the code a PR changes, using cargo-mutants. +# +# This check is advisory: it is not listed in `.asf.yaml` +# required_status_checks and never blocks a merge. It only mutates lines in +# the PR diff, so the author who wrote the code sees the results. See the +# "Mutation testing" section of docs/source/contributor-guide/testing.md. +# +# Many crates are mostly tested from other packages, for example optimizer +# rules from `datafusion/core/tests` and sqllogictest. Testing every mutant +# against those suites means rebuilding them per mutant, so the job runs two +# passes: +# 1. Every mutant against the tests of the package it is in (cheap). +# 2. Only the survivors, via `--iterate`, against the end-to-end suites. + +name: "Mutation testing" + +concurrency: + group: ${{ github.repository }}-${{ github.head_ref || github.sha }}-${{ github.workflow }} + cancel-in-progress: true + +on: + pull_request: + branches: + - main + paths: + - "**.rs" + - ".cargo/mutants.toml" + - ".github/workflows/mutants.yml" + +permissions: + contents: read + +jobs: + mutants: + name: Mutation testing of changed code + runs-on: ubuntu-latest + container: + image: amd64/rust + timeout-minutes: 180 + steps: + # The checkout is GitHub's test merge commit of the PR into the base + # branch, so HEAD^1 is the base commit the PR was merged into. + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 2 + submodules: true + - name: Setup Rust toolchain + uses: ./.github/actions/setup-builder + with: + rust-version: stable + - name: Install cargo-mutants + uses: taiki-e/install-action@3a3daafd3b7f23b8f65d9f5ba2db203fb59b79e2 # v2.90.2 + with: + tool: cargo-mutants@27.1.0 + - name: Compute PR diff + run: git diff HEAD^1 HEAD > "$RUNNER_TEMP/pr.diff" + # Keep the baseline: if the unmutated tests fail in this environment, + # for example because test data is missing, every mutant would + # otherwise be reported as caught. + # + # Exit codes 2 (missed) and 3 (timeout) are expected outcomes; the + # result is decided from missed.txt in the last step. + - name: Test mutants against their own package + run: | + set +e + cargo mutants --in-diff "$RUNNER_TEMP/pr.diff" \ + --output "$RUNNER_TEMP" + rc=$? + [ "$rc" -eq 0 ] || [ "$rc" -eq 2 ] || [ "$rc" -eq 3 ] + # The timeout is explicit because cargo-mutants 27.1.0 runs this + # baseline against the mutated package, not `--test-package`, and + # derives a test timeout far too short for these suites. Mutants then + # time out instead of being reported as missed. + - name: Retest survivors against end-to-end tests + run: | + if [ ! -s "$RUNNER_TEMP/mutants.out/missed.txt" ]; then + exit 0 + fi + set +e + cargo mutants --in-diff "$RUNNER_TEMP/pr.diff" \ + --output "$RUNNER_TEMP" --iterate --timeout 1800 \ + --test-package datafusion,datafusion-sqllogictest + rc=$? + [ "$rc" -eq 0 ] || [ "$rc" -eq 2 ] || [ "$rc" -eq 3 ] + - name: Report missed mutants + run: | + timeout="$RUNNER_TEMP/mutants.out/timeout.txt" + if [ -s "$timeout" ]; then + { + echo "## Mutants that made the tests time out" + echo + echo "These usually mean the mutant caused a hang, which counts as caught." + echo + echo '```' + cat "$timeout" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + fi + missed="$RUNNER_TEMP/mutants.out/missed.txt" + if [ -s "$missed" ]; then + { + echo "## Mutants not caught by any test" + echo + echo "Each line is a change to this PR's code that no test detected." + echo "Add a test that fails for it, or explain in the PR why it does not matter." + echo + echo '```' + cat "$missed" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + exit 1 + fi + - name: Upload cargo-mutants output + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: mutants-out + path: | + ${{ runner.temp }}/mutants.out + ${{ runner.temp }}/mutants.out.old + if-no-files-found: ignore + retention-days: 7 diff --git a/datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs b/datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs index 5e176e7140fb6..6079bf572b51e 100644 --- a/datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs +++ b/datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs @@ -1115,6 +1115,137 @@ struct DistributionChildState { requirement: Distribution, } +/// Inputs to [`ChildPolicy::new`] that are the same for every child of the +/// operator being optimized. +struct ChildPolicyConfig { + target_partitions: usize, + subset_satisfaction_threshold: usize, + /// `optimizer.preserve_file_partitions`; `0` disables it. + preserve_file_partitions: usize, + enable_round_robin: bool, + /// Partitioned hash joins and sort-merge joins match partition `N` of + /// each side, so they need exact hash satisfaction. + is_partitioned_join: bool, +} + +/// Repartitioning decisions for one child, derived from its current +/// partition count and its [`RepartitionRequirementStatus`]. +/// +/// These flags interact (see #18341), so they are computed in one place +/// where they can be tested without building a plan. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct ChildPolicy { + /// `preserve_file_partitions` is enabled and the child already has at + /// least that many partitions. + preserve_file_partition_threshold_met: bool, + /// The hash requirement includes `__grouping_id`. Grouping set aggregates + /// (ROLLUP, CUBE, GROUPING SETS) need exact hash partitioning on all group + /// columns including it, so partial aggregates combine correctly. + requires_grouping_id: bool, + /// A partitioning on a subset of the required keys satisfies the + /// requirement. + allow_subset_satisfy_partitioning: bool, + /// Add round-robin repartitioning on top of the child to increase + /// parallelism. + add_roundrobin: bool, +} + +impl ChildPolicy { + /// `keeps_partial_aggregate_partitioning` reports whether the parent's + /// partial aggregate already satisfies the final aggregate's + /// partitioning or keeps each group in one input partition, given + /// `allow_subset_satisfy_partitioning`. It is only called when the + /// `preserve_file_partitions` threshold is met. + #[expect( + deprecated, + reason = "HashPartitioned is accepted during the KeyPartitioned migration" + )] + fn new( + config: &ChildPolicyConfig, + current_partitions: usize, + status: &RepartitionRequirementStatus, + keeps_partial_aggregate_partitioning: impl FnOnce(bool) -> bool, + ) -> Self { + let preserve_file_partition_threshold_met = config.preserve_file_partitions > 0 + && current_partitions >= config.preserve_file_partitions; + + let requires_grouping_id = matches!(&status.requirement, + Distribution::HashPartitioned(exprs) | Distribution::KeyPartitioned(exprs) + if exprs.iter().any(|expr| { + (expr.as_ref() as &dyn Any) + .downcast_ref::() + .is_some_and(|col| col.name() == Aggregate::INTERNAL_GROUPING_ID) + }) + ); + + let increases_partition_count = current_partitions < config.target_partitions; + + // Allow subset satisfaction when: + // 1. Current partition count >= threshold + // 2. Not a partitioned join since must use exact hash matching for joins + // 3. Not a grouping set aggregate (requires exact hash including __grouping_id) + // + // Partitioned joins still require exact satisfaction. If that + // exact check already passes, preserve_file_partitions can skip + // repartitioning whose only purpose is increasing partition count. + let allow_subset_satisfy_partitioning = (current_partitions + >= config.subset_satisfaction_threshold + // `preserve_file_partitions` exposes existing file-group + // partitioning to the optimizer. Respect it when the only + // reason to repartition would be to increase partition count + // beyond the preserved file-group count. + || (preserve_file_partition_threshold_met && increases_partition_count)) + && !config.is_partitioned_join + && !requires_grouping_id; + + let preserve_partial_aggregate_partitioning = + preserve_file_partition_threshold_met + && keeps_partial_aggregate_partitioning( + allow_subset_satisfy_partitioning, + ); + + let add_roundrobin = config.enable_round_robin + // Operator benefits from partitioning (e.g. filter): + && status.roundrobin_beneficial + && status.roundrobin_beneficial_stats + // Unless partitioning increases the partition count, it is not beneficial: + && increases_partition_count + && !preserve_partial_aggregate_partitioning; + + Self { + preserve_file_partition_threshold_met, + requires_grouping_id, + allow_subset_satisfy_partitioning, + add_roundrobin, + } + } + + /// Whether a hash or key partitioning requirement needs a new + /// `RepartitionExec`, given whether the child's current partitioning + /// (with `child_partitions` partitions) already satisfies it. + fn needs_hash_repartition( + &self, + partitioning_satisfied: bool, + child_partitions: usize, + target_partitions: usize, + ) -> bool { + // When subset satisfaction is enabled, preserve an + // already-satisfying partitioning. Otherwise, + // repartition may also increase parallelism. + if self.allow_subset_satisfy_partitioning { + return !partitioning_satisfied; + } + let preserve_satisfying_file_partitioning = self + .preserve_file_partition_threshold_met + && !self.requires_grouping_id + && partitioning_satisfied + && target_partitions > child_partitions; + !partitioning_satisfied + || (target_partitions > child_partitions + && !preserve_satisfying_file_partitioning) + } +} + /// Calculates the `RepartitionRequirementStatus` for each children to generate /// consistent and sensible (in terms of performance) distribution requirements. /// As an example, a hash join's left (build) child might produce @@ -1617,6 +1748,14 @@ pub fn ensure_distribution_with_stats( .is_some_and(|join| join.mode == PartitionMode::Partitioned) || plan.is::(); + let policy_config = ChildPolicyConfig { + target_partitions, + subset_satisfaction_threshold, + preserve_file_partitions: config.optimizer.preserve_file_partitions, + enable_round_robin, + is_partitioned_join, + }; + let input_distributions = plan.input_distribution_requirements(); let repartition_status_flags = get_repartition_requirement_status( &plan, @@ -1637,74 +1776,24 @@ pub fn ensure_distribution_with_stats( repartition_status_flags.into_iter() ) .map( - |( - child_idx, - mut child, - required_input_ordering, - maintains, - RepartitionRequirementStatus { + |(child_idx, mut child, required_input_ordering, maintains, status)| { + let policy = ChildPolicy::new( + &policy_config, + child.plan.output_partitioning().partition_count(), + &status, + |allow_subset| { + partial_aggregate_output_satisfies_final_partitioning( + &plan, + allow_subset, + ) || partial_aggregate_input_keeps_groups_local(&plan, allow_subset) + }, + ); + let RepartitionRequirementStatus { requirement, - roundrobin_beneficial, roundrobin_beneficial_stats, hash_necessary, - }, - )| { - // Allow subset satisfaction when: - // 1. Current partition count >= threshold - // 2. Not a partitioned join since must use exact hash matching for joins - // 3. Not a grouping set aggregate (requires exact hash including __grouping_id) - // - // Partitioned joins still require exact satisfaction. If that - // exact check already passes, preserve_file_partitions can skip - // repartitioning whose only purpose is increasing partition count. - let current_partitions = child.plan.output_partitioning().partition_count(); - let preserve_file_partition_threshold_met = - config.optimizer.preserve_file_partitions > 0 - && current_partitions >= config.optimizer.preserve_file_partitions; - - // Check if the hash partitioning requirement includes __grouping_id column. - // Grouping set aggregates (ROLLUP, CUBE, GROUPING SETS) require exact hash - // partitioning on all group columns including __grouping_id to ensure partial - // aggregates from different partitions are correctly combined. - let requires_grouping_id = matches!(&requirement, - Distribution::HashPartitioned(exprs) | Distribution::KeyPartitioned(exprs) - if exprs.iter().any(|expr| { - (expr.as_ref() as &dyn Any) - .downcast_ref::() - .is_some_and(|col| col.name() == Aggregate::INTERNAL_GROUPING_ID) - }) - ); - - let allow_subset_satisfy_partitioning = (current_partitions - >= subset_satisfaction_threshold - // `preserve_file_partitions` exposes existing file-group - // partitioning to the optimizer. Respect it when the only - // reason to repartition would be to increase partition count - // beyond the preserved file-group count. - || (preserve_file_partition_threshold_met - && current_partitions < target_partitions)) - && !is_partitioned_join - && !requires_grouping_id; - - let increases_partition_count = current_partitions < target_partitions; - - let preserve_partial_aggregate_partitioning = - preserve_file_partition_threshold_met - && (partial_aggregate_output_satisfies_final_partitioning( - &plan, - allow_subset_satisfy_partitioning, - ) || partial_aggregate_input_keeps_groups_local( - &plan, - allow_subset_satisfy_partitioning, - )); - - let add_roundrobin = enable_round_robin - // Operator benefits from partitioning (e.g. filter): - && roundrobin_beneficial - && roundrobin_beneficial_stats - // Unless partitioning increases the partition count, it is not beneficial: - && increases_partition_count - && !preserve_partial_aggregate_partitioning; + .. + } = status; // When `repartition_file_scans` is set, attempt to increase // parallelism at the source. @@ -1735,35 +1824,25 @@ pub fn ensure_distribution_with_stats( .child_satisfaction( child_idx, child.plan.as_ref(), - ChildSatisfactionOptions::new() - .with_allow_subset(allow_subset_satisfy_partitioning), + ChildSatisfactionOptions::new().with_allow_subset( + policy.allow_subset_satisfy_partitioning, + ), )? .is_satisfied() || plan.downcast_ref::().is_some_and( |aggregate| { aggregate.input_keeps_groups_local( child.plan.as_ref(), - allow_subset_satisfy_partitioning, + policy.allow_subset_satisfy_partitioning, ) }, ); - let preserve_satisfying_file_partitioning = - preserve_file_partition_threshold_met - && !requires_grouping_id - && partitioning_satisfied - && target_partitions > child_partitions; - - // When subset satisfaction is enabled, preserve an - // already-satisfying partitioning. Otherwise, - // repartition may also increase parallelism. - let needs_repartition = if allow_subset_satisfy_partitioning { - !partitioning_satisfied - } else { - !partitioning_satisfied - || (target_partitions > child_partitions - && !preserve_satisfying_file_partitioning) - }; - let should_add_repartition = hash_necessary && needs_repartition; + let should_add_repartition = hash_necessary + && policy.needs_hash_repartition( + partitioning_satisfied, + child_partitions, + target_partitions, + ); // See https://github.com/apache/datafusion/issues/18341#issuecomment-3503238325 for background // Enforce unmet requirements, or increase parallelism when beneficial. @@ -1819,7 +1898,7 @@ pub fn ensure_distribution_with_stats( } } Distribution::UnspecifiedDistribution => { - if add_roundrobin { + if policy.add_roundrobin { // Add round-robin repartitioning on top of the operator // to increase parallelism. child = add_roundrobin_on_top(child, target_partitions)?; @@ -2020,4 +2099,190 @@ fn update_children(mut dist_context: DistributionContext) -> Result ChildPolicyConfig { + ChildPolicyConfig { + target_partitions: TARGET, + subset_satisfaction_threshold: THRESHOLD, + preserve_file_partitions: 0, + enable_round_robin: true, + is_partitioned_join: false, + } + } + + fn status(requirement: Distribution) -> RepartitionRequirementStatus { + RepartitionRequirementStatus { + requirement, + roundrobin_beneficial: true, + roundrobin_beneficial_stats: true, + hash_necessary: true, + } + } + + fn hash_on(names: &[&str]) -> Distribution { + Distribution::KeyPartitioned( + names + .iter() + .enumerate() + .map(|(i, name)| Arc::new(Column::new(name, i)) as _) + .collect(), + ) + } + + fn policy( + config: &ChildPolicyConfig, + current_partitions: usize, + requirement: Distribution, + ) -> ChildPolicy { + ChildPolicy::new(config, current_partitions, &status(requirement), |_| false) + } + + #[test] + fn subset_satisfaction_needs_threshold() { + let config = base_config(); + assert!( + !policy(&config, THRESHOLD - 1, hash_on(&["a"])) + .allow_subset_satisfy_partitioning + ); + assert!( + policy(&config, THRESHOLD, hash_on(&["a"])).allow_subset_satisfy_partitioning + ); + } + + #[test] + fn partitioned_join_never_allows_subset_satisfaction() { + let config = ChildPolicyConfig { + is_partitioned_join: true, + ..base_config() + }; + assert!( + !policy(&config, TARGET, hash_on(&["a"])).allow_subset_satisfy_partitioning + ); + } + + #[test] + fn grouping_id_requires_exact_hash() { + let config = base_config(); + let policy = policy( + &config, + TARGET, + hash_on(&["a", Aggregate::INTERNAL_GROUPING_ID]), + ); + assert!(policy.requires_grouping_id); + assert!(!policy.allow_subset_satisfy_partitioning); + } + + #[test] + fn preserved_file_partitions_allow_subset_below_threshold() { + let config = ChildPolicyConfig { + subset_satisfaction_threshold: TARGET + 1, + preserve_file_partitions: 2, + ..base_config() + }; + let below = policy(&config, 1, hash_on(&["a"])); + assert!(!below.preserve_file_partition_threshold_met); + assert!(!below.allow_subset_satisfy_partitioning); + + let met = policy(&config, 2, hash_on(&["a"])); + assert!(met.preserve_file_partition_threshold_met); + assert!(met.allow_subset_satisfy_partitioning); + + // Only while repartitioning would increase the partition count. + assert!( + !policy(&config, TARGET, hash_on(&["a"])).allow_subset_satisfy_partitioning + ); + } + + #[test] + fn roundrobin_only_when_every_condition_holds() { + let config = base_config(); + let unspecified = || Distribution::UnspecifiedDistribution; + assert!(policy(&config, TARGET - 1, unspecified()).add_roundrobin); + assert!(!policy(&config, TARGET, unspecified()).add_roundrobin); + + let disabled = ChildPolicyConfig { + enable_round_robin: false, + ..base_config() + }; + assert!(!policy(&disabled, 1, unspecified()).add_roundrobin); + + for (beneficial, beneficial_stats) in [(false, true), (true, false)] { + let status = RepartitionRequirementStatus { + roundrobin_beneficial: beneficial, + roundrobin_beneficial_stats: beneficial_stats, + ..status(unspecified()) + }; + assert!(!ChildPolicy::new(&config, 1, &status, |_| false).add_roundrobin); + } + } + + #[test] + fn preserved_partial_aggregate_suppresses_roundrobin() { + let config = ChildPolicyConfig { + preserve_file_partitions: 2, + ..base_config() + }; + let status = status(Distribution::UnspecifiedDistribution); + let mut seen_allow_subset = None; + let policy = ChildPolicy::new(&config, 2, &status, |allow_subset| { + seen_allow_subset = Some(allow_subset); + true + }); + assert!(!policy.add_roundrobin); + assert_eq!( + seen_allow_subset, + Some(policy.allow_subset_satisfy_partitioning) + ); + + // Below the threshold the partial aggregate check is skipped. + let policy = ChildPolicy::new(&config, 1, &status, |_| { + panic!("partial aggregate check must be lazy") + }); + assert!(policy.add_roundrobin); + } + + #[test] + fn hash_repartition_with_subset_satisfaction_only_when_unsatisfied() { + let policy = policy(&base_config(), TARGET - 1, hash_on(&["a"])); + assert!(policy.allow_subset_satisfy_partitioning); + assert!(!policy.needs_hash_repartition(true, 1, TARGET)); + assert!(policy.needs_hash_repartition(false, TARGET, TARGET)); + } + + #[test] + fn hash_repartition_without_subset_satisfaction_raises_parallelism() { + let policy = policy(&base_config(), 1, hash_on(&["a"])); + assert!(!policy.allow_subset_satisfy_partitioning); + assert!(policy.needs_hash_repartition(false, TARGET, TARGET)); + assert!(policy.needs_hash_repartition(true, 1, TARGET)); + assert!(!policy.needs_hash_repartition(true, TARGET, TARGET)); + } + + #[test] + fn hash_repartition_keeps_satisfying_preserved_file_partitioning() { + // A partitioned join cannot use subset satisfaction, but an exactly + // satisfying preserved file partitioning is still kept. + let config = ChildPolicyConfig { + preserve_file_partitions: 2, + is_partitioned_join: true, + ..base_config() + }; + let exact = policy(&config, 2, hash_on(&["a"])); + assert!(!exact.allow_subset_satisfy_partitioning); + assert!(!exact.needs_hash_repartition(true, 2, TARGET)); + assert!(exact.needs_hash_repartition(false, 2, TARGET)); + + // Not for grouping sets, which need the full hash. + let grouping = + policy(&config, 2, hash_on(&["a", Aggregate::INTERNAL_GROUPING_ID])); + assert!(grouping.needs_hash_repartition(true, 2, TARGET)); + } +} diff --git a/docs/source/contributor-guide/testing.md b/docs/source/contributor-guide/testing.md index 456cf9e44b7f0..c8f6f49af4c7f 100644 --- a/docs/source/contributor-guide/testing.md +++ b/docs/source/contributor-guide/testing.md @@ -83,6 +83,33 @@ Coverage helps find missing tests; high coverage does not guarantee good tests: [codecov]: https://app.codecov.io/gh/apache/datafusion +### Mutation testing + +Coverage shows which lines a test executes, not whether any test would fail if +those lines were wrong. [cargo-mutants] checks the second property: it makes a +small change to the code, such as replacing `>` with `>=` or `&&` with `||`, +and runs the tests. If they still pass, the mutant is _missed_. + +The [mutants.yml] workflow runs cargo-mutants on pull requests, mutating only +the lines the PR changes. It runs every mutant against the tests of its own +package, then reruns the missed ones against the `datafusion` core tests and +sqllogictests. The check is advisory and does not block merging. When it +fails, the job summary lists the missed mutants. Add a test that fails for +each, or explain in the PR why the change does not matter. + +To run the same check locally against your branch: + +```shell +cargo install --locked cargo-mutants +git diff "$(git merge-base HEAD upstream/main)" > pr.diff +cargo mutants --in-diff pr.diff +# Retest missed mutants against the end-to-end suites +cargo mutants --in-diff pr.diff --iterate --test-package datafusion,datafusion-sqllogictest +``` + +[cargo-mutants]: https://mutants.rs/ +[mutants.yml]: https://github.com/apache/datafusion/blob/main/.github/workflows/mutants.yml + ## Choosing What Kind of Test to Write When adding tests, prefer tests that exercise **user-visible behavior** over