Skip to content

Fix joined-table typegen and reject duplicate result column names - #167

Merged
mmkal merged 5 commits into
mainfrom
duplicate-result-column-names
Oct 29, 2026
Merged

mmkal merged 5 commits into
mainfrom
duplicate-result-column-names

Conversation

@mmkal

@mmkal mmkal commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes three typegen bugs around joins and repeated result column names.

Before / after

select * over a join typed every column twice

select * from posts p join post_tags t on t.tagged_post_id = p.post_id;
 export 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

select p.title
from posts p
join (select tagged_post_id from post_tags where tag = :tag) t on t.tagged_post_id = p.post_id;
-args: [params.tag, params.param2]  // one `?`, two args, plus a phantom `param2` in Params
+args: [params.tag]

Before, calling it threw SqlfuError: column index out of range (args ['greeting', undefined]).

Repeated result names are now a generate error

select s.id, u.id from sessions s join users u on u.id = s.user_id;

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 id comes back as {id: 2}, so id2 was always undefined and id held whichever column came last. Now:

Query listSessions returns more than one column named `id`. Rows are keyed by column name, so only the last one would reach your code. Select the columns explicitly and alias the duplicates, e.g. `u.id as user_id`.

Columns that SQLite itself merges don't count as duplicates. natural join, join … using (id), and using inside a parenthesized join return one id, and the generated types now match. Before, the analyzer only merged using columns 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

Change Purpose
src/vendor/typesql/sqlite-query-analyzer/antlr-shim.ts: select_core.table_or_subquery_list() is empty for an explicit JOIN chain ANTLR puts a JOIN's tables under join_clause only, and traverse.ts reads both lists. The shim listed them in both, so every joined table was walked twice. That one fix covers both the doubled select * columns and the double-bound subquery param
src/vendor/typesql/codegen/sqlite.ts: mapColumns escapes names but no longer renames repeats to id_2; noted in src/vendor/typesql/CLAUDE.md The _2 names never exist on a driver row. Leaving the names alone lets sqlfu see the duplicate
src/vendor/typesql/sqlite-query-analyzer/traverse.ts + shim: joinUsingFields drops shared NATURAL / USING columns for every joined item; a parenthesized join is walked as a nested join_clause (ANTLR's shape) with its own constraints select * lists the same columns SQLite returns, so the duplicate check below doesn't reject valid joins
traverse.ts: leftJoinFields makes every left-joined item's columns nullable A left join onto a subquery (already wrong on main) or a parenthesized join kept its columns not-null, though a miss returns null. Only plain tables were handled
src/typegen/index.ts: assertUniqueResultColumnNames in prepareQueryDescriptor, which now takes queryName Fail generate with an alias hint that names the query. All three generate paths (files, inline config, query catalog) go through it. Ad-hoc SQL runner analysis doesn't, so the UI can still run such queries
test/generate/fixtures/query-shapes.md, result-types.md, runtime.test.ts Specs: the select * join's generated types, natural / nested using joins, left-join nullability, the duplicate-name error, and a runtime call of a wrapper whose param sits in a joined subquery

The 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

main this PR Δ
packed 287.6 kB 287.8 kB +0.1%
unpacked 1.16 MB 1.16 MB +0.1%
files 226 226 0

dist/vendor/*.js bundles

main this PR Δ
vendor/sha256.js 4.3 kB 4.3 kB 0
vendor/sql-formatter/*.js 94.5 kB 94.5 kB 0
vendor/sqlfu-sqlite-parser/*.js 17.2 kB 17.2 kB 0
vendor/standard-schema/*.js 2.8 kB 2.8 kB 0
vendor/typesql/*.js 135.1 kB 135.2 kB +0.1%

Measured with npm pack --dry-run --json on sqlfu (0.0.0-dev on main vs 0.0.0-dev on this PR).

mmkal and others added 2 commits October 29, 2026 15:22
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>
@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/sqlfu@167

commit: 17b5c93

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>
@mmkal
mmkal marked this pull request as ready for review October 29, 2026 15:42
@cursor

cursor Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches vendored TypeSQL analysis/codegen and all generate paths; behavior changes for join queries and previously accepted duplicate column names now error at generate time.

Overview
Fixes join-related typegen so generated types and runtime args match SQLite, and fails generate when a query would expose duplicate result column names that drivers cannot represent faithfully.

The ANTLR shim no longer lists join-chain tables in both table_or_subquery_list() and join_clause(), which had caused joined FROM items to be traversed twice—doubling select * columns and binding parameters in joined subqueries twice. The analyzer now treats NATURAL / USING merges and parenthesized joins like SQLite (including nullable columns from LEFT joins on subqueries and nested joins), and TypeSQL codegen keeps repeated result names instead of renaming them to id_2.

prepareQueryDescriptor now takes queryName and runs assertUniqueResultColumnNames, throwing a clear alias hint when two result columns share a name (merged join columns are excluded). Fixture and runtime tests cover join select *, natural/nested using, duplicate-name errors, and single-bound params in joined subqueries.

Reviewed by Cursor Bugbot for commit 17b5c93. Bugbot is set up for automated code reviews on this repo. Configure here.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread packages/sqlfu/src/typegen/index.ts Resolved
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>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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>
@mmkal
mmkal merged commit 0aeca57 into main Oct 29, 2026
10 checks passed
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.

1 participant