You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "WeichenXu123 (via GitHub)" <gi...@apache.org> on 2023/03/20 00:58:35 UTC

[GitHub] [spark] WeichenXu123 opened a new pull request, #40479: [CONNECT][ML][WIP] Spark connect ml scala 1

WeichenXu123 opened a new pull request, #40479:
URL: https://github.com/apache/spark/pull/40479

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] WeichenXu123 commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "WeichenXu123 (via GitHub)" <gi...@apache.org>.
WeichenXu123 commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1141524125


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/ConnectUtils.scala:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.ml
+
+import org.apache.spark.connect.proto
+import org.apache.spark.ml.linalg.{Matrix, Vector, Matrices, Vectors}
+import org.apache.spark.ml.param.Params
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.common.{InvalidPlanInput, LiteralValueProtoConverter}
+import org.apache.spark.sql.types.Decimal
+import org.apache.spark.unsafe.types.CalendarInterval
+
+import scala.collection.mutable
+import scala.reflect.ClassTag
+
+object ConnectUtils {
+
+  def getInstanceParamsProto(instance: Params): proto.MlParams = {
+    val builder = proto.MlParams.newBuilder()
+
+    for (param <- instance.params) {
+      instance.get(param).map { value =>
+        builder.putParams(
+          param.name,
+          LiteralValueProtoConverter.toLiteralProto(value)
+        )
+      }
+      instance.getDefault(param).map { value =>
+        builder.putParams(
+          param.name,
+          LiteralValueProtoConverter.toLiteralProto(value)
+        )
+      }
+    }
+    builder.build()
+  }
+
+  def serializeResponseValue(data: Any): proto.MlCommandResponse = {
+    data match {
+      case v: Vector => serializeVector(v)
+      case v: Matrix => serializeMatrix(v)
+      case _: Byte | _: Short | _: Int | _: Long | _: Float | _: Double | _: Boolean | _: String |
+           _: Array[_] =>
+        proto.MlCommandResponse
+          .newBuilder()
+          .setLiteral(LiteralValueProtoConverter.toLiteralProto(data))
+          .build()
+      case _ =>
+        throw new IllegalArgumentException()
+    }
+  }
+
+  def serializeVector(data: Vector): proto.MlCommandResponse = {
+    // TODO: Support sparse
+    val values = data.toArray
+    val denseBuilder = proto.Vector.Dense.newBuilder()
+    for (i <- 0 until values.length) {
+      denseBuilder.addValue(values(i))
+    }
+
+    proto.MlCommandResponse
+      .newBuilder()
+      .setVector(proto.Vector.newBuilder().setDense(denseBuilder))
+      .build()
+  }
+
+  def deserializeVector(protoValue: proto.Vector): Vector = {
+    // TODO: Support sparse
+    Vectors.dense(
+      protoValue.getDense.getValueList.stream().mapToDouble(_.doubleValue()).toArray
+    )
+  }
+
+  def deserializeMatrix(protoValue: proto.Matrix): Matrix = {
+    // TODO: Support sparse
+    val denseProto = protoValue.getDense
+    Matrices.dense(
+      denseProto.getNumRows,
+      denseProto.getNumCols,
+      denseProto.getValueList.stream().mapToDouble(_.doubleValue()).toArray
+    )
+  }
+
+  def serializeMatrix(data: Matrix): proto.MlCommandResponse = {
+    // TODO: Support sparse
+    // TODO: optimize transposed case
+    val denseBuilder = proto.Matrix.Dense.newBuilder()
+    val values = data.toArray
+    for (i <- 0 until values.length) {
+      denseBuilder.addValue(values(i))
+    }
+    denseBuilder.setNumCols(data.numCols)
+    denseBuilder.setNumRows(data.numRows)
+    denseBuilder.setIsTransposed(false)
+    proto.MlCommandResponse
+      .newBuilder()
+      .setMatrix(proto.Matrix.newBuilder().setDense(denseBuilder))
+      .build()
+  }
+
+  def deserializeResponseValue(protoValue: proto.MlCommandResponse): Any = {
+    protoValue.getMlCommandResponseTypeCase match {
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.LITERAL =>
+        deserializeLiteral(protoValue.getLiteral)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.VECTOR =>
+        deserializeVector(protoValue.getVector)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.MATRIX =>
+        deserializeMatrix(protoValue.getMatrix)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.MODEL_REF =>
+        ModelRef.fromProto(protoValue.getModelRef)
+      case _ =>
+        throw new IllegalArgumentException()
+    }
+  }
+
+  def deserializeLiteral(protoValue: proto.Expression.Literal): Any = {

Review Comment:
   @zhengruifeng Could you help move this utility function to "common" project ?



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] zhengruifeng commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1141950975


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/classification/ProbabilisticClassifier.scala:
##########
@@ -123,4 +92,25 @@ abstract class ProbabilisticClassificationModel[
     //  then invoke the 'predictProbability' method of the remote model
     throw new NotImplementedError
   }
+
+  /**
+   *If the probability and prediction columns are set, this method returns the current model,
+   * otherwise it generates new columns for them and sets them as columns on a new copy of
+   * the current model
+   */
+  override private[classification] def findSummaryModel():

Review Comment:
   I think we don't needs any private helper functions like this?



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] github-actions[bot] commented on pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #40479:
URL: https://github.com/apache/spark/pull/40479#issuecomment-1612267661

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] zhengruifeng commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1141916289


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/ConnectUtils.scala:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.ml
+
+import org.apache.spark.connect.proto
+import org.apache.spark.ml.linalg.{Matrix, Vector, Matrices, Vectors}
+import org.apache.spark.ml.param.Params
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.common.{InvalidPlanInput, LiteralValueProtoConverter}
+import org.apache.spark.sql.types.Decimal
+import org.apache.spark.unsafe.types.CalendarInterval
+
+import scala.collection.mutable
+import scala.reflect.ClassTag
+
+object ConnectUtils {
+
+  def getInstanceParamsProto(instance: Params): proto.MlParams = {
+    val builder = proto.MlParams.newBuilder()
+
+    for (param <- instance.params) {
+      instance.get(param).map { value =>
+        builder.putParams(
+          param.name,
+          LiteralValueProtoConverter.toLiteralProto(value)
+        )
+      }
+      instance.getDefault(param).map { value =>
+        builder.putParams(
+          param.name,
+          LiteralValueProtoConverter.toLiteralProto(value)
+        )
+      }
+    }
+    builder.build()
+  }
+
+  def serializeResponseValue(data: Any): proto.MlCommandResponse = {
+    data match {
+      case v: Vector => serializeVector(v)
+      case v: Matrix => serializeMatrix(v)
+      case _: Byte | _: Short | _: Int | _: Long | _: Float | _: Double | _: Boolean | _: String |
+           _: Array[_] =>
+        proto.MlCommandResponse
+          .newBuilder()
+          .setLiteral(LiteralValueProtoConverter.toLiteralProto(data))
+          .build()
+      case _ =>
+        throw new IllegalArgumentException()
+    }
+  }
+
+  def serializeVector(data: Vector): proto.MlCommandResponse = {
+    // TODO: Support sparse
+    val values = data.toArray
+    val denseBuilder = proto.Vector.Dense.newBuilder()
+    for (i <- 0 until values.length) {
+      denseBuilder.addValue(values(i))
+    }
+
+    proto.MlCommandResponse
+      .newBuilder()
+      .setVector(proto.Vector.newBuilder().setDense(denseBuilder))
+      .build()
+  }
+
+  def deserializeVector(protoValue: proto.Vector): Vector = {
+    // TODO: Support sparse
+    Vectors.dense(
+      protoValue.getDense.getValueList.stream().mapToDouble(_.doubleValue()).toArray
+    )
+  }
+
+  def deserializeMatrix(protoValue: proto.Matrix): Matrix = {
+    // TODO: Support sparse
+    val denseProto = protoValue.getDense
+    Matrices.dense(
+      denseProto.getNumRows,
+      denseProto.getNumCols,
+      denseProto.getValueList.stream().mapToDouble(_.doubleValue()).toArray
+    )
+  }
+
+  def serializeMatrix(data: Matrix): proto.MlCommandResponse = {
+    // TODO: Support sparse
+    // TODO: optimize transposed case
+    val denseBuilder = proto.Matrix.Dense.newBuilder()
+    val values = data.toArray
+    for (i <- 0 until values.length) {
+      denseBuilder.addValue(values(i))
+    }
+    denseBuilder.setNumCols(data.numCols)
+    denseBuilder.setNumRows(data.numRows)
+    denseBuilder.setIsTransposed(false)
+    proto.MlCommandResponse
+      .newBuilder()
+      .setMatrix(proto.Matrix.newBuilder().setDense(denseBuilder))
+      .build()
+  }
+
+  def deserializeResponseValue(protoValue: proto.MlCommandResponse): Any = {
+    protoValue.getMlCommandResponseTypeCase match {
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.LITERAL =>
+        deserializeLiteral(protoValue.getLiteral)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.VECTOR =>
+        deserializeVector(protoValue.getVector)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.MATRIX =>
+        deserializeMatrix(protoValue.getMatrix)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.MODEL_REF =>
+        ModelRef.fromProto(protoValue.getModelRef)
+      case _ =>
+        throw new IllegalArgumentException()
+    }
+  }
+
+  def deserializeLiteral(protoValue: proto.Expression.Literal): Any = {

Review Comment:
   https://github.com/apache/spark/pull/40485



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] WeichenXu123 commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "WeichenXu123 (via GitHub)" <gi...@apache.org>.
WeichenXu123 commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1142163720


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/classification/ProbabilisticClassifier.scala:
##########
@@ -123,4 +92,25 @@ abstract class ProbabilisticClassificationModel[
     //  then invoke the 'predictProbability' method of the remote model
     throw new NotImplementedError
   }
+
+  /**
+   *If the probability and prediction columns are set, this method returns the current model,
+   * otherwise it generates new columns for them and sets them as columns on a new copy of
+   * the current model
+   */
+  override private[classification] def findSummaryModel():

Review Comment:
   I don't think so, it is used here:
   
   ```
     def evaluate(dataset: Dataset[_]): LogisticRegressionSummary = {
       // Handle possible missing or invalid prediction columns
       val (summaryModel, probabilityColName, predictionColName) = findSummaryModel()
       ...
   ```
   and this part code we should run it in client side.



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] zhengruifeng commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1141947414


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/Pipeline.scala:
##########
@@ -17,47 +17,13 @@
 
 package org.apache.spark.ml
 
