|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark.sql.comet |
| 21 | + |
| 22 | +import java.sql.Timestamp |
| 23 | +import java.util.UUID |
| 24 | + |
| 25 | +import org.apache.spark.sql.{CometTestBase, Row} |
| 26 | +import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow} |
| 27 | +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, InterpretedUnsafeProjection, UnsafeProjection} |
| 28 | +import org.apache.spark.sql.execution.WholeStageCodegenExec |
| 29 | +import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper |
| 30 | +import org.apache.spark.sql.internal.SQLConf |
| 31 | +import org.apache.spark.sql.types._ |
| 32 | + |
| 33 | +class CometUnsafeProjectionSuite extends CometTestBase with AdaptiveSparkPlanHelper { |
| 34 | + |
| 35 | + /** A schema with every writer GenerateUnsafeProjection nests: structs, arrays and maps. */ |
| 36 | + private def schema(nestedName: String = "a"): StructType = StructType( |
| 37 | + Seq( |
| 38 | + StructField("i", IntegerType), |
| 39 | + StructField("s", StringType), |
| 40 | + StructField("d", DecimalType(38, 10)), |
| 41 | + StructField( |
| 42 | + "st", |
| 43 | + StructType( |
| 44 | + Seq(StructField(nestedName, LongType), StructField("b", ArrayType(StringType))))), |
| 45 | + StructField( |
| 46 | + "m", |
| 47 | + MapType(StringType, ArrayType(StructType(Seq(StructField("x", DoubleType)))))), |
| 48 | + StructField("aa", ArrayType(ArrayType(IntegerType))), |
| 49 | + StructField("bin", BinaryType), |
| 50 | + StructField("ts", TimestampType))) |
| 51 | + |
| 52 | + private def attributes(nestedName: String = "a"): Seq[AttributeReference] = |
| 53 | + schema(nestedName).fields.toSeq.map(f => AttributeReference(f.name, f.dataType, f.nullable)()) |
| 54 | + |
| 55 | + /** Attributes whose layout no earlier call has cached, since a nested name is part of it. */ |
| 56 | + private def newLayout(): Seq[AttributeReference] = |
| 57 | + attributes(s"a_${UUID.randomUUID().toString.replace("-", "")}") |
| 58 | + |
| 59 | + private lazy val rows: Seq[InternalRow] = { |
| 60 | + val toCatalyst = CatalystTypeConverters.createToCatalystConverter(schema()) |
| 61 | + Seq( |
| 62 | + Row( |
| 63 | + 1, |
| 64 | + "a", |
| 65 | + BigDecimal("1.5"), |
| 66 | + Row(2L, Seq("x", null)), |
| 67 | + Map("k" -> Seq(Row(1.0), null)), |
| 68 | + Seq(Seq(1, null), null), |
| 69 | + Array[Byte](1, 2), |
| 70 | + new Timestamp(0)), |
| 71 | + Row(null, null, null, null, null, null, null, null), |
| 72 | + Row( |
| 73 | + 3, |
| 74 | + "字" * 100, |
| 75 | + BigDecimal("-1234567890123456789012345678.1234567890"), |
| 76 | + Row(null, Seq.empty), |
| 77 | + Map.empty, |
| 78 | + Seq.empty, |
| 79 | + Array.emptyByteArray, |
| 80 | + null)).map(toCatalyst(_).asInstanceOf[InternalRow]) |
| 81 | + } |
| 82 | + |
| 83 | + private def withCodegenOnly(confs: (String, String)*)(f: => Unit): Unit = |
| 84 | + withSQLConf((SQLConf.CODEGEN_FACTORY_MODE.key -> "CODEGEN_ONLY") +: confs: _*)(f) |
| 85 | + |
| 86 | + test("rows match UnsafeProjection.create") { |
| 87 | + // A small threshold splits the field writes into many methods. |
| 88 | + Seq("1024", "64").foreach { threshold => |
| 89 | + withCodegenOnly(SQLConf.CODEGEN_METHOD_SPLIT_THRESHOLD.key -> threshold) { |
| 90 | + val attrs = newLayout() |
| 91 | + val expected = UnsafeProjection.create(attrs, attrs) |
| 92 | + val actual = CometUnsafeProjection.create(attrs) |
| 93 | + rows.foreach(row => assert(actual(row) == expected(row))) |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + test("NO_CODEGEN creates an interpreted projection") { |
| 99 | + withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> "NO_CODEGEN") { |
| 100 | + val attrs = attributes() |
| 101 | + val projection = CometUnsafeProjection.create(attrs) |
| 102 | + assert(projection.isInstanceOf[InterpretedUnsafeProjection]) |
| 103 | + val expected = UnsafeProjection.create(attrs, attrs) |
| 104 | + rows.foreach(row => assert(projection(row) == expected(row))) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + test("generates the class once per layout and a new projection per call") { |
| 109 | + withCodegenOnly() { |
| 110 | + val attrs = newLayout() |
| 111 | + val before = CometUnsafeProjection.generatedClassCount |
| 112 | + val first = CometUnsafeProjection.create(attrs) |
| 113 | + val second = CometUnsafeProjection.create(attrs) |
| 114 | + assert(CometUnsafeProjection.generatedClassCount == before + 1) |
| 115 | + assert(first ne second) |
| 116 | + // Each projection writes into its own row, so the first row survives the second call. |
| 117 | + val firstRow = first(rows.head) |
| 118 | + val secondRow = second(rows(1)) |
| 119 | + val expected = UnsafeProjection.create(attrs, attrs) |
| 120 | + assert(firstRow == expected(rows.head)) |
| 121 | + assert(secondRow == expected(rows(1))) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + test("a different method split threshold generates its own class") { |
| 126 | + val attrs = newLayout() |
| 127 | + val before = CometUnsafeProjection.generatedClassCount |
| 128 | + Seq("1024", "64", "1024").foreach { threshold => |
| 129 | + withCodegenOnly(SQLConf.CODEGEN_METHOD_SPLIT_THRESHOLD.key -> threshold) { |
| 130 | + CometUnsafeProjection.create(attrs) |
| 131 | + } |
| 132 | + } |
| 133 | + assert(CometUnsafeProjection.generatedClassCount == before + 2) |
| 134 | + } |
| 135 | + |
| 136 | + test("keeps the most recently used classes") { |
| 137 | + withCodegenOnly() { |
| 138 | + val layouts = Seq.fill(CometUnsafeProjection.MaxCachedClasses + 1) { |
| 139 | + val name = s"f_${UUID.randomUUID().toString.replace("-", "")}" |
| 140 | + Seq(AttributeReference("c", StructType(Seq(StructField(name, IntegerType))))()) |
| 141 | + } |
| 142 | + layouts.foreach(CometUnsafeProjection.create) |
| 143 | + val before = CometUnsafeProjection.generatedClassCount |
| 144 | + CometUnsafeProjection.create(layouts.last) |
| 145 | + assert(CometUnsafeProjection.generatedClassCount == before) |
| 146 | + CometUnsafeProjection.create(layouts.head) |
| 147 | + assert(CometUnsafeProjection.generatedClassCount == before + 1) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + test("columnar to row outside whole-stage codegen reuses the generated class") { |
| 152 | + withTempPath { dir => |
| 153 | + val path = dir.getCanonicalPath |
| 154 | + spark |
| 155 | + .range(200) |
| 156 | + .selectExpr( |
| 157 | + "id", |
| 158 | + "named_struct('a', id, 'b', array(cast(id as string), null)) as st", |
| 159 | + "map(cast(id as string), array(id)) as m") |
| 160 | + .repartition(4) |
| 161 | + .write |
| 162 | + .parquet(path) |
| 163 | + // More fields than this keep a plan out of whole-stage codegen, as a wide schema would. |
| 164 | + withSQLConf(SQLConf.WHOLESTAGE_MAX_NUM_FIELDS.key -> "2") { |
| 165 | + val df = spark.read.parquet(path) |
| 166 | + checkSparkAnswerAndOperator(df) |
| 167 | + val plan = df.queryExecution.executedPlan |
| 168 | + assert(collect(plan) { case c: CometColumnarToRowExec => c }.nonEmpty, plan) |
| 169 | + assert(collect(plan) { case w: WholeStageCodegenExec => w }.isEmpty, plan) |
| 170 | + // The first run generated the class, so another run over every partition generates none. |
| 171 | + val before = CometUnsafeProjection.generatedClassCount |
| 172 | + assert(df.collect().length == 200) |
| 173 | + assert(CometUnsafeProjection.generatedClassCount == before) |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments