Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler plugin read improvements #949

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jetbrains.kotlinx.dataframe.plugin.impl.api

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.dataframe.plugin.impl.AbstractInterpreter
Expand Down Expand Up @@ -30,7 +29,11 @@ internal class Read0 : AbstractInterpreter<PluginDataFrameSchema>() {
val Arguments.header: List<String> by arg(defaultValue = Present(listOf()))

override fun Arguments.interpret(): PluginDataFrameSchema {
return DataFrame.read(path).schema().toPluginDataFrameSchema()
val df = when (val source = tryResolveFile(resolutionPath, path)) {
is ResolutionDirFile -> DataFrame.read(source.file)
is UrlOrAbsolutePath -> DataFrame.read(source.path)
}
return df.schema().toPluginDataFrameSchema()
}
}

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

override fun Arguments.interpret(): PluginDataFrameSchema {
val file = resolveFile(resolutionPath, fileOrUrl)
val df = if (file != null && file.exists()) {
DataFrame.readCSV(file, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
} else {
DataFrame.readCSV(fileOrUrl, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
is ResolutionDirFile -> {
DataFrame.readCSV(source.file, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
}
is UrlOrAbsolutePath -> {
DataFrame.readCSV(source.path, delimiter, skipLines = skipLines, readLines = readLines, duplicate = duplicate)
}
}
return df.schema().toPluginDataFrameSchema()
}
Expand Down Expand Up @@ -74,17 +79,14 @@ internal class ReadJson0 : AbstractInterpreter<PluginDataFrameSchema>() {
}

fun readJson(resolutionPath: String?, path: String): DataFrame<Any?> {
val file = resolveFile(resolutionPath, path)
val df = if (file != null && file.exists()) {
DataFrame.readJson(file)
} else {
DataFrame.readJson(path)
return when (val source = tryResolveFile(resolutionPath, path)) {
is ResolutionDirFile -> DataFrame.readJson(source.file)
is UrlOrAbsolutePath -> DataFrame.readJson(source.path)
}
return df
}

private fun resolveFile(resolutionPath: String?, path: String): File? {
return resolutionPath?.let {
private fun tryResolveFile(resolutionPath: String?, path: String): DataSource {
koperagen marked this conversation as resolved.
Show resolved Hide resolved
val file = resolutionPath?.let {
try {
val file = File(it)
if (file.exists() && file.isDirectory) {
Expand All @@ -96,8 +98,17 @@ private fun resolveFile(resolutionPath: String?, path: String): File? {
null
}
}
return if (file != null && file.exists()) {
ResolutionDirFile(file)
} else {
UrlOrAbsolutePath(path)
}
}

private sealed interface DataSource
private class UrlOrAbsolutePath(val path: String) : DataSource
private class ResolutionDirFile(val file: File) : DataSource

internal class ReadDelimStr : AbstractInterpreter<PluginDataFrameSchema>() {
val Arguments.text: String by arg()
val Arguments.delimiter: Char by arg(defaultValue = Present(','))
Expand Down Expand Up @@ -128,7 +139,12 @@ internal class ReadExcel : AbstractSchemaModificationInterpreter() {
val Arguments.nameRepairStrategy: NameRepairStrategy by arg(defaultValue = Present(NameRepairStrategy.CHECK_UNIQUE))

override fun Arguments.interpret(): PluginDataFrameSchema {
val df = DataFrame.readExcel(fileOrUrl, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
val df = when (val source = tryResolveFile(resolutionPath, fileOrUrl)) {
is ResolutionDirFile ->
DataFrame.readExcel(source.file, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
is UrlOrAbsolutePath ->
DataFrame.readExcel(source.path, sheetName, skipRows, columns, stringColumns, rowsCount, nameRepairStrategy)
}
return df.schema().toPluginDataFrameSchema()
}
}
Expand Down
15 changes: 15 additions & 0 deletions plugins/kotlin-dataframe/testData/box/readExcel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.annotations.*
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.io.*

fun box(): String {
val df = @Import DataFrame.read("testResources/sample.xls")
val d1: Double = df.col1[0]
val d2: Double = df.col2[0]

val df1 = @Import DataFrame.readExcel("testResources/sample.xls")
val d11: Double = df1.col1[0]
val d12: Double = df1.col2[0]
return "OK"
}
12 changes: 12 additions & 0 deletions plugins/kotlin-dataframe/testData/box/read_localFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import org.jetbrains.kotlinx.dataframe.*
import org.jetbrains.kotlinx.dataframe.annotations.*
import org.jetbrains.kotlinx.dataframe.api.*
import org.jetbrains.kotlinx.dataframe.io.*

fun box(): String {
val df = @Import DataFrame.read("testResources/achievements_all.json")

val df1 = df.explode { achievements }
df1.achievements.order
return "OK"
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ public void testReadDelimStr_delimiter() {
runTest("testData/box/readDelimStr_delimiter.kt");
}

@Test
@TestMetadata("readExcel.kt")
public void testReadExcel() {
runTest("testData/box/readExcel.kt");
}

@Test
@TestMetadata("readJson.kt")
public void testReadJson() {
Expand All @@ -364,6 +370,12 @@ public void testReadJsonStr_memberProperty() {
runTest("testData/box/readJsonStr_memberProperty.kt");
}

@Test
@TestMetadata("read_localFile.kt")
public void testRead_localFile() {
runTest("testData/box/read_localFile.kt");
}

@Test
@TestMetadata("remove.kt")
public void testRemove() {
Expand Down