Skip to content

Commit 222086a

Browse files
committed
forgot defaultValue in createColumnGuessingType(), removed column size check from guessing type. May be unexpected behavior. Fixed tests
1 parent dfaf46e commit 222086a

File tree

6 files changed

+46
-48
lines changed

6 files changed

+46
-48
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public fun dataFrameOf(header: Iterable<String>, values: Iterable<Any?>): DataFr
301301

302302
public inline fun <T, reified C> dataFrameOf(header: Iterable<T>, fill: (T) -> Iterable<C>): DataFrame<*> =
303303
header.map { value ->
304-
createColumnGuessingType(
304+
DataColumn.createWithTypeInference(
305305
name = value.toString(),
306306
values = fill(value).asList(),
307307
suggestedType = TypeSuggestion.InferWithUpperbound(typeOf<C>()),
@@ -341,7 +341,7 @@ public class DataFrameBuilder(private val header: List<String>) {
341341

342342
public inline operator fun <reified T> invoke(crossinline valuesBuilder: (String) -> Iterable<T>): DataFrame<*> =
343343
withColumns { name ->
344-
createColumnGuessingType(
344+
DataColumn.createWithTypeInference(
345345
name = name,
346346
values = valuesBuilder(name).asList(),
347347
suggestedType = TypeSuggestion.InferWithUpperbound(typeOf<T>()),

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ public sealed interface TypeSuggestion {
2323
when {
2424
suggestedType != null && guessType -> InferWithUpperbound(suggestedType)
2525
suggestedType != null && !guessType -> Use(suggestedType)
26-
suggestedType == null && guessType -> Infer
27-
else -> error("Cannot create TypeSuggestion with no suggested type and no guessing allowed.")
26+
else -> Infer // no type was suggested, so we need to guess, no matter what guessType is
2827
}
2928
}
3029

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ internal fun <T> getValuesType(values: List<T>, type: KType, infer: Infer): KTyp
392392
* @param listifyValues if true, then values and nulls will be wrapped in a list if they appear among other lists.
393393
* For example: `[1, null, listOf(1, 2, 3)]` will become `List<Int>` instead of `Any?`
394394
* Note: this parameter is ignored if another [Collection] is present in the values.
395-
* @param allColsMakesRow if true, then, if all values are non-null same-sized columns, we assume
395+
* @param allColsMakesRow if true, then, if all values are non-null columns, we assume
396396
* that a column group should be created instead of a [DataColumn][DataColumn]`<`[AnyCol][AnyCol]`>`,
397397
* so the function will return [DataRow].
398398
*/
@@ -409,7 +409,6 @@ internal fun guessValueType(
409409
var hasFrames = false
410410
var hasRows = false
411411
var hasCols = false
412-
val colSizes = mutableListOf<Int>()
413412
var hasList = false
414413
var allListsAreEmpty = true
415414
val classesInCollection = mutableSetOf<KClass<*>>()
@@ -423,10 +422,7 @@ internal fun guessValueType(
423422

424423
is AnyFrame -> hasFrames = true
425424

426-
is AnyCol -> {
427-
hasCols = true
428-
colSizes += it.size()
429-
}
425+
is AnyCol -> hasCols = true
430426

431427
is List<*> -> {
432428
hasList = true
@@ -491,13 +487,7 @@ internal fun guessValueType(
491487
hasRows && !hasFrames && !hasList && !hasCols ->
492488
DataRow::class.createStarProjectedType(false)
493489

494-
allColsMakesRow &&
495-
hasCols &&
496-
!hasFrames &&
497-
!hasList &&
498-
!hasRows &&
499-
!hasNulls &&
500-
colSizes.distinct().size == 1 ->
490+
allColsMakesRow && hasCols && !hasFrames && !hasList && !hasRows && !hasNulls ->
501491
DataRow::class.createStarProjectedType(false)
502492

503493
collectionClasses.isNotEmpty() && !hasFrames && !hasRows && !hasCols -> {
@@ -509,24 +499,24 @@ internal fun guessValueType(
509499
}
510500
}
511501
if (hasList) collectionClasses.add(List::class)
512-
(commonParent(collectionClasses) ?: Collection::class)
513-
.createTypeWithArgument(
514-
classesInCollection.commonType(
515-
nullable = nullsInCollection,
516-
upperBound = elementType ?: nothingType(nullable = nullsInCollection),
517-
),
518-
).withNullability(hasNulls)
502+
(commonParent(collectionClasses) ?: Collection::class).createTypeWithArgument(
503+
argument = classesInCollection.commonType(
504+
nullable = nullsInCollection,
505+
upperBound = elementType ?: nothingType(nullable = nullsInCollection),
506+
),
507+
).withNullability(hasNulls)
519508
}
520509

521510
hasList && collectionClasses.isEmpty() && !hasFrames && !hasRows && !hasCols -> {
522-
val elementType = upperBound?.let { if (it.jvmErasure == List::class) it.arguments[0].type else null }
523-
List::class
524-
.createTypeWithArgument(
525-
classesInCollection.commonType(
526-
nullable = nullsInCollection,
527-
upperBound = elementType ?: nothingType(nullable = nullsInCollection),
528-
),
529-
).withNullability(hasNulls && !listifyValues)
511+
val elementType = upperBound?.let {
512+
if (it.jvmErasure == List::class) it.arguments[0].type else null
513+
}
514+
List::class.createTypeWithArgument(
515+
argument = classesInCollection.commonType(
516+
nullable = nullsInCollection,
517+
upperBound = elementType ?: nothingType(nullable = nullsInCollection),
518+
),
519+
).withNullability(hasNulls && !listifyValues)
530520
}
531521

532522
else -> {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ internal fun <T> createColumnGuessingType(
239239

240240
return when (type.classifier!! as KClass<*>) {
241241
// guessValueType can only return DataRow if all values are `AnyRow?`
242-
// or allColsMakesColGroup == true, all values are `AnyCol`, and they all have the same size
242+
// or allColsMakesColGroup == true, all values are `AnyCol`
243243
DataRow::class -> {
244244
if (allColsMakesColGroup && values.firstOrNull() is AnyCol) {
245245
val df = dataFrameOf(values as Iterable<AnyCol>)
@@ -293,7 +293,12 @@ internal fun <T> createColumnGuessingType(
293293
}
294294
DataColumn.createFrameColumn(name, frames).cast()
295295
} else {
296-
DataColumn.createValueColumn(name, lists, type, defaultValue = defaultValue).cast()
296+
DataColumn.createValueColumn(
297+
name = name,
298+
values = lists,
299+
type = type,
300+
defaultValue = defaultValue,
301+
).cast()
297302
}
298303
}
299304

@@ -310,6 +315,7 @@ internal fun <T> createColumnGuessingType(
310315
// nullability already inferred by guessValueType
311316
else -> Infer.None
312317
},
318+
defaultValue = defaultValue,
313319
)
314320
}
315321
}

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
99
import org.jetbrains.kotlinx.dataframe.DataRow
1010
import org.jetbrains.kotlinx.dataframe.columns.ColumnGroup
1111
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
12+
import org.jetbrains.kotlinx.dataframe.columns.TypeSuggestion.InferWithUpperbound
1213
import org.jetbrains.kotlinx.dataframe.impl.columns.createColumnGuessingType
1314
import org.jetbrains.kotlinx.dataframe.impl.nothingType
1415
import org.jetbrains.kotlinx.dataframe.type
@@ -49,7 +50,10 @@ class ConstructorsTests {
4950
@Test
5051
fun `guess column group from rows`() {
5152
val row = dataFrameOf("a", "b")(1, 2).single()
52-
val col = createColumnGuessingType(listOf(row, DataRow.empty), typeOf<AnyRow>(), true)
53+
val col = createColumnGuessingType(
54+
values = listOf(row, DataRow.empty),
55+
suggestedType = InferWithUpperbound(typeOf<AnyRow>()),
56+
)
5357
col shouldBe columnOf(row, DataRow.empty)
5458

5559
col.hasNulls() shouldBe false
@@ -62,7 +66,10 @@ class ConstructorsTests {
6266
@Test
6367
fun `guess column group from rows with null`() {
6468
val row = dataFrameOf("a", "b")(1, 2).single()
65-
val col = createColumnGuessingType(listOf(row, DataRow.empty, null), typeOf<AnyRow?>(), true)
69+
val col = createColumnGuessingType(
70+
values = listOf(row, DataRow.empty, null),
71+
suggestedType = InferWithUpperbound(typeOf<AnyRow?>()),
72+
)
6673
col shouldBe columnOf(row, DataRow.empty, null)
6774

6875
col.hasNulls() shouldBe false
@@ -79,8 +86,7 @@ class ConstructorsTests {
7986
val col2 = columnOf("a", "b")
8087
val col = createColumnGuessingType(
8188
values = listOf(col1, col2),
82-
suggestedType = typeOf<AnyCol>(),
83-
guessTypeWithSuggestedAsUpperbound = true,
89+
suggestedType = InferWithUpperbound(typeOf<AnyCol>()),
8490
allColsMakesColGroup = true,
8591
)
8692
col shouldBe columnOf(col1, col2)
@@ -99,9 +105,8 @@ class ConstructorsTests {
99105
val col1 = columnOf(1, 2)
100106
val col2 = columnOf("a", "b")
101107
val col = createColumnGuessingType(
102-
listOf(col1, col2, null),
103-
typeOf<AnyCol?>(),
104-
true,
108+
values = listOf(col1, col2, null),
109+
suggestedType = InferWithUpperbound(typeOf<AnyCol?>()),
105110
)
106111
col.values shouldBe columnOf(col1, col2, null).values
107112

@@ -118,9 +123,8 @@ class ConstructorsTests {
118123
val df1 = dataFrameOf("a", "b")(1, 2)
119124
val df2 = dataFrameOf("a", "b")(3, 4)
120125
val col = createColumnGuessingType(
121-
listOf(df1, df2, null),
122-
typeOf<AnyCol?>(),
123-
true,
126+
values = listOf(df1, df2, null),
127+
suggestedType = InferWithUpperbound(typeOf<AnyCol?>()),
124128
)
125129
col.values shouldBe columnOf(df1, df2, null).values
126130

@@ -135,9 +139,8 @@ class ConstructorsTests {
135139
@Test
136140
fun `guess value column from nulls`() {
137141
val col = createColumnGuessingType(
138-
listOf(null, null),
139-
nothingType(true),
140-
true,
142+
values = listOf(null, null),
143+
suggestedType = InferWithUpperbound(nothingType(true)),
141144
)
142145
col.values shouldBe columnOf<Any?>(null, null).values
143146

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class UtilTests {
158158
guessValueType(
159159
sequenceOf(DataColumn.empty(), columnOf(1)),
160160
allColsMakesRow = true,
161-
) shouldBe typeOf<DataColumn<*>>()
161+
) shouldBe typeOf<DataRow<*>>()
162162

163163
guessValueType(
164164
sequenceOf(columnOf("a"), columnOf(1)),

0 commit comments

Comments
 (0)