Xiao-zhen-Liu commented on code in PR #8642:
URL: https://github.com/apache/texera/pull/8642#discussion_r4520373878


##########
amber/src/main/python/core/models/operator.py:
##########
@@ -92,6 +102,54 @@ def decode_python_template(self, data: Union[str, bytes]) 
-> str:
 
     __internal_is_source: bool = False
 
+    # The loop variables received so far, by name, and the nesting depth of
+    # the message each value came in; see ``register_state``. ``None`` until
+    # the first state message. Class-level defaults rather than ``__init__``
+    # assignments: a subclass ``__init__`` that skips ``super()`` still reads
+    # no loop variables until a state arrives.
+    __internal_loop_variables: Optional[MutableMapping[str, Any]] = None
+    __internal_loop_variable_depths: Optional[MutableMapping[str, int]] = None
+
+    @property
+    @overrides.final
+    def loop_variables(self) -> Mapping[str, Any]:
+        """
+        The loop variables received from upstream, read-only: inside a control
+        block, the values the state messages handed to this operator carried,
+        by name. The runtime registers each message (``register_state``) right
+        before ``process_state`` runs, so the callback and every call after it
+        can read them. Empty until a state message arrives.
+        """
+        return MappingProxyType(self.__internal_loop_variables or {})
+
+    @overrides.final
+    def register_state(self, state: State, loop_counter: int = 0) -> None:
+        """
+        Merge the values ``state`` carries into ``loop_variables``. The runtime
+        calls this for every state message, with the nesting depth its envelope
+        carries (``loop_counter``): 0 from the innermost loop around this
+        operator, 1 from the loop around that, and so on. A state no loop sent,
+        such as one an operator returns from ``produce_state_on_finish``,
+        travels at depth 0.
+
+        For each name, the value from the smallest depth wins, and among the
+        values from that depth, the last one received. So an inner loop's
+        variable shadows an enclosing one of the same name, even when the
+        enclosing message arrives later, and a state replaces only the names it
+        carries.
+
+        :param state: State, the state message that arrived.
+        :param loop_counter: int, the message's nesting depth.
+        """
+        if self.__internal_loop_variables is None:
+            self.__internal_loop_variables = {}
+            self.__internal_loop_variable_depths = {}
+        for name, value in state.items():
+            depth = self.__internal_loop_variable_depths.get(name)
+            if depth is None or loop_counter <= depth:

Review Comment:
   Inside a loop, if one Python UDF returns a state with `i` = 99 from 
`produce_state_on_finish`, a second Python UDF after it reads 99 as the loop's 
`i`, with no error. If I read it right, a Filter with `$i` in the second UDF's 
place fails with "loop variable i got two different values in one iteration, 0 
and 99: a loop's variables must not change inside its body". I think a Python 
UDF should get the same error.



-- 
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]

Reply via email to