Loop
experimental
Iteratively applies step
function to initial
relation until the step
returns an empty table. Returns a relation that contains rows of initial relation and all intermediate relations.
This behavior could be expressed with following pseudo-code:
def loop(step, initial):
result = []
current = initial
while current is not empty:
result = append(result, current)
current = step(current)
return result
Examples
PRQL
from_text format:json '[{"n": 1 }]'
loop (
filter n<4
select n = n+1
)
# returns [1, 2, 3, 4]
SQL
WITH table_0 AS (
SELECT
1 AS n
),
table_4 AS (
WITH RECURSIVE loop AS (
SELECT
n
FROM
table_0 AS table_1
UNION
ALL
SELECT
n + 1
FROM
loop AS table_2
WHERE
n < 4
)
SELECT
*
FROM
loop
)
SELECT
n
FROM
table_4 AS table_3
Note
Behavior of WITH RECURSIVE may depend on database configuration (MySQL). prql-compiler assumes behavior described by Postgres documentation and will not produce correct results for alternative configurations of MySQL.
Note
Currently, loop
may produce references to the recursive CTE in sub-queries, which is not supported by some database engines (SQLite). For now, we suggest you keep step functions simple enough to fit into a single SELECT statement.