Skip to content
Merged
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
22 changes: 18 additions & 4 deletions Arrow/Sources/Arrow/ArrowWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,25 @@ public class ArrowWriter { // swiftlint:disable:this type_body_length
withUnsafeBytes(of: CONTINUATIONMARKER.littleEndian) {writer.append(Data($0))}
withUnsafeBytes(of: rbResult.1.o.littleEndian) {writer.append(Data($0))}
writer.append(rbResult.0)
addPadForAlignment(&writer)
Copy link
Member

Choose a reason for hiding this comment

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

This is for "Padding bytes to an 8-byte boundary" https://arrow.apache.org/docs/format/Columnar.html#recordbatch-message , right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes - I believe that every time a FlatBuffers message is written it needs to be padded. I think that FlatBuffers messages align to 8 bytes internally but not necessarily where they terminate.

let metadataLength = writer.count - startIndex
let bodyStart = writer.count
switch writeRecordBatchData(&writer, fields: batch.schema.fields, columns: batch.columns) {
case .success:
let bodyLength = writer.count - bodyStart
let expectedSize = startIndex + metadataLength + bodyLength
guard expectedSize == writer.count else {
return .failure(.invalid(
"Invalid Block. Expected \(expectedSize), got \(writer.count)"
))
}
rbBlocks.append(
org_apache_arrow_flatbuf_Block(offset: Int64(startIndex),
metaDataLength: Int32(0),
bodyLength: Int64(rbResult.1.o)))
org_apache_arrow_flatbuf_Block(
offset: Int64(startIndex),
metaDataLength: Int32(metadataLength),
bodyLength: Int64(bodyLength)
)
)
case .failure(let error):
return .failure(error)
}
Expand Down Expand Up @@ -293,6 +306,7 @@ public class ArrowWriter { // swiftlint:disable:this type_body_length
case .success(let schemaOffset):
fbb.finish(offset: schemaOffset)
writer.append(fbb.data)
addPadForAlignment(&writer)
case .failure(let error):
return .failure(error)
}
Expand Down Expand Up @@ -379,7 +393,7 @@ public class ArrowWriter { // swiftlint:disable:this type_body_length
addPadForAlignment(&markerData)

var writer: any DataWriter = FileDataWriter(fileHandle)
writer.append(FILEMARKER.data(using: .utf8)!)
writer.append(markerData)
switch writeFile(&writer, info: info) {
case .success:
writer.append(FILEMARKER.data(using: .utf8)!)
Expand Down