Expressions¶
Vortex expressions represent simple filtering conditions on the rows of a Vortex array. For example, the following expression represents the set of rows for which the age column lies between 23 and 55:
>>> import vortex.expr
>>> age = vortex.expr.column("age")
>>> (23 > age) & (age < 55)
Expressions are picklable, so a filter built in one process can be sent to another (for example to a
multiprocessing worker or a Ray task). Pickling uses the same protobuf wire format exposed by
vortex.expr.Expr.serialize() and vortex.expr.deserialize().
An expression describes how to filter rows when reading an array from a file. |
|
Create an expression that refers to the identity scope. |
|
Create an expression that refers to a column by its name. |
|
Create an expression that represents a literal value. |
|
Extract a named field from a struct expression. |
|
Negate a Boolean expression. |
|
True if both arguments are true. |
|
True if either argument is true. |
|
Combine expressions with logical AND using a balanced tree. |
|
Combine expressions with logical OR using a balanced tree. |
|
True where both arguments are equal. |
|
True where the arguments are not equal. |
|
True where left is greater than right. |
|
True where left is greater than or equal to right. |
|
True where left is less than right. |
|
True where left is less than or equal to right. |
|
The sum of the arguments, erroring on overflow. |
|
The difference between the arguments. |
|
The product of the arguments. |
|
left divided by right. |
|
True where child lies between lower and upper. |
|
Checks which elements of its child are null. |
|
Creates an expression that checks for non-null values. |
|
Replace null values with a fill value. |
|
A SQL |
|
A case-insensitive SQL |
|
A negated SQL |
|
A negated case-insensitive SQL |
|
The byte length of each element, akin to SQL |
|
Project only the named fields of a struct expression. |
|
Project every field of a struct expression except the named ones. |
|
Pack expressions into a struct with named fields. |
|
Merge struct expressions into a single struct. |
|
True where the list contains the given value. |
|
The number of elements in each list, akin to SQL |
|
The sum of the elements of each list. |
|
A |
|
Select element-wise between two expressions based on a boolean mask. |
|
Null out the elements of an expression where the mask is true. |
|
Cast an expression to a compatible type. |
|
Extract the storage values of an extension-typed expression. |
|
Extract a path from a Variant expression. |
|
Rebuild an expression from its protobuf wire format. |
Leaves and scope¶
- vortex.expr.root()¶
Create an expression that refers to the identity scope.
That is, it returns the full input that the extension is run against.
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.root() <vortex.Expr object at ...>
- vortex.expr.column(name)¶
Create an expression that refers to a column by its name.
- Parameters:
name (
str) – The name of the column.- Return type:
Examples
>>> import vortex.expr as ve >>> ve.column("age") <vortex.Expr object at ...>
See also
Use
vortex.expr.Expr.__getitem__()to retrieve a field of a struct array.
- vortex.expr.literal(dtype, value)¶
Create an expression that represents a literal value.
- Parameters:
dtype (
vortex.DType) – The data type of the literal value.value (
Any) – The literal value.
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.literal(vx.int_(), 42) <vortex.Expr object at ...>
- vortex.expr.get_item(field, child=None)¶
Extract a named field from a struct expression.
- Parameters:
field (
str) – The name of the field.child (
vortex.Expr, optional) – The struct expression to read from. Defaults toroot().
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.get_item("yy", ve.column("y")) <vortex.Expr object at ...>
Boolean logic¶
- vortex.expr.not_(child)¶
Negate a Boolean expression.
- Parameters:
child (
Any) – A boolean expression.- Return type:
Examples
>>> import vortex.expr as ve >>> import vortex as vx >>> ve.not_(ve.literal(vx.int_(), 42) == ve.literal(vx.int_(), 42)) <vortex.Expr object at ...>
- vortex.expr.and_(left, right)¶
True if both arguments are true.
- Parameters:
- Return type:
Examples
>>> import vortex.expr as ve >>> import vortex as vx >>> ve.and_(ve.literal(vx.bool_(), True), ve.literal(vx.bool_(), True)) <vortex.Expr object at ...>
- vortex.expr.or_(left, right)¶
True if either argument is true.
- Parameters:
- Return type:
Examples
>>> import vortex.expr as ve >>> import vortex as vx >>> ve.or_(ve.literal(vx.bool_(), True), ve.literal(vx.bool_(), False)) <vortex.Expr object at ...>
- vortex.expr.and_collect(exprs)¶
Combine expressions with logical AND using a balanced tree.
- Parameters:
exprs (
Iterable) – The boolean expressions to combine.- Returns:
Noneifexprsis empty.- Return type:
vortex.ExprorNone
- vortex.expr.or_collect(exprs)¶
Combine expressions with logical OR using a balanced tree.
- Parameters:
exprs (
Iterable) – The boolean expressions to combine.- Returns:
Noneifexprsis empty.- Return type:
vortex.ExprorNone
Comparisons and arithmetic¶
- vortex.expr.eq(left, right)¶
True where both arguments are equal. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.not_eq(left, right)¶
True where the arguments are not equal. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.gt(left, right)¶
True where left is greater than right. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.gt_eq(left, right)¶
True where left is greater than or equal to right. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.lt(left, right)¶
True where left is less than right. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.lt_eq(left, right)¶
True where left is less than or equal to right. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.add(left, right)¶
The sum of the arguments, erroring on overflow. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.sub(left, right)¶
The difference between the arguments. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.mul(left, right)¶
The product of the arguments. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.div(left, right)¶
left divided by right. :param left: :type left:
Any:param right: :type right:Any- Return type:
- vortex.expr.between(child, lower, upper, *, lower_strict=False, upper_strict=False)¶
True where child lies between lower and upper.
- Parameters:
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.between(ve.column("age"), 23, 55) <vortex.Expr object at ...>
Nullability¶
- vortex.expr.is_null(child)¶
Checks which elements of its child are null.
- Parameters:
child (
Expr) – Any expression.- Return type:
- vortex.expr.is_not_null(child)¶
Creates an expression that checks for non-null values.
- Parameters:
child (
vortex.Expr)- Return type:
- vortex.expr.fill_null(child, fill_value)¶
Replace null values with a fill value.
- Parameters:
child (
Any)fill_value (
Any)
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.fill_null(ve.column("age"), 0) <vortex.Expr object at ...>
Strings¶
- vortex.expr.like(child, pattern)¶
A SQL
LIKEexpression.- Parameters:
child (
Any) – The string expression to match.pattern (
Any) – The SQLLIKEpattern, where%matches any run of characters and_matches any single character.
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.like(ve.column("name"), "Ali%") <vortex.Expr object at ...>
- vortex.expr.ilike(child, pattern)¶
A case-insensitive SQL
ILIKEexpression.- Parameters:
child (
Any)pattern (
Any)
- Return type:
- vortex.expr.not_like(child, pattern)¶
A negated SQL
NOT LIKEexpression.- Parameters:
child (
Any)pattern (
Any)
- Return type:
- vortex.expr.not_ilike(child, pattern)¶
A negated case-insensitive SQL
NOT ILIKEexpression.- Parameters:
child (
Any)pattern (
Any)
- Return type:
- vortex.expr.byte_length(child)¶
The byte length of each element, akin to SQL
OCTET_LENGTH().- Parameters:
child (
Any)- Return type:
Structs¶
- vortex.expr.select(fields, child=None)¶
Project only the named fields of a struct expression.
- Parameters:
child (
vortex.Expr, optional) – The struct expression to project. Defaults toroot().
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.select(["name", "age"]) <vortex.Expr object at ...>
- vortex.expr.select_exclude(fields, child=None)¶
Project every field of a struct expression except the named ones.
- Parameters:
child (
vortex.Expr, optional) – The struct expression to project. Defaults toroot().
- Return type:
- vortex.expr.pack(fields, *, nullable=False)¶
Pack expressions into a struct with named fields.
- Parameters:
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.pack({"id": ve.column("user_id"), "constant": 42}) <vortex.Expr object at ...>
- vortex.expr.merge(exprs, *, duplicate_handling='error')¶
Merge struct expressions into a single struct.
- Parameters:
exprs (
Iterableofvortex.Expr) – The struct expressions to merge.duplicate_handling (
str) – Either"error"(the default) to reject duplicated field names, or"rightmost"to take the value from the right-most expression.
- Return type:
Lists¶
- vortex.expr.list_contains(child, value)¶
True where the list contains the given value.
- Parameters:
child (
Any) – A list expression.value (
Any) – The value to search for.
- Return type:
- vortex.expr.list_length(child)¶
The number of elements in each list, akin to SQL
CARDINALITY().- Parameters:
child (
Any) – A list or fixed-size-list expression.- Return type:
- vortex.expr.list_sum(child, *, skip_nans=True)¶
The sum of the elements of each list.
Follows SQL
SUMsemantics per list: null lists, empty lists, and lists whose elements are all null yield null, and null elements are skipped.- Parameters:
child (
Any) – A list or fixed-size-list expression.skip_nans (
bool) – IfTrue(the default), NaN float elements are skipped. Otherwise a single NaN poisons the list’s sum.
- Return type:
Conditionals and conversions¶
- vortex.expr.case_when(when_then, else_value=None)¶
A
CASE WHENexpression.- Parameters:
when_then (
Iterableof (Any,Any)) – One or more(condition, value)pairs, evaluated in order.else_value (
Any, optional) – The value to use when no condition matches. Defaults to null.
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.case_when([(ve.column("age") > 21, "adult")], else_value="minor") <vortex.Expr object at ...>
- vortex.expr.zip_(mask, if_true, if_false)¶
Select element-wise between two expressions based on a boolean mask.
- Parameters:
mask (
Any) – A boolean expression.if_true (
Any) – The value used wheremaskis true.if_false (
Any) – The value used wheremaskis false.
- Return type:
- vortex.expr.mask(child, mask)¶
Null out the elements of an expression where the mask is true.
- Parameters:
child (
Any) – The expression to mask.mask (
Any) – A boolean expression.
- Return type:
- vortex.expr.cast(child, dtype)¶
Cast an expression to a compatible type.
- Parameters:
child (
Expr) – The expression to cast.- Return type:
Examples
Cast to a wider integer type:
>>> import vortex.expr as ve >>> import vortex as vx >>> ve.cast(ve.literal(vx.int_(8), 1), vx.int_(16)) <vortex.Expr object at ...>
Cast to a wider floating-point type:
>>> import vortex.expr as ve >>> import vortex as vx >>> ve.cast(ve.literal(vx.float_(16), 3.145), vx.float_(64)) <vortex.Expr object at ...>
- vortex.expr.ext_storage(child)¶
Extract the storage values of an extension-typed expression.
- Parameters:
child (
Any)- Return type:
- vortex.expr.variant_get(child, path, dtype=None)¶
Extract a path from a Variant expression.
Missing paths, traversal mismatches, and failed casts all return null.
- Parameters:
child (
Any) – A Variant expression.path (
str,int, orIterableofstrorint) – The path to extract. Strings select object fields, integers select list elements.dtype (
vortex.DType, optional) – The requested output type. When omitted, the result is a nullable Variant.
- Return type:
Examples
>>> import vortex.expr as ve >>> ve.variant_get(ve.column("payload"), ["user", "id"]) <vortex.Expr object at ...>
Serialization¶
- vortex.expr.deserialize(data)¶
Rebuild an expression from its protobuf wire format.
- Parameters:
data (
bytes) – Bytes produced byvortex.expr.Expr.serialize().- Return type:
Examples
>>> import vortex.expr as ve >>> expr = ve.column("age") > 21 >>> str(ve.deserialize(expr.serialize())) == str(expr) True
The expression class¶
- class vortex.expr.Expr¶
An expression describes how to filter rows when reading an array from a file.
See also
- __getitem__(name, /)¶
Extract a field of a struct array.
- Parameters:
name (
str) – The name of the field.
- Return type:
Examples
>>> import vortex as vx >>> import vortex.expr as ve >>> import pyarrow as pa >>> >>> array = pa.array([ ... {"x": 1, "y": {"yy": "a"}}, ... {"x": 2, "y": {"yy": "b"}}, ... ]) >>> >>> vx.io.write(vx.array(array), '/tmp/foo.vortex') >>> (vx.file.open('/tmp/foo.vortex') ... .scan(expr=vx.expr.column("y")["yy"] == "a") ... .read_all() ... .to_pylist() ... ) [{'x': 1, 'y': {'yy': 'a'}}]
- serialize()¶
Serialize this expression to its Vortex protobuf wire format.
The result can be sent to another process or machine and rebuilt with
vortex.expr.deserialize().- Return type:
- Raises:
RuntimeError – If the expression contains a scalar function that is not serializable.
Examples
>>> import vortex.expr as ve >>> expr = ve.column("age") > 21 >>> str(ve.deserialize(expr.serialize())) == str(expr) True