@@ -37,6 +37,7 @@ import org.apache.texera.amber.core.workflow.{
3737 OutputPort ,
3838 PhysicalOp ,
3939 PhysicalPlan ,
40+ PortIdentity ,
4041 WorkflowContext
4142}
4243import org .apache .texera .amber .engine .architecture .coordinator .{
@@ -48,11 +49,17 @@ import org.apache.texera.amber.engine.architecture.rpc.controlcommands.WorkflowR
4849import org .apache .texera .amber .engine .architecture .rpc .controlreturns .EmptyReturn
4950import org .apache .texera .amber .engine .architecture .rpc .coordinatorservice .CoordinatorServiceFs2Grpc
5051import org .apache .texera .amber .engine .common .client .AmberClient
52+ import org .apache .texera .amber .operator .filter .{
53+ ComparisonType ,
54+ FilterPredicate ,
55+ SpecializedFilterOpDesc
56+ }
5157import org .apache .texera .amber .operator .limit .LimitOpDesc
58+ import org .apache .texera .amber .operator .loop .{LoopEndOpDesc , LoopStartOpDesc }
5259import org .apache .texera .amber .operator .metadata .{OperatorGroupConstants , OperatorInfo }
5360import org .apache .texera .amber .operator .{LogicalOp , StateTransferFunc }
5461import org .apache .texera .amber .util .JSONUtils .objectMapper
55- import org .apache .texera .common .compiler .model .LogicalPlan
62+ import org .apache .texera .common .compiler .model .{ LogicalLink , LogicalPlan }
5663import org .apache .texera .web .model .websocket .event .TexeraWebSocketEvent
5764import org .apache .texera .web .model .websocket .request .ModifyLogicRequest
5865import org .apache .texera .web .model .websocket .response .{
@@ -470,6 +477,99 @@ class ExecutionReconfigurationServiceSpec
470477 queued.map(_._1.id) shouldBe List (alreadyQueued.id)
471478 }
472479
480+ // ---------------------------------------------------------------------------
481+ // An operator that refers to loop variables
482+ // ---------------------------------------------------------------------------
483+
484+ private def loopStartOp (): LoopStartOpDesc = {
485+ val op = new LoopStartOpDesc
486+ op.initialization = " i = 0"
487+ op.output = " table.iloc[i]"
488+ op
489+ }
490+
491+ private def loopEndOp (): LoopEndOpDesc = {
492+ val op = new LoopEndOpDesc
493+ op.update = " i += 1"
494+ op.condition = " i < len(table)"
495+ op
496+ }
497+
498+ /** A service over a workflow whose plan is LoopStart -> `deployedOp` -> LoopEnd. */
499+ private def insideLoopBlock (deployedOp : LogicalOp ): (ExecutionStateStore , WorkflowOnlyService ) = {
500+ val start = loopStartOp()
501+ val end = loopEndOp()
502+ def link (from : LogicalOp , to : LogicalOp ): LogicalLink =
503+ LogicalLink (from.operatorIdentifier, PortIdentity (), to.operatorIdentifier, PortIdentity ())
504+ val workflow = Workflow (
505+ new WorkflowContext (workflowId = testWorkflowId, executionId = testExecutionId),
506+ LogicalPlan (
507+ List (start, deployedOp, end),
508+ List (link(start, deployedOp), link(deployedOp, end))
509+ ),
510+ PhysicalPlan (Set .empty, Set .empty)
511+ )
512+ val stateStore = new ExecutionStateStore
513+ (stateStore, new WorkflowOnlyService (stateStore, workflow))
514+ }
515+
516+ private def filterOp (opId : String , value : String ): SpecializedFilterOpDesc = {
517+ val op = new SpecializedFilterOpDesc
518+ op.setOperatorId(opId)
519+ op.predicates = List (new FilterPredicate (" line" , ComparisonType .EQUAL_TO , value))
520+ op
521+ }
522+
523+ private def refused (name : String ): String =
524+ s " $name refers to loop variables, so it can't be changed while the workflow runs. "
525+
526+ " modifyOperatorLogic" should
527+ " refuse to modify an operator whose deployed setting refers to a loop variable" in {
528+ // The references the compiler left on the deployed operator, as it does inside a loop block.
529+ val deployedOp = mkLimitOp(" limit-a" , 0 )
530+ deployedOp.stateReferences = Map (" /limit" -> " n" )
531+ val (stateStore, service) = insideLoopBlock(deployedOp)
532+
533+ service.modifyOperatorLogic(ModifyLogicRequest (mkLimitOp(" limit-a" , 7 ))) shouldBe
534+ ModifyLogicResponse (" limit-a" , isValid = false , refused(" Limit" ))
535+ stateStore.reconfigurationStore.getState.unscheduledReconfigurations shouldBe empty
536+ }
537+
538+ it should " refuse an edit that makes an operator inside a loop block refer to a loop variable" in {
539+ val (typedStore, typed) = insideLoopBlock(mkLimitOp(" limit-a" , 1 ))
540+ // As the frontend sends it: "$n" in the Int property.
541+ val typedEdit = objectMapper.readValue(
542+ """ {"operatorID":"limit-a","limit":"$n","operatorType":"Limit"}""" ,
543+ classOf [LogicalOp ]
544+ )
545+ typed.modifyOperatorLogic(ModifyLogicRequest (typedEdit)) shouldBe
546+ ModifyLogicResponse (" limit-a" , isValid = false , refused(" Limit" ))
547+ typedStore.reconfigurationStore.getState.unscheduledReconfigurations shouldBe empty
548+
549+ // A whole-string "$x" refers to a loop variable inside a loop block; the compiler never saw it.
550+ val (literalStore, literal) = insideLoopBlock(filterOp(" filter-a" , " Asia" ))
551+ literal.modifyOperatorLogic(ModifyLogicRequest (filterOp(" filter-a" , " $x" ))) shouldBe
552+ ModifyLogicResponse (" filter-a" , isValid = false , refused(" Filter" ))
553+ literalStore.reconfigurationStore.getState.unscheduledReconfigurations shouldBe empty
554+ }
555+
556+ it should " still modify an operator inside a loop block that refers to no loop variable, and a '$x' literal outside every block" in {
557+ val (insideStore, inside) = insideLoopBlock(mkLimitOp(" limit-a" , 1 ))
558+ inside.modifyOperatorLogic(ModifyLogicRequest (mkLimitOp(" limit-a" , 7 ))) shouldBe
559+ ModifyLogicResponse (" limit-a" , isValid = true , " " )
560+ insideStore.reconfigurationStore.getState.unscheduledReconfigurations.map {
561+ case (op, _) => limitOf(op)
562+ } shouldBe List (7 )
563+
564+ // Outside every block "$x" is the literal it is on main.
565+ val outsideStore = new ExecutionStateStore
566+ val outside =
567+ new WorkflowOnlyService (outsideStore, mkWorkflow(List (filterOp(" filter-a" , " Asia" ))))
568+ outside.modifyOperatorLogic(ModifyLogicRequest (filterOp(" filter-a" , " $x" ))) shouldBe
569+ ModifyLogicResponse (" filter-a" , isValid = true , " " )
570+ outsideStore.reconfigurationStore.getState.unscheduledReconfigurations should have size 1
571+ }
572+
473573 " performReconfigurationOnResume" should
474574 " return without dispatching when no reconfigurations are pending" in {
475575 val stateStore = new ExecutionStateStore ()
0 commit comments