Skip to content

SELECT WITH query with multiple table aliases in FROM get ignored, only last one considered #1237

Description

@gsora

Version

1.10.0

What happened?

When selecting with SELECT WITH with multiple table aliases in the FROM statement, only the last one on the statements list gets considered.

Given the query, we're expecting to select everything from both c1 and c2, but the generated query only selects from c2:

WITH
	q
		AS (
			SELECT
				authors.name, authors.bio
			FROM
				authors
				LEFT JOIN fake ON authors.name = fake.name
		)
SELECT
	c2.name, c2.bio, c2.name, c2.bio
FROM
	q AS c1,
	q as c2

Relevant log output

No response

Database schema

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

CREATE TABLE fake (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

SQL queries

-- name: BadQuery :exec
WITH
	q
		AS (
			SELECT
				authors.name, authors.bio
			FROM
				authors
				LEFT JOIN fake ON authors.name = fake.name
		)
SELECT
	*
FROM
	q AS c1,
	q as c2;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/79910ee88639b38cdbcbbefeccb1e1a55b8492776ed7fd0a4bbb617217411d46

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Oct 14, 2021
  2. added a commit that references this issue on Oct 10, 2023
    3b2ec9f
  3. kyleconroy commented on Oct 24, 2023

    @kyleconroy
    Collaborator

    This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future.

    You can play around with the working example on the playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions