Skip to content

Commit

Permalink
refactor and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andygrove committed May 29, 2024
1 parent f052013 commit e7a4a03
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 87 deletions.
25 changes: 6 additions & 19 deletions fuzz-testing/src/main/scala/org/apache/comet/fuzz/DataGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,9 @@ object DataGen {
numRows: Int,
numColumns: Int): Unit = {

val dataTypes = Seq(
(DataTypes.BooleanType, 0.2),
(DataTypes.ByteType, 0.2),
(DataTypes.ShortType, 0.2),
(DataTypes.IntegerType, 0.2),
(DataTypes.LongType, 0.2),
(DataTypes.FloatType, 0.2),
(DataTypes.DoubleType, 0.2),
//TODO add Decimal support
//(DataTypes.createDecimalType(10,2), 0.2),
(DataTypes.DateType, 0.2),
(DataTypes.TimestampType, 0.2),
(DataTypes.TimestampNTZType, 0.2),
(DataTypes.StringType, 0.2),
(DataTypes.BinaryType, 0.2))

// generate schema using random data types
val fields = Range(0, numColumns)
.map(i => StructField(s"c$i", Utils.randomWeightedChoice(dataTypes), nullable = true))
.map(i => StructField(s"c$i", Utils.randomWeightedChoice(Meta.dataTypes), nullable = true))
val schema = StructType(fields)

// generate columnar data
Expand All @@ -83,7 +67,9 @@ object DataGen {
def generateColumn(r: Random, dataType: DataType, numRows: Int): Seq[Any] = {
dataType match {
case DataTypes.BooleanType =>
generateColumn(r, DataTypes.LongType, numRows).map(_.asInstanceOf[Long].toShort).map(s => s%2 ==0)
generateColumn(r, DataTypes.LongType, numRows)
.map(_.asInstanceOf[Long].toShort)
.map(s => s % 2 == 0)
case DataTypes.ByteType =>
generateColumn(r, DataTypes.LongType, numRows).map(_.asInstanceOf[Long].toByte)
case DataTypes.ShortType =>
Expand Down Expand Up @@ -143,7 +129,8 @@ object DataGen {
}
})
case DataTypes.BinaryType =>
generateColumn(r, DataTypes.StringType, numRows).map(_.asInstanceOf[String].getBytes(Charset.defaultCharset()))
generateColumn(r, DataTypes.StringType, numRows)
.map(_.asInstanceOf[String].getBytes(Charset.defaultCharset()))
case DataTypes.DateType =>
Range(0, numRows).map(_ => new java.sql.Date(1716645600011L + r.nextInt()))
case DataTypes.TimestampType | DataTypes.TimestampNTZType =>
Expand Down
109 changes: 109 additions & 0 deletions fuzz-testing/src/main/scala/org/apache/comet/fuzz/Meta.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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.fuzz

import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.DataTypes

object Meta {

val dataTypes: Seq[(DataType, Double)] = Seq(
(DataTypes.BooleanType, 0.2),
(DataTypes.ByteType, 0.2),
(DataTypes.ShortType, 0.2),
(DataTypes.IntegerType, 0.2),
(DataTypes.LongType, 0.2),
(DataTypes.FloatType, 0.2),
(DataTypes.DoubleType, 0.2),
// TODO add Decimal support
// (DataTypes.createDecimalType(10,2), 0.2),
(DataTypes.DateType, 0.2),
(DataTypes.TimestampType, 0.2),
(DataTypes.TimestampNTZType, 0.2),
(DataTypes.StringType, 0.2),
(DataTypes.BinaryType, 0.2))

val scalarFunc: Seq[Function] = Seq(
// string
Function("substring", 3),
Function("coalesce", 1),
Function("starts_with", 2),
Function("ends_with", 2),
Function("contains", 2),
Function("ascii", 1),
Function("bit_length", 1),
Function("octet_length", 1),
Function("upper", 1),
Function("lower", 1),
Function("chr", 1),
Function("init_cap", 1),
Function("trim", 1),
Function("ltrim", 1),
Function("rtrim", 1),
Function("btrim", 1),
Function("concat_ws", 2),
Function("repeat", 2),
Function("length", 1),
Function("reverse", 1),
Function("in_str", 2),
Function("replace", 2),
Function("translate", 2),
// date
Function("year", 1),
Function("hour", 1),
Function("minute", 1),
Function("second", 1),
// math
Function("abs", 1),
Function("acos", 1),
Function("asin", 1),
Function("atan", 1),
Function("Atan2", 1),
Function("Cos", 1),
Function("Exp", 2),
Function("Ln", 1),
Function("Log10", 1),
Function("Log2", 1),
Function("Pow", 2),
Function("Round", 1),
Function("Signum", 1),
Function("Sin", 1),
Function("Sqrt", 1),
Function("Tan", 1),
Function("Ceil", 1),
Function("Floor", 1))

val aggFunc: Seq[Function] = Seq(
Function("min", 1),
Function("max", 1),
Function("count", 1),
Function("avg", 1),
Function("sum", 1),
Function("first", 1),
Function("last", 1),
Function("var_pop", 1),
Function("var_samp", 1),
Function("covar_pop", 1),
Function("covar_samp", 1),
Function("stddev_pop", 1),
Function("stddev_samp", 1),
Function("corr", 2))

}
70 changes: 2 additions & 68 deletions fuzz-testing/src/main/scala/org/apache/comet/fuzz/QueryGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,77 +55,11 @@ object QueryGen {
w.close()
}

val scalarFunc: Seq[Function] = Seq(
// string
Function("substring", 3),
Function("coalesce", 1),
Function("starts_with", 2),
Function("ends_with", 2),
Function("contains", 2),
Function("ascii", 1),
Function("bit_length", 1),
Function("octet_length", 1),
Function("upper", 1),
Function("lower", 1),
Function("chr", 1),
Function("init_cap", 1),
Function("trim", 1),
Function("ltrim", 1),
Function("rtrim", 1),
Function("btrim", 1),
Function("concat_ws", 2),
Function("repeat", 2),
Function("length", 1),
Function("reverse", 1),
Function("in_str", 2),
Function("replace", 2),
Function("translate", 2),
// date
Function("year", 1),
Function("hour", 1),
Function("minute", 1),
Function("second", 1),
// math
Function("abs", 1),
Function("acos", 1),
Function("asin", 1),
Function("atan", 1),
Function("Atan2", 1),
Function("Cos", 1),
Function("Exp", 2),
Function("Ln", 1),
Function("Log10", 1),
Function("Log2", 1),
Function("Pow", 2),
Function("Round", 1),
Function("Signum", 1),
Function("Sin", 1),
Function("Sqrt", 1),
Function("Tan", 1),
Function("Ceil", 1),
Function("Floor", 1))

val aggFunc: Seq[Function] = Seq(
Function("min", 1),
Function("max", 1),
Function("count", 1),
Function("avg", 1),
Function("sum", 1),
Function("first", 1),
Function("last", 1),
Function("var_pop", 1),
Function("var_samp", 1),
Function("covar_pop", 1),
Function("covar_samp", 1),
Function("stddev_pop", 1),
Function("stddev_samp", 1),
Function("corr", 2))

private def generateAggregate(r: Random, spark: SparkSession, numFiles: Int): String = {
val tableName = s"test${r.nextInt(numFiles)}"
val table = spark.table(tableName)

val func = Utils.randomChoice(aggFunc, r)
val func = Utils.randomChoice(Meta.aggFunc, r)
val args = Range(0, func.num_args)
.map(_ => Utils.randomChoice(table.columns, r))

Expand All @@ -147,7 +81,7 @@ object QueryGen {
val tableName = s"test${r.nextInt(numFiles)}"
val table = spark.table(tableName)

val func = Utils.randomChoice(scalarFunc, r)
val func = Utils.randomChoice(Meta.scalarFunc, r)
val args = Range(0, func.num_args)
.map(_ => Utils.randomChoice(table.columns, r))

Expand Down

0 comments on commit e7a4a03

Please sign in to comment.