Skip to content

Commit e1b2652

Browse files
committed
renamed List<Boolean>.toIndices() to getTrueIndices to better reflect its functionality. Added reference to type inference issue with TODO in commonType tests. Ran korro
1 parent 6be5e1a commit e1b2652

File tree

140 files changed

+1781
-1733
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+1781
-1733
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
66
import org.jetbrains.kotlinx.dataframe.Predicate
77
import org.jetbrains.kotlinx.dataframe.RowFilter
88
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
9-
import org.jetbrains.kotlinx.dataframe.impl.toIndices
9+
import org.jetbrains.kotlinx.dataframe.impl.getTrueIndices
1010
import org.jetbrains.kotlinx.dataframe.indices
1111
import kotlin.reflect.KProperty
1212

@@ -27,7 +27,7 @@ public fun <T> DataFrame<T>.filter(predicate: RowFilter<T>): DataFrame<T> =
2727
}.let { get(it) }
2828

2929
public fun <T> DataFrame<T>.filterBy(column: ColumnSelector<T, Boolean>): DataFrame<T> =
30-
getRows(getColumn(column).toList().toIndices())
30+
getRows(getColumn(column).toList().getTrueIndices())
3131

3232
public fun <T> DataFrame<T>.filterBy(column: String): DataFrame<T> = filterBy { column.toColumnOf() }
3333

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ internal fun <T : Any> convert(src: Int, tartypeOf: KClass<T>): T = when (tartyp
6868
else -> throw NotImplementedError("Casting int to $tartypeOf is not supported")
6969
}
7070

