|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +from typing import final |
| 7 | + |
| 8 | +from ._lib import scan as _scan # pyright: ignore[reportMissingModuleSource] |
| 9 | +from ._lib.iter import ArrayIterator # pyright: ignore[reportMissingModuleSource] |
| 10 | +from ._lib.scalar import Scalar # pyright: ignore[reportMissingModuleSource] |
| 11 | + |
| 12 | + |
| 13 | +@final |
| 14 | +class RepeatedScan: |
| 15 | + """ |
| 16 | + A prepared scan that is optimized for repeated execution. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, scan: _scan.RepeatedScan): |
| 20 | + self._scan = scan |
| 21 | + |
| 22 | + def execute( |
| 23 | + self, |
| 24 | + *, |
| 25 | + row_range: tuple[int, int] | None = None, |
| 26 | + ) -> ArrayIterator: |
| 27 | + """Execute the scan returning a :class:`vortex.ArrayIterator`. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + row_range : tuple[int, int] | None |
| 32 | + Tuple is interpreted as [start, stop). |
| 33 | +
|
| 34 | + Examples |
| 35 | + -------- |
| 36 | +
|
| 37 | + Scan a file with a structured column and nulls at multiple levels and in multiple columns. |
| 38 | +
|
| 39 | + >>> import vortex as vx |
| 40 | + >>> import vortex.expr as ve |
| 41 | + >>> a = vx.array([ |
| 42 | + ... {'name': 'Joseph', 'age': 25}, |
| 43 | + ... {'name': None, 'age': 31}, |
| 44 | + ... {'name': 'Angela', 'age': None}, |
| 45 | + ... {'name': 'Mikhail', 'age': 57}, |
| 46 | + ... {'name': None, 'age': None}, |
| 47 | + ... ]) |
| 48 | + >>> vx.io.write(a, "a.vortex") |
| 49 | + >>> scan = vx.open("a.vortex").to_repeated_scan() |
| 50 | + >>> scan.execute(row_range=(1, 3)).read_all().to_arrow_array() |
| 51 | + <pyarrow.lib.StructArray object at ...> |
| 52 | + -- is_valid: all not null |
| 53 | + -- child 0 type: int64 |
| 54 | + [ |
| 55 | + 31, |
| 56 | + null |
| 57 | + ] |
| 58 | + -- child 1 type: string_view |
| 59 | + [ |
| 60 | + null, |
| 61 | + "Angela" |
| 62 | + ] |
| 63 | + """ |
| 64 | + if row_range is None: |
| 65 | + start, stop = None, None |
| 66 | + else: |
| 67 | + start, stop = row_range |
| 68 | + return self._scan.execute(start=start, stop=stop) |
| 69 | + |
| 70 | + def scalar_at(self, index: int) -> Scalar: |
| 71 | + """Fetch a scalar from the scan returning a :class:`vortex.Scalar`. |
| 72 | +
|
| 73 | + Parameters |
| 74 | + ---------- |
| 75 | + index : int |
| 76 | + The row index to fetch. Raises an :class:`IndexError` if out of bounds or |
| 77 | + if the given row index was not included in the scan. |
| 78 | +
|
| 79 | + Examples |
| 80 | + -------- |
| 81 | +
|
| 82 | + Scan a file with a structured column and nulls at multiple levels and in multiple columns. |
| 83 | +
|
| 84 | + >>> import vortex as vx |
| 85 | + >>> import vortex.expr as ve |
| 86 | + >>> a = vx.array([ |
| 87 | + ... {'name': 'Joseph', 'age': 25}, |
| 88 | + ... {'name': None, 'age': 31}, |
| 89 | + ... {'name': 'Angela', 'age': None}, |
| 90 | + ... {'name': 'Mikhail', 'age': 57}, |
| 91 | + ... {'name': None, 'age': None}, |
| 92 | + ... ]) |
| 93 | + >>> vx.io.write(a, "a.vortex") |
| 94 | + >>> scan = vx.open("a.vortex").to_repeated_scan() |
| 95 | + >>> scan.scalar_at(1) |
| 96 | + <vortex.StructScalar object at ...> |
| 97 | + """ |
| 98 | + return self._scan.scalar_at(index) |
0 commit comments