Skip to content

Commit 9d492b1

Browse files
committed
clarification with docs as asked in the review
1 parent 5e01d86 commit 9d492b1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

core/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ dependencies {
7373

7474
api(libs.kotlin.datetimeJvm)
7575
implementation(libs.kotlinpoet)
76+
implementation(libs.kotlinLogging)
7677

7778
testImplementation(libs.junit)
7879
testImplementation(libs.kotestAssertions) {

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

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jetbrains.kotlinx.dataframe.impl.api
22

3+
import io.github.oshai.kotlinlogging.KotlinLogging
34
import org.jetbrains.kotlinx.dataframe.AnyFrame
45
import org.jetbrains.kotlinx.dataframe.AnyRow
56
import org.jetbrains.kotlinx.dataframe.ColumnsSelector
@@ -47,6 +48,8 @@ import kotlin.reflect.KType
4748
import kotlin.reflect.full.withNullability
4849
import kotlin.reflect.jvm.jvmErasure
4950

51+
private val logger = KotlinLogging.logger {}
52+
5053
private open class Converter(val transform: ConverterScope.(Any?) -> Any?, val skipNulls: Boolean)
5154

5255
private class Filler(val columns: ColumnsSelector<*, *>, val expr: RowExpression<*, *>)
@@ -262,6 +265,7 @@ internal fun AnyFrame.convertToImpl(
262265
try {
263266
newColumns += targetColumn.createNullFilledColumn(name, size)
264267
} catch (e: IllegalStateException) {
268+
logger.debug(e) { "" }
265269
// if this could not be done automatically, they need to be filled manually
266270
missingPaths.add(path + name)
267271
}
@@ -274,20 +278,39 @@ internal fun AnyFrame.convertToImpl(
274278
val marker = MarkersExtractor.get(clazz)
275279
var result = convertToSchema(marker.schema, emptyPath())
276280

281+
/*
282+
* Here we handle all registered fillers of the user.
283+
* Fillers are registered in the DSL like:
284+
* ```kt
285+
* df.convertTo<Target> {
286+
* fill { col1 and col2 }.with { something }
287+
* fill { col3 }.with { somethingElse }
288+
* }
289+
* ```
290+
* Users can use this to fill up any column that was missing during the conversion.
291+
* They can also fill up and thus overwrite any existing column here.
292+
*/
277293
dsl.fillers.forEach { filler ->
294+
// get all paths from the `fill { col1 and col2 }` part
278295
val paths = result.getColumnPaths(UnresolvedColumnsPolicy.Create, filler.columns).toSet()
296+
297+
// split the paths into those that are already in the df and those that are missing
279298
val (newPaths, existingPaths) = paths.partition { it in missingPaths }
280-
missingPaths -= paths
281299

282-
// first fill cols that are already in the df
300+
// first fill cols that are already in the df using the `with {}` part of the dsl
283301
result = result.update { existingPaths.toColumnSet() }.with { filler.expr(this, this) }
284302

285-
// then create any missing ones by filling
303+
// then create any missing ones by filling using the `with {}` part of the dsl
286304
result = newPaths.fold(result) { df, newPath ->
287305
df.add(newPath, Infer.Type) { filler.expr(this, this) }
288306
}
307+
308+
// remove the paths that are now filled
309+
missingPaths -= paths
289310
}
290311

312+
// Inform the user which target columns could not be created in the conversion
313+
// The user will need to supply extra information for these, like `fill {}` them.
291314
if (missingPaths.isNotEmpty()) {
292315
throw IllegalArgumentException(
293316
"The following columns were not found in DataFrame: ${

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ internal fun ColumnSchema.createNullFilledColumn(name: String, numberOfRows: Int
141141
schema = lazyOf(schema),
142142
)
143143

144-
else -> error("Unexpected ColumnSchema: $this")
144+
else -> error("Cannot create null-filled column of unexpected ColumnSchema: $this")
145145
}
146146

147147
internal fun DataFrameSchema.createEmptyDataFrame(): AnyFrame =

0 commit comments

Comments
 (0)