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

Converting quotes properly to double quotes in Postgres in custom field name scenario #4878

Open
2 tasks done
IndrekV opened this issue Sep 3, 2024 · 5 comments
Open
2 tasks done
Labels
bug Invalid compiler output or panic

Comments

@IndrekV
Copy link

IndrekV commented Sep 3, 2024

What happened?

When using quotes in field names, it should properly convert them to double quotes, but PRQL just removes them.

Note the quoted sum_income part that converts to sum_income without any quotes.

PRQL input

from invoices
group customer_id (
  aggregate {
    `sum_income` = sum total,
  }
)
join c=customers (==customer_id)
select {
  sum_income
}
take 1..10

SQL output

WITH table_0 AS (
  SELECT
    COALESCE(SUM(total), 0) AS sum_income,
    customer_id
  FROM
    invoices
  GROUP BY
    customer_id
)
SELECT
  table_0.sum_income
FROM
  table_0
  JOIN customers AS c ON table_0.customer_id = c.customer_id
LIMIT
  10

Expected SQL output

WITH table_0 AS (
  SELECT
    COALESCE(SUM(total), 0) AS "sum_income",
    customer_id
  FROM
    invoices
  GROUP BY
    customer_id
)
SELECT
  table_0."sum_income"
FROM
  table_0
  JOIN customers AS c ON table_0.customer_id = c.customer_id
LIMIT
  10

MVCE confirmation

  • Minimal example
  • New issue

Anything else?

No response

@IndrekV IndrekV added the bug Invalid compiler output or panic label Sep 3, 2024
@max-sixty
Copy link
Member

Thanks for the issue.

Currently PRQL evaluates that ident as not requiring quotes — are they required?

@IndrekV
Copy link
Author

IndrekV commented Sep 3, 2024

Thanks for the quick reply.

In most of the cases they are not required. The scenario I am working on is if a user entered text of the field name is a reserved word by Postgres. In that case I add quotes and expect them to be double quotes as otherwise SQL fails.

A good example is if we replace use_income with user (which is a reserved word in Postgres) with double quotes there would not be any problems as SQL is valid but without quotes it the Postgres query is invalid.

@max-sixty
Copy link
Member

max-sixty commented Sep 3, 2024

There does indeed seem to be a problem here:

from x
derive y = user

...doesn't quote user...

SELECT
  *,
  user AS y
FROM
  x

-- Generated by PRQL compiler version:0.13.0 (https://prql-lang.org)

...despite user being in the list of keywords we look at, from

let reverse_index: HashMap<&Keyword, usize> = ALL_KEYWORDS_INDEX

I'll have a look later, thanks for the issue

@IndrekV
Copy link
Author

IndrekV commented Sep 18, 2024

Hey @max-sixty is there any update on this issue?

@max-sixty
Copy link
Member

max-sixty commented Sep 18, 2024

Sorry for the delay, I will actually take a look at this, latest this weekend. Hold me accountable if you don't hear anything :)

Edit: didn't get to it this weekend but haven't dropped it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Invalid compiler output or panic
Projects
None yet
Development

No branches or pull requests

2 participants