CodeSampleX

Exemple

pyarrow 25.0.1: PyArrow Table integer indexing selects columns rather than rows while len(table) returns row count, slice indexing flips to row slicing, and column selection by list raises TypeError unless select() is used

Échantillon vérifié pour pypi pyarrow 25.0.1: PyArrow Table integer indexing selects columns rather than rows while len(table) returns row count, slice…

sha256:256773321b98eae577fe17bcb5eb96a79b0d3bfd7f053fd619c1ba9a1922e4c3

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
3
Clés de signature qui l’ont compilé
3
Environnement déclaré python linux x64 python python pip

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-17
python 3.12 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-18
python 3.12 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1python:3.12-slim@sha256:09f7da3bc104…
2026-09-08

Cas

HOW
Objectif
PyArrow Table integer indexing selects columns rather than rows while len(table) returns row count, slice indexing flips to row slicing, and column selection by list raises TypeError unless select() is used
Paquets
Symboles
  • pyarrow.table
  • pyarrow.Table
  • pyarrow.Table.__getitem__
  • pyarrow.Table.slice
  • pyarrow.Table.select
  • pyarrow.Table.filter
  • pyarrow.record_batch
  • pyarrow.RecordBatch
  • pyarrow.ChunkedArray
  • pyarrow.compute.equal
  • pyarrow.compute.invert
Environnement
python
Créé
2026-08-17T02:16:01Z

Contrat

  1. assert len(table) == 5 and len(list(table)) == 2 and isinstance(table[0], pa.ChunkedArray)
  2. assert table[3] raises IndexError even though 3 < len(table)
  3. assert isinstance(table[0], pa.ChunkedArray) and isinstance(table[0:1], pa.Table)
  4. assert table[['a']] raises TypeError and table.select(['a']).column_names == ['a']
  5. assert isinstance(table[0], pa.ChunkedArray) and isinstance(batch[0], pa.Array)
  6. assert len(table.filter(pc.equal(table['a'], 20))) + len(table.filter(pc.invert(pc.equal(table['a'], 20)))) == 4 != len(table)

Fichiers

  • NOTES.md
  • csx.json
  • requirements.lock
  • requirements.txt
  • test/contract.py

Télécharger l’artefact source (tar.gz)

Code source

NOTES.md
# PyArrow Table Indexing and Tabular Layout Semantics

## Prior Search Result

`search_known_solution` matched sample `sha256:9edb237c0ecdc2c2ff1db0a48f82ff6121180f6f5beada8304796b12985ce100` (covering `StringScalar` comparison with string literals, null scalar truthiness, and pandas metadata degradation). This contract targets a separate behavioral trap in `pyarrow.Table` container access semantics.

## Behavior and Failure Modes

`pyarrow.Table` violates common sequence container intuitions by splitting row and column access across different protocols:
1. `len(table)` reports `table.num_rows`, but `iter(table)` iterates over column `ChunkedArray` instances (`len(list(table)) == table.num_columns != len(table)`).
2. Integer indexing `table[i]` accesses column `i` rather than row `i`, raising `IndexError` when `i >= table.num_columns` even if `i < len(table)`.
3. Slice indexing `table[start:end]` flips axes to perform row slicing and returns a `Table` (meaning `table[0]` is a `ChunkedArray` column, whereas `table[0:1]` is a single-row `Table`).
4. Multi-column list indexing `table[['a']]` raises `TypeError` instead of subsetting columns, which requires `table.select(['a'])`.
5. Column access on `Table` returns `ChunkedArray`, whereas column access on `RecordBatch` returns contiguous `Array`.
6. Predicate filtering with `pyarrow.compute.equal` uses three-valued logic where nulls evaluate to null and are dropped by default from both positive and inverted filters (`len(matched) + len(inverted) != len(table)`).

## Failure Modes

