Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
ARROW-8060: [Python] Make dataset Expression objects serializable#6702
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
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b826431
make dataset expressions serializable
kszucs 8194d76
use the existing scalar wrappers
kszucs 549bd89
expose both variants of cast expression's target type
kszucs 53bce90
address review comments
kszucs cb2566a
revert wrapping cast-like cast expression
kszucs 87fb175
rebase
kszucs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 |
|---|---|---|
| @@ -27,6 +27,8 @@ from pyarrow.lib cimport * | ||
| from pyarrow.includes.libarrow_dataset cimport * | ||
| from pyarrow.compat import frombytes, tobytes | ||
| from pyarrow._fs cimport FileSystem, FileInfo, FileSelector | ||
| from pyarrow.types import (is_null, is_boolean, is_integer, is_floating, | ||
| is_string) | ||
| def _forbid_instantiation(klass, subclasses_instead=True): | ||
| @@ -1305,6 +1307,10 @@ cdef class UnaryExpression(Expression): | ||
| Expression.init(self, sp) | ||
| self.unary = <CUnaryExpression*> sp.get() | ||
| @property | ||
| def operand(self): | ||
| return Expression.wrap(self.unary.operand()) | ||
| cdef class BinaryExpression(Expression): | ||
| @@ -1332,7 +1338,9 @@ cdef class ScalarExpression(Expression): | ||
| shared_ptr[CScalar] scalar | ||
| shared_ptr[CScalarExpression] expr | ||
| if isinstance(value, bool): | ||
| if value is None: | ||
| scalar.reset(new CNullScalar()) | ||
| elif isinstance(value, bool): | ||
| scalar = MakeScalar(<c_bool>value) | ||
| elif isinstance(value, float): | ||
| scalar = MakeScalar(<double>value) | ||
| @@ -1350,6 +1358,14 @@ cdef class ScalarExpression(Expression): | ||
| Expression.init(self, sp) | ||
| self.scalar = <CScalarExpression*> sp.get() | ||
| @property | ||
| def value(self): | ||
| cdef ScalarValue scalar = pyarrow_wrap_scalar(self.scalar.value()) | ||
| return scalar.as_py() | ||
| def __reduce__(self): | ||
| return ScalarExpression, (self.value,) | ||
| cdef class FieldExpression(Expression): | ||
| @@ -1366,9 +1382,13 @@ cdef class FieldExpression(Expression): | ||
| Expression.init(self, sp) | ||
| self.scalar = <CFieldExpression*> sp.get() | ||
| @property | ||
| def name(self): | ||
| return frombytes(self.scalar.name()) | ||
| def __reduce__(self): | ||
| return FieldExpression, (self.name,) | ||
| cpdef enum CompareOperator: | ||
| Equal = <int8_t> CCompareOperator_EQUAL | ||
| @@ -1399,9 +1419,15 @@ cdef class ComparisonExpression(BinaryExpression): | ||
| BinaryExpression.init(self, sp) | ||
| self.comparison = <CComparisonExpression*> sp.get() | ||
| @property | ||
| def op(self): | ||
| return <CompareOperator> self.comparison.op() | ||
| def __reduce__(self): | ||
| return ComparisonExpression, ( | ||
| self.op, self.left_operand, self.right_operand | ||
| ) | ||
| cdef class IsValidExpression(UnaryExpression): | ||
| @@ -1410,34 +1436,90 @@ cdef class IsValidExpression(UnaryExpression): | ||
| expr = make_shared[CIsValidExpression](operand.unwrap()) | ||
| self.init(<shared_ptr[CExpression]> expr) | ||
| def __reduce__(self): | ||
| return IsValidExpression, (self.operand,) | ||
| cdef class CastExpression(UnaryExpression): | ||
| cdef CCastExpression *cast | ||
| def __init__(self, Expression operand not None, DataType to not None, | ||
| bint safe=True): | ||
| # TODO(kszucs): safe is consistently used across pyarrow, but on long | ||
| # term we should expose the CastOptions object | ||
| cdef: | ||
| CastOptions options | ||
| shared_ptr[CExpression] expr | ||
| options = CastOptions.safe() if safe else CastOptions.unsafe() | ||
| expr.reset(new CCastExpression( | ||
| operand.unwrap(), | ||
| pyarrow_unwrap_data_type(to), | ||
| options.unwrap() | ||
| )) | ||
| expr.reset( | ||
| new CCastExpression( | ||
| operand.unwrap(), | ||
| pyarrow_unwrap_data_type(to), | ||
| options.unwrap() | ||
| ) | ||
| ) | ||
| self.init(expr) | ||
| cdef void init(self, const shared_ptr[CExpression]& sp): | ||
| UnaryExpression.init(self, sp) | ||
| self.cast = <CCastExpression*> sp.get() | ||
| @property | ||
| def to(self): | ||
| """ | ||
| Target DataType or Expression of the cast operation. | ||
| Returns | ||
| ------- | ||
| DataType or Expression | ||
| """ | ||
| cdef shared_ptr[CDataType] typ = self.cast.to_type() | ||
| if typ.get() != nullptr: | ||
| return pyarrow_wrap_data_type(typ) | ||
| else: | ||
| raise TypeError( | ||
| 'Cannot determine the target type of the cast expression' | ||
| ) | ||
| @property | ||
| def safe(self): | ||
| """ | ||
| Whether to check for overflows or other unsafe conversions. | ||
| Returns | ||
| ------- | ||
| bool | ||
| """ | ||
| cdef CastOptions options = CastOptions.wrap(self.cast.options()) | ||
| return options.is_safe() | ||
| def __reduce__(self): | ||
| return CastExpression, (self.operand, self.to, self.safe) | ||
kszucs marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| cdef class InExpression(UnaryExpression): | ||
| def __init__(self, Expression operand not None, Array haystack not None): | ||
| cdef: | ||
| CInExpression* inexpr | ||
| def __init__(self, Expression operand not None, Array set_ not None): | ||
| cdef shared_ptr[CExpression] expr | ||
| expr.reset( | ||
| new CInExpression(operand.unwrap(), pyarrow_unwrap_array(haystack)) | ||
| new CInExpression(operand.unwrap(), pyarrow_unwrap_array(set_)) | ||
| ) | ||
| self.init(expr) | ||
| cdef void init(self, const shared_ptr[CExpression]& sp): | ||
| UnaryExpression.init(self, sp) | ||
| self.inexpr = <CInExpression*> sp.get() | ||
| @property | ||
| def set_(self): | ||
| return pyarrow_wrap_array(self.inexpr.set()) | ||
| def __reduce__(self): | ||
| return InExpression, (self.operand, self.set_) | ||
| cdef class NotExpression(UnaryExpression): | ||
| @@ -1446,30 +1528,27 @@ cdef class NotExpression(UnaryExpression): | ||
| expr = CMakeNotExpression(operand.unwrap()) | ||
| self.init(<shared_ptr[CExpression]> expr) | ||
| def __reduce__(self): | ||
| return NotExpression, (self.operand,) | ||
| cdef class AndExpression(BinaryExpression): | ||
| def __init__(self, Expression left not None, Expression right not None, | ||
| *additional_operands): | ||
| cdef: | ||
| Expression operand | ||
| vector[shared_ptr[CExpression]] exprs | ||
| exprs.push_back(left.unwrap()) | ||
| exprs.push_back(right.unwrap()) | ||
| for operand in additional_operands: | ||
| exprs.push_back(operand.unwrap()) | ||
| self.init(CMakeAndExpression(exprs)) | ||
| def __init__(self, Expression left not None, Expression right not None): | ||
| cdef shared_ptr[CAndExpression] expr | ||
| expr.reset(new CAndExpression(left.unwrap(), right.unwrap())) | ||
| self.init(<shared_ptr[CExpression]> expr) | ||
kszucs marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| def __reduce__(self): | ||
| return AndExpression, (self.left_operand, self.right_operand) | ||
| cdef class OrExpression(BinaryExpression): | ||
| def __init__(self, Expression left not None, Expression right not None, | ||
| *additional_operands): | ||
| cdef: | ||
| Expression operand | ||
| vector[shared_ptr[CExpression]] exprs | ||
| exprs.push_back(left.unwrap()) | ||
| exprs.push_back(right.unwrap()) | ||
| for operand in additional_operands: | ||
| exprs.push_back(operand.unwrap()) | ||
| self.init(CMakeOrExpression(exprs)) | ||
| def __init__(self, Expression left not None, Expression right not None): | ||
| cdef shared_ptr[COrExpression] expr | ||
| expr.reset(new COrExpression(left.unwrap(), right.unwrap())) | ||
| self.init(<shared_ptr[CExpression]> expr) | ||
| def __reduce__(self): | ||
| return OrExpression, (self.left_operand, self.right_operand) | ||
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.