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


##########
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:
   A valid struct containing a NULL `List` child now emits `{a: []}` or the 
corresponding function call because list serialization ignores validity. This 
changes `get_field(s, 'a') IS NULL` from true to false. Preserve the typed 
NULL, or reject null list children until they can be represented faithfully.



##########
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:
   The child field's Arrow type is lost when it becomes a bare literal. For 
example, a valid non-null struct containing an `Int32(None)` field now emits 
`named_struct('a', NULL)` (or `{a: NULL}`), which replans as `Struct(a: Null)`, 
not `Struct(a: Int32)`: `parse_value` treats bare NULL as `ScalarValue::Null`, 
and `NamedStructFunc` derives its fields from argument types. This changes the 
output schema and type-dependent expressions such as `arrow_typeof`. Preserve 
the declared child types with appropriate casts, and return an unsupported 
error when a type cannot be represented faithfully.



##########
datafusion/sql/src/unparser/expr.rs:
##########
@@ -732,6 +732,10 @@ impl Unparser<'_> {
             "named_struct must have an even number of arguments"
         );
 
+        if !self.dialect.supports_dictionary_syntax() {
+            return self.function_to_sql_internal("named_struct", args);

Review Comment:
   The new function-call path does not escape backslashes in field-name 
strings. MySQL input `named_struct('a\\nb', x)` has a literal backslash 
followed by `n` in its field name, but the emitted name literal is `'a\nb'`; 
reparsing turns that into a newline. MySQL's [backslash-escape 
behavior](https://github.com/apache/datafusion-sqlparser-rs/blob/ba850faff0d7bde0193e4c8635fdf5155a801d97/src/dialect/mysql.rs#L76-L81)
 is shown on the linked default branch and was also checked at the pinned 
v0.63.0 tag; BigQuery enables it too. Please escape backslashes through the 
dialect string-literal hooks and add a SQL-text round-trip regression case.



##########
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) => {

Review Comment:
   This guard also accepts valid Arrow structs that `named_struct` cannot 
represent: a zero-field struct such as `StructArray::new_empty_fields(1, 
None)`, or a struct with an empty field name. These now return successful SQL 
such as `named_struct()` or `named_struct('', 1)`, but 
`NamedStructFunc::return_field_from_args` explicitly rejects both schemas; the 
dictionary forms go through the same planner. Please retain the 
unsupported-scalar error for these cases unless a round-trippable 
representation is implemented.



##########
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),
+                        ])
+                    })
+                    .collect::<Result<Vec<_>>>()?
+                    .concat();
+                self.named_struct_to_sql(&args)

Review Comment:
   A dialect's custom `named_struct` handler is ignored when the expression is 
constant-folded into a struct literal, so optimization can change the emitted 
dialect syntax. Consult `scalar_function_to_sql_overrides` here before falling 
back to `named_struct_to_sql`.



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