ozankabak commented on code in PR #11506: URL: https://github.com/apache/datafusion/pull/11506#discussion_r1687127355
########## datafusion/physical-expr-common/src/physical_expr.rs: ########## @@ -191,6 +193,33 @@ pub fn with_new_children_if_necessary( } } +/// Rewrites an expression according to new schema; i.e. changes the columns it +/// refers to with the column at corresponding index in the new schema. Returns +/// an error if the given schema has fewer columns than the original schema. +/// Note that the resulting expression may not be valid if data types in the +/// new schema is incompatible with expression nodes. +pub fn with_new_schema( + expr: Arc<dyn PhysicalExpr>, + schema: &SchemaRef, +) -> Result<Arc<dyn PhysicalExpr>> { + Ok(expr + .transform_up(|expr| { + if let Some(col) = expr.as_any().downcast_ref::<Column>() { + let idx = col.index(); + let Some(field) = schema.fields().get(idx) else { + return plan_err!( Review Comment: This function can be called (in theory) by any compatible superset schema. If the caller gives a schema that is narrower, it is surely and error (as the return value reflects). Since the error is triggerred by a bad argument, and the function is called during planning time, we decided to go with `plan_err`. I will add an entry in my notes to revisit this in the future -- if it turns out this error is surely indicative of a bug in all use cases, we should change it to `internal_err`. -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org