-
Notifications
You must be signed in to change notification settings - Fork 150
Refresh PyVortex to be more Arrow focussed #7505
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
Draft
gatesn
wants to merge
4
commits into
develop
Choose a base branch
from
ngates/pyvortex-refresh
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,3 @@ | ||
| # Changelog | ||
|
|
||
| For older releases, see the full [release history on GitHub](https://github.com/vortex-data/vortex/releases). | ||
|
|
||
| ```{toctree} | ||
| --- | ||
| maxdepth: 1 | ||
| --- | ||
| ``` | ||
| See the full [release history on GitHub](https://github.com/vortex-data/vortex/releases). |
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 |
|---|---|---|
|
|
@@ -29,25 +29,50 @@ Use {func}`~vortex.open` to lazily open a Vortex file: | |
|
|
||
| ### As an Arrow Table | ||
|
|
||
| {meth}`.VortexFile.to_arrow` returns a {class}`pyarrow.RecordBatchReader`. Call | ||
| {meth}`~pyarrow.RecordBatchReader.read_all` to collect into a {class}`pyarrow.Table`: | ||
| {meth}`.VortexFile.to_table` collects the scan into a {class}`pyarrow.Table`: | ||
|
|
||
| ```{doctest} pycon | ||
| >>> table = f.to_arrow().read_all() | ||
| >>> table = f.to_table() | ||
| >>> table.num_rows | ||
| 1000 | ||
| ``` | ||
|
|
||
| {meth}`.VortexFile.to_arrow` returns a streaming {class}`pyarrow.RecordBatchReader`. | ||
|
|
||
| ### Column Projection | ||
|
|
||
| Read only the columns you need: | ||
|
|
||
| ```{doctest} pycon | ||
| >>> table = f.to_arrow(['tip_amount', 'fare_amount']).read_all() | ||
| >>> table = f.to_table(columns=['tip_amount', 'fare_amount']) | ||
| >>> table.column_names | ||
| ['tip_amount', 'fare_amount'] | ||
| ``` | ||
|
|
||
| ### Filters | ||
|
|
||
| Vortex expressions are the stable pushdown API. PyVortex plans them against the file schema before | ||
| the scan runs: | ||
|
|
||
| ```{doctest} pycon | ||
| >>> table = f.to_table(columns=['tip_amount'], filter=vx.col('tip_amount') > 10) | ||
| >>> table.num_rows > 0 | ||
| True | ||
| ``` | ||
|
|
||
| PyArrow compute expressions are accepted as compatibility input. PyVortex converts them through | ||
| Substrait, then runs the same Vortex planner: | ||
|
|
||
| ```{doctest} pycon | ||
| >>> import pyarrow.compute as pc | ||
| >>> table = f.to_table(columns=['tip_amount'], filter=pc.field('tip_amount') > 10) | ||
| >>> table.num_rows > 0 | ||
| True | ||
| ``` | ||
|
|
||
| Use `filter_policy="pushdown"` to raise when a PyArrow expression cannot be pushed into Vortex. Use | ||
| `filter_policy="fallback"` to read the rows and apply the PyArrow filter after the scan. | ||
|
Comment on lines
+73
to
+74
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. what is default? |
||
|
|
||
| ### Streaming Record Batches | ||
|
|
||
| Iterate over record batches for streaming processing: | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
gatesn marked this conversation as resolved.
|
||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| //! Expression planning against an input scope. | ||
|
|
||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_bail; | ||
|
|
||
| use crate::dtype::DType; | ||
| use crate::expr::Expression; | ||
| use crate::expr::transform::coerce_expression; | ||
|
|
||
| /// Plan an expression against an input [`DType`]. | ||
| /// | ||
| /// Planning typeifies the expression by inserting casts required by scalar functions, simplifies | ||
| /// the resulting tree, and verifies that the planned expression has a valid return type for the | ||
| /// provided scope. | ||
| pub fn plan_expression(expr: Expression, scope: &DType) -> VortexResult<Expression> { | ||
| let expr = coerce_expression(expr, scope)?; | ||
| let expr = expr.optimize_recursive(scope)?; | ||
| expr.return_dtype(scope)?; | ||
| Ok(expr) | ||
| } | ||
|
|
||
| /// Plan a filter expression against an input [`DType`]. | ||
| /// | ||
| /// This performs the same planning pass as [`plan_expression`] and then requires the expression to | ||
| /// return a Boolean value. | ||
| pub fn plan_filter_expression(expr: Expression, scope: &DType) -> VortexResult<Expression> { | ||
| let expr = plan_expression(expr, scope)?; | ||
| let dtype = expr.return_dtype(scope)?; | ||
| if !matches!(dtype, DType::Bool(_)) { | ||
| vortex_bail!("filter expression must return bool, got {}", dtype); | ||
| } | ||
| Ok(expr) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use vortex_error::VortexResult; | ||
|
|
||
| use crate::dtype::DType; | ||
| use crate::dtype::Nullability::NonNullable; | ||
| use crate::dtype::Nullability::Nullable; | ||
| use crate::dtype::PType; | ||
| use crate::dtype::StructFields; | ||
| use crate::expr::col; | ||
| use crate::expr::lit; | ||
| use crate::expr::plan_expression; | ||
| use crate::expr::plan_filter_expression; | ||
| use crate::scalar::Scalar; | ||
| use crate::scalar_fn::ScalarFnVTableExt; | ||
| use crate::scalar_fn::fns::binary::Binary; | ||
| use crate::scalar_fn::fns::cast::Cast; | ||
| use crate::scalar_fn::fns::operators::Operator; | ||
|
|
||
| fn scope() -> DType { | ||
| DType::Struct( | ||
| StructFields::new( | ||
| ["i32", "i64", "u8", "flag"].into(), | ||
| vec![ | ||
| DType::Primitive(PType::I32, NonNullable), | ||
| DType::Primitive(PType::I64, NonNullable), | ||
| DType::Primitive(PType::U8, NonNullable), | ||
| DType::Bool(NonNullable), | ||
| ], | ||
| ), | ||
| NonNullable, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn mixed_numeric_comparison_inserts_cast() -> VortexResult<()> { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::Lt, [col("i32"), col("i64")]); | ||
|
|
||
| let planned = plan_filter_expression(expr, &scope)?; | ||
|
|
||
| assert!(planned.child(0).is::<Cast>()); | ||
| assert_eq!( | ||
| planned.child(0).return_dtype(&scope)?, | ||
| DType::Primitive(PType::I64, NonNullable) | ||
| ); | ||
| assert!(!planned.child(1).is::<Cast>()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn mixed_numeric_arithmetic_inserts_casts() -> VortexResult<()> { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::Add, [col("u8"), col("i32")]); | ||
|
|
||
| let planned = plan_expression(expr, &scope)?; | ||
|
|
||
| assert!(planned.child(0).is::<Cast>()); | ||
| assert_eq!( | ||
| planned.return_dtype(&scope)?, | ||
| DType::Primitive(PType::I64, NonNullable) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn literal_values_are_coerced_against_column_types() -> VortexResult<()> { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::Eq, [col("i32"), lit(1i64)]); | ||
|
|
||
| let planned = plan_filter_expression(expr, &scope)?; | ||
|
|
||
| assert!(!planned.child(0).is::<Cast>()); | ||
| assert!(planned.child(1).is::<Cast>()); | ||
| assert_eq!( | ||
| planned.child(1).return_dtype(&scope)?, | ||
| DType::Primitive(PType::I32, NonNullable) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn null_literals_are_typed_from_context() -> VortexResult<()> { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::Eq, [col("i32"), lit(Scalar::null(DType::Null))]); | ||
|
|
||
| let planned = plan_filter_expression(expr, &scope)?; | ||
|
|
||
| assert!(planned.child(1).is::<Cast>()); | ||
| assert_eq!( | ||
| planned.child(1).return_dtype(&scope)?, | ||
| DType::Primitive(PType::I32, Nullable) | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn boolean_and_preserves_boolean_inputs() -> VortexResult<()> { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::And, [col("flag"), col("flag")]); | ||
|
|
||
| let planned = plan_filter_expression(expr, &scope)?; | ||
|
|
||
| assert_eq!(planned.return_dtype(&scope)?, DType::Bool(NonNullable)); | ||
| assert!(!planned.child(0).is::<Cast>()); | ||
| assert!(!planned.child(1).is::<Cast>()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn filter_planning_rejects_non_boolean_outputs() { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::Add, [col("i32"), lit(1i32)]); | ||
|
|
||
| let err = plan_filter_expression(expr, &scope).unwrap_err(); | ||
|
|
||
| assert!( | ||
| err.to_string() | ||
| .contains("filter expression must return bool") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn logical_operators_reject_non_boolean_inputs() { | ||
| let scope = scope(); | ||
| let expr = Binary.new_expr(Operator::And, [col("i32"), col("i64")]); | ||
|
|
||
| let err = plan_filter_expression(expr, &scope).unwrap_err(); | ||
|
|
||
| assert!( | ||
| err.to_string() | ||
| .contains("logical operation requires boolean operands") | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.