LiaCastaneda commented on code in PR #27099:
URL: https://github.com/apache/datafusion/pull/27099#discussion_r4617819396


##########
datafusion/substrait/src/logical_plan/consumer/expr/literal.rs:
##########
@@ -573,10 +578,244 @@ pub(crate) fn from_substrait_literal(
     Ok(scalar_value)
 }
 
+/// Renames the fields of the struct literals in `exprs` after those of the 
first
+/// expression in `exprs` that isn't a literal and has a struct type, such as 
a column.
+///
+/// For example, in `coalesce(s, [])` with `s: List<Struct{a, b}>`:
+///
+/// - before: `s` is `List<Struct{a, b}>`, `[]` is `List<Struct{c0, c1}>`
+/// - after: both are `List<Struct{a, b}>`
+///
+/// This is needed because a Substrait literal's type has no field names, so 
the
+/// consumer names its fields `c0`, `c1`, ..., while a column keeps the names 
from its
+/// table. DataFusion matches struct fields by name, so it can't combine
+/// `Struct{a, b}` with `Struct{c0, c1}`. Substrait matches them by position, 
so giving
+/// the literal the column's names doesn't change what the plan means.
+///
+/// A literal whose shape differs from the column's (another number of fields, 
other
+/// nesting) is left as it is, and so is everything if no expression has a 
struct type.
+pub(crate) fn name_struct_literals_like_first_struct_expr<'a>(
+    exprs: impl IntoIterator<Item = &'a mut Expr>,
+    input_schema: &DFSchema,
+) {
+    let mut exprs = exprs.into_iter().collect::<Vec<_>>();

Review Comment:
   This makes sense. I measured it again with and without the fix:
   
   | n terms | before | after |
   |---|---|---|
   | 250 | 3.1 ms | 0.6 ms |
   | 500 | 10.5 ms | 0.6 ms |
   | 1000 | 42 ms | 1.3 ms |



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