Skip to content

Generating correct LIMIT and OFFSET field names#209

Merged
kyleconroy merged 1 commit intosqlc-dev:masterfrom
dominikbraun:issue/201
Dec 22, 2019
Merged

Generating correct LIMIT and OFFSET field names#209
kyleconroy merged 1 commit intosqlc-dev:masterfrom
dominikbraun:issue/201

Conversation

@dominikbraun
Copy link
Contributor

Fixes #201.

A query like this caused incorrect field names for LIMIT and OFFSET:

-- name: GetFoo :one
SELECT
    (SELECT 1) AS baz
FROM foo
WHERE
	bar = $1
LIMIT $2
OFFSET $3;

The generated struct used to look like this:

type GetFooParams struct {
	Bar   string `json:"bar"`
	Bar_2 string `json:"bar_2"`
	Bar_3 string `json:"bar_3"`
}

This happened because LIMIT and OFFSET are nil for the subquery but they've been assigned to the paramSearch though.

@kyleconroy
Copy link
Collaborator

@dominikbraun Thanks for the pull request. I think the existing limit / offset code needs to be totally rewritten. For example I don't think the following query will compile correctly:

-- name: GetFoo :one
WITH cte as (
    SELECT foo FROM bar LIMIT $1
)
SELECT
foo
FROM cte
OFFSET $2;

@dominikbraun
Copy link
Contributor Author

dominikbraun commented Dec 21, 2019

Yap, and while (SELECT 1) AS baz works as a subquery for the query shown above, (SELECT 1 FROM foo) AS baz runs into an error:

column reference "bar" is ambiguous

You may merge this PR as a temporary hotfix, or just rewrite the affected code from scratch.

@kyleconroy
Copy link
Collaborator

You may merge this PR as a temporary hotfix

Since I'm not sure about the long-term fix, adding a hot fix seems like a fine approach.

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.

LIMIT, OFFSET don't work when subquery is present

2 participants