Skip to content

Commit 1c085c3

Browse files
authored
perf: resolve aliased field once during metadata inference (apache#26156)
## Which issue does this PR close? Related to apache#24264. This isolates the alias single-resolution optimization from the broader, closed apache#24316. ## Rationale for this change Deriving a field for nested aliases resolves each child twice: once through `metadata()` and once through `to_field()`. Since `metadata()` itself calls `to_field()`, the work grows exponentially with alias depth. This affects schema inference and planning; no row execution path changes. ## What changes are included in this PR? Resolve the aliased child field once and read its metadata from that same field before applying explicit alias metadata. A 12-level regression test records 4,096 source-field lookups before the change and one after it, while checking metadata override, preserved metadata, output name, type, and nullability. ## What is the testing strategy for this PR? - `cargo fmt --all --check` - `cargo clippy --all-targets --all-features -- -D warnings` - `cargo test -p datafusion-expr --lib` (282 passed) - Full documented extended workspace command with initialized test-data submodules and Arrow/Parquet fixtures (passed) - Independent read-only review: approved with no blocking findings. ## Are there any user-facing changes? No schema or API behavior change; this reduces planning cost.
1 parent d2bd93c commit 1c085c3

1 file changed

Lines changed: 59 additions & 5 deletions

File tree

‎datafusion/expr/src/expr_schema.rs‎

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -537,15 +537,13 @@ impl ExprSchemable for Expr {
537537
metadata,
538538
..
539539
}) => {
540-
let mut combined_metadata = expr.metadata(schema)?;
540+
let field = expr.to_field(schema)?.1;
541+
let mut combined_metadata = FieldMetadata::from(field.metadata());
541542
if let Some(metadata) = metadata {
542543
combined_metadata.extend(metadata.clone());
543544
}
544545

545-
Ok(expr
546-
.to_field(schema)
547-
.map(|(_, f)| f)?
548-
.with_field_metadata(&combined_metadata))
546+
Ok(field.with_field_metadata(&combined_metadata))
549547
}
550548
Expr::Negative(expr) => expr.to_field(schema).map(|(_, f)| f),
551549
Expr::Column(c) => schema.field_from_column(c).map(Arc::clone),
@@ -1286,6 +1284,62 @@ mod tests {
12861284
assert_eq!(expr.metadata(&schema).unwrap(), alias_metadata);
12871285
}
12881286

1287+
#[test]
1288+
fn test_nested_alias_resolves_child_field_once() -> Result<()> {
1289+
#[derive(Debug)]
1290+
struct CountingSchema {
1291+
field: FieldRef,
1292+
lookups: std::cell::Cell<usize>,
1293+
}
1294+
1295+
impl ExprSchema for CountingSchema {
1296+
fn nullable(&self, _col: &Column) -> Result<bool> {
1297+
Ok(self.field.is_nullable())
1298+
}
1299+
1300+
fn field_from_column(&self, _col: &Column) -> Result<&FieldRef> {
1301+
self.lookups.set(self.lookups.get() + 1);
1302+
Ok(&self.field)
1303+
}
1304+
}
1305+
1306+
let schema = CountingSchema {
1307+
field: Arc::new(Field::new("value", DataType::Int32, false).with_metadata(
1308+
HashMap::from([
1309+
("override".to_string(), "base".to_string()),
1310+
("keep".to_string(), "base".to_string()),
1311+
]),
1312+
)),
1313+
lookups: std::cell::Cell::new(0),
1314+
};
1315+
let mut expr = col("value");
1316+
for index in 0..12 {
1317+
let metadata = Some(FieldMetadata::from(HashMap::from([(
1318+
"override".to_string(),
1319+
index.to_string(),
1320+
)])));
1321+
expr = if index == 11 {
1322+
expr.alias_qualified_with_metadata(
1323+
Some("outer"),
1324+
format!("alias_{index}"),
1325+
metadata,
1326+
)
1327+
} else {
1328+
expr.alias_with_metadata(format!("alias_{index}"), metadata)
1329+
};
1330+
}
1331+
1332+
let (relation, field) = expr.to_field(&schema)?;
1333+
assert_eq!(schema.lookups.get(), 1);
1334+
assert_eq!(relation, Some(TableReference::bare("outer")));
1335+
assert_eq!(field.name(), "alias_11");
1336+
assert_eq!(field.data_type(), &DataType::Int32);
1337+
assert!(!field.is_nullable());
1338+
assert_eq!(field.metadata().get("override"), Some(&"11".to_string()));
1339+
assert_eq!(field.metadata().get("keep"), Some(&"base".to_string()));
1340+
Ok(())
1341+
}
1342+
12891343
#[test]
12901344
fn test_expr_placeholder() {
12911345
let schema = MockExprSchema::new();

0 commit comments

Comments
 (0)