Skip to content

fix: respect volatility for all function expressions - #25511

Open
1fanwang wants to merge 3 commits into
apache:mainfrom
1fanwang:1fannnw/fix-udf-volatility
Open

1fanwang wants to merge 3 commits into
apache:mainfrom
1fanwang:1fannnw/fix-udf-volatility

Conversation

@1fanwang

@1fanwang 1fanwang commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #25504.

Rationale for this change

Repeated calls to a volatile higher-order UDF can reuse the first call's result. Comparing two calls can also return true without invoking the function. Both queries now execute each call.

What changes are included in this PR?

The volatility check reads the declared signature for aggregate, window and higher-order expressions, as it already does for scalar expressions. This includes aggregates used as window functions. The join optimizer uses the shared repeatability check instead of checking aggregate volatility separately.

What is the testing strategy for this PR?

The regression registers a counter UDF and runs both queries through the SQL engine. It also checks every function kind, volatility level and aliased expression. Join-optimizer controls preserve DISTINCT handling and reject volatile expressions and subqueries.

On macOS arm64 with Rust 1.98.1 and the pinned test data:

cargo test --locked --profile ci -j6 -p datafusion --test user_defined_integration volatility -- --nocapture
cargo test --locked --profile ci -j6 -p datafusion-expr -p datafusion-optimizer --lib
cargo test --locked --profile ci -j6 -p datafusion-sqllogictest --test sqllogictests -- --test-threads 6 cse.slt expr.slt array_transform.slt eliminate_outer_join.slt aggregates_simplify.slt
Raw logs

Before the fix:

+-------+--------+
| first | second |
+-------+--------+
| 0     | 0      |
+-------+--------+
calls=1
+-------+
| equal |
+-------+
| true  |
+-------+
calls=0

After:

+-------+--------+
| first | second |
+-------+--------+
| 0     | 1      |
+-------+--------+
calls=2
+-------+
| equal |
+-------+
| false |
+-------+
calls=2
Progress: 6/6 files completed (100%)
slt_exit_code=0

Are there any user-facing changes?

The affected queries return the results of separate function calls. There is no public API change.

@github-actions github-actions Bot added logical-expr Logical plan and expressions core Core DataFusion crate labels Sep 19, 2026
@codecov-commenter

codecov-commenter commented Sep 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.50%. Comparing base (e2ca7f3) to head (813d657).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25511      +/-   ##
==========================================
- Coverage   82.50%   82.50%   -0.01%     
==========================================
  Files        1140     1140              
  Lines      438050   438057       +7     
  Branches   438050   438057       +7     
==========================================
+ Hits       361415   361417       +2     
  Misses      54829    54829              
- Partials    21806    21811       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@neilconway

Copy link
Copy Markdown
Contributor

There's also #25513 for the same bug

@1fanwang

Copy link
Copy Markdown
Contributor Author

#25513 is closed without merging. This PR includes SQL regressions for projection reuse and equality simplification.

@neilconway neilconway left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks reasonable!

Can we cleanup/simplify is_duplicate_insensitive_aggregate, please?

@github-actions github-actions Bot added the optimizer Optimizer rules label Sep 23, 2026
@1fanwang

Copy link
Copy Markdown
Contributor Author

Done in 97efd3a.

@1fanwang
1fanwang force-pushed the 1fannnw/fix-udf-volatility branch from 97efd3a to 624078d Compare September 24, 2026 06:16
Signed-off-by: 1fanwang <1fannnw@gmail.com>
The shared expression walk checks aggregate volatility, so a separate signature check is redundant.

Signed-off-by: 1fanwang <1fannnw@gmail.com>
@1fanwang
1fanwang force-pushed the 1fannnw/fix-udf-volatility branch from 624078d to b2707e9 Compare September 24, 2026 06:54

@neilconway neilconway left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @1fanwang !

@neilconway
neilconway added this pull request to the merge queue Sep 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 24, 2026
@neilconway
neilconway added this pull request to the merge queue Sep 24, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions optimizer Optimizer rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expr::is_volatile ignores the volatility of aggregate, window and higher-order functions

3 participants