Skip to content

Commit 866463f

Browse files
Merge pull request #23 from willtemperley/main
Add struct array support to ArrowIPC.
2 parents 19cd3b3 + 1cfc295 commit 866463f

File tree

7 files changed

+286
-181
lines changed

7 files changed

+286
-181
lines changed

Sources/Arrow/Array/Array.swift

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import Foundation
1616

17-
public protocol ArrowArrayProtocol<ItemType> {
17+
public protocol ArrowArrayProtocol {
1818
associatedtype ItemType
1919
subscript(_ index: Int) -> ItemType? { get }
2020
var offset: Int { get }
@@ -70,18 +70,32 @@ public struct ArrowArrayBoolean: ArrowArrayProtocol {
7070
}
7171

7272
/// An Arrow array of fixed-width types.
73-
struct ArrowArrayFixed<Element, ValueBuffer>: ArrowArrayProtocol
73+
public struct ArrowArrayFixed<ValueBuffer>: ArrowArrayProtocol
7474
where
75-
Element: Numeric,
76-
ValueBuffer: FixedWidthBufferProtocol<Element>
75+
ValueBuffer: FixedWidthBufferProtocol,
76+
ValueBuffer.ElementType: Numeric
7777
{
78-
typealias ItemType = Element
79-
let offset: Int
80-
let length: Int
78+
public typealias ItemType = ValueBuffer.ElementType
79+
80+
// public typealias ItemType = Element
81+
public let offset: Int
82+
public let length: Int
8183
let nullBuffer: NullBuffer
8284
let valueBuffer: ValueBuffer
8385

84-
subscript(index: Int) -> Element? {
86+
public init(
87+
offset: Int = 0,
88+
length: Int,
89+
nullBuffer: NullBuffer,
90+
valueBuffer: ValueBuffer
91+
) {
92+
self.offset = offset
93+
self.length = length
94+
self.nullBuffer = nullBuffer
95+
self.valueBuffer = valueBuffer
96+
}
97+
98+
public subscript(index: Int) -> ValueBuffer.ElementType? {
8599
precondition(index >= 0 && index < length, "Invalid index.")
86100
let offsetIndex = self.offset + index
87101
if !self.nullBuffer.isSet(offsetIndex) {
@@ -90,7 +104,7 @@ where
90104
return valueBuffer[offsetIndex]
91105
}
92106

93-
func slice(offset: Int, length: Int) -> Self {
107+
public func slice(offset: Int, length: Int) -> Self {
94108
.init(
95109
offset: offset,
96110
length: length,
@@ -116,7 +130,7 @@ where
116130
let valueBuffer: ValueBuffer
117131

118132
public init(
119-
offset: Int,
133+
offset: Int = 0,
120134
length: Int,
121135
nullBuffer: NullBuffer,
122136
offsetsBuffer: OffsetsBuffer,
@@ -162,7 +176,7 @@ where
162176
{
163177
typealias ItemType = Date
164178

165-
let array: ArrowArrayFixed<Date32, ValueBuffer>
179+
let array: ArrowArrayFixed<ValueBuffer>
166180

167181
var offset: Int {
168182
array.offset
@@ -192,11 +206,11 @@ where
192206
/// An Arrow array of `Date`s with a resolution of 1 second.
193207
struct ArrowArrayDate64<ValueBuffer>: ArrowArrayProtocol
194208
where
195-
ValueBuffer: FixedWidthBufferProtocol<Int64>
209+
ValueBuffer: FixedWidthBufferProtocol<Date64>
196210
{
197211
typealias ItemType = Date
198212

199-
let array: ArrowArrayFixed<Date64, ValueBuffer>
213+
let array: ArrowArrayFixed<ValueBuffer>
200214

201215
var offset: Int {
202216
array.offset
@@ -259,3 +273,44 @@ where
259273
)
260274
}
261275
}
276+
277+
/// An Arrow struct array.
278+
public struct ArrowStructArray: ArrowArrayProtocol {
279+
public typealias ItemType = [String: Any]
280+
281+
let nullBuffer: NullBuffer
282+
public let offset: Int
283+
public let length: Int
284+
public let fields: [(name: String, array: any ArrowArrayProtocol)]
285+
286+
public init(
287+
offset: Int = 0,
288+
length: Int,
289+
nullBuffer: NullBuffer,
290+
fields: [(name: String, array: any ArrowArrayProtocol)]
291+
) {
292+
self.offset = offset
293+
self.length = length
294+
self.nullBuffer = nullBuffer
295+
self.fields = fields
296+
}
297+
298+
public subscript(index: Int) -> ItemType? {
299+
guard nullBuffer.isSet(offset + index) else { return nil }
300+
301+
var result: [String: Any] = [:]
302+
for (name, array) in fields {
303+
result[name] = array.any(at: index)
304+
}
305+
return result
306+
}
307+
308+
public func slice(offset newOffset: Int, length newLength: Int) -> Self {
309+
.init(
310+
offset: self.offset + newOffset,
311+
length: newLength,
312+
nullBuffer: nullBuffer,
313+
fields: fields
314+
)
315+
}
316+
}

Sources/Arrow/Array/Builder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ArrayBuilderBoolean: AnyArrayBuilder {
7676
/// A builder for Arrow arrays holding fixed-width types.
7777
class ArrayBuilderFixedWidth<T: Numeric>: AnyArrayBuilder {
7878

79-
typealias ArrayType = ArrowArrayFixed<T, FixedWidthBuffer<T>>
79+
typealias ArrayType = ArrowArrayFixed<FixedWidthBuffer<T>>
8080

8181
var length: Int
8282
let nullBuilder: NullBufferBuilder

Sources/Arrow/Array/StructArray.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

Sources/Arrow/Buffer/OffsetProtocol.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
/// A type which provides offset ranges in Arrow arrays.
1616
protocol OffsetsBuffer {
17-
/// Number of offset pairs available
18-
// var count: Int { get }
1917

2018
/// Get the start and end offsets for the element at index
2119
/// - Parameter index: Zero-based index of the element

Sources/ArrowIPC/Array+IPC.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import Arrow
1616
import Foundation
1717

18+
/// A `Data` backed Arrow utf8 array.
1819
typealias ArrowArrayUtf8 = ArrowArrayVariable<
1920
String,
2021
FixedWidthBufferIPC<Int32>,
@@ -39,7 +40,6 @@ extension ArrowArrayUtf8 {
3940
let offsetsBufferTyped = FixedWidthBufferIPC<Int32>(buffer: offsetsBuffer)
4041
let valueBufferTyped = VariableLengthBufferIPC<String>(buffer: valueBuffer)
4142
return Self(
42-
offset: 0,
4343
length: length,
4444
nullBuffer: nullBuffer,
4545
offsetsBuffer: offsetsBufferTyped,
@@ -72,7 +72,6 @@ extension ArrowArrayBinary {
7272
let offsetsBufferTyped = FixedWidthBufferIPC<Int32>(buffer: offsetsBuffer)
7373
let valueBufferTyped = VariableLengthBufferIPC<Data>(buffer: valueBuffer)
7474
return Self(
75-
offset: 0,
7675
length: length,
7776
nullBuffer: nullBuffer,
7877
offsetsBuffer: offsetsBufferTyped,

0 commit comments

Comments
 (0)