Skip to content

Commit

Permalink
Fix ExprSchema extraction of metadata for Cast expressions. (apache#1…
Browse files Browse the repository at this point in the history
…3305)

* test(12733): reproducers for schema bugs

* fix(12733): properly extract field metadata from Cast expr

* test(12733): update metadata preservation test, for new contract (a.k.a. cast preserves field metadata)
  • Loading branch information
wiedld authored Nov 11, 2024
1 parent 7ebd993 commit be19afc
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
7 changes: 3 additions & 4 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ impl ExprSchemable for Expr {
match self {
Expr::Column(c) => Ok(schema.metadata(c)?.clone()),
Expr::Alias(Alias { expr, .. }) => expr.metadata(schema),
Expr::Cast(Cast { expr, .. }) => expr.metadata(schema),
_ => Ok(HashMap::new()),
}
}
Expand Down Expand Up @@ -681,13 +682,11 @@ mod tests {
.with_data_type(DataType::Int32)
.with_metadata(meta.clone());

// col and alias should be metadata-preserving
// col, alias, and cast should be metadata-preserving
assert_eq!(meta, expr.metadata(&schema).unwrap());
assert_eq!(meta, expr.clone().alias("bar").metadata(&schema).unwrap());

// cast should drop input metadata since the type has changed
assert_eq!(
HashMap::new(),
meta,
expr.clone()
.cast_to(&DataType::Int64, &schema)
.unwrap()
Expand Down
69 changes: 69 additions & 0 deletions datafusion/sqllogictest/test_files/metadata.slt
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,74 @@ LIMIT 1;
2020-09-08T13:42:29.190855123Z



# distinct (aggregate) alone
query P
SELECT
DISTINCT ts as dist
FROM table_with_metadata;
----
2020-09-08T13:42:29.190855123

# cast alone
query D
SELECT
ts::DATE as casted
FROM table_with_metadata;
----
2020-09-08
2020-09-08
2020-09-08

# Regression test: distinct with cast
query D
SELECT DISTINCT (ts::DATE) AS dist
FROM table_with_metadata;
----
2020-09-08



# count distinct with group by
query II
SELECT
id AS grp,
COUNT(DISTINCT nonnull_name) as dist
FROM table_with_metadata
GROUP BY grp
order by 1 asc nulls last;
----
1 1
3 1
NULL 1

# count (not distinct) & cast, with group by
query TI
SELECT
CAST(id AS TEXT) AS grp,
COUNT(nonnull_name) as dist
FROM table_with_metadata
GROUP BY grp
order by 1 asc nulls last;
----
1 1
3 1
NULL 1

# Regression test: count distinct & cast, with group by
query TI
SELECT
CAST(id AS TEXT) AS grp,
COUNT(DISTINCT nonnull_name) as dist
FROM table_with_metadata
GROUP BY grp
order by 1 asc nulls last;
----
1 1
3 1
NULL 1



statement ok
drop table table_with_metadata;

0 comments on commit be19afc

Please sign in to comment.