WeichenXu123 commented on code in PR #40297:
URL: https://github.com/apache/spark/pull/40297#discussion_r1136648435


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/ml/MLHandler.scala:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.spark.sql.connect.ml
+
+import scala.collection.JavaConverters._
+
+import org.apache.spark.connect.proto
+import org.apache.spark.ml.Model
+import org.apache.spark.ml.param.ParamMap
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.connect.common.LiteralValueProtoConverter
+import org.apache.spark.sql.connect.service.SessionHolder
+
+object MLHandler {
+
+  def handleMlCommand(
+      sessionHolder: SessionHolder,
+      mlCommand: proto.MlCommand): proto.MlCommandResponse = {
+    mlCommand.getMlCommandTypeCase match {
+      case proto.MlCommand.MlCommandTypeCase.FIT =>
+        val fitCommandProto = mlCommand.getFit
+        val estimatorProto = fitCommandProto.getEstimator
+        assert(estimatorProto.getType == proto.MlStage.StageType.ESTIMATOR)
+
+        val algoName = fitCommandProto.getEstimator.getName
+        val algo = AlgorithmRegistry.get(algoName)
+
+        val estimator = algo.initiateEstimator(estimatorProto.getUid)
+        MLUtils.setInstanceParams(estimator, estimatorProto.getParams)
+        val dataset = MLUtils.parseRelationProto(fitCommandProto.getDataset, 
sessionHolder)
+        val model = estimator.fit(dataset).asInstanceOf[Model[_]]
+        val refId = sessionHolder.mlCache.modelCache.register(model, algo)
+
+        proto.MlCommandResponse
+          .newBuilder()
+          .setModelInfo(
+            proto.MlCommandResponse.ModelInfo.newBuilder
+              .setModelRefId(refId)
+              .setModelUid(model.uid))
+          .build()
+
+      case proto.MlCommand.MlCommandTypeCase.FETCH_MODEL_ATTR =>
+        val getModelAttrProto = mlCommand.getFetchModelAttr
+        val modelEntry = 
sessionHolder.mlCache.modelCache.get(getModelAttrProto.getModelRefId)
+        val model = modelEntry._1
+        val algo = modelEntry._2
+        algo.getModelAttr(model, getModelAttrProto.getName).left.get
+
+      case proto.MlCommand.MlCommandTypeCase.FETCH_MODEL_SUMMARY_ATTR =>
+        val getModelSummaryAttrProto = mlCommand.getFetchModelSummaryAttr
+        val modelEntry =
+          
sessionHolder.mlCache.modelCache.get(getModelSummaryAttrProto.getModelRefId)
+        val model = modelEntry._1
+        val algo = modelEntry._2
+        // Create a copied model to avoid concurrently modify model params.
+        val copiedModel = model.copy(ParamMap.empty).asInstanceOf[Model[_]]
+        MLUtils.setInstanceParams(copiedModel, 
getModelSummaryAttrProto.getParams)
+
+        val datasetOpt = if (getModelSummaryAttrProto.hasEvaluationDataset) {
+          val evalDF = MLUtils.parseRelationProto(
+            getModelSummaryAttrProto.getEvaluationDataset,
+            sessionHolder)
+          Some(evalDF)
+        } else None

Review Comment:
   But `getModelSummaryAttrProto` does not have a method `evaluationDataset` 
that returns Option[X] type value. The proto interfaces are generated in java 
style.



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to