Skip to content

Commit 741854c

Browse files
authored
Merge pull request #949 from Kotlin/compiler-plugin-read-improvements
Compiler plugin read improvements
2 parents 7a895e2 + 58e91d3 commit 741854c

File tree

9 files changed

+94
-19
lines changed

9 files changed

+94
-19
lines changed

plugins/kotlin-dataframe/src/org/jetbrains/kotlinx/dataframe/plugin/impl/api/flatten.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import org.jetbrains.kotlinx.dataframe.plugin.impl.toPluginDataFrameSchema
1414
class FlattenDefault : AbstractSchemaModificationInterpreter() {
1515
val Arguments.receiver by dataFrame()
1616
val Arguments.keepParentNameForColumns: Boolean by arg(defaultValue = Present(false))
17-
val Arguments.separator: String by arg(defaultValue = Present("."))
17+
val Arguments.separator: String by arg(defaultValue = Present("_"))
1818

1919
override fun Arguments.interpret(): PluginDataFrameSchema {
2020
return receiver.asDataFrame().flatten(keepParentNameForColumns, separator).toPluginDataFrameSchema()
@@ -24,7 +24,7 @@ class FlattenDefault : AbstractSchemaModificationInterpreter() {
2424
class Flatten0 : AbstractSchemaModificationInterpreter() {
2525
val Arguments.receiver by dataFrame()
2626
val Arguments.keepParentNameForColumns: Boolean by arg(defaultValue = Present(false))
27-
val Arguments.separator: String by arg(defaultValue = Present("."))
27+
val Arguments.separator: String by arg(defaultValue = Present("_"))
2828
val Arguments.columns: ColumnsResolver by arg()
2929

3030
override fun Arguments.interpret(): PluginDataFrameSchema {

plugins/kotlin-dataframe/src/org/jetbrains/kotlinx/dataframe/plugin/impl/api/read.kt

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.jetbrains.kotlinx.dataframe.plugin.impl.api
22

3-
import kotlinx.serialization.decodeFromString
43
import kotlinx.serialization.json.Json
54
import org.jetbrains.kotlinx.dataframe.DataFrame
65
import org.jetbrains.kotlinx.dataframe.DataRow
@@ -31,7 +30,11 @@ internal class Read0 : AbstractInterpreter<PluginDataFrameSchema>() {
3130
val Arguments.header: List<String> by arg(defaultValue = Present(listOf()))
3231

3332
override fun Arguments.interpret(): PluginDataFrameSchema {
34-
return DataFrame.read(path).schema().toPluginDataFrameSchema()
33+
val df = when (val source = tryResolveFile(resolutionPath, path)) {
34+
is ResolutionDirFile -> DataFrame.read(source.file)
35+
is UrlOrAbsolutePath -> DataFrame.read(source.path)
36+
}
37+
return df.schema().toPluginDataFrameSchema()
3538
}
3639
}
3740

@@ -43,11 +46,13 @@ internal class ReadCSV0 : AbstractInterpreter<PluginDataFrameSchema>() {
4346
val Arguments.duplicate: Boolean by arg(defaultValue = Present(true))
4447

4548
override fun Arguments.interpret(): PluginDataFrameSchema {
46-
val file = resolveFile(resolutionPath, fileOrUrl)
47-
val df = if (file != null && file.exists()) {
48-
DataFrame.readCSV(file, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
49-
} else {
50-
DataFrame.readCSV(fileOrUrl, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
49+
val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
50+
is ResolutionDirFile -> {
51+
DataFrame.readCSV(source.file, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
52+
}
53+
is UrlOrAbsolutePath -> {
54+
DataFrame.readCSV(source.path, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
55+
}
5156
}
5257
return df.schema().toPluginDataFrameSchema()
5358
}
@@ -75,17 +80,14 @@ internal class ReadJson0 : AbstractInterpreter<PluginDataFrameSchema>() {
7580
}
7681

7782
fun readJson(resolutionPath: String?, path: String): DataFrame<Any?> {
78-
val file = resolveFile(resolutionPath, path)
79-
val df = if (file != null && file.exists()) {
80-
DataFrame.readJson(file)
81-
} else {
82-
DataFrame.readJson(path)
83+
return when (val source = tryResolveFile(resolutionPath, path)) {
84+
is ResolutionDirFile -> DataFrame.readJson(source.file)
85+
is UrlOrAbsolutePath -> DataFrame.readJson(source.path)
8386
}
84-
return df
8587
}
8688

87-
private fun resolveFile(resolutionPath: String?, path: String): File? {
88-
return resolutionPath?.let {
89+
private fun tryResolveFile(resolutionPath: String?, path: String): DataSource {
90+
val file = resolutionPath?.let {
8991
try {
9092
val file = File(it)
9193
if (file.exists() && file.isDirectory) {
@@ -97,8 +99,17 @@ private fun resolveFile(resolutionPath: String?, path: String): File? {
9799
null
98100
}
99101
}
102+
return if (file != null && file.exists()) {
103+
ResolutionDirFile(file)
104+
} else {
105+
UrlOrAbsolutePath(path)
106+
}
100107
}
101108

109+
private sealed interface DataSource
110+
private class UrlOrAbsolutePath(val path: String) : DataSource
111+
private class ResolutionDirFile(val file: File) : DataSource
112+
102113
internal class ReadDelimStr : AbstractInterpreter<PluginDataFrameSchema>() {
103114
val Arguments.text: String by arg()
104115
val Arguments.delimiter: Char by arg(defaultValue = Present(','))
@@ -138,7 +149,12 @@ internal class ReadExcel : AbstractSchemaModificationInterpreter() {
138149
val Arguments.nameRepairStrategy: NameRepairStrategy by arg(defaultValue = Present(NameRepairStrategy.CHECK_UNIQUE))
139150

140151
override fun Arguments.interpret(): PluginDataFrameSchema {
141-
val df = DataFrame.readExcel(fileOrUrl, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
152+
val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
153+
is ResolutionDirFile ->
154+
DataFrame.readExcel(source.file, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
155+
is UrlOrAbsolutePath ->
156+
DataFrame.readExcel(source.path, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
157+
}
142158
return df.schema().toPluginDataFrameSchema()
143159
}
144160
}

plugins/kotlin-dataframe/src/org/jetbrains/kotlinx/dataframe/plugin/loadInterpreter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import org.jetbrains.kotlinx.dataframe.plugin.impl.api.Move0
9191
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.PairConstructor
9292
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.PairToConstructor
9393
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.ReadExcel
94+
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.StringColumnsConstructor
9495
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.ToDataFrame
9596
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.ToDataFrameColumn
9697
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.ToDataFrameDefault
@@ -234,7 +235,7 @@ internal inline fun <reified T> String.load(): T {
234235
"DataFrameOf0" -> DataFrameOf0()
235236
"DataFrameBuilderInvoke0" -> DataFrameBuilderInvoke0()
236237
"ToDataFrameColumn" -> ToDataFrameColumn()
237-
"StringColumns" -> ToDataFrameColumn()
238+
"StringColumns" -> StringColumnsConstructor()
238239
"ReadExcel" -> ReadExcel()
239240
"FillNulls0" -> FillNulls0()
240241
"UpdateWith0" -> UpdateWith0()

plugins/kotlin-dataframe/testData/box/flatten.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ fun box(): String {
1414
flattened.compareSchemas(strict = true)
1515
flattened.ungroup { f }.compareSchemas(strict = true)
1616

17+
grouped.flatten(keepParentNameForColumns = true) { f.e }.compareSchemas(strict = true)
18+
1719
grouped.flatten { f.e and f }.compareSchemas(strict = true)
1820
return "OK"
1921
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import org.jetbrains.kotlinx.dataframe.*
2+
import org.jetbrains.kotlinx.dataframe.annotations.*
3+
import org.jetbrains.kotlinx.dataframe.api.*
4+
import org.jetbrains.kotlinx.dataframe.io.*
5+
6+
fun box(): String {
7+
val df = @Import DataFrame.read("testResources/sample.xls")
8+
val d1: Double = df.col1[0]
9+
val d2: Double = df.col2[0]
10+
11+
val df1 = @Import DataFrame.readExcel("testResources/sample.xls")
12+
val d11: Double = df1.col1[0]
13+
val d12: Double = df1.col2[0]
14+
return "OK"
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import org.jetbrains.kotlinx.dataframe.*
2+
import org.jetbrains.kotlinx.dataframe.annotations.*
3+
import org.jetbrains.kotlinx.dataframe.api.*
4+
import org.jetbrains.kotlinx.dataframe.io.*
5+
6+
fun box(): String {
7+
val df = @Import DataFrame.readExcel("testResources/sample.xls", stringColumns = StringColumns("A"))
8+
val d1: String = df.col1[0]
9+
val d2: Double = df.col2[0]
10+
return "OK"
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import org.jetbrains.kotlinx.dataframe.*
2+
import org.jetbrains.kotlinx.dataframe.annotations.*
3+
import org.jetbrains.kotlinx.dataframe.api.*
4+
import org.jetbrains.kotlinx.dataframe.io.*
5+
6+
fun box(): String {
7+
val df = @Import DataFrame.read("testResources/achievements_all.json")
8+
9+
val df1 = df.explode { achievements }
10+
df1.achievements.order
11+
return "OK"
12+
}
Binary file not shown.

plugins/kotlin-dataframe/tests-gen/org/jetbrains/kotlin/fir/dataframe/DataFrameBlackBoxCodegenTestGenerated.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,18 @@ public void testReadDelimStr_delimiter() {
346346
runTest("testData/box/readDelimStr_delimiter.kt");
347347
}
348348

349+
@Test
350+
@TestMetadata("readExcel.kt")
351+
public void testReadExcel() {
352+
runTest("testData/box/readExcel.kt");
353+
}
354+
355+
@Test
356+
@TestMetadata("readExcel_stringColumns.kt")
357+
public void testReadExcel_stringColumns() {
358+
runTest("testData/box/readExcel_stringColumns.kt");
359+
}
360+
349361
@Test
350362
@TestMetadata("readJson.kt")
351363
public void testReadJson() {
@@ -376,6 +388,12 @@ public void testReadJsonStr_memberProperty() {
376388
runTest("testData/box/readJsonStr_memberProperty.kt");
377389
}
378390

391+
@Test
392+
@TestMetadata("read_localFile.kt")
393+
public void testRead_localFile() {
394+
runTest("testData/box/read_localFile.kt");
395+
}
396+
379397
@Test
380398
@TestMetadata("remove.kt")
381399
public void testRemove() {

0 commit comments

Comments
 (0)