diff --git a/datafusion/core/src/physical_plan/planner.rs b/datafusion/core/src/physical_plan/planner.rs index 1a165b1ce0e7..577e76a92927 100644 --- a/datafusion/core/src/physical_plan/planner.rs +++ b/datafusion/core/src/physical_plan/planner.rs @@ -128,13 +128,13 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result { name += "END"; Ok(name) } - Expr::Cast { expr, .. } => { - // CAST does not change the name of an expression. It just changes the type. - create_physical_name(expr, false) + Expr::Cast { expr, data_type } => { + let expr = create_physical_name(expr, false)?; + Ok(format!("CAST({} AS {:?})", expr, data_type)) } - Expr::TryCast { expr, .. } => { - // TRY_CAST does not change the name of an expression. It just changes the type. - create_physical_name(expr, false) + Expr::TryCast { expr, data_type } => { + let expr = create_physical_name(expr, false)?; + Ok(format!("TRY_CAST({} AS {:?})", expr, data_type)) } Expr::Not(expr) => { let expr = create_physical_name(expr, false)?; diff --git a/datafusion/core/tests/dataframe_functions.rs b/datafusion/core/tests/dataframe_functions.rs index 0d3631b18a70..19694285cc31 100644 --- a/datafusion/core/tests/dataframe_functions.rs +++ b/datafusion/core/tests/dataframe_functions.rs @@ -667,14 +667,14 @@ async fn test_fn_substr() -> Result<()> { async fn test_cast() -> Result<()> { let expr = cast(col("b"), DataType::Float64); let expected = vec![ - "+--------+", - "| test.b |", - "+--------+", - "| 1 |", - "| 10 |", - "| 10 |", - "| 100 |", - "+--------+", + "+-------------------------+", + "| CAST(test.b AS Float64) |", + "+-------------------------+", + "| 1 |", + "| 10 |", + "| 10 |", + "| 100 |", + "+-------------------------+", ]; assert_fn_batches!(expr, expected); diff --git a/datafusion/core/tests/sql/aggregates.rs b/datafusion/core/tests/sql/aggregates.rs index deabe5c12eda..7e0e785da805 100644 --- a/datafusion/core/tests/sql/aggregates.rs +++ b/datafusion/core/tests/sql/aggregates.rs @@ -462,11 +462,11 @@ async fn csv_query_external_table_sum() { "SELECT SUM(CAST(c7 AS BIGINT)), SUM(CAST(c8 AS BIGINT)) FROM aggregate_test_100"; let actual = execute_to_batches(&ctx, sql).await; let expected = vec![ - "+----------------------------+----------------------------+", - "| SUM(aggregate_test_100.c7) | SUM(aggregate_test_100.c8) |", - "+----------------------------+----------------------------+", - "| 13060 | 3017641 |", - "+----------------------------+----------------------------+", + "+-------------------------------------------+-------------------------------------------+", + "| SUM(CAST(aggregate_test_100.c7 AS Int64)) | SUM(CAST(aggregate_test_100.c8 AS Int64)) |", + "+-------------------------------------------+-------------------------------------------+", + "| 13060 | 3017641 |", + "+-------------------------------------------+-------------------------------------------+", ]; assert_batches_eq!(expected, &actual); } diff --git a/datafusion/core/tests/sql/functions.rs b/datafusion/core/tests/sql/functions.rs index a1594393cc21..e7bcb24c744d 100644 --- a/datafusion/core/tests/sql/functions.rs +++ b/datafusion/core/tests/sql/functions.rs @@ -43,12 +43,12 @@ async fn csv_query_cast() -> Result<()> { let actual = execute_to_batches(&ctx, sql).await; let expected = vec![ - "+------------------------+", - "| aggregate_test_100.c12 |", - "+------------------------+", - "| 0.39144436 |", - "| 0.3887028 |", - "+------------------------+", + "+-----------------------------------------+", + "| CAST(aggregate_test_100.c12 AS Float32) |", + "+-----------------------------------------+", + "| 0.39144436 |", + "| 0.3887028 |", + "+-----------------------------------------+", ]; assert_batches_eq!(expected, &actual); @@ -98,14 +98,14 @@ async fn query_concat() -> Result<()> { let sql = "SELECT concat(c1, '-hi-', cast(c2 as varchar)) FROM test"; let actual = execute_to_batches(&ctx, sql).await; let expected = vec![ - "+--------------------------------------+", - "| concat(test.c1,Utf8(\"-hi-\"),test.c2) |", - "+--------------------------------------+", - "| -hi-0 |", - "| a-hi-1 |", - "| aa-hi- |", - "| aaa-hi-3 |", - "+--------------------------------------+", + "+----------------------------------------------------+", + "| concat(test.c1,Utf8(\"-hi-\"),CAST(test.c2 AS Utf8)) |", + "+----------------------------------------------------+", + "| -hi-0 |", + "| a-hi-1 |", + "| aa-hi- |", + "| aaa-hi-3 |", + "+----------------------------------------------------+", ]; assert_batches_eq!(expected, &actual); Ok(()) @@ -133,14 +133,14 @@ async fn query_array() -> Result<()> { let sql = "SELECT make_array(c1, cast(c2 as varchar)) FROM test"; let actual = execute_to_batches(&ctx, sql).await; let expected = vec![ - "+----------------------------+", - "| makearray(test.c1,test.c2) |", - "+----------------------------+", - "| [, 0] |", - "| [a, 1] |", - "| [aa, ] |", - "| [aaa, 3] |", - "+----------------------------+", + "+------------------------------------------+", + "| makearray(test.c1,CAST(test.c2 AS Utf8)) |", + "+------------------------------------------+", + "| [, 0] |", + "| [a, 1] |", + "| [aa, ] |", + "| [aaa, 3] |", + "+------------------------------------------+", ]; assert_batches_eq!(expected, &actual); Ok(()) diff --git a/datafusion/core/tests/sql/parquet.rs b/datafusion/core/tests/sql/parquet.rs index 8bec4f1dd3db..51304f60876c 100644 --- a/datafusion/core/tests/sql/parquet.rs +++ b/datafusion/core/tests/sql/parquet.rs @@ -31,18 +31,18 @@ async fn parquet_query() { let sql = "SELECT id, CAST(string_col AS varchar) FROM alltypes_plain"; let actual = execute_to_batches(&ctx, sql).await; let expected = vec![ - "+----+---------------------------+", - "| id | alltypes_plain.string_col |", - "+----+---------------------------+", - "| 4 | 0 |", - "| 5 | 1 |", - "| 6 | 0 |", - "| 7 | 1 |", - "| 2 | 0 |", - "| 3 | 1 |", - "| 0 | 0 |", - "| 1 | 1 |", - "+----+---------------------------+", + "+----+-----------------------------------------+", + "| id | CAST(alltypes_plain.string_col AS Utf8) |", + "+----+-----------------------------------------+", + "| 4 | 0 |", + "| 5 | 1 |", + "| 6 | 0 |", + "| 7 | 1 |", + "| 2 | 0 |", + "| 3 | 1 |", + "| 0 | 0 |", + "| 1 | 1 |", + "+----+-----------------------------------------+", ]; assert_batches_eq!(expected, &actual);