Skip to content

fix position() counting bytes instead of characters - #7720

Merged
philrz merged 1 commit into
mainfrom
position-characters
Nov 24, 2026
Merged

philrz merged 1 commit into
mainfrom
position-characters

Conversation

@philrz

@philrz philrz commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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 match len(), slices and substring, 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 whatever index_base is set to.

Trying it out

On current tip of main, commit 14a07c1:

$ super -version && super -c 'values "éébar" | values {pos:position(this, "bar"), rest:this[position(this, "bar")-1:]}'
Version: v0.4.0-119-g14a07c12c
{pos:5,rest:"r"}

On this branch, commit a385ff9:

$ super -version && super -c 'values "éébar" | values {pos:position(this, "bar"), rest:this[position(this, "bar")-1:]}'
Version: v0.4.0-120-ga385ff922
{pos:3,rest:"bar"}

Testing

  • New rows in position.yaml have non-ASCII characters before the match, one of them in the substring too. Both fail on main.
  • make test-unit, make test-system and the book's mdtests pass. The position docs now say what is counted and how to get a 0-based index, with an example that slices at the result.
  • On a query that does nothing but call position() on all 75.7M rows of the mgbench bench2 dataset, the branch is 5% slower than main (632 → 669 ms), which is the cost of counting characters. The only benchmark query that uses POSITION (mgbench bench2/q3) gives the same results as main and DuckDB.

Not changed here: index_base

As discussed in #7718, position stays 1-based whatever index_base is 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 -1 and a check for "not found", because 0 - 1 is -1 and a slice that starts at -1 begins at the last character.

$ super -c '
values "éébar", "foo"
| values {input:this, pos:position(this, "bar")}
| values {input, pos, unguarded:input[pos-1:], guarded:pos > 0 ? input[pos-1:] : null}'
{input:"éébar",pos:3,unguarded:"bar",guarded:"bar"}
{input:"foo",pos:0,unguarded:"o",guarded:null}

If position obeyed index_base, the -1 would 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". With pragma index_base = 1, results would be the same as today. A 0-based "not found" would need a new value, though. -1 is the usual choice in other languages, but it would still need the check above, while null would make the slice null without one. @mccanne, if you'd like either of those, I can add it to this PR. Otherwise it's ready as is.

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.
@philrz philrz self-assigned this Nov 24, 2026
@philrz
philrz merged commit 431a0c4 into main Nov 24, 2026
4 checks passed
@philrz
philrz deleted the position-characters branch November 24, 2026 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants