Skip to content

Commit 2b4c204

Browse files
committed
add failing fuzz test dates to tests and remove failing fuzz test
1 parent 19fa952 commit 2b4c204

File tree

2 files changed

+56
-11
lines changed

2 files changed

+56
-11
lines changed

core/src/execution/datafusion/expressions/cast.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,12 +1762,44 @@ mod tests {
17621762
}
17631763

17641764
#[test]
1765-
fn test_cast_string_to_invalid_dates() {
1765+
fn test_cast_string_array_with_valid_dates() {
1766+
let array_with_invalid_date: ArrayRef = Arc::new(StringArray::from(vec![
1767+
Some("-262143-12-31"),
1768+
Some("\n -262143-12-31 "),
1769+
Some("-262143-12-31T \t\n"),
1770+
Some("\n\t-262143-12-31T\r"),
1771+
Some("-262143-12-31T 123123123"),
1772+
Some("\r\n-262143-12-31T \r123123123"),
1773+
Some("\n -262143-12-31T \n\t"),
1774+
]));
1775+
1776+
for eval_mode in &[EvalMode::Legacy, EvalMode::Try, EvalMode::Ansi] {
1777+
let result =
1778+
Cast::cast_string_to_date(&array_with_invalid_date, &DataType::Date32, *eval_mode)
1779+
.unwrap();
1780+
1781+
let date32_array = result
1782+
.as_any()
1783+
.downcast_ref::<arrow::array::Date32Array>()
1784+
.unwrap();
1785+
assert_eq!(result.len(), 7);
1786+
date32_array
1787+
.iter()
1788+
.for_each(|v| assert_eq!(v.unwrap(), -96464928));
1789+
}
1790+
}
1791+
1792+
#[test]
1793+
fn test_cast_string_array_with_invalid_dates() {
17661794
let array_with_invalid_date: ArrayRef = Arc::new(StringArray::from(vec![
17671795
Some("2020"),
17681796
Some("2020-01"),
17691797
Some("2020-01-01"),
1798+
//4 invalid dates
17701799
Some("2020-010-01T"),
1800+
Some("202"),
1801+
Some(" 202 "),
1802+
Some("\n 2020-\r8 "),
17711803
Some("2020-01-01T"),
17721804
]));
17731805

@@ -1782,7 +1814,16 @@ mod tests {
17821814
.unwrap();
17831815
assert_eq!(
17841816
date32_array.iter().collect::<Vec<_>>(),
1785-
vec![Some(18262), Some(18262), Some(18262), None, Some(18262)]
1817+
vec![
1818+
Some(18262),
1819+
Some(18262),
1820+
Some(18262),
1821+
None,
1822+
None,
1823+
None,
1824+
None,
1825+
Some(18262)
1826+
]
17861827
);
17871828
}
17881829

spark/src/test/scala/org/apache/comet/CometCastSuite.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package org.apache.comet
2222
import java.io.File
2323

2424
import scala.util.Random
25-
import scala.util.matching.Regex
2625

2726
import org.apache.spark.sql.{CometTestBase, DataFrame, SaveMode}
2827
import org.apache.spark.sql.catalyst.expressions.Cast
@@ -613,14 +612,19 @@ class CometCastSuite extends CometTestBase with AdaptiveSparkPlanHelper {
613612
"abc-def-ghi jkl",
614613
"2020-mar-20",
615614
"not_a_date",
616-
"T2")
617-
618-
// due to limitations of NaiveDate we only support years between 262143 BC and 262142 AD"
619-
val unsupportedYearPattern: Regex = "^\\s*[0-9]{5,}".r
620-
val fuzzDates = gen
621-
.generateStrings(dataSize, datePattern, 8)
622-
.filterNot(str => unsupportedYearPattern.findFirstMatchIn(str).isDefined)
623-
castTest((validDates ++ invalidDates ++ fuzzDates).toDF("a"), DataTypes.DateType)
615+
"T2",
616+
"\t\n3938\n8",
617+
"8701\t",
618+
"\n8757",
619+
"7593\t\t\t",
620+
"\t9374 \n ",
621+
"\n 9850 \t",
622+
"\r\n\t9840",
623+
"\t9629\n",
624+
"\r\n 9629 \r\n",
625+
"\r\n 962 \r\n",
626+
"\r\n 62 \r\n")
627+
castTest((validDates ++ invalidDates).toDF("a"), DataTypes.DateType)
624628
}
625629

626630
test("cast StringType to TimestampType disabled by default") {

0 commit comments

Comments
 (0)