-
Notifications
You must be signed in to change notification settings - Fork 169
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
test: Add some fuzz testing for cast operations #16
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f30f192
Add some basic fuzz testing for cast operations
andygrove e0badd1
remove test file
andygrove fc9c4d2
Add comment
andygrove 105b768
add more tests
andygrove 64748e4
code cleanup and add a few more tests
andygrove eda04f9
Merge remote-tracking branch 'apache/main' into fuzz-test
andygrove 5963296
formatting and address TODO comment
andygrove 79ff354
Merge remote-tracking branch 'apache/main' into fuzz-test
andygrove 297d5cf
make temp dir more robust
andygrove e32a40a
use withTempDir
andygrove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
spark/src/test/scala/org/apache/comet/CometCastSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* 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 org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper | ||
import org.apache.spark.sql.{CometTestBase, DataFrame, SaveMode} | ||
import org.apache.spark.sql.functions.col | ||
import org.apache.spark.sql.types.{DataType, DataTypes} | ||
|
||
import scala.util.Random | ||
|
||
class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper { | ||
import testImplicits._ | ||
|
||
ignore("cast long to short") { | ||
castTest(generateLongs, DataTypes.ShortType) | ||
} | ||
|
||
test("cast float to bool") { | ||
castTest(generateFloats, DataTypes.BooleanType) | ||
} | ||
|
||
test("cast float to int") { | ||
castTest(generateFloats, DataTypes.IntegerType) | ||
} | ||
|
||
ignore("cast float to string") { | ||
castTest(generateFloats, DataTypes.StringType) | ||
} | ||
|
||
ignore("cast string to bool") { | ||
castTest(Seq("TRUE", "True", "true", "FALSE", "False", "false", "1", "0", "").toDF("a"), DataTypes.BooleanType) | ||
fuzzCastFromString("truefalseTRUEFALSEyesno10 \t\r\n", 8, DataTypes.BooleanType) | ||
} | ||
|
||
ignore("cast string to short") { | ||
fuzzCastFromString("0123456789e+- \t\r\n", 8, DataTypes.ShortType) | ||
} | ||
|
||
ignore("cast string to float") { | ||
fuzzCastFromString("0123456789e+- \t\r\n", 8, DataTypes.FloatType) | ||
} | ||
|
||
ignore("cast string to double") { | ||
fuzzCastFromString("0123456789e+- \t\r\n", 8, DataTypes.DoubleType) | ||
} | ||
|
||
ignore("cast string to date") { | ||
fuzzCastFromString("0123456789/ \t\r\n", 16, DataTypes.DateType) | ||
} | ||
|
||
ignore("cast string to timestamp") { | ||
castTest(Seq("2020-01-01T12:34:56.123456", "T2").toDF("a"), DataTypes.TimestampType) | ||
fuzzCastFromString("0123456789/:T \t\r\n", 32, DataTypes.TimestampType) | ||
} | ||
|
||
private def generateFloats = { | ||
val r = new Random(0) | ||
Range(0, 10000).map(_ => r.nextFloat()).toDF("a") | ||
} | ||
|
||
private def generateLongs = { | ||
val r = new Random(0) | ||
Range(0, 10000).map(_ => r.nextLong()).toDF("a") | ||
} | ||
|
||
private def genString(r: Random, chars: String, maxLen: Int): String = { | ||
val len = r.nextInt(maxLen) | ||
Range(0,len).map(_ => chars.charAt(r.nextInt(chars.length))).mkString | ||
} | ||
|
||
private def fuzzCastFromString(chars: String, maxLen: Int, toType: DataType) { | ||
val r = new Random(0) | ||
val inputs = Range(0, 10000).map(_ => genString(r, chars, maxLen)) | ||
castTest(inputs.toDF("a"), toType) | ||
} | ||
|
||
private def castTest(input: DataFrame, toType: DataType) { | ||
val df = roundtripParquet(input) | ||
.withColumn("converted", col("a").cast(toType)) | ||
checkSparkAnswer(df) | ||
} | ||
|
||
private def roundtripParquet(df: DataFrame): DataFrame = { | ||
// TODO create true temp file and delete after test completes | ||
val filename = s"/tmp/castTest_${System.currentTimeMillis()}.parquet" | ||
df.write.mode(SaveMode.Overwrite).parquet(filename) | ||
spark.read.parquet(filename) | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@sunchao Do we run style check in CI? Looks like the import order looks not correct?
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.
I need to double-check. The scalafix check is done here but I'm not sure if the import order is checked by something else.
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.
Oh it's because the scalastyle-maven-plugin is disabled. Let me try to re-enable it.