Github user mengxr commented on a diff in the pull request: https://github.com/apache/spark/pull/12259#discussion_r60785238 --- Diff: sql/core/src/test/scala/org/apache/spark/sql/UDTRegistrationSuite.scala --- @@ -0,0 +1,304 @@ +/* + * 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 + +import scala.beans.{BeanInfo, BeanProperty} + +import org.apache.spark.SparkException +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.CatalystTypeConverters +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.expressions.GenericMutableRow +import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData} +import org.apache.spark.sql.execution.datasources.parquet.ParquetTest +import org.apache.spark.sql.functions._ +import org.apache.spark.sql.test.SharedSQLContext +import org.apache.spark.sql.types._ + +private[sql] trait DVector extends Serializable + +private[sql] class MyDVector(val data: Array[Double]) extends DVector with Serializable { + override def equals(other: Any): Boolean = other match { + case v: MyDVector => + java.util.Arrays.equals(this.data, v.data) + case _ => false + } +} + +private[sql] class MyDVector2(val data: Array[Double]) extends DVector with Serializable { + override def equals(other: Any): Boolean = other match { + case v: MyDVector2 => + java.util.Arrays.equals(this.data, v.data) + case _ => false + } +} + +object MyDVector { + def unapply(dv: MyDVector): Option[Array[Double]] = Some(dv.data) +} + +object MyDVector2 { + def unapply(dv: MyDVector2): Option[Array[Double]] = Some(dv.data) +} + +@BeanInfo +private[sql] case class MyPoint( + @BeanProperty label: Double, + @BeanProperty features: DVector) + +private[sql] class TestUserClass { +} + +private[sql] class NonUserDefinedType { +} + +private[sql] class MyDVectorUDT extends UserDefinedType[DVector] { + + override def sqlType: DataType = { + // type: 0 = MyDVector, 1 = MyDVector2 + StructType(Seq( + StructField("type", ByteType, nullable = false), + StructField("values", ArrayType(DoubleType, containsNull = false), nullable = false))) + } + + override def serialize(features: DVector): InternalRow = { + val row = new GenericMutableRow(2) + features match { + case MyDVector(data) => + row.setByte(0, 0) + row.update(1, new GenericArrayData(data.map(_.asInstanceOf[Any]))) + case MyDVector2(data) => + row.setByte(0, 1) + row.update(1, new GenericArrayData(data.map(_.asInstanceOf[Any]))) + } + row + } + + override def deserialize(datum: Any): DVector = { + datum match { + case row: InternalRow => + require(row.numFields == 2, "MyDVectorUDT.deserialize given row with length " + + s"${row.numFields} but requires length == 2") + val tpe = row.getByte(0) + tpe match { + case 0 => + val values = row.getArray(1).toDoubleArray() + new MyDVector(values) + case 1 => + val values = row.getArray(1).toDoubleArray() + new MyDVector2(values) + } + } + } + + override def userClass: Class[DVector] = classOf[DVector] + + private[spark] override def asNullable: MyDVectorUDT = this + + override def equals(other: Any): Boolean = other match { + case _: MyDVectorUDT => true + case _ => false + } +} --- End diff -- I don't see why we need this setup to unit test UDTRegistration. I would test the following: 1. default values (see my previous comments) 2. register a class 3. query a registered class 4. query a unregistered class We do need to test new `VectorUDT` and `MatrixUDT`. That should be in a unit test under MLlib, not SQL, which we already covered in this PR.
--- 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