wangyum edited a comment on issue #24872: [SPARK-28023][SQL] Trim the string when cast string type to Boolean/Numeric types URL: https://github.com/apache/spark/pull/24872#issuecomment-503827016 Benchmark and benchmark result. ```scala /* * 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.execution.benchmark import org.apache.spark.benchmark.Benchmark /** * Benchmark trim the string when casting string type to Boolean/Numeric types. * To run this benchmark: * {{{ * 1. without sbt: * bin/spark-submit --class <this class> --jars <spark core test jar> <spark sql test jar> * 2. build/sbt "sql/test:runMain <this class>" * 3. generate result: SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "sql/test:runMain <this class>" * Results will be written to "benchmarks/CastBenchmark-results.txt". * }}} */ object CastBenchmark extends SqlBasedBenchmark { override def runBenchmarkSuite(mainArgs: Array[String]): Unit = { val title = "Benchmark trim the string" runBenchmark(title) { withTempPath { dir => val N = 500L << 13 val df = spark.range(N) val withoutWhitespace = "withoutWhitespace" val withWhitespace = "withWhitespace" val types = Seq("int", "long", "float", "double", "decimal", "boolean") df.selectExpr("cast(id as string) as str") .write.mode("overwrite").parquet(dir + withoutWhitespace) df.selectExpr(s"concat('${" " * 5}', id, '${" " * 5}') as str") .write.mode("overwrite").parquet(dir + withWhitespace) val benchmark = new Benchmark(title, N, minNumIters = 5, output = output) Seq(withoutWhitespace, withWhitespace).foreach { data => Seq(false, true).foreach { isTrimStr => val expr = types.map(t => s"cast(${if (isTrimStr) "trim(str)" else "str"} as $t) as c_$t") val name = s"$data ${if (isTrimStr) "with" else "without"} trim" benchmark.addCase(name) { _ => spark.read.parquet(dir + data).selectExpr(expr: _*).collect() } } } benchmark.run() } } } } ``` Before this pr(after SPARK-28066): ``` [info] Java HotSpot(TM) 64-Bit Server VM 1.8.0_191-b12 on Mac OS X 10.12.6 [info] Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz [info] Benchmark trim the string: Best Time(ms) Avg Time(ms) Stdev(ms) Rate(M/s) Per Row(ns) Relative [info] ------------------------------------------------------------------------------------------------------------------------ [info] withoutWhitespace without trim 7452 7945 529 0.5 1819.2 1.0X [info] withoutWhitespace with trim 8013 8289 277 0.5 1956.3 0.9X [info] withWhitespace without trim 12502 13474 1062 0.3 3052.3 0.6X [info] withWhitespace with trim 7652 8544 688 0.5 1868.1 1.0X ``` After this pr(after SPARK-28066): ``` [info] Java HotSpot(TM) 64-Bit Server VM 1.8.0_191-b12 on Mac OS X 10.12.6 [info] Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz [info] Benchmark trim the string: Best Time(ms) Avg Time(ms) Stdev(ms) Rate(M/s) Per Row(ns) Relative [info] ------------------------------------------------------------------------------------------------------------------------ [info] withoutWhitespace without trim 7467 7849 336 0.5 1823.0 1.0X [info] withoutWhitespace with trim 7445 8006 702 0.6 1817.6 1.0X [info] withWhitespace without trim 5665 6889 1295 0.7 1383.0 1.3X [info] withWhitespace with trim 5627 6201 630 0.7 1373.8 1.3X ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org