aglinxinyuan commented on code in PR #4206: URL: https://github.com/apache/texera/pull/4206#discussion_r3353092814
########## common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/loop/LoopStartOpDesc.scala: ########## @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.amber.operator.loop + +import com.fasterxml.jackson.annotation.JsonProperty +import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaTitle +import org.apache.texera.amber.core.executor.OpExecWithCode +import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} +import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PhysicalOp} +import org.apache.texera.amber.operator.LogicalOp +import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo} + +class LoopStartOpDesc extends LogicalOp { + @JsonProperty(required = true, defaultValue = "i = 0") + @JsonSchemaTitle("Initialization") + var initialization: String = _ + + @JsonProperty(required = true, defaultValue = "table.iloc[i]") + @JsonSchemaTitle("Output") + var output: String = _ + + override def getPhysicalOp( + workflowId: WorkflowIdentity, + executionId: ExecutionIdentity + ): PhysicalOp = + PhysicalOp + .oneToOnePhysicalOp( + workflowId, + executionId, + operatorIdentifier, + OpExecWithCode(generatePythonCode(), "python") + ) + .withInputPorts(operatorInfo.inputPorts) + .withOutputPorts(operatorInfo.outputPorts) + .withSuggestedWorkerNum(1) + .withParallelizable(false) + + override def operatorInfo: OperatorInfo = + OperatorInfo( + "Loop Start", + "Begin a loop that iterates over rows of the input table; pairs with Loop End.", + OperatorGroupConstants.CONTROL_GROUP, + inputPorts = List(InputPort()), + outputPorts = List(OutputPort()) + ) + + def generatePythonCode(): String = { + s""" + |from pytexera import * + |class ProcessLoopStartOperator(LoopStartOperator): + | @overrides + | def open(self): + | self.state = {"loop_counter": 0} + | exec("$initialization", {}, self.state) + | + | @overrides Review Comment: Fixed in 9ec60f07cc. Switched the four user fields (`initialization`, `output`, `update`, `condition`) to `EncodableString` and rewrote both `generatePythonCode()` with the `pyb"..."` macro from `common/pybuilder`. The macro base64-encodes each splice at build time and emits a `self.decode_python_template('<b64>')` call at runtime — the raw user text never enters the generated source, so quotes/newlines/backslashes/etc. can't break it. It also rejects (at compile time) any splice placed inside a quoted string, so the old `"$initialization"` pattern can't sneak back in. The surrounding `"..."` around the `exec(...)` arguments is gone because the decoder already returns a Python `str`; for the `output = ` / `condition` branches the literal prefix stays inline (`exec("output = " + $output, ...)`), so the runtime concatenates the literal with the decoded expression. Added `LoopOpDescsSpec` (new — there was no spec for the loop OpDescs) covering both: subclassing of `LoopStart/EndOperator`, the four `exec` call sites use `self.decode_python_template`, a tricky value with `"`, `'`, `\n`, `\` does NOT appear verbatim in the generated source, and empty-default fields still produce a parseable template. 10 tests, all passing. Diff scoped to the two OpDescs + the new spec; no unrelated churn. -- 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]
