Skip to content

fix(mysql, sqlite): convert boolean literal to bool type#4282

Closed
kitagry wants to merge 3 commits into
sqlc-dev:mainfrom
kitagry:fix-boolean-literal
Closed

fix(mysql, sqlite): convert boolean literal to bool type#4282
kitagry wants to merge 3 commits into
sqlc-dev:mainfrom
kitagry:fix-boolean-literal

Conversation

@kitagry

@kitagry kitagry commented Jan 29, 2026

Copy link
Copy Markdown

Previously, MySQL boolean literals like SELECT true were incorrectly converted to int32 type, while table columns with BOOL/BOOLEAN/TINYINT(1) types were correctly converted to bool.

Previously, MySQL boolean literals like `SELECT true` were incorrectly
converted to int32 type, while table columns with BOOL/BOOLEAN/TINYINT(1)
types were correctly converted to bool.

The TiDB parser represents boolean literals as mysql.TypeLonglong with
value 1 (true) or 0 (false), which was indistinguishable from integer
literals like `SELECT 1`.

This fix:
- Detects boolean literals by examining the original SQL text
- Converts `true`/`false` (case-insensitive) to bool type
- Preserves integer literals like `SELECT 1` as int32
- Supports all case variations: TRUE, True, FALSE, False, etc.

Test cases:
- SELECT true/false → bool
- SELECT TRUE/FALSE → bool
- SELECT true AS col → bool
- SELECT 1/0 → int32 (unchanged)
Previously, SQLite boolean literals like `SELECT true` were incorrectly
converted to int64 type, while PostgreSQL correctly returned bool.

The ANTLR parser provides TRUE_() and FALSE_() tokens, but these were
originally implemented to return Integer in PR sqlc-dev#1447 (2022-02-19) without
specific reasoning, likely because SQLite internally uses integers (1/0)
for boolean values.

This fix changes the TRUE_()/FALSE_() handling to return ast.Boolean
instead of ast.Integer, aligning SQLite's behavior with PostgreSQL
and providing a more type-safe interface.

Test results:
- SELECT true/false → bool (was int64)
- SELECT TRUE/FALSE → bool (was int64)
- SELECT 1 → int64 (unchanged)

Added test: internal/endtoend/testdata/select_true_literal/sqlite/
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang labels Jan 29, 2026

Copy link
Copy Markdown
Collaborator

🤖 Automated message

This PR has been closed because it has merge conflicts with main. To reopen it, rebase onto the latest main to resolve the conflicts, then reopen the PR.


Generated by Claude Code

@kyleconroy kyleconroy closed this Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files. 🔧 golang

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants