diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDesc.scala index 42028c7cf64..e2daa5a2d13 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDesc.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDesc.scala @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject import org.apache.texera.amber.core.tuple.{AttributeType, Schema} +import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{ + requireNonEmpty, + requireNumericInputColumn +} import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity} @@ -189,13 +193,13 @@ class HuggingFaceIrisLogisticRegressionOpDesc override def getOutputSchemas( inputSchemas: Map[PortIdentity, Schema] ): Map[PortIdentity, Schema] = { - if ( - predictionClassName == null || predictionClassName.trim.isEmpty || - predictionProbabilityName == null || predictionProbabilityName.trim.isEmpty - ) - throw new RuntimeException("Result attribute name should not be empty") + val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id) + requireNumericInputColumn(inputSchema, "Petal Length Cm Attribute", petalLengthCmAttribute) + requireNumericInputColumn(inputSchema, "Petal Width Cm Attribute", petalWidthCmAttribute) + requireNonEmpty("Prediction class name", predictionClassName) + requireNonEmpty("Prediction probability name", predictionProbabilityName) Map( - operatorInfo.outputPorts.head.id -> inputSchemas(operatorInfo.inputPorts.head.id) + operatorInfo.outputPorts.head.id -> inputSchema .add(predictionClassName, AttributeType.STRING) .add(predictionProbabilityName, AttributeType.DOUBLE) ) diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSchemaValidation.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSchemaValidation.scala new file mode 100644 index 00000000000..e29d235f720 --- /dev/null +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSchemaValidation.scala @@ -0,0 +1,67 @@ +/* + * 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.huggingFace + +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} + +/** + * Configuration checks shared by the Spam Detection, Sentiment Analysis, Text + * Summarization and Iris Logistic Regression operators' `getOutputSchemas`. + * + * Each check throws. The compiler runs `getOutputSchemas` inside a `Try` and stores + * a thrown error on the operator's output port, which is how an operator that is + * not configured yet reports why. Messages name the field as the form shows it. + */ +private[huggingFace] object HuggingFaceSchemaValidation { + + private val NUMERIC_TYPES = Set(AttributeType.INTEGER, AttributeType.LONG, AttributeType.DOUBLE) + + /** Require a column-name field to be set and not blank. */ + def requireNonEmpty(fieldTitle: String, value: String): Unit = + if (value == null || value.trim.isEmpty) { + throw new RuntimeException(s"$fieldTitle cannot be empty") + } + + /** + * Require the column a field names to be in the input table, spelled exactly as + * it is there: Schema lookups ignore case, but the generated Python does not. + */ + def requireInputColumn(schema: Schema, fieldTitle: String, column: String): Attribute = { + requireNonEmpty(fieldTitle, column) + schema.attributes + .find(_.getName == column) + .getOrElse( + throw new RuntimeException( + s"$fieldTitle '$column' is not in the input table. " + + s"Available columns: ${schema.getAttributeNames.mkString(", ")}" + ) + ) + } + + /** As [[requireInputColumn]], and the column must also be numeric. */ + def requireNumericInputColumn(schema: Schema, fieldTitle: String, column: String): Unit = { + val columnType = requireInputColumn(schema, fieldTitle, column).getType + if (!NUMERIC_TYPES.contains(columnType)) { + throw new RuntimeException( + s"$fieldTitle '$column' must be numeric (integer, long or double), not ${columnType.getName}" + ) + } + } +} diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDesc.scala index 6d7c62bff4f..722e9b24c14 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDesc.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDesc.scala @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject import org.apache.texera.amber.core.tuple.{AttributeType, Schema} +import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{ + requireInputColumn, + requireNonEmpty +} import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity} import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator} import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn} @@ -164,14 +168,13 @@ class HuggingFaceSentimentAnalysisOpDesc override def getOutputSchemas( inputSchemas: Map[PortIdentity, Schema] ): Map[PortIdentity, Schema] = { - if ( - resultAttributePositive == null || resultAttributePositive.trim.isEmpty || - resultAttributeNeutral == null || resultAttributeNeutral.trim.isEmpty || - resultAttributeNegative == null || resultAttributeNegative.trim.isEmpty - ) - return null + val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id) + requireInputColumn(inputSchema, "Attribute", attribute) + requireNonEmpty("Positive result attribute", resultAttributePositive) + requireNonEmpty("Neutral result attribute", resultAttributeNeutral) + requireNonEmpty("Negative result attribute", resultAttributeNegative) Map( - operatorInfo.outputPorts.head.id -> inputSchemas(operatorInfo.inputPorts.head.id) + operatorInfo.outputPorts.head.id -> inputSchema .add(resultAttributePositive, AttributeType.DOUBLE) .add(resultAttributeNeutral, AttributeType.DOUBLE) .add(resultAttributeNegative, AttributeType.DOUBLE) diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDesc.scala index e0a9138d82c..818536efbcc 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDesc.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDesc.scala @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject import org.apache.texera.amber.core.tuple.{AttributeType, Schema} +import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{ + requireInputColumn, + requireNonEmpty +} import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity} import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator} import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn} @@ -129,13 +133,12 @@ class HuggingFaceSpamSMSDetectionOpDesc override def getOutputSchemas( inputSchemas: Map[PortIdentity, Schema] ): Map[PortIdentity, Schema] = { - if ( - resultAttributeSpam == null || resultAttributeSpam.trim.isEmpty || - resultAttributeProbability == null || resultAttributeProbability.trim.isEmpty - ) - return null + val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id) + requireInputColumn(inputSchema, "Attribute", attribute) + requireNonEmpty("Spam result attribute", resultAttributeSpam) + requireNonEmpty("Score result attribute", resultAttributeProbability) Map( - operatorInfo.outputPorts.head.id -> inputSchemas(operatorInfo.inputPorts.head.id) + operatorInfo.outputPorts.head.id -> inputSchema .add(resultAttributeSpam, AttributeType.BOOLEAN) .add(resultAttributeProbability, AttributeType.DOUBLE) ) diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDesc.scala index 2f6b113bfff..2fbb59b1456 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDesc.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDesc.scala @@ -22,6 +22,10 @@ package org.apache.texera.amber.operator.huggingFace import com.fasterxml.jackson.annotation.{JsonProperty, JsonPropertyDescription} import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject import org.apache.texera.amber.core.tuple.{AttributeType, Schema} +import org.apache.texera.amber.operator.huggingFace.HuggingFaceSchemaValidation.{ + requireInputColumn, + requireNonEmpty +} import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity} import org.apache.texera.amber.operator.{PythonOperatorDescriptor, StandaloneCodeGenerator} import org.apache.texera.amber.operator.metadata.annotations.{AutofillAttributeName, SampleColumn} @@ -134,10 +138,11 @@ class HuggingFaceTextSummarizationOpDesc override def getOutputSchemas( inputSchemas: Map[PortIdentity, Schema] ): Map[PortIdentity, Schema] = { - if (resultAttribute == null || resultAttribute.trim.isEmpty) - throw new RuntimeException("Result attribute name should be given") + val inputSchema = inputSchemas(operatorInfo.inputPorts.head.id) + requireInputColumn(inputSchema, "Attribute", attribute) + requireNonEmpty("Result attribute name", resultAttribute) Map( - operatorInfo.outputPorts.head.id -> inputSchemas.values.head + operatorInfo.outputPorts.head.id -> inputSchema .add(resultAttribute, AttributeType.STRING) ) } diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDescSpec.scala index bdb587ade20..83d3ab719b1 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceIrisLogisticRegressionOpDescSpec.scala @@ -65,18 +65,12 @@ class HuggingFaceIrisLogisticRegressionOpDescSpec } "HuggingFaceIrisLogisticRegressionOpDesc.getOutputSchemas" should - "reject a blank prediction result name" in { - val d = new HuggingFaceIrisLogisticRegressionOpDesc - val in = Schema().add("sepal", AttributeType.STRING) - val ex = intercept[RuntimeException] { - d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) - } - ex.getMessage shouldBe "Result attribute name should not be empty" - } - - it should "append a STRING class column and a DOUBLE probability column, keyed by the output port" in { + "append a STRING class column and a DOUBLE probability column, keyed by the output port" in { val d = configured() - val in = Schema().add("sepal", AttributeType.STRING) + val in = Schema() + .add("sepal", AttributeType.STRING) + .add("petalLength", AttributeType.DOUBLE) + .add("petalWidth", AttributeType.DOUBLE) val out = d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) val schema = out(d.operatorInfo.outputPorts.head.id) schema.getAttribute("sepal").getType shouldBe AttributeType.STRING @@ -84,6 +78,86 @@ class HuggingFaceIrisLogisticRegressionOpDescSpec schema.getAttribute("probability").getType shouldBe AttributeType.DOUBLE } + private def petals( + lengthType: AttributeType = AttributeType.DOUBLE, + widthType: AttributeType = AttributeType.DOUBLE + ): Schema = Schema().add("petalLength", lengthType).add("petalWidth", widthType) + + private def schemaError( + d: HuggingFaceIrisLogisticRegressionOpDesc, + in: Schema = petals() + ): String = + intercept[RuntimeException] { + d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) + }.getMessage + + it should "reject each unset or blank prediction result name, naming the field" in { + for (empty <- Seq(null, "", " ")) { + withClue(s"[$empty]: ") { + val noClass = configured() + noClass.predictionClassName = empty + schemaError(noClass) shouldBe "Prediction class name cannot be empty" + + val noProbability = configured() + noProbability.predictionProbabilityName = empty + schemaError(noProbability) shouldBe "Prediction probability name cannot be empty" + } + } + } + + it should "reject an unset petal column" in { + val noLength = configured() + noLength.petalLengthCmAttribute = null + schemaError(noLength) shouldBe "Petal Length Cm Attribute cannot be empty" + + val noWidth = configured() + noWidth.petalWidthCmAttribute = "" + schemaError(noWidth) shouldBe "Petal Width Cm Attribute cannot be empty" + } + + it should "reject a petal column that is not in the input table, listing the columns" in { + val d = configured() + d.petalWidthCmAttribute = "petal_width" + schemaError(d) shouldBe + "Petal Width Cm Attribute 'petal_width' is not in the input table. " + + "Available columns: petalLength, petalWidth" + } + + it should "match a petal column's case exactly, as the generated Python does" in { + val d = configured() + d.petalLengthCmAttribute = "PetalLength" + schemaError(d) should startWith( + "Petal Length Cm Attribute 'PetalLength' is not in the input table" + ) + } + + it should "reject a petal column that is not numeric" in { + schemaError(configured(), petals(lengthType = AttributeType.STRING)) shouldBe + "Petal Length Cm Attribute 'petalLength' must be numeric (integer, long or double), not string" + schemaError(configured(), petals(widthType = AttributeType.BOOLEAN)) shouldBe + "Petal Width Cm Attribute 'petalWidth' must be numeric (integer, long or double), not boolean" + } + + it should "accept integer, long and double petal columns" in { + for (numeric <- Seq(AttributeType.INTEGER, AttributeType.LONG, AttributeType.DOUBLE)) { + val d = configured() + val in = petals(numeric, numeric) + noException should be thrownBy d.getOutputSchemas( + Map(d.operatorInfo.inputPorts.head.id -> in) + ) + } + } + + it should "leave a readable error on its output port when the compiler propagates the schema" in { + val d = configured() + d.predictionClassName = null + val physical = d + .getPhysicalOp(workflowId, executionId) + .propagateSchema(Some(d.operatorInfo.inputPorts.head.id -> petals())) + val port = physical.outputPorts(d.operatorInfo.outputPorts.head.id)._3 + port.left.map(_.getMessage) shouldBe Left("Prediction class name cannot be empty") + } + "HuggingFaceIrisLogisticRegressionOpDesc.generatePythonCode" should "emit the logistic-regression operator carrying the configured columns (encoded)" in { val d = configured() diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDescSpec.scala index 7e6ef12d11e..9960792e4fb 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSentimentAnalysisOpDescSpec.scala @@ -70,63 +70,69 @@ class HuggingFaceSentimentAnalysisOpDescSpec } "HuggingFaceSentimentAnalysisOpDesc.getOutputSchemas" should - "return null when any result column is unset" in { - val d = new HuggingFaceSentimentAnalysisOpDesc + "append the three sentiment columns as DOUBLE, keyed by the declared output port" in { + val d = configured() val in = Schema().add("text", AttributeType.STRING) - d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) shouldBe null + val out = d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) + val schema = out(d.operatorInfo.outputPorts.head.id) + schema.getAttribute("text").getType shouldBe AttributeType.STRING + schema.getAttribute("pos").getType shouldBe AttributeType.DOUBLE + schema.getAttribute("neu").getType shouldBe AttributeType.DOUBLE + schema.getAttribute("neg").getType shouldBe AttributeType.DOUBLE } - it should "return null when a result column is set but blank" in { - // Each of the three guards has a null half and a whitespace half. The whitespace - // half is the one a user actually hits: a cleared-out text box arrives as " ", - // not as null, and a schema whose column name is blank is not a usable schema. - val in = Schema().add("text", AttributeType.STRING) - val blanks = Seq("", " ", "\t") - blanks.foreach { blank => - val d = configured() - d.resultAttributePositive = blank - withClue(s"positive = [$blank]: ") { - d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) shouldBe null + private val textInput = Schema().add("text", AttributeType.STRING) + + private def schemaError(d: HuggingFaceSentimentAnalysisOpDesc, in: Schema = textInput): String = + intercept[RuntimeException] { + d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) + }.getMessage + + it should "reject each unset or blank result column, naming the field" in { + // Split per column: a copy-pasted guard that re-tests one field in another's + // clause would pass an all-unset case, and naming the field catches it. + val fields: Seq[(String, (HuggingFaceSentimentAnalysisOpDesc, String) => Unit)] = Seq( + "Positive result attribute" -> ((d, v) => d.resultAttributePositive = v), + "Neutral result attribute" -> ((d, v) => d.resultAttributeNeutral = v), + "Negative result attribute" -> ((d, v) => d.resultAttributeNegative = v) + ) + for ((title, set) <- fields; empty <- Seq(null, "", " ", "\t")) { + withClue(s"$title = [$empty]: ") { + val d = configured() + set(d, empty) + schemaError(d) shouldBe s"$title cannot be empty" } } } - it should "return null when only the neutral result column is unset" in { - // Split out per column: a copy-pasted guard that re-tests `resultAttributePositive` - // in the neutral clause passes the "all three unset" case and this one is what - // catches it. - val in = Schema().add("text", AttributeType.STRING) - - val nulled = configured() - nulled.resultAttributeNeutral = null - nulled.getOutputSchemas(Map(nulled.operatorInfo.inputPorts.head.id -> in)) shouldBe null - - val blanked = configured() - blanked.resultAttributeNeutral = " " - blanked.getOutputSchemas(Map(blanked.operatorInfo.inputPorts.head.id -> in)) shouldBe null + it should "reject an unset input attribute" in { + val d = configured() + d.attribute = " " + schemaError(d) shouldBe "Attribute cannot be empty" } - it should "return null when only the negative result column is unset" in { - val in = Schema().add("text", AttributeType.STRING) - - val nulled = configured() - nulled.resultAttributeNegative = null - nulled.getOutputSchemas(Map(nulled.operatorInfo.inputPorts.head.id -> in)) shouldBe null + it should "reject an input attribute that is not in the input table, listing the columns" in { + val d = configured() + d.attribute = "review" + schemaError(d) shouldBe "Attribute 'review' is not in the input table. Available columns: text" + } - val blanked = configured() - blanked.resultAttributeNegative = " " - blanked.getOutputSchemas(Map(blanked.operatorInfo.inputPorts.head.id -> in)) shouldBe null + it should "match the input attribute's case exactly, as the generated Python does" in { + val d = configured() + d.attribute = "TEXT" + schemaError(d) should startWith("Attribute 'TEXT' is not in the input table") } - it should "append the three sentiment columns as DOUBLE, keyed by the declared output port" in { + it should "leave a readable error on its output port when the compiler propagates the schema" in { + // The compiler runs getOutputSchemas inside a Try and stores a thrown error on the + // output port. A returned null escaped that Try as a NullPointerException instead. val d = configured() - val in = Schema().add("text", AttributeType.STRING) - val out = d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) - val schema = out(d.operatorInfo.outputPorts.head.id) - schema.getAttribute("text").getType shouldBe AttributeType.STRING - schema.getAttribute("pos").getType shouldBe AttributeType.DOUBLE - schema.getAttribute("neu").getType shouldBe AttributeType.DOUBLE - schema.getAttribute("neg").getType shouldBe AttributeType.DOUBLE + d.resultAttributeNeutral = null + val physical = d + .getPhysicalOp(workflowId, executionId) + .propagateSchema(Some(d.operatorInfo.inputPorts.head.id -> textInput)) + val port = physical.outputPorts(d.operatorInfo.outputPorts.head.id)._3 + port.left.map(_.getMessage) shouldBe Left("Neutral result attribute cannot be empty") } "HuggingFaceSentimentAnalysisOpDesc.generatePythonCode" should diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDescSpec.scala index 2f4c6f46e2b..eac0a31d914 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceSpamSMSDetectionOpDescSpec.scala @@ -64,7 +64,7 @@ class HuggingFaceSpamSMSDetectionOpDescSpec "HuggingFaceSpamSMSDetectionOpDesc.getOutputSchemas" should "append a BOOLEAN spam column and a DOUBLE score column, keyed by the declared output port" in { val d = configured() - val in = Schema().add("msg", AttributeType.STRING) + val in = Schema().add("msg", AttributeType.STRING).add("text", AttributeType.STRING) val out = d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) val schema = out(d.operatorInfo.outputPorts.head.id) schema.getAttribute("msg").getType shouldBe AttributeType.STRING @@ -72,33 +72,60 @@ class HuggingFaceSpamSMSDetectionOpDescSpec schema.getAttribute("score").getType shouldBe AttributeType.DOUBLE } - it should "return null while a result attribute is still unset" in { - // getOutputSchemas is called continuously as the user configures the operator, - // so an unset name means "no schema yet", not "build one with a null column". - // The sibling operators (sentiment analysis, iris) already answer this way. - val in = Schema().add("msg", AttributeType.STRING) - val ports = Map((new HuggingFaceSpamSMSDetectionOpDesc).operatorInfo.inputPorts.head.id -> in) + private val textInput = Schema().add("text", AttributeType.STRING) - val noSpamCol = configured() - noSpamCol.resultAttributeSpam = null - noSpamCol.getOutputSchemas(ports) shouldBe null + private def schemaError(d: HuggingFaceSpamSMSDetectionOpDesc, in: Schema = textInput): String = + intercept[RuntimeException] { + d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) + }.getMessage - val noScoreCol = configured() - noScoreCol.resultAttributeProbability = null - noScoreCol.getOutputSchemas(ports) shouldBe null + it should "reject an unset or blank result attribute, naming the field" in { + Seq(null, "", " ").foreach { empty => + withClue(s"[$empty]: ") { + val noSpamCol = configured() + noSpamCol.resultAttributeSpam = empty + schemaError(noSpamCol) shouldBe "Spam result attribute cannot be empty" + + val noScoreCol = configured() + noScoreCol.resultAttributeProbability = empty + schemaError(noScoreCol) shouldBe "Score result attribute cannot be empty" + } + } + } + + it should "reject an unset input attribute" in { + val d = configured() + d.attribute = null + schemaError(d) shouldBe "Attribute cannot be empty" } - it should "return null when a result attribute is blank" in { - val in = Schema().add("msg", AttributeType.STRING) - val ports = Map((new HuggingFaceSpamSMSDetectionOpDesc).operatorInfo.inputPorts.head.id -> in) + it should "reject an input attribute that is not in the input table, listing the columns" in { + val d = configured() + d.attribute = "txt" + schemaError( + d, + Schema().add("text", AttributeType.STRING).add("id", AttributeType.INTEGER) + ) shouldBe + "Attribute 'txt' is not in the input table. Available columns: text, id" + } - val blankSpamCol = configured() - blankSpamCol.resultAttributeSpam = " " - blankSpamCol.getOutputSchemas(ports) shouldBe null + it should "match the input attribute's case exactly, as the generated Python does" in { + // Schema lookups ignore case, but tuple_[...] and out1df[...] do not. + val d = configured() + d.attribute = "Text" + schemaError(d) should startWith("Attribute 'Text' is not in the input table") + } - val blankScoreCol = configured() - blankScoreCol.resultAttributeProbability = "" - blankScoreCol.getOutputSchemas(ports) shouldBe null + it should "leave a readable error on its output port when the compiler propagates the schema" in { + // The compiler runs getOutputSchemas inside a Try and stores a thrown error on the + // output port. A returned null escaped that Try as a NullPointerException instead. + val d = configured() + d.resultAttributeSpam = null + val physical = d + .getPhysicalOp(workflowId, executionId) + .propagateSchema(Some(d.operatorInfo.inputPorts.head.id -> textInput)) + val port = physical.outputPorts(d.operatorInfo.outputPorts.head.id)._3 + port.left.map(_.getMessage) shouldBe Left("Spam result attribute cannot be empty") } "HuggingFaceSpamSMSDetectionOpDesc.generatePythonCode" should diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDescSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDescSpec.scala index 50599e966db..a670611811c 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDescSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/huggingFace/HuggingFaceTextSummarizationOpDescSpec.scala @@ -23,6 +23,7 @@ import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject import org.apache.texera.amber.core.executor.OpExecWithCode import org.apache.texera.amber.core.tuple.{AttributeType, Schema} import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity} +import org.apache.texera.amber.core.workflow.PortIdentity import org.apache.texera.amber.operator.LogicalOp import org.apache.texera.amber.operator.metadata.OperatorGroupConstants import org.apache.texera.amber.util.JSONUtils.objectMapper @@ -61,16 +62,7 @@ class HuggingFaceTextSummarizationOpDescSpec } "HuggingFaceTextSummarizationOpDesc.getOutputSchemas" should - "reject a blank result attribute name" in { - val d = new HuggingFaceTextSummarizationOpDesc - val in = Schema().add("text", AttributeType.STRING) - val ex = intercept[RuntimeException] { - d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) - } - ex.getMessage shouldBe "Result attribute name should be given" - } - - it should "append the summary column as STRING, keyed by the declared output port" in { + "append the summary column as STRING, keyed by the declared output port" in { val d = configured() val in = Schema().add("text", AttributeType.STRING) val out = d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) @@ -79,6 +71,62 @@ class HuggingFaceTextSummarizationOpDescSpec schema.getAttribute("summary").getType shouldBe AttributeType.STRING } + private val textInput = Schema().add("text", AttributeType.STRING) + + private def schemaError(d: HuggingFaceTextSummarizationOpDesc, in: Schema = textInput): String = + intercept[RuntimeException] { + d.getOutputSchemas(Map(d.operatorInfo.inputPorts.head.id -> in)) + }.getMessage + + it should "reject an unset or blank result attribute name, naming the field" in { + Seq(null, "", " ").foreach { empty => + withClue(s"[$empty]: ") { + val d = configured() + d.resultAttribute = empty + schemaError(d) shouldBe "Result attribute name cannot be empty" + } + } + } + + it should "reject an unset input attribute" in { + val d = configured() + d.attribute = null + schemaError(d) shouldBe "Attribute cannot be empty" + } + + it should "reject an input attribute that is not in the input table, listing the columns" in { + val d = configured() + d.attribute = "body" + schemaError(d) shouldBe "Attribute 'body' is not in the input table. Available columns: text" + } + + it should "match the input attribute's case exactly, as the generated Python does" in { + val d = configured() + d.attribute = "Text" + schemaError(d) should startWith("Attribute 'Text' is not in the input table") + } + + it should "read the input schema from its declared input port" in { + // Keyed by port id, as the other three operators do, rather than taking + // whichever schema the map yields first. + val d = configured() + val other = Schema().add("unrelated", AttributeType.INTEGER) + val out = d.getOutputSchemas( + Map(PortIdentity(7) -> other, d.operatorInfo.inputPorts.head.id -> textInput) + ) + out(d.operatorInfo.outputPorts.head.id).getAttributeNames shouldBe List("text", "summary") + } + + it should "leave a readable error on its output port when the compiler propagates the schema" in { + val d = configured() + d.resultAttribute = null + val physical = d + .getPhysicalOp(workflowId, executionId) + .propagateSchema(Some(d.operatorInfo.inputPorts.head.id -> textInput)) + val port = physical.outputPorts(d.operatorInfo.outputPorts.head.id)._3 + port.left.map(_.getMessage) shouldBe Left("Result attribute name cannot be empty") + } + "HuggingFaceTextSummarizationOpDesc.generatePythonCode" should "emit the bert-mini2bert summarizer carrying the configured columns (encoded)" in { val d = configured()