-
Notifications
You must be signed in to change notification settings - Fork 515
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
PostgreSQL CTEs auxiliary statements cannot reference each other #4441
Comments
With The generated queries can insert into orders and select totals. Is there something I am missing from your case above? CREATE TABLE orders(
region TEXT,
product TEXT,
amount INTEGER,
quantity INTEGER
);
insertOrders:
INSERT INTO orders (region, product, amount, quantity) VALUES (?, ?, ?, ?);
selectRegionalSales:
WITH regional_sales AS (
SELECT region, SUM(amount) AS total_sales
FROM orders
GROUP BY region
), top_regions AS (
SELECT region
FROM regional_sales
WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
)
SELECT region,
product,
SUM(quantity) AS product_units,
SUM(amount) AS product_sales
FROM orders
WHERE region IN (SELECT region FROM top_regions)
GROUP BY region, product; |
@griffio thanks for taking a look! Perhaps the issue I'm having is more nuanced than just any references between any CTEs auxiliary statements. I made a repo demonstrating my issue for you. Here's an example of the type of query that I'm unable to write. I also included a main function that uses the example query. In the commit I swapped |
🥼 I have reproduced a simplifed test case The example is using a CTE to create 3 rows in 3 tables that share the same key, a Looks like it doesn't currently work as the CTE Also - in the example above the TIMEZONE function is not supported by SqlDelight PostgreSql dialect (that can be added). Error is from sql-psi TableNameMixin I will have to see if there is a solution 🔬
|
Aha! Good catch.
Oh wow. It appears to compile with SQLDelight and I'm using that in production. Perhaps it's failing silently or something. I've been using that syntax to ensure UTC regardless of system timezone because other syntaxes (e.g.
Thanks 💁🏻♂️
Thanks @griffio! |
SQLDelight Version
2.0.0
SQLDelight Dialect
PostgreSQL
Describe the Bug
The PostgreSQL docs on Common Table Expressions show that auxiliary statements can reference each other. Here's the example they provide:
In this example the output of
regional_sales
is used intop_regions
. However when building PostgreSQL queries in SQLDelight, this technique fails with aNo table found with name regional_sales
error.It does work to reference the auxiliary statements in the main command (i.e. the
SELECT region FROM top_regions
bit in the example above), just not within other auxiliary statements.Stacktrace
No response
The text was updated successfully, but these errors were encountered: