mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-29 09:33:48 +00:00
Make GROUP BY and ORDER BY use BINARY for computed keys by default, matching SQLite. Direct columns keep their declared collation, and explicit COLLATE still applies. Use the existing comparison resolver for GROUP BY and ORDER BY keys so the whole expression determines the rule. Add two SQL regression tests, including ORDER BY with and without LIMIT. We found this during SQLite compatibility checks for our OpenTelemetry server, which uses Turso for storage. In the example below, `main` inherits the host’s NOCASE rule when building the URL. It merges two `/help/` failures with one `/Help/` failure, causing a false positive alert. SQLite and this patch keep them separate. Both new regression tests fail on main; all 122 collation cases pass on this branch. ## Real-world example The http_requests table records HTTP requests. We want to find URLs that returned 404 at least three times. ### Question **Which URLs returned 404 at least 3 times**? ### `requests` table | id | received_at | host | path | status | |---|---|---|---|---| | 101 | 2026-10-08T09:10:00Z | fakos.dev | /docs/ | 200 | | 102 | 2026-10-08T09:35:00Z | fakos.dev | /docs/quickstart/ | 200 | | 103 | 2026-10-08T11:20:00Z | fakos.dev | /help/ | 404 | | 104 | 2026-10-08T11:21:00Z | fakos.dev | /Help/ | 404 | | 105 | 2026-10-08T12:05:00Z | fakos.dev | /help/ | 404 | | 106 | 2026-10-08T12:10:00Z | fakos.dev | /docs/getting-started/ | 404 | | 107 | 2026-10-08T12:15:00Z | fakos.dev | /docs/getting-started/ | 404 | | 108 | 2026-10-08T12:20:00Z | fakos.dev | /docs/getting-started/ | 404 | > [!IMPORTANT] > > `host` uses NOCASE. The full URL must keep path case. `endpoint` names that URL in SELECT, GROUP BY, and ORDER BY. ### Query (`query.sql`) ```sql SELECT 'https://' || host || path AS endpoint, count(*) AS not_found FROM http_requests WHERE status = 404 GROUP BY endpoint HAVING count(*) >= 3 ORDER BY not_found DESC, endpoint; ``` ### ❌ turso - `main` | endpoint | not_found | |---|---| | https://fakos.dev/docs/getting-started/ | 3 | | https://fakos.dev/help/ | 3 | Main wrongly combines `/help/` and `/Help/` into three failures. ### ✅ sqlite and turso `group-order-by-default-collation` | endpoint | not_found | |---|---| | https://fakos.dev/docs/getting-started/ | 3 | Only `/docs/getting-started/` failed three times. ## Steps to reproduce ### 1 - Create `requests_table.sql` ```sql CREATE TABLE http_requests( id INTEGER PRIMARY KEY, received_at TEXT, host TEXT COLLATE NOCASE, path TEXT, status INTEGER ); INSERT INTO http_requests VALUES (101, '2026-10-08T09:10:00Z', 'fakos.dev', '/docs/', 200), (102, '2026-10-08T09:35:00Z', 'fakos.dev', '/docs/quickstart/', 200), (103, '2026-10-08T11:20:00Z', 'fakos.dev', '/help/', 404), (104, '2026-10-08T11:21:00Z', 'fakos.dev', '/Help/', 404), (105, '2026-10-08T12:05:00Z', 'fakos.dev', '/help/', 404), (106, '2026-10-08T12:10:00Z', 'fakos.dev', '/docs/getting-started/', 404), (107, '2026-10-08T12:15:00Z', 'fakos.dev', '/docs/getting-started/', 404), (108, '2026-10-08T12:20:00Z', 'fakos.dev', '/docs/getting-started/', 404); ``` ### 2 - Run `requests_table.sql` and `query.sql` in SQLite for reference ``` sqlite3 :memory: ".read requests_table.sql" ".read query.sql" ``` ### 3 - Run `requests_table.sql` and `query.sql` in Turso (`main`) ``` cargo run -q --bin tursodb -- :memory: -q -m list "$(cat requests_table.sql query.sql)" ``` We used AI agents (mostly Codex) to trace the incorrect collation lookup and prepare the smallest possible Rust patch. We have reviewed both end-to-end SQL behavior and all code changes included in this patch. --- **UPDATE**: Reviewing the snapshot update commit is easier with whitespace disabled: https://github.com/tursodatabase/turso/pull/9655/ch anges/42a4c81f9a8a5f514f2d544b8fa6e3e16d01ca27?w=1 Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com> Closes #9655