neilconway commented on code in PR #26073:
URL: https://github.com/apache/datafusion/pull/26073#discussion_r4220167679
##########
datafusion/functions-nested/src/min_max.rs:
##########
@@ -311,3 +317,106 @@ fn scalar_min_max<N: ArrowNativeTypeOp>(
}
best
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::array::{GenericListBuilder, ListArray, StringArray,
StringBuilder};
+ use arrow::datatypes::Int64Type;
+
+ fn utf8_list<O: OffsetSizeTrait>(
+ rows: Vec<Option<Vec<Option<&str>>>>,
+ ) -> GenericListArray<O> {
+ let mut builder = GenericListBuilder::<O,
_>::new(StringBuilder::new());
+ for row in rows {
+ match row {
+ Some(values) => {
+ for v in values {
+ builder.values().append_option(v);
+ }
+ builder.append(true);
+ }
+ None => builder.append(false),
+ }
+ }
+ builder.finish()
+ }
+
+ #[test]
+ fn zero_rows_non_primitive_returns_empty_array() -> Result<()> {
+ for is_min in [true, false] {
+ let out = array_min_max_helper(&utf8_list::<i32>(vec![]), is_min)?;
+ assert_eq!(out.len(), 0);
+ assert_eq!(out.data_type(), &DataType::Utf8);
+
+ let out = array_min_max_helper(&utf8_list::<i64>(vec![]), is_min)?;
+ assert_eq!(out.len(), 0);
+ assert_eq!(out.data_type(), &DataType::Utf8);
+ }
+ Ok(())
+ }
+
+ #[test]
+ fn zero_rows_primitive_returns_empty_array() -> Result<()> {
+ let empty = ListArray::from_iter_primitive::<Int64Type, _, _>(Vec::<
+ Option<Vec<Option<i64>>>,
+ >::new());
+ for is_min in [true, false] {
+ let out = array_min_max_helper(&empty, is_min)?;
+ assert_eq!(out.len(), 0);
+ assert_eq!(out.data_type(), &DataType::Int64);
+ }
+ Ok(())
+ }
Review Comment:
For these tests, how about using the DataFrame APIs instead? In general it
is better to use SQL or public-facing APIs for tests when possible. For example
https://gist.github.com/neilconway/2657bc0a3c9512d9a2dfb9c7945ebdbe
##########
datafusion/functions-nested/src/min_max.rs:
##########
@@ -311,3 +317,106 @@ fn scalar_min_max<N: ArrowNativeTypeOp>(
}
best
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::array::{GenericListBuilder, ListArray, StringArray,
StringBuilder};
+ use arrow::datatypes::Int64Type;
+
+ fn utf8_list<O: OffsetSizeTrait>(
+ rows: Vec<Option<Vec<Option<&str>>>>,
+ ) -> GenericListArray<O> {
+ let mut builder = GenericListBuilder::<O,
_>::new(StringBuilder::new());
+ for row in rows {
+ match row {
+ Some(values) => {
+ for v in values {
+ builder.values().append_option(v);
+ }
+ builder.append(true);
+ }
+ None => builder.append(false),
+ }
+ }
+ builder.finish()
+ }
+
+ #[test]
+ fn zero_rows_non_primitive_returns_empty_array() -> Result<()> {
+ for is_min in [true, false] {
+ let out = array_min_max_helper(&utf8_list::<i32>(vec![]), is_min)?;
+ assert_eq!(out.len(), 0);
+ assert_eq!(out.data_type(), &DataType::Utf8);
+
+ let out = array_min_max_helper(&utf8_list::<i64>(vec![]), is_min)?;
+ assert_eq!(out.len(), 0);
+ assert_eq!(out.data_type(), &DataType::Utf8);
+ }
+ Ok(())
+ }
+
+ #[test]
+ fn zero_rows_primitive_returns_empty_array() -> Result<()> {
+ let empty = ListArray::from_iter_primitive::<Int64Type, _, _>(Vec::<
+ Option<Vec<Option<i64>>>,
+ >::new());
+ for is_min in [true, false] {
+ let out = array_min_max_helper(&empty, is_min)?;
+ assert_eq!(out.len(), 0);
+ assert_eq!(out.data_type(), &DataType::Int64);
+ }
+ Ok(())
+ }
+
+ #[test]
+ fn non_primitive_rows() -> Result<()> {
+ let list = utf8_list::<i32>(vec![
+ Some(vec![Some("prod"), Some("api")]),
+ Some(vec![]),
+ None,
+ Some(vec![Some("web"), None, Some("db")]),
+ ]);
+ let min = array_min_max_helper(&list, true)?;
+ let max = array_min_max_helper(&list, false)?;
+ assert_eq!(
+ min.as_string::<i32>(),
+ &StringArray::from(vec![Some("api"), None, None, Some("db")])
+ );
+ assert_eq!(
+ max.as_string::<i32>(),
+ &StringArray::from(vec![Some("prod"), None, None, Some("web")])
+ );
+ Ok(())
+ }
Review Comment:
Can we move this to an SLT test instead?
--
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]