|
| 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.benchmark |
| 21 | + |
| 22 | +case class CastNumericToNumericConfig( |
| 23 | + name: String, |
| 24 | + query: String, |
| 25 | + extraCometConfigs: Map[String, String] = Map.empty) |
| 26 | + |
| 27 | +// spotless:off |
| 28 | +/** |
| 29 | + * Benchmark to measure performance of Comet cast between numeric types. To run this |
| 30 | + * benchmark: |
| 31 | + * `SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometCastNumericToNumericBenchmark` |
| 32 | + * Results will be written to "spark/benchmarks/CometCastNumericToNumericBenchmark-**results.txt". |
| 33 | + */ |
| 34 | +// spotless:on |
| 35 | +object CometCastNumericToNumericBenchmark extends CometBenchmarkBase { |
| 36 | + |
| 37 | + private val castFunctions = Seq("CAST", "TRY_CAST") |
| 38 | + |
| 39 | + // Integer widening conversions |
| 40 | + private val integerWideningPairs = Seq( |
| 41 | + ("BYTE", "c_byte", "SHORT"), |
| 42 | + ("BYTE", "c_byte", "INT"), |
| 43 | + ("BYTE", "c_byte", "LONG"), |
| 44 | + ("SHORT", "c_short", "INT"), |
| 45 | + ("SHORT", "c_short", "LONG"), |
| 46 | + ("INT", "c_int", "LONG")) |
| 47 | + |
| 48 | + // Integer narrowing conversions |
| 49 | + private val integerNarrowingPairs = Seq( |
| 50 | + ("LONG", "c_long", "INT"), |
| 51 | + ("LONG", "c_long", "SHORT"), |
| 52 | + ("LONG", "c_long", "BYTE"), |
| 53 | + ("INT", "c_int", "SHORT"), |
| 54 | + ("INT", "c_int", "BYTE"), |
| 55 | + ("SHORT", "c_short", "BYTE")) |
| 56 | + |
| 57 | + // Floating point conversions |
| 58 | + private val floatPairs = Seq(("FLOAT", "c_float", "DOUBLE"), ("DOUBLE", "c_double", "FLOAT")) |
| 59 | + |
| 60 | + // Integer to floating point conversions |
| 61 | + private val intToFloatPairs = Seq( |
| 62 | + ("BYTE", "c_byte", "FLOAT"), |
| 63 | + ("SHORT", "c_short", "FLOAT"), |
| 64 | + ("INT", "c_int", "FLOAT"), |
| 65 | + ("LONG", "c_long", "FLOAT"), |
| 66 | + ("INT", "c_int", "DOUBLE"), |
| 67 | + ("LONG", "c_long", "DOUBLE")) |
| 68 | + |
| 69 | + // Floating point to integer conversions |
| 70 | + private val floatToIntPairs = Seq( |
| 71 | + ("FLOAT", "c_float", "INT"), |
| 72 | + ("FLOAT", "c_float", "LONG"), |
| 73 | + ("DOUBLE", "c_double", "INT"), |
| 74 | + ("DOUBLE", "c_double", "LONG")) |
| 75 | + |
| 76 | + // Decimal conversions |
| 77 | + private val decimalPairs = Seq( |
| 78 | + ("INT", "c_int", "DECIMAL(10,2)"), |
| 79 | + ("LONG", "c_long", "DECIMAL(20,4)"), |
| 80 | + ("DOUBLE", "c_double", "DECIMAL(15,5)"), |
| 81 | + ("DECIMAL(10,2)", "c_decimal", "INT"), |
| 82 | + ("DECIMAL(10,2)", "c_decimal", "LONG"), |
| 83 | + ("DECIMAL(10,2)", "c_decimal", "DOUBLE")) |
| 84 | + |
| 85 | + private def generateConfigs( |
| 86 | + pairs: Seq[(String, String, String)]): Seq[CastNumericToNumericConfig] = { |
| 87 | + for { |
| 88 | + castFunc <- castFunctions |
| 89 | + (sourceType, colName, targetType) <- pairs |
| 90 | + } yield CastNumericToNumericConfig( |
| 91 | + s"$castFunc $sourceType to $targetType", |
| 92 | + s"SELECT $castFunc($colName AS $targetType) FROM parquetV1Table") |
| 93 | + } |
| 94 | + |
| 95 | + override def runCometBenchmark(mainArgs: Array[String]): Unit = { |
| 96 | + val values = getBenchmarkRows(1024 * 1024 * 5) // 5M rows default |
| 97 | + |
| 98 | + // Generate input data once with all numeric types |
| 99 | + runBenchmarkWithTable("Numeric to Numeric casts", values) { v => |
| 100 | + withTempPath { dir => |
| 101 | + withTempTable("parquetV1Table") { |
| 102 | + // Generate varied numeric data including edge cases |
| 103 | + prepareTable( |
| 104 | + dir, |
| 105 | + spark.sql(s""" |
| 106 | + SELECT |
| 107 | + CASE WHEN value % 100 = 0 THEN NULL ELSE CAST((value % 128) - 64 AS BYTE) END AS c_byte, |
| 108 | + CASE WHEN value % 100 = 1 THEN NULL ELSE CAST((value % 32768) - 16384 AS SHORT) END AS c_short, |
| 109 | + CASE WHEN value % 100 = 2 THEN NULL ELSE CAST(value - 2500000 AS INT) END AS c_int, |
| 110 | + CASE WHEN value % 100 = 3 THEN NULL ELSE CAST(value * 1000 AS LONG) END AS c_long, |
| 111 | + CASE |
| 112 | + WHEN value % 100 = 4 THEN NULL |
| 113 | + WHEN value % 100 = 5 THEN CAST('NaN' AS FLOAT) |
| 114 | + WHEN value % 100 = 6 THEN CAST('Infinity' AS FLOAT) |
| 115 | + WHEN value % 100 = 7 THEN CAST('-Infinity' AS FLOAT) |
| 116 | + ELSE CAST((value - 2500000) / 100.0 AS FLOAT) |
| 117 | + END AS c_float, |
| 118 | + CASE |
| 119 | + WHEN value % 100 = 8 THEN NULL |
| 120 | + WHEN value % 100 = 9 THEN CAST('NaN' AS DOUBLE) |
| 121 | + WHEN value % 100 = 10 THEN CAST('Infinity' AS DOUBLE) |
| 122 | + WHEN value % 100 = 11 THEN CAST('-Infinity' AS DOUBLE) |
| 123 | + ELSE CAST((value - 2500000) / 100.0 AS DOUBLE) |
| 124 | + END AS c_double, |
| 125 | + CASE WHEN value % 100 = 12 THEN NULL ELSE CAST((value - 2500000) / 100.0 AS DECIMAL(10,2)) END AS c_decimal |
| 126 | + FROM $tbl |
| 127 | + """)) |
| 128 | + |
| 129 | + // Run all benchmark categories |
| 130 | + (generateConfigs(integerWideningPairs) ++ |
| 131 | + generateConfigs(integerNarrowingPairs) ++ |
| 132 | + generateConfigs(floatPairs) ++ |
| 133 | + generateConfigs(intToFloatPairs) ++ |
| 134 | + generateConfigs(floatToIntPairs) ++ |
| 135 | + generateConfigs(decimalPairs)).foreach { config => |
| 136 | + runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments