Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

compiler: apply column overrides to BETWEEN parameters - #4734

Open
farisbasha wants to merge 1 commit into
sqlc-dev:mainfrom
farisbasha:fix-between-param-overrides
Open

farisbasha wants to merge 1 commit into
sqlc-dev:mainfrom
farisbasha:fix-between-param-overrides

Conversation

@farisbasha

Copy link
Copy Markdown

On the MySQL and SQLite engines, a column override does not apply to the parameters of a BETWEEN. The same column compared with = gets the override, so one query takes the custom type while its neighbour takes the plain one:

overrides:
  - column: products.code
    go_type: github.com/sqlc-dev/sqlc-testdata/pkg.CustomType
-- name: ListProductsByCode :many
SELECT * FROM products WHERE code = ?;

-- name: ListProductsBetweenCodes :many
SELECT * FROM products WHERE code BETWEEN ? AND ?;
func (q *Queries) ListProductsByCode(ctx context.Context, code pkg.CustomType) ([]Product, error)

type ListProductsBetweenCodesParams struct {
	FromCode string // expected pkg.CustomType
	ToCode   string
}

Named params (BETWEEN sqlc.arg(min_code) AND sqlc.arg(max_code)) behave the same way, and so does go_struct_tag. The PostgreSQL engine and the core analyzer (SQLCEXPERIMENT=coreanalyzer) already apply the override here.

Cause: in resolveCatalogRefs, the *ast.BetweenExpr branch builds the parameter column without OriginalName. The override lookup in codegen matches on OriginalName when it is set and on Name otherwise. For these params Name is from_code / to_code or the sqlc.arg name, so it never matches code. The A_Expr column-reference branch sets OriginalName: c.Name.

Fix: set OriginalName: c.Name in the BETWEEN branch as well. The param names stay the same.

Test: new end-to-end case internal/endtoend/testdata/overrides_between for mysql and sqlite, covering positional and named params plus an = query for comparison. Without the fix, TestReplay/base/overrides_between/{mysql,sqlite} fails on the FromCode/ToCode/MinCode/MaxCode field types. With the fix it passes.

Tests were run on a Linux server: go test ./...: every TestReplay case passes, including the new one. The only failures are TestValidSchema and internal/x/expander, which need a live PostgreSQL/MySQL ("not installed"); they fail the same way on main and go vet ./internal/compiler/. No MySQL or PostgreSQL server was running there, so the database-backed contexts and tests skipped.

This PR was prepared with AI assistance (Claude).

On the MySQL and SQLite engines, a parameter on either side of a BETWEEN
was resolved without the name of the column it is compared to, so a
column override (go_type or go_struct_tag) did not apply to it: the
params got the default type while the same column compared with = got
the override. Set OriginalName as the other column-reference paths do.
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