Skip to content

codegen/golang: map sized SQLite types like CHAR(255) and BOOLEAN(2) - #4727

Open
imharjot wants to merge 1 commit into
sqlc-dev:mainfrom
imharjot:fix/sqlite-sized-types
Open

imharjot wants to merge 1 commit into
sqlc-dev:mainfrom
imharjot:fix/sqlite-sized-types

Conversation

@imharjot

@imharjot imharjot commented Dec 1, 2026

Copy link
Copy Markdown

The SQLite engine keeps the declared arguments in the column type name, so CHAR(255), BOOLEAN(2), NUMERIC(10,2), FLOAT(8) and INT(11) missed the exact-name switch in sqliteType and were generated as any. A bare CHAR was also missing from the text family.

This strips the argument list (everything from the first () before matching, and adds char to the text case. The existing case lists and the nullable/pointer handling are unchanged, and varchar(36) still maps to string as before. I did not touch the affinity rules, so types such as BIGINT UNSIGNED behave as they did.

This is a visible codegen change. It applies to any SQLite type with an argument list whose base name sqlc already maps, not only the ones in the issue. For example, BLOB(16) now generates []byte and TEXT(20) generates string, where both were any before. Sized date and time types such as DATETIME(3) stay any. SQLite drivers (ncruces/go-sqlite3, mattn, modernc) only parse a column into time.Time when the declared type is exactly DATE, DATETIME or TIMESTAMP, so time.Time would fail to scan the string they return. The opt-in core analyzer already passes DATETIME(3) through as datetime with a separate length, so it maps that column to time.Time the same way it does on main. I left that path alone, so the core replay of the new case differs on k_dt.

The integer primary key autoincrement example from the comments already generates int64 on main, so this covers the remaining cases in the issue.

Tests: I added the endtoend case internal/endtoend/testdata/sqlite_sized_types/sqlite. The schema covers CHAR(255), CHAR(16) NOT NULL, CHAR, VARCHAR(36), BOOLEAN(2), NUMERIC(10,2), NUMERIC(10, 2), FLOAT(8), INT(11) (nullable and not null), DATETIME(3) (which stays any) and BLOB(16). It has one package with default settings and one with emit_pointers_for_null_types: true. With the code change reverted the case fails and the models come out as any.

Commands run:

  • go test ./internal/endtoend -run 'TestReplay/base/sqlite_sized_types'
  • go test ./internal/codegen/... ./internal/endtoend/... (database-backed contexts skip without databases)
  • go vet on the codegen package, and gofmt -l on the changed files

No other golden files changed.

Fixes #3754

The SQLite engine keeps declared arguments in the column type name, so
CHAR(255), BOOLEAN(2), NUMERIC(10,2), FLOAT(8) and INT(11) missed the
exact-name switch in sqliteType and came out as any. Strip the argument
list before matching, and treat bare char as a text type.

Fixes sqlc-dev#3754
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant