Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vortex-python/python/vortex/_lib/expr.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ def root() -> Expr: ...
def literal(dtype: DType, value: ScalarPyType) -> Expr: ...
def not_(child: Expr) -> Expr: ...
def and_(left: Expr, right: Expr) -> Expr: ...
def cast(child: Expr, dtype: DType) -> Expr: ...
4 changes: 2 additions & 2 deletions vortex-python/python/vortex/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
# SPDX-FileCopyrightText: Copyright the Vortex contributors


from ._lib.expr import Expr, and_, column, literal, not_, root # pyright: ignore[reportMissingModuleSource]
from ._lib.expr import Expr, and_, cast, column, literal, not_, root # pyright: ignore[reportMissingModuleSource]

__all__ = ["Expr", "column", "literal", "root", "not_", "and_"]
__all__ = ["Expr", "column", "literal", "root", "not_", "and_", "cast"]
49 changes: 46 additions & 3 deletions vortex-python/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::*;
use vortex::dtype::{DType, Nullability, PType};
use vortex::expr;
use vortex::expr::{Binary, Expression, GetItem, Operator, VTableExt, and, lit, not};

use crate::dtype::PyDType;
Expand All @@ -23,6 +24,7 @@ pub(crate) fn init(py: Python, parent: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(literal, &m)?)?;
m.add_function(wrap_pyfunction!(not_, &m)?)?;
m.add_function(wrap_pyfunction!(and_, &m)?)?;
m.add_function(wrap_pyfunction!(cast, &m)?)?;
m.add_class::<PyExpr>()?;

Ok(())
Expand Down Expand Up @@ -215,7 +217,7 @@ pub fn literal<'py>(
#[pyfunction]
pub fn root() -> PyExpr {
PyExpr {
inner: vortex::expr::root(),
inner: expr::root(),
}
}

Expand Down Expand Up @@ -249,7 +251,7 @@ pub fn column<'py>(name: &Bound<'py, PyString>) -> PyResult<Bound<'py, PyExpr>>
Bound::new(
py,
PyExpr {
inner: vortex::expr::get_item(name, vortex::expr::root()),
inner: expr::get_item(name, expr::root()),
},
)
}
Expand Down Expand Up @@ -301,7 +303,10 @@ pub fn not_(child: PyExpr) -> PyResult<PyExpr> {
///
/// Parameters
/// ----------
/// child : :class:`Any`
/// left : :class:`Expr`
/// A boolean expression.
///
/// right : :class:`Expr`
/// A boolean expression.
///
/// Returns
Expand All @@ -323,3 +328,41 @@ pub fn and_(left: PyExpr, right: PyExpr) -> PyResult<PyExpr> {
inner: and(left.inner, right.inner),
})
}

/// Cast an expression to a compatible type.
///
/// Parameters
/// ----------
/// child : :class:`Expr`
/// The expression to cast.
///
/// Returns
/// -------
/// :class:`vortex.Expr`
///
/// Examples
/// --------
///
/// Cast to a wider integer type:
///
/// ```python
/// >>> 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:
///
/// ```python
/// >>> 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 ...>
Comment on lines +360 to +361
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason we omit the types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joseph-isaacs what do you mean? I think this is just the default printed form for any expression.

/// ```
#[pyfunction]
pub fn cast(child: PyExpr, dtype: PyDType) -> PyResult<PyExpr> {
Ok(PyExpr {
inner: expr::cast(child.into_inner(), dtype.into_inner()),
})
}
18 changes: 16 additions & 2 deletions vortex-python/test/test_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import vortex as vx
import vortex.expr as ve
from vortex.scan import RepeatedScan


Expand All @@ -20,14 +21,19 @@ def record(x: int, columns: list[str] | set[str] | None = None) -> dict[str, int


@pytest.fixture(scope="session")
def vxscan(tmpdir_factory) -> vx.RepeatedScan: # pyright: ignore[reportUnknownParameterType, reportMissingParameterType]
def vxscan(vxfile: vx.VortexFile) -> vx.RepeatedScan:
return vxfile.to_repeated_scan()


@pytest.fixture(scope="session")
def vxfile(tmpdir_factory) -> vx.VortexFile: # pyright: ignore[reportUnknownParameterType, reportMissingParameterType]
fname = tmpdir_factory.mktemp("data") / "foo.vortex" # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]

if not os.path.exists(fname): # pyright: ignore[reportUnknownArgumentType]
a = pa.array([record(x) for x in range(1_000)])
arr = vx.compress(vx.array(a))
vx.io.write(arr, str(fname)) # pyright: ignore[reportUnknownArgumentType]
return vx.open(str(fname)).to_repeated_scan() # pyright: ignore[reportUnknownArgumentType]
return vx.open(str(fname)) # pyright: ignore[reportUnknownArgumentType]


def test_execute(vxscan: RepeatedScan):
Expand All @@ -50,3 +56,11 @@ def test_scalar_at(vxscan: RepeatedScan):
"bool": True,
"float": math.sqrt(10),
}


def test_scan_with_cast(vxfile: vx.VortexFile):
actual = vxfile.scan(expr=ve.cast(ve.column("index"), vx.int_(16)) == ve.literal(vx.int_(16), 1)).read_all()
expected = pa.array(
[{"index": 1, "string": pa.scalar("1", pa.string_view()), "bool": False, "float": math.sqrt(1)}]
)
assert str(actual.to_arrow_array()) == str(expected)
Loading