ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Arrow.h File Reference

The sequences of ADS as the columns of Apache Arrow, by its C Data Interface (https://arrow.apache.org/docs/format/CDataInterface.html): two structures of C, ArrowSchema (the type of a column) and ArrowArray (its buffers), each with a callback release that frees what its producer keeps alive, through which pyarrow, Polars, DuckDB, pandas and nanoarrow hand columns to one another within a process with no copy. More...

#include "ADS/Contract.h"
#include "ADS/Portable.h"
#include "ADS/parallel/Executor.h"
#include "ADS/succinct/BitVector.h"
#include "ADS/succinct/PackedArray.h"
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <memory>
#include <ranges>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

struct  ArrowSchema
 the type of a column of Arrow More...
struct  ArrowArray
 the buffers of a column of Arrow More...
class  ADS::ArrowColumn< T >
 a column of Arrow of numbers of type \( T \), taken without copy: its values stay in the memory of its producer, which this object releases when destroyed More...

Namespaces

namespace  ADS

Concepts

concept  ADS::ArrowNumber
 the numbers that are the values of a column of Arrow: the integers of 8, 16, 32 and 64 bits, float and double
concept  ADS::ArrowExportable
 what exportArrow() exports: a BitVector, a PackedArray or a sized sequence of numbers

Macros

#define ARROW_C_DATA_INTERFACE

Functions

template<ArrowNumber T>
constexpr const char * ADS::arrowFormat (void)
 the format of Arrow of the numbers of type \( T \)
template<class X>
requires ArrowExportable< std::remove_const_t< X > >
void ADS::exportArrow (std::shared_ptr< X > x, ArrowArray *a, ArrowSchema *s, std::string_view name="")
 the structure \( *x \) as a column of Arrow of no nulls, named \( \mathit{name} \), in \( *a \) and \( *s \), which its consumer releases; the values are those of \( *x \), which the array keeps alive (copied where the layouts differ, see above)
template<class X>
requires ArrowExportable< X >
void ADS::exportArrow (X x, ArrowArray *a, ArrowSchema *s, std::string_view name="")
 the structure \( x \) as a column of Arrow of no nulls, named \( \mathit{name} \), in \( *a \) and \( *s \): \( x \) is moved into the array, which keeps it alive until its consumer releases it
BitVector ADS::toBitVector (const ArrowArray &a, const ArrowSchema &s)
 the column of booleans \( a \) of type \( s \) copied into a BitVector; \( a \) and \( s \) are only read
template<unsigned B, bool S = false>
PackedArray< B, S > ADS::toPackedArray (const ArrowArray &a, const ArrowSchema &s)
 the column \( a \) of type \( s \) copied into a PackedArray< B , S >: a column of booleans if \( B = 1 \), or of integers of any width, each value clamped to \( 2^B - 1 \) if \( S \); \( a \) and \( s \) are only read
template<ArrowNumber T>
std::vector< T > ADS::toVector (const ArrowArray &a, const ArrowSchema &s)
 the column \( a \) of numbers of type \( T \), of type \( s \), copied into a std::vector; \( a \) and \( s \) are only read

Detailed description

The sequences of ADS as the columns of Apache Arrow, by its C Data Interface (https://arrow.apache.org/docs/format/CDataInterface.html): two structures of C, ArrowSchema (the type of a column) and ArrowArray (its buffers), each with a callback release that frees what its producer keeps alive, through which pyarrow, Polars, DuckDB, pandas and nanoarrow hand columns to one another within a process with no copy.

The specification froze them as an ABI and asks a producer to copy their definition rather than to depend on Arrow, which this header does under the guard ARROW_C_DATA_INTERFACE that the specification prescribes, so that a unit that also includes Arrow or nanoarrow sees them once; hence this header needs no library and no option.

exportArrow( x , &array , &schema ) fills the two structures with a column of no nulls, whose buffer of values is the memory of \( x \) itself, kept alive by a std::shared_ptr in the private data of the array until its consumer calls release: a BitVector as a column of booleans (format b), since Arrow numbers the bit \( i \) as the bit \( i \bmod 8 \) of the byte \( \lfloor i / 8 \rfloor \), which is the layout of the words of BitVector on a little-endian processor; a PackedArray< B > of fields of 8, 16 or 32 bits as a column of unsigned integers of that width (C, S, I), and of 1 bit as one of booleans; a contiguous sequence of numbers (std::vector, std::array or any contiguous range of integers of 8 to 64 bits, float or double) as a column of their type (c, C, s, S, i, I, l, L, f, g); a view, such as a std::span, is kept alive as a view, so that the memory it shows must outlive the array. The values are copied only where the layouts differ: a PackedArray of 2 or 4 bits, decoded into bytes, a sequence that is not contiguous (a TieredVector, a std::deque), and, on a big-endian processor, the words of a BitVector and of a PackedArray, whose bytes come in the reverse order there (the C Data Interface keeps the numbers in the order of the processor, and the bits of a boolean column always from the least significant one of a byte).

The converse takes a column of Arrow, which is in the memory of its producer: ArrowColumn< T > takes the array without copy (as the specification allows, by moving the structure and marking the source released) and shows its values as a std::span< const T >, the input of a build of ADS (a wavelet matrix, an Elias-Fano sequence), releasing it when destroyed; toBitVector(), toPackedArray< B >() and toVector< T >() copy a column into a structure of ADS, which owns its words. A column with nulls, of another type, or with children is refused with std::invalid_argument; the schema is only read, and its release is left to its owner.

This header is of the set interop: no entry point of ADS includes it.

See also
Apache Arrow, The Arrow C data interface, https://arrow.apache.org/docs/format/CDataInterface.html
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa

Macro definition documentation

◆ ARROW_C_DATA_INTERFACE

#define ARROW_C_DATA_INTERFACE