Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-41804: [Swift] Add Struct (Nested) type #43082

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions swift/Arrow/Sources/Arrow/ArrowData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class ArrowData {
if typeId == ArrowTypeId.unknown {
throw ArrowError.unknownType("Unknown time type for data")
}
case let .complexInfo(typeId):
if typeId == ArrowTypeId.unknown {
throw ArrowError.unknownType("Unknown complex type for data")
}
}

self.type = arrowType
Expand Down
50 changes: 50 additions & 0 deletions swift/Arrow/Sources/Arrow/ArrowType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public class ArrowTypeTime64: ArrowType {
}
}

public class ArrowNestedType: ArrowType {
let fields: [ArrowField]
public init(_ info: ArrowType.Info, fields: [ArrowField]) {
self.fields = fields
super.init(info)
}
}

public class ArrowType {
public private(set) var info: ArrowType.Info
public static let ArrowInt8 = Info.primitiveInfo(ArrowTypeId.int8)
Expand All @@ -142,6 +150,7 @@ public class ArrowType {
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 ArrowStruct = Info.complexInfo(ArrowTypeId.strct)

public init(_ info: ArrowType.Info) {
self.info = info
Expand All @@ -155,13 +164,51 @@ public class ArrowType {
return id
case .variableInfo(let id):
return id
case .complexInfo(let id):
return id
}
}

public enum Info {
case primitiveInfo(ArrowTypeId)
case variableInfo(ArrowTypeId)
case timeInfo(ArrowTypeId)
case complexInfo(ArrowTypeId)
}

public static func infoForType( // swiftlint:disable:this cyclomatic_complexity
_ type: Any.Type) -> ArrowType.Info {
if type == String.self {
return ArrowType.ArrowString
} else if type == Date.self {
return ArrowType.ArrowDate64
} else if type == Bool.self {
return ArrowType.ArrowBool
} else if type == Data.self {
return ArrowType.ArrowBinary
} else if type == Int8.self {
return ArrowType.ArrowInt8
} else if type == Int16.self {
return ArrowType.ArrowInt16
} else if type == Int32.self {
return ArrowType.ArrowInt32
} else if type == Int64.self {
return ArrowType.ArrowInt64
} else if type == UInt8.self {
return ArrowType.ArrowUInt8
} else if type == UInt16.self {
return ArrowType.ArrowUInt16
} else if type == UInt32.self {
return ArrowType.ArrowUInt32
} else if type == UInt64.self {
return ArrowType.ArrowUInt64
} else if type == Float.self {
return ArrowType.ArrowFloat
} else if type == Double.self {
return ArrowType.ArrowDouble
} else {
return ArrowType.ArrowUnknown
}
}

public static func infoForNumericType<T>(_ type: T.Type) -> ArrowType.Info {
Expand Down Expand Up @@ -227,6 +274,8 @@ public class ArrowType {
return MemoryLayout<Int8>.stride
case .string:
return MemoryLayout<Int8>.stride
case .strct:
return 0
default:
fatalError("Stride requested for unknown type: \(self)")
}
Expand Down Expand Up @@ -351,3 +400,4 @@ func getBytesFor<T>(_ data: T) -> Data? {
return nil
}
}
// swiftlint:disable:this file_length
Loading