GitHub user aglinxinyuan edited a discussion: Use a loop's variables in any 
operator inside the loop: type $K in a property

> **In one sentence:** inside a Loop Start ... Loop End block, type `$i` into a 
> property of any operator, and every iteration runs that operator with the 
> loop's current `i`.

**Pull requests** (reviewed and merged separately):

| Part | Pull request | Issue |
|---|---|---|
| Engine: binds `$i` in operator properties | [#8639 feat(amber): bind $i 
loop-variable references in 
operators](https://github.com/apache/texera/pull/8639) | #8635 |
| Property panel: accepts and suggests `$i` | [#8640 feat(frontend): accept 
$variable references in loop 
blocks](https://github.com/apache/texera/pull/8640) | #8636 |
| Python operators: `self.state` in UDFs | [#8642 feat(pyamber): register the 
state before process_state](https://github.com/apache/texera/pull/8642) | #8641 
|

## How it works

```mermaid
flowchart LR
  R[Reviews] --> LS["Loop Start<br/>Initialization: min_stars = 1<br/>Output: 
table"]
  LS --> F["Filter<br/>stars >= $min_stars"]
  F --> LE["Loop End<br/>Update: min_stars += 1<br/>Condition: min_stars <= 5"]
```

Declare the variable in Loop Start, type `$min_stars` into a property of any 
operator in the loop, and run. Each iteration uses its own value, and the 
operator's code does not change.

## Screenshots

| Outside a loop (as today) | Inside a loop | Undeclared name |
|---|---|---|
| ![Number field holding 
3](https://raw.githubusercontent.com/aglinxinyuan/texera/390db38157491c992db75f25dc703bad05b4ab65/panel-before-outside-loop.png)
 | ![Text box holding $i, with $i 
suggested](https://raw.githubusercontent.com/aglinxinyuan/texera/390db38157491c992db75f25dc703bad05b4ab65/panel-after-inside-loop.png)
 | ![$foo flagged as not a variable of an enclosing 
block](https://raw.githubusercontent.com/aglinxinyuan/texera/390db38157491c992db75f25dc703bad05b4ab65/panel-after-undeclared.png)
 |

A run of `Text Input (1..6) -> Loop Start (i = 0) -> Limit ($i) -> Loop End`: 
iteration `i` lets `i` rows through, so Loop End collects 0 + 1 + ... + 5 = 15 
rows.

![Loop End result: 15 
rows](https://raw.githubusercontent.com/aglinxinyuan/texera/ca12ba98464825cb6d16ba1ba90316fac37ca6ca/loop-end-result-15-rows.png)

## In a Python UDF

```python
class ProcessTupleOperator(UDFOperatorV2):
    def process_state(self, state, port):
        self.min_stars = state["min_stars"]  # the loop's variables arrive here
        return state

    def process_tuple(self, tuple_, port):
        if tuple_["stars"] >= self.min_stars:
            yield tuple_
```

## Not supported yet

Python-generated operators (sklearn, charts), operators that also take input 
from outside the loop, properties that name a column, a literal `$...` text 
inside a loop, operators in an inner loop of nested loops, and the Form View.

## Feedback wanted

1. Is `$name` the right syntax, and how should a literal `$` be written inside 
a loop?
2. Inside a loop, true/false fields become text boxes. Would a checkbox with a 
"use a loop variable" toggle be better?
3. Which operators should come next? The sklearn K-means, with `$i` as its 
number of clusters, is the obvious one.



GitHub link: https://github.com/apache/texera/discussions/8643

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to