robertream opened a new pull request, #17016: URL: https://github.com/apache/datafusion/pull/17016
## Summary Fixes #16998: SortExec shares DynamicFilterPhysicalExpr across multiple executions This PR addresses an issue where `SortExec` was sharing the same `DynamicFilterPhysicalExpr` across multiple invocations of `with_new_children` (e.g., from `reset_plan_states`), causing recursive queries to fail. ## Changes - **Fixed `SortExec::with_new_children`**: Now properly clones the `DynamicFilterPhysicalExpr` instead of sharing the same instance across multiple executions - **Added comprehensive unit test**: `test_sort_exec_filter_cloning_issue_16998` that reproduces the issue and verifies the fix ## Problem Description The issue occurred when recursive queries would repeatedly execute the same `SortExec` plan. The shared `DynamicFilterPhysicalExpr` would maintain state across executions, leading to incorrect results. For example: ```sql with recursive r as ( select 0 as k, 0 as v union all ( select * from r order by v limit 1 ) ) select * from r limit 5; ``` This query would return only 2 rows instead of the expected 5 rows. ## Solution The fix ensures each `SortExec` instance gets its own copy of the dynamic filter by: 1. Creating a new `DynamicFilterPhysicalExpr` instance with the same children expressions 2. Preserving the current expression state or falling back to a true literal 3. Ensuring independent state management across multiple executions ## Test Verification - Added `test_sort_exec_filter_cloning_issue_16998` that specifically tests the recursive query scenario - All existing sort tests continue to pass - The test fails when the fix is reverted, confirming it properly detects the issue 🤖 Generated with [Claude Code](https://claude.ai/code) -- 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-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org