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


##########
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:
   The description says this check decides every reason, for number, true/false 
and text references, from one result. Only the text reasons are decided here; 
the number and true/false reasons come from the operator and are merged in. The 
two places repeat checks, such as a value read when the operator starts or a 
value inside a text, each with its own message. I think one place should decide 
both kinds. Could they move 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]

Reply via email to