Skip to content

Commit e87dba9

Browse files
Merge pull request #1729 from Kotlin/columnOf_types
Added a `columnOf` types test
2 parents a7bbfad + 1b2cc9c commit e87dba9

6 files changed

Lines changed: 155 additions & 13 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ internal fun renderType(type: KType?): String {
6565
else -> {
6666
val fullName = type.jvmErasure.qualifiedName ?: return type.toString()
6767
val name = when {
68-
// catching cases like `typeOf<Array<Int>>().jvmErasure.qualifiedName == "IntArray"`
69-
// https://github.com/Kotlin/dataframe/issues/678
7068
type.isSubtypeOf(typeOf<Array<*>>()) ->
7169
"Array"
7270

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -607,14 +607,7 @@ internal val KClass<*>.isArray: Boolean
607607
* Use [KType.isArray] to also check for `Array<>`.
608608
*/
609609
internal val KType.isPrimitiveArray: Boolean
610-
get() =
611-
if (arguments.isNotEmpty()) {
612-
// Catching https://github.com/Kotlin/dataframe/issues/678
613-
// as typeOf<Array<Int>>().classifier == IntArray::class
614-
false
615-
} else {
616-
(classifier as? KClass<*>)?.isPrimitiveArray == true
617-
}
610+
get() = (classifier as? KClass<*>)?.isPrimitiveArray == true
618611

619612
/**
620613
* Returns `true` if this type is of an array, either a primitive array like `XArray` or `Array<>`.

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,33 @@ import kotlin.reflect.typeOf
1919

2020
class ConstructorsTests {
2121

22+
@Test
23+
fun `columnOf correct types`() {
24+
val intCol = columnOf(1, 2, 3)
25+
intCol.type shouldBe typeOf<Int>()
26+
27+
val stringCol = columnOf("a", "b", "c")
28+
stringCol.type shouldBe typeOf<String>()
29+
30+
val nullableIntCol = columnOf(1, 2, null)
31+
nullableIntCol.type shouldBe typeOf<Int?>()
32+
33+
val doubleCol = columnOf(1.0, 2.5, 3.7)
34+
doubleCol.type shouldBe typeOf<Double>()
35+
36+
val booleanCol = columnOf(true, false, true)
37+
booleanCol.type shouldBe typeOf<Boolean>()
38+
39+
val arrayIntCol = columnOf(arrayOf(1))
40+
arrayIntCol.type shouldBe typeOf<Array<Int>>()
41+
42+
val intArrayCol = columnOf(intArrayOf(1))
43+
intArrayCol.type shouldBe typeOf<IntArray>()
44+
45+
val nullableArrayIntCol = columnOf(arrayOf(1, null))
46+
nullableArrayIntCol.type shouldBe typeOf<Array<Int?>>()
47+
}
48+
2249
@Test
2350
fun `untitled column naming`() {
2451
val builder = DynamicDataFrameBuilder()
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.jetbrains.kotlinx.dataframe.impl
2+
3+
import io.kotest.matchers.shouldBe
4+
import kotlinx.datetime.LocalDateTime
5+
import kotlinx.datetime.LocalTime
6+
import org.jetbrains.kotlinx.dataframe.DataFrame
7+
import org.junit.Test
8+
import java.net.URL
9+
import kotlin.reflect.typeOf
10+
import kotlin.time.Duration
11+
import kotlin.time.Instant
12+
13+
class Rendering {
14+
15+
@Test
16+
fun `renderType by KType test`() {
17+
// null
18+
renderType(null) shouldBe "*"
19+
20+
// Nothing?
21+
renderType(typeOf<Nothing?>()) shouldBe "Nothing?"
22+
23+
// Primitive arrays
24+
renderType(typeOf<IntArray>()) shouldBe "IntArray"
25+
renderType(typeOf<ByteArray>()) shouldBe "ByteArray"
26+
renderType(typeOf<LongArray>()) shouldBe "LongArray"
27+
renderType(typeOf<DoubleArray>()) shouldBe "DoubleArray"
28+
renderType(typeOf<FloatArray>()) shouldBe "FloatArray"
29+
renderType(typeOf<BooleanArray>()) shouldBe "BooleanArray"
30+
31+
// Array<T>
32+
renderType(typeOf<Array<Int>>()) shouldBe "Array<Int>"
33+
renderType(typeOf<Array<String>>()) shouldBe "Array<String>"
34+
renderType(typeOf<Array<Int?>>()) shouldBe "Array<Int?>"
35+
renderType(typeOf<Array<*>>()) shouldBe "Array<*>"
36+
37+
// URL
38+
renderType(typeOf<URL>()) shouldBe "URL"
39+
40+
// kotlinx.datetime
41+
renderType(typeOf<LocalDateTime>()) shouldBe "LocalDateTime"
42+
renderType(typeOf<LocalTime>()) shouldBe "LocalTime"
43+
44+
// kotlin.collections
45+
renderType(typeOf<List<Int>>()) shouldBe "List<Int>"
46+
renderType(typeOf<Map<String, Int>>()) shouldBe "Map<String, Int>"
47+
renderType(typeOf<Set<String>>()) shouldBe "Set<String>"
48+
renderType(typeOf<MutableList<Int>>()) shouldBe "List<Int>"
49+
50+
// kotlin.time
51+
renderType(typeOf<Duration>()) shouldBe "Duration"
52+
renderType(typeOf<Instant>()) shouldBe "Instant"
53+
54+
// kotlin.*
55+
renderType(typeOf<Int>()) shouldBe "Int"
56+
renderType(typeOf<String>()) shouldBe "String"
57+
renderType(typeOf<Boolean>()) shouldBe "Boolean"
58+
renderType(typeOf<Double>()) shouldBe "Double"
59+
60+
// nullable types
61+
renderType(typeOf<Int?>()) shouldBe "Int?"
62+
renderType(typeOf<String?>()) shouldBe "String?"
63+
renderType(typeOf<List<Int>?>()) shouldBe "List<Int>?"
64+
renderType(typeOf<List<Int?>>()) shouldBe "List<Int?>"
65+
66+
// dataframe types
67+
renderType(typeOf<DataFrame<*>>()) shouldBe "DataFrame<*>"
68+
69+
// non-kotlin types (fully qualified)
70+
renderType(typeOf<java.math.BigDecimal>()) shouldBe "java.math.BigDecimal"
71+
72+
// variance
73+
renderType(typeOf<List<*>>()) shouldBe "List<*>"
74+
renderType(typeOf<Comparable<in Int>>()) shouldBe "Comparable<in Int>"
75+
}
76+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.jetbrains.kotlinx.dataframe.impl
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.Test
5+
import kotlin.reflect.typeOf
6+
7+
class TypeUtils {
8+
@Test
9+
fun `isPrimitiveArray test`() {
10+
// All primitive array types -> true
11+
typeOf<IntArray>().isPrimitiveArray shouldBe true
12+
typeOf<ByteArray>().isPrimitiveArray shouldBe true
13+
typeOf<ShortArray>().isPrimitiveArray shouldBe true
14+
typeOf<LongArray>().isPrimitiveArray shouldBe true
15+
typeOf<FloatArray>().isPrimitiveArray shouldBe true
16+
typeOf<DoubleArray>().isPrimitiveArray shouldBe true
17+
typeOf<BooleanArray>().isPrimitiveArray shouldBe true
18+
typeOf<CharArray>().isPrimitiveArray shouldBe true
19+
20+
// Unsigned primitive arrays -> true
21+
@OptIn(ExperimentalUnsignedTypes::class)
22+
run {
23+
typeOf<UByteArray>().isPrimitiveArray shouldBe true
24+
typeOf<UShortArray>().isPrimitiveArray shouldBe true
25+
typeOf<UIntArray>().isPrimitiveArray shouldBe true
26+
typeOf<ULongArray>().isPrimitiveArray shouldBe true
27+
}
28+
29+
// Nullable primitive arrays -> true
30+
typeOf<IntArray?>().isPrimitiveArray shouldBe true
31+
typeOf<ByteArray?>().isPrimitiveArray shouldBe true
32+
typeOf<DoubleArray?>().isPrimitiveArray shouldBe true
33+
typeOf<BooleanArray?>().isPrimitiveArray shouldBe true
34+
35+
// Array<T> (has type arguments) -> false
36+
typeOf<Array<Int>>().isPrimitiveArray shouldBe false
37+
typeOf<Array<Int?>>().isPrimitiveArray shouldBe false
38+
typeOf<Array<String>>().isPrimitiveArray shouldBe false
39+
typeOf<Array<*>>().isPrimitiveArray shouldBe false
40+
typeOf<Array<Any>>().isPrimitiveArray shouldBe false
41+
42+
// Non-array types -> false
43+
typeOf<Int>().isPrimitiveArray shouldBe false
44+
typeOf<String>().isPrimitiveArray shouldBe false
45+
typeOf<List<Int>>().isPrimitiveArray shouldBe false
46+
typeOf<Map<String, Int>>().isPrimitiveArray shouldBe false
47+
typeOf<Any>().isPrimitiveArray shouldBe false
48+
typeOf<Nothing?>().isPrimitiveArray shouldBe false
49+
}
50+
}

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/rendering/RenderingTests.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,7 @@ class RenderingTests : TestBase() {
194194
val df = dataFrameOf(
195195
columnOf(1, null).named("a"),
196196
columnOf(intArrayOf(1), intArrayOf(2)).named("b"),
197-
// TODO https://github.com/Kotlin/dataframe/issues/679
198-
// columnOf(arrayOf(1), arrayOf(2)).named("d"),
199-
DataColumn.createValueColumn("c", listOf(arrayOf(1), arrayOf(2))),
197+
columnOf(arrayOf(1), arrayOf(2)).named("c"),
200198
columnOf(arrayOf(1, null), arrayOf(2, null)).named("d"),
201199
)
202200

0 commit comments

Comments
 (0)