@@ -65,6 +65,7 @@ import java.nio.channels.WritableByteChannel
65
65
import java.time.LocalDate
66
66
import java.time.LocalDateTime
67
67
import java.time.LocalTime
68
+ import kotlin.reflect.full.isSubtypeOf
68
69
import kotlin.reflect.typeOf
69
70
70
71
private val writeWarningMessage: (String ) -> Unit = {message: String -> System .err.println (message)}
@@ -75,39 +76,30 @@ private val writeWarningMessage: (String) -> Unit = {message: String -> System.e
75
76
*/
76
77
public fun List<AnyCol>.toArrowSchema (warningSubscriber : (String ) -> Unit = writeWarningMessage): Schema {
77
78
val fields = this .map { column ->
78
- when (column.type()) {
79
- typeOf<String ?>() -> Field (column.name(), FieldType (true , ArrowType .Utf8 (), null ), emptyList())
80
- typeOf<String >() -> Field (column.name(), FieldType (false , ArrowType .Utf8 (), null ), emptyList())
79
+ val columnType = column.type()
80
+ val nullable = columnType.isMarkedNullable
81
+ when {
82
+ columnType.isSubtypeOf(typeOf<String ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Utf8 (), null ), emptyList())
81
83
82
- typeOf<Boolean ?>() -> Field (column.name(), FieldType (true , ArrowType .Bool (), null ), emptyList())
83
- typeOf<Boolean >() -> Field (column.name(), FieldType (false , ArrowType .Bool (), null ), emptyList())
84
+ columnType.isSubtypeOf(typeOf<Boolean ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Bool (), null ), emptyList())
84
85
85
- typeOf<Byte ?>() -> Field (column.name(), FieldType (true , ArrowType .Int (8 , true ), null ), emptyList())
86
- typeOf<Byte >() -> Field (column.name(), FieldType (false , ArrowType .Int (8 , true ), null ), emptyList())
86
+ columnType.isSubtypeOf(typeOf<Byte ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Int (8 , true ), null ), emptyList())
87
87
88
- typeOf<Short ?>() -> Field (column.name(), FieldType (true , ArrowType .Int (16 , true ), null ), emptyList())
89
- typeOf<Short >() -> Field (column.name(), FieldType (false , ArrowType .Int (16 , true ), null ), emptyList())
88
+ columnType.isSubtypeOf(typeOf<Short ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Int (16 , true ), null ), emptyList())
90
89
91
- typeOf<Int ?>() -> Field (column.name(), FieldType (true , ArrowType .Int (32 , true ), null ), emptyList())
92
- typeOf<Int >() -> Field (column.name(), FieldType (false , ArrowType .Int (32 , true ), null ), emptyList())
90
+ columnType.isSubtypeOf(typeOf<Int ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Int (32 , true ), null ), emptyList())
93
91
94
- typeOf<Long ?>() -> Field (column.name(), FieldType (true , ArrowType .Int (64 , true ), null ), emptyList())
95
- typeOf<Long >() -> Field (column.name(), FieldType (false , ArrowType .Int (64 , true ), null ), emptyList())
92
+ columnType.isSubtypeOf(typeOf<Long ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Int (64 , true ), null ), emptyList())
96
93
97
- typeOf<Float ?>() -> Field (column.name(), FieldType (true , ArrowType .FloatingPoint (FloatingPointPrecision .SINGLE ), null ), emptyList())
98
- typeOf<Float >() -> Field (column.name(), FieldType (false , ArrowType .FloatingPoint (FloatingPointPrecision .SINGLE ), null ), emptyList())
94
+ columnType.isSubtypeOf(typeOf<Float ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .FloatingPoint (FloatingPointPrecision .SINGLE ), null ), emptyList())
99
95
100
- typeOf<Double ?>() -> Field (column.name(), FieldType (true , ArrowType .FloatingPoint (FloatingPointPrecision .DOUBLE ), null ), emptyList())
101
- typeOf<Double >() -> Field (column.name(), FieldType (false , ArrowType .FloatingPoint (FloatingPointPrecision .DOUBLE ), null ), emptyList())
96
+ columnType.isSubtypeOf(typeOf<Double ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .FloatingPoint (FloatingPointPrecision .DOUBLE ), null ), emptyList())
102
97
103
- typeOf<LocalDate ?>(), typeOf< kotlinx.datetime.LocalDate ? > () -> Field (column.name(), FieldType (true , ArrowType .Date (DateUnit .DAY ), null ), emptyList())
104
- typeOf<LocalDate >(), typeOf< kotlinx.datetime.LocalDate > () -> Field (column.name(), FieldType (false , ArrowType .Date (DateUnit .DAY ), null ), emptyList())
98
+ columnType.isSubtypeOf(typeOf<LocalDate ?>()) || columnType.isSubtypeOf(typeOf< kotlinx.datetime.LocalDate ? > ()) -> Field (column.name(), FieldType (nullable, ArrowType .Date (DateUnit .DAY ), null ), emptyList())
105
99
106
- typeOf<LocalDateTime ?>(), typeOf< kotlinx.datetime.LocalDateTime ? > () -> Field (column.name(), FieldType (true , ArrowType .Date (DateUnit .MILLISECOND ), null ), emptyList())
107
- typeOf<LocalDateTime >(), typeOf< kotlinx.datetime.LocalDateTime > () -> Field (column.name(), FieldType (false , ArrowType .Date (DateUnit .MILLISECOND ), null ), emptyList())
100
+ columnType.isSubtypeOf(typeOf<LocalDateTime ?>()) || columnType.isSubtypeOf(typeOf< kotlinx.datetime.LocalDateTime ? > ()) -> Field (column.name(), FieldType (nullable, ArrowType .Date (DateUnit .MILLISECOND ), null ), emptyList())
108
101
109
- typeOf<LocalTime ?>() -> Field (column.name(), FieldType (true , ArrowType .Time (TimeUnit .NANOSECOND , 64 ), null ), emptyList())
110
- typeOf<LocalTime >() -> Field (column.name(), FieldType (false , ArrowType .Time (TimeUnit .NANOSECOND , 64 ), null ), emptyList())
102
+ columnType.isSubtypeOf(typeOf<LocalTime ?>()) -> Field (column.name(), FieldType (nullable, ArrowType .Time (TimeUnit .NANOSECOND , 64 ), null ), emptyList())
111
103
112
104
else -> {
113
105
warningSubscriber(" Column ${column.name()} has type ${column.typeClass.java.canonicalName} , will be saved as String" )
0 commit comments