-
Notifications
You must be signed in to change notification settings - Fork 263
chore: Add microbenchmark for casting string to numeric #2979
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * 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 | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.Cast | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| import org.apache.comet.CometConf | ||
|
|
||
| case class CastStringToNumericConfig( | ||
| name: String, | ||
| query: String, | ||
| extraCometConfigs: Map[String, String] = Map.empty) | ||
|
|
||
| // spotless:off | ||
| /** | ||
| * Benchmark to measure performance of Comet cast from String to numeric types. To run this | ||
| * benchmark: | ||
| * `SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometCastStringToNumericBenchmark` | ||
| * Results will be written to "spark/benchmarks/CometCastStringToNumericBenchmark-**results.txt". | ||
| */ | ||
| // spotless:on | ||
| object CometCastStringToNumericBenchmark extends CometBenchmarkBase { | ||
|
|
||
| private val castFunctions = Seq("CAST", "TRY_CAST") | ||
| private val targetTypes = | ||
| Seq("BOOLEAN", "BYTE", "SHORT", "INT", "LONG", "FLOAT", "DOUBLE", "DECIMAL(10,2)") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit : Perhaps one could argue that |
||
|
|
||
| private val castConfigs = for { | ||
| castFunc <- castFunctions | ||
| targetType <- targetTypes | ||
| } yield CastStringToNumericConfig( | ||
| s"$castFunc String to $targetType", | ||
| s"SELECT $castFunc(c1 AS $targetType) FROM parquetV1Table", | ||
| Map( | ||
| SQLConf.ANSI_ENABLED.key -> "false", | ||
| CometConf.getExprAllowIncompatConfigKey(classOf[Cast]) -> "true")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if we could handle the cast compatibility on a case by case basis which would help us better evaluate custom implementations vs datafusion supported operation along with unsupported cast ops . This can be done in a follow up PR (I will file an issue once this is merged) |
||
|
|
||
| override def runCometBenchmark(mainArgs: Array[String]): Unit = { | ||
| val values = 1024 * 1024 // 1M rows | ||
|
|
||
| // Generate input data once for all benchmarks | ||
| runBenchmarkWithTable("String to numeric casts", values) { v => | ||
| withTempPath { dir => | ||
| withTempTable("parquetV1Table") { | ||
| // Generate numeric strings with both integer and decimal values | ||
| // Also include some special values: nulls (~2%), NaN (~2%), Infinity (~2%) | ||
| prepareTable( | ||
| dir, | ||
| spark.sql(s""" | ||
| SELECT CASE | ||
| WHEN value % 50 = 0 THEN NULL | ||
| WHEN value % 50 = 1 THEN 'NaN' | ||
| WHEN value % 50 = 2 THEN 'Infinity' | ||
| WHEN value % 50 = 3 THEN '-Infinity' | ||
| WHEN value % 50 < 10 THEN CAST(value % 99 AS STRING) | ||
| WHEN value % 50 < 30 THEN CAST(value % 999999 AS STRING) | ||
| ELSE CAST((value - 500000) / 100.0 AS STRING) | ||
| END AS c1 | ||
| FROM $tbl | ||
| """)) | ||
|
|
||
| castConfigs.foreach { config => | ||
| runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
@andygrove nit : perhaps we might dont need to turn off spotless given that all other benchmarks dont ?
Example comment form
CometCastBenchmark