Repository navigation
fix position() counting bytes instead of characters - #7720
Merged
Merged
Conversation
position() returned a byte offset, so non-ASCII characters before the match made the result too large. Count Unicode code points instead, as len(), slices and substring do, and as SQL specifies.
mccanne
approved these changes
Nov 24, 2026
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.
position()returned a byte offset, so any non-ASCII characters before the match made the result too large, e.g.,position("éébar", "bar")gave 5 instead of 3. That didn't matchlen(), slices andsubstring, which count Unicode code points, so slicing a string at the result cut off the start of the match. Postgres and DuckDB give 3, and the SQL standard defines the result in characters. As discussed in #7718, the result stays 1-based whateverindex_baseis set to.Trying it out
On current tip of
main, commit 14a07c1:On this branch, commit a385ff9:
Testing
position.yamlhave non-ASCII characters before the match, one of them in the substring too. Both fail onmain.make test-unit,make test-systemand the book's mdtests pass. Thepositiondocs now say what is counted and how to get a 0-based index, with an example that slices at the result.position()on all 75.7M rows of the mgbenchbench2dataset, the branch is 5% slower thanmain(632 → 669 ms), which is the cost of counting characters. The only benchmark query that usesPOSITION(mgbench bench2/q3) gives the same results asmainand DuckDB.Not changed here:
index_baseAs discussed in #7718,
positionstays 1-based whateverindex_baseis set to. While writing the new docs example, I saw what that means in a pipe query, which is 0-based by default: slicing at the match takes a-1and a check for "not found", because0 - 1is-1and a slice that starts at-1begins at the last character.If
positionobeyedindex_base, the-1would go away, and in SQL queries that don't set the pragma,SUBSTRING(s FROM POSITION('bar' IN s))on"foobar"would give"bar"instead of today's"ar". Withpragma index_base = 1, results would be the same as today. A 0-based "not found" would need a new value, though.-1is the usual choice in other languages, but it would still need the check above, whilenullwould make the slicenullwithout one. @mccanne, if you'd like either of those, I can add it to this PR. Otherwise it's ready as is.