Repository navigation
Fix joined-table typegen and reject duplicate result column names - #167
Conversation
Failing specs:
- `select * from posts p join post_tags t ...` types every column twice
(`post_id`, ..., `post_id_2`, ...)
- a param inside a joined subquery is bound twice, so the wrapper sends
`['greeting', undefined]` for one `?` and sqlite throws "column index
out of range"
- `select s.id, u.id ...` generates `{id, id_2}`, but drivers key rows by
column name and return one `id`; generate should refuse it instead
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The parser shim listed an explicit JOIN's tables under both select_core and join_clause; ANTLR (whose shape traverse.ts expects) puts them under join_clause only. So every joined table was walked twice: `select *` typed each column twice (`post_id`, ..., `post_id_2`), and a param in a joined subquery was bound twice, crashing the wrapper at runtime. Separately, vendored TypeSQL renamed genuinely repeated result names to `id`, `id_2`. sqlfu's drivers key rows by column name, so `id_2` was always undefined. Vendored `mapColumns` now keeps the names, and generate rejects the query with an alias hint instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
commit: |
Conflicts: both PRs appended a local-divergence note to src/vendor/typesql/CLAUDE.md and fixtures to query-shapes.md; kept both. Also updated #166's uniqueFieldsByName comment: duplicate result names are now rejected by assertUniqueResultColumnNames, not renamed to id_2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PR SummaryMedium Risk Overview The ANTLR shim no longer lists join-chain tables in both
Reviewed by Cursor Bugbot for commit 17b5c93. Bugbot is set up for automated code reviews on this repo. Configure here. |
Now that repeated result names fail generate, the analyzer has to list `select *` columns the way SQLite returns them. It already dropped the right table's USING columns for a plain table, but listed shared columns twice for `natural join` and for USING inside a parenthesised join, so generate rejected valid SQL (flagged by Cursor Bugbot on #167). - the shim exposes NATURAL_() on join operators, and a parenthesised join as ANTLR's nested join_clause - traverse walks that nested join_clause with its own constraints, and drops NATURAL / USING columns for every joined item, not just tables Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit db4dca7. Configure here.
Under LEFT JOIN the analyzer only marked a plain table's columns nullable. A left-joined parenthesised join (walked by this PR's new nested join_clause path) and a left-joined subquery (already wrong on main) kept their columns not-null, though a miss returns null. All three kinds of joined item now go through one `leftJoinFields` helper. Flagged by Cursor Bugbot on #167. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Fixes three typegen bugs around joins and repeated result column names.
Before / after
select *over a join typed every column twiceexport type Result = { postId: number; title: string; taggedPostId: number; tag: string; - postId2: number; // always undefined at runtime - title2: string; - taggedPostId2: number; - tag2: string; };select s.*had the same problem.A param inside a joined subquery was bound twice
Before, calling it threw
SqlfuError: column index out of range(args['greeting', undefined]).Repeated result names are now a generate error
Before, this generated
RawResult = {id: …; id_2: …}, but node:sqlite, better-sqlite3, D1, libsql and the rest key rows by column name.select 1 as id, 2 as idcomes back as{id: 2}, soid2was alwaysundefinedandidheld whichever column came last. Now:Columns that SQLite itself merges don't count as duplicates.
natural join,join … using (id), andusinginside a parenthesized join return oneid, and the generated types now match. Before, the analyzer only mergedusingcolumns for plain tables.The other option was to type the name as the last column (what drivers return). I didn't take it because it silently hides a column the SQL asked for.
Changes
src/vendor/typesql/sqlite-query-analyzer/antlr-shim.ts:select_core.table_or_subquery_list()is empty for an explicit JOIN chainjoin_clauseonly, andtraverse.tsreads both lists. The shim listed them in both, so every joined table was walked twice. That one fix covers both the doubledselect *columns and the double-bound subquery paramsrc/vendor/typesql/codegen/sqlite.ts:mapColumnsescapes names but no longer renames repeats toid_2; noted insrc/vendor/typesql/CLAUDE.md_2names never exist on a driver row. Leaving the names alone lets sqlfu see the duplicatesrc/vendor/typesql/sqlite-query-analyzer/traverse.ts+ shim:joinUsingFieldsdrops shared NATURAL / USING columns for every joined item; a parenthesized join is walked as a nestedjoin_clause(ANTLR's shape) with its own constraintsselect *lists the same columns SQLite returns, so the duplicate check below doesn't reject valid joinstraverse.ts:leftJoinFieldsmakes every left-joined item's columns nullableleft joinonto a subquery (already wrong on main) or a parenthesized join kept its columns not-null, though a miss returns null. Only plain tables were handledsrc/typegen/index.ts:assertUniqueResultColumnNamesinprepareQueryDescriptor, which now takesqueryNametest/generate/fixtures/query-shapes.md,result-types.md,runtime.test.tsselect *join's generated types,natural/ nestedusingjoins, left-join nullability, the duplicate-name error, and a runtime call of a wrapper whose param sits in a joined subqueryThe first commit adds the specs, failing; the second fixes them. Merged with main after #166. All 1605 sqlfu tests pass locally.
Session: "sqlfu bug investigation" (
local_b1eb82a7-9715-4d23-a619-707fa217f21f)🤖 Generated with Claude Code
Package size — packed 287.8 kB (+188 B, +0.1%)
Package size
dist/vendor/*.jsbundlesvendor/sha256.jsvendor/sql-formatter/*.jsvendor/sqlfu-sqlite-parser/*.jsvendor/standard-schema/*.jsvendor/typesql/*.jsMeasured with
npm pack --dry-run --jsononsqlfu(0.0.0-dev on main vs 0.0.0-dev on this PR).