Skip to content

SQL set operations ignore top-level ORDER BY and LIMIT #7729

Backlog

Description

@yuchen-ecnu

Describe the bug

A top-level ORDER BY or LIMIT following a SQL set operation is silently ignored. This can return rows in the wrong order or return more rows than requested.

To Reproduce

import daft

query = "SELECT 2 AS id UNION ALL SELECT 1 AS id ORDER BY id"
print(daft.sql(query).to_pydict())
print(daft.sql(query + " LIMIT 1").to_pydict())

With Daft 0.7.26 using the native runner, the output is:

{'id': [2, 1]}
{'id': [2, 1]}

Expected behavior

The first query should return {'id': [1, 2]}; the second should return {'id': [1]}. The clauses apply to the result of the entire set operation.

Component(s)

Other (SQL planner)

Additional context

Reproduced with the official Daft 0.7.26 wheel, Python 3.12.13, and the native runner on Linux. PostgreSQL 16.16, MySQL 8.0.46, Spark SQL 3.5.1, DuckDB 2.0.0, SQLite 3.53.1, and Flink SQL 1.20.3 in batch mode return 1, 2 for the first query.

The same early return remains in the current SQL planner: the set-operation branch returns before the top-level clauses are planned.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions