cockroachdb / cockroachdb/cockroach

sql: null_ordered_last does not produce correct results with tuples

Open
#93,558 0 comments 0 reactions 0 assignees View on GitHub
A-sql-pgcompat C-bug docs-done docs-known-limitation T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

By default, CockroachDB orders NULLs before all other values. For compatibility with Postgres, we added a session setting, `null_ordered_last`, which changes the default to order NULLs after all other values. This works in most cases, thanks to some transformations we make in the optimizer to add extra ordering columns. However, it is broken when the ordering column is a tuple.

**To Reproduce**

On `./cockroach demo`, on master (23.1 alpha), run the following:
```
SET null_ordered_last = true;

WITH t (x, y) AS (
VALUES
((1, 1), 1),
((NULL::RECORD), 2),
((1, NULL::INT), 3),
((NULL::INT, NULL::INT), 4)
)
SELECT *
FROM t
ORDER BY x;
```
The result on CockorachDB:
```
x | y
--------+----
(1,) | 3
(1,1) | 1
NULL | 2
(,) | 4
```
The result on Postgres:
```
x | y
-------+---
(1,1) | 1
(1,) | 3
(,) | 4
| 2
```

**Expected behavior**
The CockroachDB output should match Postgres when `null_ordered_last` is true.

**Environment:**
- CockroachDB version: v23.1.0-alpha....dirty
- Server OS: macOS Monterey
- Client app: cockroach demo

Jira issue: CRDB-22400

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.