Skip to content

Commit ca25548

Browse files
Merge branch 'columnar-swift:main' into main
2 parents 0b3d158 + 4731778 commit ca25548

12 files changed

+77
-93
lines changed

Sources/Arrow/ArrowArray.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,26 @@ public class ArrowArray<T>: AsString, AnyArray {
129129
self.arrowData = arrowData
130130
}
131131

132-
public func isNull(_ at: UInt) throws -> Bool {
133-
if at >= self.length {
134-
throw ArrowError.outOfBounds(index: Int64(at))
132+
public func isNull(at index: UInt) throws -> Bool {
133+
if index >= self.length {
134+
throw ArrowError.outOfBounds(index: Int64(index))
135135
}
136-
137-
return self.arrowData.isNull(at)
136+
return self.arrowData.isNull(index)
138137
}
139138

140139
public subscript(_ index: UInt) -> T? {
141140
fatalError("subscript() has not been implemented")
142141
}
143142

144143
public func asString(_ index: UInt) -> String {
145-
if self[index] == nil {
144+
guard let value = self[index] else {
146145
return ""
147146
}
148-
149-
return "\(self[index]!)"
147+
return "\(value)"
150148
}
151149

152150
public func asAny(_ index: UInt) -> Any? {
153-
if self[index] == nil {
154-
return nil
155-
}
156-
157-
return self[index]!
151+
return self[index]
158152
}
159153
}
160154

Sources/Arrow/ArrowArrayBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class ArrowArrayBuilder<T: ArrowBufferBuilder, U: ArrowArray<T.ItemType>>
7070
}
7171
}
7272

73-
public class NumberArrayBuilder<T>: ArrowArrayBuilder<FixedBufferBuilder<T>, FixedArray<T>> {
73+
public class NumberArrayBuilder<T>: ArrowArrayBuilder<FixedBufferBuilder<T>, FixedArray<T>> where T: Numeric {
7474
fileprivate convenience init() throws {
7575
try self.init(ArrowType(ArrowType.infoForNumericType(T.self)))
7676
}

Sources/Arrow/ArrowBufferBuilder.swift

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ public class ValuesBufferBuilder<T>: BaseBufferBuilder {
6565
}
6666
}
6767

68-
public class FixedBufferBuilder<T>: ValuesBufferBuilder<T>, ArrowBufferBuilder {
68+
/// A builder for fixed-width buffers.
69+
public class FixedBufferBuilder<T>: ValuesBufferBuilder<T>, ArrowBufferBuilder where T: Numeric {
6970
public typealias ItemType = T
70-
private let defaultVal: ItemType
71+
private let defaultVal: ItemType = 0
72+
7173
public required init() throws {
72-
self.defaultVal = try FixedBufferBuilder<T>.defaultValueForType()
7374
let values = ArrowBuffer.createBuffer(0, size: UInt(MemoryLayout<T>.stride))
7475
let nulls = ArrowBuffer.createBuffer(0, size: UInt(MemoryLayout<UInt8>.stride))
7576
super.init(values: values, nulls: nulls)
@@ -114,33 +115,6 @@ public class FixedBufferBuilder<T>: ValuesBufferBuilder<T>, ArrowBufferBuilder {
114115
ArrowBuffer.copyCurrent(self.nulls, to: &nulls, len: nulls.capacity)
115116
return [nulls, values]
116117
}
117-
118-
fileprivate static func defaultValueForType() throws -> T {
119-
let type = T.self
120-
if type == Int8.self {
121-
return Int8(0) as! T // swiftlint:disable:this force_cast
122-
} else if type == Int16.self {
123-
return Int16(0) as! T // swiftlint:disable:this force_cast
124-
} else if type == Int32.self {
125-
return Int32(0) as! T // swiftlint:disable:this force_cast
126-
} else if type == Int64.self {
127-
return Int64(0) as! T // swiftlint:disable:this force_cast
128-
} else if type == UInt8.self {
129-
return UInt8(0) as! T // swiftlint:disable:this force_cast
130-
} else if type == UInt16.self {
131-
return UInt16(0) as! T // swiftlint:disable:this force_cast
132-
} else if type == UInt32.self {
133-
return UInt32(0) as! T // swiftlint:disable:this force_cast
134-
} else if type == UInt64.self {
135-
return UInt64(0) as! T // swiftlint:disable:this force_cast
136-
} else if type == Float.self {
137-
return Float(0) as! T // swiftlint:disable:this force_cast
138-
} else if type == Double.self {
139-
return Double(0) as! T // swiftlint:disable:this force_cast
140-
}
141-
142-
throw ArrowError.unknownType("Unable to determine default value")
143-
}
144118
}
145119

146120
public class BoolBufferBuilder: ValuesBufferBuilder<Bool>, ArrowBufferBuilder {
@@ -280,7 +254,7 @@ public class VariableBufferBuilder<T>: ValuesBufferBuilder<T>, ArrowBufferBuilde
280254
}
281255
}
282256

283-
public class AbstractWrapperBufferBuilder<T, U>: ArrowBufferBuilder {
257+
public class AbstractWrapperBufferBuilder<T, U>: ArrowBufferBuilder where U: Numeric {
284258
public typealias ItemType = T
285259
public var capacity: UInt { return self.bufferBuilder.capacity }
286260
public var length: UInt { return self.bufferBuilder.length }

Sources/Arrow/ArrowCExporter.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,30 @@ public class ArrowCExporter {
3838
private let arrowType: ArrowType
3939
private let name: String
4040
private let arrowTypeName: String
41+
private let nameBuffer: [CChar]
42+
private let arrowTypeNameBuffer: [CChar]
43+
4144
@MainActor
4245
init(_ arrowType: ArrowType, name: String = "") throws {
4346
self.arrowType = arrowType
44-
// keeping the name str to ensure the cstring buffer remains valid
4547
self.name = name
4648
self.arrowTypeName = try arrowType.cDataFormatId
47-
self.nameCstr = (self.name as NSString).utf8String!
48-
self.arrowTypeNameCstr = (self.arrowTypeName as NSString).utf8String!
49+
// Create owned buffers for the C strings
50+
self.nameBuffer = Array(self.name.utf8CString)
51+
self.arrowTypeNameBuffer = Array(self.arrowTypeName.utf8CString)
52+
// Get stable pointers to the buffers
53+
guard let nameCstr = self.nameBuffer.withUnsafeBufferPointer({
54+
$0.baseAddress
55+
}) else {
56+
throw ArrowError.runtimeError("Failed to create field name C string.")
57+
}
58+
self.nameCstr = nameCstr
59+
guard let arrowTypeNameCstr = self.arrowTypeNameBuffer.withUnsafeBufferPointer({
60+
$0.baseAddress
61+
}) else {
62+
throw ArrowError.runtimeError("Failed to create type name C string.")
63+
}
64+
self.arrowTypeNameCstr = arrowTypeNameCstr
4965
super.init()
5066
}
5167
}

Sources/Arrow/ArrowReader.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public class ArrowReader {
7171

7272
public init() {}
7373

74-
private func loadSchema(_ schema: Schema) -> Result<
75-
ArrowSchema, ArrowError
76-
> {
74+
private func loadSchema(
75+
_ schema: Schema
76+
) -> Result<ArrowSchema, ArrowError> {
7777
let builder = ArrowSchema.Builder()
7878
for index in 0..<schema.fieldsCount {
7979
let field = schema.fields(at: index)!
@@ -91,9 +91,7 @@ public class ArrowReader {
9191
private func loadStructData(
9292
_ loadInfo: DataLoadInfo,
9393
field: FlatField
94-
)
95-
-> Result<ArrowArrayHolder, ArrowError>
96-
{
94+
) -> Result<ArrowArrayHolder, ArrowError> {
9795
guard let node = loadInfo.batchData.nextNode() else {
9896
return .failure(.invalid("Node not found"))
9997
}

Sources/Arrow/ArrowReaderHelper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func makeNestedHolder(
163163
}
164164

165165
func makeArrayHolder(
166-
_ field: org_apache_arrow_flatbuf_Field,
166+
_ field: FlatField,
167167
buffers: [ArrowBuffer],
168168
nullCount: UInt,
169169
children: [ArrowData]?,
@@ -174,7 +174,7 @@ func makeArrayHolder(
174174
arrowField, buffers: buffers, nullCount: nullCount, children: children, rbLength: rbLength)
175175
}
176176

177-
func makeArrayHolder( // swiftlint:disable:this cyclomatic_complexity
177+
func makeArrayHolder(
178178
_ field: ArrowField,
179179
buffers: [ArrowBuffer],
180180
nullCount: UInt,

Sources/Arrow/ArrowWriterHelper.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,29 @@ func toFBTypeEnum(_ arrowType: ArrowType) -> Result<org_apache_arrow_flatbuf_Typ
2525
let typeId = arrowType.id
2626
switch typeId {
2727
case .int8, .int16, .int32, .int64, .uint8, .uint16, .uint32, .uint64:
28-
return .success(org_apache_arrow_flatbuf_Type_.int)
28+
return .success(FlatType.int)
2929
case .float, .double:
30-
return .success(org_apache_arrow_flatbuf_Type_.floatingpoint)
30+
return .success(FlatType.floatingpoint)
3131
case .string:
32-
return .success(org_apache_arrow_flatbuf_Type_.utf8)
32+
return .success(FlatType.utf8)
3333
case .binary:
34-
return .success(org_apache_arrow_flatbuf_Type_.binary)
34+
return .success(FlatType.binary)
3535
case .boolean:
36-
return .success(org_apache_arrow_flatbuf_Type_.bool)
36+
return .success(FlatType.bool)
3737
case .date32, .date64:
38-
return .success(org_apache_arrow_flatbuf_Type_.date)
38+
return .success(FlatType.date)
3939
case .time32, .time64:
40-
return .success(org_apache_arrow_flatbuf_Type_.time)
40+
return .success(FlatType.time)
4141
case .timestamp:
42-
return .success(org_apache_arrow_flatbuf_Type_.timestamp)
42+
return .success(FlatType.timestamp)
4343
case .strct:
44-
return .success(org_apache_arrow_flatbuf_Type_.struct_)
44+
return .success(FlatType.struct_)
4545
default:
4646
return .failure(.unknownType("Unable to find flatbuf type for Arrow type: \(typeId)"))
4747
}
4848
}
4949

50-
func toFBType( // swiftlint:disable:this cyclomatic_complexity function_body_length
50+
func toFBType(
5151
_ fbb: inout FlatBufferBuilder,
5252
arrowType: ArrowType
5353
) -> Result<Offset, ArrowError> {

Sources/Arrow/ChunkedArray.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ChunkedArrayHolder {
3232

3333
public let getBufferData: () -> Result<[Data], ArrowError>
3434
public let getBufferDataSizes: () -> Result<[Int], ArrowError>
35-
public init<T>(_ chunked: ChunkedArray<T>) { // swiftlint:disable:this cyclomatic_complexity
35+
public init<T>(_ chunked: ChunkedArray<T>) {
3636
self.holder = chunked
3737
self.length = chunked.length
3838
self.type = chunked.type
@@ -119,7 +119,6 @@ public class ChunkedArray<T>: AsString {
119119
if arrays.count == 0 {
120120
return nil
121121
}
122-
123122
var localIndex = index
124123
var arrayIndex = 0
125124
var len: UInt = arrays[arrayIndex].length

Sources/Arrow/FlatBuffersTypes.swift renamed to Sources/Arrow/Generated/FlatBuffersTypes.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ typealias Buffer = org_apache_arrow_flatbuf_Buffer
2121
typealias FieldNode = org_apache_arrow_flatbuf_FieldNode
2222
typealias FlatRecordBatch = org_apache_arrow_flatbuf_RecordBatch
2323
typealias FlatType = org_apache_arrow_flatbuf_Type_
24+
typealias FloatingPoint = org_apache_arrow_flatbuf_FloatingPoint
25+
typealias FlatInt = org_apache_arrow_flatbuf_Int
2426
public typealias MessageHeader = org_apache_arrow_flatbuf_MessageHeader

Tests/ArrowTests/ArrayTests.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import XCTest
1616

1717
@testable import Arrow
1818

19-
final class ArrayTests: XCTestCase { // swiftlint:disable:this type_body_length
19+
final class ArrayTests: XCTestCase {
2020
func testPrimitiveArray() throws {
2121
// This is an example of a functional test case.
2222
// Use XCTAssert and related functions to verify your tests produce the correct
@@ -35,7 +35,7 @@ final class ArrayTests: XCTestCase { // swiftlint:disable:this type_body_length
3535
XCTAssertEqual(array.length, 101)
3636
XCTAssertEqual(array[1]!, 1)
3737
XCTAssertEqual(array[10]!, 10)
38-
XCTAssertEqual(try array.isNull(100), true)
38+
XCTAssertEqual(try array.isNull(at: 100), true)
3939

4040
let doubleBuilder: NumberArrayBuilder<Double> = try ArrowArrayBuilders.loadNumberArrayBuilder()
4141
doubleBuilder.append(14)
@@ -66,7 +66,7 @@ final class ArrayTests: XCTestCase { // swiftlint:disable:this type_body_length
6666
XCTAssertEqual(stringArray.length, 100)
6767
for index in 0..<stringArray.length {
6868
if index % 10 == 9 {
69-
XCTAssertEqual(try stringArray.isNull(index), true)
69+
XCTAssertEqual(try stringArray.isNull(at: index), true)
7070
} else {
7171
XCTAssertEqual(stringArray[index]!, "test" + String(index))
7272
}
@@ -142,7 +142,7 @@ final class ArrayTests: XCTestCase { // swiftlint:disable:this type_body_length
142142
XCTAssertEqual(binaryArray.length, 100)
143143
for index in 0..<binaryArray.length {
144144
if index % 10 == 9 {
145-
XCTAssertEqual(try binaryArray.isNull(index), true)
145+
XCTAssertEqual(try binaryArray.isNull(at: index), true)
146146
} else {
147147
let stringData = String(bytes: binaryArray[index]!, encoding: .utf8)
148148
XCTAssertEqual(stringData, "test" + String(index))
@@ -266,8 +266,10 @@ final class ArrayTests: XCTestCase { // swiftlint:disable:this type_body_length
266266
// Test timestamp with nanoseconds unit
267267
let nsBuilder = try ArrowArrayBuilders.loadTimestampArrayBuilder(.nanoseconds, timezone: nil)
268268
nsBuilder.append(nil)
269-
nsBuilder.append(1_609_459_200_000_000_000) // 2021-01-01 00:00:00.000000000
270-
nsBuilder.append(1_609_545_600_000_000_000) // 2021-01-02 00:00:00.000000000
269+
// 2021-01-01 00:00:00.000000000
270+
nsBuilder.append(1_609_459_200_000_000_000)
271+
// 2021-01-02 00:00:00.000000000
272+
nsBuilder.append(1_609_545_600_000_000_000)
271273
XCTAssertEqual(nsBuilder.nullCount, 1)
272274
XCTAssertEqual(nsBuilder.length, 3)
273275
XCTAssertEqual(nsBuilder.capacity, 264)

0 commit comments

Comments
 (0)