Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion fuzz-testing/src/main/scala/org/apache/comet/fuzz/Meta.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ case class SparkArrayType(elementType: SparkType) extends SparkType
case class SparkMapType(keyType: SparkType, valueType: SparkType) extends SparkType
case class SparkStructType(fields: Seq[SparkType]) extends SparkType
case object SparkAnyType extends SparkType
case class SparkTypeWithValues(sparkType: SparkType, validValues: Seq[String]) extends SparkType

case class FunctionSignature(inputTypes: Seq[SparkType])

Expand Down Expand Up @@ -302,7 +303,12 @@ object Meta {
createFunctionWithInputTypes("hour", Seq(SparkDateOrTimestampType)),
createFunctionWithInputTypes("minute", Seq(SparkDateOrTimestampType)),
createFunctionWithInputTypes("second", Seq(SparkDateOrTimestampType)),
createFunctionWithInputTypes("trunc", Seq(SparkDateOrTimestampType, SparkStringType)),
createFunctionWithInputTypes(
"date_trunc",
Seq(SparkDateOrTimestampType, SparkTypeWithValues(SparkStringType, Seq("year", "week")))),
createFunctionWithInputTypes(
"trunc",
Seq(SparkDateOrTimestampType, SparkTypeWithValues(SparkStringType, Seq("year", "week")))),
createFunctionWithInputTypes("year", Seq(SparkDateOrTimestampType)),
createFunctionWithInputTypes("month", Seq(SparkDateOrTimestampType)),
createFunctionWithInputTypes("day", Seq(SparkDateOrTimestampType)),
Expand Down
36 changes: 35 additions & 1 deletion fuzz-testing/src/main/scala/org/apache/comet/fuzz/QueryGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ object QueryGen {
val func = Utils.randomChoice(Meta.scalarFunc, r)
try {
val signature = Utils.randomChoice(func.signatures, r)
val args = signature.inputTypes.map(x => pickRandomColumn(r, table, x))
val args = signature.inputTypes.map(x => pickRandomColumnOrLiteral(r, table, x))

// Example SELECT c0, log(c0) as x FROM test0
s"SELECT ${args.mkString(", ")}, ${func.name}(${args.mkString(", ")}) AS x " +
Expand All @@ -117,6 +117,40 @@ object QueryGen {
}
}

private def pickRandomColumnOrLiteral(
r: Random,
df: DataFrame,
targetType: SparkType): String = {
if (r.nextBoolean) {
targetType match {
case SparkIntegralType =>
r.nextInt(10).toString
case SparkStringType =>
formatLiteral(r.nextString(4), SparkStringType)
case SparkTimestampType =>
"now()"
case SparkTypeWithValues(sparkType, values) =>
// choose between known valid input and random input
if (r.nextBoolean()) {
formatLiteral(Utils.randomChoice(values, r), sparkType)
} else {
pickRandomColumnOrLiteral(r, df, sparkType)
}
case _ =>
pickRandomColumn(r, df, targetType)
}
} else {
pickRandomColumn(r, df, targetType)
}
}

private def formatLiteral(validValue: Any, sparkType: SparkType): String = {
sparkType match {
case SparkStringType => s""""$validValue""""
case _ => validValue.toString
}
}

private def pickRandomColumn(r: Random, df: DataFrame, targetType: SparkType): String = {
targetType match {
case SparkAnyType =>
Expand Down