Skip to content

Commit 7b120e5

Browse files
committed
Remove AnyArray.
1 parent b8a0755 commit 7b120e5

File tree

10 files changed

+48
-65
lines changed

10 files changed

+48
-65
lines changed

Sources/Arrow/ArrowArray.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public protocol AnyArrowArray {
2020
var type: ArrowType { get }
2121
var length: UInt { get }
2222
var nullCount: UInt { get }
23-
var array: AnyArray { get }
24-
var data: ArrowData { get }
23+
var arrowData: ArrowData { get }
2524
var bufferData: [Data] { get }
2625
var bufferDataSizes: [Int] { get }
26+
func asAny(_ index: UInt) -> Any?
2727
}
2828

29-
public class ArrowArray<T>: AsString, AnyArray, AnyArrowArray {
29+
public class ArrowArray<T>: AsString, AnyArrowArray {
3030
public typealias ItemType = T
3131
public let arrowData: ArrowData
3232
public var nullCount: UInt { self.arrowData.nullCount }
@@ -40,11 +40,6 @@ public class ArrowArray<T>: AsString, AnyArray, AnyArrowArray {
4040
arrowData.type
4141
}
4242

43-
// FIXME: Temporary to mimic ArrowArrayHolder protocol.
44-
public var array: any AnyArray { self }
45-
46-
public var data: ArrowData { self.arrowData }
47-
4843
public var bufferData: [Data] {
4944
self.arrowData.buffers.map { buffer in
5045
var data = Data()
@@ -347,13 +342,13 @@ public class NestedArray: ArrowArray<[Any?]> {
347342
.load(as: Int32.self)
348343
var items: [Any?] = []
349344
for i in startOffset..<endOffset {
350-
items.append(values.array.asAny(UInt(i)))
345+
items.append(values.asAny(UInt(i)))
351346
}
352347
return items
353348
case .strct(let _):
354349
var result: [Any?] = []
355350
for field in children {
356-
result.append(field.array.asAny(index))
351+
result.append(field.asAny(index))
357352
}
358353
return result
359354
default:
@@ -393,7 +388,7 @@ public class NestedArray: ArrowArray<[Any?]> {
393388
var output = "{"
394389
if let children = self.children {
395390
let parts = children.compactMap { child in
396-
(child.array as? AsString)?.asString(index)
391+
(child as? AsString)?.asString(index)
397392
}
398393
output.append(parts.joined(separator: ","))
399394
}

Sources/Arrow/ArrowArrayBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public class StructArrayBuilder: ArrowArrayBuilder<
201201
let buffers = self.bufferBuilder.finish()
202202
var childData: [ArrowData] = []
203203
for builder in self.builders {
204-
childData.append(try builder.toHolder().array.arrowData)
204+
childData.append(try builder.toHolder().arrowData)
205205
}
206206

207207
let arrowData = try ArrowData(
@@ -240,7 +240,7 @@ public class ListArrayBuilder: ArrowArrayBuilder<ListBufferBuilder, NestedArray>
240240

241241
public override func finish() throws(ArrowError) -> NestedArray {
242242
let buffers = self.bufferBuilder.finish()
243-
let childData = try valueBuilder.toHolder().array.arrowData
243+
let childData = try valueBuilder.toHolder().arrowData
244244
let arrowData = try ArrowData(
245245
self.type,
246246
buffers: buffers,

Sources/Arrow/ArrowDecoder.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,39 @@ public class ArrowDecoder: Decoder {
9191
ArrowSingleValueDecoding(self, codingPath: codingPath)
9292
}
9393

94-
func getCol(_ name: String) throws -> AnyArray {
94+
func getCol(_ name: String) throws -> AnyArrowArray {
9595
guard let col = self.nameToCol[name] else {
9696
throw ArrowError.invalid("Column for key \"\(name)\" not found")
9797
}
9898

99-
return col.array
99+
return col
100100
}
101101

102-
func getCol(_ index: Int) throws -> AnyArray {
102+
func getCol(_ index: Int) throws -> AnyArrowArray {
103103
if index >= self.columns.count {
104104
throw ArrowError.outOfBounds(index: Int64(index))
105105
}
106106

107-
return self.columns[index].array
107+
return self.columns[index]
108108
}
109109

110110
func doDecode<T>(_ key: CodingKey) throws -> T? {
111-
let array: AnyArray = try self.getCol(key.stringValue)
111+
let array: AnyArrowArray = try self.getCol(key.stringValue)
112112
return array.asAny(self.rbIndex) as? T
113113
}
114114

115115
func doDecode<T>(_ col: Int) throws -> T? {
116-
let array: AnyArray = try self.getCol(col)
116+
let array: AnyArrowArray = try self.getCol(col)
117117
return array.asAny(self.rbIndex) as? T
118118
}
119119

120120
func isNull(_ key: CodingKey) throws -> Bool {
121-
let array: AnyArray = try self.getCol(key.stringValue)
121+
let array: AnyArrowArray = try self.getCol(key.stringValue)
122122
return array.asAny(self.rbIndex) == nil
123123
}
124124

125125
func isNull(_ col: Int) throws -> Bool {
126-
let array: AnyArray = try self.getCol(col)
126+
let array: AnyArrowArray = try self.getCol(col)
127127
return array.asAny(self.rbIndex) == nil
128128
}
129129
}

Sources/Arrow/ArrowReader.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public struct ArrowReader: Sendable {
126126
}
127127
switch loadField(loadInfo, field: childField) {
128128
case .success(let holder):
129-
children.append(holder.array.arrowData)
129+
children.append(holder.arrowData)
130130
case .failure(let error):
131131
return .failure(error)
132132
}
@@ -173,7 +173,7 @@ public struct ArrowReader: Sendable {
173173
return makeArrayHolder(
174174
field, buffers: [arrowNullBuffer, arrowOffsetBuffer],
175175
nullCount: UInt(node.nullCount),
176-
children: [childHolder.array.arrowData],
176+
children: [childHolder.arrowData],
177177
rbLength: UInt(loadInfo.batchData.recordBatch.length))
178178
case .failure(let error):
179179
return .failure(error)

Sources/Arrow/ArrowTable.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public class ArrowTable {
127127
) throws(ArrowError) -> ArrowColumn {
128128
var arrays: [ArrowArray<T>] = []
129129
for holder in holders {
130-
guard let array = holder.array as? ArrowArray<T> else {
130+
guard let array = holder as? ArrowArray<T> else {
131131
throw .runtimeError(
132132
"Array type mismatch: expected \(T.self) for field \(field.name)"
133133
)
@@ -266,20 +266,15 @@ public class RecordBatch {
266266
for columnIndex: Int
267267
) throws(ArrowError) -> ArrowArray<T> {
268268
let arrayHolder = column(columnIndex)
269-
if let array = arrayHolder.array as? ArrowArray<T> {
269+
if let array = arrayHolder as? ArrowArray<T> {
270270
return array
271271
} else {
272272
throw .invalid(
273-
"Could not convert \(arrayHolder.array) for \(columnIndex)"
273+
"Could not convert \(arrayHolder) for \(columnIndex)"
274274
)
275275
}
276276
}
277277

278-
public func anyData(for columnIndex: Int) -> AnyArray {
279-
let arrayHolder = column(columnIndex)
280-
return arrayHolder.array
281-
}
282-
283278
public func column(_ index: Int) -> AnyArrowArray {
284279
self.columns[index]
285280
}

Sources/Arrow/ArrowWriter.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public class ArrowWriter {
209209
offsets.append(fbb.create(struct: fieldNode))
210210
if case .strct(let fields) = column.type {
211211

212-
let nestedArray = column.array as? NestedArray
212+
let nestedArray = column as? NestedArray
213213
if let nestedFields = nestedArray?.fields {
214214
writeFieldNodes(
215215
fields,
@@ -239,7 +239,7 @@ public class ArrowWriter {
239239
bufferOffset += bufferDataSize
240240

241241
if case .strct(let fields) = column.type {
242-
let nestedArray = column.array as? NestedArray
242+
let nestedArray = column as? NestedArray
243243
if let nestedFields = nestedArray?.fields {
244244
writeBufferInfo(
245245
fields, columns: nestedFields,
@@ -312,7 +312,7 @@ public class ArrowWriter {
312312
addPadForAlignment(&bufferData)
313313
writer.append(bufferData)
314314
if case .strct(let fields) = column.type {
315-
guard let nestedArray = column.array as? NestedArray,
315+
guard let nestedArray = column as? NestedArray,
316316
let nestedFields = nestedArray.fields
317317
else {
318318
return .failure(

Sources/Arrow/ChunkedArray.swift

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

1515
import Foundation
1616

17-
public protocol AnyArray {
18-
var arrowData: ArrowData { get }
19-
func asAny(_ index: UInt) -> Any?
20-
var length: UInt { get }
21-
}
22-
2317
public protocol AsString {
2418
func asString(_ index: UInt) -> String
2519
}

Tests/ArrowTests/ArrayTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ struct ArrayTests {
379379
#expect(structArray.length == 3)
380380
#expect(structArray[1] == nil)
381381
#expect(structArray.fields![0].length == 3)
382-
#expect(structArray.fields![0].array.asAny(1) == nil)
382+
#expect(structArray.fields![0].asAny(1) == nil)
383383
#expect(structArray[0]![STIndex.bool.rawValue] as? Bool == true)
384384
#expect(structArray[0]![STIndex.int8.rawValue] as? Int8 == 1)
385385
#expect(structArray[0]![STIndex.int16.rawValue] as? Int16 == 2)

Tests/ArrowTests/CodableTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ struct CodableTests {
244244

245245
func getArrayValue<T>(_ rb: RecordBatch, colIndex: Int, rowIndex: UInt) -> T?
246246
{
247-
let anyArray = rb.columns[colIndex].array
247+
let anyArray = rb.columns[colIndex]
248248
return anyArray.asAny(UInt(rowIndex)) as? T
249249
}
250250

@@ -371,7 +371,7 @@ struct CodableTests {
371371
#expect(rb.columns[0].type == .int32)
372372
for index in 0..<100 {
373373
if index == 10 {
374-
let anyArray = rb.columns[0].array
374+
let anyArray = rb.columns[0]
375375
#expect(anyArray.asAny(UInt(index)) == nil)
376376
} else {
377377
#expect(

Tests/ArrowTests/IPCTests.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func checkBoolRecordBatch(
7171
#expect(recordBatch.schema.fields[1].type == .utf8)
7272
for index in 0..<recordBatch.length {
7373
let column = recordBatch.columns[0]
74-
guard let str = column.array as? AsString else {
74+
guard let str = column as? AsString else {
7575
throw .invalid("Could not cast column to AsString")
7676
}
7777
let val = "\(str.asString(index))"
@@ -108,8 +108,7 @@ func checkStructRecordBatch(
108108
Issue.record("Expected field 0 to be a struct")
109109
return []
110110
}
111-
let nestedArray = recordBatch.columns[0].array as? NestedArray
112-
guard let nestedArray else {
111+
guard let nestedArray = recordBatch.columns[0] as? NestedArray else {
113112
throw .runtimeError("Could not cast to NestedArray")
114113
}
115114
guard let fields = nestedArray.fields else {
@@ -119,12 +118,12 @@ func checkStructRecordBatch(
119118
#expect(fields[0].type == .utf8)
120119
#expect(fields[1].type == .boolean)
121120
let column = recordBatch.columns[0]
122-
guard let str = column.array as? AsString else {
121+
guard let str = column as? AsString else {
123122
throw .runtimeError("String array is nil")
124123
}
125124
#expect("\(str.asString(0))" == "{0,false}")
126125
#expect("\(str.asString(1))" == "{1,true}")
127-
#expect(column.array.asAny(2) == nil)
126+
#expect(column.asAny(2) == nil)
128127
}
129128
return recordBatches
130129
}
@@ -278,15 +277,15 @@ struct IPCStreamReaderTests {
278277
#expect(recordBatch.schema.fields[4].type == .float32)
279278
let columns = recordBatch.columns
280279
#expect(columns[0].nullCount == 2)
281-
let dateVal = "\((columns[2].array as! AsString).asString(0))"
280+
let dateVal = "\((columns[2] as! AsString).asString(0))"
282281
#expect(dateVal == "2014-09-10 00:00:00 +0000")
283-
let stringVal = "\((columns[1].array as! AsString).asString(1))"
282+
let stringVal = "\((columns[1] as! AsString).asString(1))"
284283
#expect(stringVal == "test22")
285-
let uintVal = "\((columns[0].array as! AsString).asString(0))"
284+
let uintVal = "\((columns[0] as! AsString).asString(0))"
286285
#expect(uintVal == "10")
287-
let stringVal2 = "\((columns[1].array as! AsString).asString(3))"
286+
let stringVal2 = "\((columns[1] as! AsString).asString(3))"
288287
#expect(stringVal2 == "test44")
289-
let uintVal2 = "\((columns[0].array as! AsString).asString(3))"
288+
let uintVal2 = "\((columns[0] as! AsString).asString(3))"
290289
#expect(uintVal2 == "44")
291290
}
292291
case .failure(let error):
@@ -324,7 +323,7 @@ struct IPCFileReaderTests {
324323
recordBatch.schema.fields[1].type == .utf8)
325324
for index in 0..<recordBatch.length {
326325
let column = recordBatch.columns[1]
327-
let str = column.array as! AsString
326+
let str = column as! AsString
328327
let val = "\(str.asString(index))"
329328
if index != 1 {
330329
#expect(!val.isEmpty)
@@ -431,15 +430,15 @@ struct IPCFileReaderTests {
431430
#expect(recordBatch.schema.fields[4].type == .float32)
432431
let columns = recordBatch.columns
433432
#expect(columns[0].nullCount == 2)
434-
let dateVal = "\((columns[2].array as! AsString).asString(0))"
433+
let dateVal = "\((columns[2] as! AsString).asString(0))"
435434
#expect(dateVal == "2014-09-10 00:00:00 +0000")
436-
let stringVal = "\((columns[1].array as! AsString).asString(1))"
435+
let stringVal = "\((columns[1] as! AsString).asString(1))"
437436
#expect(stringVal == "test22")
438-
let uintVal = "\((columns[0].array as! AsString).asString(0))"
437+
let uintVal = "\((columns[0] as! AsString).asString(0))"
439438
#expect(uintVal == "10")
440-
let stringVal2 = "\((columns[1].array as! AsString).asString(3))"
439+
let stringVal2 = "\((columns[1] as! AsString).asString(3))"
441440
#expect(stringVal2 == "test44")
442-
let uintVal2 = "\((columns[0].array as! AsString).asString(3))"
441+
let uintVal2 = "\((columns[0] as! AsString).asString(3))"
443442
#expect(uintVal2 == "44")
444443
}
445444
case .failure(let error):
@@ -572,11 +571,11 @@ struct IPCFileReaderTests {
572571
#expect(fields.count == 14)
573572
let columns = recordBatch.columns
574573
#expect(columns[0].nullCount == 1)
575-
#expect(columns[0].array.asAny(1) == nil)
576-
let structVal = "\((columns[0].array as? AsString)!.asString(0))"
574+
#expect(columns[0].asAny(1) == nil)
575+
let structVal = "\((columns[0] as? AsString)!.asString(0))"
577576
#expect(
578577
structVal == "{true,1,2,3,4,5,6,7,8,9.9,10.1,11,12,\(currentDate)}")
579-
let nestedArray = (recordBatch.columns[0].array as? NestedArray)!
578+
let nestedArray = (recordBatch.columns[0] as? NestedArray)!
580579
#expect(nestedArray.length == 3)
581580
#expect(nestedArray.fields != nil)
582581
#expect(nestedArray.fields!.count == 14)
@@ -626,7 +625,7 @@ struct IPCFileReaderTests {
626625
#expect(recordBatch.length == 4)
627626
let columns = recordBatch.columns
628627
let stringVal =
629-
"\((columns[0].array as! AsString).asString(1))"
628+
"\((columns[0] as! AsString).asString(1))"
630629
#expect(stringVal == "test22")
631630
case .failure(let error):
632631
throw error
@@ -661,10 +660,10 @@ struct IPCFileReaderTests {
661660
#expect(recordBatch.length == 4)
662661
let columns = recordBatch.columns
663662
let stringVal =
664-
"\((columns[0].array as! AsString).asString(0))"
663+
"\((columns[0] as! AsString).asString(0))"
665664
#expect(stringVal == "12345678")
666665
let stringVal2 =
667-
"\((columns[1].array as! AsString).asString(3))"
666+
"\((columns[1] as! AsString).asString(3))"
668667
#expect(stringVal2 == "3")
669668
case .failure(let error):
670669
throw error

0 commit comments

Comments
 (0)