-
Notifications
You must be signed in to change notification settings - Fork 268
perf: Implement more microbenchmarks for cast expressions #3031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9af4184
skip some CI workflows for benchmark changes
andygrove 93ab38f
implement more microbenchmarks for casts
andygrove a3d45af
fix docs
andygrove 691d7b5
Add temporal casts
andygrove 199cc43
use consistent row count
andygrove f28b984
remove legacy benchmark
andygrove 6869f79
skip failing suite
andygrove a9c3157
Merge branch 'skip-ci-bench' into cast-bench
andygrove ef6249f
Merge remote-tracking branch 'apache/main' into cast-bench
andygrove fc8b30d
address feedback
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 0 additions & 96 deletions
96
spark/src/test/scala/org/apache/spark/sql/benchmark/CometCastBenchmark.scala
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
spark/src/test/scala/org/apache/spark/sql/benchmark/CometCastBooleanBenchmark.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * 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.benchmark | ||
|
|
||
| case class CastBooleanConfig( | ||
| name: String, | ||
| query: String, | ||
| extraCometConfigs: Map[String, String] = Map.empty) | ||
|
|
||
| /** | ||
| * Benchmark to measure performance of Comet cast operations involving Boolean type. To run this | ||
| * benchmark: | ||
| * {{{ | ||
| * SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometCastBooleanBenchmark | ||
| * }}} | ||
| * Results will be written to "spark/benchmarks/CometCastBooleanBenchmark-**results.txt". | ||
| */ | ||
| object CometCastBooleanBenchmark extends CometBenchmarkBase { | ||
|
|
||
| private val castFunctions = Seq("CAST", "TRY_CAST") | ||
|
|
||
| // Boolean to String | ||
| private val boolToStringConfigs = for { | ||
| castFunc <- castFunctions | ||
| } yield CastBooleanConfig( | ||
| s"$castFunc Boolean to String", | ||
| s"SELECT $castFunc(c_bool AS STRING) FROM parquetV1Table") | ||
|
|
||
| // Boolean to numeric types | ||
| private val boolToNumericTypes = | ||
| Seq("BYTE", "SHORT", "INT", "LONG", "FLOAT", "DOUBLE", "DECIMAL(10,2)") | ||
| private val boolToNumericConfigs = for { | ||
| castFunc <- castFunctions | ||
| targetType <- boolToNumericTypes | ||
| } yield CastBooleanConfig( | ||
| s"$castFunc Boolean to $targetType", | ||
| s"SELECT $castFunc(c_bool AS $targetType) FROM parquetV1Table") | ||
|
|
||
| // Numeric to Boolean | ||
| private val numericTypes = Seq( | ||
| ("BYTE", "c_byte"), | ||
| ("SHORT", "c_short"), | ||
| ("INT", "c_int"), | ||
| ("LONG", "c_long"), | ||
| ("FLOAT", "c_float"), | ||
| ("DOUBLE", "c_double"), | ||
| ("DECIMAL(10,2)", "c_decimal")) | ||
|
|
||
| private val numericToBoolConfigs = for { | ||
| castFunc <- castFunctions | ||
| (sourceType, colName) <- numericTypes | ||
| } yield CastBooleanConfig( | ||
| s"$castFunc $sourceType to Boolean", | ||
| s"SELECT $castFunc($colName AS BOOLEAN) FROM parquetV1Table") | ||
|
|
||
| override def runCometBenchmark(mainArgs: Array[String]): Unit = { | ||
| val values = 1024 * 1024 // 1M rows | ||
|
|
||
| // Generate boolean data for boolean-to-other casts | ||
| runBenchmarkWithTable("Boolean to other types casts", values) { v => | ||
| withTempPath { dir => | ||
| withTempTable("parquetV1Table") { | ||
| // Data distribution: 1% NULL, 50/50 true/false | ||
| prepareTable( | ||
| dir, | ||
| spark.sql(s""" | ||
| SELECT CASE | ||
| WHEN value % 100 = 0 THEN NULL | ||
| ELSE (value % 2 = 0) | ||
| END AS c_bool | ||
| FROM $tbl | ||
| """)) | ||
|
|
||
| (boolToStringConfigs ++ boolToNumericConfigs).foreach { config => | ||
| runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Generate numeric data for numeric-to-boolean casts | ||
| runBenchmarkWithTable("Numeric to Boolean casts", values) { v => | ||
| withTempPath { dir => | ||
| withTempTable("parquetV1Table") { | ||
| // Data distribution: 1% NULL per column, values in {-1, 0, 1} (~33% each) | ||
| prepareTable( | ||
| dir, | ||
| spark.sql(s""" | ||
| SELECT | ||
| CASE WHEN value % 100 = 0 THEN NULL ELSE CAST((value % 3) - 1 AS BYTE) END AS c_byte, | ||
| CASE WHEN value % 100 = 1 THEN NULL ELSE CAST((value % 3) - 1 AS SHORT) END AS c_short, | ||
| CASE WHEN value % 100 = 2 THEN NULL ELSE CAST((value % 3) - 1 AS INT) END AS c_int, | ||
| CASE WHEN value % 100 = 3 THEN NULL ELSE CAST((value % 3) - 1 AS LONG) END AS c_long, | ||
| CASE WHEN value % 100 = 4 THEN NULL ELSE CAST((value % 3) - 1 AS FLOAT) END AS c_float, | ||
| CASE WHEN value % 100 = 5 THEN NULL ELSE CAST((value % 3) - 1 AS DOUBLE) END AS c_double, | ||
| CASE WHEN value % 100 = 6 THEN NULL ELSE CAST((value % 3) - 1 AS DECIMAL(10,2)) END AS c_decimal | ||
| FROM $tbl | ||
| """)) | ||
|
|
||
| numericToBoolConfigs.foreach { config => | ||
| runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
147 changes: 147 additions & 0 deletions
147
spark/src/test/scala/org/apache/spark/sql/benchmark/CometCastNumericToNumericBenchmark.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * 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.benchmark | ||
|
|
||
| case class CastNumericToNumericConfig( | ||
| name: String, | ||
| query: String, | ||
| extraCometConfigs: Map[String, String] = Map.empty) | ||
|
|
||
| /** | ||
| * Benchmark to measure performance of Comet cast between numeric types. To run this benchmark: | ||
| * {{{ | ||
| * SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometCastNumericToNumericBenchmark | ||
| * }}} | ||
| * Results will be written to "spark/benchmarks/CometCastNumericToNumericBenchmark-**results.txt". | ||
| */ | ||
| object CometCastNumericToNumericBenchmark extends CometBenchmarkBase { | ||
|
|
||
| private val castFunctions = Seq("CAST", "TRY_CAST") | ||
|
|
||
| // Integer widening conversions | ||
| private val integerWideningPairs = Seq( | ||
| ("BYTE", "c_byte", "SHORT"), | ||
| ("BYTE", "c_byte", "INT"), | ||
| ("BYTE", "c_byte", "LONG"), | ||
| ("SHORT", "c_short", "INT"), | ||
| ("SHORT", "c_short", "LONG"), | ||
| ("INT", "c_int", "LONG")) | ||
|
|
||
| // Integer narrowing conversions | ||
| private val integerNarrowingPairs = Seq( | ||
| ("LONG", "c_long", "INT"), | ||
| ("LONG", "c_long", "SHORT"), | ||
| ("LONG", "c_long", "BYTE"), | ||
| ("INT", "c_int", "SHORT"), | ||
| ("INT", "c_int", "BYTE"), | ||
| ("SHORT", "c_short", "BYTE")) | ||
|
|
||
| // Floating point conversions | ||
| private val floatPairs = Seq(("FLOAT", "c_float", "DOUBLE"), ("DOUBLE", "c_double", "FLOAT")) | ||
|
|
||
| // Integer to floating point conversions | ||
| private val intToFloatPairs = Seq( | ||
| ("BYTE", "c_byte", "FLOAT"), | ||
| ("SHORT", "c_short", "FLOAT"), | ||
| ("INT", "c_int", "FLOAT"), | ||
| ("LONG", "c_long", "FLOAT"), | ||
| ("INT", "c_int", "DOUBLE"), | ||
| ("LONG", "c_long", "DOUBLE")) | ||
|
|
||
| // Floating point to integer conversions | ||
| private val floatToIntPairs = Seq( | ||
| ("FLOAT", "c_float", "INT"), | ||
| ("FLOAT", "c_float", "LONG"), | ||
| ("DOUBLE", "c_double", "INT"), | ||
| ("DOUBLE", "c_double", "LONG")) | ||
|
|
||
| // Decimal conversions | ||
| private val decimalPairs = Seq( | ||
| ("INT", "c_int", "DECIMAL(10,2)"), | ||
| ("LONG", "c_long", "DECIMAL(20,4)"), | ||
| ("DOUBLE", "c_double", "DECIMAL(15,5)"), | ||
| ("DECIMAL(10,2)", "c_decimal", "INT"), | ||
| ("DECIMAL(10,2)", "c_decimal", "LONG"), | ||
| ("DECIMAL(10,2)", "c_decimal", "DOUBLE")) | ||
|
|
||
| private def generateConfigs( | ||
| pairs: Seq[(String, String, String)]): Seq[CastNumericToNumericConfig] = { | ||
| for { | ||
| castFunc <- castFunctions | ||
| (sourceType, colName, targetType) <- pairs | ||
| } yield CastNumericToNumericConfig( | ||
| s"$castFunc $sourceType to $targetType", | ||
| s"SELECT $castFunc($colName AS $targetType) FROM parquetV1Table") | ||
| } | ||
|
|
||
| override def runCometBenchmark(mainArgs: Array[String]): Unit = { | ||
| val values = 1024 * 1024 // 1M rows | ||
|
|
||
| // Generate input data once with all numeric types | ||
| runBenchmarkWithTable("Numeric to Numeric casts", values) { v => | ||
| withTempPath { dir => | ||
| withTempTable("parquetV1Table") { | ||
| // Data distribution: 1% NULL per column | ||
| // - c_byte: full range -64 to 63 | ||
| // - c_short: full range -16384 to 16383 | ||
| // - c_int: centered around 0 (-2.5M to +2.5M) | ||
| // - c_long: large positive values (0 to ~5 billion) | ||
| // - c_float/c_double: 4% special values (NaN/Infinity), rest centered around 0 | ||
| // - c_decimal: values from -25000.00 to +25000.00 | ||
| prepareTable( | ||
| dir, | ||
| spark.sql(s""" | ||
| SELECT | ||
| CASE WHEN value % 100 = 0 THEN NULL ELSE CAST((value % 128) - 64 AS BYTE) END AS c_byte, | ||
| CASE WHEN value % 100 = 1 THEN NULL ELSE CAST((value % 32768) - 16384 AS SHORT) END AS c_short, | ||
| CASE WHEN value % 100 = 2 THEN NULL ELSE CAST(value - 2500000 AS INT) END AS c_int, | ||
| CASE WHEN value % 100 = 3 THEN NULL ELSE CAST(value * 1000 AS LONG) END AS c_long, | ||
| CASE | ||
| WHEN value % 100 = 4 THEN NULL | ||
| WHEN value % 100 = 5 THEN CAST('NaN' AS FLOAT) | ||
| WHEN value % 100 = 6 THEN CAST('Infinity' AS FLOAT) | ||
| WHEN value % 100 = 7 THEN CAST('-Infinity' AS FLOAT) | ||
| ELSE CAST((value - 2500000) / 100.0 AS FLOAT) | ||
| END AS c_float, | ||
| CASE | ||
| WHEN value % 100 = 8 THEN NULL | ||
| WHEN value % 100 = 9 THEN CAST('NaN' AS DOUBLE) | ||
| WHEN value % 100 = 10 THEN CAST('Infinity' AS DOUBLE) | ||
| WHEN value % 100 = 11 THEN CAST('-Infinity' AS DOUBLE) | ||
| ELSE CAST((value - 2500000) / 100.0 AS DOUBLE) | ||
| END AS c_double, | ||
| CASE WHEN value % 100 = 12 THEN NULL ELSE CAST((value - 2500000) / 100.0 AS DECIMAL(10,2)) END AS c_decimal | ||
| FROM $tbl | ||
| """)) | ||
|
|
||
| // Run all benchmark categories | ||
| (generateConfigs(integerWideningPairs) ++ | ||
| generateConfigs(integerNarrowingPairs) ++ | ||
| generateConfigs(floatPairs) ++ | ||
| generateConfigs(intToFloatPairs) ++ | ||
| generateConfigs(floatToIntPairs) ++ | ||
| generateConfigs(decimalPairs)).foreach { config => | ||
| runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as in #3026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated