Skip to content
Closed
10 changes: 10 additions & 0 deletions mllib/src/main/scala/org/apache/spark/ml/feature/RFormula.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.apache.spark.ml.util._
import org.apache.spark.sql.{DataFrame, Dataset}
import org.apache.spark.sql.functions.col
import org.apache.spark.sql.types._
import org.apache.spark.util.SizeEstimator

/**
* Base trait for [[RFormula]] and [[RFormulaModel]].
Expand Down Expand Up @@ -354,6 +355,15 @@ class RFormulaModel private[feature](
// For ml connect only
private[ml] def this() = this("", null, null)

private[spark] override def estimatedSize: Long = {
var size = estimateMatadataSize
// ResolvedRFormula(label: String, terms: Seq[Seq[String]], hasIntercept: Boolean)
size += SizeEstimator.estimate(resolvedFormula)
// pipelineModel: PipelineModel
size += pipelineModel.estimatedSize
size
}

@Since("2.0.0")
override def transform(dataset: Dataset[_]): DataFrame = {
checkCanTransform(dataset.schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class RFormulaSuite extends MLTest with DefaultReadWriteTest {
ParamsSuite.checkParams(new RFormula())
}

test("RFormulaModel estimated size") {
val dataset = Seq(("a", 1.0), ("b", 2.0), ("c", 3.0)).toDF("category", "label")
val model = new RFormula().setFormula("label ~ category").fit(dataset)
val maxSize = 1024 * 16
assert(
model.estimatedSize < maxSize,
s"Estimation (${model.estimatedSize}) should be less than $maxSize")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test asserts only an upper bound (estimatedSize < 16KiB). It genuinely fails without the fix (the default walk blows past 16KiB via SparkSession), so it is a real regression guard, but a buggy implementation returning 0 or a too-small value would also pass. Please add a lower bound to match that standard from predecessor PRs.


test("transform numeric data") {
val formula = new RFormula().setFormula("id ~ v1 + v2")
val original = Seq((0, 1.0, 3.0), (2, 2.0, 5.0)).toDF("id", "v1", "v2")
Expand Down