Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

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

/**
 * Benchmark to measure Comet execution performance. To run this benchmark:
 * {{{
 *   SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometCastBenchmark
 * }}}
 *
 * Results will be written to "spark/benchmarks/CometCastBenchmark-**results.txt".
 */

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)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit : Perhaps one could argue that Boolean isn't necessarily a numeric input ? Also , could we add some more higher precision + scale decimals too ?

https://spark.apache.org/docs/latest/sql-ref-datatypes.html


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"))
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
}
}
}
}
}
}
Loading