Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ExprSchema extraction of metadata for Cast expressions. #13305

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;