jayzhan211 commented on code in PR #27702:
URL: https://github.com/apache/datafusion/pull/27702#discussion_r4719388425


##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -1574,6 +1578,24 @@ impl Unparser<'_> {
             ScalarValue::DurationNanosecond(None) => {
                 Ok(ast::Expr::value(ast::Value::Null))
             }
+            ScalarValue::Struct(array) if array.len() == 1 && 
array.is_valid(0) => {
+                let args = array
+                    .fields()
+                    .iter()
+                    .zip(array.columns())
+                    .map(|(field, column)| {
+                        Ok([
+                            Expr::Literal(
+                                ScalarValue::Utf8(Some(field.name().clone())),
+                                None,
+                            ),
+                            Expr::Literal(ScalarValue::try_from_array(column, 
0)?, None),
+                        ])

Review Comment:
   @adriangb thanks for the PR! Agreeing with this one, with a repro: the new 
struct arm reaches the List arms of `scalar_to_sql`, which ignore validity, so 
a folded struct with a NULL list child unparses as `{a: []}`. On `main` this 
query errors with "Unsupported scalar"; on this branch it returns `{a: []}` 
instead of `{a: NULL}` (`--unparser-round-trip --unparser-optimize`):
   
   ```sql
   CREATE TABLE t (x INT) AS VALUES (1), (2);
   SELECT s FROM (SELECT named_struct('a', CAST(NULL AS INT[])) AS s, x FROM t) 
ORDER BY x;
   ```
   
   Returning NULL for a null list scalar fixes this case and the same bug for a 
top-level null list literal (`CAST(NULL AS INT[])` already unparses as `[]` on 
`main`):
   
   <details>
   <summary>Fix in <code>scalar_to_sql</code> (outside the diff) and a unit 
test</summary>
   
   ```diff
                | ScalarValue::LargeBinary(None) => 
Ok(ast::Expr::value(ast::Value::Null)),
   +            ScalarValue::FixedSizeList(_)
   +            | ScalarValue::List(_)
   +            | ScalarValue::LargeList(_)
   +            | ScalarValue::ListView(_)
   +            | ScalarValue::LargeListView(_)
   +                if v.is_null() =>
   +            {
   +                Ok(ast::Expr::value(ast::Value::Null))
   +            }
                ScalarValue::FixedSizeList(a) => 
self.scalar_value_list_to_sql(a.values()),
   ```
   
   ```rs
   #[test]
   fn test_struct_literal_with_null_list_child_to_sql() -> Result<()> {
       let list_field = Arc::new(Field::new_list_field(DataType::Int32, true));
       let fields = Fields::from(vec![Field::new(
           "a",
           DataType::List(Arc::clone(&list_field)),
           true,
       )]);
       let array = StructArray::new(
           fields,
           vec![Arc::new(ListArray::new_null(list_field, 1))],
           None,
       );
       let expr = Expr::Literal(ScalarValue::Struct(Arc::new(array)), None);
   
       let ast = Unparser::new(&DefaultDialect {}).expr_to_sql(&expr)?;
       assert_eq!(ast.to_string(), "{a: NULL}");
   
       Ok(())
   }
   ```
   
   </details>
   



-- 
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]

Reply via email to