diff --git a/Sources/Arrow/ArrowArray.swift b/Sources/Arrow/ArrowArray.swift index 0942ecb..b9b1852 100644 --- a/Sources/Arrow/ArrowArray.swift +++ b/Sources/Arrow/ArrowArray.swift @@ -20,13 +20,13 @@ public protocol AnyArrowArray { var type: ArrowType { get } var length: UInt { get } var nullCount: UInt { get } - var array: AnyArray { get } - var data: ArrowData { get } + var arrowData: ArrowData { get } var bufferData: [Data] { get } var bufferDataSizes: [Int] { get } + func asAny(_ index: UInt) -> Any? } -public class ArrowArray: AsString, AnyArray, AnyArrowArray { +public class ArrowArray: AsString, AnyArrowArray { public typealias ItemType = T public let arrowData: ArrowData public var nullCount: UInt { self.arrowData.nullCount } @@ -40,11 +40,6 @@ public class ArrowArray: AsString, AnyArray, AnyArrowArray { arrowData.type } - // FIXME: Temporary to mimic ArrowArrayHolder protocol. - public var array: any AnyArray { self } - - public var data: ArrowData { self.arrowData } - public var bufferData: [Data] { self.arrowData.buffers.map { buffer in var data = Data() @@ -347,13 +342,13 @@ public class NestedArray: ArrowArray<[Any?]> { .load(as: Int32.self) var items: [Any?] = [] for i in startOffset.. { var output = "{" if let children = self.children { let parts = children.compactMap { child in - (child.array as? AsString)?.asString(index) + (child as? AsString)?.asString(index) } output.append(parts.joined(separator: ",")) } diff --git a/Sources/Arrow/ArrowArrayBuilder.swift b/Sources/Arrow/ArrowArrayBuilder.swift index 16fdf12..09fa818 100644 --- a/Sources/Arrow/ArrowArrayBuilder.swift +++ b/Sources/Arrow/ArrowArrayBuilder.swift @@ -201,7 +201,7 @@ public class StructArrayBuilder: ArrowArrayBuilder< let buffers = self.bufferBuilder.finish() var childData: [ArrowData] = [] for builder in self.builders { - childData.append(try builder.toHolder().array.arrowData) + childData.append(try builder.toHolder().arrowData) } let arrowData = try ArrowData( @@ -240,7 +240,7 @@ public class ListArrayBuilder: ArrowArrayBuilder public override func finish() throws(ArrowError) -> NestedArray { let buffers = self.bufferBuilder.finish() - let childData = try valueBuilder.toHolder().array.arrowData + let childData = try valueBuilder.toHolder().arrowData let arrowData = try ArrowData( self.type, buffers: buffers, diff --git a/Sources/Arrow/ArrowDecoder.swift b/Sources/Arrow/ArrowDecoder.swift index 7192fe0..c28be7b 100644 --- a/Sources/Arrow/ArrowDecoder.swift +++ b/Sources/Arrow/ArrowDecoder.swift @@ -91,39 +91,39 @@ public class ArrowDecoder: Decoder { ArrowSingleValueDecoding(self, codingPath: codingPath) } - func getCol(_ name: String) throws -> AnyArray { + func getCol(_ name: String) throws -> AnyArrowArray { guard let col = self.nameToCol[name] else { throw ArrowError.invalid("Column for key \"\(name)\" not found") } - return col.array + return col } - func getCol(_ index: Int) throws -> AnyArray { + func getCol(_ index: Int) throws -> AnyArrowArray { if index >= self.columns.count { throw ArrowError.outOfBounds(index: Int64(index)) } - return self.columns[index].array + return self.columns[index] } func doDecode(_ key: CodingKey) throws -> T? { - let array: AnyArray = try self.getCol(key.stringValue) + let array: AnyArrowArray = try self.getCol(key.stringValue) return array.asAny(self.rbIndex) as? T } func doDecode(_ col: Int) throws -> T? { - let array: AnyArray = try self.getCol(col) + let array: AnyArrowArray = try self.getCol(col) return array.asAny(self.rbIndex) as? T } func isNull(_ key: CodingKey) throws -> Bool { - let array: AnyArray = try self.getCol(key.stringValue) + let array: AnyArrowArray = try self.getCol(key.stringValue) return array.asAny(self.rbIndex) == nil } func isNull(_ col: Int) throws -> Bool { - let array: AnyArray = try self.getCol(col) + let array: AnyArrowArray = try self.getCol(col) return array.asAny(self.rbIndex) == nil } } diff --git a/Sources/Arrow/ArrowReader.swift b/Sources/Arrow/ArrowReader.swift index 0d1be95..0443661 100644 --- a/Sources/Arrow/ArrowReader.swift +++ b/Sources/Arrow/ArrowReader.swift @@ -126,7 +126,7 @@ public struct ArrowReader: Sendable { } switch loadField(loadInfo, field: childField) { case .success(let holder): - children.append(holder.array.arrowData) + children.append(holder.arrowData) case .failure(let error): return .failure(error) } @@ -173,7 +173,7 @@ public struct ArrowReader: Sendable { return makeArrayHolder( field, buffers: [arrowNullBuffer, arrowOffsetBuffer], nullCount: UInt(node.nullCount), - children: [childHolder.array.arrowData], + children: [childHolder.arrowData], rbLength: UInt(loadInfo.batchData.recordBatch.length)) case .failure(let error): return .failure(error) diff --git a/Sources/Arrow/ArrowTable.swift b/Sources/Arrow/ArrowTable.swift index 0e22bd4..47b170f 100644 --- a/Sources/Arrow/ArrowTable.swift +++ b/Sources/Arrow/ArrowTable.swift @@ -127,7 +127,7 @@ public class ArrowTable { ) throws(ArrowError) -> ArrowColumn { var arrays: [ArrowArray] = [] for holder in holders { - guard let array = holder.array as? ArrowArray else { + guard let array = holder as? ArrowArray else { throw .runtimeError( "Array type mismatch: expected \(T.self) for field \(field.name)" ) @@ -266,20 +266,15 @@ public class RecordBatch { for columnIndex: Int ) throws(ArrowError) -> ArrowArray { let arrayHolder = column(columnIndex) - if let array = arrayHolder.array as? ArrowArray { + if let array = arrayHolder as? ArrowArray { return array } else { throw .invalid( - "Could not convert \(arrayHolder.array) for \(columnIndex)" + "Could not convert \(arrayHolder) for \(columnIndex)" ) } } - public func anyData(for columnIndex: Int) -> AnyArray { - let arrayHolder = column(columnIndex) - return arrayHolder.array - } - public func column(_ index: Int) -> AnyArrowArray { self.columns[index] } diff --git a/Sources/Arrow/ArrowWriter.swift b/Sources/Arrow/ArrowWriter.swift index 755d5f8..013cdb4 100644 --- a/Sources/Arrow/ArrowWriter.swift +++ b/Sources/Arrow/ArrowWriter.swift @@ -209,7 +209,7 @@ public class ArrowWriter { offsets.append(fbb.create(struct: fieldNode)) if case .strct(let fields) = column.type { - let nestedArray = column.array as? NestedArray + let nestedArray = column as? NestedArray if let nestedFields = nestedArray?.fields { writeFieldNodes( fields, @@ -239,7 +239,7 @@ public class ArrowWriter { bufferOffset += bufferDataSize if case .strct(let fields) = column.type { - let nestedArray = column.array as? NestedArray + let nestedArray = column as? NestedArray if let nestedFields = nestedArray?.fields { writeBufferInfo( fields, columns: nestedFields, @@ -312,7 +312,7 @@ public class ArrowWriter { addPadForAlignment(&bufferData) writer.append(bufferData) if case .strct(let fields) = column.type { - guard let nestedArray = column.array as? NestedArray, + guard let nestedArray = column as? NestedArray, let nestedFields = nestedArray.fields else { return .failure( diff --git a/Sources/Arrow/ChunkedArray.swift b/Sources/Arrow/ChunkedArray.swift index aa97a38..bd72930 100644 --- a/Sources/Arrow/ChunkedArray.swift +++ b/Sources/Arrow/ChunkedArray.swift @@ -14,12 +14,6 @@ import Foundation -public protocol AnyArray { - var arrowData: ArrowData { get } - func asAny(_ index: UInt) -> Any? - var length: UInt { get } -} - public protocol AsString { func asString(_ index: UInt) -> String } diff --git a/Tests/ArrowTests/ArrayTests.swift b/Tests/ArrowTests/ArrayTests.swift index dd8b146..9bf9434 100644 --- a/Tests/ArrowTests/ArrayTests.swift +++ b/Tests/ArrowTests/ArrayTests.swift @@ -379,7 +379,7 @@ struct ArrayTests { #expect(structArray.length == 3) #expect(structArray[1] == nil) #expect(structArray.fields![0].length == 3) - #expect(structArray.fields![0].array.asAny(1) == nil) + #expect(structArray.fields![0].asAny(1) == nil) #expect(structArray[0]![STIndex.bool.rawValue] as? Bool == true) #expect(structArray[0]![STIndex.int8.rawValue] as? Int8 == 1) #expect(structArray[0]![STIndex.int16.rawValue] as? Int16 == 2) diff --git a/Tests/ArrowTests/CodableTests.swift b/Tests/ArrowTests/CodableTests.swift index 125458d..67ae61d 100644 --- a/Tests/ArrowTests/CodableTests.swift +++ b/Tests/ArrowTests/CodableTests.swift @@ -244,7 +244,7 @@ struct CodableTests { func getArrayValue(_ rb: RecordBatch, colIndex: Int, rowIndex: UInt) -> T? { - let anyArray = rb.columns[colIndex].array + let anyArray = rb.columns[colIndex] return anyArray.asAny(UInt(rowIndex)) as? T } @@ -371,7 +371,7 @@ struct CodableTests { #expect(rb.columns[0].type == .int32) for index in 0..<100 { if index == 10 { - let anyArray = rb.columns[0].array + let anyArray = rb.columns[0] #expect(anyArray.asAny(UInt(index)) == nil) } else { #expect( diff --git a/Tests/ArrowTests/IPCTests.swift b/Tests/ArrowTests/IPCTests.swift index 2a19942..6614627 100644 --- a/Tests/ArrowTests/IPCTests.swift +++ b/Tests/ArrowTests/IPCTests.swift @@ -71,7 +71,7 @@ func checkBoolRecordBatch( #expect(recordBatch.schema.fields[1].type == .utf8) for index in 0..