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 + withoutWhitespace).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                     7049           
7594         571          0.6        1720.9       1.0X
   [info] withoutWhitespace with trim                        7083           
7571         440          0.6        1729.2       1.0X
   [info] withWhitespace without trim                        5343           
5409          47          0.8        1304.4       1.3X
   [info] withWhitespace with trim                           5476           
5536          70          0.7        1337.0       1.3X
   ```
   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                     7165           
8269        1023          0.6        1749.2       1.0X
   [info] withoutWhitespace with trim                        6000           
7498        1074          0.7        1464.8       1.2X
   [info] withWhitespace without trim                        5171           
6195        1038          0.8        1262.4       1.4X
   [info] withWhitespace with trim                           4858           
6608         NaN          0.8        1186.0       1.5X
   ```

----------------------------------------------------------------
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

Reply via email to