kosiew commented on code in PR #27274:
URL: https://github.com/apache/datafusion/pull/27274#discussion_r4767155958
##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -3488,6 +3507,90 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_null_nested_scalar_value_to_sql() -> Result<()> {
+ let item = Arc::new(Field::new_list_field(DataType::Int32, true));
+ let map_entries = Arc::new(Field::new(
+ "entries",
+ DataType::Struct(
+ vec![
+ Field::new("key", DataType::Utf8, false),
+ Field::new("value", DataType::Int32, true),
+ ]
+ .into(),
+ ),
+ false,
+ ));
+ let data_types = [
+ DataType::List(Arc::clone(&item)),
+ DataType::LargeList(Arc::clone(&item)),
+ DataType::FixedSizeList(Arc::clone(&item), 2),
+ DataType::ListView(Arc::clone(&item)),
+ DataType::LargeListView(Arc::clone(&item)),
+ DataType::Struct(vec![Field::new("a", DataType::Int32,
true)].into()),
+ DataType::Map(map_entries, false),
+ DataType::Union(
+ UnionFields::try_new(
+ vec![0],
+ vec![Field::new("a", DataType::Int32, true)],
+ )?,
+ UnionMode::Sparse,
+ ),
+ ];
+
+ for data_type in data_types {
+ let expr = Expr::Literal(ScalarValue::try_new_null(&data_type)?,
None);
+ assert_eq!(
+ expr_to_sql(&expr)?.to_string(),
Review Comment:
Could we include `data_type` in the error context for `expr_to_sql(&expr)`?
An unparsing error currently exits before the assertion reports the failing
type.
##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -1311,6 +1311,22 @@ impl Unparser<'_> {
fn scalar_to_sql(&self, v: &ScalarValue) -> Result<ast::Expr> {
match v {
ScalarValue::Null => Ok(ast::Expr::value(ast::Value::Null)),
+ // The list arms below render the child values without checking
whether
+ // the list itself is null, and the struct/map/union arms reject
every
+ // value. Catch nulls of these types first so they unparse as
`NULL`,
+ // like other typed nulls.
+ ScalarValue::List(_)
+ | ScalarValue::LargeList(_)
+ | ScalarValue::FixedSizeList(_)
+ | ScalarValue::ListView(_)
+ | ScalarValue::LargeListView(_)
+ | ScalarValue::Struct(_)
+ | ScalarValue::Map(_)
+ | ScalarValue::Union(..)
+ if v.is_null() =>
+ {
+ Ok(ast::Expr::value(ast::Value::Null))
Review Comment:
Bare `NULL` loses the list type, causing `array_dims(NULL)` to fail. Please
preserve it (e.g., `CAST(NULL AS INT[])`; the current cast mapper rejects
`List`) and add unparse/replan/execute regression coverage for projected null
`List<Int32>` values and `array_dims`.
##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -3488,6 +3507,90 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_null_nested_scalar_value_to_sql() -> Result<()> {
+ let item = Arc::new(Field::new_list_field(DataType::Int32, true));
+ let map_entries = Arc::new(Field::new(
+ "entries",
+ DataType::Struct(
+ vec![
+ Field::new("key", DataType::Utf8, false),
+ Field::new("value", DataType::Int32, true),
+ ]
+ .into(),
+ ),
+ false,
+ ));
+ let data_types = [
+ DataType::List(Arc::clone(&item)),
+ DataType::LargeList(Arc::clone(&item)),
+ DataType::FixedSizeList(Arc::clone(&item), 2),
+ DataType::ListView(Arc::clone(&item)),
+ DataType::LargeListView(Arc::clone(&item)),
+ DataType::Struct(vec![Field::new("a", DataType::Int32,
true)].into()),
+ DataType::Map(map_entries, false),
+ DataType::Union(
+ UnionFields::try_new(
+ vec![0],
+ vec![Field::new("a", DataType::Int32, true)],
+ )?,
+ UnionMode::Sparse,
+ ),
+ ];
+
+ for data_type in data_types {
+ let expr = Expr::Literal(ScalarValue::try_new_null(&data_type)?,
None);
+ assert_eq!(
+ expr_to_sql(&expr)?.to_string(),
+ "NULL",
+ "data type: {data_type}"
+ );
+ }
+
+ Ok(())
+ }
+
+ #[test]
+ fn test_non_null_nested_scalar_value_to_sql() -> Result<()> {
+ let list_type =
+ DataType::List(Arc::new(Field::new_list_field(DataType::Int32,
true)));
+ let list_with_null_element =
ScalarValue::List(ScalarValue::new_list_nullable(
+ &[
+ ScalarValue::try_new_null(&list_type)?,
+ ScalarValue::List(ScalarValue::new_list_nullable(
+ &[ScalarValue::Int32(Some(1))],
+ &DataType::Int32,
+ )),
+ ],
+ &list_type,
+ ));
+ assert_eq!(
+ expr_to_sql(&Expr::Literal(list_with_null_element,
None))?.to_string(),
+ "[NULL, [1]]"
+ );
+
+ let empty_list =
+ ScalarValue::List(ScalarValue::new_list_nullable(&[],
&DataType::Int32));
+ assert_eq!(
+ expr_to_sql(&Expr::Literal(empty_list, None))?.to_string(),
+ "[]"
+ );
+
+ let non_null_struct = ScalarStructBuilder::new()
Review Comment:
Consider using an `Int32(None)` child to verify that child nullness does not
cause a valid struct to render as `NULL`. The expected error remains
`NotImplemented`.
--
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]