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

Error when generating semantically correct postgres SQL #1858

Closed
patrickdevivo opened this issue Sep 22, 2022 · 1 comment · Fixed by #2579
Closed

Error when generating semantically correct postgres SQL #1858

patrickdevivo opened this issue Sep 22, 2022 · 1 comment · Fixed by #2579

Comments

@patrickdevivo
Copy link

Version

1.15.0

What happened?

Hi! First of all thank you for all the hard work going into sqlc - we're big fans! We use sqlc for this project and encountered an error when trying to compile a query that has 3 "parts" in a column name in a select:

unknown number of fields: 3

I've made this small repo to show reproduction of the issue: https://github.com/mergestat/sqlc-compile-bug

The error seems to originate here: https://github.com/kyleconroy/sqlc/blob/0a1b04eb47c80236db176805fe43f709042b805c/internal/compiler/output_columns.go#L474 where it looks like only 1 or 2 "parts" are permitted, when it should be possible to allow 3 (in our queries at least, which run fine in postgres directly).

We've been able to get around this by aliasing the schema.table in our query, so we only need to refer to a column with 2 "parts" - but it would still be nice to avoid this limitation (as it would allow us to keep our queries a bit cleaner, especially in more complex ones).

Relevant log output

unknown number of fields: 3

Database schema

CREATE SCHEMA some_schema;

CREATE TABLE some_schema.some_table (
  id   BIGSERIAL PRIMARY KEY,
  some_column  text
);

SQL queries

-- AllRowsOK :many
SELECT id, some_table.some_column FROM some_schema.some_table;

-- AllRowsBug :many
SELECT id, some_schema.some_table.some_column FROM some_schema.some_table;

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "query.sql"
    schema: "schema.sql"
    gen:
      go:
        package: "example"
        out: "example"

Playground URL

No response

What operating system are you using?

Linux, macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@patrickdevivo patrickdevivo added bug Something isn't working triage New issues that hasn't been reviewed labels Sep 22, 2022
@kyleconroy kyleconroy added 📚 postgresql 💻 darwin 💻 linux 🔧 golang and removed triage New issues that hasn't been reviewed labels Nov 9, 2022
@GeertJohan
Copy link

I encounter the same error for a slightly different scenario. In our schema.sql the following statement is present:

CREATE MATERIALIZED VIEW stock.available_quantities
AS 
SELECT
   stock.items.id AS item_id,
   items.available_quantity
FROM
   stock.items;

It fails on stock.items.id. The fix was easy ofcourse (just use items.id) but I thought it would be useful to report here :)

orisano added a commit to orisano/sqlc that referenced this issue Aug 5, 2023
kyleconroy pushed a commit that referenced this issue Aug 28, 2023
* fix(compiler): support identifiers with schema

close #1858

* test: update endtoend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants