Skip to content

Exponential planning time when window function is partitioned by multiple columns #17401

Description

@findepi

Describe the bug

Planning time, or to be precise: optimization time, is very long for queries that have a window function partitioned by multiple columns.

To Reproduce

WITH source AS (
    SELECT
        1 AS n,
        '' AS a1, '' AS a2, '' AS a3, '' AS a4, '' AS a5, '' AS a6, '' AS a7, '' AS a8,
        '' AS a9, '' AS a10, '' AS a11, '' AS a12
)
SELECT
    sum(n) OVER (PARTITION BY
        a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12
    )
FROM source;

In 48.0.1 this takes ~543 seconds.

On current main this takes ~292 seconds
It looks O(4ⁿ)1 where n is number of columns used in PARTITION BY

Expected behavior

In DataFusion 45.0.0 this query runs in <100ms.

Additional context

No response

Footnotes

  1. based on timings when adding or removing a column. Definitely exponential, not sure the base. ↩

Activity

  1. added theissue type on Sep 4, 2025
  2. changed the title [-]Long planning time when window function is partitioned by multiple columns[/-] [+]Exponential planning time when window function is partitioned by multiple columns[/+] on Sep 4, 2025
  3. findepi commented on Sep 4, 2025

    @findepi
    MemberAuthor

    Related issues

  4. findepi commented on Sep 4, 2025

    @findepi
    MemberAuthor

    I run git bisect, inspecting timing of the query

    ( cd datafusion-cli && cargo run --bin datafusion-cli -- --command "
    WITH source AS (
        SELECT
            1 AS n,
            '' AS a1, '' AS a2, '' AS a3, '' AS a4, '' AS a5, '' AS a6, '' AS a7, '' AS a8,
            '' AS a9, '' AS a10, '' AS a11, '' AS a12
    )
    SELECT
        sum(n) OVER (PARTITION BY
            a1, a2, a3, a4, a5, a6, a7, a8, a9
        )
    FROM source;
    "; )

    it resulted in

    ea0686b8ce4b45cb61ba7fecac563cfa17f13595 is the first bad commit
    commit ea0686b8ce4b45cb61ba7fecac563cfa17f13595 (HEAD)
    Date:   Wed Feb 26 17:35:37 2025 +0300
    
        Window Functions Order Conservation -- Follow-up On Set Monotonicity (#14813)
    

    cc @berkaysynnada @ozankabak @Omega359 @alamb

  5. findepi commented on Sep 4, 2025

    @findepi
    MemberAuthor

    Some code pieces with seemingly exponential complexity

    However, every usage of multi_cartesian_product looks suspicious.
    For example, this one looks also dangerous, just not related to window functions

    .multi_cartesian_product()

  6. berkaysynnada commented on Sep 6, 2025

    @berkaysynnada
    Contributor

    Perhaps we can find a way of detecting redundancy of the order propagation over window ops (or just a simple rule) and skip those high complexity calculations

  7. findepi commented on Sep 9, 2025

    @findepi
    MemberAuthor

    @berkaysynnada has an idea how to replace the exponential algorithm with a linear one: #14813 (comment)

    🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingoptimizerOptimizer rules

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions