Skip to content

Commit 3a22022

Browse files
committed
Test CASE metadata across supported leaf expressions
1 parent 1ca200b commit 3a22022

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

‎datafusion/expr/src/expr_schema.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,47 @@ mod tests {
17171717
Ok(())
17181718
}
17191719

1720+
#[test]
1721+
fn test_case_field_metadata_leaf_branches() -> Result<()> {
1722+
let metadata = HashMap::from([("type".to_string(), "structured".to_string())]);
1723+
let field = Arc::new(
1724+
Field::new("value", DataType::Int32, false).with_metadata(metadata.clone()),
1725+
);
1726+
let schema = DFSchema::from_unqualified_fields(
1727+
vec![
1728+
Field::new("value", DataType::Int32, false)
1729+
.with_metadata(metadata.clone()),
1730+
]
1731+
.into(),
1732+
HashMap::new(),
1733+
)?;
1734+
1735+
let branches = [
1736+
Expr::OuterReferenceColumn(Arc::clone(&field), Column::from_name("outer")),
1737+
Expr::ScalarVariable(Arc::clone(&field), vec!["value".to_string()]),
1738+
Expr::Placeholder(Placeholder::new_with_field(
1739+
"$1".to_string(),
1740+
Some(Arc::clone(&field)),
1741+
)),
1742+
Expr::LambdaVariable(LambdaVariable::new("arg".into(), Some(field))),
1743+
];
1744+
for branch in branches {
1745+
let case = when(lit(true), col("value")).otherwise(branch)?;
1746+
assert_eq!(case.to_field(&schema)?.1.metadata(), &metadata);
1747+
}
1748+
1749+
let null_first =
1750+
when(lit(true), lit(ScalarValue::Null)).otherwise(col("value"))?;
1751+
assert_eq!(null_first.to_field(&schema)?.1.metadata(), &metadata);
1752+
1753+
let wrong_type = when(lit(true), col("value")).otherwise(Expr::Literal(
1754+
ScalarValue::Boolean(Some(false)),
1755+
Some(FieldMetadata::from(metadata)),
1756+
))?;
1757+
assert!(wrong_type.to_field(&schema)?.1.metadata().is_empty());
1758+
Ok(())
1759+
}
1760+
17201761
#[test]
17211762
fn test_alias_metadata_is_preserved_in_field_metadata() {
17221763
let schema = MockExprSchema::new().with_data_type(DataType::Int32);

0 commit comments

Comments
 (0)