hsiang-c commented on code in PR #2616: URL: https://github.com/apache/datafusion-comet/pull/2616#discussion_r2448963298
########## spark/src/main/scala/org/apache/comet/testing/FuzzDataGenerator.scala: ########## @@ -0,0 +1,257 @@ +/* + * 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.testing + +import java.math.{BigDecimal, RoundingMode} +import java.nio.charset.Charset +import java.sql.Timestamp +import java.text.SimpleDateFormat +import java.time.{Instant, LocalDateTime, ZoneId} + +import scala.collection.mutable.ListBuffer +import scala.util.Random + +import org.apache.commons.lang3.RandomStringUtils +import org.apache.spark.sql.{DataFrame, Row, SparkSession} +import org.apache.spark.sql.types._ + +object FuzzDataGenerator { + + /** + * Date to use as base for generating temporal columns. Random integers will be added to or + * subtracted from this value. + * + * Date was chosen to trigger generating a timestamp that's larger than a 64-bit nanosecond + * timestamp can represent so that we test support for INT96 timestamps. + */ + val defaultBaseDate: Long = + new SimpleDateFormat("YYYY-MM-DD hh:mm:ss").parse("3333-05-25 12:34:56").getTime + + private val primitiveTypes = Seq( + DataTypes.BooleanType, + DataTypes.ByteType, + DataTypes.ShortType, + DataTypes.IntegerType, + DataTypes.LongType, + DataTypes.FloatType, + DataTypes.DoubleType, + DataTypes.createDecimalType(10, 2), + DataTypes.createDecimalType(36, 18), + DataTypes.DateType, + DataTypes.TimestampType, + DataTypes.TimestampNTZType, + DataTypes.StringType, + DataTypes.BinaryType) + + private def filteredPrimitives(excludeTypes: Seq[DataType]) = { + + primitiveTypes.filterNot { dataType => + excludeTypes.exists { + case _: DecimalType => + // For DecimalType, match if the type is also a DecimalType (ignore precision/scale) + dataType.isInstanceOf[DecimalType] + case excludeType => + dataType == excludeType + } + } + } + + def generateDataFrame( + r: Random, + spark: SparkSession, + numRows: Int, + options: DataGenOptions): DataFrame = { + + val filteredPrimitiveTypes = filteredPrimitives(options.excludeTypes) + val dataTypes = ListBuffer[DataType]() + dataTypes.appendAll(filteredPrimitiveTypes) + + val arraysOfPrimitives = filteredPrimitiveTypes.map(DataTypes.createArrayType) + + if (options.generateStruct) { + dataTypes += StructType(filteredPrimitiveTypes.zipWithIndex.map(x => + StructField(s"c${x._2}", x._1, nullable = true))) + + if (options.generateArray) { + dataTypes += StructType(arraysOfPrimitives.zipWithIndex.map(x => + StructField(s"c${x._2}", x._1, nullable = true))) + } + } + + if (options.generateMap) { + dataTypes += MapType(DataTypes.IntegerType, DataTypes.StringType) + } + + if (options.generateArray) { + dataTypes.appendAll(arraysOfPrimitives) + + if (options.generateStruct) { + dataTypes += DataTypes.createArrayType( + StructType(filteredPrimitiveTypes.zipWithIndex.map(x => + StructField(s"c${x._2}", x._1, nullable = true)))) + } + + if (options.generateMap) { + dataTypes += DataTypes.createArrayType( + MapType(DataTypes.IntegerType, DataTypes.StringType)) + } + } + + // generate schema using random data types + val fields = dataTypes.zipWithIndex + .map(i => StructField(s"c${i._2}", i._1, nullable = true)) + val schema = StructType(fields.toSeq) + + // generate columnar data + val cols: Seq[Seq[Any]] = + schema.fields.map(f => generateColumn(r, f.dataType, numRows, options)).toSeq + + // convert to rows + val rows = Range(0, numRows).map(rowIndex => { + Row.fromSeq(cols.map(_(rowIndex))) + }) + + spark.createDataFrame(spark.sparkContext.parallelize(rows), schema) + } + + private def generateColumn( + r: Random, + dataType: DataType, + numRows: Int, + options: DataGenOptions): Seq[Any] = { + dataType match { + case ArrayType(elementType, _) => + val values = generateColumn(r, elementType, numRows, options) + val list = ListBuffer[Any]() + for (i <- 0 until numRows) { + if (i % 10 == 0 && options.allowNull) { + list += null + } else { + list += Range(0, r.nextInt(5)).map(j => values((i + j) % values.length)).toArray + } + } + list.toSeq + case StructType(fields) => + val values = fields.map(f => generateColumn(r, f.dataType, numRows, options)) + Range(0, numRows).map(i => Row(values.indices.map(j => values(j)(i)): _*)) + case MapType(keyType, valueType, _) => + val mapOptions = options.copy(allowNull = false) + val k = generateColumn(r, keyType, numRows, mapOptions) + val v = generateColumn(r, valueType, numRows, mapOptions) + k.zip(v).map(x => Map(x._1 -> x._2)) + case DataTypes.BooleanType => + generateColumn(r, DataTypes.LongType, numRows, options) + .map(_.asInstanceOf[Long].toShort) + .map(s => s % 2 == 0) + case DataTypes.ByteType => + generateColumn(r, DataTypes.LongType, numRows, options) + .map(_.asInstanceOf[Long].toByte) + case DataTypes.ShortType => + generateColumn(r, DataTypes.LongType, numRows, options) + .map(_.asInstanceOf[Long].toShort) + case DataTypes.IntegerType => + generateColumn(r, DataTypes.LongType, numRows, options) + .map(_.asInstanceOf[Long].toInt) + case DataTypes.LongType => + Range(0, numRows).map(_ => { + r.nextInt(50) match { + case 0 if options.allowNull => null + case 1 => 0L + case 2 => Byte.MinValue.toLong + case 3 => Byte.MaxValue.toLong + case 4 => Short.MinValue.toLong + case 5 => Short.MaxValue.toLong + case 6 => Int.MinValue.toLong + case 7 => Int.MaxValue.toLong + case 8 => Long.MinValue + case 9 => Long.MaxValue + case _ => r.nextLong() + } + }) + case DataTypes.FloatType => + Range(0, numRows).map(_ => { + r.nextInt(20) match { + case 0 if options.allowNull => null + case 1 => Float.NegativeInfinity + case 2 => Float.PositiveInfinity + case 3 => Float.MinValue + case 4 => Float.MaxValue + case 5 => 0.0f + case 6 if options.generateNegativeZero => -0.0f + case _ => r.nextFloat() + } + }) + case DataTypes.DoubleType => + Range(0, numRows).map(_ => { + r.nextInt(20) match { + case 0 if options.allowNull => null + case 1 => Double.NegativeInfinity + case 2 => Double.PositiveInfinity + case 3 => Double.MinValue + case 4 => Double.MaxValue Review Comment: Could we include `NaN`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
