Skip to content

A RIGHT or FULL JOIN doesn't make the joins to its left nullable #4690

Open

Description

@Bockit

Version

1.31.1

What happened?

A RIGHT JOIN or FULL JOIN makes everything joined before it nullable, and a parenthesised join on the nullable side of a LEFT JOIN is nullable as a whole. sqlc decides each join from its own type alone, so the tables of the earlier join are generated as never null.

For the query below I get Name string. I expected pgtype.Text: a book whose author_id matches no author comes back with a NULL name.

type ListTitlesRow struct {
	Name  string
	Title string
}

The same happens with FROM authors a LEFT JOIN (books b JOIN books c ON c.id = b.id) ON b.author_id = a.id: the columns of b and c are never null.

It is the same on main (3ad8b20). I found no existing issue; #604 is a different case.

isTableRequired passes a fixed value down for each join type and ignores the side it was reached through. A five-line fix with test cases is here: Bockit@fd812e9

The analysis was done with AI help and checked by me.

Relevant log output

No response

Database schema

CREATE TABLE authors (id BIGSERIAL PRIMARY KEY, name text NOT NULL);
CREATE TABLE books (id BIGSERIAL PRIMARY KEY, author_id bigint NOT NULL, title text NOT NULL);

SQL queries

-- name: ListTitles :many
SELECT a.name, c.title
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
RIGHT JOIN books c ON c.author_id = a.id;

Configuration

version: "2"
sql:
  - engine: postgresql
    schema: schema.sql
    queries: query.sql
    gen:
      go:
        package: db
        out: db
        sql_package: pgx/v5

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions