|
| 1 | +# Static interpretation of DataFrame API |
| 2 | + |
| 3 | +Plugin evaluates dataframe operations, given compile-time known arguments such as constant String, resolved types, property access calls. |
| 4 | +It updates the return type of the function call to provide properties that match column names and types. |
| 5 | +The goal is to reflect the result of operations you apply to dataframe in types and have convenient typed API |
| 6 | + |
| 7 | +```kotlin |
| 8 | +val weatherData = dataFrameOf( |
| 9 | + "time" to columnOf(0, 1, 2, 4, 5, 7, 8, 9), |
| 10 | + "temperature" to columnOf(12.0, 14.2, 15.1, 15.9, 17.9, 15.6, 14.2, 24.3), |
| 11 | + "humidity" to columnOf(0.5, 0.32, 0.11, 0.89, 0.68, 0.57, 0.56, 0.5) |
| 12 | +) |
| 13 | + |
| 14 | +weatherData.filter { temperature > 15.0 }.print() |
| 15 | +``` |
| 16 | + |
| 17 | +## Schema info |
| 18 | + |
| 19 | +The schema of DataFrame, as the compiler plugin sees it, |
| 20 | +is displayed when you hover on an expression or variable: |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +This is a way to tell what properties are available. |
| 25 | +For expressions with several operations, you can see how DataFrame changes at each step. |
| 26 | + |
| 27 | +## Visibility of the generated code |
| 28 | + |
| 29 | +Generated code itself is very similar to @DataSchema declarations in nature. |
| 30 | +Take this expression as an example: |
| 31 | + |
| 32 | +```kotlin |
| 33 | +fun main() { |
| 34 | + val df: /* DataFrame<DataFrameOf_39> */ = dataFrameOf("col" to columnOf(42)) |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +It produces two additional local classes: |
| 39 | + |
| 40 | +```kotlin |
| 41 | +// Represents data schema |
| 42 | +class DataFrameOf_39 { |
| 43 | + val a: Int |
| 44 | +} |
| 45 | + |
| 46 | +// Injected to implicit receiver scope of `main` function |
| 47 | +class Scope { |
| 48 | + val DataRow<DataFrameOf_39>.a: Int |
| 49 | + val ColumnsScope<DataFrameOf_39>.a: DataColumn<Int> |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +You can read about the code transformation pipeline in [more detail](https://youtrack.jetbrains.com/issue/KT-65859). |
| 54 | + |
| 55 | +The fact that generated classes are anonymous local types limits their scope to the private scope of the file. |
| 56 | +It means you can do this: |
| 57 | + |
| 58 | +```kotlin |
| 59 | +private fun create(i: Int) = dataFrameOf("number" to columnOf(i)) |
| 60 | + .first() |
| 61 | + |
| 62 | +fun main() { |
| 63 | + val row = create(42) |
| 64 | + println(row.number) |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +But you cannot refer to these classes from your code, have them appear in the explicit type of the variable or as parameter of a function. |
| 69 | + |
| 70 | +## Scope of compiler plugin |
| 71 | + |
| 72 | +Compiler plugin aims to cover all functions where the result of the operation depends only on input schema and arguments that can be resolved at compile time. |
| 73 | +In the library, such functions are annotated with `@Refine` or `@Interpretable`. |
| 74 | + |
| 75 | +There are functions that are not supported: |
| 76 | +`pivot`, `parse`, `read`, `ColumnSelectionDsl.filter`, etc. — operations where the resulting schema depends on data, so it's out of the scope |
| 77 | +`gather`, `split`, `implode`, some CS DSL functions — they will be supported in the future release |
| 78 | + |
| 79 | +In Gradle projects it means that sometimes you'd need to provide [data schema](dataSchema.md) or fall back to String API. |
| 80 | + |
| 81 | +In Kotlin Notebook, the compiler plugin complements the built-in code generator that updates types or variables after cell execution. |
| 82 | + |
| 83 | +```kotlin |
| 84 | +val df = DataFrame.read("...") |
| 85 | +``` |
| 86 | + |
| 87 | +In the next cell you can add, convert, remove, aggregate columns and expect that schema will be updated accordingly, |
| 88 | +without having to split your pipeline into multiple steps and trigger notebook code generation. |
| 89 | + |
| 90 | + |
0 commit comments