@@ -42,13 +42,13 @@ public enum ArrowError: Error {
42
42
case invalid( String )
43
43
}
44
44
45
- public enum ArrowTypeId : Sendable {
45
+ public enum ArrowTypeId : Sendable , Equatable {
46
46
case binary
47
47
case boolean
48
48
case date32
49
49
case date64
50
50
case dateType
51
- case decimal128
51
+ case decimal128( _ precision : Int32 , _ scale : Int32 )
52
52
case decimal256
53
53
case dictionary
54
54
case double
@@ -129,6 +129,23 @@ public class ArrowTypeTime64: ArrowType {
129
129
}
130
130
}
131
131
132
+ public class ArrowTypeDecimal128 : ArrowType {
133
+ let precision : Int32
134
+ let scale : Int32
135
+
136
+ public init ( precision: Int32 , scale: Int32 ) {
137
+ self . precision = precision
138
+ self . scale = scale
139
+ super. init ( ArrowType . ArrowDecimal128)
140
+ }
141
+
142
+ public override var cDataFormatId : String {
143
+ get throws {
144
+ return " d: \( precision) , \( scale) "
145
+ }
146
+ }
147
+ }
148
+
132
149
/// @nodoc
133
150
public class ArrowNestedType : ArrowType {
134
151
let fields : [ ArrowField ]
@@ -156,6 +173,7 @@ public class ArrowType {
156
173
public static let ArrowBool = Info . primitiveInfo ( ArrowTypeId . boolean)
157
174
public static let ArrowDate32 = Info . primitiveInfo ( ArrowTypeId . date32)
158
175
public static let ArrowDate64 = Info . primitiveInfo ( ArrowTypeId . date64)
176
+ public static let ArrowDecimal128 = Info . primitiveInfo ( ArrowTypeId . decimal128 ( 38 , 18 ) )
159
177
public static let ArrowBinary = Info . variableInfo ( ArrowTypeId . binary)
160
178
public static let ArrowTime32 = Info . timeInfo ( ArrowTypeId . time32)
161
179
public static let ArrowTime64 = Info . timeInfo ( ArrowTypeId . time64)
@@ -216,6 +234,8 @@ public class ArrowType {
216
234
return ArrowType . ArrowFloat
217
235
} else if type == Double . self {
218
236
return ArrowType . ArrowDouble
237
+ } else if type == Decimal . self {
238
+ return ArrowType . ArrowDecimal128
219
239
} else {
220
240
return ArrowType . ArrowUnknown
221
241
}
@@ -242,6 +262,8 @@ public class ArrowType {
242
262
return ArrowType . ArrowFloat
243
263
} else if type == Double . self {
244
264
return ArrowType . ArrowDouble
265
+ } else if type == Decimal . self {
266
+ return ArrowType . ArrowDecimal128
245
267
} else {
246
268
return ArrowType . ArrowUnknown
247
269
}
@@ -271,6 +293,8 @@ public class ArrowType {
271
293
return MemoryLayout< Float> . stride
272
294
case . double:
273
295
return MemoryLayout< Double> . stride
296
+ case . decimal128:
297
+ return 16 // Decimal 128 (= 16 * 8) bits
274
298
case . boolean:
275
299
return MemoryLayout< Bool> . stride
276
300
case . date32:
@@ -315,6 +339,8 @@ public class ArrowType {
315
339
return " f "
316
340
case ArrowTypeId . double:
317
341
return " g "
342
+ case ArrowTypeId . decimal128( let precision, let scale) :
343
+ return " d: \( precision) , \( scale) "
318
344
case ArrowTypeId . boolean:
319
345
return " b "
320
346
case ArrowTypeId . date32:
@@ -344,6 +370,7 @@ public class ArrowType {
344
370
public static func fromCDataFormatId( // swiftlint:disable:this cyclomatic_complexity
345
371
_ from: String
346
372
) throws -> ArrowType {
373
+ let REGEX_DECIMAL_TYPE = /^ d: ( \d + ) , ( \d + ) $/
347
374
if from == " c " {
348
375
return ArrowType ( ArrowType . ArrowInt8)
349
376
} else if from == " s " {
@@ -364,6 +391,10 @@ public class ArrowType {
364
391
return ArrowType ( ArrowType . ArrowFloat)
365
392
} else if from == " g " {
366
393
return ArrowType ( ArrowType . ArrowDouble)
394
+ } else if from. contains ( REGEX_DECIMAL_TYPE) {
395
+ let match = from. firstMatch ( of: REGEX_DECIMAL_TYPE) !
396
+ let decimalType = ArrowTypeId . decimal128 ( Int32 ( match. 1 ) !, Int32 ( match. 2 ) !)
397
+ return ArrowType ( Info . primitiveInfo ( decimalType) )
367
398
} else if from == " b " {
368
399
return ArrowType ( ArrowType . ArrowBool)
369
400
} else if from == " tdD " {
0 commit comments