Skip to content

Commit

Permalink
apacheGH-38131: [Swift][CI] Add linting and fix linting errors (apach…
Browse files Browse the repository at this point in the history
…e#38133)

Description:

- Added swiftlint to swift CI job: swift_test.sh.
- Fixed swiftlint errors in swift files.
* Closes: apache#38131

Lead-authored-by: Alva Bandy <[email protected]>
Co-authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
2 people authored and loicalleyne committed Nov 13, 2023
1 parent db15b29 commit 8a58751
Show file tree
Hide file tree
Showing 39 changed files with 906 additions and 798 deletions.
16 changes: 13 additions & 3 deletions ci/docker/ubuntu-swift.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 7 additions & 1 deletion ci/scripts/swift_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions swift/.swiftlint.yml
Original file line number Diff line number Diff line change
@@ -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
165 changes: 78 additions & 87 deletions swift/Arrow/Sources/Arrow/ArrowArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -48,20 +48,22 @@ public class ArrowArrayHolder {

return bufferDataSizes
}

self.getArrowColumn = {(field: ArrowField, arrayHolders: [ArrowArrayHolder]) throws -> ArrowColumn in
var arrays = [ArrowArray<T>]()
for arrayHolder in arrayHolders {
if let array = arrayHolder.array as? ArrowArray<T> {
arrays.append(array)
}
}

return ArrowColumn(field, chunked: ChunkedArrayHolder(try ChunkedArray<T>(arrays)))
}
}

public static func makeArrowColumn(_ field: ArrowField, holders: [ArrowArrayHolder]) -> Result<ArrowColumn, ArrowError> {

public static func makeArrowColumn(_ field: ArrowField,
holders: [ArrowArrayHolder]
) -> Result<ArrowColumn, ArrowError> {
do {
return .success(try holders[0].getArrowColumn(field, holders))
} catch {
Expand All @@ -73,9 +75,9 @@ public class ArrowArrayHolder {
public class ArrowArray<T>: 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
}
Expand All @@ -84,102 +86,92 @@ public class ArrowArray<T>: 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<T>: ArrowArray<T> {
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<String> {
public override subscript(_ index: UInt) -> String? {
get {
let offsetIndex = MemoryLayout<Int32>.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<Int32>.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<Int32>.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<UInt8>(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<Int32>.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<UInt8>(start: rawPointer, count: arrayLength)
let byteArray = Array(buffer)
return String(data: Data(byteArray), encoding: .utf8)
}
}

public class BoolArray: ArrowArray<Bool> {
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<Date> {
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<Date> {
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))
}
}

Expand All @@ -189,40 +181,39 @@ public class Time64Array: FixedArray<Time64> {}
public class BinaryArray: ArrowArray<Data> {
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<Int32>.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<Int32>.stride * Int(index)
if self.arrowData.isNull(index) {
return nil
}

let endIndex = offsets.rawPointer.advanced(by: offsetIndex + MemoryLayout<Int32>.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<UInt8>(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<Int32>.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<UInt8>(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()
Expand Down
Loading

0 comments on commit 8a58751

Please sign in to comment.