Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BETWEEN keyword in CTE generates duplicate params #2983

Open
penmanglewood opened this issue Nov 16, 2023 · 0 comments · May be fixed by #3164
Open

BETWEEN keyword in CTE generates duplicate params #2983

penmanglewood opened this issue Nov 16, 2023 · 0 comments · May be fixed by #3164
Labels
bug Something isn't working triage New issues that hasn't been reviewed

Comments

@penmanglewood
Copy link

penmanglewood commented Nov 16, 2023

Version

1.23.0

What happened?

I have a query using a CTE with a WHERE clause filtering a date range using BETWEEN, and sqlc generated the params struct with duplicate fields for the from and to dates.

const listAuthorsWithCTE = `-- name: ListAuthorsWithCTE :many
WITH page AS (SELECT id FROM authors WHERE authors.created_at BETWEEN ? AND ?)
SELECT authors.id, name, bio, created_at, page.id FROM authors
INNER JOIN page ON page.id = authors.id
ORDER BY name
`

type ListAuthorsWithCTEParams struct {
	FromCreatedAt   time.Time
	FromCreatedAt_2 time.Time
	ToCreatedAt     time.Time
	ToCreatedAt_2   time.Time
}

And the database is queried with all of them.

rows, err := q.db.QueryContext(ctx, listAuthorsWithCTE,
	arg.FromCreatedAt,
	arg.FromCreatedAt_2,
	arg.ToCreatedAt,
	arg.ToCreatedAt_2,
)

Note that changing the WHERE clause to use >= and < outputs the correct number of parameters.

Relevant log output

No response

Database schema

CREATE TABLE authors (
  id   INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255)      NOT NULL,
  bio  VARCHAR(255),
  created_at DATETIME NOT NULL
);

SQL queries

-- name: ListAuthorsWithCTE :many
WITH page AS (SELECT id FROM authors WHERE authors.created_at BETWEEN ? AND ?)
SELECT * FROM authors
INNER JOIN page ON page.id = authors.id
ORDER BY name;

Configuration

{
  "version": "2",
  "sql": [{
    "schema": "schema.sql",
    "queries": "query.sql",
    "engine": "mysql",
    "gen": {
      "go": {
        "out": "db"
      }
    }
  }]
}

Playground URL

https://play.sqlc.dev/p/f475a847db9fef33de461eadc8638ca289c85e2cd7ab40df344454ccb721c836

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

@penmanglewood penmanglewood added bug Something isn't working triage New issues that hasn't been reviewed labels Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage New issues that hasn't been reviewed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant