|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.sql.execution.benchmark |
| 18 | + |
| 19 | +import org.apache.gluten.delta.DeltaDeletionVectorScanInfo |
| 20 | +import org.apache.gluten.extension.DeltaPostTransformRules |
| 21 | + |
| 22 | +import org.apache.spark.benchmark.Benchmark |
| 23 | +import org.apache.spark.sql.SparkSession |
| 24 | +import org.apache.spark.sql.delta.DeltaLog |
| 25 | + |
| 26 | +import org.apache.hadoop.fs.Path |
| 27 | + |
| 28 | +/** |
| 29 | + * Benchmark for Delta Lake planning-time operations in Gluten. |
| 30 | + * |
| 31 | + * Measures two hot paths that our performance optimizations target: |
| 32 | + * |
| 33 | + * 1. '''DV Materialization''' (`DeltaDeletionVectorScanInfo.normalize`): resolves table paths, |
| 34 | + * loads DV bitmaps from storage, and serializes them into split metadata. Our optimizations |
| 35 | + * (caching table path, Hadoop conf, DV store across files) target this path. |
| 36 | + * 2. '''Post-transform rule application''' (`DeltaPostTransformRules.rules`): traverses the |
| 37 | + * physical plan to strip DV synthetic columns, push down input_file_name, and apply column |
| 38 | + * mapping. Our optimizations (early-exit guard, shallow child check, pre-computed names, |
| 39 | + * batched attribute mapping) target this path. |
| 40 | + * |
| 41 | + * To run: |
| 42 | + * {{{ |
| 43 | + * bin/spark-submit --class org.apache.spark.sql.execution.benchmark.DeltaPlanningBenchmark \ |
| 44 | + * --jars <spark-core-test-jar>,<gluten-backends-velox-jar> |
| 45 | + * }}} |
| 46 | + * |
| 47 | + * Or via Maven (from the backends-velox module): |
| 48 | + * {{{ |
| 49 | + * ./build/mvn test -pl backends-velox -Pspark-3.5 -Pbackends-velox -Pdelta -Pjava-17 \ |
| 50 | + * -Dtest=none -DfailIfNoTests=false \ |
| 51 | + * -Dsuites="org.apache.spark.sql.execution.benchmark.DeltaPlanningBenchmark" |
| 52 | + * }}} |
| 53 | + */ |
| 54 | +object DeltaPlanningBenchmark extends SqlBasedBenchmark { |
| 55 | + |
| 56 | + override def getSparkSession: SparkSession = { |
| 57 | + SparkSession |
| 58 | + .builder() |
| 59 | + .master("local[1]") |
| 60 | + .appName("DeltaPlanningBenchmark") |
| 61 | + .config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension") |
| 62 | + .config( |
| 63 | + "spark.sql.catalog.spark_catalog", |
| 64 | + "org.apache.spark.sql.delta.catalog.DeltaCatalog") |
| 65 | + .config("spark.plugins", "org.apache.gluten.GlutenPlugin") |
| 66 | + .config("spark.memory.offHeap.enabled", "true") |
| 67 | + .config("spark.memory.offHeap.size", "1024MB") |
| 68 | + .config("spark.ui.enabled", "false") |
| 69 | + .config("spark.default.parallelism", "1") |
| 70 | + .getOrCreate() |
| 71 | + } |
| 72 | + |
| 73 | + private val numFiles = |
| 74 | + spark.sparkContext.conf.getInt("spark.gluten.benchmark.delta.numFiles", 100) |
| 75 | + private val rowsPerFile = |
| 76 | + spark.sparkContext.conf.getInt("spark.gluten.benchmark.delta.rowsPerFile", 10000) |
| 77 | + private val benchmarkIters = |
| 78 | + spark.sparkContext.conf.getInt("spark.gluten.benchmark.iterations", 5) |
| 79 | + |
| 80 | + override def runBenchmarkSuite(mainArgs: Array[String]): Unit = { |
| 81 | + runDvMaterializationBenchmark() |
| 82 | + runPostTransformRulesBenchmark() |
| 83 | + runNonDeltaRulesOverheadBenchmark() |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Benchmarks DeltaDeletionVectorScanInfo.normalize() -- the critical path that loads DVs from |
| 88 | + * storage on the driver. Measures how caching table path + DV store reduces overhead. |
| 89 | + */ |
| 90 | + private def runDvMaterializationBenchmark(): Unit = { |
| 91 | + val benchmark = new Benchmark( |
| 92 | + s"DV Materialization (normalize) - $numFiles files", |
| 93 | + numFiles.toLong, |
| 94 | + minNumIters = benchmarkIters, |
| 95 | + output = output) |
| 96 | + |
| 97 | + withDeltaTableWithDVs(numFiles, rowsPerFile) { |
| 98 | + (path, partitionedFiles) => |
| 99 | + benchmark.addCase(s"normalize() - $numFiles DV files", benchmarkIters) { |
| 100 | + _ => |
| 101 | + DeltaDeletionVectorScanInfo.normalize( |
| 102 | + partitionColumnCount = 0, |
| 103 | + partitionFiles = partitionedFiles) |
| 104 | + } |
| 105 | + |
| 106 | + benchmark.run() |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Benchmarks DeltaPostTransformRules application on a Delta plan with DV columns. Measures the |
| 112 | + * combined cost of DV stripping, input_file pushdown, and column mapping. |
| 113 | + */ |
| 114 | + private def runPostTransformRulesBenchmark(): Unit = { |
| 115 | + val benchmark = new Benchmark( |
| 116 | + "Post-transform rules (Delta plan)", |
| 117 | + 1L, |
| 118 | + minNumIters = benchmarkIters, |
| 119 | + output = output) |
| 120 | + |
| 121 | + withDeltaTableWithDVs(numFiles = 10, rowsPerFile = 1000) { |
| 122 | + (path, _) => |
| 123 | + val df = spark.read.format("delta").load(path) |
| 124 | + // Force planning to get the executed plan with DeltaScanTransformer |
| 125 | + val plan = df.queryExecution.executedPlan |
| 126 | + |
| 127 | + benchmark.addCase("apply rules (Delta plan with DV)", benchmarkIters) { |
| 128 | + _ => |
| 129 | + val result = DeltaPostTransformRules.rules.foldLeft(plan) { |
| 130 | + (p, rule) => rule(p) |
| 131 | + } |
| 132 | + // Prevent dead code elimination |
| 133 | + assert(result != null) |
| 134 | + } |
| 135 | + |
| 136 | + benchmark.run() |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Benchmarks post-transform rules on a non-Delta plan to verify zero overhead from the early-exit |
| 142 | + * guard. This is the "control" case showing that non-Delta queries don't pay for Delta rule |
| 143 | + * traversals. |
| 144 | + */ |
| 145 | + private def runNonDeltaRulesOverheadBenchmark(): Unit = { |
| 146 | + val benchmark = new Benchmark( |
| 147 | + "Post-transform rules (non-Delta plan)", |
| 148 | + 1L, |
| 149 | + minNumIters = benchmarkIters, |
| 150 | + output = output) |
| 151 | + |
| 152 | + withTempPath { |
| 153 | + p => |
| 154 | + // Create a parquet table (not Delta) |
| 155 | + val path = p.getCanonicalPath |
| 156 | + spark |
| 157 | + .range(0, 100000, 1, numPartitions = 10) |
| 158 | + .selectExpr("id", "id * 2 as value", "cast(id as string) as name") |
| 159 | + .write |
| 160 | + .parquet(path) |
| 161 | + |
| 162 | + val df = spark.read.parquet(path) |
| 163 | + val plan = df.queryExecution.executedPlan |
| 164 | + |
| 165 | + benchmark.addCase("apply rules (non-Delta parquet plan)", benchmarkIters) { |
| 166 | + _ => |
| 167 | + val result = DeltaPostTransformRules.rules.foldLeft(plan) { |
| 168 | + (p, rule) => rule(p) |
| 169 | + } |
| 170 | + assert(result != null) |
| 171 | + } |
| 172 | + |
| 173 | + benchmark.run() |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Creates a Delta table with deletion vectors and provides the partitioned files for direct DV |
| 179 | + * materialization benchmarking. |
| 180 | + */ |
| 181 | + private def withDeltaTableWithDVs(numFiles: Int, rowsPerFile: Int)( |
| 182 | + f: (String, Seq[org.apache.spark.sql.execution.datasources.PartitionedFile]) => Unit |
| 183 | + ): Unit = { |
| 184 | + withTempPath { |
| 185 | + p => |
| 186 | + val path = p.getCanonicalPath |
| 187 | + val totalRows = numFiles.toLong * rowsPerFile |
| 188 | + |
| 189 | + // Write data across multiple files |
| 190 | + spark |
| 191 | + .range(0, totalRows, 1, numPartitions = numFiles) |
| 192 | + .selectExpr("id", "id * 2 as value") |
| 193 | + .write |
| 194 | + .format("delta") |
| 195 | + .save(path) |
| 196 | + |
| 197 | + // Enable DVs and delete some rows to create DV entries |
| 198 | + spark.sql(s"""ALTER TABLE delta.`$path` |
| 199 | + SET TBLPROPERTIES ('delta.enableDeletionVectors' = true)""") |
| 200 | + // Delete ~10% of rows to generate DVs on most files |
| 201 | + spark.sql(s"DELETE FROM delta.`$path` WHERE id % 10 = 0") |
| 202 | + |
| 203 | + // Extract partitioned files with DV metadata |
| 204 | + val deltaLog = DeltaLog.forTable(spark, new Path(path)) |
| 205 | + val snapshot = deltaLog.update() |
| 206 | + val allFiles = snapshot.allFiles.collect() |
| 207 | + |
| 208 | + import org.apache.spark.paths.SparkPath |
| 209 | + import org.apache.spark.sql.catalyst.InternalRow |
| 210 | + import org.apache.spark.sql.delta.GlutenDeltaParquetFileFormat |
| 211 | + import org.apache.spark.sql.execution.datasources.PartitionedFile |
| 212 | + |
| 213 | + val partitionedFiles = allFiles.map { |
| 214 | + dataFile => |
| 215 | + val metadata: Map[String, Object] = |
| 216 | + if (dataFile.deletionVector != null) { |
| 217 | + Map( |
| 218 | + GlutenDeltaParquetFileFormat.FILE_ROW_INDEX_FILTER_ID_ENCODED -> |
| 219 | + dataFile.deletionVector.serializeToBase64(), |
| 220 | + GlutenDeltaParquetFileFormat.FILE_ROW_INDEX_FILTER_TYPE -> "IF_CONTAINED" |
| 221 | + ) |
| 222 | + } else { |
| 223 | + Map.empty[String, Object] |
| 224 | + } |
| 225 | + PartitionedFile( |
| 226 | + partitionValues = InternalRow.empty, |
| 227 | + filePath = SparkPath.fromPath(new Path(path, dataFile.path)), |
| 228 | + start = 0L, |
| 229 | + length = dataFile.size, |
| 230 | + fileSize = dataFile.size, |
| 231 | + otherConstantMetadataColumnValues = metadata |
| 232 | + ) |
| 233 | + }.toSeq |
| 234 | + |
| 235 | + f(path, partitionedFiles) |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments