This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-9415-45626ef1c24c7d42d8c26a6fa8fe8c718f12b998 in repository https://gitbox.apache.org/repos/asf/texera.git
commit e6e621796b3e99add765bc82510aa363aebf8c05 Author: Xinyuan Lin <[email protected]> AuthorDate: Sun Nov 15 13:26:43 2026 +0000 test(workflow-operator): share fixtures across UDF specs (#9415) ### What changes were proposed in this PR? The six UDF descriptor specs (Java, R, R source, Python, dual-input Python and Python source) repeated the same setup and assertions. A new test trait, `UdfDescriptorSpecSupport`, holds them once: physical-op construction, operator-info checks, polymorphic JSON round trips, worker-count rejection, schema fixtures, port descriptions and UI-parameter builders. This removes 182 net lines. Each spec keeps its own language, executor code, port and value expectations. Every existing case keeps its name, order and outcome, including the Python default-code template cases and the Java UI-parameter round trip. ### Any related issues, documentation, discussions? Closes #9414. ### How was this PR tested? With JDK 17: ```sh sbt "WorkflowOperator/testOnly org.apache.texera.amber.operator.udf.java.JavaUDFOpDescSpec org.apache.texera.amber.operator.udf.python.DualInputPortsPythonUDFOpDescV2Spec org.apache.texera.amber.operator.udf.python.PythonUDFOpDescV2Spec org.apache.texera.amber.operator.udf.python.source.PythonUDFSourceOpDescV2Spec org.apache.texera.amber.operator.udf.r.RUDFOpDescSpec org.apache.texera.amber.operator.udf.r.RUDFSourceOpDescSpec" sbt "WorkflowOperator/Test/scalafmtCheck" "WorkflowOperator/Test/scalafix --check" ``` The six suites run 78 cases, all passing, the same count as on main. Formatting and lint checks pass. ### Was this PR authored or co-authored using generative AI tooling? Generated-by: Antigravity (Gemini), Claude Opus 5.5 --- .../operator/udf/UdfDescriptorSpecSupport.scala | 135 ++++++++++++++++++ .../operator/udf/java/JavaUDFOpDescSpec.scala | 86 +++-------- .../DualInputPortsPythonUDFOpDescV2Spec.scala | 108 ++++---------- .../udf/python/PythonUDFOpDescV2Spec.scala | 158 +++++---------------- .../source/PythonUDFSourceOpDescV2Spec.scala | 90 ++++-------- .../amber/operator/udf/r/RUDFOpDescSpec.scala | 97 +++---------- .../operator/udf/r/RUDFSourceOpDescSpec.scala | 42 ++---- 7 files changed, 267 insertions(+), 449 deletions(-) diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/UdfDescriptorSpecSupport.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/UdfDescriptorSpecSupport.scala new file mode 100644 index 0000000000..ba5e3dff48 --- /dev/null +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/UdfDescriptorSpecSupport.scala @@ -0,0 +1,135 @@ +/* + * 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.udf + +import org.apache.texera.amber.core.executor.OpExecWithCode +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} +import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} +import org.apache.texera.amber.core.workflow.{PhysicalOp, PortIdentity, UnknownPartition} +import org.apache.texera.amber.operator.{LogicalOp, PortDescription} +import org.apache.texera.amber.operator.metadata.OperatorInfo +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.matchers.should.Matchers + +import scala.reflect.ClassTag + +private[udf] trait UdfDescriptorSpecSupport extends Matchers { + protected val workflowId = WorkflowIdentity(1L) + protected val executionId = ExecutionIdentity(1L) + + protected final def buildPhysicalOp(desc: LogicalOp): PhysicalOp = + desc.getPhysicalOp(workflowId, executionId) + + protected final def expectOperatorInfo( + desc: LogicalOp, + name: String, + group: String, + inputs: Int + ): OperatorInfo = { + val info = desc.operatorInfo + info.userFriendlyName shouldBe name + info.operatorGroupName shouldBe group + info.inputPorts should have length inputs + info.outputPorts should have length 1 + info + } + + protected final def roundTrip[D <: LogicalOp: ClassTag](desc: D): D = { + val restored = objectMapper.readValue(objectMapper.writeValueAsString(desc), classOf[LogicalOp]) + restored shouldBe a[D] + restored.asInstanceOf[D] + } + + protected final def expectInvalidWorkerCount[D <: LogicalOp](desc: D)( + configure: D => Unit + ): Unit = { + configure(desc) + intercept[IllegalArgumentException] { buildPhysicalOp(desc) } + } + + protected final def resultColumn: Attribute = new Attribute("res", AttributeType.INTEGER) + + protected final def inputSchema: Schema = + Schema().add(new Attribute("in", AttributeType.STRING)) + + protected final def expectOutputColumns(desc: LogicalOp, retainInput: Boolean): Unit = + expectOutputColumns(desc, retainInput, Map(desc.operatorInfo.inputPorts.head.id -> inputSchema)) + + protected final def expectOutputColumns( + desc: LogicalOp, + retainInput: Boolean, + inputs: Map[PortIdentity, Schema] + ): Unit = { + val expected = (if (retainInput) inputSchema else Schema()).add(resultColumn) + desc.getExternalOutputSchemas(inputs) shouldBe Map( + desc.operatorInfo.outputPorts.head.id -> expected + ) + } + + protected final def portDescription( + id: String, + name: String, + disallowMultiInputs: Boolean = false, + dependencies: List[Int] = Nil + ): PortDescription = + PortDescription( + id, + name, + disallowMultiInputs, + isDynamicPort = false, + UnknownPartition(), + dependencies + ) + + protected val tupleUdfCode = + """from pytexera import * + | + |class ProcessTupleOperator(UDFOperatorV2): + | def process_tuple(self, tuple_, port): + | yield tuple_ + |""".stripMargin + + protected final def uiParameter(name: String, value: String): UiUDFParameter = + parameter(name, value, AttributeType.INTEGER, "") + + protected final def datasetParameter(name: String, value: String): UiUDFParameter = + parameter(name, value, AttributeType.STRING, "dataset") + + private def parameter( + name: String, + value: String, + attributeType: AttributeType, + inputType: String + ): UiUDFParameter = { + val parameter = new UiUDFParameter + parameter.attribute = new Attribute(name, attributeType) + parameter.inputType = inputType + parameter.value = value + parameter + } + + protected final def expectInjectedPython(desc: LogicalOp): Unit = + buildPhysicalOp(desc).opExecInitInfo match { + case OpExecWithCode(code, "python") => + code should include("def _texera_injected_ui_parameters") + code should include("self.decode_python_template") + case other => fail(s"expected Python OpExecWithCode, got $other") + } +} diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/java/JavaUDFOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/java/JavaUDFOpDescSpec.scala index 8c9632c057..16465af45d 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/java/JavaUDFOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/java/JavaUDFOpDescSpec.scala @@ -20,28 +20,16 @@ package org.apache.texera.amber.operator.udf.java import org.apache.texera.amber.core.executor.OpExecWithCode -import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} -import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} import org.apache.texera.amber.core.workflow.UnknownPartition -import org.apache.texera.amber.operator.udf.UiUDFParameter -import org.apache.texera.amber.operator.{LogicalOp, PortDescription} import org.apache.texera.amber.operator.metadata.OperatorGroupConstants -import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.amber.operator.udf.UdfDescriptorSpecSupport import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers -class JavaUDFOpDescSpec extends AnyFlatSpec with Matchers { - - private val workflowId = WorkflowIdentity(1L) - private val executionId = ExecutionIdentity(1L) +class JavaUDFOpDescSpec extends AnyFlatSpec with UdfDescriptorSpecSupport { "JavaUDFOpDesc.operatorInfo" should "advertise the name, Java group, and a default 1-in/1-out shape" in { - val info = (new JavaUDFOpDesc).operatorInfo - info.userFriendlyName shouldBe "Java UDF" - info.operatorGroupName shouldBe OperatorGroupConstants.JAVA_GROUP - info.inputPorts should have length 1 - info.outputPorts should have length 1 + expectOperatorInfo(new JavaUDFOpDesc, "Java UDF", OperatorGroupConstants.JAVA_GROUP, 1) } "JavaUDFOpDesc" should "default code to empty, workers to 1, retainInputColumns to false" in { @@ -55,59 +43,38 @@ class JavaUDFOpDescSpec extends AnyFlatSpec with Matchers { "wire OpExecWithCode(code, \"java\") and carry port identities" in { val d = new JavaUDFOpDesc d.code = "return t;" - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "java" - code shouldBe "return t;" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("return t;", "java") physical.inputPorts.keySet shouldBe d.operatorInfo.inputPorts.map(_.id).toSet physical.outputPorts.keySet shouldBe d.operatorInfo.outputPorts.map(_.id).toSet } it should "reject a non-positive worker count" in { - val d = new JavaUDFOpDesc - d.workers = 0 - intercept[IllegalArgumentException] { d.getPhysicalOp(workflowId, executionId) } + expectInvalidWorkerCount(new JavaUDFOpDesc)(_.workers = 0) } "JavaUDFOpDesc schema propagation" should "emit only the output columns when input columns are not retained (default)" in { val d = new JavaUDFOpDesc - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val input = Schema().add(new Attribute("in", AttributeType.STRING)) - d.getExternalOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> input)) shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema().add( - new Attribute("res", AttributeType.INTEGER) - ) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = false) } it should "retain input columns plus the output columns when retainInputColumns is true" in { val d = new JavaUDFOpDesc d.retainInputColumns = true - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val input = Schema().add(new Attribute("in", AttributeType.STRING)) - d.getExternalOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> input)) shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema() - .add(new Attribute("in", AttributeType.STRING)) - .add(new Attribute("res", AttributeType.INTEGER)) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = true) } "JavaUDFOpDesc" should "round-trip its config fields through the polymorphic base" in { val d = new JavaUDFOpDesc d.code = "x" - val p = new UiUDFParameter - p.attribute = new Attribute("count", AttributeType.INTEGER) - p.value = "42" + val p = uiParameter("count", "42") d.uiParameters = List(p) d.workers = 4 d.retainInputColumns = true - val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp]) - restored shouldBe a[JavaUDFOpDesc] - val j = restored.asInstanceOf[JavaUDFOpDesc] + val j = roundTrip(d) j.code shouldBe "x" j.uiParameters.head.attribute shouldBe p.attribute j.uiParameters.head.value shouldBe "42" @@ -120,36 +87,24 @@ class JavaUDFOpDescSpec extends AnyFlatSpec with Matchers { val d = new JavaUDFOpDesc d.code = "return t;" d.workers = 2 - val physical = d.getPhysicalOp(workflowId, executionId) + val physical = buildPhysicalOp(d) physical.parallelizable shouldBe true physical.isOneToManyOp shouldBe true physical.suggestedWorkerNum shouldBe Some(2) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - code shouldBe "return t;" - language shouldBe "java" - case other => fail(s"expected OpExecWithCode, got $other") - } + physical.opExecInitInfo shouldBe OpExecWithCode("return t;", "java") } "JavaUDFOpDesc.operatorInfo" should "derive input ports from a configured inputPorts list" in { val d = new JavaUDFOpDesc d.inputPorts = List( - PortDescription( - portID = "0", - displayName = "left", - disallowMultiInputs = true, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("0", "left", disallowMultiInputs = true) ) val info = d.operatorInfo info.inputPorts should have length 1 info.inputPorts.head.displayName shouldBe "left" // also drives the getPhysicalOp inputPorts != null partitionRequirement branch - val physical = d.getPhysicalOp(workflowId, executionId) + val physical = buildPhysicalOp(d) physical.inputPorts.keySet shouldBe info.inputPorts.map(_.id).toSet physical.partitionRequirement shouldBe List(Some(UnknownPartition())) } @@ -157,14 +112,7 @@ class JavaUDFOpDescSpec extends AnyFlatSpec with Matchers { it should "derive output ports from a configured outputPorts list" in { val d = new JavaUDFOpDesc d.outputPorts = List( - PortDescription( - portID = "0", - displayName = "result", - disallowMultiInputs = false, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("0", "result") ) val info = d.operatorInfo info.outputPorts should have length 1 diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/DualInputPortsPythonUDFOpDescV2Spec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/DualInputPortsPythonUDFOpDescV2Spec.scala index b360ec3934..f539855a80 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/DualInputPortsPythonUDFOpDescV2Spec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/DualInputPortsPythonUDFOpDescV2Spec.scala @@ -19,29 +19,14 @@ package org.apache.texera.amber.operator.udf.python -import org.apache.texera.amber.operator.udf.UiUDFParameter - import org.apache.texera.amber.core.executor.OpExecWithCode import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} -import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} import org.apache.texera.amber.core.workflow.{PortIdentity, UnknownPartition} -import org.apache.texera.amber.operator.LogicalOp import org.apache.texera.amber.operator.metadata.OperatorGroupConstants -import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.amber.operator.udf.UdfDescriptorSpecSupport import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers - -class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { - private val workflowId = WorkflowIdentity(1L) - private val executionId = ExecutionIdentity(1L) - - private def uiParameter(name: String, value: String): UiUDFParameter = { - val parameter = new UiUDFParameter - parameter.attribute = new Attribute(name, AttributeType.INTEGER) - parameter.value = value - parameter - } +class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with UdfDescriptorSpecSupport { private def twoPortSchemas(dataSchema: Schema): Map[PortIdentity, Schema] = Map( @@ -51,35 +36,27 @@ class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { "DualInputPortsPythonUDFOpDescV2.operatorInfo" should "advertise the 2-in Python UDF with a model port and a dependent tuples port" in { - val info = (new DualInputPortsPythonUDFOpDescV2).operatorInfo - info.userFriendlyName shouldBe "2-in Python UDF" - info.operatorGroupName shouldBe OperatorGroupConstants.PYTHON_GROUP - info.inputPorts should have length 2 + val info = expectOperatorInfo( + new DualInputPortsPythonUDFOpDescV2, + "2-in Python UDF", + OperatorGroupConstants.PYTHON_GROUP, + 2 + ) info.inputPorts.head.displayName shouldBe "model" info.inputPorts(1).displayName shouldBe "tuples" info.inputPorts(1).id shouldBe PortIdentity(1) info.inputPorts(1).dependencies shouldBe List(PortIdentity(0)) - info.outputPorts should have length 1 } "DualInputPortsPythonUDFOpDescV2.getPhysicalOp" should "reject a non-positive worker count" in { - val d = new DualInputPortsPythonUDFOpDescV2 - d.workers = 0 - intercept[IllegalArgumentException] { - d.getPhysicalOp(workflowId, executionId) - } + expectInvalidWorkerCount(new DualInputPortsPythonUDFOpDescV2)(_.workers = 0) } it should "wire a single-worker op with the default environment and unknown partition" in { val d = new DualInputPortsPythonUDFOpDescV2 d.code = "yield t" - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "python" - code shouldBe "yield t" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("yield t", "python") physical.parallelizable shouldBe false physical.suggestedWorkerNum shouldBe None physical.pveName shouldBe "" @@ -91,44 +68,25 @@ class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { it should "wire a parallelizable op with the suggested worker count when workers > 1" in { val d = new DualInputPortsPythonUDFOpDescV2 d.workers = 2 - val physical = d.getPhysicalOp(workflowId, executionId) + val physical = buildPhysicalOp(d) physical.parallelizable shouldBe true physical.suggestedWorkerNum shouldBe Some(2) } it should "inject configured UI parameters into the generated Python code" in { val d = new DualInputPortsPythonUDFOpDescV2 - d.code = """from pytexera import * - | - |class ProcessTupleOperator(UDFOperatorV2): - | def process_tuple(self, tuple_, port): - | yield tuple_ - |""".stripMargin + d.code = tupleUdfCode d.uiParameters = List(uiParameter("count", "7")) - d.getPhysicalOp(workflowId, executionId).opExecInitInfo match { - case OpExecWithCode(code, "python") => - code should include("def _texera_injected_ui_parameters") - code should include("self.decode_python_template") - case other => fail(s"expected Python OpExecWithCode, got $other") - } + expectInjectedPython(d) } it should "leave a resource parameter to the execution" in { val d = new DualInputPortsPythonUDFOpDescV2 - d.code = """from pytexera import * - | - |class ProcessTupleOperator(UDFOperatorV2): - | def process_tuple(self, tuple_, port): - | yield tuple_ - |""".stripMargin - val resource = new UiUDFParameter - resource.attribute = new Attribute("DS", AttributeType.STRING) - resource.inputType = "dataset" - resource.value = "/dataset/[email protected]/ds/v1" - d.uiParameters = List(resource) - - d.getPhysicalOp(workflowId, executionId).executionTimeBinding should not be empty + d.code = tupleUdfCode + d.uiParameters = List(datasetParameter("DS", "/dataset/[email protected]/ds/v1")) + + buildPhysicalOp(d).executionTimeBinding should not be empty } it should "reject a blank virtual-environment name when the default env is disabled" in { @@ -136,7 +94,7 @@ class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { d.defaultEnv = false d.envName = " " val ex = intercept[RuntimeException] { - d.getPhysicalOp(workflowId, executionId) + buildPhysicalOp(d) } ex.getMessage shouldBe "Virtual Environment name is required when not using the default Python environment." @@ -146,35 +104,21 @@ class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { val d = new DualInputPortsPythonUDFOpDescV2 d.defaultEnv = false d.envName = " myenv " - d.getPhysicalOp(workflowId, executionId).pveName shouldBe "myenv" + buildPhysicalOp(d).pveName shouldBe "myenv" } "DualInputPortsPythonUDFOpDescV2 schema propagation" should "emit only the output columns when input columns are not retained" in { val d = new DualInputPortsPythonUDFOpDescV2 - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val out = d.getExternalOutputSchemas( - twoPortSchemas(Schema().add(new Attribute("in", AttributeType.STRING))) - ) - out shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema().add( - new Attribute("res", AttributeType.INTEGER) - ) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = false, twoPortSchemas(inputSchema)) } it should "retain the tuples-port columns (not the model port) when retaining input" in { val d = new DualInputPortsPythonUDFOpDescV2 d.retainInputColumns = true - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val out = d.getExternalOutputSchemas( - twoPortSchemas(Schema().add(new Attribute("in", AttributeType.STRING))) - ) - out shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema() - .add(new Attribute("in", AttributeType.STRING)) - .add(new Attribute("res", AttributeType.INTEGER)) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = true, twoPortSchemas(inputSchema)) } it should "reject an output column that collides with a retained input column" in { @@ -199,9 +143,7 @@ class DualInputPortsPythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { d.envName = "myenv" d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) d.uiParameters = List(uiParameter("count", "7")) - val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp]) - restored shouldBe a[DualInputPortsPythonUDFOpDescV2] - val r = restored.asInstanceOf[DualInputPortsPythonUDFOpDescV2] + val r = roundTrip(d) r.code shouldBe "print(1)" r.workers shouldBe 3 r.retainInputColumns shouldBe true diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/PythonUDFOpDescV2Spec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/PythonUDFOpDescV2Spec.scala index 5c94d1ec3e..be228d6e8c 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/PythonUDFOpDescV2Spec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/PythonUDFOpDescV2Spec.scala @@ -19,55 +19,25 @@ package org.apache.texera.amber.operator.udf.python -import org.apache.texera.amber.operator.udf.UiUDFParameter - import org.apache.texera.amber.core.executor.OpExecWithCode import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} -import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} import org.apache.texera.amber.core.workflow.{PortIdentity, UnknownPartition} -import org.apache.texera.amber.operator.{LogicalOp, PortDescription} import org.apache.texera.amber.operator.metadata.OperatorGroupConstants -import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.amber.operator.udf.UdfDescriptorSpecSupport import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers - -class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { - - private val workflowId = WorkflowIdentity(1L) - private val executionId = ExecutionIdentity(1L) - - private def resourceParameter(name: String, inputType: String, value: String): UiUDFParameter = { - val parameter = new UiUDFParameter - parameter.attribute = new Attribute(name, AttributeType.STRING) - parameter.inputType = inputType - parameter.value = value - parameter - } - - private val tupleUdfCode = - """from pytexera import * - | - |class ProcessTupleOperator(UDFOperatorV2): - | def process_tuple(self, tuple_, port): - | yield tuple_ - |""".stripMargin - private def uiParameter(name: String, value: String): UiUDFParameter = { - val parameter = new UiUDFParameter - parameter.attribute = new Attribute(name, AttributeType.INTEGER) - parameter.value = value - parameter - } +class PythonUDFOpDescV2Spec extends AnyFlatSpec with UdfDescriptorSpecSupport { "PythonUDFOpDescV2.operatorInfo" should "advertise the name, Python group, dynamic ports, and a default 1-in/1-out shape" in { - val info = (new PythonUDFOpDescV2).operatorInfo - info.userFriendlyName shouldBe "Python UDF" - info.operatorGroupName shouldBe OperatorGroupConstants.PYTHON_GROUP + val info = expectOperatorInfo( + new PythonUDFOpDescV2, + "Python UDF", + OperatorGroupConstants.PYTHON_GROUP, + 1 + ) info.dynamicInputPorts shouldBe true info.dynamicOutputPorts shouldBe true - info.inputPorts should have length 1 - info.outputPorts should have length 1 } "PythonUDFOpDescV2" should "default code/workers/flags" in { @@ -82,61 +52,44 @@ class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { "wire OpExecWithCode(code, \"python\") and carry port identities" in { val d = new PythonUDFOpDescV2 d.code = "yield t" - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "python" - code shouldBe "yield t" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("yield t", "python") physical.inputPorts.keySet shouldBe d.operatorInfo.inputPorts.map(_.id).toSet physical.outputPorts.keySet shouldBe d.operatorInfo.outputPorts.map(_.id).toSet } it should "reject a non-positive worker count" in { - val d = new PythonUDFOpDescV2 - d.workers = 0 - intercept[IllegalArgumentException] { d.getPhysicalOp(workflowId, executionId) } + expectInvalidWorkerCount(new PythonUDFOpDescV2)(_.workers = 0) } it should "inject configured UI parameters into the generated Python code" in { val d = new PythonUDFOpDescV2 - d.code = """from pytexera import * - | - |class ProcessTupleOperator(UDFOperatorV2): - | def process_tuple(self, tuple_, port): - | yield tuple_ - |""".stripMargin + d.code = tupleUdfCode d.uiParameters = List(uiParameter("count", "7")) - d.getPhysicalOp(workflowId, executionId).opExecInitInfo match { - case OpExecWithCode(code, "python") => - code should include("def _texera_injected_ui_parameters") - code should include("self.decode_python_template") - case other => fail(s"expected Python OpExecWithCode, got $other") - } + expectInjectedPython(d) } it should "defer nothing when no parameter names a resource" in { val d = new PythonUDFOpDescV2 d.code = tupleUdfCode d.uiParameters = List(uiParameter("count", "7")) - d.getPhysicalOp(workflowId, executionId).executionTimeBinding shouldBe None + buildPhysicalOp(d).executionTimeBinding shouldBe None } it should "leave a resource parameter to the execution, compiling without resolving it" in { val d = new PythonUDFOpDescV2 d.code = tupleUdfCode - d.uiParameters = List(resourceParameter("DS", "dataset", "/dataset/[email protected]/ds/v1")) + d.uiParameters = List(datasetParameter("DS", "/dataset/[email protected]/ds/v1")) // Resolving needs a database, which this suite has none of, so compiling must not try. - d.getPhysicalOp(workflowId, executionId).executionTimeBinding should not be empty + buildPhysicalOp(d).executionTimeBinding should not be empty } it should "refuse a resource parameter whose version was never chosen" in { val d = new PythonUDFOpDescV2 d.code = tupleUdfCode - d.uiParameters = List(resourceParameter("DS", "dataset", " ")) - val failure = the[RuntimeException] thrownBy d.getPhysicalOp(workflowId, executionId) + d.uiParameters = List(datasetParameter("DS", " ")) + val failure = the[RuntimeException] thrownBy buildPhysicalOp(d) failure.getMessage should include("No dataset selected for the parameter 'DS'") } @@ -144,33 +97,21 @@ class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { val d = new PythonUDFOpDescV2 d.defaultEnv = false d.envName = " " - intercept[RuntimeException] { d.getPhysicalOp(workflowId, executionId) } + intercept[RuntimeException] { buildPhysicalOp(d) } } "PythonUDFOpDescV2 schema propagation" should "emit only the output columns when input columns are not retained (default)" in { val d = new PythonUDFOpDescV2 - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val input = Schema().add(new Attribute("in", AttributeType.STRING)) - val out = d.getExternalOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> input)) - out shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema().add( - new Attribute("res", AttributeType.INTEGER) - ) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = false) } it should "retain input columns plus the output columns when retainInputColumns is true" in { val d = new PythonUDFOpDescV2 d.retainInputColumns = true - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val input = Schema().add(new Attribute("in", AttributeType.STRING)) - val out = d.getExternalOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> input)) - out shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema() - .add(new Attribute("in", AttributeType.STRING)) - .add(new Attribute("res", AttributeType.INTEGER)) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = true) } it should "reject an output column that collides with a retained input column" in { @@ -192,9 +133,7 @@ class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { d.envName = "myenv" d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) d.uiParameters = List(uiParameter("count", "7")) - val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp]) - restored shouldBe a[PythonUDFOpDescV2] - val p = restored.asInstanceOf[PythonUDFOpDescV2] + val p = roundTrip(d) p.code shouldBe "print(1)" p.workers shouldBe 3 p.retainInputColumns shouldBe true @@ -210,38 +149,26 @@ class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { val d = new PythonUDFOpDescV2 d.code = "yield t" d.workers = 4 - val physical = d.getPhysicalOp(workflowId, executionId) + val physical = buildPhysicalOp(d) physical.parallelizable shouldBe true physical.suggestedWorkerNum shouldBe Some(4) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - code shouldBe "yield t" - language shouldBe "python" - case other => fail(s"expected OpExecWithCode, got $other") - } + physical.opExecInitInfo shouldBe OpExecWithCode("yield t", "python") } it should "carry the trimmed virtual-environment name when the default env is disabled" in { val d = new PythonUDFOpDescV2 d.defaultEnv = false d.envName = " myenv " - d.getPhysicalOp(workflowId, executionId).pveName shouldBe "myenv" + buildPhysicalOp(d).pveName shouldBe "myenv" } it should "map each custom input port's partitionRequirement into the physical op" in { val d = new PythonUDFOpDescV2 d.code = "yield t" d.inputPorts = List( - PortDescription( - portID = "p0", - displayName = "in", - disallowMultiInputs = false, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("p0", "in") ) - d.getPhysicalOp(workflowId, executionId).partitionRequirement shouldBe List( + buildPhysicalOp(d).partitionRequirement shouldBe List( Some(UnknownPartition()) ) } @@ -250,22 +177,8 @@ class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { "derive input ports from a custom inputPorts descriptor list" in { val d = new PythonUDFOpDescV2 d.inputPorts = List( - PortDescription( - portID = "p0", - displayName = "left", - disallowMultiInputs = true, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ), - PortDescription( - portID = "p1", - displayName = "right", - disallowMultiInputs = false, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List(0) - ) + portDescription("p0", "left", disallowMultiInputs = true), + portDescription("p1", "right", dependencies = List(0)) ) val info = d.operatorInfo info.inputPorts should have length 2 @@ -278,14 +191,7 @@ class PythonUDFOpDescV2Spec extends AnyFlatSpec with Matchers { it should "derive output ports from a custom outputPorts descriptor list" in { val d = new PythonUDFOpDescV2 d.outputPorts = List( - PortDescription( - portID = "o0", - displayName = "result", - disallowMultiInputs = false, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("o0", "result") ) val info = d.operatorInfo info.outputPorts should have length 1 diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/source/PythonUDFSourceOpDescV2Spec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/source/PythonUDFSourceOpDescV2Spec.scala index ab14f793a8..bb80d34fe3 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/source/PythonUDFSourceOpDescV2Spec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/python/source/PythonUDFSourceOpDescV2Spec.scala @@ -21,34 +21,28 @@ package org.apache.texera.amber.operator.udf.python.source import org.apache.texera.amber.core.executor.OpExecWithCode import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} -import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} -import org.apache.texera.amber.operator.LogicalOp import org.apache.texera.amber.operator.metadata.OperatorGroupConstants -import org.apache.texera.amber.operator.udf.UiUDFParameter -import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.amber.operator.udf.UdfDescriptorSpecSupport import org.apache.texera.amber.operator.udf.python.PythonUdfTemplateTestUtils import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers -class PythonUDFSourceOpDescV2Spec extends AnyFlatSpec with Matchers { - - private val workflowId = WorkflowIdentity(1L) - private val executionId = ExecutionIdentity(1L) - - private def uiParameter(name: String, value: String): UiUDFParameter = { - val parameter = new UiUDFParameter - parameter.attribute = new Attribute(name, AttributeType.INTEGER) - parameter.value = value - parameter - } +class PythonUDFSourceOpDescV2Spec extends AnyFlatSpec with UdfDescriptorSpecSupport { + private val sourceUdfCode = + """from pytexera import * + | + |class GenerateOperator(UDFSourceOperator): + | def produce(self): + | yield + |""".stripMargin "PythonUDFSourceOpDescV2.operatorInfo" should "advertise the 1-out Python UDF source (no inputs, one output, reconfigurable)" in { - val info = (new PythonUDFSourceOpDescV2).operatorInfo - info.userFriendlyName shouldBe "1-out Python UDF" - info.operatorGroupName shouldBe OperatorGroupConstants.PYTHON_GROUP - info.inputPorts shouldBe empty - info.outputPorts should have length 1 + val info = expectOperatorInfo( + new PythonUDFSourceOpDescV2, + "1-out Python UDF", + OperatorGroupConstants.PYTHON_GROUP, + 0 + ) info.supportReconfiguration shouldBe true } @@ -63,55 +57,29 @@ class PythonUDFSourceOpDescV2Spec extends AnyFlatSpec with Matchers { "wire OpExecWithCode(code, \"python\") as a source op with one output port" in { val d = new PythonUDFSourceOpDescV2 d.code = "yield {'a': 1}" - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "python" - code shouldBe "yield {'a': 1}" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("yield {'a': 1}", "python") physical.outputPorts.keySet shouldBe d.operatorInfo.outputPorts.map(_.id).toSet } it should "reject a non-positive worker count" in { - val d = new PythonUDFSourceOpDescV2 - d.workers = 0 - intercept[IllegalArgumentException] { d.getPhysicalOp(workflowId, executionId) } + expectInvalidWorkerCount(new PythonUDFSourceOpDescV2)(_.workers = 0) } it should "inject configured UI parameters into the generated Python code" in { val d = new PythonUDFSourceOpDescV2 - d.code = """from pytexera import * - | - |class GenerateOperator(UDFSourceOperator): - | def produce(self): - | yield - |""".stripMargin + d.code = sourceUdfCode d.uiParameters = List(uiParameter("count", "7")) - d.getPhysicalOp(workflowId, executionId).opExecInitInfo match { - case OpExecWithCode(code, "python") => - code should include("def _texera_injected_ui_parameters") - code should include("self.decode_python_template") - case other => fail(s"expected Python OpExecWithCode, got $other") - } + expectInjectedPython(d) } it should "leave a resource parameter to the execution" in { val d = new PythonUDFSourceOpDescV2 - d.code = """from pytexera import * - | - |class GenerateOperator(UDFSourceOperator): - | def produce(self): - | yield - |""".stripMargin - val resource = new UiUDFParameter - resource.attribute = new Attribute("DS", AttributeType.STRING) - resource.inputType = "dataset" - resource.value = "/dataset/[email protected]/ds/v1" - d.uiParameters = List(resource) - - d.getPhysicalOp(workflowId, executionId).executionTimeBinding should not be empty + d.code = sourceUdfCode + d.uiParameters = List(datasetParameter("DS", "/dataset/[email protected]/ds/v1")) + + buildPhysicalOp(d).executionTimeBinding should not be empty } it should "reject a blank virtual-environment name when the default env is disabled" in { @@ -119,7 +87,7 @@ class PythonUDFSourceOpDescV2Spec extends AnyFlatSpec with Matchers { d.code = "yield" d.defaultEnv = false d.envName = " " - val ex = intercept[RuntimeException] { d.getPhysicalOp(workflowId, executionId) } + val ex = intercept[RuntimeException] { buildPhysicalOp(d) } ex.getMessage shouldBe "Virtual Environment name is required when not using the default Python environment." } @@ -129,14 +97,14 @@ class PythonUDFSourceOpDescV2Spec extends AnyFlatSpec with Matchers { d.code = "yield" d.defaultEnv = false d.envName = " my-venv " - d.getPhysicalOp(workflowId, executionId).pveName shouldBe "my-venv" + buildPhysicalOp(d).pveName shouldBe "my-venv" } it should "mark the op parallelizable with the requested worker count when workers > 1" in { val d = new PythonUDFSourceOpDescV2 d.code = "yield" d.workers = 3 - val physical = d.getPhysicalOp(workflowId, executionId) + val physical = buildPhysicalOp(d) physical.parallelizable shouldBe true physical.suggestedWorkerNum shouldBe Some(3) } @@ -149,9 +117,7 @@ class PythonUDFSourceOpDescV2Spec extends AnyFlatSpec with Matchers { d.envName = "venv" d.columns = List(new Attribute("a", AttributeType.STRING)) d.uiParameters = List(uiParameter("count", "7")) - val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp]) - restored shouldBe a[PythonUDFSourceOpDescV2] - val p = restored.asInstanceOf[PythonUDFSourceOpDescV2] + val p = roundTrip(d) p.code shouldBe "yield" p.workers shouldBe 2 p.defaultEnv shouldBe false diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFOpDescSpec.scala index fbffc5d6cc..61291987fe 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFOpDescSpec.scala @@ -20,27 +20,16 @@ package org.apache.texera.amber.operator.udf.r import org.apache.texera.amber.core.executor.OpExecWithCode -import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} -import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} import org.apache.texera.amber.core.workflow.{PortIdentity, UnknownPartition} -import org.apache.texera.amber.operator.{LogicalOp, PortDescription} import org.apache.texera.amber.operator.metadata.OperatorGroupConstants -import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.amber.operator.udf.UdfDescriptorSpecSupport import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers -class RUDFOpDescSpec extends AnyFlatSpec with Matchers { - - private val workflowId = WorkflowIdentity(1L) - private val executionId = ExecutionIdentity(1L) +class RUDFOpDescSpec extends AnyFlatSpec with UdfDescriptorSpecSupport { "RUDFOpDesc.operatorInfo" should "advertise the name, R group, and a default 1-in/1-out shape" in { - val info = (new RUDFOpDesc).operatorInfo - info.userFriendlyName shouldBe "R UDF" - info.operatorGroupName shouldBe OperatorGroupConstants.R_GROUP - info.inputPorts should have length 1 - info.outputPorts should have length 1 + expectOperatorInfo(new RUDFOpDesc, "R UDF", OperatorGroupConstants.R_GROUP, 1) } "RUDFOpDesc" should "default code/workers/useTupleAPI/retainInputColumns" in { @@ -55,45 +44,28 @@ class RUDFOpDescSpec extends AnyFlatSpec with Matchers { "wire OpExecWithCode(code, \"r-table\") and carry port identities" in { val d = new RUDFOpDesc d.code = "function(t) t" - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "r-table" - code shouldBe "function(t) t" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("function(t) t", "r-table") physical.inputPorts.keySet shouldBe d.operatorInfo.inputPorts.map(_.id).toSet physical.outputPorts.keySet shouldBe d.operatorInfo.outputPorts.map(_.id).toSet } it should "reject a non-positive worker count" in { - val d = new RUDFOpDesc - d.workers = 0 - intercept[IllegalArgumentException] { d.getPhysicalOp(workflowId, executionId) } + expectInvalidWorkerCount(new RUDFOpDesc)(_.workers = 0) } "RUDFOpDesc schema propagation" should "emit only the output columns when input columns are not retained (default)" in { val d = new RUDFOpDesc - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val input = Schema().add(new Attribute("in", AttributeType.STRING)) - d.getExternalOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> input)) shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema().add( - new Attribute("res", AttributeType.INTEGER) - ) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = false) } it should "retain input columns plus the output columns when retainInputColumns is true" in { val d = new RUDFOpDesc d.retainInputColumns = true - d.outputColumns = List(new Attribute("res", AttributeType.INTEGER)) - val input = Schema().add(new Attribute("in", AttributeType.STRING)) - d.getExternalOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> input)) shouldBe Map( - d.operatorInfo.outputPorts.head.id -> Schema() - .add(new Attribute("in", AttributeType.STRING)) - .add(new Attribute("res", AttributeType.INTEGER)) - ) + d.outputColumns = List(resultColumn) + expectOutputColumns(d, retainInput = true) } "RUDFOpDesc" should "round-trip its config fields through the polymorphic base" in { @@ -102,9 +74,7 @@ class RUDFOpDescSpec extends AnyFlatSpec with Matchers { d.workers = 2 d.useTupleAPI = true d.retainInputColumns = true - val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp]) - restored shouldBe a[RUDFOpDesc] - val r = restored.asInstanceOf[RUDFOpDesc] + val r = roundTrip(d) r.code shouldBe "f" r.workers shouldBe 2 r.useTupleAPI shouldBe true @@ -117,13 +87,8 @@ class RUDFOpDescSpec extends AnyFlatSpec with Matchers { d.code = "gen" d.useTupleAPI = true d.workers = 3 - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "r-tuple" - code shouldBe "gen" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("gen", "r-tuple") physical.parallelizable shouldBe true physical.suggestedWorkerNum shouldBe Some(3) } @@ -132,16 +97,9 @@ class RUDFOpDescSpec extends AnyFlatSpec with Matchers { val d = new RUDFOpDesc d.code = "f" d.inputPorts = List( - PortDescription( - portID = "0", - displayName = "in0", - disallowMultiInputs = false, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("0", "in0") ) - d.getPhysicalOp(workflowId, executionId).partitionRequirement shouldBe List( + buildPhysicalOp(d).partitionRequirement shouldBe List( Some(UnknownPartition()) ) } @@ -150,24 +108,10 @@ class RUDFOpDescSpec extends AnyFlatSpec with Matchers { "build ports from custom inputPorts/outputPorts descriptions when provided" in { val d = new RUDFOpDesc d.inputPorts = List( - PortDescription( - portID = "0", - displayName = "in0", - disallowMultiInputs = true, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("0", "in0", disallowMultiInputs = true) ) d.outputPorts = List( - PortDescription( - portID = "0", - displayName = "out0", - disallowMultiInputs = false, - isDynamicPort = false, - partitionRequirement = UnknownPartition(), - dependencies = List.empty - ) + portDescription("0", "out0") ) val info = d.operatorInfo info.inputPorts should have length 1 @@ -187,11 +131,6 @@ class RUDFOpDescSpec extends AnyFlatSpec with Matchers { result.isSuccess shouldBe true val (physical, stateTransfer) = result.get stateTransfer shouldBe None - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - code shouldBe "f2" - language shouldBe "r-table" - case other => fail(s"expected OpExecWithCode, got $other") - } + physical.opExecInitInfo shouldBe OpExecWithCode("f2", "r-table") } } diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFSourceOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFSourceOpDescSpec.scala index d03eba1280..5aee581f0f 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFSourceOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/udf/r/RUDFSourceOpDescSpec.scala @@ -22,25 +22,17 @@ package org.apache.texera.amber.operator.udf.r import com.fasterxml.jackson.databind.node.ObjectNode import org.apache.texera.amber.core.executor.OpExecWithCode import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} -import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} import org.apache.texera.amber.operator.LogicalOp import org.apache.texera.amber.operator.metadata.OperatorGroupConstants import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.amber.operator.udf.UdfDescriptorSpecSupport import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers -class RUDFSourceOpDescSpec extends AnyFlatSpec with Matchers { - - private val workflowId = WorkflowIdentity(1L) - private val executionId = ExecutionIdentity(1L) +class RUDFSourceOpDescSpec extends AnyFlatSpec with UdfDescriptorSpecSupport { "RUDFSourceOpDesc.operatorInfo" should "advertise the 1-out R UDF source (no inputs, one output)" in { - val info = (new RUDFSourceOpDesc).operatorInfo - info.userFriendlyName shouldBe "1-out R UDF" - info.operatorGroupName shouldBe OperatorGroupConstants.R_GROUP - info.inputPorts shouldBe empty - info.outputPorts should have length 1 + expectOperatorInfo(new RUDFSourceOpDesc, "1-out R UDF", OperatorGroupConstants.R_GROUP, 0) } "RUDFSourceOpDesc.sourceSchema" should "be empty by default and reflect the configured columns" in { @@ -66,13 +58,8 @@ class RUDFSourceOpDescSpec extends AnyFlatSpec with Matchers { "wire OpExecWithCode(code, \"r-table\") as a source op with one output port" in { val d = new RUDFSourceOpDesc d.code = "data.frame(a=1)" - val physical = d.getPhysicalOp(workflowId, executionId) - physical.opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "r-table" - code shouldBe "data.frame(a=1)" - case other => fail(s"expected OpExecWithCode, got $other") - } + val physical = buildPhysicalOp(d) + physical.opExecInitInfo shouldBe OpExecWithCode("data.frame(a=1)", "r-table") physical.outputPorts.keySet shouldBe d.operatorInfo.outputPorts.map(_.id).toSet // A source that fans out is marked one-to-many for the scheduler; the flag // defaults to false, so nothing else in the spec would notice it flipping. @@ -86,7 +73,7 @@ class RUDFSourceOpDescSpec extends AnyFlatSpec with Matchers { val d = new RUDFSourceOpDesc d.code = "data.frame(a=1)" d.columns = List(new Attribute("a", AttributeType.STRING)) - val physical = d.getPhysicalOp(workflowId, executionId) + val physical = buildPhysicalOp(d) physical.propagateSchema.func(Map.empty) shouldBe Map( d.operatorInfo.outputPorts.head.id -> Schema().add(new Attribute("a", AttributeType.STRING)) ) @@ -96,12 +83,10 @@ class RUDFSourceOpDescSpec extends AnyFlatSpec with Matchers { val d = new RUDFSourceOpDesc d.code = "coro::generator(function() {})" d.useTupleAPI = true - d.getPhysicalOp(workflowId, executionId).opExecInitInfo match { - case OpExecWithCode(code, language) => - language shouldBe "r-tuple" - code shouldBe "coro::generator(function() {})" - case other => fail(s"expected OpExecWithCode, got $other") - } + buildPhysicalOp(d).opExecInitInfo shouldBe OpExecWithCode( + "coro::generator(function() {})", + "r-tuple" + ) } it should "parallelize at the requested worker count only when more than one worker is asked for" in { @@ -129,8 +114,7 @@ class RUDFSourceOpDescSpec extends AnyFlatSpec with Matchers { it should "reject a non-positive worker count" in { val d = new RUDFSourceOpDesc d.code = "x" - d.workers = 0 - intercept[IllegalArgumentException] { d.getPhysicalOp(workflowId, executionId) } + expectInvalidWorkerCount(d)(_.workers = 0) } "RUDFSourceOpDesc" should "round-trip its config fields through the polymorphic base" in { @@ -139,9 +123,7 @@ class RUDFSourceOpDescSpec extends AnyFlatSpec with Matchers { d.workers = 2 d.useTupleAPI = true d.columns = List(new Attribute("a", AttributeType.STRING)) - val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp]) - restored shouldBe a[RUDFSourceOpDesc] - val r = restored.asInstanceOf[RUDFSourceOpDesc] + val r = roundTrip(d) r.code shouldBe "f" r.workers shouldBe 2 r.useTupleAPI shouldBe true