71-
internal fun BooleanArray.toIndices(): List<Int> {
71+
internal fun BooleanArray.getTrueIndices(): List<Int> {
7272
val res = ArrayList<Int>(size)
73-
for (i in 0 until size)
73+
for (i in indices)
7474
if (this[i]) res.add(i)
7575
return res
7676
}
7777

78-
internal fun List<Boolean>.toIndices(): List<Int> {
78+
internal fun List<Boolean>.getTrueIndices(): List<Int> {
7979
val res = ArrayList<Int>(size)
80-
for (i in 0 until size)
80+
for (i in indices)
8181
if (this[i]) res.add(i)
8282
return res
8383
}
@@ -148,15 +148,18 @@ internal fun Iterable<KType?>.commonType(useStar: Boolean = true): KType {
148148
return when {
149149
distinct.isEmpty() || distinct.contains(null) -> typeOf<Any>().withNullability(nullable)
150150
distinct.size == 1 -> distinct.single()!!
151+
151152
else -> {
152153
// common parent class of all KTypes
153154
val kClass = commonParent(distinct.map { it!!.jvmErasure })
154155
?: return typeOf<Any>().withNullability(nullable)
155-
// all KTypes projected to the common parent class with erased generic type parameters (no <T>
156+
157+
// all KTypes projected to the common parent class with filled-in generic type parameters (no <T>, but <UpperBound>)
156158
val projections = distinct
157159
.map { it!!.projectUpTo(kClass).replaceGenericTypeParametersWithUpperbound() }
158160
require(projections.all { it.jvmErasure == kClass })
159161

162+
// make new type arguments for the common parent class
160163
val arguments = List(kClass.typeParameters.size) { i ->
161164
val typeParameter = kClass.typeParameters[i]
162165
val projectionTypes = projections

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
2424
import org.jetbrains.kotlinx.dataframe.api.indices
2525
import org.jetbrains.kotlinx.dataframe.api.toColumnOf
2626
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
27-
import org.jetbrains.kotlinx.dataframe.columns.*
27+
import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
28+
import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
29+
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
30+
import org.jetbrains.kotlinx.dataframe.columns.toColumnsSetOf
2831
import org.jetbrains.kotlinx.dataframe.impl.DataFrameReceiver
2932
import org.jetbrains.kotlinx.dataframe.impl.DataRowImpl
3033
import org.jetbrains.kotlinx.dataframe.impl.asList
31-
import org.jetbrains.kotlinx.dataframe.impl.replaceGenericTypeParametersWithUpperbound
3234
import org.jetbrains.kotlinx.dataframe.impl.guessValueType
35+
import org.jetbrains.kotlinx.dataframe.impl.replaceGenericTypeParametersWithUpperbound
3336
import org.jetbrains.kotlinx.dataframe.index
3437
import org.jetbrains.kotlinx.dataframe.nrow
3538
import kotlin.reflect.KClass

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

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.jetbrains.kotlinx.dataframe.types
22

33
import io.kotest.matchers.shouldBe
4-
import org.jetbrains.kotlinx.dataframe.DataFrame
54
import org.jetbrains.kotlinx.dataframe.impl.commonParent
65
import org.jetbrains.kotlinx.dataframe.impl.commonParents
76
import org.jetbrains.kotlinx.dataframe.impl.commonType
@@ -165,11 +164,6 @@ class UtilTests {
165164
) shouldBe typeOf<Collection<Nothing?>?>()
166165
}
167166

168-
class TypeOut<out T>
169-
class TypeIn<in T>
170-
class Type<T>
171-
172-
173167
interface UpperBound
174168
interface TypeWithUpperbound1<T : UpperBound>
175169
interface TestType1<T : UpperBound> : TypeWithUpperbound1<T>
@@ -182,34 +176,73 @@ class UtilTests {
182176

183177
val comparableTypeT = TestTypeIn1::class.supertypes.first() // Comparable<T>
184178
comparableTypeT.replaceGenericTypeParametersWithUpperbound() shouldBe typeOf<Comparable<Nothing>>()
185-
186-
187-
val a1: List<TypeOut<Any>> = listOf(TypeOut<Int>(), TypeOut<DataFrame<*>>())
188-
// val a1_: List<Type1<Nothing>> = listOf(Type1<Int>(), Type1<DataFrame<*>>())
189-
val a1__: List<TypeOut<*>> = listOf(TypeOut<Int>(), TypeOut<DataFrame<*>>())
190-
val a1___: List<TypeOut<out Any>> = listOf(TypeOut<Int>(), TypeOut<DataFrame<*>>())
191-
192-
// val a2: List<Type2<Any>> = listOf(Type2<Int>(), Type2<DataFrame<*>>())
193-
val a2_: List<TypeIn<Nothing>> = listOf(TypeIn<Int>(), TypeIn<DataFrame<*>>())
194-
val a2__: List<TypeIn<*>> = listOf(TypeIn<Int>(), TypeIn<DataFrame<*>>())
195-
val a2___: List<TypeIn<in Nothing>> = listOf(TypeIn<Int>(), TypeIn<DataFrame<*>>())
196-
197-
val a3: List<Type<out Any>> = listOf(Type<Int>(), Type<DataFrame<*>>())
198-
val a3_: List<Type<in Nothing>> = listOf(Type<Int>(), Type<DataFrame<*>>())
199-
val a3__: List<Type<*>> = listOf(Type<Int>(), Type<DataFrame<*>>())
200179
}
201180

181+
interface AbstractType<T>
182+
202183
@Test
203184
fun `commonType KTypes test`() {
185+
// TODO issue #471: Type inference incorrect w.r.t. variance
186+
listOf(null).commonType(false) shouldBe typeOf<Any?>()
187+
// listOf(null).commonType(true) shouldBe null
188+
listOf(typeOf<Int>(), typeOf<Any>()).commonType() shouldBe typeOf<Any>()
204189
listOf(typeOf<Int>(), typeOf<List<Any>>()).commonType() shouldBe typeOf<Any>()
205190
listOf(typeOf<Int>(), typeOf<List<Any>?>()).commonType() shouldBe typeOf<Any?>()
206191
listOf(typeOf<Int>(), typeOf<Int>()).commonType() shouldBe typeOf<Int>()
207192
listOf(typeOf<Int>(), typeOf<Int?>()).commonType() shouldBe typeOf<Int?>()
208193
listOf(typeOf<Int>(), nothingType(true)).commonType() shouldBe typeOf<Int?>()
209194
listOf(typeOf<Int>(), nothingType(false)).commonType() shouldBe typeOf<Int>()
210-
listOf(typeOf<List<Int>>(), typeOf<List<String>>()).commonType(false) shouldBe typeOf<List<out Comparable<Nothing>>>()
195+
listOf(typeOf<Comparable<Int>>(), typeOf<Comparable<Int>>()).commonType() shouldBe typeOf<Comparable<Int>>()
196+
// listOf(typeOf<Comparable<Int>>(), typeOf<Comparable<String>>()).commonType() shouldBe typeOf<Comparable<*>>()
197+
// listOf(typeOf<List<Int>>(), typeOf<List<String>>()).commonType(false) shouldBe typeOf<List<Comparable<Nothing>>>()
198+
// listOf(typeOf<List<Int>>(), typeOf<List<String>>()).commonType() shouldBe typeOf<List<Comparable<*>>>()
199+
// listOf(typeOf<List<Int>>(), typeOf<Set<String>>()).commonType(false) shouldBe typeOf<Collection<Comparable<Nothing>>>()
200+
// listOf(typeOf<List<Int>>(), typeOf<Set<String>>()).commonType() shouldBe typeOf<Collection<Comparable<*>>>()
201+
// listOf(typeOf<List<Int>>(), typeOf<List<List<Any>>>()).commonType() shouldBe typeOf<List<Any>>()
202+
// listOf(typeOf<List<Int>>(), typeOf<List<List<Any>?>>()).commonType(false) shouldBe typeOf<List<Any?>>()
203+
// listOf(typeOf<List<Int>>(), typeOf<List<List<Any>?>>()).commonType() shouldBe typeOf<List<*>>()
204+
// listOf(typeOf<List<Nothing>>(), typeOf<Set<Nothing>>()).commonType() shouldBe typeOf<Collection<Nothing>>()
205+
listOf(nothingType(false)).commonType() shouldBe nothingType(false)
206+
listOf(nothingType(true)).commonType() shouldBe nothingType(true)
207+
// emptyList<KType>().commonType() shouldBe nothingType(false)
208+
listOf(
209+
typeOf<AbstractType<Int>>(),
210+
typeOf<AbstractType<Int>>()
211+
).commonType() shouldBe typeOf<AbstractType<Int>>()
212+
// listOf(typeOf<AbstractType<Int>>(), typeOf<AbstractType<Any>>()).commonType() shouldBe typeOf<AbstractType<out Any>>()
213+
// listOf(typeOf<AbstractType<Int>>(), typeOf<AbstractType<in Int>>()).commonType() shouldBe typeOf<AbstractType<in Int>>()
214+
listOf(
215+
typeOf<AbstractType<in Int>>(),
216+
typeOf<AbstractType<in Int>>()
217+
).commonType() shouldBe typeOf<AbstractType<in Int>>()
218+
// listOf(typeOf<AbstractType<Int>>(), typeOf<AbstractType<out Int>>()).commonType() shouldBe typeOf<AbstractType<out Int>>()
219+
listOf(
220+
typeOf<AbstractType<out Int>>(),
221+
typeOf<AbstractType<out Int>>()
222+
).commonType() shouldBe typeOf<AbstractType<out Int>>()
223+
// listOf(typeOf<AbstractType<out Int>>(), typeOf<AbstractType<in Int>>()).commonType(useStar = false) shouldBe typeOf<AbstractType<out Any?>>()
224+
// listOf(typeOf<AbstractType<out Int>>(), typeOf<AbstractType<in Int>>()).commonType() shouldBe typeOf<AbstractType<*>>()
225+
226+
listOf(
227+
typeOf<AbstractType<in Int>>(),
228+
typeOf<AbstractType<Any>>()
229+
).commonType() shouldBe typeOf<AbstractType<in Int>>()
230+
231+
listOf(typeOf<Int>(), typeOf<List<Any>>()).commonType() shouldBe typeOf<Any>()
232+
listOf(typeOf<Int>(), typeOf<List<Any>?>()).commonType() shouldBe typeOf<Any?>()
233+
listOf(typeOf<Int>(), typeOf<Int>()).commonType() shouldBe typeOf<Int>()
234+
listOf(typeOf<Int>(), typeOf<Int?>()).commonType() shouldBe typeOf<Int?>()
235+
listOf(typeOf<Int>(), nothingType(true)).commonType() shouldBe typeOf<Int?>()
236+
listOf(typeOf<Int>(), nothingType(false)).commonType() shouldBe typeOf<Int>()
237+
listOf(
238+
typeOf<List<Int>>(),
239+
typeOf<List<String>>()
240+
).commonType(false) shouldBe typeOf<List<out Comparable<Nothing>>>()
211241
listOf(typeOf<List<Int>>(), typeOf<List<String>>()).commonType() shouldBe typeOf<List<out Comparable<*>>>()
212-
listOf(typeOf<List<Int>>(), typeOf<Set<String>>()).commonType(false) shouldBe typeOf<Collection<out Comparable<Nothing>>>()
242+
listOf(
243+
typeOf<List<Int>>(),
244+
typeOf<Set<String>>()
245+
).commonType(false) shouldBe typeOf<Collection<out Comparable<Nothing>>>()
213246
listOf(typeOf<List<Int>>(), typeOf<Set<String>>()).commonType() shouldBe typeOf<Collection<out Comparable<*>>>()
214247
listOf(typeOf<List<Int>>(), typeOf<List<List<Any>>>()).commonType() shouldBe typeOf<List<out Any>>()
215248
listOf(typeOf<List<Int>>(), typeOf<List<List<Any>?>>()).commonType(false) shouldBe typeOf<List<out Any?>>()
@@ -222,6 +255,7 @@ class UtilTests {
222255

223256
@Test
224257
fun `commonTypeListifyValues test`() {
258+
// TODO issue #471: Type inference incorrect w.r.t. variance
225259
listOf<KType>().commonTypeListifyValues() shouldBe typeOf<Any>()
226260
listOf(typeOf<Int>(), typeOf<Int>()).commonTypeListifyValues() shouldBe typeOf<Int>()
227261
listOf(typeOf<Int>(), typeOf<Int?>()).commonTypeListifyValues() shouldBe typeOf<Int?>()

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/api/filter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
66
import org.jetbrains.kotlinx.dataframe.Predicate
77
import org.jetbrains.kotlinx.dataframe.RowFilter
88
import org.jetbrains.kotlinx.dataframe.columns.ColumnReference
9-
import org.jetbrains.kotlinx.dataframe.impl.toIndices
9+
import org.jetbrains.kotlinx.dataframe.impl.getTrueIndices
1010
import org.jetbrains.kotlinx.dataframe.indices
1111
import kotlin.reflect.KProperty
1212

@@ -27,7 +27,7 @@ public fun <T> DataFrame<T>.filter(predicate: RowFilter<T>): DataFrame<T> =
2727
}.let { get(it) }
2828

2929
public fun <T> DataFrame<T>.filterBy(column: ColumnSelector<T, Boolean>): DataFrame<T> =
30-
getRows(getColumn(column).toList().toIndices())
30+
getRows(getColumn(column).toList().getTrueIndices())
3131

3232
public fun <T> DataFrame<T>.filterBy(column: String): DataFrame<T> = filterBy { column.toColumnOf() }
3333

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/Utils.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ internal fun <T : Any> convert(src: Int, tartypeOf: KClass<T>): T = when (tartyp
6868
else -> throw NotImplementedError("Casting int to $tartypeOf is not supported")
6969
}
7070

71-
internal fun BooleanArray.toIndices(): List<Int> {
71+
internal fun BooleanArray.getTrueIndices(): List<Int> {
7272
val res = ArrayList<Int>(size)
73-
for (i in 0 until size)
73+
for (i in indices)
7474
if (this[i]) res.add(i)
7575
return res
7676
}
7777

78-
internal fun List<Boolean>.toIndices(): List<Int> {
78+
internal fun List<Boolean>.getTrueIndices(): List<Int> {
7979
val res = ArrayList<Int>(size)
80-
for (i in 0 until size)
80+
for (i in indices)
8181
if (this[i]) res.add(i)
8282
return res
8383
}
@@ -148,15 +148,18 @@ internal fun Iterable<KType?>.commonType(useStar: Boolean = true): KType {
148148
return when {
149149
distinct.isEmpty() || distinct.contains(null) -> typeOf<Any>().withNullability(nullable)
150150
distinct.size == 1 -> distinct.single()!!
151+
151152
else -> {
152153
// common parent class of all KTypes
153154
val kClass = commonParent(distinct.map { it!!.jvmErasure })
154155
?: return typeOf<Any>().withNullability(nullable)
155-
// all KTypes projected to the common parent class with erased generic type parameters (no <T>
156+
157+
// all KTypes projected to the common parent class with filled-in generic type parameters (no <T>, but <UpperBound>)
156158
val projections = distinct
157159
.map { it!!.projectUpTo(kClass).replaceGenericTypeParametersWithUpperbound() }
158160
require(projections.all { it.jvmErasure == kClass })
159161

162+
// make new type arguments for the common parent class
160163
val arguments = List(kClass.typeParameters.size) { i ->
161164
val typeParameter = kClass.typeParameters[i]
162165
val projectionTypes = projections

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/columns/constructors.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
2424
import org.jetbrains.kotlinx.dataframe.api.indices
2525
import org.jetbrains.kotlinx.dataframe.api.toColumnOf
2626
import org.jetbrains.kotlinx.dataframe.api.toDataFrame
27-
import org.jetbrains.kotlinx.dataframe.columns.*
27+
import org.jetbrains.kotlinx.dataframe.columns.ColumnResolutionContext
28+
import org.jetbrains.kotlinx.dataframe.columns.ColumnSet
29+
import org.jetbrains.kotlinx.dataframe.columns.ColumnWithPath
30+
import org.jetbrains.kotlinx.dataframe.columns.toColumnsSetOf
2831
import org.jetbrains.kotlinx.dataframe.impl.DataFrameReceiver
2932
import org.jetbrains.kotlinx.dataframe.impl.DataRowImpl
3033
import org.jetbrains.kotlinx.dataframe.impl.asList
31-
import org.jetbrains.kotlinx.dataframe.impl.replaceGenericTypeParametersWithUpperbound
3234
import org.jetbrains.kotlinx.dataframe.impl.guessValueType
35+
import org.jetbrains.kotlinx.dataframe.impl.replaceGenericTypeParametersWithUpperbound
3336
import org.jetbrains.kotlinx.dataframe.index
3437
import org.jetbrains.kotlinx.dataframe.nrow
3538
import kotlin.reflect.KClass

0 commit comments

Comments
 (0)