Skip to content

Commit 3d7abab

Browse files
Automated commit of generated code
1 parent 7ff578a commit 3d7abab

File tree

9 files changed

+65
-29
lines changed

9 files changed

+65
-29
lines changed

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/TypeUtils.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,19 @@ internal fun commonParent(vararg classes: KClass<*>): KClass<*>? = commonParent(
197197
internal fun Iterable<KClass<*>>.withMostSuperclasses(): KClass<*>? = maxByOrNull { it.allSuperclasses.size }
198198

199199
internal fun Iterable<KClass<*>>.createType(nullable: Boolean, upperBound: KType? = null): KType =
200-
if (upperBound == null) {
201-
(withMostSuperclasses() ?: Any::class).createStarProjectedType(nullable)
202-
} else {
203-
val upperClass = upperBound.classifier as KClass<*>
204-
val baseClass = filter { it.isSubclassOf(upperClass) }.withMostSuperclasses() ?: withMostSuperclasses()
205-
if (baseClass == null) {
206-
upperBound.withNullability(nullable)
207-
} else {
208-
upperBound.projectTo(baseClass).withNullability(nullable)
200+
when {
201+
!iterator().hasNext() -> upperBound?.withNullability(nullable) ?: nothingType(nullable)
202+
203+
upperBound == null -> (withMostSuperclasses() ?: Any::class).createStarProjectedType(nullable)
204+
205+
else -> {
206+
val upperClass = upperBound.classifier as KClass<*>
207+
val baseClass = filter { it.isSubclassOf(upperClass) }.withMostSuperclasses() ?: withMostSuperclasses()
208+
if (baseClass == null) {
209+
upperBound.withNullability(nullable)
210+
} else {
211+
upperBound.projectTo(baseClass).withNullability(nullable)
212+
}
209213
}
210214
}
211215

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/describe.kt

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import org.jetbrains.kotlinx.dataframe.columns.size
2525
import org.jetbrains.kotlinx.dataframe.columns.values
2626
import org.jetbrains.kotlinx.dataframe.impl.columns.addPath
2727
import org.jetbrains.kotlinx.dataframe.impl.columns.asAnyFrameColumn
28+
import org.jetbrains.kotlinx.dataframe.impl.renderType
2829
import org.jetbrains.kotlinx.dataframe.index
2930
import org.jetbrains.kotlinx.dataframe.kind
3031
import org.jetbrains.kotlinx.dataframe.type
31-
import kotlin.reflect.jvm.jvmErasure
3232

3333
internal fun describeImpl(cols: List<AnyCol>): DataFrame<ColumnDescription> {
3434
fun List<AnyCol>.collectAll(atAnyDepth: Boolean): List<AnyCol> =
@@ -65,7 +65,7 @@ internal fun describeImpl(cols: List<AnyCol>): DataFrame<ColumnDescription> {
6565
if (hasLongPaths) {
6666
ColumnDescription::path from { it.path() }
6767
}
68-
ColumnDescription::type from { buildTypeName(it) }
68+
ColumnDescription::type from { renderType(it.type) }
6969
ColumnDescription::count from { it.size }
7070
ColumnDescription::unique from { it.countDistinct() }
7171
ColumnDescription::nulls from { it.values.count { it == null } }
@@ -94,12 +94,3 @@ internal fun describeImpl(cols: List<AnyCol>): DataFrame<ColumnDescription> {
9494

9595
return df.cast()
9696
}
97-
98-
private fun buildTypeName(it: AnyCol): String {
99-
val rawJavaType = it.type.jvmErasure.simpleName.toString()
100-
return if (it.type.isMarkedNullable) {
101-
"$rawJavaType?"
102-
} else {
103-
rawJavaType
104-
}
105-
}

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/mean.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jetbrains.kotlinx.dataframe.math
22

33
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
4+
import org.jetbrains.kotlinx.dataframe.impl.renderType
45
import java.math.BigDecimal
56
import kotlin.reflect.KType
67
import kotlin.reflect.full.withNullability
@@ -31,7 +32,10 @@ internal fun <T : Number> Sequence<T>.mean(type: KType, skipNA: Boolean = skipNA
3132

3233
Number::class -> (this as Sequence<Number>).map { it.toDouble() }.mean(skipNA)
3334

34-
else -> throw IllegalArgumentException("Unable to compute mean for type $type")
35+
// this means the sequence is empty
36+
Nothing::class -> Double.NaN
37+
38+
else -> throw IllegalArgumentException("Unable to compute the mean for type ${renderType(type)}")
3539
}
3640
}
3741

core/generated-sources/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math/std.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.jetbrains.kotlinx.dataframe.math
22

33
import org.jetbrains.kotlinx.dataframe.api.ddof_default
44
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
5+
import org.jetbrains.kotlinx.dataframe.impl.renderType
56
import java.math.BigDecimal
67
import kotlin.reflect.KType
78
import kotlin.reflect.full.withNullability
@@ -13,11 +14,10 @@ internal fun <T : Number> Iterable<T?>.std(
1314
ddof: Int = ddof_default,
1415
): Double {
1516
if (type.isMarkedNullable) {
16-
if (skipNA) {
17-
return filterNotNull().std(type.withNullability(false), true, ddof)
18-
} else {
19-
if (contains(null)) return Double.NaN
20-
return std(type.withNullability(false), skipNA, ddof)
17+
return when {
18+
skipNA -> filterNotNull().std(type = type.withNullability(false), skipNA = true, ddof = ddof)
19+
contains(null) -> Double.NaN
20+
else -> std(type = type.withNullability(false), skipNA = false, ddof = ddof)
2121
}
2222
}
2323
return when (type.classifier) {
@@ -26,7 +26,8 @@ internal fun <T : Number> Iterable<T?>.std(
2626
Int::class, Short::class, Byte::class -> (this as Iterable<Int>).std(ddof)
2727
Long::class -> (this as Iterable<Long>).std(ddof)
2828
BigDecimal::class -> (this as Iterable<BigDecimal>).std(ddof)
29-
else -> throw IllegalArgumentException("Unsupported type ${type.classifier}")
29+
Nothing::class -> Double.NaN
30+
else -> throw IllegalArgumentException("Unable to compute the std for type ${renderType(type)}")
3031
}
3132
}
3233

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/constructors.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.jetbrains.kotlinx.dataframe.api
22

33
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.impl.nothingType
5+
import org.jetbrains.kotlinx.dataframe.type
46
import org.junit.Test
57

68
class ConstructorsTests {
@@ -24,4 +26,10 @@ class ConstructorsTests {
2426
df.columnsCount() shouldBe 2
2527
df.columnNames() shouldBe listOf(column.name(), "${column.name()}1")
2628
}
29+
30+
@Test
31+
fun `dataFrameOf with nothing columns`() {
32+
dataFrameOf("a" to emptyList())["a"].type shouldBe nothingType(false)
33+
dataFrameOf("a" to listOf(null))["a"].type shouldBe nothingType(true)
34+
}
2735
}

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/statistics/BasicMathTests.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.jetbrains.kotlinx.dataframe.statistics
22

33
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.DataColumn
45
import org.jetbrains.kotlinx.dataframe.api.columnOf
56
import org.jetbrains.kotlinx.dataframe.api.mean
7+
import org.jetbrains.kotlinx.dataframe.impl.nothingType
68
import org.junit.Test
79
import kotlin.reflect.typeOf
810

@@ -18,5 +20,8 @@ class BasicMathTests {
1820
fun `mean with nans and nulls`() {
1921
columnOf(10, 20, Double.NaN, null).mean() shouldBe Double.NaN
2022
columnOf(10, 20, Double.NaN, null).mean(skipNA = true) shouldBe 15
23+
24+
DataColumn.createValueColumn("", emptyList<Nothing>(), nothingType(false)).mean() shouldBe Double.NaN
25+
DataColumn.createValueColumn("", listOf(null), nothingType(true)).mean() shouldBe Double.NaN
2126
}
2227
}

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/statistics/std.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.jetbrains.kotlinx.dataframe.statistics
22

33
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.DataColumn
45
import org.jetbrains.kotlinx.dataframe.api.columnOf
56
import org.jetbrains.kotlinx.dataframe.api.columnTypes
67
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
78
import org.jetbrains.kotlinx.dataframe.api.std
9+
import org.jetbrains.kotlinx.dataframe.impl.nothingType
810
import org.jetbrains.kotlinx.dataframe.math.std
11+
import org.jetbrains.kotlinx.dataframe.type
912
import org.junit.Test
1013
import kotlin.reflect.typeOf
1114

@@ -37,4 +40,16 @@ class StdTests {
3740
df[value].std() shouldBe expected
3841
df.std { value } shouldBe expected
3942
}
43+
44+
@Test
45+
fun `std on empty or nullable column`() {
46+
val empty = DataColumn.createValueColumn("", emptyList<Nothing>(), nothingType(false))
47+
val nullable = DataColumn.createValueColumn("", listOf(null), nothingType(true))
48+
49+
empty.values().std(empty.type) shouldBe Double.NaN
50+
nullable.values().std(nullable.type) shouldBe Double.NaN
51+
52+
empty.std() shouldBe Double.NaN
53+
nullable.std() shouldBe Double.NaN
54+
}
4055
}

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/testSets/person/DataFrameTests.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.kotest.matchers.shouldBe
77
import io.kotest.matchers.shouldNotBe
88
import org.jetbrains.kotlinx.dataframe.AnyFrame
99
import org.jetbrains.kotlinx.dataframe.AnyRow
10+
import org.jetbrains.kotlinx.dataframe.DataColumn
1011
import org.jetbrains.kotlinx.dataframe.DataFrame
1112
import org.jetbrains.kotlinx.dataframe.DataRow
1213
import org.jetbrains.kotlinx.dataframe.RowExpression
@@ -2196,6 +2197,9 @@ class DataFrameTests : BaseTest() {
21962197
fun `isNumber`() {
21972198
typed.age.isNumber() shouldBe true
21982199
typed.weight.isNumber() shouldBe true
2200+
2201+
DataColumn.createValueColumn("a", emptyList<Nothing>(), nothingType(false)).isNumber() shouldBe true
2202+
DataColumn.createValueColumn("a", listOf(null), nothingType(true)).isNumber() shouldBe true
21992203
}
22002204

22012205
@Test

core/generated-sources/src/test/kotlin/org/jetbrains/kotlinx/dataframe/types/UtilTests.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class UtilTests {
9797

9898
@Test
9999
fun `createType test`() {
100-
emptyList<KClass<*>>().createType(nullable = false) shouldBe typeOf<Any>()
101-
emptyList<KClass<*>>().createType(nullable = true) shouldBe typeOf<Any?>()
100+
emptyList<KClass<*>>().createType(nullable = false) shouldBe nothingType(nullable = false)
101+
emptyList<KClass<*>>().createType(nullable = true) shouldBe nothingType(nullable = true)
102102

103103
listOf(Nothing::class).createType(nullable = false) shouldBe nothingType(nullable = false)
104104
listOf(Nothing::class).createType(nullable = true) shouldBe nothingType(nullable = true)
@@ -111,6 +111,9 @@ class UtilTests {
111111

112112
listOf(Nothing::class).commonType(false) shouldBe nothingType(nullable = false)
113113
listOf(Nothing::class).commonType(true) shouldBe nothingType(nullable = true)
114+
115+
emptyList<KClass<*>>().commonType(false, null) shouldBe nothingType(nullable = false)
116+
emptyList<KClass<*>>().commonType(true, null) shouldBe nothingType(nullable = true)
114117
}
115118

116119
val a = listOf(1, 2.0, "a")
@@ -133,6 +136,7 @@ class UtilTests {
133136
guessValueType(sequenceOf(1, 2.0, "a", null, listOf(1, 2))) shouldBe typeOf<Any?>()
134137

135138
guessValueType(sequenceOf(null, null)) shouldBe nothingType(nullable = true)
139+
guessValueType(emptySequence()) shouldBe nothingType(nullable = false)
136140

137141
guessValueType(sequenceOf(listOf<Int?>(null))) shouldBe typeOf<List<Nothing?>>()
138142
guessValueType(sequenceOf(emptyList<Int>())) shouldBe typeOf<List<Nothing>>()

0 commit comments

Comments
 (0)