-
Notifications
You must be signed in to change notification settings - Fork 205
feat: Add aggregate expression fuzz testing in CI #1374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 7 commits
57300a6
9aa97e2
a780771
6668bf4
2977d8b
f73dea1
b6a7cda
4dfbce2
142b74a
5c4e7a0
0030f22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* 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.comet | ||
|
||
import scala.util.Random | ||
|
||
import org.apache.hadoop.fs.Path | ||
import org.apache.spark.sql.CometTestBase | ||
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
import org.apache.spark.sql.types._ | ||
|
||
import org.apache.comet.testing.{DataGenOptions, ParquetGenerator} | ||
|
||
class CometFuzzTestSuite extends CometTestBase with AdaptiveSparkPlanHelper { | ||
|
||
private val fuzzTestEnabled: Boolean = sys.env.contains("COMET_FUZZ_TEST") | ||
|
||
test("aggregates") { | ||
assume(fuzzTestEnabled) | ||
withTempDir { dir => | ||
val path = new Path(dir.toURI.toString, "test.parquet") | ||
val filename = path.toString | ||
val random = new Random(42) | ||
withSQLConf(CometConf.COMET_ENABLED.key -> "false") { | ||
ParquetGenerator.makeParquetFile(random, spark, filename, 10000, DataGenOptions()) | ||
} | ||
val table = spark.read.parquet(filename).coalesce(1) | ||
table.createOrReplaceTempView("t1") | ||
|
||
val groupingFields: Array[StructField] = table.schema.fields.filterNot(isNumeric) | ||
|
||
// test grouping by each non-numeric column, grouping by all non-numeric columns, and no grouping | ||
val groupByIndividualCols: Seq[Seq[String]] = groupingFields.map(f => Seq(f.name)).toSeq | ||
val groupByAllCols: Seq[Seq[String]] = Seq(groupingFields.map(_.name).toSeq) | ||
val noGroup: Seq[Seq[String]] = Seq(Seq.empty) | ||
val groupings: Seq[Seq[String]] = groupByIndividualCols ++ groupByAllCols ++ noGroup | ||
|
||
val scanTypes = Seq( | ||
CometConf.SCAN_NATIVE_COMET | ||
/*CometConf.SCAN_NATIVE_DATAFUSION, | ||
CometConf.SCAN_NATIVE_ICEBERG_COMPAT*/ ) | ||
|
||
for (scan <- scanTypes) { | ||
for (shuffleMode <- Seq("auto", "jvm", "native")) { | ||
withSQLConf( | ||
CometConf.COMET_NATIVE_SCAN_IMPL.key -> scan, | ||
CometConf.COMET_SHUFFLE_MODE.key -> shuffleMode) { | ||
for (group <- groupings) { | ||
for (agg <- Exprs.aggregate) { | ||
|
||
// pick all compatible columns for all input args | ||
val argFields: Seq[Array[StructField]] = agg.args.map(argType => | ||
table.schema.fields.filter(f => isMatch(f.dataType, argType))) | ||
|
||
// just pick the first compatible column for each type for now, but should randomize this or | ||
// test all combinations | ||
val args: Seq[StructField] = argFields.map(_.head) | ||
|
||
if (agg.name == "_avg" && args.head.dataType.isInstanceOf[DecimalType]) { | ||
// skip known issue | ||
} else { | ||
|
||
val aggSql = s"${agg.name}(${args.map(_.name).mkString(",")})" | ||
|
||
val sql = if (group.isEmpty) { | ||
s"SELECT $aggSql FROM t1" | ||
} else { | ||
val groupCols = group.mkString(", ") | ||
s"SELECT $groupCols, $aggSql FROM t1 GROUP BY $groupCols ORDER BY $groupCols" | ||
} | ||
println(sql) | ||
checkSparkAnswerWithTol(sql) | ||
// TODO check operators | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private def isNumeric(field: StructField) = { | ||
field.dataType match { | ||
case _: ByteType | _: ShortType | _: IntegerType | _: LongType => true | ||
case _: FloatType | _: DoubleType => true | ||
case _: DecimalType => true | ||
case _ => false | ||
} | ||
} | ||
|
||
def isMatch(dt: DataType, at: ArgType): Boolean = { | ||
at match { | ||
case AnyType => true | ||
case NumericType => | ||
dt match { | ||
case _: ByteType | _: ShortType | _: IntegerType | _: LongType => true | ||
case _: FloatType | _: DoubleType => true | ||
case _: DecimalType => true | ||
case _ => false | ||
} | ||
case OrderedTypes => | ||
// TODO exclude map or other complex types that contain maps | ||
true | ||
case _ => false | ||
} | ||
} | ||
|
||
} | ||
|
||
object Exprs { | ||
|
||
/** | ||
* Aggregate expressions. Note that `first` and `last` are excluded because they are | ||
* non-deterministic. | ||
*/ | ||
val aggregate: Seq[ExprMeta] = Seq( | ||
ExprMeta("min", Seq(OrderedTypes)), | ||
ExprMeta("max", Seq(OrderedTypes)), | ||
ExprMeta("count", Seq(AnyType)), | ||
ExprMeta("sum", Seq(NumericType)), | ||
ExprMeta("avg", Seq(NumericType)), | ||
ExprMeta("median", Seq(NumericType)), | ||
ExprMeta("stddev", Seq(NumericType)), | ||
ExprMeta("stddev_pop", Seq(NumericType)), | ||
ExprMeta("stddev_samp", Seq(NumericType)), | ||
ExprMeta("variance", Seq(NumericType)), | ||
ExprMeta("var_pop", Seq(NumericType)), | ||
ExprMeta("var_samp", Seq(NumericType)), | ||
ExprMeta("corr", Seq(NumericType, NumericType)), | ||
ExprMeta("covar_pop", Seq(NumericType, NumericType)), | ||
ExprMeta("covar_samp", Seq(NumericType, NumericType))) | ||
} | ||
|
||
/** Meta-data about a Spark expression */ | ||
case class ExprMeta(name: String, args: Seq[ArgType]) | ||
|
||
/** Represents that data type(s) that an argument accepts */ | ||
sealed trait ArgType | ||
|
||
/** Supports any input type */ | ||
case object AnyType extends ArgType | ||
|
||
/** Integral, floating-point, and decimal */ | ||
case object NumericType extends ArgType | ||
|
||
/** Types that can ordered. Includes struct and array but excludes maps */ | ||
case object OrderedTypes extends ArgType |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,12 +116,49 @@ abstract class CometTestBase | |
require(absTol > 0 && absTol <= 1e-6, s"absTol $absTol is out of range (0, 1e-6]") | ||
|
||
actualAnswer.toSeq.zip(expectedAnswer.toSeq).foreach { | ||
case (actual: Float, expected: Float) => | ||
def isPosInfinity(value: Float): Boolean = { | ||
value.isPosInfinity || value == 3.4028235e38 | ||
} | ||
|
||
if ((actual.isNaN && expected.isNaN) || | ||
(isPosInfinity(actual) && isPosInfinity(expected)) || | ||
(actual.isNegInfinity && expected.isNegInfinity)) { | ||
// ok | ||
} else { | ||
|
||
def almostEqual(a: Double, b: Double, tolerance: Double = 1e-6f): Boolean = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like there are multiple identical definitions of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the code is hacky and experimental. Will clean up before marking as ready for review. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the two implementations of |
||
Math.abs(a - b) <= tolerance * Math.max(Math.abs(a), Math.abs(b)) | ||
} | ||
|
||
assert( | ||
almostEqual(actual, expected), | ||
s"actual answer $actual not within $absTol of correct answer $expected") | ||
} | ||
|
||
case (actual: Double, expected: Double) => | ||
if (!actual.isNaN && !expected.isNaN) { | ||
def isPosInfinity(value: Double): Boolean = { | ||
value.isPosInfinity || value == 3.4028235e38 | ||
} | ||
|
||
if ((actual.isNaN && expected.isNaN) || | ||
(isPosInfinity(actual) && isPosInfinity(expected)) || | ||
(actual.isNegInfinity && expected.isNegInfinity)) { | ||
// ok | ||
} else { | ||
|
||
def almostEqual(a: Double, b: Double, tolerance: Double = 1e-6f): Boolean = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same implementation as above |
||
Math.abs(a - b) <= tolerance * Math.max(Math.abs(a), Math.abs(b)) | ||
} | ||
|
||
assert( | ||
math.abs(actual - expected) < absTol, | ||
almostEqual(actual, expected), | ||
s"actual answer $actual not within $absTol of correct answer $expected") | ||
} | ||
|
||
case (actual: Array[_], expected: Array[_]) => | ||
assert(actual.sameElements(expected), s"$actualAnswer did not equal $expectedAnswer") | ||
|
||
case (actual, expected) => | ||
assert(actual == expected, s"$actualAnswer did not equal $expectedAnswer") | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we plan to have these scan implementations also added to the fuzz testing at some point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. These are commented out for now because they fail due to timestamp issues.