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().

Expr

An expression describes how to filter rows when reading an array from a file.

root

Create an expression that refers to the identity scope.

column

Create an expression that refers to a column by its name.

literal

Create an expression that represents a literal value.

get_item

Extract a named field from a struct expression.

not_

Negate a Boolean expression.

and_

True if both arguments are true.

or_

True if either argument is true.

and_collect

Combine expressions with logical AND using a balanced tree.

or_collect

Combine expressions with logical OR using a balanced tree.

eq

True where both arguments are equal.

not_eq

True where the arguments are not equal.

gt

True where left is greater than right.

gt_eq

True where left is greater than or equal to right.

lt

True where left is less than right.

lt_eq

True where left is less than or equal to right.

add

The sum of the arguments, erroring on overflow.

sub

The difference between the arguments.

mul

The product of the arguments.

div

left divided by right.

between

True where child lies between lower and upper.

is_null

Checks which elements of its child are null.

is_not_null

Creates an expression that checks for non-null values.

fill_null

Replace null values with a fill value.

like

A SQL LIKE expression.

ilike

A case-insensitive SQL ILIKE expression.

not_like

A negated SQL NOT LIKE expression.

not_ilike

A negated case-insensitive SQL NOT ILIKE expression.

byte_length

The byte length of each element, akin to SQL OCTET_LENGTH().

select

Project only the named fields of a struct expression.

select_exclude

Project every field of a struct expression except the named ones.

pack

Pack expressions into a struct with named fields.

merge

Merge struct expressions into a single struct.

list_contains

True where the list contains the given value.

list_length

The number of elements in each list, akin to SQL CARDINALITY().

list_sum

The sum of the elements of each list.

case_when

A CASE WHEN expression.

zip_

Select element-wise between two expressions based on a boolean mask.

mask

Null out the elements of an expression where the mask is true.

cast

Cast an expression to a compatible type.

ext_storage

Extract the storage values of an extension-typed expression.

variant_get

Extract a path from a Variant expression.

deserialize

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:

vortex.Expr

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:

vortex.Expr

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:

vortex.Expr

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 to root().

Return type:

vortex.Expr

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:

vortex.Expr

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:
  • left (Expr) – A boolean expression.

  • right (Expr) – A boolean expression.

Return type:

vortex.Expr

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:
  • left (Expr) – A boolean expression.

  • right (Expr) – A boolean expression.

Return type:

vortex.Expr

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:

None if exprs is empty.

Return type:

vortex.Expr or None

vortex.expr.or_collect(exprs)

Combine expressions with logical OR using a balanced tree.

Parameters:

exprs (Iterable) – The boolean expressions to combine.

Returns:

None if exprs is empty.

Return type:

vortex.Expr or None

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

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

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

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

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

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

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

vortex.expr.sub(left, right)

The difference between the arguments. :param left: :type left: Any :param right: :type right: Any

Return type:

vortex.Expr

vortex.expr.mul(left, right)

The product of the arguments. :param left: :type left: Any :param right: :type right: Any

Return type:

vortex.Expr

vortex.expr.div(left, right)

left divided by right. :param left: :type left: Any :param right: :type right: Any

Return type:

vortex.Expr

vortex.expr.between(child, lower, upper, *, lower_strict=False, upper_strict=False)

True where child lies between lower and upper.

Parameters:
  • child (Any) – The expression to test.

  • lower (Any) – The lower bound.

  • upper (Any) – The upper bound.

  • lower_strict (bool) – If True, compare the lower bound with < instead of <=.

  • upper_strict (bool) – If True, compare the upper bound with < instead of <=.

Return type:

vortex.Expr

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

vortex.expr.is_not_null(child)

Creates an expression that checks for non-null values.

Parameters:

child (vortex.Expr)

Return type:

vortex.Expr

vortex.expr.fill_null(child, fill_value)

Replace null values with a fill value.

Parameters:
  • child (Any)

  • fill_value (Any)

Return type:

vortex.Expr

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 LIKE expression.

Parameters:
  • child (Any) – The string expression to match.

  • pattern (Any) – The SQL LIKE pattern, where % matches any run of characters and _ matches any single character.

Return type:

vortex.Expr

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 ILIKE expression.

Parameters:
  • child (Any)

  • pattern (Any)

Return type:

vortex.Expr

vortex.expr.not_like(child, pattern)

A negated SQL NOT LIKE expression.

Parameters:
  • child (Any)

  • pattern (Any)

Return type:

vortex.Expr

vortex.expr.not_ilike(child, pattern)

A negated case-insensitive SQL NOT ILIKE expression.

Parameters:
  • child (Any)

  • pattern (Any)

Return type:

vortex.Expr

vortex.expr.byte_length(child)

The byte length of each element, akin to SQL OCTET_LENGTH().

Parameters:

child (Any)

Return type:

vortex.Expr

Structs

vortex.expr.select(fields, child=None)

Project only the named fields of a struct expression.

Parameters:
  • fields (str or Iterable of str) – The field names to keep.

  • child (vortex.Expr, optional) – The struct expression to project. Defaults to root().

Return type:

vortex.Expr

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:
  • fields (str or Iterable of str) – The field names to drop.

  • child (vortex.Expr, optional) – The struct expression to project. Defaults to root().

Return type:

vortex.Expr

vortex.expr.pack(fields, *, nullable=False)

Pack expressions into a struct with named fields.

Parameters:
  • fields (dict or Iterable of (str, Any)) – The field names and their expressions.

  • nullable (bool) – Whether the resulting struct is nullable.

Return type:

vortex.Expr

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 (Iterable of vortex.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:

vortex.Expr

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

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

vortex.expr.list_sum(child, *, skip_nans=True)

The sum of the elements of each list.

Follows SQL SUM semantics 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) – If True (the default), NaN float elements are skipped. Otherwise a single NaN poisons the list’s sum.

Return type:

vortex.Expr

Conditionals and conversions

vortex.expr.case_when(when_then, else_value=None)

A CASE WHEN expression.

Parameters:
  • when_then (Iterable of (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:

vortex.Expr

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 where mask is true.

  • if_false (Any) – The value used where mask is false.

Return type:

vortex.Expr

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

vortex.expr.cast(child, dtype)

Cast an expression to a compatible type.

Parameters:

child (Expr) – The expression to cast.

Return type:

vortex.Expr

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

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, or Iterable of str or int) – 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:

vortex.Expr

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 by vortex.expr.Expr.serialize().

Return type:

vortex.Expr

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

column()

__getitem__(name, /)

Extract a field of a struct array.

Parameters:
  • name (str) – The name of the field.

Return type:

vortex.Expr

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:

bytes

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