dejankrak-db commented on code in PR #60741: URL: https://github.com/apache/spark/pull/60741#discussion_r4644823805
########## sql/core/src/test/scala/org/apache/spark/sql/FileTypeDatasetSuite.scala: ########## @@ -0,0 +1,119 @@ +/* + * 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.BeanProperty +import scala.jdk.CollectionConverters._ + +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types._ + +case class FileRecord(id: Int, file: FileRef, files: Seq[FileRef]) + +class FileBean extends Serializable { + @BeanProperty var id: Int = _ + @BeanProperty var file: FileRef = _ +} + +/** + * Tests for FILE values in Datasets and DataFrames: the FileRef encoder, Row.getFile and the + * paths a FILE value takes through the engine. + */ +class FileTypeDatasetSuite extends QueryTest with SharedSparkSession { + import testImplicits._ + + private val f1 = new FileRef("s3://bucket/dir/doc.pdf", 10L, 2048L, "application/pdf", "MD5:abc") + private val f2 = new FileRef("/data/b.txt", null, null, null, null) + + test("a Dataset of FileRef") { + val ds = Seq(f1, f2, null).toDS() + assert(ds.schema === new StructType().add("value", FileType(), nullable = true)) + assert(ds.collect().toSeq === Seq(f1, f2, null)) + assert(ds.map(f => if (f == null) null else f.uri).collect().toSeq === + Seq(f1.uri, f2.uri, null)) + } + + test("a Dataset of a case class with FileRef fields") { + val records = Seq(FileRecord(1, f1, Seq(f2)), FileRecord(2, null, Seq(f1, null))) + val ds = records.toDS() + assert(ds.schema("file").dataType === FileType()) + assert(ds.collect().toSeq === records) + assert(ds.filter($"id" === 2).collect().toSeq === records.tail) + val rows = ds.select($"file", $"files").collect() + assert(rows.map(_.getFile(0)).toSeq === Seq(f1, null)) + assert(rows.map(_.getSeq[FileRef](1)).toSeq === Seq(Seq(f2), Seq(f1, null))) + } + + test("a DataFrame with a FILE EXTERNAL column, read back as FileRef") { + val schema = new StructType().add("id", IntegerType).add("f", FileType.external) + val df = spark.createDataFrame(Seq(Row(1, f1), Row(2, f2), Row(3, null)).asJava, schema) + assert(df.schema === schema) + checkAnswer(df, Seq(Row(1, f1), Row(2, f2), Row(3, null))) + assert(df.collect().map(_.getFile(1)).toSeq === Seq(f1, f2, null)) + assert(df.select($"f").as[FileRef].collect().toSeq === Seq(f1, f2, null)) + } + + test("show renders a FILE value as its canonical JSON") { + val df = Seq(FileRecord(1, f2, Seq(f2))).toDF() + Seq(true, false).foreach { codegen => + withSQLConf(SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> codegen.toString) { Review Comment: Done: the show test also runs with `spark.sql.codegen.factoryMode=NO_CODEGEN`, and `ToPrettyStringSuite` has a FILE case (top level, array, map value, struct) that `checkEvaluation` runs interpreted and generated. ########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/types/ops/FileTypeOpsSuite.scala: ########## @@ -0,0 +1,310 @@ +/* + * 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.catalyst.types.ops + +import org.apache.spark.{SparkException, SparkFunSuite} +import org.apache.spark.sql.{AnalysisException, Row} +import org.apache.spark.sql.catalyst.{CatalystTypeConverters, DeserializerBuildHelper, InternalRow, SerializerBuildHelper} +import org.apache.spark.sql.catalyst.encoders.{ExpressionEncoder, RowEncoder} +import org.apache.spark.sql.catalyst.encoders.AgnosticEncoders.{FileRefEncoder, OptionEncoder} +import org.apache.spark.sql.catalyst.expressions._ +import org.apache.spark.sql.catalyst.expressions.codegen.{GenerateSafeProjection, GenerateUnsafeProjection} +import org.apache.spark.sql.catalyst.plans.SQLHelper +import org.apache.spark.sql.catalyst.types.{DataTypeUtils, PhysicalDataType, PhysicalStructType} +import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, GenericArrayData} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types._ +import org.apache.spark.unsafe.types.UTF8String + +case class FileHolder(id: Int, file: FileRef, maybe: Option[FileRef], files: Seq[FileRef]) + +/** + * Tests for FileType in the Types Framework: its physical representation, the conversions + * between [[FileRef]] and InternalRow, the row and code generation paths, formatting and the + * FileRef encoder. + */ +class FileTypeOpsSuite extends SparkFunSuite with SQLHelper { + + private val allFields = FileRefInternals.create( + "s3://bucket/dir/doc.pdf", 10L, 2048L, "application/pdf", "MD5:abc123", Array[Byte](1, 2, 3)) + + private val uriOnly = new FileRef("/data/a.txt", null, null, null, null) + + private def fileRow( + uri: String, + offset: java.lang.Long = null, + size: java.lang.Long = null, + contentType: String = null, + checksum: String = null, + inline: Array[Byte] = null): InternalRow = new GenericInternalRow(Array[Any]( + UTF8String.fromString(uri), offset, size, UTF8String.fromString(contentType), + UTF8String.fromString(checksum), inline)) + + private def assertFileRow(row: InternalRow, expected: FileRef): Unit = { + assert(FileTypeOps.toFileRef(row) === expected) + } + + // ==================== Physical Type Representation ==================== + + test("FileType has a struct physical type and row representation") { + Seq(FileType(), FileType.external).foreach { ft => + val ops = TypeOps(ft).get + assert(ops.isInstanceOf[FileTypeOps]) + assert(PhysicalDataType(ft) === PhysicalStructType(FileType.FIELDS)) + assert(ops.rowRepresentationType === Some(FileType.asStruct)) + assert(ops.getElementSize === 8) + assert(ops.getJavaClass === classOf[InternalRow]) + assert(ops.getMutableValue.isInstanceOf[MutableAny]) + assert(TypeApiOps(ft).get.isInstanceOf[FileTypeApiOps]) + } + assert(TypeOps.rowRepresentationOrSelf(FileType()) === FileType.asStruct) + assert(TypeOps.rowRepresentationOrSelf(IntegerType) === IntegerType) + } + + test("default literal") { + val lit = Literal.default(FileType.external) + assert(lit.dataType === FileType.external) + assertFileRow(lit.value.asInstanceOf[InternalRow], new FileRef("", null, null, null, null)) + } + + test("there is no Java literal") { + val e = intercept[SparkException] { + FileTypeOps(FileType()).getJavaLiteral(fileRow("/a")) + } + assert(e.getCondition === "INTERNAL_ERROR") + } + + test("literal validation") { + Literal.create(fileRow("/a"), FileType()) + intercept[IllegalArgumentException](Literal.create("/a", FileType())) + } + + // ==================== External Type Conversion ==================== + + test("FileRef to InternalRow and back keeps every field, including inline") { + val ops = FileTypeOps(FileType()) + Seq(allFields, uriOnly, new FileRef(null, null, 3L, "image/png", null), + FileRefInternals.create(null, null, null, null, null, Array[Byte](7))).foreach { f => + val row = ops.toCatalystImpl(f).asInstanceOf[InternalRow] + assert(row.numFields === FileType.FIELDS.length) + assert(ops.toScala(row) === f) + } + val row = ops.toCatalystImpl(allFields).asInstanceOf[InternalRow] + assert(row.getUTF8String(FileType.URI_INDEX).toString === "s3://bucket/dir/doc.pdf") + assert(row.getLong(FileType.OFFSET_INDEX) === 10L) + assert(row.getLong(FileType.SIZE_INDEX) === 2048L) + assert(row.getUTF8String(FileType.CONTENT_TYPE_INDEX).toString === "application/pdf") + assert(row.getUTF8String(FileType.CHECKSUM_INDEX).toString === "MD5:abc123") + assert(row.getBinary(FileType.INLINE_INDEX).toSeq === Seq[Byte](1, 2, 3)) + val nulls = ops.toCatalystImpl(uriOnly).asInstanceOf[InternalRow] + (1 until FileType.FIELDS.length).foreach(i => assert(nulls.isNullAt(i), s"field $i")) + assert(ops.toScala(null) === null) + } + + test("CatalystTypeConverters round-trip") { + val toCatalyst = CatalystTypeConverters.createToCatalystConverter(FileType.external) + val toScala = CatalystTypeConverters.createToScalaConverter(FileType.external) + assert(toScala(toCatalyst(allFields)) === allFields) + assert(toCatalyst(null) === null) + val structType = new StructType().add("f", FileType.external) + val rowToCatalyst = CatalystTypeConverters.createToCatalystConverter(structType) + val rowToScala = CatalystTypeConverters.createToScalaConverter(structType) + assert(rowToScala(rowToCatalyst(Row(allFields))) === Row(allFields)) + } + + // ==================== Row Access and Code Generation ==================== + + test("read through SpecializedGettersReader") { + val row = new GenericInternalRow(Array[Any](fileRow("/r.txt", size = 100L))) + val value = SpecializedGettersReader.read(row, 0, FileType(), true, false) + assertFileRow(value.asInstanceOf[InternalRow], new FileRef("/r.txt", null, 100L, null, null)) + } + + private val values: Seq[FileRef] = Seq(allFields, uriOnly) + + private def unsafeProjections(dt: DataType): Seq[(String, UnsafeProjection)] = { + val exprs = Seq(BoundReference(0, dt, nullable = true)) + Seq( + "interpreted" -> new InterpretedUnsafeProjection(exprs.toArray), + "codegen" -> GenerateUnsafeProjection.generate(exprs)) + } + + test("unsafe projection of a FILE column, a null value and null fields") { + val ops = FileTypeOps(FileType()) + unsafeProjections(FileType()).foreach { case (mode, projection) => + values.foreach { f => + val out = projection(new GenericInternalRow(Array[Any](ops.toCatalystImpl(f)))).copy() + assert(FileTypeOps.toFileRef(out.getStruct(0, FileType.FIELDS.length)) === f, mode) + } + assert(projection(new GenericInternalRow(Array[Any](null))).isNullAt(0), mode) + } + } + + test("unsafe projection of an array of FILE, a map with FILE values and a struct with FILE") { + val ops = FileTypeOps(FileType()) + val elems = values.map(ops.toCatalystImpl) :+ null + val arrayType = ArrayType(FileType(), containsNull = true) + unsafeProjections(arrayType).foreach { case (mode, projection) => + val out = projection(new GenericInternalRow(Array[Any](new GenericArrayData(elems)))) + val array = out.getArray(0) + assert(array.numElements() === 3, mode) + values.indices.foreach { i => + assert(FileTypeOps.toFileRef(array.getStruct(i, FileType.FIELDS.length)) === values(i)) + } + assert(array.isNullAt(2), mode) + } + val mapType = MapType(StringType, FileType(), valueContainsNull = true) + unsafeProjections(mapType).foreach { case (mode, projection) => + val map = ArrayBasedMapData( + Array[Any](UTF8String.fromString("k1"), UTF8String.fromString("k2")), + Array[Any](ops.toCatalystImpl(allFields), null)) + val out = projection(new GenericInternalRow(Array[Any](map))).getMap(0) + assert(FileTypeOps.toFileRef(out.valueArray().getStruct(0, FileType.FIELDS.length)) === + allFields, mode) + assert(out.valueArray().isNullAt(1), mode) + } + val structType = new StructType().add("id", IntegerType).add("f", FileType()) + unsafeProjections(structType).foreach { case (mode, projection) => + val inner = new GenericInternalRow(Array[Any](1, ops.toCatalystImpl(uriOnly))) + val out = projection(new GenericInternalRow(Array[Any](inner))).getStruct(0, 2) + assert(out.getInt(0) === 1, mode) + assert(FileTypeOps.toFileRef(out.getStruct(1, FileType.FIELDS.length)) === uriOnly, mode) + } + } + + test("safe projection copies a FILE value out of a reused UnsafeRow") { + val ops = FileTypeOps(FileType()) + val producer = UnsafeProjection.create(Array[DataType](FileType())) + val input = producer(new GenericInternalRow(Array[Any](ops.toCatalystImpl(allFields)))) + val exprs = Seq(BoundReference(0, FileType(), nullable = true)) + Seq( + "interpreted" -> InterpretedSafeProjection.createProjection(exprs), + "codegen" -> GenerateSafeProjection.generate(exprs)).foreach { case (mode, projection) => + val out = projection(input) + producer(new GenericInternalRow(Array[Any](ops.toCatalystImpl(uriOnly)))) + assert(FileTypeOps.toFileRef(out.getStruct(0, FileType.FIELDS.length)) === allFields, mode) + producer(new GenericInternalRow(Array[Any](ops.toCatalystImpl(allFields)))) + } + } + + // ==================== Formatting ==================== + + test("format renders the canonical JSON and omits null fields") { + val ops = FileTypeOps(FileType()) + assert(ops.format(ops.toCatalystImpl(allFields)) === + """{"uri":"s3://bucket/dir/doc.pdf","offset":10,"size":2048,""" + + """"content_type":"application/pdf","checksum":"MD5:abc123","inline":"AQID"}""") + assert(ops.format(ops.toCatalystImpl(uriOnly)) === """{"uri":"/data/a.txt"}""") + assert(ops.format(fileRow("""a "quoted"\path""")) === """{"uri":"a \"quoted\"\\path"}""") + assert(ops.toSQLValue(ops.toCatalystImpl(uriOnly)) === """FILE {"uri":"/data/a.txt"}""") + // The external value renders the same way. + assert(new FileTypeApiOps(FileType()).format(allFields) === + ops.format(ops.toCatalystImpl(allFields))) + assert(ops.formatExternal(uriOnly) === Some("""{"uri":"/data/a.txt"}""")) + } + + test("FileRef equality, hash code and string representation") { + val same = FileRefInternals.create( + "s3://bucket/dir/doc.pdf", 10L, 2048L, "application/pdf", "MD5:abc123", Array[Byte](1, 2, 3)) + assert(allFields === same) + assert(allFields.hashCode === same.hashCode) + assert(allFields !== FileRefInternals.create( + "s3://bucket/dir/doc.pdf", 10L, 2048L, "application/pdf", "MD5:abc123", Array[Byte](1, 2))) + assert(uriOnly !== new FileRef("/data/a.txt", 0L, null, null, null)) + assert(allFields.toString === "FileRef{uri='s3://bucket/dir/doc.pdf', offset=10, size=2048, " + + "contentType='application/pdf', checksum='MD5:abc123', inline=<3 bytes>}") + assert(new FileRef(null, null, null, null, null).toString === "FileRef{}") + } + + // ==================== Encoders ==================== + + test("encoders of FileRef, of a case class with FileRef fields and of a Row") { + val enc = ExpressionEncoder[FileRef]().resolveAndBind() + assert(enc.schema === new StructType().add("value", FileType(), nullable = true)) + values.foreach { f => + assert(enc.createDeserializer()(enc.createSerializer()(f)) === f) + } + + val holderEnc = ExpressionEncoder[FileHolder]().resolveAndBind() + assert(holderEnc.schema("file").dataType === FileType()) + assert(holderEnc.schema("files").dataType === ArrayType(FileType(), containsNull = true)) + Seq(FileHolder(1, allFields, Some(uriOnly), Seq(uriOnly, null)), + FileHolder(2, null, None, Nil)).foreach { h => + assert(holderEnc.createDeserializer()(holderEnc.createSerializer()(h).copy()) === h) + } + + val schema = new StructType().add("f", FileType.external).add("fs", ArrayType(FileType())) + val rowEnc = ExpressionEncoder(RowEncoder.encoderFor(schema)).resolveAndBind() Review Comment: Done: the test asserts the Row encoder's schema. ########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/types/ops/TypeOps.scala: ########## @@ -329,10 +329,18 @@ object TypeOps { case tt: TimeType => Some(TimeTypeOps(tt)) case t: TimestampNTZNanosType => Some(TimestampNTZNanosTypeOps(t)) case t: TimestampLTZNanosType => Some(TimestampLTZNanosTypeOps(t)) + case ft: FileType => Some(FileTypeOps(ft)) // Add new types here - single registration point case ft: TestableFrameworkType => Some(ft.createTypeOps()) case _ => None } + + /** + * Returns the row representation of `dt` (see [[TypeOps.rowRepresentationType]]), or `dt` + * itself when it has none. For callers in Java, such as the column vectors. Review Comment: Done: the note now limits the gap to array- and map-backed types. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
