Skip to content
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

Raise IndexError and KeyError for invalid column accessors #272

Merged
merged 2 commits into from
Dec 9, 2024
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
6 changes: 3 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ jobs:
- name: Build python packages
run: |
# arro3-core needs to be first
uv run maturin develop -m arro3-core/Cargo.toml
uv run maturin develop -m arro3-compute/Cargo.toml
uv run maturin develop -m arro3-io/Cargo.toml
uv run maturin dev -m arro3-core/Cargo.toml
uv run maturin dev -m arro3-compute/Cargo.toml
uv run maturin dev -m arro3-io/Cargo.toml

- name: Deploy docs
env:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ jobs:
- name: Build rust submodules
run: |
# Note: core module must be first, because it's depended on by others
uv run maturin develop -m arro3-core/Cargo.toml
uv run maturin develop -m arro3-compute/Cargo.toml
uv run maturin develop -m arro3-io/Cargo.toml
uv run maturin dev -m arro3-core/Cargo.toml
uv run maturin dev -m arro3-compute/Cargo.toml
uv run maturin dev -m arro3-io/Cargo.toml

- name: Run python tests
run: |
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This is the changelog for arro3. pyo3-arrow has a separate changelog.

## [0.4.4] - 2024-12-09

### Bug fixes :bug:

- Raise IndexError and KeyError for invalid column access https://github.com/kylebarron/arro3/pull/272

## [0.4.3] - 2024-11-21

### What's Changed
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ resolver = "2"

[workspace.package]
# Package version for arro3-*, not for pyo3-arrow
version = "0.4.3"
version = "0.4.4"
authors = ["Kyle Barron <[email protected]>"]
edition = "2021"
homepage = "https://kylebarron.dev/arro3"
Expand Down
6 changes: 3 additions & 3 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
rm -rf .venv
uv sync
# Note: need to install core first because others depend on core
uv run maturin develop -m arro3-core/Cargo.toml
uv run maturin develop -m arro3-compute/Cargo.toml
uv run maturin develop -m arro3-io/Cargo.toml
uv run maturin dev -m arro3-core/Cargo.toml
uv run maturin dev -m arro3-compute/Cargo.toml
uv run maturin dev -m arro3-io/Cargo.toml
uv run mkdocs serve
```

Expand Down
19 changes: 15 additions & 4 deletions pyo3-arrow/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::Arc;

use arrow_array::{Datum, RecordBatchIterator, RecordBatchReader};
use arrow_schema::{ArrowError, Field, FieldRef, Fields, Schema, SchemaRef};
use pyo3::exceptions::PyValueError;
use pyo3::exceptions::{PyIndexError, PyKeyError, PyValueError};
use pyo3::prelude::*;

use crate::array_reader::PyArrayReader;
Expand Down Expand Up @@ -162,10 +162,21 @@ pub(crate) enum FieldIndexInput {
}

impl FieldIndexInput {
pub fn into_position(self, schema: &Schema) -> PyArrowResult<usize> {
/// This will additionally check that the input is valid against the given schema.
///
/// This will raise a KeyError if the provided name does not exist, or an IndexError if the
/// provided integer index is out of bounds.
pub fn into_position(self, schema: &Schema) -> PyResult<usize> {
match self {
Self::Name(name) => Ok(schema.index_of(name.as_ref())?),
Self::Position(position) => Ok(position),
Self::Name(name) => schema
.index_of(name.as_ref())
.map_err(|err| PyKeyError::new_err(err.to_string())),
Self::Position(position) => {
if position >= schema.fields().len() {
return Err(PyIndexError::new_err("Index out of range").into());
}
Ok(position)
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/core/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ def test_table_getitem():
a = pa.chunked_array([[1, 2, 3, 4]])
b = pa.chunked_array([["a", "b", "c", "d"]])
table = Table.from_pydict({"a": a, "b": b})

assert a == pa.chunked_array(table["a"])
assert b == pa.chunked_array(table["b"])
assert a == pa.chunked_array(table[0])
assert b == pa.chunked_array(table[1])

with pytest.raises(KeyError):
table["foo"]

with pytest.raises(IndexError):
table[10]


def test_table_from_arrays():
a = pa.array([1, 2, 3, 4])
Expand Down
Loading