Skip to content

Restructure core codepaths to prevent stack overflows #16788

@ahmed-mez

Description

@ahmed-mez

Describe the bug

In PR #16506, we added #[recursive] annotations (behind the recursive_protection feature flag) to a subset of recursive functions across the optimizer, physical planner, and expression evaluator. While this prevented stack-overflow panics on deeply nested plans, benchmarks showed a 1–2% slowdown in planning time when the feature is enabled.

“The benchmarks seem to imply this has some non trivial overhead (slows down planning by 1–2%).
I wonder if there is any way to restructure the affected codepaths to avoid the recursion (change them into an iterative version, for example, or Box more structures to reduce the stack size?)”
@alamb

This epic will serve as a central place to explore multiple approaches (e.g., iterative rewrites, Box-based data structures to shrink stack usage, or other patterns) to reduce/eliminate deep recursion in key codepaths, remove the need for defensive macros, and regain any lost performance, all while still handling arbitrarily nested plans reliably.

To Reproduce

#16787 provides a reproducer.

Expected behavior

DataFusion doesn't overflow its stack.

Additional context

See the discussion in #16506

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions