Skip to content

Commit 8c38f54

Browse files
adriangbclaude
andcommitted
refactor: check the evaluation sites invariant from tests, not in the optimizer
Replace the switch and the hook in Optimizer::optimize with one public function, check_no_new_volatile_evaluations. Tests call it through a check(sql) helper. A known failure is an is_err() assertion with a link to its issue (apache#26910, apache#25477), so the fix PR flips one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8fb4d86 commit 8c38f54

5 files changed

Lines changed: 103 additions & 92 deletions

File tree

‎datafusion/core/tests/sql/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ mod tpcds_planning;
6262
mod union_comparison;
6363
mod union_nullable;
6464
mod unparser;
65+
mod volatile_evaluations;
6566

6667
async fn register_aggregate_csv_by_sql(ctx: &SessionContext) {
6768
let testdata = test_util::arrow_test_data();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3+
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4+
5+
//! The optimizer must not evaluate a volatile call at more sites than the
6+
//! input plan does, see `datafusion_optimizer::evaluation_sites`.
7+
8+
use super::*;
9+
use datafusion_optimizer::evaluation_sites::check_no_new_volatile_evaluations;
10+
11+
/// Plan `sql`, optimize it, and check that the optimizer added no evaluation
12+
/// site of a volatile call.
13+
async fn check(sql: &str) -> Result<()> {
14+
let ctx = SessionContext::new();
15+
ctx.sql("CREATE TABLE t (a INT, b DOUBLE) AS VALUES (1, 1.0), (2, 2.0)")
16+
.await?
17+
.collect()
18+
.await?;
19+
let input = ctx.sql(sql).await?.into_unoptimized_plan();
20+
let optimized = ctx.state().optimize(&input)?;
21+
check_no_new_volatile_evaluations(&input, &optimized)
22+
}
23+
24+
#[tokio::test]
25+
async fn filter_on_a_volatile_column() {
26+
check("SELECT * FROM (SELECT random() AS r, a FROM t) WHERE r < 0.5")
27+
.await
28+
.unwrap();
29+
}
30+
31+
#[tokio::test]
32+
async fn volatile_column_used_two_times() {
33+
check("SELECT r, r + 1 FROM (SELECT random() AS r FROM t)")
34+
.await
35+
.unwrap();
36+
}
37+
38+
#[tokio::test]
39+
async fn filter_on_a_volatile_column_above_a_join() {
40+
check(
41+
"SELECT * FROM (SELECT a, random() AS r FROM t) x \
42+
JOIN t y ON x.a = y.a WHERE x.r < 0.5",
43+
)
44+
.await
45+
.unwrap();
46+
}
47+
48+
#[tokio::test]
49+
async fn having_on_a_volatile_group_key() {
50+
check("SELECT r FROM (SELECT random() AS r FROM t) GROUP BY r HAVING r > 0.5")
51+
.await
52+
.unwrap();
53+
}
54+
55+
#[tokio::test]
56+
async fn filter_on_a_volatile_column_above_a_union() {
57+
check(
58+
"SELECT * FROM (SELECT random() AS r FROM t UNION ALL SELECT b FROM t) \
59+
WHERE r < 0.5",
60+
)
61+
.await
62+
.unwrap();
63+
}
64+
65+
#[tokio::test]
66+
async fn preimage_of_a_volatile_argument() {
67+
// see https://github.com/apache/datafusion/issues/26910
68+
assert!(
69+
check("SELECT a FROM t WHERE floor(random() * 10) = 3")
70+
.await
71+
.is_err()
72+
);
73+
}
74+
75+
#[tokio::test]
76+
async fn coalesce_of_a_volatile_argument() {
77+
// see https://github.com/apache/datafusion/issues/25477
78+
assert!(
79+
check("SELECT coalesce(nullif(random(), 0.5), b) FROM t")
80+
.await
81+
.is_err()
82+
);
83+
}

‎datafusion/optimizer/src/evaluation_sites.rs‎

Lines changed: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
//!
1919
//! # An end of run invariant, by design
2020
//!
21-
//! [`EvaluationSites::check_no_new_evaluations`] runs one time, at the end of
22-
//! [`crate::Optimizer::optimize`], and compares the final plan against the
23-
//! plan the optimizer was given. Intermediate plans are not checked, because
21+
//! [`check_no_new_volatile_evaluations`] compares the final plan of an
22+
//! optimizer run against the plan the optimizer was given. Tests call it, see
23+
//! `datafusion/core/tests/sql/volatile_evaluations.rs`. Intermediate plans are
24+
//! not checked, because
2425
//! rules cooperate: `simplify_expressions` expands `x BETWEEN a AND b` into
2526
//! two references to `x`, and `CommonSubexprEliminate` hoists the duplicate
2627
//! back out in the same pass. Only the plan that the user pays for matters,
2728
//! so only that plan is checked.
2829
//!
29-
//! The price is that the invariant does not name the rule at fault. That is a
30-
//! debugging aid, not a gate, and the before and after site counts in the
31-
//! message are enough to start from.
30+
//! The price is that the check does not name the rule at fault. The before
31+
//! and after site counts in the message are enough to start from.
3232
//!
3333
//! # What is counted
3434
//!
@@ -76,34 +76,6 @@ use datafusion_common::{
7676
use datafusion_expr::expr::{Exists, InSubquery, SetComparison};
7777
use datafusion_expr::{Expr, LogicalPlan};
7878

79-
/// Report a volatile call that the optimizer run made the plan evaluate at
80-
/// more sites than the input plan does.
81-
///
82-
/// Off, because one shape still fails on `main`:
83-
///
84-
/// - `random() BETWEEN 0.0 AND 1.0` in `expr.slt`, which is
85-
/// <https://github.com/apache/datafusion/issues/25457>, a wrong results bug
86-
/// with a fix of its own
87-
///
88-
/// Turn it on once that is fixed. One other shape is worth recording here,
89-
/// because it is what an end of run check buys: `file_row_index()` re-inlined
90-
/// by `optimize_projections` in `file_row_index.slt`. A per rule check reports
91-
/// it, this check does not, because `CommonSubexprEliminate` hoists the call
92-
/// and `optimize_projections` puts it back, which leaves the final plan with
93-
/// the site count the input plan has. The classification order that lets a
94-
/// volatile call be inlined at all is fixed by
95-
/// <https://github.com/apache/datafusion/pull/25456>.
96-
const ENFORCE_NO_NEW_VOLATILE_EVALUATIONS: bool = false;
97-
98-
/// True while the invariant is checked.
99-
///
100-
/// Debug builds only. The check walks the plan two times per optimizer run,
101-
/// which is cheap but not free, and the other plan invariants are checked
102-
/// there too.
103-
const fn enabled() -> bool {
104-
cfg!(debug_assertions) && ENFORCE_NO_NEW_VOLATILE_EVALUATIONS
105-
}
106-
10779
/// The number of evaluation sites of one call, per row lineage, keyed by
10880
/// [`structural_key`].
10981
type Counts = HashMap<String, usize>;
@@ -118,16 +90,6 @@ pub(crate) struct EvaluationSites {
11890

11991
impl EvaluationSites {
12092
/// Count the evaluation sites of `plan`.
121-
///
122-
/// Empty, and free, while the invariant is off.
123-
pub(crate) fn of(plan: &LogicalPlan) -> Self {
124-
if !enabled() {
125-
return Self::default();
126-
}
127-
Self::count(plan)
128-
}
129-
130-
/// Count the evaluation sites of `plan`, whatever the switch says.
13193
fn count(plan: &LogicalPlan) -> Self {
13294
let mut counts = Counts::new();
13395
count_plan(plan, &mut counts);
@@ -137,15 +99,6 @@ impl EvaluationSites {
13799
/// Fail if `plan` evaluates a volatile call more times than `self` does.
138100
///
139101
/// `self` must be the count of the plan that the optimizer was given.
140-
pub(crate) fn check_no_new_evaluations(&self, plan: &LogicalPlan) -> Result<()> {
141-
if !enabled() {
142-
return Ok(());
143-
}
144-
self.check(plan)
145-
}
146-
147-
/// The body of [`Self::check_no_new_evaluations`], without the switch, so
148-
/// that the machinery has a test while the switch is off.
149102
fn check(&self, plan: &LogicalPlan) -> Result<()> {
150103
let after = Self::count(plan);
151104
let mut offenders: Vec<(&String, usize, usize)> = after
@@ -183,6 +136,17 @@ impl EvaluationSites {
183136
}
184137
}
185138

139+
/// Fail if `optimized` evaluates a volatile call at more sites than `input`
140+
/// does. `input` is the plan the optimizer was given.
141+
///
142+
/// See the [module documentation](self) for what is counted.
143+
pub fn check_no_new_volatile_evaluations(
144+
input: &LogicalPlan,
145+
optimized: &LogicalPlan,
146+
) -> Result<()> {
147+
EvaluationSites::count(input).check(optimized)
148+
}
149+
186150
/// `own_sites(plan) + max over inputs of count_plan(input)`.
187151
#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
188152
fn count_plan(plan: &LogicalPlan, acc: &mut Counts) {
@@ -460,7 +424,7 @@ mod tests {
460424
Ok(())
461425
}
462426

463-
/// The machinery against a real optimizer run, with the switch off: a
427+
/// The machinery against a real optimizer run: a
464428
/// filter on a volatile column defined by a projection must not be pushed
465429
/// below it, so the optimized plan keeps the site count of the input.
466430
#[test]
@@ -477,27 +441,4 @@ mod tests {
477441
before.check(&optimized)?;
478442
Ok(())
479443
}
480-
481-
/// <https://github.com/apache/datafusion/issues/25457>, pinned so that the
482-
/// test fails, and must be flipped to `?`, once that is fixed.
483-
#[test]
484-
fn reports_between_expansion_of_a_kept_volatile_call() -> Result<()> {
485-
let udf = ScalarUDF::new_from_impl(
486-
PlacementTestUDF::new()
487-
.with_placement(ExpressionPlacement::KeepInPlace)
488-
.with_volatility(Volatility::Volatile),
489-
);
490-
let input = LogicalPlanBuilder::from(test_table_scan()?)
491-
.filter(udf.call(vec![col("a")]).between(lit(1u32), lit(5u32)))?
492-
.build()?;
493-
let before = EvaluationSites::count(&input);
494-
let optimized =
495-
Optimizer::new().optimize(input, &OptimizerContext::new(), |_, _| {})?;
496-
let error = before.check(&optimized).unwrap_err().to_string();
497-
assert!(
498-
error.contains("is evaluated at 2 sites, against 1"),
499-
"{error}"
500-
);
501-
Ok(())
502-
}
503444
}

‎datafusion/optimizer/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub mod eliminate_nested_union {
4444
pub type EliminateNestedUnion = OptimizeUnions;
4545
}
4646
pub mod eliminate_outer_join;
47-
mod evaluation_sites;
47+
pub mod evaluation_sites;
4848
pub mod extract_equijoin_predicate;
4949
pub mod extract_leaf_expressions;
5050
pub mod filter_null_join_keys;

‎datafusion/optimizer/src/optimizer.rs‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use crate::eliminate_group_by_constant::EliminateGroupByConstant;
3939
use crate::eliminate_join::EliminateJoin;
4040
use crate::eliminate_limit::EliminateLimit;
4141
use crate::eliminate_outer_join::EliminateOuterJoin;
42-
use crate::evaluation_sites::EvaluationSites;
4342
use crate::extract_equijoin_predicate::ExtractEquijoinPredicate;
4443
use crate::extract_leaf_expressions::{ExtractLeafExpressions, PushDownLeafProjections};
4544
use crate::filter_null_join_keys::FilterNullJoinKeys;
@@ -606,10 +605,6 @@ impl Optimizer {
606605
plan.check_invariants(InvariantLevel::Executable)
607606
.map_err(|e| e.context("Invalid input plan before LP Optimizers"))?;
608607

609-
// The evaluation sites of the plan the optimizer was given. Empty, and
610-
// free, while the check is off. See `EvaluationSites`.
611-
let starting_evaluation_sites = EvaluationSites::of(&plan);
612-
613608
let start_time = Instant::now();
614609
let options = config.options();
615610
let mut new_plan = plan;
@@ -757,15 +752,6 @@ impl Optimizer {
757752
e.context("Check optimizer-specific invariants after all passes")
758753
})?;
759754

760-
// verify invariant: the optimized plan must not evaluate a volatile
761-
// expression at more sites than the input plan does. Checked one time,
762-
// over the whole run, because rules cooperate. See `EvaluationSites`.
763-
starting_evaluation_sites
764-
.check_no_new_evaluations(&new_plan)
765-
.map_err(|e| {
766-
e.context("Check optimizer-specific invariants after all passes")
767-
})?;
768-
769755
// verify LP is valid, after the last optimizer pass.
770756
new_plan
771757
.check_invariants(InvariantLevel::Executable)

0 commit comments

Comments
 (0)