#[repr(C, align(64))]pub struct Table {
pub cols: Vec<FieldArray>,
pub n_rows: usize,
pub name: String,
pub metadata: BTreeMap<String, String>,
}Expand description
§Table
§Description
- Standard columnar table with named columns (
FieldArray), a fixed number of rows, and an optional logical table name. - All columns are required to be equal length and have consistent schema.
- Supports zero-copy slicing, efficient iteration, and bulk operations.
- Equivalent to the
RecordBatchin Apache Arrow.
§Structure
cols: A vector ofFieldArray, each representing a column with metadata and data.n_rows: The logical number of rows (guaranteed equal for all columns).name: Optional logical name or alias for this table instance.
§Usage
- Use
Tableas a general-purpose, in-memory columnar data container. - Good for analytics, and transformation pipelines.
- For batched/partitioned tables, see
SuperTableor windowed/chunked abstractions. - Cast into Polars dataframe via
.to_polars()or Apache Arrow via.to_apache_arrow() - FFI-compatible
§Notes
- Table instances are typically lightweight to clone and pass by value.
- For mutation, construct a new table or replace individual columns as needed.
- There is an alias
RecordBatchunder crate::aliases::RecordBatch
§Example
use minarrow::{fa_i32, fa_str32, Print, Table};
let col1 = fa_i32!("numbers", 1, 2, 3);
let col2 = fa_str32!("letters", "x", "y", "z");
let mut tbl = Table::new("Demo".into(), vec![col1, col2].into());
tbl.print();Fields§
§cols: Vec<FieldArray>FieldArrays representing named columns.
n_rows: usizeNumber of rows in the table.
name: StringTable name
metadata: BTreeMap<String, String>Schema-level metadata as key-value pairs. Captures metadata that Arrow producers like PyArrow embed in the top-level ArrowSchema.metadata, e.g. pandas categorical ordering.
Implementations§
Source§impl Table
impl Table
Sourcepub fn new(name: String, cols: Option<Vec<FieldArray>>) -> Self
pub fn new(name: String, cols: Option<Vec<FieldArray>>) -> Self
Constructs a new Table with a specified name and optional columns.
If cols is provided, the number of rows will be inferred from the first column.
Sourcepub fn new_with_metadata(
name: String,
cols: Option<Vec<FieldArray>>,
metadata: BTreeMap<String, String>,
) -> Self
pub fn new_with_metadata( name: String, cols: Option<Vec<FieldArray>>, metadata: BTreeMap<String, String>, ) -> Self
Constructs a new Table with schema-level metadata.
Use this when importing data from Arrow producers that embed metadata in the top-level schema, e.g. pandas categorical ordering via PyArrow.
Sourcepub fn metadata(&self) -> &BTreeMap<String, String>
pub fn metadata(&self) -> &BTreeMap<String, String>
Returns a reference to the schema-level metadata.
Sourcepub fn from_arena(
name: String,
schema: &[Arc<Field>],
arena: Arena,
regions: Vec<AAMaker>,
n_rows: usize,
) -> Self
pub fn from_arena( name: String, schema: &[Arc<Field>], arena: Arena, regions: Vec<AAMaker>, n_rows: usize, ) -> Self
Build a Table from an Arena and its collected array regions.
Freezes the arena into a SharedBuffer, then reconstructs each
column as a zero-copy view into the shared allocation. This is
the read-side complement to Arena::write_slices and friends.
Typical use: IPC or streaming ingestion where batch sizes are known from message headers, allowing a single arena allocation per batch.
Sourcepub fn add_col(&mut self, field_array: FieldArray)
pub fn add_col(&mut self, field_array: FieldArray)
Adds a column with a name.
Sourcepub fn rename_columns(
&mut self,
mapping: &[(&str, &str)],
) -> Result<(), MinarrowError>
pub fn rename_columns( &mut self, mapping: &[(&str, &str)], ) -> Result<(), MinarrowError>
Rename columns in place. Each pair is (old_name, new_name).
Returns an error if any old name is not found. This is metadata-only - array data is not touched.
Sourcepub fn col_name_index(&self, name: &str) -> Option<usize>
pub fn col_name_index(&self, name: &str) -> Option<usize>
Returns the index of a column by name.
Sourcepub fn col_numeric(&self, name: &str) -> Result<NumericArrayV, MinarrowError>
pub fn col_numeric(&self, name: &str) -> Result<NumericArrayV, MinarrowError>
Resolve a named column to a NumericArrayV.
Sourcepub fn col_text(&self, name: &str) -> Result<TextArrayV, MinarrowError>
pub fn col_text(&self, name: &str) -> Result<TextArrayV, MinarrowError>
Resolve a named column to a TextArrayV.
Sourcepub fn col_bitmask(&self, name: &str) -> Result<BitmaskV, MinarrowError>
pub fn col_bitmask(&self, name: &str) -> Result<BitmaskV, MinarrowError>
Resolve a named column to a BitmaskV.
Sourcepub fn delete_range(&mut self, start: usize, end: usize)
pub fn delete_range(&mut self, start: usize, end: usize)
Removes the rows in [start, end) from every column, shifting later
rows left.
Columns delete in place through Vec64::delete_range.
Shared columns and shared buffers are cloned first i.e. copy-on-write.
§Panics
Panics if start > end or end > n_rows.
Sourcepub fn remove_col(&mut self, name: &str) -> bool
pub fn remove_col(&mut self, name: &str) -> bool
Removes a column by name.
Sourcepub fn remove_col_at(&mut self, idx: usize) -> bool
pub fn remove_col_at(&mut self, idx: usize) -> bool
Removes a column by index.
Sourcepub fn cols(&self) -> &[FieldArray]
pub fn cols(&self) -> &[FieldArray]
Returns all columns as a slice.
Sourcepub fn cols_mut(&mut self) -> &mut [FieldArray]
pub fn cols_mut(&mut self) -> &mut [FieldArray]
Returns mutable reference to all columns.
pub fn iter(&self) -> Iter<'_, FieldArray>
pub fn iter_mut(&mut self) -> IterMut<'_, FieldArray>
pub fn set_name(&mut self, name: impl Into<String>)
pub fn len(&self) -> usize
Sourcepub fn slice_clone(&self, offset: usize, len: usize) -> Self
pub fn slice_clone(&self, offset: usize, len: usize) -> Self
Returns a new owned Table containing rows [offset, offset+len).
All columns are deeply copied, but only for the affected row(s).
Sourcepub fn slice(&self, offset: usize, len: usize) -> TableV
pub fn slice(&self, offset: usize, len: usize) -> TableV
Returns a zero-copy view over rows [offset, offset+len).
This view borrows from the parent table and does not copy data.
Sourcepub fn map_col<T, F>(&self, col_name: &str, func: F) -> Option<T>where
F: FnOnce(&FieldArray) -> T,
pub fn map_col<T, F>(&self, col_name: &str, func: F) -> Option<T>where
F: FnOnce(&FieldArray) -> T,
Maps a function over a single column by name, returning the result. Returns None if the column doesn’t exist.
Sourcepub fn map_cols_by_name<T, F>(&self, col_names: &[&str], func: F) -> Vec<T>where
F: FnMut(&FieldArray) -> T,
pub fn map_cols_by_name<T, F>(&self, col_names: &[&str], func: F) -> Vec<T>where
F: FnMut(&FieldArray) -> T,
Maps a function over multiple columns by name, returning a Vec of results. Warns if any requested columns are missing.
Sourcepub fn map_cols_by_index<T, F>(&self, indices: &[usize], func: F) -> Vec<T>where
F: FnMut(&FieldArray) -> T,
pub fn map_cols_by_index<T, F>(&self, indices: &[usize], func: F) -> Vec<T>where
F: FnMut(&FieldArray) -> T,
Maps a function over multiple columns by index, returning a Vec of results. Warns if any requested indices are out of bounds.
Sourcepub fn map_all_cols<T, F>(&self, func: F) -> Vec<T>where
F: FnMut(&FieldArray) -> T,
pub fn map_all_cols<T, F>(&self, func: F) -> Vec<T>where
F: FnMut(&FieldArray) -> T,
Maps a function over all columns, returning a Vec of results.
Sourcepub fn apply_cols<E>(
&self,
f: impl FnMut(&FieldArray) -> Result<FieldArray, E>,
) -> Result<Table, E>
pub fn apply_cols<E>( &self, f: impl FnMut(&FieldArray) -> Result<FieldArray, E>, ) -> Result<Table, E>
Apply a transformation to each column, producing a new table.
The closure receives each FieldArray and returns a transformed FieldArray.
To pass a column through unchanged, clone it. The closure can dispatch
on Array variant to handle numeric, text, temporal, and boolean columns
differently.
Sourcepub fn insert_rows(
&mut self,
index: usize,
other: &Self,
) -> Result<(), MinarrowError>
pub fn insert_rows( &mut self, index: usize, other: &Self, ) -> Result<(), MinarrowError>
Inserts rows from another table at the specified index.
This is an O(n) operation where n is the number of rows after the insertion point.
§Arguments
index- Position before which to insert (0 = prepend, n_rows = append)other- Table to insert
§Requirements
- Both tables must have the same number of columns
- Column names, types, and nullability must match in order
indexmust be <=self.n_rows()
§Errors
IndexErrorif index > n_rowsIncompatibleTypeErrorif column schemas don’t match
Sourcepub fn split(self, index: usize) -> Result<SuperTable, MinarrowError>
pub fn split(self, index: usize) -> Result<SuperTable, MinarrowError>
Splits the Table at the specified row index, consuming self and returning a SuperTable with two Table batches.
Splits the underlying buffers, allocating new storage for the second half.
Source§impl Table
impl Table
pub fn par_iter(&self) -> Iter<'_, FieldArray>
pub fn par_iter_mut(&mut self) -> IterMut<'_, FieldArray>
Sourcepub fn to_apache_arrow(&self) -> RecordBatch
pub fn to_apache_arrow(&self) -> RecordBatch
Export each column to arrow-rs ArrayRef and build a RecordBatch.
The Arrow schema is derived from the imported array dtypes while preserving the original field names and nullability flags.
Panics on FFI failure or empty table. For a fallible variant, see
Table::try_to_apache_arrow.
Sourcepub fn try_to_apache_arrow(&self) -> Result<RecordBatch, MinarrowError>
pub fn try_to_apache_arrow(&self) -> Result<RecordBatch, MinarrowError>
Fallible variant of Table::to_apache_arrow.
Sourcepub fn to_polars(&self) -> DataFrame
pub fn to_polars(&self) -> DataFrame
Casts the table to a Polars DataFrame.
Panics on FFI failure. For a fallible variant, see
Table::try_to_polars.
Sourcepub fn try_to_polars(&self) -> Result<DataFrame, MinarrowError>
pub fn try_to_polars(&self) -> Result<DataFrame, MinarrowError>
Fallible variant of Table::to_polars.
Sourcepub fn from_apache_arrow(rb: &RecordBatch) -> Table
pub fn from_apache_arrow(rb: &RecordBatch) -> Table
Build a Table from an arrow-rs RecordBatch. Column names, dtypes,
nullability, and any custom field metadata are recovered from the
RecordBatch schema. The Table name will be empty; assign one via
Table::new if needed.
Panics on FFI failure. For a fallible variant, see
Table::try_from_apache_arrow.
Sourcepub fn try_from_apache_arrow(rb: &RecordBatch) -> Result<Table, MinarrowError>
pub fn try_from_apache_arrow(rb: &RecordBatch) -> Result<Table, MinarrowError>
Fallible variant of Table::from_apache_arrow.
Sourcepub fn from_polars(df: &DataFrame) -> Table
pub fn from_polars(df: &DataFrame) -> Table
Build a Table from a Polars DataFrame.
A polars DataFrame has columns that are inherently multi-chunked;
the canonical mapping is DataFrame <-> crate::SuperTable. This
helper routes through crate::SuperTable::from_polars and then
consolidates the batches into a single contiguous Table with
64-byte aligned column buffers.
Column names, dtypes, nullability, and any custom Series metadata are recovered.
§Performance note
Two separate costs to be aware of:
-
Alignment copy: Polars data is typically 8-byte aligned (per the Arrow spec default), while Minarrow uses 64-byte aligned
Vec64<T>buffers for SIMD. Most of the time this results in a memory copy to realign on import, unless the source data happens to be pre-aligned to 64 bytes. The FFI hand-off itself is pointer-level zero-copy; the realignment is done byBuffer::from_sharedwhen the source isn’t 64-byte aligned. -
Consolidation copy: Multi-chunk columns are merged into a single contiguous buffer per column, which is a second O(n) allocation and copy pass. Single-chunk DataFrames (e.g. after
df.align_chunks_par()thendf.rechunk()on the caller side) skip this step. The consolidation itself is cheap on Linux when thevmap64feature is enabled.
In practice you should expect at least one full allocation + copy
when importing a polars DataFrame into a Table. If you would like
to preserve the original chunk boundaries and avoid the
consolidation step, use crate::SuperTable::from_polars
directly - though the alignment copy will still occur per chunk
that isn’t pre-aligned.
Panics on FFI failure. For a fallible variant, see
Table::try_from_polars.
Sourcepub fn try_from_polars(df: &DataFrame) -> Result<Table, MinarrowError>
pub fn try_from_polars(df: &DataFrame) -> Result<Table, MinarrowError>
Fallible variant of Table::from_polars.
Trait Implementations§
Source§impl ColumnSelection for Table
Available on crate features select and views only.
impl ColumnSelection for Table
select and views only.Source§type ColumnView = ArrayV
type ColumnView = ArrayV
Source§type ColumnOwned = FieldArray
type ColumnOwned = FieldArray
.get() e.g. FieldArray for Table, Arc