Github user dbtsai commented on a diff in the pull request:
https://github.com/apache/spark/pull/12259#discussion_r60190633
--- Diff: mllib/src/main/scala/org/apache/spark/ml/linalg/VectorUDT.scala
---
@@ -0,0 +1,103 @@
+/*
+ * 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.annotation.AlphaComponent
+import org.apache.spark.ml.linalg.{DenseVector, SparseVector, Vector}
+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._
+
+/**
+ * :: AlphaComponent ::
+ *
+ * User-defined type for [[Vector]] in [[mllib-local]] which allows easy
interaction with SQL
+ * via [[org.apache.spark.sql.Dataset]].
+ */
+@AlphaComponent
+private[ml] class VectorUDT extends UserDefinedType[Vector] {
+
+ override def sqlType: StructType = {
+ // type: 0 = sparse, 1 = dense
+ // We only use "values" for dense vectors, and "size", "indices", and
"values" for sparse
+ // vectors. The "values" field is nullable because we might want to
add binary vectors later,
+ // which uses "size" and "indices", but not "values".
+ StructType(Seq(
+ StructField("type", ByteType, nullable = false),
+ StructField("size", IntegerType, nullable = true),
+ StructField("indices", ArrayType(IntegerType, containsNull = false),
nullable = true),
+ StructField("values", ArrayType(DoubleType, containsNull = false),
nullable = true)))
+ }
+
+ override def serialize(obj: Vector): InternalRow = {
+ obj match {
+ case SparseVector(size, indices, values) =>
+ val row = new GenericMutableRow(4)
+ row.setByte(0, 0)
+ row.setInt(1, size)
+ row.update(2, new
GenericArrayData(indices.map(_.asInstanceOf[Any])))
+ row.update(3, new
GenericArrayData(values.map(_.asInstanceOf[Any])))
+ row
+ case DenseVector(values) =>
+ val row = new GenericMutableRow(4)
+ row.setByte(0, 1)
+ row.setNullAt(1)
+ row.setNullAt(2)
+ row.update(3, new
GenericArrayData(values.map(_.asInstanceOf[Any])))
+ row
+ }
+ }
+
+ override def deserialize(datum: Any): Vector = {
+ datum match {
+ case row: InternalRow =>
+ require(row.numFields == 4,
+ s"VectorUDT.deserialize given row with length ${row.numFields}
but requires length == 4")
+ val tpe = row.getByte(0)
+ tpe match {
+ case 0 =>
+ val size = row.getInt(1)
+ val indices = row.getArray(2).toIntArray()
+ val values = row.getArray(3).toDoubleArray()
+ new SparseVector(size, indices, values)
+ case 1 =>
+ val values = row.getArray(3).toDoubleArray()
+ new DenseVector(values)
+ }
+ }
+ }
+
+ override def pyUDT: String = "pyspark.mllib.linalg.VectorUDT"
--- End diff --
ditto
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]