kosiew commented on code in PR #26156:
URL: https://github.com/apache/datafusion/pull/26156#discussion_r4399629319
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -1278,6 +1276,54 @@ mod tests {
assert_eq!(expr.metadata(&schema).unwrap(), alias_metadata);
}
+ #[test]
+ fn test_nested_alias_resolves_child_field_once() -> Result<()> {
+ #[derive(Debug)]
+ struct CountingSchema {
+ field: FieldRef,
+ lookups: std::cell::Cell<usize>,
+ }
+
+ impl ExprSchema for CountingSchema {
+ fn nullable(&self, _col: &Column) -> Result<bool> {
+ Ok(self.field.is_nullable())
+ }
+
+ fn field_from_column(&self, _col: &Column) -> Result<&FieldRef> {
+ self.lookups.set(self.lookups.get() + 1);
+ Ok(&self.field)
+ }
+ }
+
+ let schema = CountingSchema {
+ field: Arc::new(Field::new("value", DataType::Int32,
false).with_metadata(
+ HashMap::from([
+ ("override".to_string(), "base".to_string()),
+ ("keep".to_string(), "base".to_string()),
+ ]),
+ )),
+ lookups: std::cell::Cell::new(0),
+ };
+ let mut expr = col("value");
+ for index in 0..12 {
+ let Expr::Alias(alias) = expr.alias(format!("alias_{index}")) else
{
Review Comment:
Optional simplification: Could we use
`expr.alias_with_metadata(format!("alias_{index}"), Some(...))` directly? It
performs the same alias construction and metadata attachment, avoiding the
destructuring and `unreachable!()` branch while preserving the existing
assertions.
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -1278,6 +1276,54 @@ mod tests {
assert_eq!(expr.metadata(&schema).unwrap(), alias_metadata);
}
+ #[test]
+ fn test_nested_alias_resolves_child_field_once() -> Result<()> {
+ #[derive(Debug)]
+ struct CountingSchema {
+ field: FieldRef,
+ lookups: std::cell::Cell<usize>,
+ }
+
+ impl ExprSchema for CountingSchema {
+ fn nullable(&self, _col: &Column) -> Result<bool> {
+ Ok(self.field.is_nullable())
+ }
+
+ fn field_from_column(&self, _col: &Column) -> Result<&FieldRef> {
+ self.lookups.set(self.lookups.get() + 1);
+ Ok(&self.field)
+ }
+ }
+
+ let schema = CountingSchema {
+ field: Arc::new(Field::new("value", DataType::Int32,
false).with_metadata(
+ HashMap::from([
+ ("override".to_string(), "base".to_string()),
+ ("keep".to_string(), "base".to_string()),
+ ]),
+ )),
+ lookups: std::cell::Cell::new(0),
+ };
+ let mut expr = col("value");
+ for index in 0..12 {
+ let Expr::Alias(alias) = expr.alias(format!("alias_{index}")) else
{
+ unreachable!();
+ };
+ expr = Expr::Alias(alias.with_metadata(Some(FieldMetadata::from(
+ HashMap::from([("override".to_string(), index.to_string())]),
+ ))));
+ }
+
+ let field = expr.to_field(&schema)?.1;
Review Comment:
Optional coverage: Consider constructing the outermost alias with
`alias_qualified_with_metadata(Some("outer"), format!("alias_{index}"),
Some(...))`, then destructuring `to_field` into `(relation, field)` and
asserting `assert_eq!(relation, Some(TableReference::bare("outer")))`. This
would additionally verify qualifier preservation while keeping the existing
field-name, lookup-count, and metadata assertions unchanged.
--
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]