aglinxinyuan commented on code in PR #8642:
URL: https://github.com/apache/texera/pull/8642#discussion_r4520444282
##########
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:
Done in 269d93d59b — a UDF that reads a loop variable two states from the
same loop disagree on now gets the JVM's error (`loop variable i got two
different values in one iteration, 0 and 99: ...`), while equal copies, an
inner loop's value and the other names still read. Once #9366 gives control
messages ids, both engines can switch to the later value together.
--
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]