Repository navigation
Fix internal error on ORDER BY positions into a star or out of range - #672
Merged
staticlibs merged 1 commit intoNov 29, 2026
Merged
Conversation
With a database collation that differs from DuckDB's byte-wise string ordering (e.g. en_US.UTF-8), an ORDER BY position that does not map to a select-list entry - a column a star expands to, or an out-of-range position - crashed the pushdown: INTERNAL Error: Attempting to dereference an optional pointer that is not set e.g. SELECT * FROM pg.t ORDER BY 2 PostgresOrderedSelectEntry encoded "a position whose column is unknown" as a null entry with is_reference set, and the collation-pinning branch dereferenced that entry. ORDER BY keys that refer to result columns are now resolved to an explicit PostgresOrderedColumn - its result position, the select-list entry producing it, or both - and a single write_column decides whether to keep the position / alias, pin the collation through the entry, or fall back to local execution. Column types come from binding the whole query, which expands stars, instead of separate leftmost-SELECT and per-position caches. Positions into a star keep the position for non-string columns, string columns with a differing collation and out-of-range positions run locally (DuckDB orders them / raises its binder error), and positions before a star can now pin the collation of their entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
staticlibs
approved these changes
Nov 29, 2026
staticlibs
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR! It looks good to me.
It also introduces a minor problem with a C-collation databases - I will fix it in a follow-up.
Contributor
Author
|
thanks @staticlibs - and good catch for #675 🙌 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On a Postgres database whose collation differs from DuckDB's byte-wise string ordering (e.g.
en_US.UTF-8), the remote pushdown crashes when anORDER BYposition doesn't map to a select-list entry. That happens for a column a*expands to, or for an out-of-range position:PostgresOrderedSelectEntryrepresented "a position whose column is unknown" as a null entry withis_referenceset. The collation-pinning branch then dereferenced that null entry.Fix
WriteOrderListnow resolves eachORDER BYkey that refers to a result column into aPostgresOrderedColumn. It holds the result position, the select-list entry producing that column, or both. A singlewrite_columnthen picks one of three outcomes:COLLATE "C"through the entry's expression;PostgresPushdownNotSupported).Column types now come from binding the whole query, which also expands stars. This replaces the separate leftmost-SELECT and per-position type caches.
Behavior
SELECT * … ORDER BY 2(string column, non-C collation)SELECT * … ORDER BY 3, 1(non-string columns)ORDER BY 3 …, 1 …SELECT v … ORDER BY 2(out of range)SELECT v || '!' AS w, * … ORDER BY 1COLLATE "C"ORDER BY ALLwith a*in the select listTests
test/sql/remote_pushdown/remote_pushdown_collation.test: positions into*, positions before and after*, aUNION ALLof*queries, and the out-of-range errors. Each case checks both results andEXPLAIN.test/sql/*suite passes locally against PostgreSQL 18 with anen_US.UTF-8database loaded bycreate-postgres-tables.sh: 192 test cases, 147,866 assertions, 11 skipped for missing environments. That run used CI's settings: UTC and thepostgresrole.remote_pushdownsuite also passes against aC-collation database.🤖 Generated with Claude Code