|
| 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.comet |
| 21 | + |
| 22 | +import java.io.File |
| 23 | + |
| 24 | +import scala.util.Random |
| 25 | + |
| 26 | +import org.scalactic.source.Position |
| 27 | +import org.scalatest.Tag |
| 28 | + |
| 29 | +import org.apache.commons.io.FileUtils |
| 30 | +import org.apache.spark.sql.CometTestBase |
| 31 | +import org.apache.spark.sql.comet.{CometNativeScanExec, CometScanExec} |
| 32 | +import org.apache.spark.sql.execution.SparkPlan |
| 33 | +import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper |
| 34 | +import org.apache.spark.sql.internal.SQLConf |
| 35 | + |
| 36 | +import org.apache.comet.testing.{DataGenOptions, ParquetGenerator} |
| 37 | + |
| 38 | +class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper { |
| 39 | + |
| 40 | + private var filename: String = null |
| 41 | + |
| 42 | + /** |
| 43 | + * We use Asia/Kathmandu because it has a non-zero number of minutes as the offset, so is an |
| 44 | + * interesting edge case. Also, this timezone tends to be different from the default system |
| 45 | + * timezone. |
| 46 | + * |
| 47 | + * Represents UTC+5:45 |
| 48 | + */ |
| 49 | + private val defaultTimezone = "Asia/Kathmandu" |
| 50 | + |
| 51 | + override def beforeAll(): Unit = { |
| 52 | + super.beforeAll() |
| 53 | + val tempDir = System.getProperty("java.io.tmpdir") |
| 54 | + filename = s"$tempDir/CometFuzzTestSuite_${System.currentTimeMillis()}.parquet" |
| 55 | + val random = new Random(42) |
| 56 | + withSQLConf( |
| 57 | + CometConf.COMET_ENABLED.key -> "false", |
| 58 | + SQLConf.SESSION_LOCAL_TIMEZONE.key -> defaultTimezone) { |
| 59 | + val options = |
| 60 | + DataGenOptions(generateArray = true, generateStruct = true, generateNegativeZero = false) |
| 61 | + ParquetGenerator.makeParquetFile(random, spark, filename, 1000, options) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + protected override def afterAll(): Unit = { |
| 66 | + super.afterAll() |
| 67 | + FileUtils.deleteDirectory(new File(filename)) |
| 68 | + } |
| 69 | + |
| 70 | + test("select *") { |
| 71 | + val df = spark.read.parquet(filename) |
| 72 | + df.createOrReplaceTempView("t1") |
| 73 | + val sql = "SELECT * FROM t1" |
| 74 | + if (CometConf.isExperimentalNativeScan) { |
| 75 | + checkSparkAnswerAndOperator(sql) |
| 76 | + } else { |
| 77 | + checkSparkAnswer(sql) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + test("select * with limit") { |
| 82 | + val df = spark.read.parquet(filename) |
| 83 | + df.createOrReplaceTempView("t1") |
| 84 | + val sql = "SELECT * FROM t1 LIMIT 500" |
| 85 | + if (CometConf.isExperimentalNativeScan) { |
| 86 | + checkSparkAnswerAndOperator(sql) |
| 87 | + } else { |
| 88 | + checkSparkAnswer(sql) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + test("order by single column") { |
| 93 | + val df = spark.read.parquet(filename) |
| 94 | + df.createOrReplaceTempView("t1") |
| 95 | + for (col <- df.columns) { |
| 96 | + val sql = s"SELECT $col FROM t1 ORDER BY $col" |
| 97 | + // cannot run fully natively due to range partitioning and sort |
| 98 | + val (_, cometPlan) = checkSparkAnswer(sql) |
| 99 | + if (CometConf.isExperimentalNativeScan) { |
| 100 | + assert(1 == collectNativeScans(cometPlan).length) |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + test("count distinct") { |
| 106 | + val df = spark.read.parquet(filename) |
| 107 | + df.createOrReplaceTempView("t1") |
| 108 | + for (col <- df.columns) { |
| 109 | + val sql = s"SELECT count(distinct $col) FROM t1" |
| 110 | + val (_, cometPlan) = checkSparkAnswer(sql) |
| 111 | + if (CometConf.isExperimentalNativeScan) { |
| 112 | + assert(1 == collectNativeScans(cometPlan).length) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + test("order by multiple columns") { |
| 118 | + val df = spark.read.parquet(filename) |
| 119 | + df.createOrReplaceTempView("t1") |
| 120 | + val allCols = df.columns.mkString(",") |
| 121 | + val sql = s"SELECT $allCols FROM t1 ORDER BY $allCols" |
| 122 | + // cannot run fully natively due to range partitioning and sort |
| 123 | + val (_, cometPlan) = checkSparkAnswer(sql) |
| 124 | + if (CometConf.isExperimentalNativeScan) { |
| 125 | + assert(1 == collectNativeScans(cometPlan).length) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + test("aggregate group by single column") { |
| 130 | + val df = spark.read.parquet(filename) |
| 131 | + df.createOrReplaceTempView("t1") |
| 132 | + for (col <- df.columns) { |
| 133 | + // cannot run fully natively due to range partitioning and sort |
| 134 | + val sql = s"SELECT $col, count(*) FROM t1 GROUP BY $col ORDER BY $col" |
| 135 | + val (_, cometPlan) = checkSparkAnswer(sql) |
| 136 | + if (CometConf.isExperimentalNativeScan) { |
| 137 | + assert(1 == collectNativeScans(cometPlan).length) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + test("min/max aggregate") { |
| 143 | + val df = spark.read.parquet(filename) |
| 144 | + df.createOrReplaceTempView("t1") |
| 145 | + for (col <- df.columns) { |
| 146 | + // cannot run fully native due to HashAggregate |
| 147 | + val sql = s"SELECT min($col), max($col) FROM t1" |
| 148 | + val (_, cometPlan) = checkSparkAnswer(sql) |
| 149 | + if (CometConf.isExperimentalNativeScan) { |
| 150 | + assert(1 == collectNativeScans(cometPlan).length) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + test("join") { |
| 156 | + val df = spark.read.parquet(filename) |
| 157 | + df.createOrReplaceTempView("t1") |
| 158 | + df.createOrReplaceTempView("t2") |
| 159 | + for (col <- df.columns) { |
| 160 | + // cannot run fully native due to HashAggregate |
| 161 | + val sql = s"SELECT count(*) FROM t1 JOIN t2 ON t1.$col = t2.$col" |
| 162 | + val (_, cometPlan) = checkSparkAnswer(sql) |
| 163 | + if (CometConf.isExperimentalNativeScan) { |
| 164 | + assert(2 == collectNativeScans(cometPlan).length) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + override protected def test(testName: String, testTags: Tag*)(testFun: => Any)(implicit |
| 170 | + pos: Position): Unit = { |
| 171 | + Seq("native", "jvm").foreach { shuffleMode => |
| 172 | + Seq("native_comet", "native_datafusion", "native_iceberg_compat").foreach { scanImpl => |
| 173 | + super.test(testName + s" ($scanImpl, $shuffleMode shuffle)", testTags: _*) { |
| 174 | + withSQLConf( |
| 175 | + CometConf.COMET_NATIVE_SCAN_IMPL.key -> scanImpl, |
| 176 | + CometConf.COMET_SCAN_ALLOW_INCOMPATIBLE.key -> "true", |
| 177 | + CometConf.COMET_SHUFFLE_MODE.key -> shuffleMode) { |
| 178 | + testFun |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private def collectNativeScans(plan: SparkPlan): Seq[SparkPlan] = { |
| 186 | + collect(plan) { |
| 187 | + case scan: CometScanExec => scan |
| 188 | + case scan: CometNativeScanExec => scan |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | +} |
0 commit comments