-import org.apache.spark.annotation.DeveloperApi
 import org.apache.spark.internal.Logging
 import org.apache.spark.ml.param.{ParamMap, Params}
-import org.apache.spark.sql.types.StructType
 
 /**
  * A stage in a pipeline, either an [[Estimator]] or a [[Transformer]].
  */
 abstract class PipelineStage extends Params with Logging {
 
-  /**
-   * Check transform validity and derive the output schema from the input schema.
-   *
-   * We check validity for interactions between parameters during `transformSchema` and raise an
-   * exception if any parameter value is invalid. Parameter value checks which do not depend on
-   * other parameters are handled by `Param.validate()`.
-   *
-   * Typical implementation should first conduct verification on schema change and parameter
-   * validity, including complex parameter interaction checks.
-   */
-  def transformSchema(schema: StructType): StructType

Review Comment:
   why removing the `transformSchema` method?
   it seems in SCSC, it can be implemented with functions in `mllib-common`



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] WeichenXu123 commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "WeichenXu123 (via GitHub)" <gi...@apache.org>.
WeichenXu123 commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1142159747


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/Pipeline.scala:
##########
@@ -17,47 +17,13 @@
 
 package org.apache.spark.ml
 
-import org.apache.spark.annotation.DeveloperApi
 import org.apache.spark.internal.Logging
 import org.apache.spark.ml.param.{ParamMap, Params}
