Skip to content

Commit 516a3ea

Browse files
committed
apacheGH-46296: [Swift] Add struct ipc unit test, enum fix and changes from PR 46297
1 parent 2bb5fd5 commit 516a3ea

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

swift/Arrow/Sources/Arrow/ArrowType.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ extension ArrowType.Info: Equatable {
385385
return lhsId == rhsId
386386
case (.timeInfo(let lhsId), .timeInfo(let rhsId)):
387387
return lhsId == rhsId
388+
case (.complexInfo(let lhsId), .complexInfo(let rhsId)):
389+
return lhsId == rhsId
388390
default:
389391
return false
390392
}

swift/Arrow/Sources/Arrow/ProtoUtil.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func fromProto( // swiftlint:disable:this cyclomatic_complexity
6464
let arrowUnit: ArrowTime64Unit = timeType.unit == .microsecond ? .microseconds : .nanoseconds
6565
arrowType = ArrowTypeTime64(arrowUnit)
6666
}
67+
case .struct_:
68+
arrowType = ArrowType(ArrowType.ArrowStruct)
6769
default:
6870
arrowType = ArrowType(ArrowType.ArrowUnknown)
6971
}

swift/Arrow/Tests/ArrowTests/IPCTests.swift

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,44 @@ func makeRecordBatch() throws -> RecordBatch {
118118
}
119119
}
120120

121-
final class IPCFileReaderTests: XCTestCase {
121+
final class IPCFileReaderTests: XCTestCase { // swiftlint:disable:this type_body_length
122+
func testFileReader_struct() throws {
123+
let fileURL = currentDirectory().appendingPathComponent("../../testdata_struct.arrow")
124+
let arrowReader = ArrowReader()
125+
let result = arrowReader.fromFile(fileURL)
126+
let recordBatches: [RecordBatch]
127+
switch result {
128+
case .success(let result):
129+
recordBatches = result.batches
130+
case .failure(let error):
131+
throw error
132+
}
133+
134+
XCTAssertEqual(recordBatches.count, 1)
135+
for recordBatch in recordBatches {
136+
XCTAssertEqual(recordBatch.length, 3)
137+
XCTAssertEqual(recordBatch.columns.count, 1)
138+
XCTAssertEqual(recordBatch.schema.fields.count, 1)
139+
XCTAssertEqual(recordBatch.schema.fields[0].type.info, ArrowType.ArrowStruct)
140+
let column = recordBatch.columns[0]
141+
XCTAssertNotNil(column.array as? StructArray)
142+
if let structArray = column.array as? StructArray {
143+
XCTAssertEqual(structArray.arrowFields?.count, 2)
144+
XCTAssertEqual(structArray.arrowFields?[0].type.info, ArrowType.ArrowString)
145+
XCTAssertEqual(structArray.arrowFields?[1].type.info, ArrowType.ArrowBool)
146+
for index in 0..<structArray.length {
147+
if index == 2 {
148+
XCTAssertNil(structArray[index])
149+
} else {
150+
XCTAssertEqual(structArray[index]?[0] as? String, "\(index)")
151+
XCTAssertEqual(structArray[index]?[1] as? Bool, index % 2 == 1)
152+
}
153+
}
154+
}
155+
156+
}
157+
}
158+
122159
func testFileReader_double() throws {
123160
let fileURL = currentDirectory().appendingPathComponent("../../testdata_double.arrow")
124161
let arrowReader = ArrowReader()
@@ -384,3 +421,4 @@ final class IPCFileReaderTests: XCTestCase {
384421
}
385422
}
386423
}
424+
// swiftlint:disable:this file_length

swift/data-generator/swift-datagen/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
module swift-datagen/main
1818

19-
go 1.22.7
19+
go 1.23.0
20+
2021
toolchain go1.24.1
2122

2223
require github.com/apache/arrow-go/v18 v18.2.0

swift/data-generator/swift-datagen/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,35 @@ func writeDoubleData() {
8282
writeBytes(rec, "testdata_double.arrow")
8383
}
8484

85+
func writeStructData() {
86+
mem := memory.NewGoAllocator()
87+
88+
fields := []arrow.Field{
89+
{Name: "my struct", Type: arrow.StructOf([]arrow.Field{
90+
{Name: "my string", Type: arrow.BinaryTypes.String},
91+
{Name: "my bool", Type: arrow.FixedWidthTypes.Boolean},
92+
}...)},
93+
}
94+
95+
schema := arrow.NewSchema(fields, nil)
96+
97+
bld := array.NewRecordBuilder(mem, schema)
98+
defer bld.Release()
99+
100+
sb := bld.Field(0).(*array.StructBuilder)
101+
f1b := sb.FieldBuilder(0).(*array.StringBuilder)
102+
f2b := sb.FieldBuilder(1).(*array.BooleanBuilder)
103+
104+
sb.AppendValues([]bool{true, true, false})
105+
f1b.AppendValues([]string{"0", "1", ""}, []bool{true, true, false})
106+
f2b.AppendValues([]bool{false, true, false}, []bool{true, true, false})
107+
108+
rec := bld.NewRecord()
109+
writeBytes(rec, "testdata_struct.arrow")
110+
}
111+
85112
func main() {
86113
writeBoolData()
87114
writeDoubleData()
115+
writeStructData()
88116
}

0 commit comments

Comments
 (0)