Wrong results: `ORDER BY ... NULLS LAST` on a running `MAX` window function puts the NULL row first

Aberta
#27,267 4 comentários 0 reações 1 responsável Ver no GitHub

Mantenedores costumam responder em até 1 dia

@itang06 já está trabalhando nisso.

Desde 11/11/2026.

Avaliação

Esta issue ainda não foi avaliada.

Descrição

bug
Describe the bug

A query that computes MAX(x) OVER (ORDER BY ts) and then sorts on that column with ASC NULLS LAST returns rows in the wrong order when x contains NULLs. The NULL row is returned first, where the query asked for it last.

The same wrong order appears for:

  • MIN(x) OVER (ORDER BY ts) sorted DESC NULLS LAST
  • MAX(x) OVER (ORDER BY ts ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) sorted DESC NULLS FIRST
To Reproduce
set datafusion.execution.target_partitions = 1;

CREATE TABLE t(ts INT, x INT) AS VALUES (1, NULL), (2, 3), (3, 1), (4, 5);

SELECT ts, m
FROM (
  SELECT ts, MAX(x) OVER (ORDER BY ts) AS m
  FROM (SELECT * FROM t ORDER BY ts)
)
ORDER BY m ASC NULLS LAST;

Returns:

+----+------+
| ts | m    |
+----+------+
| 1  | NULL |     <-- NULL is first, not last!
| 2  | 3    |
| 3  | 3    |
| 4  | 5    |
+----+------+
Expected behavior

NULLS LAST means the NULL row comes after every non-NULL value:

+----+------+
| ts | m    |
+----+------+
| 2  | 3    |
| 3  | 3    |
| 4  | 5    |
| 1  | NULL |
+----+------+
Additional context

EXPLAIN shows the final sort on m is removed, so the planner believes the window output already satisfies m ASC NULLS LAST. Reproduced on main at 94486b1e93.

Found by @jayzhan211 while reviewing the EquivalenceProperties docs in #27250. The original report and repro are in this review comment: https://github.com/apache/datafusion/pull/27250#discussion_r4512590554

Linguagem predominante
Rust
Estrelas
9.6k
Forks
2.6k
Merge médio
3d 4h
PRs com merge (30d)
345

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de apache/datafusion

Todas as issues de apache/datafusion

Issues semelhantes

Mais issues de Rust

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.