diff --git a/format/IPC.md b/format/IPC.md index d386e6048cf1..f0a67e292186 100644 --- a/format/IPC.md +++ b/format/IPC.md @@ -14,65 +14,106 @@ # Interprocess messaging / communication (IPC) -## File format +## Encapsulated message format + +Data components in the stream and file formats are represented as encapsulated +*messages* consisting of: -We define a self-contained "file format" containing an Arrow schema along with -one or more record batches defining a dataset. See [format/File.fbs][1] for the -precise details of the file metadata. +* A length prefix indicating the metadata size +* The message metadata as a [Flatbuffer][3] +* Padding bytes to an 8-byte boundary +* The message body -In general, the file looks like: +Schematically, we have: ``` - - + + + + +``` + +The `metadata_size` includes the size of the flatbuffer plus padding. The +`Message` flatbuffer includes a version number, the particular message (as a +flatbuffer union), and the size of the message body: + +``` +table Message { + version: org.apache.arrow.flatbuf.MetadataVersion; + header: MessageHeader; + bodyLength: long; +} +``` + +Currently, we support 4 types of messages: + +* Schema +* RecordBatch +* DictionaryBatch +* Tensor + +## Streaming format + +We provide a streaming format for record batches. It is presented as a sequence +of encapsulated messages, each of which follows the format above. The schema +comes first in the stream, and it is the same for all of the record batches +that follow. If any fields in the schema are dictionary-encoded, one or more +`DictionaryBatch` messages will follow the schema. + +``` + ... ... - - - + ``` -See the File.fbs document for details about the Flatbuffers metadata. The -record batches have a particular structure, defined next. +When a stream reader implementation is reading a stream, after each message, it +may read the next 4 bytes to know how large the message metadata that follows +is. Once the message flatbuffer is read, you can then read the message body. + +The stream writer can signal end-of-stream (EOS) either by writing a 0 length +as an `int32` or simply closing the stream interface. + +## File format -### Record batches +We define a "file format" supporting random access in a very similar format to +the streaming format. The file starts and ends with a magic string `ARROW1` +(plus padding). What follows in the file is identical to the stream format. At +the end of the file, we write a *footer* including offsets and sizes for each +of the data blocks in the file, so that random access is possible. See +[format/File.fbs][1] for the precise details of the file footer. -The record batch metadata is written as a flatbuffer (see -[format/Message.fbs][2] -- the RecordBatch message type) prefixed by its size, -followed by each of the memory buffers in the batch written end to end (with -appropriate alignment and padding): +Schematically we have: ``` - - - - + + + +