@@ -3,16 +3,18 @@ package org.jetbrains.kotlinx.dataframe.impl
33import org.jetbrains.kotlinx.dataframe.AnyFrame
44import org.jetbrains.kotlinx.dataframe.AnyRow
55import org.jetbrains.kotlinx.dataframe.DataColumn
6- import org.jetbrains.kotlinx.dataframe.DataFrame
7- import org.jetbrains.kotlinx.dataframe.DataRow
6+ import org.jetbrains.kotlinx.dataframe.api.asDataColumn
7+ import org.jetbrains.kotlinx.dataframe.api.cast
88import org.jetbrains.kotlinx.dataframe.api.concat
99import org.jetbrains.kotlinx.dataframe.api.toDataFrame
1010import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnGuessingType
1111import kotlin.reflect.KClass
1212import kotlin.reflect.KType
1313import kotlin.reflect.full.isSubclassOf
14+ import kotlin.reflect.full.isSubtypeOf
1415import kotlin.reflect.full.withNullability
1516import kotlin.reflect.jvm.jvmErasure
17+ import kotlin.reflect.typeOf
1618
1719public interface DataCollector <T > {
1820
@@ -38,17 +40,29 @@ internal abstract class DataCollectorBase<T>(initCapacity: Int) : DataCollector<
3840 data.add(value)
3941 }
4042
41- protected fun createColumn (name : String , type : KType ): DataColumn <T > {
42- val classifier = type.classifier as KClass <* >
43- if (classifier.isSubclassOf(DataFrame ::class ) && ! hasNulls) {
44- return DataColumn .createFrameColumn(name, data as List <AnyFrame >) as DataColumn <T >
45- }
46- if (classifier.isSubclassOf(DataRow ::class ) && ! hasNulls) {
47- val mergedDf = (data as List <AnyRow >).map { it.toDataFrame() }.concat()
48- return DataColumn .createColumnGroup(name, mergedDf) as DataColumn <T >
49- }
50- return DataColumn .createValueColumn(name, data, type.withNullability(hasNulls)) as DataColumn <T >
51- }
43+ @Suppress(" UNCHECKED_CAST" )
44+ protected fun createColumn (name : String , type : KType ): DataColumn <T > =
45+ when {
46+ type == nothingType -> {
47+ require(values.isEmpty()) { " Cannot create non-empty DataColumn of type Nothing" }
48+ DataColumn .empty(name)
49+ }
50+
51+ type == nullableNothingType -> {
52+ require(values.all { it == null }) { " Cannot create DataColumn of type Nothing? with non-null values" }
53+ DataColumn .createValueColumn(name, values, nullableNothingType)
54+ }
55+
56+ type.isSubtypeOf(typeOf<AnyFrame ?>()) && ! hasNulls ->
57+ DataColumn .createFrameColumn(name, data as List <AnyFrame >)
58+
59+ type.isSubtypeOf(typeOf<AnyRow ?>()) && ! hasNulls -> {
60+ val mergedDf = (data as List <AnyRow >).map { it.toDataFrame() }.concat()
61+ DataColumn .createColumnGroup(name, mergedDf).asDataColumn()
62+ }
63+
64+ else -> DataColumn .createValueColumn(name, data, type.withNullability(hasNulls))
65+ }.cast()
5266}
5367
5468internal open class ColumnDataCollector (initCapacity : Int = 0 , val typeOf : (KClass <* >) -> KType ) :
0 commit comments