-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add Field to Expr::Cast -- allow logical expressions to express a cast to an extension type
#18136
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
Open
paleolimbot
wants to merge
23
commits into
apache:main
Choose a base branch
from
paleolimbot:cast-with-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+392
−189
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f5dd7f5
passing tests
paleolimbot 89547f4
clippy
paleolimbot 1e5e1b0
proto
paleolimbot b23bb42
fmt
paleolimbot 4ac0296
use the helper
paleolimbot 32c6d26
clippy
paleolimbot e2e695e
test not suported
paleolimbot 64980ab
rename data type to field
paleolimbot e6a7e90
clippy
paleolimbot 0955d58
maybe better substrait consumer integration
paleolimbot 2e103a5
no need to update this
paleolimbot 00237be
comment about nullability
paleolimbot 2d3aed2
Merge branch 'main' into cast-with-extension
paleolimbot b402c44
nope use the other version
paleolimbot 5657e89
add upgrade guide
paleolimbot 758070d
Merge branch 'main' into cast-with-extension
paleolimbot f4d2673
fmt
paleolimbot 55de0cb
typos
paleolimbot c621f87
Merge branch 'main' into cast-with-extension
paleolimbot 7048c7e
comments
paleolimbot 8afa764
Merge branch 'main' into cast-with-extension
paleolimbot 87ea98a
fix build and format
paleolimbot e45d227
Merge branch 'main' into cast-with-extension
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ use crate::{ | |
|
|
||
| use arrow::datatypes::Schema; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_common::metadata::FieldMetadata; | ||
| use datafusion_common::metadata::{FieldMetadata, format_type_and_metadata}; | ||
| use datafusion_common::{ | ||
| DFSchema, Result, ScalarValue, ToDFSchema, exec_err, not_impl_err, plan_err, | ||
| }; | ||
|
|
@@ -34,7 +34,7 @@ use datafusion_expr::expr::{Alias, Cast, InList, Placeholder, ScalarFunction}; | |
| use datafusion_expr::var_provider::VarType; | ||
| use datafusion_expr::var_provider::is_system_variables; | ||
| use datafusion_expr::{ | ||
| Between, BinaryExpr, Expr, Like, Operator, TryCast, binary_expr, lit, | ||
| Between, BinaryExpr, Expr, ExprSchemable, Like, Operator, TryCast, binary_expr, lit, | ||
| }; | ||
|
|
||
| /// [PhysicalExpr] evaluate DataFusion expressions such as `A + 1`, or `CAST(c1 | ||
|
|
@@ -288,16 +288,44 @@ pub fn create_physical_expr( | |
| }; | ||
| Ok(expressions::case(expr, when_then_expr, else_expr)?) | ||
| } | ||
| Expr::Cast(Cast { expr, data_type }) => expressions::cast( | ||
| create_physical_expr(expr, input_dfschema, execution_props)?, | ||
| input_schema, | ||
| data_type.clone(), | ||
| ), | ||
| Expr::TryCast(TryCast { expr, data_type }) => expressions::try_cast( | ||
| create_physical_expr(expr, input_dfschema, execution_props)?, | ||
| input_schema, | ||
| data_type.clone(), | ||
| ), | ||
| Expr::Cast(Cast { expr, field }) => { | ||
| if !field.metadata().is_empty() { | ||
| let (_, src_field) = expr.to_field(input_dfschema)?; | ||
| return plan_err!( | ||
| "Cast from {} to {} is not supported", | ||
| format_type_and_metadata( | ||
| src_field.data_type(), | ||
| Some(src_field.metadata()), | ||
| ), | ||
| format_type_and_metadata(field.data_type(), Some(field.metadata())) | ||
| ); | ||
| } | ||
|
|
||
| expressions::cast( | ||
| create_physical_expr(expr, input_dfschema, execution_props)?, | ||
| input_schema, | ||
| field.data_type().clone(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does end up tying into #19097: I think they'd work well together, we'd just want to pass the field directly here. |
||
| ) | ||
| } | ||
| Expr::TryCast(TryCast { expr, field }) => { | ||
| if !field.metadata().is_empty() { | ||
| let (_, src_field) = expr.to_field(input_dfschema)?; | ||
| return plan_err!( | ||
| "TryCast from {} to {} is not supported", | ||
| format_type_and_metadata( | ||
| src_field.data_type(), | ||
| Some(src_field.metadata()), | ||
| ), | ||
| format_type_and_metadata(field.data_type(), Some(field.metadata())) | ||
| ); | ||
| } | ||
|
|
||
| expressions::try_cast( | ||
| create_physical_expr(expr, input_dfschema, execution_props)?, | ||
| input_schema, | ||
| field.data_type().clone(), | ||
| ) | ||
| } | ||
| Expr::Not(expr) => { | ||
| expressions::not(create_physical_expr(expr, input_dfschema, execution_props)?) | ||
| } | ||
|
|
@@ -417,7 +445,7 @@ pub fn logical2physical(expr: &Expr, schema: &Schema) -> Arc<dyn PhysicalExpr> { | |
| mod tests { | ||
| use arrow::array::{ArrayRef, BooleanArray, RecordBatch, StringArray}; | ||
| use arrow::datatypes::{DataType, Field}; | ||
|
|
||
| use datafusion_common::datatype::DataTypeExt; | ||
| use datafusion_expr::{Operator, col, lit}; | ||
|
|
||
| use super::*; | ||
|
|
@@ -447,6 +475,41 @@ mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_cast_to_extension_type() -> Result<()> { | ||
| let extension_field_type = Arc::new( | ||
| DataType::FixedSizeBinary(16) | ||
| .into_nullable_field() | ||
| .with_metadata( | ||
| [("ARROW:extension:name".to_string(), "arrow.uuid".to_string())] | ||
| .into(), | ||
| ), | ||
| ); | ||
| let expr = lit("3230e5d4-888e-408b-b09b-831f44aa0c58"); | ||
| let cast_expr = Expr::Cast(Cast::new_from_field( | ||
| Box::new(expr.clone()), | ||
| Arc::clone(&extension_field_type), | ||
| )); | ||
| let err = | ||
| create_physical_expr(&cast_expr, &DFSchema::empty(), &ExecutionProps::new()) | ||
| .unwrap_err(); | ||
| assert!(err.message().contains("arrow.uuid")); | ||
|
|
||
| let try_cast_expr = Expr::TryCast(TryCast::new_from_field( | ||
| Box::new(expr.clone()), | ||
| Arc::clone(&extension_field_type), | ||
| )); | ||
| let err = create_physical_expr( | ||
| &try_cast_expr, | ||
| &DFSchema::empty(), | ||
| &ExecutionProps::new(), | ||
| ) | ||
| .unwrap_err(); | ||
| assert!(err.message().contains("arrow.uuid")); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Test that deeply nested expressions do not cause a stack overflow. | ||
| /// | ||
| /// This test only runs when the `recursive_protection` feature is enabled, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep
data_typeas a deprecated field that we populate fromfield.data_type()for a couple of releases?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could also implement this by adding a
metadata: FieldMetadatafield rather than switching thedata_typeto aFieldRef. We mostly just switchedDataTypetoFieldRefin other places so that's what I did here.