Apache DataFusion SQL Query Engine
fix: Preimage rewrites are not allowed for volatile arguments (#26995)
Vikrant Mehta867bf3c70849
diff --git a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rsindex 248fc2fa1b..a7f384b8ad 100644--- a/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs+++ b/datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs@@ -2132,6 +2132,11 @@ fn get_preimage(if func.signature().volatility != Volatility::Immutable {return Ok(PreimageResult::None);}+ // We can get duplicate expressions due to preimage rewrites due to volatile arguments+ if args.iter().any(Expr::is_volatile) {+ return Ok(PreimageResult::None);+ }+func.preimage(args, right_expr, info)}diff --git a/datafusion/sqllogictest/test_files/floor_preimage.slt b/datafusion/sqllogictest/test_files/floor_preimage.sltindex b1b3a7cb44..284fe7b930 100644--- a/datafusion/sqllogictest/test_files/floor_preimage.slt+++ b/datafusion/sqllogictest/test_files/floor_preimage.slt@@ -187,6 +187,22 @@ logical_plan01)Filter: test_data.float_val >= Float64(5) AND test_data.float_val < Float64(6) OR test_data.float_val >= Float64(7) AND test_data.float_val < Float64(8)02)--TableScan: test_data projection=[id, float_val, int_val, decimal_val]+# 10. Volatile arguments must not be duplicated due to SimplifyExpressions' preimage rewrites+query TT+EXPLAIN SELECT * FROM test_data WHERE floor(random() * 10) = 3;+----+logical_plan+01)Filter: floor(random() * Float64(10)) = Float64(3)+02)--TableScan: test_data projection=[id, float_val, int_val, decimal_val]++# 11. Preimage rewrites must not duplicate IN-list as well.+query TT+EXPLAIN SELECT * FROM test_data WHERE floor(random() * 10) IN (3, 5);+----+logical_plan+01)Filter: floor(random() * Float64(10)) IN ([Float64(3), Float64(5)])+02)--TableScan: test_data projection=[id, float_val, int_val, decimal_val]+# Data correctness: floor(col) = 2^53 returns no rows (no value in test_data has floor exactly 2^53)query I rowsortSELECT id FROM test_data WHERE floor(float_val) = 9007199254740992;diff --git a/datafusion/sqllogictest/test_files/volatile_evaluations.slt b/datafusion/sqllogictest/test_files/volatile_evaluations.sltindex ee8304910c..20bdbd7fbe 100644--- a/datafusion/sqllogictest/test_files/volatile_evaluations.slt+++ b/datafusion/sqllogictest/test_files/volatile_evaluations.slt@@ -105,9 +105,7 @@ SELECT evaluations();----10-# https://github.com/apache/datafusion/issues/26910-# Known bug: the preimage rewrite of `floor(x) = 3` copies `x`, so a volatile-# `x` is evaluated two times. The count must be 10.+# Volatile in a predicate.query ISELECT count(*) FROM t WHERE floor(counted_volatile(b)) = 3;@@ -117,7 +115,7 @@ SELECT count(*) FROM t WHERE floor(counted_volatile(b)) = 3;query ISELECT evaluations();-----20+10# `x BETWEEN low AND high` evaluates `x` two times. This is accepted behaviour,# Postgres does the same: https://github.com/apache/datafusion/issues/25457