zhengruifeng commented on code in PR #49601:
URL: https://github.com/apache/spark/pull/49601#discussion_r1926895418


##########
sql/connect/server/src/main/scala/org/apache/spark/sql/connect/ml/MLUtils.scala:
##########
@@ -448,98 +454,149 @@ private[ml] object MLUtils {
   // Since we're using reflection way to get the attribute, in order not to
   // leave a security hole, we define an allowed attribute list that can be 
accessed.
   // The attributes could be retrieved from the corresponding python class
-  private lazy val ALLOWED_ATTRIBUTES = HashSet(
-    "mean", // StandardScalerModel
-    "std", // StandardScalerModel
-    "maxAbs", // MaxAbsScalerModel
-    "originalMax", // MinMaxScalerModel
-    "originalMin", // MinMaxScalerModel
-    "range", // RobustScalerModel
-    "median", // RobustScalerModel
-    "toString",
-    "toDebugString",
-    "numFeatures",
-    "predict", // PredictionModel
-    "predictLeaf", // Tree models
-    "numClasses",
-    "depth", // DecisionTreeClassificationModel
-    "numNodes", // Tree models
-    "totalNumNodes", // Tree models
-    "javaTreeWeights", // Tree models
-    "treeWeights", // Tree models
-    "featureImportances", // Tree models
-    "predictRaw", // ClassificationModel
-    "predictProbability", // ProbabilisticClassificationModel
-    "scale", // LinearRegressionModel
-    "coefficients",
-    "intercept",
-    "coefficientMatrix",
-    "interceptVector", // LogisticRegressionModel
-    "summary",
-    "hasSummary",
-    "evaluate", // LogisticRegressionModel
-    "evaluateEachIteration", // GBTClassificationModel
-    "predictions",
-    "predictionCol",
-    "labelCol",
-    "weightCol",
-    "labels", // _ClassificationSummary
-    "truePositiveRateByLabel",
-    "falsePositiveRateByLabel", // _ClassificationSummary
-    "precisionByLabel",
-    "recallByLabel",
-    "fMeasureByLabel",
-    "accuracy", // _ClassificationSummary
-    "weightedTruePositiveRate",
-    "weightedFalsePositiveRate", // _ClassificationSummary
-    "weightedRecall",
-    "weightedPrecision",
-    "weightedFMeasure", // _ClassificationSummary
-    "scoreCol",
-    "roc",
-    "areaUnderROC",
-    "pr",
-    "fMeasureByThreshold", // _BinaryClassificationSummary
-    "precisionByThreshold",
-    "recallByThreshold", // _BinaryClassificationSummary
-    "probabilityCol",
-    "featuresCol", // LogisticRegressionSummary
-    "objectiveHistory",
-    "coefficientStandardErrors", // _TrainingSummary
-    "degreesOfFreedom", // LinearRegressionSummary
-    "devianceResiduals", // LinearRegressionSummary
-    "explainedVariance", // LinearRegressionSummary
-    "meanAbsoluteError", // LinearRegressionSummary
-    "meanSquaredError", // LinearRegressionSummary
-    "numInstances", // LinearRegressionSummary
-    "pValues", // LinearRegressionSummary
-    "r2", // LinearRegressionSummary
-    "r2adj", // LinearRegressionSummary
-    "residuals", // LinearRegressionSummary
-    "rootMeanSquaredError", // LinearRegressionSummary
-    "tValues", // LinearRegressionSummary
-    "totalIterations", // LinearRegressionSummary
-    "k", // KMeansSummary
-    "numIter", // KMeansSummary
-    "clusterSizes", // KMeansSummary
-    "trainingCost", // KMeansSummary
-    "cluster", // KMeansSummary
-    "computeCost", // BisectingKMeansModel
-    "rank", // ALSModel
-    "itemFactors", // ALSModel
-    "userFactors", // ALSModel
-    "recommendForAllUsers", // ALSModel
-    "recommendForAllItems", // ALSModel
-    "recommendForUserSubset", // ALSModel
-    "recommendForItemSubset", // ALSModel
-    "associationRules", // FPGrowthModel
-    "freqItemsets" // FPGrowthModel
-  )
+  private lazy val ALLOWED_ATTRIBUTES = Seq(
+    (classOf[Identifiable], Set("toString")),
+
+    // Model Traits
+    (classOf[PredictionModel[_, _]], Set("predict", "numFeatures")),
+    (classOf[ClassificationModel[_, _]], Set("predictRaw", "numClasses")),
+    (classOf[ProbabilisticClassificationModel[_, _]], 
Set("predictProbability")),
+
+    // Summary Traits
+    (classOf[HasTrainingSummary[_]], Set("hasSummary", "summary")),
+    (classOf[TrainingSummary], Set("objectiveHistory", "totalIterations")),
+    (
+      classOf[ClassificationSummary],
+      Set(
+        "predictions",
+        "predictionCol",
+        "labelCol",
+        "weightCol",
+        "labels",
+        "truePositiveRateByLabel",
+        "falsePositiveRateByLabel",
+        "precisionByLabel",
+        "recallByLabel",
+        "fMeasureByLabel",
+        "accuracy",
+        "weightedTruePositiveRate",
+        "weightedFalsePositiveRate",
+        "weightedRecall",
+        "weightedPrecision",
+        "weightedFMeasure",
+        "weightedFMeasure")),
+    (
+      classOf[BinaryClassificationSummary],
+      Set(
+        "scoreCol",
+        "roc",
+        "areaUnderROC",
+        "pr",
+        "fMeasureByThreshold",
+        "precisionByThreshold",
+        "recallByThreshold")),
+    (
+      classOf[ClusteringSummary],
+      Set(
+        "predictions",
+        "predictionCol",
+        "featuresCol",
+        "k",
+        "numIter",
+        "cluster",
+        "clusterSizes")),
+
+    // Tree Models
+    (classOf[DecisionTreeModel], Set("predictLeaf", "numNodes", "depth", 
"toDebugString")),
+    (
+      classOf[TreeEnsembleModel[_]],
+      Set(
+        "predictLeaf",
+        "trees",
+        "treeWeights",
+        "javaTreeWeights",
+        "getNumTrees",
+        "totalNumNodes",
+        "toDebugString")),
+    (classOf[DecisionTreeClassificationModel], Set("featureImportances")),
+    (classOf[RandomForestClassificationModel], Set("featureImportances", 
"evaluate")),
+    (classOf[GBTClassificationModel], Set("featureImportances", 
"evaluateEachIteration")),
+    (classOf[DecisionTreeRegressionModel], Set("featureImportances")),
+    (classOf[RandomForestRegressionModel], Set("featureImportances")),
+    (classOf[GBTRegressionModel], Set("featureImportances", 
"evaluateEachIteration")),
+
+    // Classification Models
+    (
+      classOf[LogisticRegressionModel],
+      Set("intercept", "coefficients", "interceptVector", "coefficientMatrix", 
"evaluate")),
+    (classOf[LogisticRegressionSummary], Set("probabilityCol", "featuresCol")),
+    (classOf[BinaryLogisticRegressionSummary], Set("scoreCol")),
+
+    // Regression Models
+    (classOf[LinearRegressionModel], Set("intercept", "coefficients", "scale", 
"evaluate")),
+    (
+      classOf[LinearRegressionSummary],
+      Set(
+        "predictions",
+        "predictionCol",
+        "labelCol",
+        "featuresCol",
+        "explainedVariance",
+        "meanAbsoluteError",
+        "meanSquaredError",
+        "rootMeanSquaredError",
+        "r2",
+        "r2adj",
+        "residuals",
+        "numInstances",
+        "degreesOfFreedom",
+        "devianceResiduals",
+        "coefficientStandardErrors",
+        "tValues",
+        "pValues")),
+    (classOf[LinearRegressionTrainingSummary], Set("objectiveHistory", 
"totalIterations")),
+
+    // Clustering Models
+    (classOf[KMeansModel], Set("predict", "numFeatures", "clusterCenters")),
+    (classOf[KMeansSummary], Set("trainingCost")),
+    (
+      classOf[BisectingKMeansModel],
+      Set("predict", "numFeatures", "clusterCenters", "computeCost")),
+    (classOf[BisectingKMeansSummary], Set("trainingCost")),
+
+    // Recommendation Models
+    (
+      classOf[ALSModel],
+      Set(
+        "rank",
+        "itemFactors",
+        "userFactors",
+        "recommendForAllUsers",
+        "recommendForAllItems",
+        "recommendForUserSubset",
+        "recommendForItemSubset")),
+
+    // Association Rules
+    (classOf[FPGrowthModel], Set("associationRules", "freqItemsets")),
+
+    // Feature Models
+    (classOf[StandardScalerModel], Set("mean", "std")),
+    (classOf[MaxAbsScalerModel], Set("maxAbs")),
+    (classOf[MinMaxScalerModel], Set("originalMax", "originalMin")),
+    (classOf[RobustScalerModel], Set("range", "median")))
+
+  private def validate(obj: Any, method: String): Unit = {
+    assert(obj != null)
+    val valid = ALLOWED_ATTRIBUTES.exists { case (cls, methods) =>
+      cls.isInstance(obj) && methods.contains(method)

Review Comment:
   Make sense, let's document it



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to