- Code expecting row records via `table[i]` fails loudly with `IndexError: index out of bounds` when `i >= num_columns`, or fails silently with a green build by propagating a column `ChunkedArray` into downstream scalar/record processing logic.
- Code attempting dataframe-style multi-column indexing via `table[['col1', 'col2']]` fails loudly with `TypeError: Index must either be string or integer`.
- Code partitioning rows via positive and inverted compute filters fails silently with a green build by omitting rows containing null values in the filter column from both partitions.
csx.json
{"case":{"believed":"Indexing a PyArrow Table with an integer returns the corresponding row and iterating over a Table yields rows consistent with len(table) reporting the row count.","caseId":"case:sha256:38a1c6a4df0f701c2d561217f3331dc8649039dc19c713538cfa45662d54b682","contract":["assert len(table) == 5 and len(list(table)) == 2 and isinstance(table[0], pa.ChunkedArray)","assert table[3] raises IndexError even though 3 \u003c len(table)","assert isinstance(table[0], pa.ChunkedArray) and isinstance(table[0:1], pa.Table)","assert table[['a']] raises TypeError and table.select(['a']).column_names == ['a']","assert isinstance(table[0], pa.ChunkedArray) and isinstance(batch[0], pa.Array)","assert len(table.filter(pc.equal(table['a'], 20))) + len(table.filter(pc.invert(pc.equal(table['a'], 20)))) == 4 != len(table)"],"goal":"PyArrow Table integer indexing selects columns rather than rows while len(table) returns row count, slice indexing flips to row slicing, and column selection by list raises TypeError unless select() is used","kind":"HOW","packages":["pkg:pypi/pyarrow@25.0.1"],"schemaVersion":1,"symbols":["pyarrow.table","pyarrow.Table","pyarrow.Table.__getitem__","pyarrow.Table.slice","pyarrow.Table.select","pyarrow.Table.filter","pyarrow.record_batch","pyarrow.RecordBatch","pyarrow.ChunkedArray","pyarrow.compute.equal","pyarrow.compute.invert"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","ecosystem":"pypi","executionContext":"python","language":"python","os":"linux","packageManager":"pip","runtime":"python","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/pyarrow@25.0.1"],"schemaVersion":1,"symbols":["pyarrow.table","pyarrow.Table","pyarrow.Table.__getitem__","pyarrow.Table.slice","pyarrow.Table.select","pyarrow.Table.filter","pyarrow.record_batch","pyarrow.RecordBatch","pyarrow.ChunkedArray","pyarrow.compute.equal","pyarrow.compute.invert"],"verifierAdapter":"python@1"}
requirements.lock
pyarrow==25.0.1
requirements.txt
pyarrow==25.0.1
test/contract.py
import pyarrow as pa
import pyarrow.compute as pc


def test_table_len_vs_iteration_and_indexing():
    # A Table with 5 rows and 2 columns
    table = pa.table({
        "a": [10, 20, 30, None, 50],
        "b": ["w", "x", "y", "z", None],
    })

    # len(table) returns row count, but iter(table) yields columns
    assert len(table) == 5
    assert table.num_rows == 5
    assert table.num_columns == 2

    cols = list(table)
    assert len(cols) == 2
    assert len(cols) != len(table)
    assert isinstance(cols[0], pa.ChunkedArray)

    # Integer indexing table[i] retrieves column i as a ChunkedArray, NOT row i
    col0 = table[0]
    assert isinstance(col0, pa.ChunkedArray)
    assert not isinstance(col0, pa.Array)
    assert col0.to_pylist() == [10, 20, 30, None, 50]

    # Indexing beyond column count raises IndexError even though row index < len(table)
    try:
        _ = table[3]
        assert False, "Expected IndexError when indexing table[3]"
    except IndexError:
        pass


def test_slice_vs_integer_index_dimension_flip():
    table = pa.table({
        "a": [10, 20, 30, 40, 50],
        "b": ["w", "x", "y", "z", "v"],
    })

    # table[0] retrieves column 0 (a ChunkedArray of length 5)
    col0 = table[0]
    assert isinstance(col0, pa.ChunkedArray)
    assert len(col0) == 5

    # table[0:1] slices rows across all columns, returning a Table of length 1
    row_slice = table[0:1]
    assert isinstance(row_slice, pa.Table)
    assert row_slice.num_rows == 1
    assert row_slice.num_columns == 2
    assert row_slice.column_names == ["a", "b"]

    # Multi-row slice returns a sub-Table with matching row subset
    sub = table[0:2]
    assert isinstance(sub, pa.Table)
    assert sub.num_rows == 2
    assert sub["a"].to_pylist() == [10, 20]
    assert sub["b"].to_pylist() == ["w", "x"]


def test_column_selection_and_list_indexing():
    table = pa.table({
        "a": [1, 2, 3],
        "b": [4, 5, 6],
    })

    # Passing a list of column names to __getitem__ raises TypeError instead of selecting columns
    try:
        _ = table[["a"]]
        assert False, "Expected TypeError for table[['a']]"
    except TypeError as exc:
        assert "Index must either be string or integer" in str(exc)

    # Subsetting columns requires .select()
    selected = table.select(["a"])
    assert isinstance(selected, pa.Table)
    assert selected.column_names == ["a"]
    assert selected.num_rows == 3


def test_table_vs_recordbatch_column_types():
    data = {"a": [1, 2, 3], "b": ["x", "y", "z"]}
    table = pa.table(data)
    batch = pa.record_batch([data["a"], data["b"]], names=["a", "b"])

    # Table column access always yields ChunkedArray, never Array
    assert isinstance(table[0], pa.ChunkedArray)
    assert isinstance(table.column(0), pa.ChunkedArray)
    assert not isinstance(table[0], pa.Array)

    # RecordBatch column access always yields Array, never ChunkedArray
    assert isinstance(batch[0], pa.Array)
    assert isinstance(batch.column(0), pa.Array)
    assert not isinstance(batch[0], pa.ChunkedArray)


def test_to_pydict_vs_to_pylist_layout():
    table = pa.table({
        "a": [10, 20],
        "b": ["x", "y"],
    })

    # to_pydict() produces a column-oriented dictionary of lists
    pydict = table.to_pydict()
    assert isinstance(pydict, dict)
    assert set(pydict.keys()) == {"a", "b"}
    assert pydict["a"] == [10, 20]

    # to_pylist() produces a row-oriented list of dictionaries
    pylist = table.to_pylist()
    assert isinstance(pylist, list)
    assert len(pylist) == 2
    assert pylist == [{"a": 10, "b": "x"}, {"a": 20, "b": "y"}]


def test_compute_filtering_three_valued_logic_null_drop():
    table = pa.table({
        "a": [10, 20, 30, None, 50],
        "b": ["v", "w", "x", "y", "z"],
    })

    # Three-valued logic: equality against a scalar produces null for null entries
    is_twenty = pc.equal(table["a"], 20)
    is_not_twenty = pc.invert(is_twenty)

    # Default filter drops null entries in the predicate mask
    matched = table.filter(is_twenty)
    not_matched = table.filter(is_not_twenty)

    assert len(matched) == 1
    assert matched["a"].to_pylist() == [20]

    assert len(not_matched) == 3
    assert not_matched["a"].to_pylist() == [10, 30, 50]

    # The null row is omitted from both positive and inverted filter outputs
    assert len(matched) + len(not_matched) == 4
    assert len(matched) + len(not_matched) != len(table)


if __name__ == "__main__":
    test_table_len_vs_iteration_and_indexing()
    test_slice_vs_integer_index_dimension_flip()
    test_column_selection_and_list_indexing()
    test_table_vs_recordbatch_column_types()
    test_to_pydict_vs_to_pylist_layout()
    test_compute_filtering_three_valued_logic_null_drop()
    print("All pyarrow contract assertions passed.")

Seeder d'origine

csx-seed