Iterating Without loop: — Arcs, SQL and Rhai
REAL
"The local runtime runs loop:." At CLI v6.4.1 it parses loop: into
Step.loop_config, and nothing reads that field
(git grep loop_config finds only its declaration). A loop step runs
once, as if loop: were absent, and the run ends ok with no
warning. CI checks this: examples/ch08/loop-runs-once.
The reference page docs/reference/rust_local_execution.md in
noetl/docs lists step.loop under "current DSL semantics (what local
mode must preserve)". That is a requirement, and at v6.4.1 the code
does not meet it.
Three ways to iterate locally actually work. All three are CI examples.
1. A back-edge with a when guard
Route back to an earlier step until a counter reaches its limit. The
counter travels in step vars: locally (6.4,
examples/ch06/back-edge-local; Chapter 2
uses one too). Each pass is a full step, and the guard is the only
thing that stops it.
2. Set-based SQL in one duckdb step
When the work is "do this to every item", let one query do it:
# Chapter 8.3: iterate inside ONE step with set-based SQL (local runtime, CLI 6.4.1).
# One duckdb query processes every item; no loop, no extra steps.
apiVersion: noetl.io/v2
kind: Playbook
metadata:
name: iterate_sql
workload:
cities: '["Lisbon", "Oslo", "Quito"]'
workflow:
- step: start
tool:
kind: duckdb
db: /tmp/noetl-book-ch08.duckdb
query: >-
SELECT city, length(city) AS letters
FROM (SELECT unnest(from_json(?, '["VARCHAR"]')) AS city)
ORDER BY city
params:
- "{{ workload.cities }}"
{
"executed_steps": [
"start"
],
"final_result": "[\n {\n \"city\": \"Lisbon\",\n \"letters\": 6\n },\n {\n \"city\": \"Oslo\",\n \"letters\": 4\n },\n {\n \"city\": \"Quito\",\n \"letters\": 5\n }\n]",
"playbook_name": "iterate_sql",
"status": "ok",
"step_results": {
"start": "[\n {\n \"city\": \"Lisbon\",\n \"letters\": 6\n },\n {\n \"city\": \"Oslo\",\n \"letters\": 4\n },\n {\n \"city\": \"Quito\",\n \"letters\": 5\n }\n]"
}
}
This is usually the best choice: one step, no orchestration per item,
and the database does the iteration. The list goes in as a bound
parameter and unnest(from_json(…)) turns it into rows.
3. A loop inside one rhai step
# Chapter 8.3: iterate inside ONE step with a Rhai loop (local runtime, CLI 6.4.1).
apiVersion: noetl.io/v2
kind: Playbook
metadata:
name: iterate_rhai
workflow:
- step: start
tool:
kind: rhai
code: |
let out = [];
for city in ["Lisbon", "Oslo", "Quito"] {
out.push(city + ":" + city.len());
}
out
{
"executed_steps": [
"start"
],
"final_result": "[\"Lisbon:6\",\"Oslo:4\",\"Quito:5\"]",
"playbook_name": "iterate_rhai",
"status": "ok",
"step_results": {
"start": "[\"Lisbon:6\",\"Oslo:4\",\"Quito:5\"]"
}
}
Rhai has for, while and arrays. Use it for small in-memory
transformations. Rhai has no default timeout and no operation limits
(Appendix B), so keep the loop bounded.
Which to use
| Need | Use |
|---|---|
| A per-item side effect (an HTTP call per item) | a back-edge |
| Transform or aggregate a collection | set-based SQL |
| A small in-memory computation | Rhai |
| The same playbook must also run on a server | write loop: for the server and test it there. Locally it runs once. |