Dataset¶
Vortex files implement the Arrow Dataset interface permitting efficient use of a Vortex file within query engines like DuckDB and Polars. In particular, Vortex will read data proportional to the number of rows passing a filter condition and the number of columns in a selection. For most Vortex encodings, this property holds true even when the filter condition specifies a single row.
Use open_dataset() to read many Vortex files as one dataset: a directory, a glob
pattern, an object store prefix or a list of files. Its concurrency sets how many splits a read
fetches and decodes ahead, across all the files, including reads that a query engine drives.
import duckdb
import vortex as vx
failures = vx.open_dataset("data_vortex/**/*.vortex", concurrency=32)
duckdb.sql("SELECT date, count(*) FROM failures WHERE failure = 1 GROUP BY date").fetchall()
Open many Vortex files as one |
|
Read Vortex files with row filter and column selection pushdown. |
|
A PyArrow Dataset Scanner that reads from a Vortex Array. |
|
Fragment of data from a |
- vortex.open_dataset(source: str | PathLike[str] | Sequence[str | PathLike[str]], *, store: AzureStore | CosStore | GCSStore | HfStore | HTTPStore | LocalStore | MemoryStore | S3Store | None = None, concurrency: int | None = None) VortexDataset¶
Open many Vortex files as one
VortexDataset.The dataset reads the rows of every file in order, with column selection and filters pushed into the scan of each file. Reads keep up to
concurrencysplits in flight across file boundaries, so a dataset of many small files is read as concurrently as one large file. Opening the dataset reads only the footer of the first file; the remaining files open lazily when scanned.- Parameters:
source (
str|os.PathLike| list of them) – What to read: a Vortex file, a directory (every.vortexfile below it, at any depth), a glob pattern such as"data/**/*.vortex", or a list of these, read in the order given. Each can be a local path or a URL such ass3://bucket/data/; an object store URL ending in/names a prefix and reads every.vortexfile below it. The files of a directory, prefix or pattern are read in sorted order. Every file must have the same dtype.store – An object store created from the vortex.store package. When given, every source is a path within it. By default the store is inferred from each source’s URL.
concurrency (
int| None) – The maximum number of splits each read fetches and decodes ahead of the consumer, unless the read sets its own. Query engines such as DuckDB and Polars read the dataset without setting one, so this is how to tune their reads. By default, four splits per worker thread.
Examples
Write a file per day, then count the failures across all of them with DuckDB:
>>> import duckdb >>> import pyarrow as pa >>> import vortex as vx >>> for day in range(3): ... vx.io.write(pa.table({"day": [day] * 4, "failure": [0, 1, 1, 0]}), f"day-{day}.vortex") >>> failures = vx.open_dataset("day-*.vortex") >>> failures.file_count 3 >>> duckdb.sql( ... "SELECT day, count(*) AS n FROM failures WHERE failure = 1 GROUP BY day ORDER BY day" ... ).fetchall() [(0, 2), (1, 2), (2, 2)]
- final class vortex.dataset.VortexDataset(dataset: VortexDataset, *, filters: list[Expr] | None = None)¶
Read Vortex files with row filter and column selection pushdown.
This class implements the
pyarrow.dataset.Datasetinterface which enables its use with Polars, DuckDB, Pandas and others.- property concurrency: int | None¶
The maximum number of splits a read fetches and decodes ahead, unless it sets its own.
Nonemeans four splits per worker thread.
- count_rows(filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) int¶
Count the number of rows in this dataset.
- filter(expression: Expression | Expr) VortexDataset¶
A new Dataset with a filter condition applied.
Successively calling this method conjuncts all the filter expressions together.
- static from_path(path: str) VortexDataset¶
Open the Vortex files that
pathnames. Seeopen_dataset().
- static from_url(url: str) VortexDataset¶
Open the Vortex files that
urlnames. Seeopen_dataset().
- get_fragments(filter: Expression | Expr | None = None) Iterator[VortexFragment]¶
A fragment for each file in the Dataset.
- head(num_rows: int, columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) Table¶
Load the first num_rows of the dataset.
- Parameters:
num_rows (int) – The number of rows to load.
columns (list of str) – The columns to keep, identified by name.
filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
concurrency.
- Returns:
table
- Return type:
- join(right_dataset: Dataset, keys: str | list[str], right_keys: str | list[str] | None = None, join_type: str = 'left outer', left_suffix: str | None = None, right_suffix: str | None = None, coalesce_keys: bool = True, use_threads: bool = True) InMemoryDataset¶
Not implemented.
- join_asof(right_dataset: Dataset, on: str, by: str | list[str], tolerance: int, right_on: str | list[str] | None = None, right_by: str | list[str] | None = None) InMemoryDataset¶
Not implemented.
- scanner(columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) Scanner¶
Construct a
pyarrow.dataset.Scanner.- Parameters:
columns (list of str) – The columns to keep, identified by name.
filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
concurrency.
- Returns:
table
- Return type:
- take(indices: pyarrow.Array[pyarrow.Int8Scalar | pyarrow.Int16Scalar | pyarrow.Int32Scalar | pyarrow.Int64Scalar | pyarrow.UInt8Scalar | pyarrow.UInt16Scalar | pyarrow.UInt32Scalar | pyarrow.UInt64Scalar], columns: list[str] | None = None, filter: pyarrow.dataset.Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: pyarrow.dataset.FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: pyarrow.MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) pyarrow.Table¶
Load a subset of rows identified by their absolute indices.
- Parameters:
indices (
pyarrow.Array) – A numeric array of absolute indices into self indicating which rows to keep. Rows are returned in the order of indices, which may repeat or be unordered. If filter is given or this dataset was filtered, indices must be strictly increasing.columns (list of str) – The columns to keep, identified by name.
filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.cache_metadata (bool) – Not implemented.
memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
concurrency.
- Returns:
table
- Return type:
- to_batches(columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) Iterator[RecordBatch]¶
Construct an iterator of
pyarrow.RecordBatch.- Parameters:
columns (list of str) – The columns to keep, identified by name.
filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.cache_metadata (bool) – Not implemented.
memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
concurrency.
- Returns:
table
- Return type:
- to_polars(*, concurrency: int | None = None) LazyFrame¶
Read the dataset as a
LazyFrame, pushing column selection and filters into the scan of every file.- Parameters:
concurrency (int | None) – The maximum number of splits fetched and decoded ahead of Polars, across all the files of the dataset. Defaults to the dataset’s
concurrency.
Examples
>>> import pyarrow as pa >>> import polars as pl >>> import vortex as vx >>> for day in range(3): ... vx.io.write(pa.table({"day": [day] * 4, "failure": [0, 1, 1, 0]}), f"day-{day}.vortex") >>> ( ... vx.open_dataset("day-*.vortex", concurrency=32) ... .to_polars() ... .filter(pl.col("failure") == 1) ... .group_by("day") ... .agg(pl.len().alias("failures")) ... .sort("day") ... .collect() ... .to_dicts() ... ) [{'day': 0, 'failures': 2}, {'day': 1, 'failures': 2}, {'day': 2, 'failures': 2}]
- to_record_batch_reader(columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) RecordBatchReader¶
Construct a
pyarrow.RecordBatchReader.- Parameters:
columns (list of str) – The columns to keep, identified by name.
filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
concurrency.
- Returns:
table
- Return type:
- to_table(columns: list[str] | dict[str, Expression] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None) Table¶
Construct an Arrow
pyarrow.Table.- Parameters:
columns (list of str, dict[str,
pyarrow.dataset.Expression] | None) – The columns to keep, identified by name.filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
concurrency.
- Returns:
table
- Return type:
- final class vortex.dataset.VortexFragment(dataset: VortexDataset, _row_range: tuple[int, int])¶
Fragment of data from a
VortexDataset.- count_rows(filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None) int¶
- head(num_rows: int, columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None) Table¶
- property partition_expression: Expression¶
An Expression which evaluates to true for all data viewed by this Fragment.
- property physical_schema: Schema¶
Return the physical schema of this Fragment. This schema can be different from the dataset read schema.
- scanner(schema: Schema | None = None, columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None) Scanner¶
- take(indices: pyarrow.Array[pyarrow.Int8Scalar | pyarrow.Int16Scalar | pyarrow.Int32Scalar | pyarrow.Int64Scalar | pyarrow.UInt8Scalar | pyarrow.UInt16Scalar | pyarrow.UInt32Scalar | pyarrow.UInt64Scalar], columns: list[str] | None = None, filter: pyarrow.dataset.Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: pyarrow.dataset.FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: pyarrow.MemoryPool | None = None) pyarrow.Table¶
See
vortex.dataset.VortexDataset.takeWarning
The indices are indices into the file, not indices into this fragment of the file.
- to_batches(schema: Schema | None = None, columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool = True, memory_pool: MemoryPool | None = None) Iterator[RecordBatch]¶
- to_table(schema: Schema | None = None, columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None) Table¶
- final class vortex.dataset.VortexScanner(dataset: VortexDataset, columns: list[str] | None = None, filter: Expression | Expr | None = None, batch_size: int | None = None, batch_readahead: int | None = None, fragment_readahead: int | None = None, fragment_scan_options: FragmentScanOptions | None = None, use_threads: bool = True, cache_metadata: bool | None = None, memory_pool: MemoryPool | None = None, _row_range: tuple[int, int] | None = None, *, concurrency: int | None = None)¶
A PyArrow Dataset Scanner that reads from a Vortex Array.
- Parameters:
dataset (VortexDataset) – The dataset to scan.
columns (list of str) – The columns to keep, identified by name.
filter (
pyarrow.dataset.Expression) – Keep only rows for which this expression evaluates toTrue. Any rows for which this expression evaluates toNullis removed.batch_size (int) – The maximum number of rows per batch.
batch_readahead (int) – Not implemented.
fragment_readahead (int) – Not implemented.
fragment_scan_options (
pyarrow.dataset.FragmentScanOptions) – Not implemented.use_threads (bool) – If
True, leave the configured Vortex worker pool unchanged (respectingvortex.set_worker_threads()andVORTEX_MAX_THREADSrather than forcing the default pool size). IfFalse, set worker threads to0on the first open unthreaded reader and restore the saved count when the last one closes; while any such reader is open, other reads in the process run on their calling threads. Callingvortex.set_worker_threads()while an unthreaded reader is open takes effect immediately and is overwritten when the last unthreaded reader closes and restores the saved count.memory_pool (
pyarrow.MemoryPool| None) – Not implemented.concurrency (int | None) – The maximum number of splits fetched and decoded ahead of the consumer, across all the files of the dataset. Defaults to the dataset’s
VortexDataset.concurrency.
- Returns:
table
- Return type:
- head(num_rows: int) Table¶
Load the first num_rows of the dataset.
- Parameters:
num_rows (int) – The number of rows to read.
- Returns:
table
- Return type:
- property projected_schema: Schema¶
The materialized schema of the data, accounting for projections.
This is the schema of any data returned from the scanner.
- scan_batches() Iterator[TaggedRecordBatch]¶
Not implemented.
- to_batches() Iterator[RecordBatch]¶
Construct an iterator of
pyarrow.RecordBatch.- Returns:
table
- Return type:
- to_reader() RecordBatchReader¶
Construct a
pyarrow.RecordBatchReader.- Returns:
table
- Return type:
- to_table() Table¶
Construct an Arrow
pyarrow.Table.- Returns:
table
- Return type:
- vortex.dataset.from_url(url: str) VortexDataset¶
Open the Vortex files that
urlnames. Seeopen_dataset().