|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark.sql.benchmark |
| 21 | + |
| 22 | +case class CastTemporalToTemporalConfig( |
| 23 | + name: String, |
| 24 | + query: String, |
| 25 | + extraCometConfigs: Map[String, String] = Map.empty) |
| 26 | + |
| 27 | +/** |
| 28 | + * Benchmark to measure performance of Comet cast between temporal types. To run this benchmark: |
| 29 | + * {{{ |
| 30 | + * SPARK_GENERATE_BENCHMARK_FILES=1 make benchmark-org.apache.spark.sql.benchmark.CometCastTemporalToTemporalBenchmark |
| 31 | + * }}} |
| 32 | + * Results will be written to |
| 33 | + * "spark/benchmarks/CometCastTemporalToTemporalBenchmark-**results.txt". |
| 34 | + */ |
| 35 | +object CometCastTemporalToTemporalBenchmark extends CometBenchmarkBase { |
| 36 | + |
| 37 | + private val castFunctions = Seq("CAST", "TRY_CAST") |
| 38 | + |
| 39 | + // Date to Timestamp |
| 40 | + private val dateToTimestampConfigs = for { |
| 41 | + castFunc <- castFunctions |
| 42 | + } yield CastTemporalToTemporalConfig( |
| 43 | + s"$castFunc Date to Timestamp", |
| 44 | + s"SELECT $castFunc(c_date AS TIMESTAMP) FROM parquetV1Table") |
| 45 | + |
| 46 | + // Timestamp to Date |
| 47 | + private val timestampToDateConfigs = for { |
| 48 | + castFunc <- castFunctions |
| 49 | + } yield CastTemporalToTemporalConfig( |
| 50 | + s"$castFunc Timestamp to Date", |
| 51 | + s"SELECT $castFunc(c_timestamp AS DATE) FROM parquetV1Table") |
| 52 | + |
| 53 | + override def runCometBenchmark(mainArgs: Array[String]): Unit = { |
| 54 | + val values = 1024 * 1024 * 5 // 5M rows |
| 55 | + |
| 56 | + // Generate DATE data for Date -> Timestamp benchmarks |
| 57 | + runBenchmarkWithTable("Date to Timestamp casts", values) { v => |
| 58 | + withTempPath { dir => |
| 59 | + withTempTable("parquetV1Table") { |
| 60 | + prepareTable( |
| 61 | + dir, |
| 62 | + spark.sql(s""" |
| 63 | + SELECT CASE |
| 64 | + WHEN value % 100 = 0 THEN NULL |
| 65 | + ELSE DATE_ADD('2020-01-01', CAST(value % 3650 AS INT)) |
| 66 | + END AS c_date |
| 67 | + FROM $tbl |
| 68 | + """)) |
| 69 | + |
| 70 | + dateToTimestampConfigs.foreach { config => |
| 71 | + runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Generate TIMESTAMP data for Timestamp -> Date benchmarks |
| 78 | + runBenchmarkWithTable("Timestamp to Date casts", values) { v => |
| 79 | + withTempPath { dir => |
| 80 | + withTempTable("parquetV1Table") { |
| 81 | + prepareTable( |
| 82 | + dir, |
| 83 | + spark.sql(s""" |
| 84 | + SELECT CASE |
| 85 | + WHEN value % 100 = 0 THEN NULL |
| 86 | + ELSE TIMESTAMP_MICROS(1577836800000000 + value % 31536000000000) |
| 87 | + END AS c_timestamp |
| 88 | + FROM $tbl |
| 89 | + """)) |
| 90 | + |
| 91 | + timestampToDateConfigs.foreach { config => |
| 92 | + runExpressionBenchmark(config.name, v, config.query, config.extraCometConfigs) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments