Skip to content

Commit

Permalink
Raise IndexError and KeyError for invalid column accessors (#272)
Browse files Browse the repository at this point in the history
* Raise IndexError and KeyError for invalid column accessors

* Update changelog
  • Loading branch information
kylebarron authored Dec 9, 2024
1 parent 3709db9 commit 4f330d4
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 17 deletions.
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

0 comments on commit 4f330d4

Please sign in to comment.