Skip to content

Window functions fail when ORDER BY uses a binary column #24327

Description

@fornwall

Describe the bug

Window functions with an ORDER BY expression whose type is Binary fail during type coercion when the window uses the default frame.

To Reproduce

Run this self-contained query in datafusion-cli on current main:

SELECT x, COUNT(*) OVER (ORDER BY x)
FROM (VALUES
    (arrow_cast('a', 'Binary')),
    (arrow_cast('b', 'Binary')),
    (arrow_cast('b', 'Binary'))
) AS t(x)
ORDER BY x;

It fails with:

Error: type_coercion
caused by
Internal error: Cannot run range queries on datatype: Binary.

The same problem affects LargeBinary, BinaryView, FixedSizeBinary, and dictionary-wrapped binary values.

Expected behavior

The query should succeed and treat equal binary values as peers. Its result should be equivalent to:

61 1
62 3
62 3

Free RANGE frames whose bounds are only UNBOUNDED or CURRENT ROW should support binary order keys. RANGE frames with finite offsets, such as RANGE BETWEEN 1 PRECEDING AND CURRENT ROW, should remain unsupported because they require arithmetic on the order key, but should produce a planning error rather than an internal error.

Additional context

Using an explicit row-based frame avoids the error:

SELECT x, COUNT(*) OVER (
    ORDER BY x ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
)
FROM (VALUES
    (arrow_cast('a', 'Binary')),
    (arrow_cast('b', 'Binary')),
    (arrow_cast('b', 'Binary'))
) AS t(x)
ORDER BY x;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions