aglinxinyuan opened a new issue, #9409:
URL: https://github.com/apache/texera/issues/9409

   ### What happened?
   
   Root cause: an inner loop treats its enclosing loop's message only as a 
message to send on. The inner Loop Start and the inner Loop End both pass it 
through without merging it into the namespaces their expressions run in, the 
inner Loop End's write for the next inner iteration replaces the only document 
it arrives through, and a change an inner body operator makes to it travels 
with the enclosing loop's Loop Start id and depth.
   
   ```
   main:    inner Loop End's condition j < threshold  -> NameError: name 
'threshold' is not defined
            Filter(line = $t) in the inner body       -> fails at binding from 
the second inner iteration
            inner body UDF sets K = 3                 -> the outer Loop End 
reads K = 3
   wanted:  every inner iteration reads threshold, t and K; K = 3 is an inner 
variable, and the outer K stays 2
   ```
   
   In a nested loop, the inner Loop Start's `output` and the inner Loop End's 
`update` and `condition` cannot read a variable of the enclosing loop: they 
fail with `NameError: name 'threshold' is not defined`. From the second inner 
iteration on, operators inside the inner body cannot read it either: a Python 
UDF reading `threshold` in `loop_variables` gets a `KeyError`, and a JVM 
operator whose setting refers to `$threshold` fails at binding with "... refers 
to loop variable threshold, but no state message carried it". In the other 
direction, a body operator inside the inner loop whose `process_state` changes 
an enclosing variable changes the enclosing loop's value.
   
   The enclosing loop's message enters the inner loop only through the state 
document of the inner Loop Start's input, which in these workflows is the outer 
Loop Start's output. The inner Loop Start receives it and sends it on one loop 
deeper (`loop_counter` 1), and the inner Loop End receives it at `loop_counter` 
1, but both only send it on: the inner Loop Start does not merge it into its 
own variables, and the inner Loop End does not collect it, so the namespaces 
those expressions run in hold only the inner loop's variables. The inner Loop 
End then writes the state for the next inner iteration into that same document, 
replacing it, so the enclosing message reaches the inner loop only in the first 
inner iteration of each outer iteration; only then do the inner body's 
operators get it. A body operator that changes the enclosing message sends the 
change with the envelope the message arrived in, the enclosing loop's id at 
`loop_counter` 1, so the inner Loop End sends it on one loop out
  like the message itself, and the outer Loop End reads the changed value as 
its own.
   
   Expected: an inner loop reads its enclosing loop's variables in every inner 
iteration, in its body operators, in the inner Loop Start's initialization and 
output, and in the inner Loop End's update and condition. They are read-only 
there: assigning the name in the inner Loop Start's initialization or the inner 
Loop End's update creates an inner variable, and a body operator inside the 
inner loop whose `process_state` changes an enclosing variable makes an inner 
variable too, which the inner Loop End's update and condition read. The outer 
message and the outer loop's value stay unchanged, and the inner Loop End never 
saves an outer variable for the next inner iteration. Each inner iteration runs 
on fresh workers, so the enclosing messages have to reach the whole inner loop 
again in every inner iteration. A body operator meets the enclosing message 
before the inner Loop Start's own message, so giving its change the inner 
loop's Loop Start id needs the inner loop's id from setup conf
 ig, or buffering.
   
   Reported by Xiaozhen Liu in the review of #9366, and moved here because main 
already has the gap.
   
   ### How to reproduce?
   
   Run this workflow:
   
   ```
   Text Input ("1", "2")
     -> Loop Start (outer)   initialization: i = 0; threshold = 2    output: 
table
     -> Loop Start (inner)   initialization: j = 0                   output: 
table.iloc[j]
     -> Loop End (inner)     update: j += 1                          condition: 
j < threshold
     -> Loop End (outer)     update: i += 1                          condition: 
i < len(table)
   ```
   
   The inner Loop End fails in its condition with `NameError: name 'threshold' 
is not defined`. Changing the inner Loop Start's output to 
`table.head(threshold)` fails the same way, there. The same workflow with the 
condition `j < len(table)` runs its 2 x 2 inner iterations, as 
`LoopIntegrationSpec`'s nested case does.
   
   For the inner body, put a Filter on the enclosing variable between the inner 
boundaries:
   
   ```
   Text Input ("1", "2")
     -> Loop Start (outer)   initialization: i = 0; t = 1            output: 
table
     -> Loop Start (inner)   initialization: j = 0                   output: 
table.iloc[j]
     -> Filter               line = $t
     -> Loop End (inner)     update: j += 1                          condition: 
j < len(table)
     -> Loop End (outer)     update: i += 1                          condition: 
i < len(table)
   ```
   
   The first inner iteration runs; in the second, the Filter fails at binding 
with "property /predicates/0/value refers to loop variable t, but no state 
message carried it", and the run does not finish.
   
   For a change inside the inner body, put a Python UDF that sets the enclosing 
variable in `process_state` between the inner boundaries:
   
   ```
   Text Input ("1", "2")
     -> Loop Start (outer)   initialization: i = 0; K = 2            output: 
table
     -> Loop Start (inner)   initialization: j = 0                   output: 
table.iloc[j]
     -> Python UDF           process_state: if "K" in state: state["K"] = 3
     -> Loop End (inner)     update: j += 1                          condition: 
j < len(table)
     -> Loop End (outer)     update: i += 1                          condition: 
K == 2 and i < len(table)
   ```
   
   The outer Loop End reads `K = 3` and stops after one outer iteration, so it 
holds 2 rows. With a UDF that leaves `K` alone, the outer loop runs twice and 
the outer Loop End holds 4 rows.
   
   ### Version/Branch
   
   1.4.0-incubating-SNAPSHOT (main)
   
   ### Commit Hash (Optional)
   
   4fba9ebcfaa3fc8437ef4c2479fef3d0ebb0071b
   
   ### What browsers are you seeing the problem on?
   
   _No response_
   
   ### Relevant log output
   
   ```shell
   NameError: name 'threshold' is not defined
   property /predicates/0/value refers to loop variable t, but no state message 
carried it
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

  • [I] Inner loops cannot ... aglinxinyuan (via GitHub) via notifications

Reply via email to