You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WITH table_0 AS (
SELECT
DATE_TRUNC('day', ts) AS interaction_day,
substr(user, length('u:') +1) AS user_id
FROM
table2
),
table_1 AS (
SELECT
substr(user, length('u:') +1) AS user_id,
DATE_TRUNC('day', ts) AS _expr_0
FROM
table1
UNION
ALL
SELECT*FROM
table_0
)
SELECT
user_id,
COUNT(*) AS daily_interaction
FROM
table_1
GROUP BY
user_id
-- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org)
Expected SQL output
WITH table_0 AS (
SELECT
DATE_TRUNC('day', ts) AS interaction_day,
substr(user, length('u:') +1) AS user_id
FROM
table2
),
table_1 AS (
SELECT
substr(user, length('u:') +1) AS user_id,
DATE_TRUNC('day', ts) AS interaction_day
FROM
table1
UNION
ALL
SELECT*FROM
table_0
)
SELECT
user_id,
COUNT(*) AS daily_interaction
FROM
table_1
GROUP BY
user_id
-- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org)
The naming of the column as _expr_0 is one error. The other is that the order of the columns in the first part of table_1 before the UNION (as was pointed out in the original report).
I believe the Expected SQL Output should actually be:
WITH table_0 AS (
SELECT
DATE_TRUNC('day', ts) AS interaction_day,
substr(user, length('u:') +1) AS user_id
FROM
table2
),
table_1 AS (
SELECT
DATE_TRUNC('day', ts) AS interaction_day,
substr(user, length('u:') +1) AS user_id
FROM
table1
UNION
ALL
SELECT*FROM
table_0
)
SELECT
user_id,
COUNT(*) AS daily_interaction
FROM
table_1
GROUP BY
user_id
-- Generated by PRQL compiler version:0.9.5 (https://prql-lang.org)
from tbl1
select { y, x }
append (
from tbl2
select { y, x }
)
group { x } (
aggregate {
tot = count y
}
)
which currently transpiles as
WITH table_0 AS (
SELECT
y,
x
FROM
tbl2
),
table_1 AS (
SELECT
x,
y
FROM
tbl1
UNION
ALL
SELECT
*
FROM
table_0
)
SELECT
x,
COUNT(*) AS tot
FROM
table_1
GROUP BY
x
looking more into it, i think the ordering issue can be reproduced even without an aggregation. Consider this example:
from tbl1
select { x, y }
append (from tbl2 | select { x, y } )
select { y, x}
This pipeline sequence is not split, and the last select dominates the projection of tbl1
select y, x from tbl1 union allselect*from table_0 -- table_0 is a correct CTE
the reason i think it is related to the first issue, is that in that case there is a Union followed by Select, Aggregate which does introduce a split. Then anchor_split appends a projection transform, with columns that their order is determined by the Aggregate's partition and compute arguments. When these are not in the same order as the first select, we get a similar behavior.
all of that should be taken with a grain of salt, as this all is very speculative.
On current main (0.13.14-263-gc945ca81) both reported problems produce valid SQL.
The original query compiles to a UNION ALL whose arms each select only substr(user, length('u:') + 1) AS "user_id". interaction_day is pruned because count compiles to COUNT(*), so the arms line up and the SELECT * arm is gone.
The column-order repro (select { y, x } on both sides, then group { x }) selects x, y in the same order in both arms. With max y in place of count y, the outer query reads MAX(y), which is correct.
One cosmetic difference is left. When the s-string column is actually used (e.g. adding m = max interaction_day), the first arm still aliases it "_expr_0" while the second uses "interaction_day". UNION ALL takes names from the first arm, and the outer query reads MAX("_expr_0"), so the result is correct, just less readable.
Activity
max-sixty commentedon Sep 27, 2023
Thanks @avikam .
(For others looking — the error is
DATE_TRUNC('day', ts) AS _expr_0)snth commentedon Sep 28, 2023
The naming of the column as
_expr_0is one error. The other is that the order of the columns in the first part oftable_1before theUNION(as was pointed out in the original report).I believe the Expected SQL Output should actually be:
avikam commentedon Sep 30, 2023
yeah it might be two separate issues.
the order issue can be reproduced with:
which currently transpiles as
avikam commentedon Oct 2, 2023
looking more into it, i think the ordering issue can be reproduced even without an aggregation. Consider this example:
This pipeline sequence is not split, and the last select dominates the projection of tbl1
the reason i think it is related to the first issue, is that in that case there is a Union followed by Select, Aggregate which does introduce a split. Then
anchor_splitappends a projection transform, with columns that their order is determined by the Aggregate's partition and compute arguments. When these are not in the same order as the first select, we get a similar behavior.all of that should be taken with a grain of salt, as this all is very speculative.
prql-bot commentedon Oct 7, 2026
On current
main(0.13.14-263-gc945ca81) both reported problems produce valid SQL.UNION ALLwhose arms each select onlysubstr(user, length('u:') + 1) AS "user_id".interaction_dayis pruned becausecountcompiles toCOUNT(*), so the arms line up and theSELECT *arm is gone.select { y, x }on both sides, thengroup { x }) selectsx, yin the same order in both arms. Withmax yin place ofcount y, the outer query readsMAX(y), which is correct.One cosmetic difference is left. When the s-string column is actually used (e.g. adding
m = max interaction_day), the first arm still aliases it"_expr_0"while the second uses"interaction_day".UNION ALLtakes names from the first arm, and the outer query readsMAX("_expr_0"), so the result is correct, just less readable.Output with
m = max interaction_dayaddedThis may be ready to close, unless the
_expr_0naming should be tracked here.max-sixty commentedon Nov 28, 2026
Closing as completed: on main, the union's arms keep their columns aligned, and an aggregation after
appendreads the right names.