LiaCastaneda commented on code in PR #27099:
URL: https://github.com/apache/datafusion/pull/27099#discussion_r4610966291
##########
datafusion/substrait/src/logical_plan/consumer/expr/literal.rs:
##########
@@ -573,6 +578,248 @@ 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_siblings<'a>(
+ exprs: impl IntoIterator<Item = &'a mut Expr>,
+ input_schema: &DFSchema,
+) {
+ let mut exprs = exprs.into_iter().collect::<Vec<_>>();
+ // the names come from the first expression that isn't a literal and
contains a struct
+ let Some(named_type) = exprs
+ .iter()
+ .filter(|e| !is_literal(e) && !matches!(***e, Expr::Lambda(_)))
+ .filter_map(|e| e.get_type(input_schema).ok())
+ .find(contains_struct)
+ else {
+ return;
+ };
+
+ for expr in exprs.iter_mut() {
+ name_struct_literal_like(expr, &named_type);
+ }
+}
+
+/// If `expr` is a struct-typed literal, possibly aliased, gives it the field
names of
+/// `named_type` when the two have the same shape. A `CASE` whose results are
all
+/// literals counts as a literal: each result is renamed. Anything else is
left unchanged.
+pub(crate) fn name_struct_literal_like(expr: &mut Expr, named_type: &DataType)
{
+ match expr {
+ Expr::Alias(alias) => name_struct_literal_like(&mut alias.expr,
named_type),
+ Expr::Literal(value, _) => {
+ let value_type = value.data_type();
+ if !contains_struct(&value_type) {
+ return;
+ }
+ let Some(renamed_type) = with_field_names_of(&value_type,
named_type) else {
+ return;
+ };
+ // if renaming fails, keep the literal: coercion reports the
mismatch as before
+ if let Ok(renamed) = with_data_type(value, &renamed_type) {
+ *value = renamed;
+ }
+ }
+ Expr::Case(case) if case_results(case).all(is_literal) => {
+ for (_, then) in case.when_then_expr.iter_mut() {
+ name_struct_literal_like(then, named_type);
+ }
+ if let Some(else_expr) = case.else_expr.as_mut() {
+ name_struct_literal_like(else_expr, named_type);
+ }
+ }
+ _ => {}
+ }
+}
+
+/// Returns true if `expr` is a literal, possibly aliased, or a `CASE` whose
results are
+/// all literals.
+pub(crate) fn is_literal(expr: &Expr) -> bool {
+ match expr {
+ Expr::Alias(alias) => is_literal(&alias.expr),
Review Comment:
renamed it to `has_literal_value`
--
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]