-import org.apache.spark.sql.types.StructType
 
 /**
  * A stage in a pipeline, either an [[Estimator]] or a [[Transformer]].
  */
 abstract class PipelineStage extends Params with Logging {
 
-  /**
-   * Check transform validity and derive the output schema from the input schema.
-   *
-   * We check validity for interactions between parameters during `transformSchema` and raise an
-   * exception if any parameter value is invalid. Parameter value checks which do not depend on
-   * other parameters are handled by `Param.validate()`.
-   *
-   * Typical implementation should first conduct verification on schema change and parameter
-   * validity, including complex parameter interaction checks.
-   */
-  def transformSchema(schema: StructType): StructType

Review Comment:
   I think we should run `transformSchema` in server side, we don't need to run it in client side.



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] zhengruifeng commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1141810112


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/ConnectUtils.scala:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.ml
+
+import org.apache.spark.connect.proto
+import org.apache.spark.ml.linalg.{Matrix, Vector, Matrices, Vectors}
+import org.apache.spark.ml.param.Params
+import org.apache.spark.sql.catalyst.util.{DateTimeUtils, IntervalUtils}
+import org.apache.spark.sql.connect.common.{InvalidPlanInput, LiteralValueProtoConverter}
+import org.apache.spark.sql.types.Decimal
+import org.apache.spark.unsafe.types.CalendarInterval
+
+import scala.collection.mutable
+import scala.reflect.ClassTag
+
+object ConnectUtils {
+
+  def getInstanceParamsProto(instance: Params): proto.MlParams = {
+    val builder = proto.MlParams.newBuilder()
+
+    for (param <- instance.params) {
+      instance.get(param).map { value =>
+        builder.putParams(
+          param.name,
+          LiteralValueProtoConverter.toLiteralProto(value)
+        )
+      }
+      instance.getDefault(param).map { value =>
+        builder.putParams(
+          param.name,
+          LiteralValueProtoConverter.toLiteralProto(value)
+        )
+      }
+    }
+    builder.build()
+  }
+
+  def serializeResponseValue(data: Any): proto.MlCommandResponse = {
+    data match {
+      case v: Vector => serializeVector(v)
+      case v: Matrix => serializeMatrix(v)
+      case _: Byte | _: Short | _: Int | _: Long | _: Float | _: Double | _: Boolean | _: String |
+           _: Array[_] =>
+        proto.MlCommandResponse
+          .newBuilder()
+          .setLiteral(LiteralValueProtoConverter.toLiteralProto(data))
+          .build()
+      case _ =>
+        throw new IllegalArgumentException()
+    }
+  }
+
+  def serializeVector(data: Vector): proto.MlCommandResponse = {
+    // TODO: Support sparse
+    val values = data.toArray
+    val denseBuilder = proto.Vector.Dense.newBuilder()
+    for (i <- 0 until values.length) {
+      denseBuilder.addValue(values(i))
+    }
+
+    proto.MlCommandResponse
+      .newBuilder()
+      .setVector(proto.Vector.newBuilder().setDense(denseBuilder))
+      .build()
+  }
+
+  def deserializeVector(protoValue: proto.Vector): Vector = {
+    // TODO: Support sparse
+    Vectors.dense(
+      protoValue.getDense.getValueList.stream().mapToDouble(_.doubleValue()).toArray
+    )
+  }
+
+  def deserializeMatrix(protoValue: proto.Matrix): Matrix = {
+    // TODO: Support sparse
+    val denseProto = protoValue.getDense
+    Matrices.dense(
+      denseProto.getNumRows,
+      denseProto.getNumCols,
+      denseProto.getValueList.stream().mapToDouble(_.doubleValue()).toArray
+    )
+  }
+
+  def serializeMatrix(data: Matrix): proto.MlCommandResponse = {
+    // TODO: Support sparse
+    // TODO: optimize transposed case
+    val denseBuilder = proto.Matrix.Dense.newBuilder()
+    val values = data.toArray
+    for (i <- 0 until values.length) {
+      denseBuilder.addValue(values(i))
+    }
+    denseBuilder.setNumCols(data.numCols)
+    denseBuilder.setNumRows(data.numRows)
+    denseBuilder.setIsTransposed(false)
+    proto.MlCommandResponse
+      .newBuilder()
+      .setMatrix(proto.Matrix.newBuilder().setDense(denseBuilder))
+      .build()
+  }
+
+  def deserializeResponseValue(protoValue: proto.MlCommandResponse): Any = {
+    protoValue.getMlCommandResponseTypeCase match {
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.LITERAL =>
+        deserializeLiteral(protoValue.getLiteral)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.VECTOR =>
+        deserializeVector(protoValue.getVector)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.MATRIX =>
+        deserializeMatrix(protoValue.getMatrix)
+      case proto.MlCommandResponse.MlCommandResponseTypeCase.MODEL_REF =>
+        ModelRef.fromProto(protoValue.getModelRef)
+      case _ =>
+        throw new IllegalArgumentException()
+    }
+  }
+
+  def deserializeLiteral(protoValue: proto.Expression.Literal): Any = {

Review Comment:
   a util function convert `proto.Expression.Literal` to `Any` value?
   let me take a look



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] WeichenXu123 commented on a diff in pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "WeichenXu123 (via GitHub)" <gi...@apache.org>.
WeichenXu123 commented on code in PR #40479:
URL: https://github.com/apache/spark/pull/40479#discussion_r1142784162


##########
connector/connect/client/jvm/src/main/scala/org/apache/spark/ml/Pipeline.scala:
##########
@@ -17,47 +17,13 @@
 
 package org.apache.spark.ml
 
-import org.apache.spark.annotation.DeveloperApi
 import org.apache.spark.internal.Logging
 import org.apache.spark.ml.param.{ParamMap, Params}
-import org.apache.spark.sql.types.StructType
 
 /**
  * A stage in a pipeline, either an [[Estimator]] or a [[Transformer]].
  */
 abstract class PipelineStage extends Params with Logging {
 
-  /**
-   * Check transform validity and derive the output schema from the input schema.
-   *
-   * We check validity for interactions between parameters during `transformSchema` and raise an
-   * exception if any parameter value is invalid. Parameter value checks which do not depend on
-   * other parameters are handled by `Param.validate()`.
-   *
-   * Typical implementation should first conduct verification on schema change and parameter
-   * validity, including complex parameter interaction checks.
-   */
-  def transformSchema(schema: StructType): StructType

Review Comment:
   Similarly, in pyspark side, the `Transformer/ JavaTransformer` also does not do any schema transformation.



-- 
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-unsubscribe@spark.apache.org

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


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


[GitHub] [spark] github-actions[bot] closed pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #40479: [CONNECT][ML][WIP] Spark connect ML for scala client
URL: https://github.com/apache/spark/pull/40479


-- 
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-unsubscribe@spark.apache.org

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


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