Arrow IPC#
IPC options#
-
struct arrow::ipc::IpcReadOptions#
Options for reading Arrow IPC messages.
Public Members
-
int max_recursion_depth = kMaxNestingDepth#
The maximum permitted schema nesting depth.
-
MemoryPool *memory_pool = default_memory_pool()#
The memory pool to use for allocations made during IPC reading.
While Arrow IPC is predominantly zero-copy, it may have to allocate memory in some cases (for example if compression is enabled).
-
std::vector<int> included_fields#
Top-level schema fields to include when deserializing RecordBatch.
If empty (the default), return all deserialized fields. If non-empty, the values are the indices of fields in the top-level schema.
-
bool use_threads = true#
Use global CPU thread pool to parallelize any computational tasks like decompression.
-
bool ensure_native_endian = true#
Whether to convert incoming data to platform-native endianness.
If the endianness of the received schema is not equal to platform-native endianness, then all buffers with endian-sensitive data will be byte-swapped. This includes the value buffers of numeric types, temporal types, decimal types, as well as the offset buffers of variable-sized binary and list-like types.
Endianness conversion is achieved by the RecordBatchFileReader, RecordBatchStreamReader and StreamDecoder classes.
-
io::CacheOptions pre_buffer_cache_options = io::CacheOptions::LazyDefaults()#
Options to control caching behavior when pre-buffering is requested.
The lazy property will always be reset to true to deliver the expected behavior
-
int max_recursion_depth = kMaxNestingDepth#
-
struct arrow::ipc::IpcWriteOptions#
Options for writing Arrow IPC messages.
Public Members
-
bool allow_64bit = false#
If true, allow field lengths that don’t fit in a signed 32-bit int.
Some implementations may not be able to parse streams created with this option.
-
int max_recursion_depth = kMaxNestingDepth#
The maximum permitted schema nesting depth.
-
int32_t alignment = 8#
Write padding after memory buffers up to this multiple of bytes.
-
bool write_legacy_ipc_format = false#
Write the pre-0.15.0 IPC message format.
This legacy format consists of a 4-byte prefix instead of 8-byte.
-
MemoryPool *memory_pool = default_memory_pool()#
The memory pool to use for allocations made during IPC writing.
While Arrow IPC is predominantly zero-copy, it may have to allocate memory in some cases (for example if compression is enabled).
-
std::shared_ptr<util::Codec> codec#
Compression codec to use for record batch body buffers.
May only be UNCOMPRESSED, LZ4_FRAME and ZSTD.
-
bool use_threads = true#
Use global CPU thread pool to parallelize any computational tasks like compression.
-
bool emit_dictionary_deltas = false#
Whether to emit dictionary deltas.
If false, a changed dictionary for a given field will emit a full dictionary replacement. If true, a changed dictionary will be compared against the previous version. If possible, a dictionary delta will be emitted, otherwise a full dictionary replacement.
Default is false to maximize stream compatibility.
Also, note that if a changed dictionary is a nested dictionary, then a delta is never emitted, for compatibility with the read path.
-
bool unify_dictionaries = false#
Whether to unify dictionaries for the IPC file format.
The IPC file format doesn’t support dictionary replacements. Therefore, chunks of a column with a dictionary type must have the same dictionary in each record batch (or an extended dictionary + delta).
If this option is true, RecordBatchWriter::WriteTable will attempt to unify dictionaries across each table column. If this option is false, incompatible dictionaries across a table column will simply raise an error.
Note that enabling this option has a runtime cost. Also, not all types currently support dictionary unification.
This option is ignored for IPC streams, which support dictionary replacement and deltas.
-
MetadataVersion metadata_version = MetadataVersion::V5#
Format version to use for IPC messages and their metadata.
Presently using V5 version (readable by 1.0.0 and later). V4 is also available (readable by 0.8.0 and later).
-
bool allow_64bit = false#
Reading IPC streams and files#
Blocking API#
Use either of these two classes, depending on which IPC format you want to read. The file format requires a random-access file, while the stream format only requires a sequential input stream.
-
class arrow::ipc::RecordBatchStreamReader : public arrow::RecordBatchReader#
Synchronous batch stream reader that reads from io::InputStream.
This class reads the schema (plus any dictionaries) as the first messages in the stream, followed by record batches. For more granular zero-copy reads see the ReadRecordBatch functions
Public Static Functions
-
static Result<std::shared_ptr<RecordBatchStreamReader>> Open(std::unique_ptr<MessageReader> message_reader, const IpcReadOptions &options = IpcReadOptions::Defaults())#
Create batch reader from generic MessageReader.
This will take ownership of the given MessageReader.
- Parameters
message_reader – [in] a MessageReader implementation
options – [in] any IPC reading options (optional)
- Returns
the created batch reader
-
static Result<std::shared_ptr<RecordBatchStreamReader>> Open(io::InputStream *stream, const IpcReadOptions &options = IpcReadOptions::Defaults())#
Record batch stream reader from InputStream.
- Parameters
stream – [in] an input stream instance. Must stay alive throughout lifetime of stream reader
options – [in] any IPC reading options (optional)
- Returns
the created batch reader
Open stream and retain ownership of stream object.
- Parameters
stream – [in] the input stream
options – [in] any IPC reading options (optional)
- Returns
the created batch reader
-
static Result<std::shared_ptr<RecordBatchStreamReader>> Open(std::unique_ptr<MessageReader> message_reader, const IpcReadOptions &options = IpcReadOptions::Defaults())#
-
class arrow::ipc::RecordBatchFileReader : public std::enable_shared_from_this<RecordBatchFileReader>#
Reads the record batch file format.
Public Functions
-
virtual int num_record_batches() const = 0#
Returns the number of record batches in the file.
-
virtual MetadataVersion version() const = 0#
Return the metadata version from the file metadata.
-
virtual std::shared_ptr<const KeyValueMetadata> metadata() const = 0#
Return the contents of the custom_metadata field from the file’s Footer.
-
virtual Result<std::shared_ptr<RecordBatch>> ReadRecordBatch(int i) = 0#
Read a particular record batch from the file.
Does not copy memory if the input source supports zero-copy.
- Parameters
i – [in] the index of the record batch to return
- Returns
the read batch
-
virtual Result<RecordBatchWithMetadata> ReadRecordBatchWithCustomMetadata(int i) = 0#
Read a particular record batch along with its custom metadada from the file.
Does not copy memory if the input source supports zero-copy.
- Parameters
i – [in] the index of the record batch to return
- Returns
a struct containing the read batch and its custom metadata
-
virtual Status PreBufferMetadata(const std::vector<int> &indices) = 0#
Begin loading metadata for the desired batches into memory.
This method will also begin loading all dictionaries messages into memory.
For a regular file this will immediately begin disk I/O in the background on a thread on the IOContext’s thread pool. If the file is memory mapped this will ensure the memory needed for the metadata is paged from disk into memory
- Parameters
indices – Indices of the batches to prefetch If empty then all batches will be prefetched.
-
virtual Result<AsyncGenerator<std::shared_ptr<RecordBatch>>> GetRecordBatchGenerator(const bool coalesce = false, const io::IOContext &io_context = io::default_io_context(), const io::CacheOptions cache_options = io::CacheOptions::LazyDefaults(), arrow::internal::Executor *executor = NULLPTR) = 0#
Get a reentrant generator of record batches.
- Parameters
coalesce – [in] If true, enable I/O coalescing.
io_context – [in] The IOContext to use (controls which thread pool is used for I/O).
cache_options – [in] Options for coalescing (if enabled).
executor – [in] Optionally, an executor to use for decoding record batches. This is generally only a benefit for very wide and/or compressed batches.
Public Static Functions
-
static Result<std::shared_ptr<RecordBatchFileReader>> Open(io::RandomAccessFile *file, const IpcReadOptions &options = IpcReadOptions::Defaults())#
Open a RecordBatchFileReader.
Open a file-like object that is assumed to be self-contained; i.e., the end of the file interface is the end of the Arrow file. Note that there can be any amount of data preceding the Arrow-formatted data, because we need only locate the end of the Arrow file stream to discover the metadata and then proceed to read the data into memory.
-
static Result<std::shared_ptr<RecordBatchFileReader>> Open(io::RandomAccessFile *file, int64_t footer_offset, const IpcReadOptions &options = IpcReadOptions::Defaults())#
Open a RecordBatchFileReader If the file is embedded within some larger file or memory region, you can pass the absolute memory offset to the end of the file (which contains the metadata footer).
The metadata must have been written with memory offsets relative to the start of the containing file
- Parameters
file – [in] the data source
footer_offset – [in] the position of the end of the Arrow file
options – [in] options for IPC reading
- Returns
the returned reader
Version of Open that retains ownership of file.
- Parameters
file – [in] the data source
options – [in] options for IPC reading
- Returns
the returned reader
Version of Open that retains ownership of file.
- Parameters
file – [in] the data source
footer_offset – [in] the position of the end of the Arrow file
options – [in] options for IPC reading
- Returns
the returned reader
Open a file asynchronously (owns the file).
-
static Future<std::shared_ptr<RecordBatchFileReader>> OpenAsync(io::RandomAccessFile *file, const IpcReadOptions &options = IpcReadOptions::Defaults())#
Open a file asynchronously (borrows the file).
Open a file asynchronously (owns the file).
-
static Future<std::shared_ptr<RecordBatchFileReader>> OpenAsync(io::RandomAccessFile *file, int64_t footer_offset, const IpcReadOptions &options = IpcReadOptions::Defaults())#
Open a file asynchronously (borrows the file).
-
virtual int num_record_batches() const = 0#
Event-driven API#
To read an IPC stream in event-driven fashion, you must implement a
Listener
subclass that you will pass to
StreamDecoder
.
-
class arrow::ipc::Listener#
A general listener class to receive events.
You must implement callback methods for interested events.
This API is EXPERIMENTAL.
- Since
0.17.0
Subclassed by arrow::ipc::CollectListener
Public Functions
-
virtual Status OnEOS()#
Called when end-of-stream is received.
The default implementation just returns arrow::Status::OK().
See also
- Returns
Called when a record batch is decoded.
The default implementation just returns arrow::Status::NotImplemented().
See also
- Parameters
record_batch – [in] a record batch decoded
- Returns
Called when a schema is decoded.
The default implementation just returns arrow::Status::OK().
See also
- Parameters
schema – [in] a schema decoded
- Returns
-
class arrow::ipc::StreamDecoder#
Push style stream decoder that receives data from user.
This class decodes the Apache Arrow IPC streaming format data.
This API is EXPERIMENTAL.
- Since
0.17.0
Public Functions
Construct a stream decoder.
- Parameters
listener – [in] a Listener that must implement Listener::OnRecordBatchDecoded() to receive decoded record batches
options – [in] any IPC reading options (optional)
-
Status Consume(const uint8_t *data, int64_t size)#
Feed data to the decoder as a raw data.
If the decoder can read one or more record batches by the data, the decoder calls listener->OnRecordBatchDecoded() with a decoded record batch multiple times.
- Parameters
data – [in] a raw data to be processed. This data isn’t copied. The passed memory must be kept alive through record batch processing.
size – [in] raw data size.
- Returns
Feed data to the decoder as a Buffer.
If the decoder can read one or more record batches by the Buffer, the decoder calls listener->RecordBatchReceived() with a decoded record batch multiple times.
-
std::shared_ptr<Schema> schema() const#
- Returns
the shared schema of the record batches in the stream
-
int64_t next_required_size() const#
Return the number of bytes needed to advance the state of the decoder.
This method is provided for users who want to optimize performance. Normal users don’t need to use this method.
Here is an example usage for normal users:
decoder.Consume(buffer1); decoder.Consume(buffer2); decoder.Consume(buffer3);
Decoder has internal buffer. If consumed data isn’t enough to advance the state of the decoder, consumed data is buffered to the internal buffer. It causes performance overhead.
If you pass next_required_size() size data to each Consume() call, the decoder doesn’t use its internal buffer. It improves performance.
Here is an example usage to avoid using internal buffer:
buffer1 = get_data(decoder.next_required_size()); decoder.Consume(buffer1); buffer2 = get_data(decoder.next_required_size()); decoder.Consume(buffer2);
Users can use this method to avoid creating small chunks. Record batch data must be contiguous data. If users pass small chunks to the decoder, the decoder needs concatenate small chunks internally. It causes performance overhead.
Here is an example usage to reduce small chunks:
buffer = AllocateResizableBuffer(); while ((small_chunk = get_data(&small_chunk_size))) { auto current_buffer_size = buffer->size(); buffer->Resize(current_buffer_size + small_chunk_size); memcpy(buffer->mutable_data() + current_buffer_size, small_chunk, small_chunk_size); if (buffer->size() < decoder.next_required_size()) { continue; } std::shared_ptr<arrow::Buffer> chunk(buffer.release()); decoder.Consume(chunk); buffer = AllocateResizableBuffer(); } if (buffer->size() > 0) { std::shared_ptr<arrow::Buffer> chunk(buffer.release()); decoder.Consume(chunk); }
- Returns
the number of bytes needed to advance the state of the decoder
Statistics#
-
struct arrow::ipc::ReadStats#
Public Members
-
int64_t num_messages = 0#
Number of IPC messages read.
-
int64_t num_record_batches = 0#
Number of record batches read.
-
int64_t num_dictionary_batches = 0#
Number of dictionary batches read.
Note: num_dictionary_batches >= num_dictionary_deltas + num_replaced_dictionaries
-
int64_t num_dictionary_deltas = 0#
Number of dictionary deltas read.
-
int64_t num_replaced_dictionaries = 0#
Number of replaced dictionaries (i.e.
where a dictionary batch replaces an existing dictionary with an unrelated new dictionary).
-
int64_t num_messages = 0#
Writing IPC streams and files#
Blocking API#
The IPC stream format is only optionally terminated, whereas the IPC file format
must include a terminating footer. Thus a writer of the IPC file format must be
explicitly finalized with Close()
or the resulting
file will be corrupt.
Create a new IPC stream writer from stream sink and schema.
User is responsible for closing the actual OutputStream.
- Parameters
sink – [in] output stream to write to
schema – [in] the schema of the record batches to be written
options – [in] options for serialization
- Returns
Result<std::shared_ptr<RecordBatchWriter>>
Create a new IPC stream writer from stream sink and schema.
User is responsible for closing the actual OutputStream.
- Parameters
sink – [in] output stream to write to
schema – [in] the schema of the record batches to be written
options – [in] options for serialization
- Returns
Result<std::shared_ptr<RecordBatchWriter>>
Create a new IPC file writer from stream sink and schema.
- Parameters
sink – [in] output stream to write to
schema – [in] the schema of the record batches to be written
options – [in] options for serialization, optional
metadata – [in] custom metadata for File Footer, optional
- Returns
Result<std::shared_ptr<RecordBatchWriter>>
Create a new IPC file writer from stream sink and schema.
- Parameters
sink – [in] output stream to write to
schema – [in] the schema of the record batches to be written
options – [in] options for serialization, optional
metadata – [in] custom metadata for File Footer, optional
- Returns
Result<std::shared_ptr<RecordBatchWriter>>
-
class arrow::ipc::RecordBatchWriter#
Abstract interface for writing a stream of record batches.
Subclassed by arrow::flight::MetadataRecordBatchWriter
Public Functions
-
virtual Status WriteRecordBatch(const RecordBatch &batch) = 0#
Write a record batch to the stream.
- Parameters
batch – [in] the record batch to write to the stream
- Returns
Write a record batch with custom metadata to the stream.
- Parameters
batch – [in] the record batch to write to the stream
custom_metadata – [in] the record batch’s custom metadata to write to the stream
- Returns
-
Status WriteTable(const Table &table)#
Write possibly-chunked table by creating sequence of record batches.
- Parameters
table – [in] table to write
- Returns
-
virtual Status WriteTable(const Table &table, int64_t max_chunksize)#
Write Table with a particular chunksize.
- Parameters
table – [in] table to write
max_chunksize – [in] maximum length of table chunks. To indicate that no maximum should be enforced, pass -1.
- Returns
-
virtual WriteStats stats() const = 0#
Return current write statistics.
-
virtual Status WriteRecordBatch(const RecordBatch &batch) = 0#
Statistics#
-
struct arrow::ipc::WriteStats#
Public Members
-
int64_t num_messages = 0#
Number of IPC messages written.
-
int64_t num_record_batches = 0#
Number of record batches written.
-
int64_t num_dictionary_batches = 0#
Number of dictionary batches written.
Note: num_dictionary_batches >= num_dictionary_deltas + num_replaced_dictionaries
-
int64_t num_dictionary_deltas = 0#
Number of dictionary deltas written.
-
int64_t num_replaced_dictionaries = 0#
Number of replaced dictionaries (i.e.
where a dictionary batch replaces an existing dictionary with an unrelated new dictionary).
-
int64_t total_raw_body_size = 0#
Total size in bytes of record batches emitted.
The “raw” size counts the original buffer sizes, while the “serialized” size includes padding and (optionally) compression.
-
int64_t num_messages = 0#