Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12259#discussion_r60783863
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/linalg/MatrixUDT.scala 
---
    @@ -0,0 +1,112 @@
    +/*
    + * 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.linalg.udt
    +
    +import org.apache.spark.ml.linalg.{DenseMatrix, Matrix, SparseMatrix}
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions.GenericMutableRow
    +import org.apache.spark.sql.catalyst.util.GenericArrayData
    +import org.apache.spark.sql.types._
    +
    +/**
    + * User-defined type for [[Matrix]] in [[mllib-local]] which allows easy 
interaction with SQL
    + * via [[org.apache.spark.sql.Dataset]].
    + */
    +private[ml] class MatrixUDT extends UserDefinedType[Matrix] {
    +
    +  override def sqlType: StructType = {
    +    // type: 0 = sparse, 1 = dense
    +    // the dense matrix is built by numRows, numCols, values and 
isTransposed, all of which are
    +    // set as not nullable, except values since in the future, support for 
binary matrices might
    +    // be added for which values are not needed.
    +    // the sparse matrix needs colPtrs and rowIndices, which are set as
    +    // null, while building the dense matrix.
    +    StructType(Seq(
    +      StructField("type", ByteType, nullable = false),
    +      StructField("numRows", IntegerType, nullable = false),
    +      StructField("numCols", IntegerType, nullable = false),
    +      StructField("colPtrs", ArrayType(IntegerType, containsNull = false), 
nullable = true),
    +      StructField("rowIndices", ArrayType(IntegerType, containsNull = 
false), nullable = true),
    +      StructField("values", ArrayType(DoubleType, containsNull = false), 
nullable = true),
    +      StructField("isTransposed", BooleanType, nullable = false)
    +      ))
    +  }
    +
    +  override def serialize(obj: Matrix): InternalRow = {
    +    val row = new GenericMutableRow(7)
    +    obj match {
    +      case sm: SparseMatrix =>
    +        row.setByte(0, 0)
    +        row.setInt(1, sm.numRows)
    +        row.setInt(2, sm.numCols)
    +        row.update(3, new 
GenericArrayData(sm.colPtrs.map(_.asInstanceOf[Any])))
    --- End diff --
    
    Not part of this PR, but we might need to fix this. It seems that it boxes 
primitive arrays. I created https://issues.apache.org/jira/browse/SPARK-14850 
to track the issue. cc: @cloud-fan 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to