You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@texera.apache.org by gi...@apache.org on 2026/07/09 02:17:30 UTC
(texera) branch gh-readonly-queue/main/pr-6048-00572931bf6d5644833efc838b9efa6792c60135 created (now 54b6915325)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-6048-00572931bf6d5644833efc838b9efa6792c60135
in repository https://gitbox.apache.org/repos/asf/texera.git
at 54b6915325 refactor(workflow-operator): extract shared Sklearn descriptor base (#6048)
This branch includes the following new commits:
new 54b6915325 refactor(workflow-operator): extract shared Sklearn descriptor base (#6048)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(texera) 01/01: refactor(workflow-operator): extract shared Sklearn descriptor base (#6048)
Posted by gi...@apache.org.
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-6048-00572931bf6d5644833efc838b9efa6792c60135
in repository https://gitbox.apache.org/repos/asf/texera.git
commit 54b6915325a02bfab8119692b8b37b161fa3d43a
Author: Matthew B. <mg...@uci.edu>
AuthorDate: Wed Jul 8 19:17:10 2026 -0700
refactor(workflow-operator): extract shared Sklearn descriptor base (#6048)
### What changes were proposed in this PR?
- Add abstract `SklearnModelOpDesc extends PythonOperatorDescriptor`
holding the four shared config fields (`target`, `countVectorizer`,
`text`, `tfidfTransformer`) with their Jackson/JSON-schema annotations,
the two `@JsonIgnore` hooks (`getImportStatements`,
`getUserFriendlyModelName`), and the shared `getOutputSchemas`.
- Change `SklearnClassifierOpDesc` and `SklearnTrainingOpDesc` to extend
the new base, so each keeps only what genuinely differs
(`generatePythonCode`, `operatorInfo`), removing ~50 lines of
copy-pasted, drift-prone code.
- The 51 leaf subclasses inherit the fields unchanged, so the generated
operator schema is identical (no behavior change).
### Any related issues, documentation, discussions?
Closes: #6038
### How was this PR tested?
- Pure refactor with no behavior change; covered by existing
`SklearnOpDescRegistrySpec`, which instantiates a leaf classifier, sets
the inherited `target`/`countVectorizer` fields, and asserts on
`generatePythonCode()` output. Run it in CI via `sbt
"WorkflowOperator/jacoco"`, expect the Sklearn registry specs green.
- Verified locally with `sbt "WorkflowOperator/Test/compile"` (compiles
clean; both descriptors resolve against `SklearnModelOpDesc`).
- Note: `sbt "WorkflowOperator/testOnly *SklearnOpDescRegistrySpec"`
could not run locally because sbt-jacoco offline-instruments the Test
classpath and JaCoCo 0.8.11 fails to instrument the vendored
`JsonSchemaDraft.class` on this JDK (pre-existing tooling issue,
unrelated to this change); the spec runs in CI under the
`WorkflowOperator/jacoco` job.
### Was this PR authored or co-authored using generative AI tooling?
Co-authored with Claude Opus 4.8 in compliance with ASF
---------
Co-authored-by: Xinyuan Lin <xi...@uci.edu>
---
.../operator/sklearn/SklearnClassifierOpDesc.scala | 74 +--------------------
...ainingOpDesc.scala => SklearnModelOpDesc.scala} | 41 ++----------
.../sklearn/training/SklearnTrainingOpDesc.scala | 75 ++--------------------
.../sklearn/SklearnOpDescRegistrySpec.scala | 8 +--
4 files changed, 16 insertions(+), 182 deletions(-)
diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnClassifierOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnClassifierOpDesc.scala
index 0c8a103c52..92aec692a6 100644
--- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnClassifierOpDesc.scala
+++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnClassifierOpDesc.scala
@@ -19,73 +19,15 @@
package org.apache.texera.amber.operator.sklearn
-import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription}
-import com.kjetland.jackson.jsonSchema.annotations.{
- JsonSchemaInject,
- JsonSchemaInt,
- JsonSchemaString,
- JsonSchemaTitle
-}
-import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
-import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
-import org.apache.texera.amber.operator.PythonOperatorDescriptor
-import org.apache.texera.amber.operator.metadata.annotations.{
- AutofillAttributeName,
- CommonOpDescAnnotation,
- HideAnnotation
-}
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
-abstract class SklearnClassifierOpDesc extends PythonOperatorDescriptor {
-
- @JsonSchemaTitle("Target Attribute")
- @JsonPropertyDescription("Attribute in your dataset corresponding to target.")
- @JsonProperty(required = true)
- @AutofillAttributeName
- var target: EncodableString = _
+abstract class SklearnClassifierOpDesc extends SklearnModelOpDesc {
- @JsonSchemaTitle("Count Vectorizer")
- @JsonPropertyDescription("Convert a collection of text documents to a matrix of token counts.")
- @JsonProperty(defaultValue = "false")
- var countVectorizer: Boolean = false
+ override def getImportStatements = ""
- @JsonSchemaTitle("Text Attribute")
- @JsonPropertyDescription("Attribute in your dataset with text to vectorize.")
- @JsonSchemaInject(
- strings = Array(
- new JsonSchemaString(
- path = CommonOpDescAnnotation.autofill,
- value = CommonOpDescAnnotation.attributeName
- ),
- new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
- new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
- new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
- ),
- ints = Array(
- new JsonSchemaInt(path = CommonOpDescAnnotation.autofillAttributeOnPort, value = 0)
- )
- )
- var text: EncodableString = _
-
- @JsonSchemaTitle("Tfidf Transformer")
- @JsonPropertyDescription("Transform a count matrix to a normalized tf or tf-idf representation.")
- @JsonProperty(defaultValue = "false")
- @JsonSchemaInject(
- strings = Array(
- new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
- new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
- new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
- )
- )
- val tfidfTransformer: Boolean = false
-
- @JsonIgnore
- def getImportStatements = ""
-
- @JsonIgnore
- def getUserFriendlyModelName = ""
+ override def getUserFriendlyModelName = ""
override def generatePythonCode(): String =
pyb"""$getImportStatements
@@ -126,14 +68,4 @@ abstract class SklearnClassifierOpDesc extends PythonOperatorDescriptor {
),
outputPorts = List(OutputPort(blocking = true))
)
-
- override def getOutputSchemas(
- inputSchemas: Map[PortIdentity, Schema]
- ): Map[PortIdentity, Schema] = {
- Map(
- operatorInfo.outputPorts.head.id -> Schema()
- .add("model_name", AttributeType.STRING)
- .add("model", AttributeType.BINARY)
- )
- }
}
diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnModelOpDesc.scala
similarity index 65%
copy from common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala
copy to common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnModelOpDesc.scala
index a00cc7ec9c..6665477aea 100644
--- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala
+++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/SklearnModelOpDesc.scala
@@ -17,7 +17,7 @@
* under the License.
*/
-package org.apache.texera.amber.operator.sklearn.training
+package org.apache.texera.amber.operator.sklearn
import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription}
import com.kjetland.jackson.jsonSchema.annotations.{
@@ -27,18 +27,16 @@ import com.kjetland.jackson.jsonSchema.annotations.{
JsonSchemaTitle
}
import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
-import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
-import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
+import org.apache.texera.amber.core.workflow.PortIdentity
import org.apache.texera.amber.operator.PythonOperatorDescriptor
import org.apache.texera.amber.operator.metadata.annotations.{
AutofillAttributeName,
CommonOpDescAnnotation,
HideAnnotation
}
-import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
-class SklearnTrainingOpDesc extends PythonOperatorDescriptor {
+abstract class SklearnModelOpDesc extends PythonOperatorDescriptor {
@JsonSchemaTitle("Target Attribute")
@JsonPropertyDescription("Attribute in your dataset corresponding to target.")
@@ -82,39 +80,10 @@ class SklearnTrainingOpDesc extends PythonOperatorDescriptor {
var tfidfTransformer: Boolean = false
@JsonIgnore
- def getImportStatements = "from sklearn.ensemble import RandomForestClassifier"
+ def getImportStatements: String
@JsonIgnore
- def getUserFriendlyModelName = "RandomForest Training"
-
- override def generatePythonCode(): String =
- pyb"""$getImportStatements
- |from sklearn.pipeline import make_pipeline
- |from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
- |import numpy as np
- |from pytexera import *
- |class ProcessTableOperator(UDFTableOperator):
- | @overrides
- | def process_table(self, table: Table, port: int) -> Iterator[Optional[TableLike]]:
- | Y = table[$target]
- | X = table.drop($target, axis=1)
- | X = ${if (countVectorizer) pyb"X[$text]" else "X"}
- | model = make_pipeline(${if (countVectorizer) "CountVectorizer()," else ""} ${if (
- tfidfTransformer
- ) "TfidfTransformer(),"
- else ""} ${getImportStatements.split(" ").last}()).fit(X, Y)
- | yield {"model_name" : "$getUserFriendlyModelName", "model" : model}
- |
- | """.encode
-
- override def operatorInfo: OperatorInfo =
- OperatorInfo(
- getUserFriendlyModelName,
- "Sklearn " + getUserFriendlyModelName + " Operator",
- OperatorGroupConstants.SKLEARN_TRAINING_GROUP,
- inputPorts = List(InputPort(PortIdentity(), "training")),
- outputPorts = List(OutputPort(blocking = true))
- )
+ def getUserFriendlyModelName: String
override def getOutputSchemas(
inputSchemas: Map[PortIdentity, Schema]
diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala
index a00cc7ec9c..3f354cc37f 100644
--- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala
+++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/sklearn/training/SklearnTrainingOpDesc.scala
@@ -19,73 +19,16 @@
package org.apache.texera.amber.operator.sklearn.training
-import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription}
-import com.kjetland.jackson.jsonSchema.annotations.{
- JsonSchemaInject,
- JsonSchemaInt,
- JsonSchemaString,
- JsonSchemaTitle
-}
-import org.apache.texera.amber.core.tuple.{AttributeType, Schema}
import org.apache.texera.amber.pybuilder.PythonTemplateBuilder.PythonTemplateBuilderStringContext
-import org.apache.texera.amber.pybuilder.PyStringTypes.EncodableString
import org.apache.texera.amber.core.workflow.{InputPort, OutputPort, PortIdentity}
-import org.apache.texera.amber.operator.PythonOperatorDescriptor
-import org.apache.texera.amber.operator.metadata.annotations.{
- AutofillAttributeName,
- CommonOpDescAnnotation,
- HideAnnotation
-}
import org.apache.texera.amber.operator.metadata.{OperatorGroupConstants, OperatorInfo}
+import org.apache.texera.amber.operator.sklearn.SklearnModelOpDesc
-class SklearnTrainingOpDesc extends PythonOperatorDescriptor {
-
- @JsonSchemaTitle("Target Attribute")
- @JsonPropertyDescription("Attribute in your dataset corresponding to target.")
- @JsonProperty(required = true)
- @AutofillAttributeName
- var target: EncodableString = _
-
- @JsonSchemaTitle("Count Vectorizer")
- @JsonPropertyDescription("Convert a collection of text documents to a matrix of token counts.")
- @JsonProperty(defaultValue = "false")
- var countVectorizer: Boolean = false
-
- @JsonSchemaTitle("Text Attribute")
- @JsonPropertyDescription("Attribute in your dataset with text to vectorize.")
- @JsonSchemaInject(
- strings = Array(
- new JsonSchemaString(
- path = CommonOpDescAnnotation.autofill,
- value = CommonOpDescAnnotation.attributeName
- ),
- new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
- new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
- new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
- ),
- ints = Array(
- new JsonSchemaInt(path = CommonOpDescAnnotation.autofillAttributeOnPort, value = 0)
- )
- )
- var text: EncodableString = _
-
- @JsonSchemaTitle("Tfidf Transformer")
- @JsonPropertyDescription("Transform a count matrix to a normalized tf or tf-idf representation.")
- @JsonProperty(defaultValue = "false")
- @JsonSchemaInject(
- strings = Array(
- new JsonSchemaString(path = HideAnnotation.hideTarget, value = "countVectorizer"),
- new JsonSchemaString(path = HideAnnotation.hideType, value = HideAnnotation.Type.equals),
- new JsonSchemaString(path = HideAnnotation.hideExpectedValue, value = "false")
- )
- )
- var tfidfTransformer: Boolean = false
+class SklearnTrainingOpDesc extends SklearnModelOpDesc {
- @JsonIgnore
- def getImportStatements = "from sklearn.ensemble import RandomForestClassifier"
+ override def getImportStatements = ""
- @JsonIgnore
- def getUserFriendlyModelName = "RandomForest Training"
+ override def getUserFriendlyModelName = ""
override def generatePythonCode(): String =
pyb"""$getImportStatements
@@ -115,14 +58,4 @@ class SklearnTrainingOpDesc extends PythonOperatorDescriptor {
inputPorts = List(InputPort(PortIdentity(), "training")),
outputPorts = List(OutputPort(blocking = true))
)
-
- override def getOutputSchemas(
- inputSchemas: Map[PortIdentity, Schema]
- ): Map[PortIdentity, Schema] = {
- Map(
- operatorInfo.outputPorts.head.id -> Schema()
- .add("model_name", AttributeType.STRING)
- .add("model", AttributeType.BINARY)
- )
- }
}
diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/sklearn/SklearnOpDescRegistrySpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/sklearn/SklearnOpDescRegistrySpec.scala
index 29e80162d6..3c471e8ef9 100644
--- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/sklearn/SklearnOpDescRegistrySpec.scala
+++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/sklearn/SklearnOpDescRegistrySpec.scala
@@ -166,7 +166,7 @@ class SklearnOpDescRegistrySpec extends AnyFlatSpec {
val desc = new SklearnLogisticRegressionOpDesc()
desc.target = "y"
desc.countVectorizer = false
- // `tfidfTransformer` is a val on the base class, defaults to false.
+ // `tfidfTransformer` is defined on the shared base class, defaults to false.
val code = desc.generatePythonCode()
assert(code.contains("from sklearn.linear_model import LogisticRegression"))
// Classifier OpDescs emit a UDFTableOperator pipeline.
@@ -329,10 +329,10 @@ class SklearnOpDescRegistrySpec extends AnyFlatSpec {
}
}
- "SklearnTrainingOpDesc default" should "use the RandomForest defaults until a subclass overrides" in {
+ "SklearnTrainingOpDesc default" should "use empty defaults until a subclass overrides" in {
val base = new SklearnTrainingOpDesc()
- assert(base.getImportStatements == "from sklearn.ensemble import RandomForestClassifier")
- assert(base.getUserFriendlyModelName == "RandomForest Training")
+ assert(base.getImportStatements == "")
+ assert(base.getUserFriendlyModelName == "")
}
it should "embed the import statement into generatePythonCode for a concrete subclass" in {