Skip to content

Commit 3a3587f

Browse files
koperagenSpace Team
authored andcommitted
[KDF] Consider ColumnName when a generated extension property is used in CS DSL
1 parent 8e80822 commit 3a3587f

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

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

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ package org.jetbrains.kotlinx.dataframe.plugin
88
import org.jetbrains.kotlin.fir.FirSession
99
import org.jetbrains.kotlin.fir.analysis.checkers.toClassLikeSymbol
1010
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
11+
import org.jetbrains.kotlin.fir.declarations.declaredProperties
1112
import org.jetbrains.kotlin.fir.declarations.findArgumentByName
1213
import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId
14+
import org.jetbrains.kotlin.fir.declarations.getAnnotationWithResolvedArgumentsByClassId
1315
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
1416
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
1517
import org.jetbrains.kotlin.fir.expressions.*
@@ -105,7 +107,9 @@ fun <T> KotlinTypeFacade.interpret(
105107
val expectedReturnType = expectedArgument.klass
106108
val value: Interpreter.Success<Any?>? = when (expectedArgument.lens) {
107109
is Interpreter.Value -> {
108-
extractValue(it.expression, reporter)
110+
context(session) {
111+
extractValue(it.expression, reporter)
112+
}
109113
}
110114

111115
is Interpreter.ReturnType -> {
@@ -188,6 +192,7 @@ fun <T> KotlinTypeFacade.interpret(
188192
}
189193
}
190194

195+
context(session: FirSession)
191196
private fun KotlinTypeFacade.extractValue(
192197
expression: FirExpression?,
193198
reporter: InterpretationErrorReporter,
@@ -353,20 +358,21 @@ private fun List<FirPropertySymbol>.sortPropertiesByOrderAnnotation(sessionConte
353358
return result
354359
}
355360

356-
private fun KotlinTypeFacade.columnWithPathApproximations(result: FirPropertyAccessExpression): ColumnsResolver {
357-
return result.resolvedType.let {
361+
context(session: FirSession)
362+
private fun KotlinTypeFacade.columnWithPathApproximations(propertyAccess: FirPropertyAccessExpression): ColumnsResolver {
363+
return propertyAccess.resolvedType.let {
358364
val column = when (it.classId) {
359365
Names.DATA_COLUMN_CLASS_ID -> {
360366
val type = when (val arg = it.typeArguments.single()) {
361367
is ConeStarProjection -> session.builtinTypes.nullableAnyType.coneType
362368
else -> arg as ConeClassLikeType
363369
}
364-
simpleColumnOf(f(result), type)
370+
simpleColumnOf(propertyAccess.columnName(), type)
365371
}
366372
Names.COLUM_GROUP_CLASS_ID -> {
367373
val arg = it.typeArguments.single()
368-
val path = f(result)
369-
SimpleColumnGroup(path, pluginDataFrameSchema(arg).columns())
374+
val name = propertyAccess.columnName()
375+
SimpleColumnGroup(name, pluginDataFrameSchema(arg).columns())
370376
}
371377
else -> return object : ColumnsResolver {
372378
override fun resolve(df: PluginDataFrameSchema): List<ColumnWithPathApproximation> {
@@ -376,7 +382,7 @@ private fun KotlinTypeFacade.columnWithPathApproximations(result: FirPropertyAcc
376382
}
377383
SingleColumnApproximation(
378384
ColumnWithPathApproximation(
379-
path = ColumnPathApproximation(path(result)),
385+
path = ColumnPathApproximation(path(propertyAccess)),
380386
column
381387
)
382388
)
@@ -450,8 +456,9 @@ private fun SessionContext.shouldBeConvertedToFrameColumn(it: FirPropertySymbol)
450456
private fun isDataFrame(it: FirPropertySymbol) =
451457
it.resolvedReturnType.classId == Names.DF_CLASS_ID
452458

459+
context(session: FirSession)
453460
fun path(propertyAccessExpression: FirPropertyAccessExpression): List<String> {
454-
val colName = f(propertyAccessExpression)
461+
val colName = propertyAccessExpression.columnName()
455462
val typeRef = propertyAccessExpression.dispatchReceiver?.resolvedType
456463
val joinDsl = ClassId(FqName("org.jetbrains.kotlinx.dataframe.api"), Name.identifier("JoinDsl"))
457464
if (typeRef?.classId?.equals(joinDsl) == true && colName == "right") {
@@ -470,8 +477,19 @@ fun path(propertyAccessExpression: FirPropertyAccessExpression): List<String> {
470477
}
471478
}
472479

473-
fun f(propertyAccessExpression: FirPropertyAccessExpression): String {
474-
return propertyAccessExpression.calleeReference.resolved!!.name.identifier
480+
context(session: FirSession)
481+
fun FirPropertyAccessExpression.columnName(): String {
482+
val name = toResolvedCallableSymbol()?.name
483+
val columnName =
484+
extensionReceiver?.resolvedType?.typeArguments?.getOrNull(0)?.type?.toRegularClassSymbol(session)
485+
?.declaredProperties(session)
486+
?.firstOrNull { it.name == name }
487+
?.let {
488+
val expression = it.getAnnotationWithResolvedArgumentsByClassId(Names.COLUMN_NAME_ANNOTATION, session)
489+
?.argumentMapping?.mapping[Names.COLUMN_NAME_ARGUMENT] as? FirLiteralExpression
490+
expression?.value as? String
491+
}
492+
return columnName ?: calleeReference.resolved!!.name.identifier
475493
}
476494

477495
internal fun FirFunctionCall.collectArgumentExpressions(): RefinedArguments {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
@DataSchema
7+
data class A(
8+
@ColumnName("hello_world")
9+
val helloWorld: Int
10+
)
11+
12+
fun box(): String {
13+
val df = dataFrameOf(A(123)).convert { helloWorld }.with { "test" }
14+
val col: DataColumn<String> = df.hello_world
15+
16+
val df1 = dataFrameOf(A(123)).rename { helloWorld }.into("hiWorld")
17+
val col1: DataColumn<Int> = df1.hiWorld
18+
return "OK"
19+
}

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

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)