Skip to content

BETWEEN keyword in CTE generates duplicate params #2983

@penmanglewood

Description

@penmanglewood

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions