diff --git a/ci/docker/ubuntu-swift.dockerfile b/ci/docker/ubuntu-swift.dockerfile index 5ef6bc433df38..4789c9188c226 100644 --- a/ci/docker/ubuntu-swift.dockerfile +++ b/ci/docker/ubuntu-swift.dockerfile @@ -17,8 +17,18 @@ FROM swift:5.7.3 -# Install golang +# Go is needed for generating test data RUN apt-get update -y -q && \ apt-get install -y -q --no-install-recommends \ - golang-go && \ - apt-get clean \ No newline at end of file + golang-go \ + unzip \ + wget && \ + apt-get clean + +ARG swift_lint=0.53.0 +RUN wget https://github.com/realm/SwiftLint/releases/download/${swift_lint}/swiftlint_linux.zip && \ + unzip swiftlint_linux.zip && \ + mv swiftlint /usr/local/bin/ && \ + mkdir -p /usr/local/share/doc/swiftlint/ && \ + mv LICENSE /usr/local/share/doc/swiftlint/ && \ + rm -rf swiftlint_linux.zip diff --git a/ci/scripts/swift_test.sh b/ci/scripts/swift_test.sh index b7ab37fd489c9..b523e3891d93c 100755 --- a/ci/scripts/swift_test.sh +++ b/ci/scripts/swift_test.sh @@ -20,12 +20,18 @@ set -ex data_gen_dir=${1}/swift/data-generator/swift-datagen +export GOPATH=/ pushd ${data_gen_dir} go get -d ./... -go run main.go +go run . cp *.arrow ../../Arrow popd +source_dir=${1}/swift +pushd ${source_dir} +swiftlint --strict +popd + source_dir=${1}/swift/Arrow pushd ${source_dir} swift test diff --git a/swift/.swiftlint.yml b/swift/.swiftlint.yml new file mode 100644 index 0000000000000..9ce377285973f --- /dev/null +++ b/swift/.swiftlint.yml @@ -0,0 +1,33 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +included: + - Arrow/Sources + - Arrow/Tests + - ArrowFlight/Sources + - ArrowFlight/Tests +excluded: + - Arrow/Sources/Arrow/File_generated.swift + - Arrow/Sources/Arrow/Message_generated.swift + - Arrow/Sources/Arrow/Schema_generated.swift + - Arrow/Sources/Arrow/SparseTensor_generated.swift + - Arrow/Sources/Arrow/Tensor_generated.swift + - ArrowFlight/Sources/ArrowFlight/Flight.grpc.swift + - ArrowFlight/Sources/ArrowFlight/Flight.pb.swift +identifier_name: + min_length: 2 # only warning +allow_zero_lintable_files: false diff --git a/swift/Arrow/Sources/Arrow/ArrowArray.swift b/swift/Arrow/Sources/Arrow/ArrowArray.swift index 1d41cc7e37feb..88b43e63a92b7 100644 --- a/swift/Arrow/Sources/Arrow/ArrowArray.swift +++ b/swift/Arrow/Sources/Arrow/ArrowArray.swift @@ -37,9 +37,9 @@ public class ArrowArrayHolder { buffer.append(to: &bufferData[bufferData.count - 1]) } - return bufferData; + return bufferData } - + self.getBufferDataSizes = {() -> [Int] in var bufferDataSizes = [Int]() for buffer in arrowArray.arrowData.buffers { @@ -48,7 +48,7 @@ public class ArrowArrayHolder { return bufferDataSizes } - + self.getArrowColumn = {(field: ArrowField, arrayHolders: [ArrowArrayHolder]) throws -> ArrowColumn in var arrays = [ArrowArray]() for arrayHolder in arrayHolders { @@ -56,12 +56,14 @@ public class ArrowArrayHolder { arrays.append(array) } } - + return ArrowColumn(field, chunked: ChunkedArrayHolder(try ChunkedArray(arrays))) } } - - public static func makeArrowColumn(_ field: ArrowField, holders: [ArrowArrayHolder]) -> Result { + + public static func makeArrowColumn(_ field: ArrowField, + holders: [ArrowArrayHolder] + ) -> Result { do { return .success(try holders[0].getArrowColumn(field, holders)) } catch { @@ -73,9 +75,9 @@ public class ArrowArrayHolder { public class ArrowArray: AsString { public typealias ItemType = T public let arrowData: ArrowData - public var nullCount : UInt {get{return self.arrowData.nullCount}} - public var length: UInt {get{return self.arrowData.length}} - + public var nullCount: UInt {return self.arrowData.nullCount} + public var length: UInt {return self.arrowData.length} + public required init(_ arrowData: ArrowData) { self.arrowData = arrowData } @@ -84,102 +86,92 @@ public class ArrowArray: AsString { if at >= self.length { throw ArrowError.outOfBounds(index: Int64(at)) } - + return self.arrowData.isNull(at) } public subscript(_ index: UInt) -> T? { - get { - fatalError("subscript() has not been implemented") - } + fatalError("subscript() has not been implemented") } - + public func asString(_ index: UInt) -> String { if self[index] == nil { return "" } - + return "\(self[index]!)" } } public class FixedArray: ArrowArray { public override subscript(_ index: UInt) -> T? { - get { - if self.arrowData.isNull(index) { - return nil - } - - let byteOffset = self.arrowData.stride * Int(index); - return self.arrowData.buffers[1].rawPointer.advanced(by: byteOffset).load(as: T.self) + if self.arrowData.isNull(index) { + return nil } + + let byteOffset = self.arrowData.stride * Int(index) + return self.arrowData.buffers[1].rawPointer.advanced(by: byteOffset).load(as: T.self) } } public class StringArray: ArrowArray { public override subscript(_ index: UInt) -> String? { - get { - let offsetIndex = MemoryLayout.stride * Int(index) - if self.arrowData.isNull(index) { - return nil - } - - let offsets = self.arrowData.buffers[1] - let values = self.arrowData.buffers[2] + let offsetIndex = MemoryLayout.stride * Int(index) + if self.arrowData.isNull(index) { + return nil + } - var startIndex: Int32 = 0 - if index > 0 { - startIndex = offsets.rawPointer.advanced(by: offsetIndex).load(as: Int32.self) - } + let offsets = self.arrowData.buffers[1] + let values = self.arrowData.buffers[2] - let endIndex = offsets.rawPointer.advanced(by: offsetIndex + MemoryLayout.stride ).load(as: Int32.self) - let arrayLength = Int(endIndex - startIndex); - let rawPointer = values.rawPointer.advanced(by: Int(startIndex)).bindMemory(to: UInt8.self, capacity: arrayLength) - let buffer = UnsafeBufferPointer(start: rawPointer, count: arrayLength); - let byteArray = Array(buffer) - return String(data: Data(byteArray), encoding: .utf8) + var startIndex: Int32 = 0 + if index > 0 { + startIndex = offsets.rawPointer.advanced(by: offsetIndex).load(as: Int32.self) } + + let endIndex = offsets.rawPointer.advanced(by: offsetIndex + MemoryLayout.stride ) + .load(as: Int32.self) + let arrayLength = Int(endIndex - startIndex) + let rawPointer = values.rawPointer.advanced(by: Int(startIndex)) + .bindMemory(to: UInt8.self, capacity: arrayLength) + let buffer = UnsafeBufferPointer(start: rawPointer, count: arrayLength) + let byteArray = Array(buffer) + return String(data: Data(byteArray), encoding: .utf8) } } public class BoolArray: ArrowArray { public override subscript(_ index: UInt) -> Bool? { - get { - if self.arrowData.isNull(index) { - return nil - } - - let valueBuffer = self.arrowData.buffers[1]; - return BitUtility.isSet(index, buffer: valueBuffer) + if self.arrowData.isNull(index) { + return nil } + + let valueBuffer = self.arrowData.buffers[1] + return BitUtility.isSet(index, buffer: valueBuffer) } } public class Date32Array: ArrowArray { public override subscript(_ index: UInt) -> Date? { - get { - if self.arrowData.isNull(index) { - return nil - } - - let byteOffset = self.arrowData.stride * Int(index); - let milliseconds = self.arrowData.buffers[1].rawPointer.advanced(by: byteOffset).load(as: UInt32.self) - return Date(timeIntervalSince1970: TimeInterval(milliseconds * 86400)) + if self.arrowData.isNull(index) { + return nil } + + let byteOffset = self.arrowData.stride * Int(index) + let milliseconds = self.arrowData.buffers[1].rawPointer.advanced(by: byteOffset).load(as: UInt32.self) + return Date(timeIntervalSince1970: TimeInterval(milliseconds * 86400)) } } public class Date64Array: ArrowArray { public override subscript(_ index: UInt) -> Date? { - get { - if self.arrowData.isNull(index) { - return nil - } - - let byteOffset = self.arrowData.stride * Int(index); - let milliseconds = self.arrowData.buffers[1].rawPointer.advanced(by: byteOffset).load(as: UInt64.self) - return Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000)) + if self.arrowData.isNull(index) { + return nil } + + let byteOffset = self.arrowData.stride * Int(index) + let milliseconds = self.arrowData.buffers[1].rawPointer.advanced(by: byteOffset).load(as: UInt64.self) + return Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000)) } } @@ -189,40 +181,39 @@ public class Time64Array: FixedArray {} public class BinaryArray: ArrowArray { public struct Options { public var printAsHex = false - public var printEncoding: String.Encoding = .utf8; + public var printEncoding: String.Encoding = .utf8 } - + public var options = Options() - - public override subscript(_ index: UInt) -> Data? { - get { - let offsetIndex = MemoryLayout.stride * Int(index) - if self.arrowData.isNull(index) { - return nil - } - - let offsets = self.arrowData.buffers[1] - let values = self.arrowData.buffers[2] - var startIndex: Int32 = 0 - if index > 0 { - startIndex = offsets.rawPointer.advanced(by: offsetIndex).load(as: Int32.self) - } + public override subscript(_ index: UInt) -> Data? { + let offsetIndex = MemoryLayout.stride * Int(index) + if self.arrowData.isNull(index) { + return nil + } - let endIndex = offsets.rawPointer.advanced(by: offsetIndex + MemoryLayout.stride ).load(as: Int32.self) - let arrayLength = Int(endIndex - startIndex); - let rawPointer = values.rawPointer.advanced(by: Int(startIndex)).bindMemory(to: UInt8.self, capacity: arrayLength) - let buffer = UnsafeBufferPointer(start: rawPointer, count: arrayLength); - let byteArray = Array(buffer) - return Data(byteArray) + let offsets = self.arrowData.buffers[1] + let values = self.arrowData.buffers[2] + var startIndex: Int32 = 0 + if index > 0 { + startIndex = offsets.rawPointer.advanced(by: offsetIndex).load(as: Int32.self) } + + let endIndex = offsets.rawPointer.advanced(by: offsetIndex + MemoryLayout.stride ) + .load(as: Int32.self) + let arrayLength = Int(endIndex - startIndex) + let rawPointer = values.rawPointer.advanced(by: Int(startIndex)) + .bindMemory(to: UInt8.self, capacity: arrayLength) + let buffer = UnsafeBufferPointer(start: rawPointer, count: arrayLength) + let byteArray = Array(buffer) + return Data(byteArray) } - + public override func asString(_ index: UInt) -> String { if self[index] == nil { return "" } - + let data = self[index]! if options.printAsHex { return data.hexEncodedString() diff --git a/swift/Arrow/Sources/Arrow/ArrowArrayBuilder.swift b/swift/Arrow/Sources/Arrow/ArrowArrayBuilder.swift index 049b58bc33425..32728dc7eeaa4 100644 --- a/swift/Arrow/Sources/Arrow/ArrowArrayBuilder.swift +++ b/swift/Arrow/Sources/Arrow/ArrowArrayBuilder.swift @@ -20,13 +20,13 @@ import Foundation public class ArrowArrayBuilder> { let type: ArrowType let bufferBuilder: T - public var length: UInt {get{return self.bufferBuilder.length}} - public var capacity: UInt {get{return self.bufferBuilder.capacity}} - public var nullCount : UInt {get{return self.bufferBuilder.nullCount}} - public var offset: UInt {get{return self.bufferBuilder.offset}} - + public var length: UInt {return self.bufferBuilder.length} + public var capacity: UInt {return self.bufferBuilder.capacity} + public var nullCount: UInt {return self.bufferBuilder.nullCount} + public var offset: UInt {return self.bufferBuilder.offset} + fileprivate init(_ type: ArrowType) throws { - self.type = type; + self.type = type self.bufferBuilder = try T() } @@ -35,51 +35,51 @@ public class ArrowArrayBuilder> } public func finish() throws -> ArrowArray { - let buffers = self.bufferBuilder.finish(); + let buffers = self.bufferBuilder.finish() let arrowData = try ArrowData(self.type, buffers: buffers, nullCount: self.nullCount, stride: self.getStride()) return U(arrowData) } - + public func getStride() -> Int { MemoryLayout.stride } } -public class NumberArrayBuilder : ArrowArrayBuilder, FixedArray> { +public class NumberArrayBuilder: ArrowArrayBuilder, FixedArray> { fileprivate convenience init() throws { try self.init(ArrowType(ArrowType.infoForNumericType(T.self))) } } -public class StringArrayBuilder : ArrowArrayBuilder, StringArray> { +public class StringArrayBuilder: ArrowArrayBuilder, StringArray> { fileprivate convenience init() throws { try self.init(ArrowType(ArrowType.ArrowString)) } } -public class BinaryArrayBuilder : ArrowArrayBuilder, BinaryArray> { +public class BinaryArrayBuilder: ArrowArrayBuilder, BinaryArray> { fileprivate convenience init() throws { try self.init(ArrowType(ArrowType.ArrowBinary)) } } -public class BoolArrayBuilder : ArrowArrayBuilder { +public class BoolArrayBuilder: ArrowArrayBuilder { fileprivate convenience init() throws { try self.init(ArrowType(ArrowType.ArrowBool)) } } -public class Date32ArrayBuilder : ArrowArrayBuilder { +public class Date32ArrayBuilder: ArrowArrayBuilder { fileprivate convenience init() throws { try self.init(ArrowType(ArrowType.ArrowDate32)) } - + public override func getStride() -> Int { MemoryLayout.stride } } -public class Date64ArrayBuilder : ArrowArrayBuilder { +public class Date64ArrayBuilder: ArrowArrayBuilder { fileprivate convenience init() throws { try self.init(ArrowType(ArrowType.ArrowDate64)) } @@ -89,13 +89,13 @@ public class Date64ArrayBuilder : ArrowArrayBuilder, Time32Array> { +public class Time32ArrayBuilder: ArrowArrayBuilder, Time32Array> { fileprivate convenience init(_ unit: ArrowTime32Unit) throws { try self.init(ArrowTypeTime32(unit)) } } -public class Time64ArrayBuilder : ArrowArrayBuilder, Time64Array> { +public class Time64ArrayBuilder: ArrowArrayBuilder, Time64Array> { fileprivate convenience init(_ unit: ArrowTime64Unit) throws { try self.init(ArrowTypeTime64(unit)) } @@ -103,26 +103,26 @@ public class Time64ArrayBuilder : ArrowArrayBuilder, public class ArrowArrayBuilders { public static func loadNumberArrayBuilder() throws -> NumberArrayBuilder { - let t = T.self - if t == Int8.self { + let type = T.self + if type == Int8.self { return try NumberArrayBuilder() - } else if t == Int16.self { + } else if type == Int16.self { return try NumberArrayBuilder() - } else if t == Int32.self { + } else if type == Int32.self { return try NumberArrayBuilder() - } else if t == Int64.self { + } else if type == Int64.self { return try NumberArrayBuilder() - } else if t == UInt8.self { + } else if type == UInt8.self { return try NumberArrayBuilder() - } else if t == UInt16.self { + } else if type == UInt16.self { return try NumberArrayBuilder() - } else if t == UInt32.self { + } else if type == UInt32.self { return try NumberArrayBuilder() - } else if t == UInt64.self { + } else if type == UInt64.self { return try NumberArrayBuilder() - } else if t == Float.self { + } else if type == Float.self { return try NumberArrayBuilder() - } else if t == Double.self { + } else if type == Double.self { return try NumberArrayBuilder() } else { throw ArrowError.unknownType("Type is invalid for NumberArrayBuilder") @@ -144,7 +144,7 @@ public class ArrowArrayBuilders { public static func loadDate64ArrayBuilder() throws -> Date64ArrayBuilder { return try Date64ArrayBuilder() } - + public static func loadBinaryArrayBuilder() throws -> BinaryArrayBuilder { return try BinaryArrayBuilder() } diff --git a/swift/Arrow/Sources/Arrow/ArrowBuffer.swift b/swift/Arrow/Sources/Arrow/ArrowBuffer.swift index d56279bcf8657..4ac4eb93c91db 100644 --- a/swift/Arrow/Sources/Arrow/ArrowBuffer.swift +++ b/swift/Arrow/Sources/Arrow/ArrowBuffer.swift @@ -18,8 +18,8 @@ import Foundation public class ArrowBuffer { - static let min_length: UInt = 1 << 5 - static let max_length = UInt.max + static let minLength: UInt = 1 << 5 + static let maxLength = UInt.max fileprivate(set) var length: UInt let capacity: UInt let rawPointer: UnsafeMutableRawPointer @@ -36,7 +36,7 @@ public class ArrowBuffer { func append(to data: inout Data) { let ptr = UnsafePointer(rawPointer.assumingMemoryBound(to: UInt8.self)) - data.append(ptr, count: Int(capacity)); + data.append(ptr, count: Int(capacity)) } static func createBuffer(_ data: [UInt8], length: UInt) -> ArrowBuffer { @@ -49,13 +49,13 @@ public class ArrowBuffer { } static func createBuffer(_ length: UInt, size: UInt, doAlign: Bool = true) -> ArrowBuffer { - let actualLen = max(length, ArrowBuffer.min_length) + let actualLen = max(length, ArrowBuffer.minLength) let byteCount = size * actualLen var capacity = byteCount if doAlign { capacity = alignTo64(byteCount) } - + let memory = MemoryAllocator(64) let rawPointer = memory.allocateArray(Int(capacity)) rawPointer.initializeMemory(as: UInt8.self, repeating: 0, count: Int(capacity)) @@ -64,14 +64,14 @@ public class ArrowBuffer { static func copyCurrent(_ from: ArrowBuffer, to: inout ArrowBuffer, len: UInt) { to.rawPointer.copyMemory(from: from.rawPointer, byteCount: Int(len)) - } + } private static func alignTo64(_ length: UInt) -> UInt { - let buf_alignment = length % 64; - if buf_alignment != 0 { - return length + (64 - buf_alignment) + 8; + let bufAlignment = length % 64 + if bufAlignment != 0 { + return length + (64 - bufAlignment) + 8 } - return length + 8; + return length + 8 } } diff --git a/swift/Arrow/Sources/Arrow/ArrowBufferBuilder.swift b/swift/Arrow/Sources/Arrow/ArrowBufferBuilder.swift index 39b0434c3cdc7..e4c8036c327d1 100644 --- a/swift/Arrow/Sources/Arrow/ArrowBufferBuilder.swift +++ b/swift/Arrow/Sources/Arrow/ArrowBufferBuilder.swift @@ -21,7 +21,7 @@ public protocol ArrowBufferBuilder { associatedtype ItemType var capacity: UInt {get} var length: UInt {get} - var nullCount : UInt {get} + var nullCount: UInt {get} var offset: UInt {get} init() throws func append(_ newValue: ItemType?) @@ -33,11 +33,11 @@ public protocol ArrowBufferBuilder { public class BaseBufferBuilder { var values: ArrowBuffer var nulls: ArrowBuffer - var stride: Int; + var stride: Int public var offset: UInt = 0 - public var capacity: UInt {get{return self.values.capacity}} + public var capacity: UInt {return self.values.capacity} public var length: UInt = 0 - public var nullCount : UInt = 0 + public var nullCount: UInt = 0 init(values: ArrowBuffer, nulls: ArrowBuffer, stride: Int = MemoryLayout.stride) { self.stride = stride @@ -51,13 +51,13 @@ public class BaseBufferBuilder { func resizeLength(_ data: ArrowBuffer, len: UInt = 0) -> UInt { if len == 0 || len < data.length * 2 { - if data.length == 0 || data.length * 2 < ArrowBuffer.min_length { - return ArrowBuffer.min_length + if data.length == 0 || data.length * 2 < ArrowBuffer.minLength { + return ArrowBuffer.minLength } - return UInt(data.length * 2); + return UInt(data.length * 2) } - - return UInt(len * 2); + + return UInt(len * 2) } } @@ -91,13 +91,13 @@ public class FixedBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder { public func resize(_ length: UInt) { if length > self.values.length { - let resizeLength = resizeLength(self.values); + let resizeLength = resizeLength(self.values) var values = ArrowBuffer.createBuffer(resizeLength, size: UInt(MemoryLayout.size)) var nulls = ArrowBuffer.createBuffer(resizeLength/8 + 1, size: UInt(MemoryLayout.size)) ArrowBuffer.copyCurrent(self.values, to: &values, len: self.values.capacity) ArrowBuffer.copyCurrent(self.nulls, to: &nulls, len: self.nulls.capacity) - self.values = values; - self.nulls = nulls; + self.values = values + self.nulls = nulls } } @@ -109,31 +109,31 @@ public class FixedBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder { ArrowBuffer.copyCurrent(self.nulls, to: &nulls, len: nulls.capacity) return [nulls, values] } - + fileprivate static func defaultValueForType() throws -> T { - let t = T.self - if t == Int8.self { - return Int8(0) as! T - }else if t == Int16.self { - return Int16(0) as! T - }else if t == Int32.self { - return Int32(0) as! T - }else if t == Int64.self { - return Int64(0) as! T - }else if t == UInt8.self { - return UInt8(0) as! T - }else if t == UInt16.self { - return UInt16(0) as! T - }else if t == UInt32.self { - return UInt32(0) as! T - }else if t == UInt64.self { - return UInt64(0) as! T - }else if t == Float.self { - return Float(0) as! T - }else if t == Double.self { - return Double(0) as! T + let type = T.self + if type == Int8.self { + return Int8(0) as! T // swiftlint:disable:this force_cast + } else if type == Int16.self { + return Int16(0) as! T // swiftlint:disable:this force_cast + } else if type == Int32.self { + return Int32(0) as! T // swiftlint:disable:this force_cast + } else if type == Int64.self { + return Int64(0) as! T // swiftlint:disable:this force_cast + } else if type == UInt8.self { + return UInt8(0) as! T // swiftlint:disable:this force_cast + } else if type == UInt16.self { + return UInt16(0) as! T // swiftlint:disable:this force_cast + } else if type == UInt32.self { + return UInt32(0) as! T // swiftlint:disable:this force_cast + } else if type == UInt64.self { + return UInt64(0) as! T // swiftlint:disable:this force_cast + } else if type == Float.self { + return Float(0) as! T // swiftlint:disable:this force_cast + } else if type == Double.self { + return Double(0) as! T // swiftlint:disable:this force_cast } - + throw ArrowError.unknownType("Unable to determine default value") } } @@ -160,7 +160,7 @@ public class BoolBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder { } else { BitUtility.clearBit(index + self.offset, buffer: self.values) } - + } else { self.nullCount += 1 BitUtility.clearBit(index + self.offset, buffer: self.nulls) @@ -175,8 +175,8 @@ public class BoolBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder { var nulls = ArrowBuffer.createBuffer(resizeLength, size: UInt(MemoryLayout.size)) ArrowBuffer.copyCurrent(self.values, to: &values, len: self.values.capacity) ArrowBuffer.copyCurrent(self.nulls, to: &nulls, len: self.nulls.capacity) - self.values = values; - self.nulls = nulls; + self.values = values + self.nulls = nulls } } @@ -191,7 +191,7 @@ public class BoolBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder { } public class VariableBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder { - public typealias ItemType = T + public typealias ItemType = T var offsets: ArrowBuffer let binaryStride = MemoryLayout.stride public required init() throws { @@ -212,14 +212,14 @@ public class VariableBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder var isNull = false if let val = newValue { binData = getBytesFor(val)! - }else { + } else { var nullVal = 0 isNull = true binData = Data(bytes: &nullVal, count: MemoryLayout.size) } - var currentIndex: Int32 = 0; - var currentOffset: Int32 = Int32(binData.count); + var currentIndex: Int32 = 0 + var currentOffset: Int32 = Int32(binData.count) if index > 0 { currentIndex = self.offsets.rawPointer.advanced(by: offsetIndex).load(as: Int32.self) currentOffset += currentIndex @@ -237,30 +237,32 @@ public class VariableBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder binData.withUnsafeBytes { bufferPointer in let rawPointer = bufferPointer.baseAddress! - self.values.rawPointer.advanced(by: Int(currentIndex)).copyMemory(from: rawPointer, byteCount: binData.count) + self.values.rawPointer.advanced(by: Int(currentIndex)) + .copyMemory(from: rawPointer, byteCount: binData.count) } - self.offsets.rawPointer.advanced(by: (offsetIndex + MemoryLayout.stride)).storeBytes(of: currentOffset, as: Int32.self) + self.offsets.rawPointer.advanced(by: (offsetIndex + MemoryLayout.stride)) + .storeBytes(of: currentOffset, as: Int32.self) } public func value_resize(_ length: UInt) { if length > self.values.length { - let resizeLength = resizeLength(self.values, len: length); + let resizeLength = resizeLength(self.values, len: length) var values = ArrowBuffer.createBuffer(resizeLength, size: UInt(MemoryLayout.size)) ArrowBuffer.copyCurrent(self.values, to: &values, len: self.values.capacity) - self.values = values; + self.values = values } } - + public func resize(_ length: UInt) { if length > self.offsets.length { - let resizeLength = resizeLength(self.offsets, len: length); + let resizeLength = resizeLength(self.offsets, len: length) var nulls = ArrowBuffer.createBuffer(resizeLength/8 + 1, size: UInt(MemoryLayout.size)) var offsets = ArrowBuffer.createBuffer(resizeLength, size: UInt(MemoryLayout.size)) ArrowBuffer.copyCurrent(self.nulls, to: &nulls, len: self.nulls.capacity) ArrowBuffer.copyCurrent(self.offsets, to: &offsets, len: self.offsets.capacity) - self.nulls = nulls; - self.offsets = offsets; + self.nulls = nulls + self.offsets = offsets } } @@ -278,10 +280,10 @@ public class VariableBufferBuilder: BaseBufferBuilder, ArrowBufferBuilder public class AbstractWrapperBufferBuilder: ArrowBufferBuilder { public typealias ItemType = T - public var capacity: UInt {get{return self.bufferBuilder.capacity}} - public var length: UInt {get{return self.bufferBuilder.length}} - public var nullCount : UInt {get{return self.bufferBuilder.nullCount}} - public var offset: UInt {get{return self.bufferBuilder.offset}} + public var capacity: UInt {return self.bufferBuilder.capacity} + public var length: UInt {return self.bufferBuilder.length} + public var nullCount: UInt {return self.bufferBuilder.nullCount} + public var offset: UInt {return self.bufferBuilder.offset} let bufferBuilder: FixedBufferBuilder public required init() throws { self.bufferBuilder = try FixedBufferBuilder() @@ -294,18 +296,17 @@ public class AbstractWrapperBufferBuilder: ArrowBufferBuilder { public func isNull(_ index: UInt) -> Bool { return self.bufferBuilder.isNull(index) } - + public func resize(_ length: UInt) { self.bufferBuilder.resize(length) } - + public func finish() -> [ArrowBuffer] { return self.bufferBuilder.finish() } } - -public class Date32BufferBuilder: AbstractWrapperBufferBuilder { +public class Date32BufferBuilder: AbstractWrapperBufferBuilder { public override func append(_ newValue: ItemType?) { if let val = newValue { let daysSinceEpoch = Int32(val.timeIntervalSince1970 / 86400) @@ -316,7 +317,7 @@ public class Date32BufferBuilder: AbstractWrapperBufferBuilder { } } -public class Date64BufferBuilder: AbstractWrapperBufferBuilder { +public class Date64BufferBuilder: AbstractWrapperBufferBuilder { public override func append(_ newValue: ItemType?) { if let val = newValue { let daysSinceEpoch = Int64(val.timeIntervalSince1970 * 1000) diff --git a/swift/Arrow/Sources/Arrow/ArrowData.swift b/swift/Arrow/Sources/Arrow/ArrowData.swift index 4d50ef75c3ce7..60281a8d24133 100644 --- a/swift/Arrow/Sources/Arrow/ArrowData.swift +++ b/swift/Arrow/Sources/Arrow/ArrowData.swift @@ -26,17 +26,17 @@ public class ArrowData { init(_ arrowType: ArrowType, buffers: [ArrowBuffer], nullCount: UInt, stride: Int) throws { let infoType = arrowType.info - switch(infoType) { - case let .PrimitiveInfo(typeId): - if typeId == ArrowTypeId.Unknown { + switch infoType { + case let .primitiveInfo(typeId): + if typeId == ArrowTypeId.unknown { throw ArrowError.unknownType("Unknown primitive type for data") } - case let .VariableInfo(typeId): - if typeId == ArrowTypeId.Unknown { + case let .variableInfo(typeId): + if typeId == ArrowTypeId.unknown { throw ArrowError.unknownType("Unknown variable type for data") } - case let .TimeInfo(typeId): - if typeId == ArrowTypeId.Unknown { + case let .timeInfo(typeId): + if typeId == ArrowTypeId.unknown { throw ArrowError.unknownType("Unknown time type for data") } } @@ -49,7 +49,7 @@ public class ArrowData { } public func isNull(_ at: UInt) -> Bool { - let nullBuffer = buffers[0]; + let nullBuffer = buffers[0] return nullBuffer.length > 0 && !BitUtility.isSet(at, buffer: nullBuffer) } } diff --git a/swift/Arrow/Sources/Arrow/ArrowReader.swift b/swift/Arrow/Sources/Arrow/ArrowReader.swift index b4c8c7227659a..68096133451ba 100644 --- a/swift/Arrow/Sources/Arrow/ArrowReader.swift +++ b/swift/Arrow/Sources/Arrow/ArrowReader.swift @@ -35,9 +35,9 @@ public class ArrowReader { public var schema: ArrowSchema? public var batches = [RecordBatch]() } - + public init() {} - + private func loadSchema(_ schema: org_apache_arrow_flatbuf_Schema) -> Result { let builder = ArrowSchema.Builder() for index in 0 ..< schema.fieldsCount { @@ -95,8 +95,12 @@ public class ArrowReader { } } - private func loadRecordBatch(_ message: org_apache_arrow_flatbuf_Message, schema: org_apache_arrow_flatbuf_Schema, - arrowSchema: ArrowSchema, data: Data, messageEndOffset: Int64) -> Result { + private func loadRecordBatch(_ message: org_apache_arrow_flatbuf_Message, + schema: org_apache_arrow_flatbuf_Schema, + arrowSchema: ArrowSchema, + data: Data, + messageEndOffset: Int64 + ) -> Result { let recordBatch = message.header(type: org_apache_arrow_flatbuf_RecordBatch.self) let nodesCount = recordBatch?.nodesCount ?? 0 var bufferIndex: Int32 = 0 @@ -114,16 +118,15 @@ public class ArrowReader { result = loadVariableData(loadInfo) bufferIndex += 3 } - + switch result { case .success(let holder): columns.append(holder) case .failure(let error): return .failure(error) } - } - + return .success(RecordBatch(arrowSchema, columns: columns)) } diff --git a/swift/Arrow/Sources/Arrow/ArrowReaderHelper.swift b/swift/Arrow/Sources/Arrow/ArrowReaderHelper.swift index ca635723bca8b..2e5a12c6c08f3 100644 --- a/swift/Arrow/Sources/Arrow/ArrowReaderHelper.swift +++ b/swift/Arrow/Sources/Arrow/ArrowReaderHelper.swift @@ -18,7 +18,7 @@ import FlatBuffers import Foundation -fileprivate func makeBinaryHolder(_ buffers: [ArrowBuffer]) -> Result { +private func makeBinaryHolder(_ buffers: [ArrowBuffer]) -> Result { do { let arrowData = try ArrowData(ArrowType(ArrowType.ArrowBinary), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) @@ -30,7 +30,7 @@ fileprivate func makeBinaryHolder(_ buffers: [ArrowBuffer]) -> Result Result { +private func makeStringHolder(_ buffers: [ArrowBuffer]) -> Result { do { let arrowData = try ArrowData(ArrowType(ArrowType.ArrowString), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) @@ -42,7 +42,9 @@ fileprivate func makeStringHolder(_ buffers: [ArrowBuffer]) -> Result Result { +private func makeFloatHolder(_ floatType: org_apache_arrow_flatbuf_FloatingPoint, + buffers: [ArrowBuffer] +) -> Result { switch floatType.precision { case .single: return makeFixedHolder(Float.self, buffers: buffers) @@ -53,14 +55,16 @@ fileprivate func makeFloatHolder(_ floatType: org_apache_arrow_flatbuf_FloatingP } } -fileprivate func makeDateHolder(_ dateType: org_apache_arrow_flatbuf_Date, buffers: [ArrowBuffer]) -> Result { - do { +private func makeDateHolder(_ dateType: org_apache_arrow_flatbuf_Date, + buffers: [ArrowBuffer] +) -> Result { + do { if dateType.unit == .day { let arrowData = try ArrowData(ArrowType(ArrowType.ArrowString), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) return .success(ArrowArrayHolder(Date32Array(arrowData))) } - + let arrowData = try ArrowData(ArrowType(ArrowType.ArrowString), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) return .success(ArrowArrayHolder(Date64Array(arrowData))) @@ -71,17 +75,18 @@ fileprivate func makeDateHolder(_ dateType: org_apache_arrow_flatbuf_Date, buffe } } -fileprivate func makeTimeHolder(_ timeType: org_apache_arrow_flatbuf_Time, buffers: [ArrowBuffer]) -> Result { - do { +private func makeTimeHolder(_ timeType: org_apache_arrow_flatbuf_Time, + buffers: [ArrowBuffer] +) -> Result { + do { if timeType.unit == .second || timeType.unit == .millisecond { - let arrowUnit: ArrowTime32Unit = timeType.unit == .second ? .Seconds : .Milliseconds + let arrowUnit: ArrowTime32Unit = timeType.unit == .second ? .seconds : .milliseconds let arrowData = try ArrowData(ArrowTypeTime32(arrowUnit), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) - return .success(ArrowArrayHolder(FixedArray(arrowData))) } - - let arrowUnit: ArrowTime64Unit = timeType.unit == .microsecond ? .Microseconds : .Nanoseconds + + let arrowUnit: ArrowTime64Unit = timeType.unit == .microsecond ? .microseconds : .nanoseconds let arrowData = try ArrowData(ArrowTypeTime64(arrowUnit), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) return .success(ArrowArrayHolder(FixedArray(arrowData))) @@ -92,7 +97,7 @@ fileprivate func makeTimeHolder(_ timeType: org_apache_arrow_flatbuf_Time, buffe } } -fileprivate func makeBoolHolder(_ buffers: [ArrowBuffer]) -> Result { +private func makeBoolHolder(_ buffers: [ArrowBuffer]) -> Result { do { let arrowData = try ArrowData(ArrowType(ArrowType.ArrowInt32), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) @@ -104,7 +109,7 @@ fileprivate func makeBoolHolder(_ buffers: [ArrowBuffer]) -> Result(_: T.Type, buffers: [ArrowBuffer]) -> Result { +private func makeFixedHolder(_: T.Type, buffers: [ArrowBuffer]) -> Result { do { let arrowData = try ArrowData(ArrowType(ArrowType.ArrowInt32), buffers: buffers, nullCount: buffers[0].length, stride: MemoryLayout.stride) @@ -116,7 +121,10 @@ fileprivate func makeFixedHolder(_: T.Type, buffers: [ArrowBuffer]) -> Result } } -func makeArrayHolder(_ field: org_apache_arrow_flatbuf_Field, buffers: [ArrowBuffer]) -> Result { +func makeArrayHolder( // swiftlint:disable:this cyclomatic_complexity + _ field: org_apache_arrow_flatbuf_Field, + buffers: [ArrowBuffer] +) -> Result { let type = field.typeType switch type { case .int: @@ -185,7 +193,8 @@ func isFixedPrimitive(_ type: org_apache_arrow_flatbuf_Type_) -> Bool { } } -func findArrowType(_ field: org_apache_arrow_flatbuf_Field) -> ArrowType { +func findArrowType( // swiftlint:disable:this cyclomatic_complexity + _ field: org_apache_arrow_flatbuf_Field) -> ArrowType { let type = field.typeType switch type { case .int: @@ -217,15 +226,15 @@ func findArrowType(_ field: org_apache_arrow_flatbuf_Field) -> ArrowType { if dateType.unit == .day { return ArrowType(ArrowType.ArrowDate32) } - + return ArrowType(ArrowType.ArrowDate64) case .time: let timeType = field.type(type: org_apache_arrow_flatbuf_Time.self)! if timeType.unit == .second || timeType.unit == .millisecond { - return ArrowTypeTime32(timeType.unit == .second ? .Seconds : .Milliseconds) + return ArrowTypeTime32(timeType.unit == .second ? .seconds : .milliseconds) } - - return ArrowTypeTime64(timeType.unit == .microsecond ? .Microseconds : .Nanoseconds) + + return ArrowTypeTime64(timeType.unit == .microsecond ? .microseconds : .nanoseconds) default: return ArrowType(ArrowType.ArrowUnknown) } diff --git a/swift/Arrow/Sources/Arrow/ArrowSchema.swift b/swift/Arrow/Sources/Arrow/ArrowSchema.swift index 882e66a24777e..45f13a1551c3d 100644 --- a/swift/Arrow/Sources/Arrow/ArrowSchema.swift +++ b/swift/Arrow/Sources/Arrow/ArrowSchema.swift @@ -20,7 +20,7 @@ public class ArrowField { let type: ArrowType let name: String let isNullable: Bool - + init(_ name: String, type: ArrowType, isNullable: Bool) { self.name = name self.type = type @@ -34,13 +34,13 @@ public class ArrowSchema { init(_ fields: [ArrowField]) { var fieldLookup = [String: Int]() for (index, field) in fields.enumerated() { - fieldLookup[field.name] = index; + fieldLookup[field.name] = index } - + self.fields = fields self.fieldLookup = fieldLookup } - + public func field(_ index: Int) -> ArrowField { return self.fields[index] } @@ -51,9 +51,9 @@ public class ArrowSchema { public class Builder { private var fields: [ArrowField] = [] - + public init() {} - + @discardableResult public func addField(_ field: ArrowField) -> Builder { fields.append(field) @@ -65,7 +65,7 @@ public class ArrowSchema { fields.append(ArrowField(name, type: type, isNullable: isNullable)) return self } - + public func finish() -> ArrowSchema { return ArrowSchema(fields) } diff --git a/swift/Arrow/Sources/Arrow/ArrowTable.swift b/swift/Arrow/Sources/Arrow/ArrowTable.swift index a288286a824fb..7677fb4f33a19 100644 --- a/swift/Arrow/Sources/Arrow/ArrowTable.swift +++ b/swift/Arrow/Sources/Arrow/ArrowTable.swift @@ -20,15 +20,15 @@ import Foundation public class ArrowColumn { public let field: ArrowField fileprivate let dataHolder: ChunkedArrayHolder - public var type: ArrowType {get{return self.dataHolder.type}} - public var length: UInt {get{return self.dataHolder.length}} - public var nullCount: UInt {get{return self.dataHolder.nullCount}} + public var type: ArrowType {return self.dataHolder.type} + public var length: UInt {return self.dataHolder.length} + public var nullCount: UInt {return self.dataHolder.nullCount} public func data() -> ChunkedArray { - return (self.dataHolder.holder as! ChunkedArray) + return (self.dataHolder.holder as! ChunkedArray) // swiftlint:disable:this force_cast } - public var name: String {get{return field.name}} + public var name: String {return field.name} public init(_ field: ArrowField, chunked: ChunkedArrayHolder) { self.field = field self.dataHolder = chunked @@ -37,7 +37,7 @@ public class ArrowColumn { public class ArrowTable { public let schema: ArrowSchema - public var columnCount: UInt {get{return UInt(self.columns.count)}} + public var columnCount: UInt {return UInt(self.columns.count)} public let rowCount: UInt public let columns: [ArrowColumn] init(_ schema: ArrowSchema, columns: [ArrowColumn]) { @@ -45,14 +45,14 @@ public class ArrowTable { self.columns = columns self.rowCount = columns[0].length } - + public static func from(recordBatches: [RecordBatch]) -> Result { - if(recordBatches.isEmpty) { + if recordBatches.isEmpty { return .failure(.arrayHasNoElements) } - + var holders = [[ArrowArrayHolder]]() - let schema = recordBatches[0].schema; + let schema = recordBatches[0].schema for recordBatch in recordBatches { for index in 0..(_ fieldName: String, arrowArray: ArrowArray) throws -> Builder { return self.addColumn(fieldName, chunked: try ChunkedArray([arrowArray])) @@ -115,7 +115,7 @@ public class ArrowTable { self.columns.append(column) return self } - + public func finish() -> ArrowTable { return ArrowTable(self.schemaBuilder.finish(), columns: self.columns) } @@ -124,7 +124,7 @@ public class ArrowTable { public class RecordBatch { public let schema: ArrowSchema - public var columnCount: UInt {get{return UInt(self.columns.count)}} + public var columnCount: UInt {return UInt(self.columns.count)} public let columns: [ArrowArrayHolder] public let length: UInt public init(_ schema: ArrowSchema, columns: [ArrowArrayHolder]) { @@ -132,13 +132,13 @@ public class RecordBatch { self.columns = columns self.length = columns[0].length } - + public class Builder { let schemaBuilder = ArrowSchema.Builder() var columns = [ArrowArrayHolder]() - + public init() {} - + @discardableResult public func addColumn(_ fieldName: String, arrowArray: ArrowArrayHolder) -> Builder { let field = ArrowField(fieldName, type: arrowArray.type, isNullable: arrowArray.nullCount != 0) @@ -158,7 +158,7 @@ public class RecordBatch { if columns.count > 0 { let columnLength = columns[0].length for column in columns { - if column.length != columnLength { + if column.length != columnLength { // swiftlint:disable:this for_where return .failure(.runtimeError("Columns have different sizes")) } } @@ -166,12 +166,12 @@ public class RecordBatch { return .success(RecordBatch(self.schemaBuilder.finish(), columns: self.columns)) } } - + public func data(for columnIndex: Int) -> ArrowArray { let arrayHolder = column(columnIndex) - return (arrayHolder.array as! ArrowArray) + return (arrayHolder.array as! ArrowArray) // swiftlint:disable:this force_cast } - + public func column(_ index: Int) -> ArrowArrayHolder { return self.columns[index] } @@ -180,9 +180,7 @@ public class RecordBatch { if let index = self.schema.fieldIndex(name) { return self.columns[index] } - - return nil; - } - + return nil + } } diff --git a/swift/Arrow/Sources/Arrow/ArrowType.swift b/swift/Arrow/Sources/Arrow/ArrowType.swift index cf9d7f4dba399..e1c1ef271cc0f 100644 --- a/swift/Arrow/Sources/Arrow/ArrowType.swift +++ b/swift/Arrow/Sources/Arrow/ArrowType.swift @@ -20,9 +20,9 @@ import Foundation public typealias Time32 = Int32 public typealias Time64 = Int64 -func FlatBuffersVersion_23_1_4() { +func FlatBuffersVersion_23_1_4() { // swiftlint:disable:this identifier_name } - + public enum ArrowError: Error { case none case unknownType(String) @@ -36,50 +36,50 @@ public enum ArrowError: Error { } public enum ArrowTypeId { - case Binary - case Boolean - case Date32 - case Date64 - case DateType - case Decimal128 - case Decimal256 - case Dictionary - case Double - case FixedSizeBinary - case FixedWidthType - case Float - //case HalfFloatType - case Int16 - case Int32 - case Int64 - case Int8 - case Integer - case IntervalUnit - case List - case Nested - case Null - case Number - case String - case Struct - case Time32 - case Time64 - case Time - case UInt16 - case UInt32 - case UInt64 - case UInt8 - case Union - case Unknown + case binary + case boolean + case date32 + case date64 + case dateType + case decimal128 + case decimal256 + case dictionary + case double + case fixedSizeBinary + case fixedWidthType + case float + // case HalfFloatType + case int16 + case int32 + case int64 + case int8 + case integer + case intervalUnit + case list + case nested + case null + case number + case string + case strct + case time32 + case time64 + case time + case uint16 + case uint32 + case uint64 + case uint8 + case union + case unknown } public enum ArrowTime32Unit { - case Seconds - case Milliseconds + case seconds + case milliseconds } public enum ArrowTime64Unit { - case Microseconds - case Nanoseconds + case microseconds + case nanoseconds } public class ArrowTypeTime32: ArrowType { @@ -100,70 +100,70 @@ public class ArrowTypeTime64: ArrowType { public class ArrowType { public private(set) var info: ArrowType.Info - public static let ArrowInt8 = Info.PrimitiveInfo(ArrowTypeId.Int8) - public static let ArrowInt16 = Info.PrimitiveInfo(ArrowTypeId.Int16) - public static let ArrowInt32 = Info.PrimitiveInfo(ArrowTypeId.Int32) - public static let ArrowInt64 = Info.PrimitiveInfo(ArrowTypeId.Int64) - public static let ArrowUInt8 = Info.PrimitiveInfo(ArrowTypeId.UInt8) - public static let ArrowUInt16 = Info.PrimitiveInfo(ArrowTypeId.UInt16) - public static let ArrowUInt32 = Info.PrimitiveInfo(ArrowTypeId.UInt32) - public static let ArrowUInt64 = Info.PrimitiveInfo(ArrowTypeId.UInt64) - public static let ArrowFloat = Info.PrimitiveInfo(ArrowTypeId.Float) - public static let ArrowDouble = Info.PrimitiveInfo(ArrowTypeId.Double) - public static let ArrowUnknown = Info.PrimitiveInfo(ArrowTypeId.Unknown) - public static let ArrowString = Info.VariableInfo(ArrowTypeId.String) - public static let ArrowBool = Info.PrimitiveInfo(ArrowTypeId.Boolean) - public static let ArrowDate32 = Info.PrimitiveInfo(ArrowTypeId.Date32) - public static let ArrowDate64 = Info.PrimitiveInfo(ArrowTypeId.Date64) - public static let ArrowBinary = Info.VariableInfo(ArrowTypeId.Binary) - public static let ArrowTime32 = Info.TimeInfo(ArrowTypeId.Time32) - public static let ArrowTime64 = Info.TimeInfo(ArrowTypeId.Time64) + public static let ArrowInt8 = Info.primitiveInfo(ArrowTypeId.int8) + public static let ArrowInt16 = Info.primitiveInfo(ArrowTypeId.int16) + public static let ArrowInt32 = Info.primitiveInfo(ArrowTypeId.int32) + public static let ArrowInt64 = Info.primitiveInfo(ArrowTypeId.int64) + public static let ArrowUInt8 = Info.primitiveInfo(ArrowTypeId.uint8) + public static let ArrowUInt16 = Info.primitiveInfo(ArrowTypeId.uint16) + public static let ArrowUInt32 = Info.primitiveInfo(ArrowTypeId.uint32) + public static let ArrowUInt64 = Info.primitiveInfo(ArrowTypeId.uint64) + public static let ArrowFloat = Info.primitiveInfo(ArrowTypeId.float) + public static let ArrowDouble = Info.primitiveInfo(ArrowTypeId.double) + public static let ArrowUnknown = Info.primitiveInfo(ArrowTypeId.unknown) + public static let ArrowString = Info.variableInfo(ArrowTypeId.string) + public static let ArrowBool = Info.primitiveInfo(ArrowTypeId.boolean) + public static let ArrowDate32 = Info.primitiveInfo(ArrowTypeId.date32) + public static let ArrowDate64 = Info.primitiveInfo(ArrowTypeId.date64) + public static let ArrowBinary = Info.variableInfo(ArrowTypeId.binary) + public static let ArrowTime32 = Info.timeInfo(ArrowTypeId.time32) + public static let ArrowTime64 = Info.timeInfo(ArrowTypeId.time64) public init(_ info: ArrowType.Info) { self.info = info } - + public enum Info { - case PrimitiveInfo(ArrowTypeId) - case VariableInfo(ArrowTypeId) - case TimeInfo(ArrowTypeId) + case primitiveInfo(ArrowTypeId) + case variableInfo(ArrowTypeId) + case timeInfo(ArrowTypeId) } - public static func infoForNumericType(_ t: T.Type) -> ArrowType.Info { - if t == Int8.self { + public static func infoForNumericType(_ type: T.Type) -> ArrowType.Info { + if type == Int8.self { return ArrowType.ArrowInt8 - }else if t == Int16.self { + } else if type == Int16.self { return ArrowType.ArrowInt16 - }else if t == Int32.self { + } else if type == Int32.self { return ArrowType.ArrowInt32 - }else if t == Int64.self { + } else if type == Int64.self { return ArrowType.ArrowInt64 - }else if t == UInt8.self { + } else if type == UInt8.self { return ArrowType.ArrowUInt8 - }else if t == UInt16.self { + } else if type == UInt16.self { return ArrowType.ArrowUInt16 - }else if t == UInt32.self { + } else if type == UInt32.self { return ArrowType.ArrowUInt32 - }else if t == UInt64.self { + } else if type == UInt64.self { return ArrowType.ArrowUInt64 - }else if t == Float.self { + } else if type == Float.self { return ArrowType.ArrowFloat - }else if t == Double.self { + } else if type == Double.self { return ArrowType.ArrowDouble - }else { + } else { return ArrowType.ArrowUnknown } } } extension ArrowType.Info: Equatable { - public static func==(lhs: ArrowType.Info, rhs: ArrowType.Info) -> Bool { + public static func == (lhs: ArrowType.Info, rhs: ArrowType.Info) -> Bool { switch(lhs, rhs) { - case (.PrimitiveInfo(let lhsId), .PrimitiveInfo(let rhsId)): + case (.primitiveInfo(let lhsId), .primitiveInfo(let rhsId)): return lhsId == rhsId - case (.VariableInfo(let lhsId), .VariableInfo(let rhsId)): + case (.variableInfo(let lhsId), .variableInfo(let rhsId)): return lhsId == rhsId - case (.TimeInfo(let lhsId), .TimeInfo(let rhsId)): + case (.timeInfo(let lhsId), .timeInfo(let rhsId)): return lhsId == rhsId default: return false @@ -172,11 +172,9 @@ extension ArrowType.Info: Equatable { } func getBytesFor(_ data: T) -> Data? { - let t = T.self - if t == String.self { - let temp = data as! String + if let temp = data as? String { return temp.data(using: .utf8) - } else if t == Data.self { + } else if T.self == Data.self { return data as? Data } else { return nil diff --git a/swift/Arrow/Sources/Arrow/ArrowWriter.swift b/swift/Arrow/Sources/Arrow/ArrowWriter.swift index 9f3d9bd0c853c..efd0b9d3000dc 100644 --- a/swift/Arrow/Sources/Arrow/ArrowWriter.swift +++ b/swift/Arrow/Sources/Arrow/ArrowWriter.swift @@ -24,8 +24,8 @@ public protocol DataWriter { } public class ArrowWriter { - public class InMemDataWriter : DataWriter { - public private(set) var data: Data; + public class InMemDataWriter: DataWriter { + public private(set) var data: Data public var count: Int { return data.count } public init(_ data: Data) { self.data = data @@ -33,23 +33,23 @@ public class ArrowWriter { convenience init() { self.init(Data()) } - + public func append(_ data: Data) { self.data.append(data) } } - public class FileDataWriter : DataWriter { + public class FileDataWriter: DataWriter { private var handle: FileHandle - private var current_size: Int = 0 - public var count: Int { return current_size } + private var currentSize: Int = 0 + public var count: Int { return currentSize } public init(_ handle: FileHandle) { self.handle = handle } - + public func append(_ data: Data) { self.handle.write(data) - self.current_size += data.count + self.currentSize += data.count } } @@ -67,9 +67,9 @@ public class ArrowWriter { self.init(type, schema: schema, batches: [RecordBatch]()) } } - + public init() {} - + private func writeField(_ fbb: inout FlatBufferBuilder, field: ArrowField) -> Result { let nameOffset = fbb.create(string: field.name) let fieldTypeOffsetResult = toFBType(&fbb, arrowType: field.type) @@ -82,7 +82,7 @@ public class ArrowWriter { case .failure(let error): return .failure(error) } - + switch fieldTypeOffsetResult { case .success(let offset): org_apache_arrow_flatbuf_Field.add(type: offset, &fbb) @@ -101,18 +101,21 @@ public class ArrowWriter { case .failure(let error): return .failure(error) } - + } let fieldsOffset: Offset = fbb.createVector(ofOffsets: fieldOffsets) - let schemaOffset = org_apache_arrow_flatbuf_Schema.createSchema(&fbb, endianness: .little, fieldsVectorOffset: fieldsOffset) + let schemaOffset = + org_apache_arrow_flatbuf_Schema.createSchema(&fbb, + endianness: .little, + fieldsVectorOffset: fieldsOffset) return .success(schemaOffset) - } - - private func writeRecordBatches(_ writer: inout DataWriter, batches: [RecordBatch]) -> Result<[org_apache_arrow_flatbuf_Block], ArrowError> { - var rbBlocks = [org_apache_arrow_flatbuf_Block]() + private func writeRecordBatches(_ writer: inout DataWriter, + batches: [RecordBatch] + ) -> Result<[org_apache_arrow_flatbuf_Block], ArrowError> { + var rbBlocks = [org_apache_arrow_flatbuf_Block]() for batch in batches { let startIndex = writer.count switch writeRecordBatch(batch: batch) { @@ -120,8 +123,11 @@ public class ArrowWriter { withUnsafeBytes(of: rbResult.1.o.littleEndian) {writer.append(Data($0))} writer.append(rbResult.0) switch writeRecordBatchData(&writer, batch: batch) { - case .success(_): - rbBlocks.append(org_apache_arrow_flatbuf_Block(offset: Int64(startIndex), metaDataLength: Int32(0), bodyLength: Int64(rbResult.1.o))) + case .success: + rbBlocks.append( + org_apache_arrow_flatbuf_Block(offset: Int64(startIndex), + metaDataLength: Int32(0), + bodyLength: Int64(rbResult.1.o))) case .failure(let error): return .failure(error) } @@ -129,10 +135,10 @@ public class ArrowWriter { return .failure(error) } } - + return .success(rbBlocks) } - + private func writeRecordBatch(batch: RecordBatch) -> Result<(Data, Offset), ArrowError> { let schema = batch.schema var output = Data() @@ -143,12 +149,13 @@ public class ArrowWriter { fbb.startVector(schema.fields.count, elementSize: MemoryLayout.size) for index in (0 ..< schema.fields.count).reversed() { let column = batch.column(index) - let fieldNode = org_apache_arrow_flatbuf_FieldNode(length: Int64(column.length), nullCount: Int64(column.nullCount)) + let fieldNode = + org_apache_arrow_flatbuf_FieldNode(length: Int64(column.length), + nullCount: Int64(column.nullCount)) fieldNodeOffsets.append(fbb.create(struct: fieldNode)) } - - let nodeOffset = fbb.endVector(len: schema.fields.count) + let nodeOffset = fbb.endVector(len: schema.fields.count) // write out buffers var buffers = [org_apache_arrow_flatbuf_Buffer]() var bufferOffset = Int(0) @@ -162,19 +169,18 @@ public class ArrowWriter { bufferOffset += bufferDataSize } } - + org_apache_arrow_flatbuf_RecordBatch.startVectorOfBuffers(batch.schema.fields.count, in: &fbb) - for buffer in buffers.reversed() { + for buffer in buffers.reversed() { fbb.create(struct: buffer) } let batchBuffersOffset = fbb.endVector(len: buffers.count) - let startRb = org_apache_arrow_flatbuf_RecordBatch.startRecordBatch(&fbb); - org_apache_arrow_flatbuf_RecordBatch.addVectorOf(nodes:nodeOffset, &fbb) - org_apache_arrow_flatbuf_RecordBatch.addVectorOf(buffers:batchBuffersOffset, &fbb) + let startRb = org_apache_arrow_flatbuf_RecordBatch.startRecordBatch(&fbb) + org_apache_arrow_flatbuf_RecordBatch.addVectorOf(nodes: nodeOffset, &fbb) + org_apache_arrow_flatbuf_RecordBatch.addVectorOf(buffers: batchBuffersOffset, &fbb) let recordBatchOffset = org_apache_arrow_flatbuf_RecordBatch.endRecordBatch(&fbb, start: startRb) - - let bodySize = Int64(bufferOffset); + let bodySize = Int64(bufferOffset) let startMessage = org_apache_arrow_flatbuf_Message.startMessage(&fbb) org_apache_arrow_flatbuf_Message.add(bodyLength: Int64(bodySize), &fbb) org_apache_arrow_flatbuf_Message.add(headerType: .recordbatch, &fbb) @@ -184,11 +190,11 @@ public class ArrowWriter { output.append(fbb.data) return .success((output, Offset(offset: UInt32(output.count)))) } - + private func writeRecordBatchData(_ writer: inout DataWriter, batch: RecordBatch) -> Result { for index in 0 ..< batch.schema.fields.count { let column = batch.column(index) - let colBufferData = column.getBufferData(); + let colBufferData = column.getBufferData() for var bufferData in colBufferData { addPadForAlignment(&bufferData) writer.append(bufferData) @@ -197,8 +203,10 @@ public class ArrowWriter { return .success(true) } - - private func writeFooter(schema: ArrowSchema, rbBlocks: [org_apache_arrow_flatbuf_Block]) -> Result { + + private func writeFooter(schema: ArrowSchema, + rbBlocks: [org_apache_arrow_flatbuf_Block] + ) -> Result { var fbb: FlatBufferBuilder = FlatBufferBuilder() switch writeSchema(&fbb, schema: schema) { case .success(let schemaOffset): @@ -206,10 +214,8 @@ public class ArrowWriter { for blkInfo in rbBlocks.reversed() { fbb.create(struct: blkInfo) } - + let rbBlkEnd = fbb.endVector(len: rbBlocks.count) - - let footerStartOffset = org_apache_arrow_flatbuf_Footer.startFooter(&fbb) org_apache_arrow_flatbuf_Footer.add(schema: schemaOffset, &fbb) org_apache_arrow_flatbuf_Footer.addVectorOf(recordBatches: rbBlkEnd, &fbb) @@ -231,7 +237,7 @@ public class ArrowWriter { case .failure(let error): return .failure(error) } - + switch writeRecordBatches(&writer, batches: info.batches) { case .success(let rbBlocks): switch writeFooter(schema: info.schema, rbBlocks: rbBlocks) { @@ -240,9 +246,9 @@ public class ArrowWriter { let footerOffset = writer.count writer.append(footerData) addPadForAlignment(&writer) - + withUnsafeBytes(of: Int32(0).littleEndian) { writer.append(Data($0)) } - let footerDiff = (UInt32(writer.count) - UInt32(footerOffset)); + let footerDiff = (UInt32(writer.count) - UInt32(footerOffset)) withUnsafeBytes(of: footerDiff.littleEndian) { writer.append(Data($0)) } case .failure(let error): return .failure(error) @@ -257,8 +263,12 @@ public class ArrowWriter { public func toStream(_ info: ArrowWriter.Info) -> Result { var writer: any DataWriter = InMemDataWriter() switch writeStream(&writer, info: info) { - case .success(_): - return .success((writer as! InMemDataWriter).data) + case .success: + if let memWriter = writer as? InMemDataWriter { + return .success(memWriter.data) + } else { + return .failure(.invalid("Unable to cast writer")) + } case .failure(let error): return .failure(error) } @@ -270,22 +280,22 @@ public class ArrowWriter { } catch { return .failure(.ioError("\(error)")) } - + let fileHandle = FileHandle(forUpdatingAtPath: fileName.path)! defer { fileHandle.closeFile() } - - var markerData = FILEMARKER.data(using: .utf8)!; + + var markerData = FILEMARKER.data(using: .utf8)! addPadForAlignment(&markerData) var writer: any DataWriter = FileDataWriter(fileHandle) writer.append(FILEMARKER.data(using: .utf8)!) switch writeStream(&writer, info: info) { - case .success(_): + case .success: writer.append(FILEMARKER.data(using: .utf8)!) case .failure(let error): return .failure(error) } - + return .success(true) } } diff --git a/swift/Arrow/Sources/Arrow/ArrowWriterHelper.swift b/swift/Arrow/Sources/Arrow/ArrowWriterHelper.swift index 612df55830860..fdc72ef6e7f11 100644 --- a/swift/Arrow/Sources/Arrow/ArrowWriterHelper.swift +++ b/swift/Arrow/Sources/Arrow/ArrowWriterHelper.swift @@ -47,26 +47,36 @@ func toFBTypeEnum(_ arrowType: ArrowType) -> Result Result { +func toFBType( // swiftlint:disable:this cyclomatic_complexity + _ fbb: inout FlatBufferBuilder, + arrowType: ArrowType +) -> Result { let infoType = arrowType.info if infoType == ArrowType.ArrowInt8 || infoType == ArrowType.ArrowUInt8 { - return .success(org_apache_arrow_flatbuf_Int.createInt(&fbb, bitWidth: 8, isSigned: infoType == ArrowType.ArrowInt8)) + return .success(org_apache_arrow_flatbuf_Int.createInt( + &fbb, bitWidth: 8, isSigned: infoType == ArrowType.ArrowInt8)) } else if infoType == ArrowType.ArrowInt16 || infoType == ArrowType.ArrowUInt16 { - return .success(org_apache_arrow_flatbuf_Int.createInt(&fbb, bitWidth: 16, isSigned: infoType == ArrowType.ArrowInt16)) + return .success(org_apache_arrow_flatbuf_Int.createInt( + &fbb, bitWidth: 16, isSigned: infoType == ArrowType.ArrowInt16)) } else if infoType == ArrowType.ArrowInt32 || infoType == ArrowType.ArrowUInt32 { - return .success(org_apache_arrow_flatbuf_Int.createInt(&fbb, bitWidth: 32, isSigned: infoType == ArrowType.ArrowInt32)) + return .success(org_apache_arrow_flatbuf_Int.createInt( + &fbb, bitWidth: 32, isSigned: infoType == ArrowType.ArrowInt32)) } else if infoType == ArrowType.ArrowInt64 || infoType == ArrowType.ArrowUInt64 { - return .success(org_apache_arrow_flatbuf_Int.createInt(&fbb, bitWidth: 64, isSigned: infoType == ArrowType.ArrowInt64)) + return .success(org_apache_arrow_flatbuf_Int.createInt( + &fbb, bitWidth: 64, isSigned: infoType == ArrowType.ArrowInt64)) } else if infoType == ArrowType.ArrowFloat { return .success(org_apache_arrow_flatbuf_FloatingPoint.createFloatingPoint(&fbb, precision: .single)) } else if infoType == ArrowType.ArrowDouble { return .success(org_apache_arrow_flatbuf_FloatingPoint.createFloatingPoint(&fbb, precision: .double)) } else if infoType == ArrowType.ArrowString { - return .success(org_apache_arrow_flatbuf_Utf8.endUtf8(&fbb, start: org_apache_arrow_flatbuf_Utf8.startUtf8(&fbb))) + return .success(org_apache_arrow_flatbuf_Utf8.endUtf8( + &fbb, start: org_apache_arrow_flatbuf_Utf8.startUtf8(&fbb))) } else if infoType == ArrowType.ArrowBinary { - return .success(org_apache_arrow_flatbuf_Binary.endBinary(&fbb, start: org_apache_arrow_flatbuf_Binary.startBinary(&fbb))) + return .success(org_apache_arrow_flatbuf_Binary.endBinary( + &fbb, start: org_apache_arrow_flatbuf_Binary.startBinary(&fbb))) } else if infoType == ArrowType.ArrowBool { - return .success(org_apache_arrow_flatbuf_Bool.endBool(&fbb, start: org_apache_arrow_flatbuf_Bool.startBool(&fbb))) + return .success(org_apache_arrow_flatbuf_Bool.endBool( + &fbb, start: org_apache_arrow_flatbuf_Bool.startBool(&fbb))) } else if infoType == ArrowType.ArrowDate32 { let startOffset = org_apache_arrow_flatbuf_Date.startDate(&fbb) org_apache_arrow_flatbuf_Date.add(unit: .day, &fbb) @@ -77,14 +87,20 @@ func toFBType(_ fbb: inout FlatBufferBuilder, arrowType: ArrowType) -> Result Int { if padding > 0 { return count + (alignment - padding) } - + return count } diff --git a/swift/Arrow/Sources/Arrow/BitUtility.swift b/swift/Arrow/Sources/Arrow/BitUtility.swift index 4c5da298033b0..84edf9889b62c 100644 --- a/swift/Arrow/Sources/Arrow/BitUtility.swift +++ b/swift/Arrow/Sources/Arrow/BitUtility.swift @@ -38,4 +38,3 @@ class BitUtility { buffer.rawPointer.storeBytes(of: theByte, toByteOffset: Int(byteIndex), as: UInt8.self) } } - diff --git a/swift/Arrow/Sources/Arrow/ChunkedArray.swift b/swift/Arrow/Sources/Arrow/ChunkedArray.swift index edd4d494b14c4..3a06aa46550df 100644 --- a/swift/Arrow/Sources/Arrow/ChunkedArray.swift +++ b/swift/Arrow/Sources/Arrow/ChunkedArray.swift @@ -26,16 +26,17 @@ public class ChunkedArrayHolder { public let length: UInt public let nullCount: UInt public let holder: Any + public let getBufferData: () -> Result<[Data], ArrowError> public let getBufferDataSizes: () -> Result<[Int], ArrowError> - public init(_ chunked: ChunkedArray) { + public init(_ chunked: ChunkedArray) { // swiftlint:disable:this cyclomatic_complexity self.holder = chunked self.length = chunked.length self.type = chunked.type self.nullCount = chunked.nullCount self.getBufferData = {() -> Result<[Data], ArrowError> in var bufferData = [Data]() - var numBuffers = 2; + var numBuffers = 2 switch toFBTypeEnum(chunked.type) { case .success(let fbType): if !isFixedPrimitive(fbType) { @@ -49,19 +50,19 @@ public class ChunkedArrayHolder { bufferData.append(Data()) } - for arrow_data in chunked.arrays { + for arrowData in chunked.arrays { for index in 0 ..< numBuffers { - arrow_data.arrowData.buffers[index].append(to: &bufferData[index]) + arrowData.arrowData.buffers[index].append(to: &bufferData[index]) } } - return .success(bufferData); + return .success(bufferData) } - + self.getBufferDataSizes = {() -> Result<[Int], ArrowError> in var bufferDataSizes = [Int]() - var numBuffers = 2; - + var numBuffers = 2 + switch toFBTypeEnum(chunked.type) { case .success(let fbType): if !isFixedPrimitive(fbType) { @@ -70,35 +71,34 @@ public class ChunkedArrayHolder { case .failure(let error): return .failure(error) } - + for _ in 0 ..< numBuffers { bufferDataSizes.append(Int(0)) } - - for arrow_data in chunked.arrays { + + for arrowData in chunked.arrays { for index in 0 ..< numBuffers { - bufferDataSizes[index] += Int(arrow_data.arrowData.buffers[index].capacity) + bufferDataSizes[index] += Int(arrowData.arrowData.buffers[index].capacity) } } return .success(bufferDataSizes) } - } } -public class ChunkedArray : AsString { +public class ChunkedArray: AsString { public let arrays: [ArrowArray] public let type: ArrowType public let nullCount: UInt public let length: UInt - public var arrayCount: UInt {get{return UInt(self.arrays.count)}} - + public var arrayCount: UInt {return UInt(self.arrays.count)} + public init(_ arrays: [ArrowArray]) throws { if arrays.count == 0 { throw ArrowError.arrayHasNoElements } - + self.type = arrays[0].arrowData.type var len: UInt = 0 var nullCount: UInt = 0 @@ -106,38 +106,38 @@ public class ChunkedArray : AsString { len += array.length nullCount += array.nullCount } - + self.arrays = arrays self.length = len self.nullCount = nullCount } - + public subscript(_ index: UInt) -> T? { if arrays.count == 0 { return nil } - + var localIndex = index - var arrayIndex = 0; + var arrayIndex = 0 var len: UInt = arrays[arrayIndex].length while localIndex > (len - 1) { arrayIndex += 1 if arrayIndex > arrays.count { return nil } - + localIndex -= len len = arrays[arrayIndex].length } - + return arrays[arrayIndex][localIndex] } - + public func asString(_ index: UInt) -> String { if self[index] == nil { return "" } - + return "\(self[index]!)" } } diff --git a/swift/Arrow/Sources/Arrow/MemoryAllocator.swift b/swift/Arrow/Sources/Arrow/MemoryAllocator.swift index b4a7bd8e2b890..0f6e54ec0426d 100644 --- a/swift/Arrow/Sources/Arrow/MemoryAllocator.swift +++ b/swift/Arrow/Sources/Arrow/MemoryAllocator.swift @@ -22,10 +22,10 @@ public class MemoryAllocator { init(_ alignment: Int) { self.alignment = alignment } - + func allocateArray(_ byteCount: Int) -> UnsafeMutableRawPointer { return UnsafeMutableRawPointer.allocate( byteCount: byteCount, - alignment: self.alignment); + alignment: self.alignment) } } diff --git a/swift/Arrow/Tests/ArrowTests/ArrayTests.swift b/swift/Arrow/Tests/ArrowTests/ArrayTests.swift index d36073e1618c7..069dbfc88f3ac 100644 --- a/swift/Arrow/Tests/ArrowTests/ArrayTests.swift +++ b/swift/Arrow/Tests/ArrowTests/ArrayTests.swift @@ -23,11 +23,11 @@ final class ArrayTests: XCTestCase { // This is an example of a functional test case. // Use XCTAssert and related functions to verify your tests produce the correct // results. - let arrayBuilder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); - for i in 0..<100 { - arrayBuilder.append(UInt8(i)) + let arrayBuilder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() + for index in 0..<100 { + arrayBuilder.append(UInt8(index)) } - + XCTAssertEqual(arrayBuilder.nullCount, 0) arrayBuilder.append(nil) XCTAssertEqual(arrayBuilder.length, 101) @@ -38,8 +38,8 @@ final class ArrayTests: XCTestCase { XCTAssertEqual(array[1]!, 1) XCTAssertEqual(array[10]!, 10) XCTAssertEqual(try array.isNull(100), true) - - let doubleBuilder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + + let doubleBuilder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() doubleBuilder.append(14) doubleBuilder.append(40.4) XCTAssertEqual(doubleBuilder.nullCount, 0) @@ -50,33 +50,34 @@ final class ArrayTests: XCTestCase { XCTAssertEqual(doubleArray[0]!, 14) XCTAssertEqual(doubleArray[1]!, 40.4) } - + func testStringArray() throws { - let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder(); - for i in 0..<100 { - if i % 10 == 9 { + let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder() + for index in 0..<100 { + if index % 10 == 9 { stringBuilder.append(nil) } else { - stringBuilder.append("test" + String(i)) + stringBuilder.append("test" + String(index)) } } + XCTAssertEqual(stringBuilder.nullCount, 10) XCTAssertEqual(stringBuilder.length, 100) XCTAssertEqual(stringBuilder.capacity, 648) let stringArray = try stringBuilder.finish() XCTAssertEqual(stringArray.length, 100) - for i in 0.. URL { return URL(fileURLWithPath: path).deletingLastPathComponent() } +func makeSchema() -> ArrowSchema { + let schemaBuilder = ArrowSchema.Builder() + return schemaBuilder.addField("col1", type: ArrowType(ArrowType.ArrowUInt8), isNullable: true) + .addField("col2", type: ArrowType(ArrowType.ArrowString), isNullable: false) + .addField("col3", type: ArrowType(ArrowType.ArrowDate32), isNullable: false) + .finish() +} + +func makeRecordBatch() throws -> RecordBatch { + let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() + uint8Builder.append(10) + uint8Builder.append(22) + uint8Builder.append(33) + uint8Builder.append(44) + let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder() + stringBuilder.append("test10") + stringBuilder.append("test22") + stringBuilder.append("test33") + stringBuilder.append("test44") + let date32Builder = try ArrowArrayBuilders.loadDate32ArrayBuilder() + let date2 = Date(timeIntervalSinceReferenceDate: 86400 * 1) + let date1 = Date(timeIntervalSinceReferenceDate: 86400 * 5000 + 352) + date32Builder.append(date1) + date32Builder.append(date2) + date32Builder.append(date1) + date32Builder.append(date2) + let intHolder = ArrowArrayHolder(try uint8Builder.finish()) + let stringHolder = ArrowArrayHolder(try stringBuilder.finish()) + let date32Holder = ArrowArrayHolder(try date32Builder.finish()) + let result = RecordBatch.Builder() + .addColumn("col1", arrowArray: intHolder) + .addColumn("col2", arrowArray: stringHolder) + .addColumn("col3", arrowArray: date32Holder) + .finish() + switch result { + case .success(let recordBatch): + return recordBatch + case .failure(let error): + throw error + } +} + final class IPCFileReaderTests: XCTestCase { func testFileReader_double() throws { let fileURL = currentDirectory().appendingPathComponent("../../testdata_double.arrow") @@ -71,6 +113,7 @@ final class IPCFileReaderTests: XCTestCase { case .failure(let error): throw error } + XCTAssertEqual(recordBatches.count, 1) for recordBatch in recordBatches { XCTAssertEqual(recordBatch.length, 5) @@ -82,7 +125,7 @@ final class IPCFileReaderTests: XCTestCase { XCTAssertEqual(recordBatch.schema.fields[1].type.info, ArrowType.ArrowString) for index in 0.. ArrowSchema { - let schemaBuilder = ArrowSchema.Builder(); - return schemaBuilder.addField("col1", type: ArrowType(ArrowType.ArrowUInt8), isNullable: true) - .addField("col2", type: ArrowType(ArrowType.ArrowString), isNullable: false) - .addField("col3", type: ArrowType(ArrowType.ArrowDate32), isNullable: false) - .finish() - } - func makeRecordBatch() throws -> RecordBatch { - let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); - uint8Builder.append(10) - uint8Builder.append(22) - uint8Builder.append(33) - uint8Builder.append(44) - let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder(); - stringBuilder.append("test10") - stringBuilder.append("test22") - stringBuilder.append("test33") - stringBuilder.append("test44") - let date32Builder = try ArrowArrayBuilders.loadDate32ArrayBuilder(); - let date2 = Date(timeIntervalSinceReferenceDate: 86400 * 1) - let date1 = Date(timeIntervalSinceReferenceDate: 86400 * 5000 + 352) - date32Builder.append(date1) - date32Builder.append(date2) - date32Builder.append(date1) - date32Builder.append(date2) - let intHolder = ArrowArrayHolder(try uint8Builder.finish()) - let stringHolder = ArrowArrayHolder(try stringBuilder.finish()) - let date32Holder = ArrowArrayHolder(try date32Builder.finish()) - let result = RecordBatch.Builder() - .addColumn("col1", arrowArray: intHolder) - .addColumn("col2", arrowArray: stringHolder) - .addColumn("col3", arrowArray: date32Holder) - .finish() - switch result { - case .success(let recordBatch): - return recordBatch - case .failure(let error): - throw error - } - } - + func testRBInMemoryToFromStream() throws { - //read existing file + // read existing file let schema = makeSchema() let recordBatch = try makeRecordBatch() let arrowWriter = ArrowWriter() @@ -187,15 +190,21 @@ final class IPCFileReaderTests: XCTestCase { XCTAssertEqual(recordBatch.schema.fields[1].type.info, ArrowType.ArrowString) XCTAssertEqual(recordBatch.schema.fields[2].name, "col3") XCTAssertEqual(recordBatch.schema.fields[2].type.info, ArrowType.ArrowDate32) - let dateVal = "\((recordBatch.columns[2].array as! AsString).asString(0))" + let columns = recordBatch.columns + let dateVal = + "\((columns[2].array as! AsString).asString(0))" // swiftlint:disable:this force_cast XCTAssertEqual(dateVal, "2014-09-10 00:00:00 +0000") - let stringVal = "\((recordBatch.columns[1].array as! AsString).asString(1))" + let stringVal = + "\((columns[1].array as! AsString).asString(1))" // swiftlint:disable:this force_cast XCTAssertEqual(stringVal, "test22") - let uintVal = "\((recordBatch.columns[0].array as! AsString).asString(0))" + let uintVal = + "\((columns[0].array as! AsString).asString(0))" // swiftlint:disable:this force_cast XCTAssertEqual(uintVal, "10") - let stringVal2 = "\((recordBatch.columns[1].array as! AsString).asString(3))" + let stringVal2 = + "\((columns[1].array as! AsString).asString(3))" // swiftlint:disable:this force_cast XCTAssertEqual(stringVal2, "test44") - let uintVal2 = "\((recordBatch.columns[0].array as! AsString).asString(3))" + let uintVal2 = + "\((columns[0].array as! AsString).asString(3))" // swiftlint:disable:this force_cast XCTAssertEqual(uintVal2, "44") } case.failure(let error): @@ -205,9 +214,9 @@ final class IPCFileReaderTests: XCTestCase { throw error } } - + func testSchemaInMemoryToFromStream() throws { - //read existing file + // read existing file let schema = makeSchema() let arrowWriter = ArrowWriter() let writerInfo = ArrowWriter.Info(.schema, schema: schema) @@ -232,18 +241,18 @@ final class IPCFileReaderTests: XCTestCase { throw error } } - + func makeBinaryDataset() throws -> (ArrowSchema, RecordBatch) { - let schemaBuilder = ArrowSchema.Builder(); + let schemaBuilder = ArrowSchema.Builder() let schema = schemaBuilder.addField("binary", type: ArrowType(ArrowType.ArrowBinary), isNullable: false) .finish() - - let binaryBuilder = try ArrowArrayBuilders.loadBinaryArrayBuilder(); + + let binaryBuilder = try ArrowArrayBuilders.loadBinaryArrayBuilder() binaryBuilder.append("test10".data(using: .utf8)) binaryBuilder.append("test22".data(using: .utf8)) binaryBuilder.append("test33".data(using: .utf8)) binaryBuilder.append("test44".data(using: .utf8)) - + let binaryHolder = ArrowArrayHolder(try binaryBuilder.finish()) let result = RecordBatch.Builder() .addColumn("binary", arrowArray: binaryHolder) @@ -255,24 +264,23 @@ final class IPCFileReaderTests: XCTestCase { throw error } } - + func makeTimeDataset() throws -> (ArrowSchema, RecordBatch) { - let schemaBuilder = ArrowSchema.Builder(); - let schema = schemaBuilder.addField("time64", type: ArrowTypeTime64(.Microseconds), isNullable: false) - .addField("time32", type: ArrowTypeTime32(.Milliseconds), isNullable: false) + let schemaBuilder = ArrowSchema.Builder() + let schema = schemaBuilder.addField("time64", type: ArrowTypeTime64(.microseconds), isNullable: false) + .addField("time32", type: ArrowTypeTime32(.milliseconds), isNullable: false) .finish() - - let time64Builder = try ArrowArrayBuilders.loadTime64ArrayBuilder(.Nanoseconds); + + let time64Builder = try ArrowArrayBuilders.loadTime64ArrayBuilder(.nanoseconds) time64Builder.append(12345678) time64Builder.append(1) time64Builder.append(nil) time64Builder.append(98765432) - let time32Builder = try ArrowArrayBuilders.loadTime32ArrayBuilder(.Milliseconds); + let time32Builder = try ArrowArrayBuilders.loadTime32ArrayBuilder(.milliseconds) time32Builder.append(1) time32Builder.append(2) time32Builder.append(nil) time32Builder.append(3) - let time64Holder = ArrowArrayHolder(try time64Builder.finish()) let time32Holder = ArrowArrayHolder(try time32Builder.finish()) let result = RecordBatch.Builder() @@ -286,9 +294,9 @@ final class IPCFileReaderTests: XCTestCase { throw error } } - + func testBinaryInMemroyToFromStream() throws { - let dataset = try makeBinaryDataset(); + let dataset = try makeBinaryDataset() let writerInfo = ArrowWriter.Info(.recordbatch, schema: dataset.0, batches: [dataset.1]) let arrowWriter = ArrowWriter() switch arrowWriter.toStream(writerInfo) { @@ -304,7 +312,9 @@ final class IPCFileReaderTests: XCTestCase { XCTAssertEqual(result.batches.count, 1) let recordBatch = result.batches[0] XCTAssertEqual(recordBatch.length, 4) - let stringVal = "\((recordBatch.columns[0].array as! AsString).asString(1))" + let columns = recordBatch.columns + let stringVal = + "\((columns[0].array as! AsString).asString(1))" // swiftlint:disable:this force_cast XCTAssertEqual(stringVal, "test22") case.failure(let error): throw error @@ -313,9 +323,9 @@ final class IPCFileReaderTests: XCTestCase { throw error } } - + func testTimeInMemroyToFromStream() throws { - let dataset = try makeTimeDataset(); + let dataset = try makeTimeDataset() let writerInfo = ArrowWriter.Info(.recordbatch, schema: dataset.0, batches: [dataset.1]) let arrowWriter = ArrowWriter() switch arrowWriter.toStream(writerInfo) { @@ -333,9 +343,12 @@ final class IPCFileReaderTests: XCTestCase { XCTAssertEqual(result.batches.count, 1) let recordBatch = result.batches[0] XCTAssertEqual(recordBatch.length, 4) - let stringVal = "\((recordBatch.columns[0].array as! AsString).asString(0))" + let columns = recordBatch.columns + let stringVal = + "\((columns[0].array as! AsString).asString(0))" // swiftlint:disable:this force_cast XCTAssertEqual(stringVal, "12345678") - let stringVal2 = "\((recordBatch.columns[1].array as! AsString).asString(3))" + let stringVal2 = + "\((columns[1].array as! AsString).asString(3))" // swiftlint:disable:this force_cast XCTAssertEqual(stringVal2, "3") case.failure(let error): throw error diff --git a/swift/Arrow/Tests/ArrowTests/RecordBatchTests.swift b/swift/Arrow/Tests/ArrowTests/RecordBatchTests.swift index 9a469bfbd861b..ab6cad1b5e409 100644 --- a/swift/Arrow/Tests/ArrowTests/RecordBatchTests.swift +++ b/swift/Arrow/Tests/ArrowTests/RecordBatchTests.swift @@ -20,13 +20,13 @@ import XCTest final class RecordBatchTests: XCTestCase { func testRecordBatch() throws { - let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() uint8Builder.append(10) uint8Builder.append(22) - let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder(); + let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder() stringBuilder.append("test10") stringBuilder.append("test22") - + let intHolder = ArrowArrayHolder(try uint8Builder.finish()) let stringHolder = ArrowArrayHolder(try stringBuilder.finish()) let result = RecordBatch.Builder() @@ -44,8 +44,8 @@ final class RecordBatchTests: XCTestCase { XCTAssertEqual(schema.fields[1].type.info, ArrowType.ArrowString) XCTAssertEqual(schema.fields[1].isNullable, false) XCTAssertEqual(recordBatch.columns.count, 2) - let col1: ArrowArray = recordBatch.data(for: 0); - let col2: ArrowArray = recordBatch.data(for: 1); + let col1: ArrowArray = recordBatch.data(for: 0) + let col2: ArrowArray = recordBatch.data(for: 1) XCTAssertEqual(col1.length, 2) XCTAssertEqual(col2.length, 2) case .failure(let error): diff --git a/swift/Arrow/Tests/ArrowTests/TableTests.swift b/swift/Arrow/Tests/ArrowTests/TableTests.swift index 73ca3d3726d94..1c6b2d9c0a05c 100644 --- a/swift/Arrow/Tests/ArrowTests/TableTests.swift +++ b/swift/Arrow/Tests/ArrowTests/TableTests.swift @@ -20,7 +20,7 @@ import XCTest final class TableTests: XCTestCase { func testSchema() throws { - let schemaBuilder = ArrowSchema.Builder(); + let schemaBuilder = ArrowSchema.Builder() let schema = schemaBuilder.addField("col1", type: ArrowType(ArrowType.ArrowInt8), isNullable: true) .addField("col2", type: ArrowType(ArrowType.ArrowBool), isNullable: false) .finish() @@ -32,26 +32,24 @@ final class TableTests: XCTestCase { XCTAssertEqual(schema.fields[1].type.info, ArrowType.ArrowBool) XCTAssertEqual(schema.fields[1].isNullable, false) } - + func testTable() throws { - let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() uint8Builder.append(10) uint8Builder.append(22) - let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder(); + let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder() stringBuilder.append("test10") stringBuilder.append("test22") - let date32Builder: Date32ArrayBuilder = try ArrowArrayBuilders.loadDate32ArrayBuilder(); + let date32Builder: Date32ArrayBuilder = try ArrowArrayBuilders.loadDate32ArrayBuilder() let date2 = Date(timeIntervalSinceReferenceDate: 86400 * 1) let date1 = Date(timeIntervalSinceReferenceDate: 86400 * 5000 + 352) date32Builder.append(date1) date32Builder.append(date2) - let table = try ArrowTable.Builder() .addColumn("col1", arrowArray: uint8Builder.finish()) .addColumn("col2", arrowArray: stringBuilder.finish()) .addColumn("col3", arrowArray: date32Builder.finish()) - .finish(); - + .finish() let schema = table.schema XCTAssertEqual(schema.fields.count, 3) XCTAssertEqual(schema.fields[0].name, "col1") @@ -64,37 +62,35 @@ final class TableTests: XCTestCase { XCTAssertEqual(schema.fields[1].type.info, ArrowType.ArrowString) XCTAssertEqual(schema.fields[1].isNullable, false) XCTAssertEqual(table.columns.count, 3) - let col1: ChunkedArray = table.columns[0].data(); - let col2: ChunkedArray = table.columns[1].data(); - let col3: ChunkedArray = table.columns[2].data(); + let col1: ChunkedArray = table.columns[0].data() + let col2: ChunkedArray = table.columns[1].data() + let col3: ChunkedArray = table.columns[2].data() XCTAssertEqual(col1.length, 2) XCTAssertEqual(col2.length, 2) XCTAssertEqual(col3.length, 2) } func testTableWithChunkedData() throws { - let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() uint8Builder.append(10) uint8Builder.append(22) - let uint8Builder2: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + let uint8Builder2: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() uint8Builder2.append(33) - let uint8Builder3: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + let uint8Builder3: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() uint8Builder3.append(44) - let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder(); + let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder() stringBuilder.append("test10") stringBuilder.append("test22") - let stringBuilder2 = try ArrowArrayBuilders.loadStringArrayBuilder(); + let stringBuilder2 = try ArrowArrayBuilders.loadStringArrayBuilder() stringBuilder.append("test33") stringBuilder.append("test44") - - let date32Builder: Date32ArrayBuilder = try ArrowArrayBuilders.loadDate32ArrayBuilder(); + let date32Builder: Date32ArrayBuilder = try ArrowArrayBuilders.loadDate32ArrayBuilder() let date2 = Date(timeIntervalSinceReferenceDate: 86400 * 1) let date1 = Date(timeIntervalSinceReferenceDate: 86400 * 5000 + 352) date32Builder.append(date1) date32Builder.append(date2) date32Builder.append(date1) date32Builder.append(date2) - let intArray = try ChunkedArray([uint8Builder.finish(), uint8Builder2.finish(), uint8Builder3.finish()]) let stringArray = try ChunkedArray([stringBuilder.finish(), stringBuilder2.finish()]) let dateArray = try ChunkedArray([date32Builder.finish()]) @@ -102,8 +98,7 @@ final class TableTests: XCTestCase { .addColumn("col1", chunked: intArray) .addColumn("col2", chunked: stringArray) .addColumn("col3", chunked: dateArray) - .finish(); - + .finish() let schema = table.schema XCTAssertEqual(schema.fields.count, 3) XCTAssertEqual(schema.fields[0].name, "col1") @@ -116,9 +111,9 @@ final class TableTests: XCTestCase { XCTAssertEqual(schema.fields[1].type.info, ArrowType.ArrowString) XCTAssertEqual(schema.fields[1].isNullable, false) XCTAssertEqual(table.columns.count, 3) - let col1: ChunkedArray = table.columns[0].data(); - let col2: ChunkedArray = table.columns[1].data(); - let col3: ChunkedArray = table.columns[2].data(); + let col1: ChunkedArray = table.columns[0].data() + let col2: ChunkedArray = table.columns[1].data() + let col3: ChunkedArray = table.columns[2].data() XCTAssertEqual(col1.length, 4) XCTAssertEqual(col2.length, 4) XCTAssertEqual(col3.length, 4) @@ -129,13 +124,12 @@ final class TableTests: XCTestCase { } func testTableToRecordBatch() throws { - let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder(); + let uint8Builder: NumberArrayBuilder = try ArrowArrayBuilders.loadNumberArrayBuilder() uint8Builder.append(10) uint8Builder.append(22) - let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder(); + let stringBuilder = try ArrowArrayBuilders.loadStringArrayBuilder() stringBuilder.append("test10") stringBuilder.append("test22") - let intHolder = ArrowArrayHolder(try uint8Builder.finish()) let stringHolder = ArrowArrayHolder(try stringBuilder.finish()) let result = RecordBatch.Builder() @@ -155,8 +149,8 @@ final class TableTests: XCTestCase { XCTAssertEqual(schema.fields[1].type.info, ArrowType.ArrowString) XCTAssertEqual(schema.fields[1].isNullable, false) XCTAssertEqual(table.columns.count, 2) - let col1: ChunkedArray = table.columns[0].data(); - let col2: ChunkedArray = table.columns[1].data(); + let col1: ChunkedArray = table.columns[0].data() + let col2: ChunkedArray = table.columns[1].data() XCTAssertEqual(col1.length, 2) XCTAssertEqual(col2.length, 2) case .failure(let error): diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightAction.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightAction.swift index 04e917d474cff..8db12aaa99ffc 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightAction.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightAction.swift @@ -24,16 +24,16 @@ public class FlightAction { self.type = action.type self.body = action.body } - + public init(_ type: String, body: Data = Data()) { - self.type = type; - self.body = body; + self.type = type + self.body = body } - + func toProtocol() -> Arrow_Flight_Protocol_Action { - var flight_action = Arrow_Flight_Protocol_Action() - flight_action.type = self.type - flight_action.body = self.body - return flight_action + var flightAction = Arrow_Flight_Protocol_Action() + flightAction.type = self.type + flightAction.body = self.body + return flightAction } } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightActionType.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightActionType.swift index b3b06793feade..0b4778c6864d3 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightActionType.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightActionType.swift @@ -22,13 +22,13 @@ public class FlightActionType { init(_ actionType: Arrow_Flight_Protocol_ActionType) { self.type = actionType.type self.description = actionType.description_p - + } public init(_ type: String, description: String) { self.type = type self.description = description } - + func toProtocol() -> Arrow_Flight_Protocol_ActionType { var actionType = Arrow_Flight_Protocol_ActionType() actionType.type = self.type diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightClient.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightClient.swift index f7b8564af31d7..c9d30f9caf480 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightClient.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightClient.swift @@ -27,35 +27,36 @@ public class FlightClient { public init(channel: GRPCChannel) { client = Arrow_Flight_Protocol_FlightServiceAsyncClient(channel: channel) } - + public func listActions(_ closure: (FlightActionType) -> Void) async throws { let listActions = client.makeListActionsCall(Arrow_Flight_Protocol_Empty()) for try await data in listActions.responseStream { closure(FlightActionType(data)) } } - - public func listFlights(_ criteria :FlightCriteria, closure: (FlightInfo) throws -> Void) async throws { + + public func listFlights(_ criteria: FlightCriteria, closure: (FlightInfo) throws -> Void) async throws { let listFlights = client.makeListFlightsCall(criteria.toProtocol()) for try await data in listFlights.responseStream { - try closure(FlightInfo(data)); + try closure(FlightInfo(data)) } } - - + public func doAction(_ action: FlightAction, closure: (FlightResult) throws -> Void) async throws { let actionResponse = client.makeDoActionCall(action.toProtocol()) for try await data in actionResponse.responseStream { - try closure(FlightResult(data)); + try closure(FlightResult(data)) } } - + public func getSchema(_ descriptor: FlightDescriptor) async throws -> FlightSchemaResult { let schemaResultResponse = client.makeGetSchemaCall(descriptor.toProtocol()) return FlightSchemaResult(try await schemaResultResponse.response) } - - public func doGet(_ ticket: FlightTicket, readerResultClosure: (ArrowReader.ArrowReaderResult) throws -> Void) async throws { + + public func doGet( + _ ticket: FlightTicket, + readerResultClosure: (ArrowReader.ArrowReaderResult) throws -> Void) async throws { let getResult = client.makeDoGetCall(ticket.toProtocol()) let reader = ArrowReader() for try await data in getResult.responseStream { @@ -77,9 +78,9 @@ public class FlightClient { public func doPut(_ recordBatchs: [RecordBatch], closure: (FlightPutResult) throws -> Void) async throws { if recordBatchs.isEmpty { - throw ArrowFlightError.EmptyCollection + throw ArrowFlightError.emptyCollection } - + let putCall = client.makeDoPutCall() let writer = ArrowWriter() let writerInfo = ArrowWriter.Info(.recordbatch, schema: recordBatchs[0].schema, batches: recordBatchs) @@ -104,11 +105,13 @@ public class FlightClient { } } - public func doExchange(_ recordBatchs: [RecordBatch], closure: (ArrowReader.ArrowReaderResult) throws -> Void) async throws { + public func doExchange( + _ recordBatchs: [RecordBatch], + closure: (ArrowReader.ArrowReaderResult) throws -> Void) async throws { if recordBatchs.isEmpty { - throw ArrowFlightError.EmptyCollection + throw ArrowFlightError.emptyCollection } - + let exchangeCall = client.makeDoExchangeCall() let writer = ArrowWriter() let info = ArrowWriter.Info(.recordbatch, schema: recordBatchs[0].schema, batches: recordBatchs) diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightCriteria.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightCriteria.swift index a887a22ad1737..2d02959998c1e 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightCriteria.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightCriteria.swift @@ -19,18 +19,18 @@ import Foundation public class FlightCriteria { let criteria: Arrow_Flight_Protocol_Criteria - + public var expression: Data { criteria.expression } public init(_ expression: Data = Data()) { criteria = Arrow_Flight_Protocol_Criteria.with { $0.expression = expression } } - + init(_ criteria: Arrow_Flight_Protocol_Criteria) { self.criteria = criteria } - + func toProtocol() -> Arrow_Flight_Protocol_Criteria { return criteria } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightData.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightData.swift index 004fb785f0c11..fa853baab3b92 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightData.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightData.swift @@ -18,29 +18,29 @@ import Foundation public class FlightData { - let flight_data: Arrow_Flight_Protocol_FlightData + let flightData: Arrow_Flight_Protocol_FlightData public var flightDescriptor: FlightDescriptor? { - get { return flight_data.hasFlightDescriptor ? FlightDescriptor(flight_data.flightDescriptor) : nil } + return flightData.hasFlightDescriptor ? FlightDescriptor(flightData.flightDescriptor) : nil } - - public var dataBody: Data { flight_data.dataBody } - - init(_ flight_data: Arrow_Flight_Protocol_FlightData) { - self.flight_data = flight_data + + public var dataBody: Data { flightData.dataBody } + + init(_ flightData: Arrow_Flight_Protocol_FlightData) { + self.flightData = flightData } - + public init(_ dataBody: Data, flightDescriptor: FlightDescriptor? = nil) { if flightDescriptor != nil { - self.flight_data = Arrow_Flight_Protocol_FlightData.with { + self.flightData = Arrow_Flight_Protocol_FlightData.with { $0.dataBody = dataBody $0.flightDescriptor = flightDescriptor!.toProtocol() } } else { - self.flight_data = Arrow_Flight_Protocol_FlightData.with { + self.flightData = Arrow_Flight_Protocol_FlightData.with { $0.dataBody = dataBody } } } - - func toProtocol() -> Arrow_Flight_Protocol_FlightData { self.flight_data } + + func toProtocol() -> Arrow_Flight_Protocol_FlightData { self.flightData } } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightDescriptor.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightDescriptor.swift index 68bc91a3deda1..02712aaa099c7 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightDescriptor.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightDescriptor.swift @@ -18,22 +18,22 @@ import Foundation public class FlightDescriptor { - public enum type { + public enum DescriptorType { case unknown case path case cmd } - - public let type: FlightDescriptor.type + + public let type: FlightDescriptor.DescriptorType public let cmd: Data public let paths: [String] - + init(_ descriptor: Arrow_Flight_Protocol_FlightDescriptor) { self.type = descriptor.type == .cmd ? .cmd : .path self.cmd = descriptor.cmd self.paths = descriptor.path } - + public init(cmd: Data) { self.type = .cmd self.cmd = cmd @@ -45,7 +45,7 @@ public class FlightDescriptor { self.cmd = Data() self.paths = paths } - + func toProtocol() -> Arrow_Flight_Protocol_FlightDescriptor { var descriptor = Arrow_Flight_Protocol_FlightDescriptor() descriptor.type = self.type == .cmd ? .cmd : .path diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightEndpoint.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightEndpoint.swift index 7c40a2a157ae8..0493772781a13 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightEndpoint.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightEndpoint.swift @@ -17,18 +17,18 @@ import Foundation public class FlightEndpoint { - let ticket: FlightTicket; + let ticket: FlightTicket let locations: [FlightLocation] init(_ endpoint: Arrow_Flight_Protocol_FlightEndpoint) { self.ticket = FlightTicket(endpoint.ticket.ticket) self.locations = endpoint.location.map {return FlightLocation($0)} } - + public init(_ ticket: FlightTicket, locations: [FlightLocation]) { self.ticket = ticket - self.locations = locations; + self.locations = locations } - + func toProtocol() -> Arrow_Flight_Protocol_FlightEndpoint { var endpoint = Arrow_Flight_Protocol_FlightEndpoint() endpoint.ticket = self.ticket.toProtocol() diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightInfo.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightInfo.swift index b370c00db3d42..e59c0bcc6d997 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightInfo.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightInfo.swift @@ -19,37 +19,37 @@ import Foundation import Arrow public class FlightInfo { - let flight_info: Arrow_Flight_Protocol_FlightInfo + let flightInfo: Arrow_Flight_Protocol_FlightInfo public var flightDescriptor: FlightDescriptor? { - get { return flight_info.hasFlightDescriptor ? FlightDescriptor(flight_info.flightDescriptor) : nil } + return flightInfo.hasFlightDescriptor ? FlightDescriptor(flightInfo.flightDescriptor) : nil } - + public var endpoints: [FlightEndpoint] { - return self.flight_info.endpoint.map { FlightEndpoint($0) } + return self.flightInfo.endpoint.map { FlightEndpoint($0) } } - public var schema: Data { flight_info.schema } - + public var schema: Data { flightInfo.schema } + var endpoint: [Arrow_Flight_Protocol_FlightEndpoint] = [] - init(_ flight_info: Arrow_Flight_Protocol_FlightInfo) { - self.flight_info = flight_info + init(_ flightInfo: Arrow_Flight_Protocol_FlightInfo) { + self.flightInfo = flightInfo } - + public init(_ schema: Data, endpoints: [FlightEndpoint] = [FlightEndpoint](), descriptor: FlightDescriptor? = nil) { if let localDescriptor = descriptor { - self.flight_info = Arrow_Flight_Protocol_FlightInfo.with { + self.flightInfo = Arrow_Flight_Protocol_FlightInfo.with { $0.schema = schema $0.flightDescriptor = localDescriptor.toProtocol() $0.endpoint = endpoints.map { $0.toProtocol() } } } else { - self.flight_info = Arrow_Flight_Protocol_FlightInfo.with { + self.flightInfo = Arrow_Flight_Protocol_FlightInfo.with { $0.schema = schema $0.endpoint = endpoints.map { $0.toProtocol() } } } } - + func toProtocol() -> Arrow_Flight_Protocol_FlightInfo { - return self.flight_info + return self.flightInfo } } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightLocation.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightLocation.swift index b87671c903d44..9c89d100336f1 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightLocation.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightLocation.swift @@ -19,15 +19,15 @@ import Foundation public class FlightLocation { public let uri: String - + init(_ location: Arrow_Flight_Protocol_Location) { self.uri = location.uri } - + public init(_ uri: String) { - self.uri = uri; + self.uri = uri } - + func toProtocol() -> Arrow_Flight_Protocol_Location { var location = Arrow_Flight_Protocol_Location() location.uri = uri diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightPutResult.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightPutResult.swift index bf73c716e39c0..3b22f8f0baf11 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightPutResult.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightPutResult.swift @@ -22,11 +22,11 @@ public class FlightPutResult { public init(_ appMetadata: Data = Data()) { self.appMetadata = appMetadata } - + init(_ putResult: Arrow_Flight_Protocol_PutResult) { self.appMetadata = putResult.appMetadata } - + func toProtocol() -> Arrow_Flight_Protocol_PutResult { var putResult = Arrow_Flight_Protocol_PutResult() putResult.appMetadata = self.appMetadata diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightResult.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightResult.swift index ba55bede7c70c..d7cf828b96367 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightResult.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightResult.swift @@ -22,11 +22,11 @@ public class FlightResult { init(_ result: Arrow_Flight_Protocol_Result) { self.body = result.body } - + public init(_ body: Data) { self.body = body } - + func toProtocol() -> Arrow_Flight_Protocol_Result { var result = Arrow_Flight_Protocol_Result() result.body = self.body diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightSchemaResult.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightSchemaResult.swift index 8d5323b731ea8..fcb4b50c1aa91 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightSchemaResult.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightSchemaResult.swift @@ -19,18 +19,18 @@ import Foundation public class FlightSchemaResult { let schemaResult: Arrow_Flight_Protocol_SchemaResult - + public var schema: Data { schemaResult.schema } public init(_ schema: Data) { self.schemaResult = Arrow_Flight_Protocol_SchemaResult.with { $0.schema = schema } } - + init(_ schemaResult: Arrow_Flight_Protocol_SchemaResult) { self.schemaResult = schemaResult } - + func toProtocol() -> Arrow_Flight_Protocol_SchemaResult { return schemaResult } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightServer.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightServer.swift index f67f612b0bcb4..58280a0049f4b 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightServer.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightServer.swift @@ -23,10 +23,10 @@ import SwiftProtobuf import Arrow public enum ArrowFlightError: Error { - case Unknown(String?) - case NotImplemented(String? = nil) - case EmptyCollection - case IOError(String? = nil) + case unknown(String?) + case notImplemented(String? = nil) + case emptyCollection + case ioError(String? = nil) } public func schemaToArrowStream(_ schema: ArrowSchema) throws -> Data { @@ -46,117 +46,133 @@ public func streamToArrowSchema(_ schema: Data) throws -> ArrowSchema { if let retSchema = result.schema { return retSchema } - - throw ArrowFlightError.IOError("Schema not found") + + throw ArrowFlightError.ioError("Schema not found") case .failure(let error): throw error } } -public protocol ArrowFlightServer : Sendable { +public protocol ArrowFlightServer: Sendable { func listFlights(_ criteria: FlightCriteria, writer: FlightInfoStreamWriter) async throws func getFlightInfo(_ request: FlightDescriptor) async throws -> FlightInfo func getSchema(_ request: FlightDescriptor) async throws -> ArrowFlight.FlightSchemaResult func listActions(_ writer: ActionTypeStreamWriter) async throws func doAction(_ action: FlightAction, writer: ResultStreamWriter) async throws - func doGet(_ ticket: FlightTicket, writer: RecordBatchStreamWriter) async throws; + func doGet(_ ticket: FlightTicket, writer: RecordBatchStreamWriter) async throws func doPut(_ reader: RecordBatchStreamReader, writer: PutResultDataStreamWriter) async throws func doExchange(_ reader: RecordBatchStreamReader, writer: RecordBatchStreamWriter) async throws } -public func MakeFlightServer(_ handler: ArrowFlightServer) -> CallHandlerProvider { +public func makeFlightServer(_ handler: ArrowFlightServer) -> CallHandlerProvider { return InternalFlightServer(handler) } -internal final class InternalFlightServer : Arrow_Flight_Protocol_FlightServiceAsyncProvider { +internal final class InternalFlightServer: Arrow_Flight_Protocol_FlightServiceAsyncProvider { let arrowFlightServer: ArrowFlightServer? - + init(_ arrowFlightServer: ArrowFlightServer?) { self.arrowFlightServer = arrowFlightServer } - - func handshake(requestStream: GRPC.GRPCAsyncRequestStream, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { - throw ArrowFlightError.NotImplemented() + + func handshake(requestStream: GRPC.GRPCAsyncRequestStream, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { + throw ArrowFlightError.notImplemented() } - - func listFlights(request: Arrow_Flight_Protocol_Criteria, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { + + func listFlights(request: Arrow_Flight_Protocol_Criteria, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { if let server = arrowFlightServer { let writer = FlightInfoStreamWriter(responseStream) try await server.listFlights(FlightCriteria(request), writer: writer) return } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func getFlightInfo(request: Arrow_Flight_Protocol_FlightDescriptor, context: GRPC.GRPCAsyncServerCallContext) async throws -> Arrow_Flight_Protocol_FlightInfo { + + func getFlightInfo(request: Arrow_Flight_Protocol_FlightDescriptor, + context: GRPC.GRPCAsyncServerCallContext) async throws -> Arrow_Flight_Protocol_FlightInfo { if let server = arrowFlightServer { return try await server.getFlightInfo(FlightDescriptor(request)).toProtocol() } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func getSchema(request: Arrow_Flight_Protocol_FlightDescriptor, context: GRPC.GRPCAsyncServerCallContext) async throws -> Arrow_Flight_Protocol_SchemaResult { + + func getSchema(request: Arrow_Flight_Protocol_FlightDescriptor, + context: GRPC.GRPCAsyncServerCallContext) async throws -> Arrow_Flight_Protocol_SchemaResult { if let server = arrowFlightServer { return try await server.getSchema(FlightDescriptor(request)).toProtocol() } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func doGet(request: Arrow_Flight_Protocol_Ticket, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { + + func doGet(request: Arrow_Flight_Protocol_Ticket, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { if let server = arrowFlightServer { let writer = RecordBatchStreamWriter(responseStream) let ticket = FlightTicket(request) try await server.doGet(ticket, writer: writer) return } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func doPut(requestStream: GRPC.GRPCAsyncRequestStream, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { + + func doPut(requestStream: GRPC.GRPCAsyncRequestStream, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { if let server = arrowFlightServer { let reader = RecordBatchStreamReader(requestStream) let writer = PutResultDataStreamWriter(responseStream) try await server.doPut(reader, writer: writer) return } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func doExchange(requestStream: GRPC.GRPCAsyncRequestStream, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { + + func doExchange(requestStream: GRPC.GRPCAsyncRequestStream, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { if let server = arrowFlightServer { let reader = RecordBatchStreamReader(requestStream) let writer = RecordBatchStreamWriter(responseStream) try await server.doExchange(reader, writer: writer) return } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func doAction(request: Arrow_Flight_Protocol_Action, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { + + func doAction(request: Arrow_Flight_Protocol_Action, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { if let server = arrowFlightServer { try await server.doAction(FlightAction(request), writer: ResultStreamWriter(responseStream)) return } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - func listActions(request: Arrow_Flight_Protocol_Empty, responseStream: GRPC.GRPCAsyncResponseStreamWriter, context: GRPC.GRPCAsyncServerCallContext) async throws { + + func listActions(request: Arrow_Flight_Protocol_Empty, + responseStream: GRPC.GRPCAsyncResponseStreamWriter, + context: GRPC.GRPCAsyncServerCallContext) async throws { if let server = arrowFlightServer { let writer = ActionTypeStreamWriter(responseStream) try await server.listActions(writer) return } - - throw ArrowFlightError.NotImplemented() + + throw ArrowFlightError.notImplemented() } - - internal var interceptors: Arrow_Flight_Protocol_FlightServiceServerInterceptorFactoryProtocol? { get { return nil } } + + internal var interceptors: Arrow_Flight_Protocol_FlightServiceServerInterceptorFactoryProtocol? { return nil } } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/FlightTicket.swift b/swift/ArrowFlight/Sources/ArrowFlight/FlightTicket.swift index f77fc3545af5c..ab3cb321c6229 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/FlightTicket.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/FlightTicket.swift @@ -22,11 +22,11 @@ public class FlightTicket { init(_ ticket: Arrow_Flight_Protocol_Ticket) { self.data = ticket.ticket } - + public init(_ data: Data) { self.data = data } - + func toProtocol() -> Arrow_Flight_Protocol_Ticket { var ticket = Arrow_Flight_Protocol_Ticket() ticket.ticket = self.data diff --git a/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamReader.swift b/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamReader.swift index a6b9ce93a9acd..f0054a2577e3f 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamReader.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamReader.swift @@ -31,7 +31,7 @@ public class RecordBatchStreamReader: AsyncSequence, AsyncIteratorProtocol { self.stream = stream self.streamIterator = self.stream.makeAsyncIterator() } - + public func next() async throws -> Arrow.RecordBatch? { guard !Task.isCancelled else { return nil @@ -42,21 +42,24 @@ public class RecordBatchStreamReader: AsyncSequence, AsyncIteratorProtocol { batchIndex += 1 return batch } - + while true { let flightData = try await self.streamIterator.next() if flightData == nil { return nil } - - let data = (flightData as! Arrow_Flight_Protocol_FlightData).dataBody - switch reader.fromStream(data) { - case .success(let rbResult): - batches = rbResult.batches - batchIndex = 1 - return batches[0] - case .failure(let error): - throw error + + if let data = (flightData as? Arrow_Flight_Protocol_FlightData)?.dataBody { + switch reader.fromStream(data) { + case .success(let rbResult): + batches = rbResult.batches + batchIndex = 1 + return batches[0] + case .failure(let error): + throw error + } + } else { + throw ArrowError.invalid("Flight data is incorrect type.") } } } diff --git a/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamWriter.swift b/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamWriter.swift index 1efeba5310369..94fc0e91dff07 100644 --- a/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamWriter.swift +++ b/swift/ArrowFlight/Sources/ArrowFlight/RecordBatchStreamWriter.swift @@ -24,7 +24,7 @@ public class ActionTypeStreamWriter { init(_ stream: GRPCAsyncResponseStreamWriter) { self.stream = stream } - + public func write(_ actionType: FlightActionType) async throws { try await self.stream.send(actionType.toProtocol()) } @@ -35,7 +35,7 @@ public class ResultStreamWriter { init(_ stream: GRPCAsyncResponseStreamWriter) { self.stream = stream } - + public func write(_ result: FlightResult) async throws { try await self.stream.send(result.toProtocol()) } @@ -46,7 +46,7 @@ public class FlightInfoStreamWriter { init(_ stream: GRPCAsyncResponseStreamWriter) { self.stream = stream } - + public func write(_ result: FlightInfo) async throws { try await self.stream.send(result.toProtocol()) } @@ -57,7 +57,7 @@ public class PutResultDataStreamWriter { init(_ stream: GRPCAsyncResponseStreamWriter) { self.stream = stream } - + public func write(_ result: FlightPutResult) async throws { try await self.stream.send(result.toProtocol()) } @@ -69,7 +69,7 @@ public class RecordBatchStreamWriter { init(_ stream: GRPCAsyncResponseStreamWriter) { self.stream = stream } - + public func write(_ rb: RecordBatch) async throws { let info = ArrowWriter.Info(.recordbatch, schema: rb.schema, @@ -82,7 +82,7 @@ public class RecordBatchStreamWriter { let data = Arrow_Flight_Protocol_FlightData.with { $0.dataBody = rbResult } - + try await self.stream.send(data) case .failure(let error): throw error diff --git a/swift/ArrowFlight/Tests/ArrowFlightTests/FlightTest.swift b/swift/ArrowFlight/Tests/ArrowFlightTests/FlightTest.swift index 3fd52af08b82f..31dbcc1829a6a 100644 --- a/swift/ArrowFlight/Tests/ArrowFlightTests/FlightTest.swift +++ b/swift/ArrowFlight/Tests/ArrowFlightTests/FlightTest.swift @@ -67,54 +67,58 @@ func makeRecordBatch() throws -> RecordBatch { } } -final class MyFlightServer : ArrowFlightServer { - func doExchange(_ reader: ArrowFlight.RecordBatchStreamReader, writer: ArrowFlight.RecordBatchStreamWriter) async throws { +final class MyFlightServer: ArrowFlightServer { + func doExchange( + _ reader: ArrowFlight.RecordBatchStreamReader, + writer: ArrowFlight.RecordBatchStreamWriter) async throws { do { for try await rb in reader { XCTAssertEqual(rb.schema.fields.count, 3) XCTAssertEqual(rb.length, 4) } - + let rb = try makeRecordBatch() try await writer.write(rb) } catch { print("Unknown error: \(error)") } } - - func doPut(_ reader: ArrowFlight.RecordBatchStreamReader, writer: ArrowFlight.PutResultDataStreamWriter) async throws { + + func doPut( + _ reader: ArrowFlight.RecordBatchStreamReader, + writer: ArrowFlight.PutResultDataStreamWriter) async throws { for try await rb in reader { XCTAssertEqual(rb.schema.fields.count, 3) XCTAssertEqual(rb.length, 4) try await writer.write(FlightPutResult()) } } - + func doGet(_ ticket: ArrowFlight.FlightTicket, writer: ArrowFlight.RecordBatchStreamWriter) async throws { try await writer.write(try makeRecordBatch()) } - + func getSchema(_ request: ArrowFlight.FlightDescriptor) async throws -> ArrowFlight.FlightSchemaResult { XCTAssertEqual(String(bytes: request.cmd, encoding: .utf8)!, "schema info") XCTAssertEqual(request.type, .cmd) return try ArrowFlight.FlightSchemaResult(schemaToArrowStream(makeSchema())) } - + func getFlightInfo(_ request: ArrowFlight.FlightDescriptor) async throws -> ArrowFlight.FlightInfo { return ArrowFlight.FlightInfo(Data()) } - + func listFlights(_ criteria: ArrowFlight.FlightCriteria, writer: ArrowFlight.FlightInfoStreamWriter) async throws { XCTAssertEqual(String(bytes: criteria.expression, encoding: .utf8), "flight criteria expression") - let flight_info = try ArrowFlight.FlightInfo(schemaToArrowStream(makeSchema())) - try await writer.write(flight_info) + let flightInfo = try ArrowFlight.FlightInfo(schemaToArrowStream(makeSchema())) + try await writer.write(flightInfo) } - + func listActions(_ writer: ArrowFlight.ActionTypeStreamWriter) async throws { try await writer.write(FlightActionType("type1", description: "desc1")) try await writer.write(FlightActionType("type2", description: "desc2")) } - + func doAction(_ action: FlightAction, writer: ResultStreamWriter) async throws { XCTAssertEqual(action.type, "test_action") XCTAssertEqual(String(bytes: action.body, encoding: .utf8)!, "test_action body") @@ -131,16 +135,15 @@ struct FlightServerImpl { // Create an event loop group for the server to run on. let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount) // Create a provider using the features we read. - let provider = ArrowFlight.MakeFlightServer(MyFlightServer()) - + let provider = ArrowFlight.makeFlightServer(MyFlightServer()) + // Start the server and print its address once it has started. FlightServerImpl.server = try await Server.insecure(group: group) .withServiceProviders([provider]) .bind(host: "localhost", port: 8088) .get() - - print("server started on port \(server!.channel.localAddress!.port!)") + print("server started on port \(server!.channel.localAddress!.port!)") // Wait on the server's `onClose` future to stop the program from exiting. } catch { print("Unknown server error: \(error)") @@ -152,7 +155,7 @@ public class FlightClientTester { var client: FlightClient? var group: MultiThreadedEventLoopGroup? var channel: GRPCChannel? - + init() async throws { // Load the features. let group = PlatformSupport.makeEventLoopGroup(loopCount: 1) @@ -164,132 +167,135 @@ public class FlightClientTester { client = FlightClient(channel: channel) } - + deinit { try? group?.syncShutdownGracefully() try? channel?.close().wait() } - + func listActionTest() async throws { var actionTypes = [FlightActionType]() - try await client?.listActions( { action in + try await client?.listActions({ action in actionTypes.append(action) }) - + XCTAssertEqual(actionTypes.count, 2) XCTAssertEqual(actionTypes[0].type, "type1") XCTAssertEqual(actionTypes[0].description, "desc1") XCTAssertEqual(actionTypes[1].type, "type2") XCTAssertEqual(actionTypes[1].description, "desc2") } - + func listFlightsTest() async throws { let flightCriteria = FlightCriteria("flight criteria expression".data(using: .utf8)!) - var num_calls = 0 + var numCalls = 0 try await client?.listFlights(flightCriteria, closure: { data in - num_calls += 1 + numCalls += 1 let schema = try streamToArrowSchema(data.schema) XCTAssertEqual(schema.fields.count, 3) }) - - XCTAssertEqual(num_calls, 1) + + XCTAssertEqual(numCalls, 1) } - + func doActionTest() async throws { let action = FlightAction("test_action", body: "test_action body".data(using: .utf8)!) var actionResults = [FlightResult]() try await client?.doAction(action, closure: { result in actionResults.append(result) }) - + XCTAssertEqual(actionResults.count, 1) - XCTAssertEqual(String(bytes:actionResults[0].body, encoding: .utf8), "test_action result") + XCTAssertEqual(String(bytes: actionResults[0].body, encoding: .utf8), "test_action result") } - + func getSchemaTest() async throws { let descriptor = FlightDescriptor(cmd: "schema info".data(using: .utf8)!) let schemaResult = try await client?.getSchema(descriptor) let schema = try streamToArrowSchema(schemaResult!.schema) XCTAssertEqual(schema.fields.count, 3) } - + func doGetTest() async throws { let ticket = FlightTicket("flight_ticket test".data(using: .utf8)!) - var num_call = 0 + var numCall = 0 try await client?.doGet(ticket, readerResultClosure: { rb in - num_call += 1 + numCall += 1 XCTAssertEqual(rb.schema!.fields.count, 3) XCTAssertEqual(rb.batches[0].length, 4) }) - - XCTAssertEqual(num_call, 1) + + XCTAssertEqual(numCall, 1) } - + func doGetTestFlightData() async throws { let ticket = FlightTicket("flight_ticket test".data(using: .utf8)!) - var num_call = 0 + var numCall = 0 try await client?.doGet(ticket, flightDataClosure: { flightData in - let reader = ArrowReader(); + let reader = ArrowReader() let result = reader.fromStream(flightData.dataBody) switch result { case .success(let rb): XCTAssertEqual(rb.schema?.fields.count, 3) XCTAssertEqual(rb.batches[0].length, 4) - num_call += 1 + numCall += 1 case .failure(let error): throw error } }) - - XCTAssertEqual(num_call, 1) + + XCTAssertEqual(numCall, 1) } - + func doPutTest() async throws { let rb = try makeRecordBatch() - var num_call = 0 - try await client?.doPut([rb], closure: { result in - num_call += 1 + var numCall = 0 + try await client?.doPut([rb], closure: { _ in + numCall += 1 }) - - XCTAssertEqual(num_call, 1) + + XCTAssertEqual(numCall, 1) } func doExchangeTest() async throws { let rb = try makeRecordBatch() - var num_call = 0 + var numCall = 0 try await client?.doExchange([rb], closure: { result in - num_call += 1 + numCall += 1 XCTAssertEqual(result.schema?.fields.count, 3) XCTAssertEqual(result.batches[0].length, 4) }) - - XCTAssertEqual(num_call, 1) + + XCTAssertEqual(numCall, 1) } } actor FlightServerData { public var serverup = false - func SetServerUp(_ serverUp: Bool) { + func setServerUp(_ serverUp: Bool) { self.serverup = serverUp } - - func IsServerUp() -> Bool { + + func isServerUp() -> Bool { return serverup } } final class FlightTest: XCTestCase { let serverData = FlightServerData() - + func testFlightServer() async throws { let basicTask = Task { try await FlightServerImpl.run() defer { print("server shutting down") - try! FlightServerImpl.group?.syncShutdownGracefully() + do { + try FlightServerImpl.group?.syncShutdownGracefully() + } catch { + } } - - await serverData.SetServerUp(true) + + await serverData.setServerUp(true) try await FlightServerImpl.server?.onClose.get() return "done" } @@ -298,11 +304,11 @@ final class FlightTest: XCTestCase { defer { _ = FlightServerImpl.server?.close() } - - while await !serverData.IsServerUp() { + + while await !serverData.isServerUp() { try await Task.sleep(nanoseconds: 1_000_000) } - + let clientImpl = try await FlightClientTester() try await clientImpl.listActionTest() try await clientImpl.listFlightsTest() @@ -312,11 +318,10 @@ final class FlightTest: XCTestCase { try await clientImpl.doGetTestFlightData() try await clientImpl.doPutTest() try await clientImpl.doExchangeTest() - return "done" } - - let _ = try await [basicTask.value, secondTask.value] + + _ = try await [basicTask.value, secondTask.value] print("done running") } }