Skip to content

Commit f267630

Browse files
committed
[Compiler plugin ] Handle "relative path" argument in read operations
1 parent f410705 commit f267630

File tree

5 files changed

+71
-16
lines changed

5 files changed

+71
-16
lines changed

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.plugin.impl.AbstractInterpreter
@@ -30,7 +29,11 @@ internal class Read0 : AbstractInterpreter<PluginDataFrameSchema>() {
3029
val Arguments.header: List<String> by arg(defaultValue = Present(listOf()))
3130

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

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

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

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

86-
private fun resolveFile(resolutionPath: String?, path: String): File? {
87-
return resolutionPath?.let {
88+
private fun tryResolveFile(resolutionPath: String?, path: String): DataSource {
89+
val file = resolutionPath?.let {
8890
try {
8991
val file = File(it)
9092
if (file.exists() && file.isDirectory) {
@@ -96,8 +98,17 @@ private fun resolveFile(resolutionPath: String?, path: String): File? {
9698
null
9799
}
98100
}
101+
return if (file != null && file.exists()) {
102+
ResolutionDirFile(file)
103+
} else {
104+
UrlOrAbsolutePath(path)
105+
}
99106
}
100107

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

130141
override fun Arguments.interpret(): PluginDataFrameSchema {
131-
val df = DataFrame.readExcel(fileOrUrl, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
142+
val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
143+
is ResolutionDirFile ->
144+
DataFrame.readExcel(source.file, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
145+
is UrlOrAbsolutePath ->
146+
DataFrame.readExcel(source.path, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
147+
}
132148
return df.schema().toPluginDataFrameSchema()
133149
}
134150
}
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: 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ public void testReadDelimStr_delimiter() {
340340
runTest("testData/box/readDelimStr_delimiter.kt");
341341
}
342342

343+
@Test
344+
@TestMetadata("readExcel.kt")
345+
public void testReadExcel() {
346+
runTest("testData/box/readExcel.kt");
347+
}
348+
343349
@Test
344350
@TestMetadata("readJson.kt")
345351
public void testReadJson() {
@@ -364,6 +370,12 @@ public void testReadJsonStr_memberProperty() {
364370
runTest("testData/box/readJsonStr_memberProperty.kt");
365371
}
366372

373+
@Test
374+
@TestMetadata("read_localFile.kt")
375+
public void testRead_localFile() {
376+
runTest("testData/box/read_localFile.kt");
377+
}
378+
367379
@Test
368380
@TestMetadata("remove.kt")
369381
public void testRemove() {

0 commit comments

Comments
 (0)