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

Preserve the Cast expression in columnize_expr #4137

Merged
merged 2 commits into from
Nov 8, 2022
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: 7 additions & 0 deletions datafusion/core/tests/sql/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ async fn execute_sql(sql: &str) -> Vec<RecordBatch> {
execute_to_batches(&ctx, sql).await
}

#[tokio::test]
async fn cast_from_subquery() -> Result<()> {
let actual = execute_sql("SELECT cast(c as varchar) FROM (SELECT 1 as c)").await;
assert_eq!(&DataType::Utf8, actual[0].schema().field(0).data_type());
Ok(())
}

#[tokio::test]
async fn cast_tinyint() -> Result<()> {
let actual = execute_sql("SELECT cast(10 as tinyint)").await;
Expand Down
6 changes: 5 additions & 1 deletion datafusion/expr/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::logical_plan::{
Limit, Partitioning, Projection, Repartition, Sort, Subquery, SubqueryAlias, Union,
Values, Window,
};
use crate::{Expr, ExprSchemable, LogicalPlan, LogicalPlanBuilder};
use crate::{Cast, Expr, ExprSchemable, LogicalPlan, LogicalPlanBuilder};
use arrow::datatypes::{DataType, TimeUnit};
use datafusion_common::{
Column, DFField, DFSchema, DFSchemaRef, DataFusionError, Result, ScalarValue,
Expand Down Expand Up @@ -676,6 +676,10 @@ pub fn columnize_expr(e: Expr, input_schema: &DFSchema) -> Expr {
Expr::Alias(inner_expr, name) => {
Expr::Alias(Box::new(columnize_expr(*inner_expr, input_schema)), name)
}
Expr::Cast(Cast { expr, data_type }) => Expr::Cast(Cast {
expr: Box::new(columnize_expr(*expr, input_schema)),
data_type,
}),
Expr::ScalarSubquery(_) => e.clone(),
_ => match e.display_name() {
Ok(name) => match input_schema.field_with_unqualified_name(&name) {
Expand Down
11 changes: 11 additions & 0 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2974,6 +2974,17 @@ mod tests {
);
}

#[test]
fn cast_from_subquery() {
quick_test(
"SELECT CAST (a AS FLOAT) FROM (SELECT 1 AS a)",
"Projection: CAST(a AS Float32)\
\n Projection: a\
\n Projection: Int64(1) AS a\
\n EmptyRelation",
);
}

#[test]
fn cast_to_invalid_decimal_type() {
// precision == 0
Expand Down