The GitHub Actions job "Running Copilot Code Review" on texera.git/main has 
succeeded.
Run started by GitHub user Copilot (triggered by Copilot).

Head commit for run:
4fba9ebcfaa3fc8437ef4c2479fef3d0ebb0071b / Xinyuan Lin <[email protected]>
feat(pyamber): register the state before process_state (#8642)

### What changes were proposed in this PR?

Inside a control block (Loop Start ... Loop End), the loop variables
reach every operator as state messages. Until now a Python operator saw
each message only as the argument of `process_state`. With this PR it
can read the loop variables at any point, in `process_tuple` or
`process_table` too, through `self.loop_variables`.

- `Operator.loop_variables` is a read-only mapping of the loop variables
the operator received from upstream, by name. It is empty until the
first state message arrives.
- The worker calls `Operator.register_state(state, loop_counter)` for
every state message, right before `process_state`: in
`DataProcessor.process_state`, and in the Loop End's deferred consume in
`MainLoop`, which calls the operator directly. `loop_counter` is the
nesting depth on the message's envelope: 0 from the innermost loop
around the operator, 1 from the loop around that, and so on.

For each name, the value from the smallest depth wins, so an inner
loop's variable shadows an outer one of the same name, even when another
copy of the outer message arrives later, and a message adds only the
names it carries. A message from the same depth may repeat a value, as
copies from several workers or branches do, but not change it: reading a
name that got two different values from one depth fails with the error a
JVM operator whose setting refers to it gets (`StateReferenceBinding`),
and the other names still read:

```
state message                        depth   self.loop_variables
{i: 1, n: 10}  from the outer loop     1     {i: 1, n: 10}
{i: 5}         from the inner loop     0     {i: 5, n: 10}
{i: 1}         from the outer loop     1     {i: 5, n: 10}              the 
inner i still wins
{note: "x"}    from a body UDF         0     {i: 5, n: 10, note: "x"}   adds 
its own name only
{i: 9}         from a body UDF         0     reading i fails: "loop variable i 
got two different values in one iteration, 5 and 9"
```

A state no loop sent, such as one a body operator returns from
`produce_state_on_finish`, travels at depth 0. It adds only the names it
carries, so a UDF behind a stateful UDF still reads the loop's `i`; a
state that carries `i` with another value makes reading `i` fail, as
above.

`process_state` itself is unchanged: the default still forwards the
message. Loop Start and Loop End keep their own variables in
`self.state`, as on `main`.

### Any related issues, documentation, discussions?

Closes #8641.

### How was this PR tested?

New tests at three levels; each new unit test was run red before its
change.

- Operator (`test_operator.py`): `loop_variables` is empty until a state
is registered; reading a name that got two different values from one
depth raises the JVM's error, while equal copies read, the other names
read and the name stays listed; an inner value wins over an outer one
that arrives later, at any depth, also over two different outer values;
an outer variable the inner loop does not name stays visible; a state
that names no loop variable leaves them intact; the mapping is read-only
and per instance, also when a subclass `__init__` skips `super()`; the
registered message is left unchanged.
- Worker (`test_data_processor.py`, `test_main_loop.py`): the state is
registered before `process_state` runs, with the depth from its
envelope, and in the Loop End's deferred consume too; an operator still
reads `i` in `process_tuple` after another operator's state arrives, and
fails with that error when the state carries `i` with another value,
also through the real `MainLoop` and `DataProcessor` threads; Loop
Start's first-entry merge and Loop End's back edge still use their own
`state`.
- End to end (`LoopIntegrationSpec`), `Text Input (a, b, c) -> Loop
Start (i = 0, table.iloc[i]) -> ... -> Loop End (i += 1 while i <
len(table))`: a Python UDF that writes `self.loop_variables["i"]` into
each row in `process_tuple` produces `(a, 0), (b, 1), (c, 2)`, and so
does one that reads it in `process_table` behind a UDF that returns its
own state from `produce_state_on_finish`. That second case fails with
`KeyError: 'i'` when `loop_variables` holds only the latest message.

```
cd amber && pytest src/test/python/core/models src/test/python/core/runnables \
  src/test/python/core/architecture/managers/test_state_processing_manager.py
# 475 passed, 9 skipped, 1 xfailed; on Windows only, test_tuple.py's test_hash 
fails, as on main
ruff check src/main/python src/test/python && ruff format --check 
src/main/python src/test/python
# clean
sbt "WorkflowExecutionService/testOnly 
org.apache.texera.amber.engine.e2e.LoopIntegrationSpec -- -z UDF"
# 2 passed
sbt "WorkflowExecutionService/scalafmtCheckAll" 
"WorkflowExecutionService/scalafixAll --check"
# clean
```

### Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Fable 5.1, Claude Opus 5.5)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Report URL: https://github.com/apache/texera/actions/runs/42750689539

With regards,
GitHub Actions via GitBox

Reply via email to