aglinxinyuan commented on code in PR #9359:
URL: https://github.com/apache/texera/pull/9359#discussion_r4569747123


##########
common/workflow-compiler/src/main/scala/org/apache/texera/common/compiler/WorkflowCompiler.scala:
##########
@@ -167,20 +177,142 @@ object WorkflowCompiler {
     * A reference is bound in the executor that `ExecFactory` builds from the 
descriptor's JSON: the
     * state message writes the loop variable into the descriptor that executor 
parsed. An operator
     * whose executor runs code instead -- a UDF, or a descriptor that 
generates Python from its
-    * properties, such as the sklearn operators -- never takes that path: its 
code is built from the
-    * placeholders before the loop runs, and nothing binds them.
+    * properties, such as the sklearn operators -- never takes that path: its 
code is built before
+    * the loop runs. A UDF's code and properties are the user's, and nothing 
binds them.
+    *
+    * A generated operator's references are all decided here, from `binding`, 
its
+    * `loopVariableBinding`: the code generated once, which its physical 
operator runs. A numeric
+    * or boolean property's value is read from the loop state where the 
descriptor found it written
+    * into its code; the binding says why when it cannot be (`unbound`), or 
when the value decides
+    * the output schemas (`unboundBySchema`). A String property's `$name` is 
bound only where pyb's
+    * decoder rendered it, which `PythonOperatorDescriptor` turned into a read 
of the loop state,
+    * `loopVariableLookup(name)`, and then only if the code holds the text 
`$name` nowhere (where it
+    * does, the code pastes it in as it is) and no output column the operator 
adds holds the text
+    * (its schema is computed from the literal), and the code reads it nowhere 
when the operator
+    * starts (in `open` or `__init__`, 
`PythonOperatorDescriptor.runsAtStartup`), before the
+    * iteration's state can arrive; the binding tells the same of a numeric or 
boolean value. Nor
+    * may a copy of the operator with a column of an input or another of its 
texts in the
+    * reference's place show it checking the text, or computing its output 
schemas from it, both of
+    * which it does from the literal too 
(`LoopVariableBinding.textsReadBeforeTheLoop`, on the copy
+    * the code was generated from, asked only once every input has a schema). 
The texts are judged
+    * once every numeric or boolean reference is bound: until then the code 
holds a probe value in
+    * its place, which may decide what the code reads.
+    * When the generation failed (with every probe value and with the 
properties' defaults, where
+    * a numeric or boolean property refers to a loop variable) there is no 
code to read either
+    * way, and the code-generation check reports why it failed instead.
+    *
+    * @param physicalOps the operator's own physical operators, their schemas 
propagated.
+    * @param binding the operator's `loopVariableBinding`, if it generates its 
code.
     */
-  def unbindableStateReferences(logicalOp: LogicalOp, subPlan: PhysicalPlan): 
Option[Throwable] =
-    Option.when(
-      logicalOp.stateReferences.nonEmpty &&
-        subPlan.operators.exists(_.opExecInitInfo.isInstanceOf[OpExecWithCode])
-    ) {
-      new UnsupportedOperationException(
-        s"${logicalOp.operatorInfo.userFriendlyName} cannot refer to loop 
variables yet " +
-          s"(${formatReferences(logicalOp.stateReferences)}): its code is 
generated before " +
-          "the loop runs"
-      )
+  def unbindableStateReferences(
+      logicalOp: LogicalOp,
+      physicalOps: Iterable[PhysicalOp],
+      binding: Option[LoopVariableBinding]
+  ): Option[Throwable] = {
+    val codes = physicalOps.toSeq.map(_.opExecInitInfo).collect {
+      case OpExecWithCode(code, language) => (code, language)
     }
+    if (logicalOp.stateReferences.isEmpty || codes.isEmpty) {
+      None
+    } else {
+      val unbound: Seq[(Iterable[String], String)] = binding match {
+        case Some(generated) if codes.forall(_._2 == "python") =>
+          val tree = objectMapper.valueToTree[ObjectNode](logicalOp)
+          val text = textReferences(tree, logicalOp.stateReferences)
+          val scripts = codes.map(_._1)
+          val added = addedColumns(physicalOps)
+          def whyUnbound(name: String): Option[String] = {
+            val lookup = loopVariableLookup(name)
+            if (scripts.exists(_.contains("$" + name))) Some(pastedIntoCode)
+            else if (!scripts.exists(_.contains(lookup))) Some(unreadByCode)
+            else if (scripts.exists(code => offsetsOf(code, 
lookup).exists(runsAtStartup(code, _))))
+              Some(readAtStartup)
+            else if (added.exists(_.contains("$" + name))) 
Some(namesOutputColumn)
+            else None
+          }
+          // As the schema propagation calls it: only once every input has a 
schema.
+          val inputSchemas = inputSchemasOf(physicalOps)
+          val bySchema = inputSchemas.fold(Map.empty[String, 
String])(generated.unboundBySchema)
+          val typedUnbound = generated.unbound ++ (bySchema -- 
generated.unbound.keys)

Review Comment:
   Moved them together in e33b951a2a: the probing and every reason this check 
reports for a generated operator, for number, true/false and text references 
alike, now live in `LoopVariableBinding.scala`, and for such an operator 
`unbindableStateReferences` only reports what `LoopVariableBinding.reasons` 
returns; the base only calls `LoopVariableBinding.of(this)` and builds its 
physical operator from the binding (773 → 134 lines). Both kinds now go through 
one startup check, `readsAtStartup`, and one builder for each pair of messages 
that differ only by "text" or "value", with every message unchanged; a value 
inside a text and a text the code embeds stay two checks, since one looks for 
where a probe value lands inside a string, a comment, a name or a longer 
number, and the other for a `$name` the code keeps as it is.
   



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