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
I worry that judging whether a query is non-deterministic may be not easy. Perhaps we can first leave this judgment to the user, only do this when the user specifies Materialized and there are multiple references.
DuckDB may have taken a similar approach.
v0.10.3-dev779 d26007417b
D with cte as (select random()) select* from cte union select* from cte;
┌────────────────────┐
│ random() │
│ double │
├────────────────────┤
│ 0.9430218460038304 │
│ 0.3114725165069103 │
└────────────────────┘
D with cte as materialized (select random()) select* from cte union select* from cte;
┌─────────────────────┐
│ random() │
│ double │
├─────────────────────┤
│ 0.13616445031948388 │
└─────────────────────┘
Due to parallel execution, non-deterministic behavior occurs more frequently than in traditional databases. I'm not sure if it's appropriate to disable inline CTE for all non-deterministic queries, as inline has advantages in some scenarios.
Additionally, I believe that detecting non-deterministic behavior is more difficult than in traditional databases, as it is affected by whether the input is multi-partitioned, the configuration of target_partitions, whether a Repartition node has been added, and some queries, like MIN, may be immune to unordered execution, etc.
So I think it might be a good starting point to have the user specify explicitly. When users want to avoid non-deterministic behavior or recomputation, they can explicitly request no inlining.
On defining which expressions are non-deterministic: I believe we can just use the existing concept of volatile functions. CTEs containing such expressions should not be inlined by default.
Activity
tgujar commentedon May 3, 2024
I can work on this if we can confirm in this is indeed a correctly reported bug. Let me know what you think, thanks!
jonahgao commentedon May 6, 2024
I worry that judging whether a query is non-deterministic may be not easy. Perhaps we can first leave this judgment to the user, only do this when the user specifies Materialized and there are multiple references.
DuckDB may have taken a similar approach.
tgujar commentedon May 7, 2024
I think this would push the responsibility to the user to figure out what may be non-deterministic. I am not sure if this would be a good approach
jonahgao commentedon May 8, 2024
Due to parallel execution, non-deterministic behavior occurs more frequently than in traditional databases. I'm not sure if it's appropriate to disable inline CTE for all non-deterministic queries, as inline has advantages in some scenarios.
Additionally, I believe that detecting non-deterministic behavior is more difficult than in traditional databases, as it is affected by whether the input is multi-partitioned, the configuration of target_partitions, whether a Repartition node has been added, and some queries, like MIN, may be immune to unordered execution, etc.
So I think it might be a good starting point to have the user specify explicitly. When users want to avoid non-deterministic behavior or recomputation, they can explicitly request no inlining.
neilconway commentedon Jun 5, 2026
On defining which expressions are non-deterministic: I believe we can just use the existing concept of volatile functions. CTEs containing such expressions should not be inlined by default.
fix: don't duplicate volatile expressions when pushing projection int…
fix: don't duplicate volatile expressions when pushing projection int…
[branch-54] fix: don't duplicate volatile expressions when pushing pr…
feat: support `WITH ... AS [NOT] MATERIALIZED` CTEs (apache#25580)
feat: support `WITH ... AS [NOT] MATERIALIZED` CTEs (apache#25580)
feat: support `WITH ... AS [NOT] MATERIALIZED` CTEs (apache#25580)