Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-245: add endianness to RecordBatch #113

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ The Apache Software Foundation (http://www.apache.org/).
This product includes software from the SFrame project (BSD, 3-clause).
* Copyright (C) 2015 Dato, Inc.
* Copyright (c) 2009 Carnegie Mellon University.

This product includes software from the Numpy project (BSD-new)
https://github.com/numpy/numpy/blob/e1f191c46f2eebd6cb892a4bfe14d9dd43a06c4e/numpy/core/src/multiarray/multiarraymodule.c#L2910
* Copyright (c) 1995, 1996, 1997 Jim Hugunin, hugunin@mit.edu
* Copyright (c) 2005 Travis E. Oliphant oliphant@ee.byu.edu Brigham Young University
20 changes: 18 additions & 2 deletions cpp/src/arrow/ipc/metadata-internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ Status FieldFromFlatbuffer(const flatbuf::Field* field, std::shared_ptr<Field>*

// Implement MessageBuilder

// will return the endianness of the system we are running on
// based the NUMPY_API function. See NOTICE.txt
flatbuf::Endianness endianness() {
union {
uint32_t i;
char c[4];
} bint = {0x01020304};

return bint.c[0] == 1 ? flatbuf::Endianness_Big : flatbuf::Endianness_Little;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a suitable reference for this code https://github.com/numpy/numpy/blob/master/numpy/core/src/multiarray/multiarraymodule.c#L2910 — is there another source where it might have come from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's where it's from I guess since this was a recommended approach by
http://stackoverflow.com/users/11465/david-cournapeau
http://stackoverflow.com/a/1001373/5492075
Although there's little variation in how many ways you can determine endianness.
Should I add a comment with attribution?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a mention in the NOTICE file

}

Status MessageBuilder::SetSchema(const Schema* schema) {
header_type_ = flatbuf::MessageHeader_Schema;

Expand All @@ -254,7 +265,11 @@ Status MessageBuilder::SetSchema(const Schema* schema) {
field_offsets.push_back(offset);
}

header_ = flatbuf::CreateSchema(fbb_, fbb_.CreateVector(field_offsets)).Union();
header_ = flatbuf::CreateSchema(
fbb_,
endianness(),
fbb_.CreateVector(field_offsets))
.Union();
body_length_ = 0;
return Status::OK();
}
Expand All @@ -263,7 +278,8 @@ Status MessageBuilder::SetRecordBatch(int32_t length, int64_t body_length,
const std::vector<flatbuf::FieldNode>& nodes,
const std::vector<flatbuf::Buffer>& buffers) {
header_type_ = flatbuf::MessageHeader_RecordBatch;
header_ = flatbuf::CreateRecordBatch(fbb_, length, fbb_.CreateVectorOfStructs(nodes),
header_ = flatbuf::CreateRecordBatch(fbb_, length,
fbb_.CreateVectorOfStructs(nodes),
fbb_.CreateVectorOfStructs(buffers))
.Union();
body_length_ = body_length;
Expand Down
Binary file added format/Arrow.graffle
Binary file not shown.
Binary file added format/Arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 8 additions & 1 deletion format/Layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ Base requirements

## Byte Order ([Endianness][3])

The Arrow format is little endian.
The Arrow format is little endian by default.
The Schema metadata has an endianness field indicating endianness of RecordBatches.
Typically this is the endianness of the system where the RecordBatch was generated.
The main use case is exchanging RecordBatches between systems with the same Endianness.
At first we will return an error when trying to read a Schema with an endianness
that does not match the underlying system. The reference implementation is focused on
Little Endian and provides tests for it. Eventually we may provide automatic conversion
via byte swapping.

## Alignment and Padding

Expand Down
11 changes: 11 additions & 0 deletions format/Message.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,21 @@ table Field {
children: [Field];
}

/// ----------------------------------------------------------------------
/// Endianness of the platform that produces the RecordBatch

enum Endianness:int { Little, Big }

/// ----------------------------------------------------------------------
/// A Schema describes the columns in a row batch

table Schema {

/// endianness of the buffer
/// it is Little Endian by default
/// if endianness doesn't match the underlying system then the vectors need to be converted
endianness: Endianness=Little;

fields: [Field];
}

Expand Down