Skip to content

Commit b989c63

Browse files
committed
[Compiler plugin] Consider column name annotation when extracting schema
1 parent 10935d2 commit b989c63

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import org.jetbrains.kotlinx.dataframe.plugin.impl.SimpleColumnGroup
7878
import org.jetbrains.kotlinx.dataframe.plugin.impl.SimpleFrameColumn
7979
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.ColumnsResolver
8080
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.SingleColumnApproximation
81+
import org.jetbrains.kotlinx.dataframe.plugin.impl.api.TypeApproximation
8182

8283
fun <T> KotlinTypeFacade.interpret(
8384
functionCall: FirFunctionCall,
@@ -374,19 +375,23 @@ private fun KotlinTypeFacade.columnWithPathApproximations(result: FirPropertyAcc
374375
}
375376
}
376377

377-
private fun KotlinTypeFacade.columnOf(it: FirPropertySymbol, mapping: Map<FirTypeParameterSymbol, ConeTypeProjection>): SimpleCol? =
378-
when {
378+
private fun KotlinTypeFacade.columnOf(it: FirPropertySymbol, mapping: Map<FirTypeParameterSymbol, ConeTypeProjection>): SimpleCol? {
379+
val annotation = it.getAnnotationByClassId(Names.COLUMN_NAME_ANNOTATION, session)
380+
val columnName = (annotation?.argumentMapping?.mapping?.get(Names.COLUMN_NAME_ARGUMENT) as? FirLiteralExpression)?.value as? String
381+
val name = columnName ?: it.name.identifier
382+
return when {
379383
shouldBeConvertedToFrameColumn(it) -> {
380-
val nestedColumns = it.resolvedReturnType.typeArguments[0].type
381-
?.toRegularClassSymbol(session)
382-
?.declaredMemberScope(session, FirResolvePhase.DECLARATIONS)
383-
?.collectAllProperties()
384-
?.filterIsInstance<FirPropertySymbol>()
385-
?.mapNotNull { columnOf(it, mapping) }
386-
?: emptyList()
387-
388-
SimpleFrameColumn(it.name.identifier, nestedColumns)
389-
}
384+
val nestedColumns = it.resolvedReturnType.typeArguments[0].type
385+
?.toRegularClassSymbol(session)
386+
?.declaredMemberScope(session, FirResolvePhase.DECLARATIONS)
387+
?.collectAllProperties()
388+
?.filterIsInstance<FirPropertySymbol>()
389+
?.mapNotNull { columnOf(it, mapping) }
390+
?: emptyList()
391+
392+
SimpleFrameColumn(name, nestedColumns)
393+
}
394+
390395
shouldBeConvertedToColumnGroup(it) -> {
391396
val type = if (isDataRow(it)) it.resolvedReturnType.typeArguments[0].type!! else it.resolvedReturnType
392397
val nestedColumns = type
@@ -396,8 +401,9 @@ private fun KotlinTypeFacade.columnOf(it: FirPropertySymbol, mapping: Map<FirTyp
396401
?.filterIsInstance<FirPropertySymbol>()
397402
?.mapNotNull { columnOf(it, mapping) }
398403
?: emptyList()
399-
SimpleColumnGroup(it.name.identifier, nestedColumns)
404+
SimpleColumnGroup(name, nestedColumns)
400405
}
406+
401407
else -> {
402408
val type = when (val type = it.resolvedReturnType) {
403409
is ConeTypeParameterType -> {
@@ -408,13 +414,15 @@ private fun KotlinTypeFacade.columnOf(it: FirPropertySymbol, mapping: Map<FirTyp
408414
projection as? ConeKotlinType
409415
}
410416
}
417+
411418
else -> type
412419
}
413-
type?.let { type -> SimpleDataColumn(it.name.identifier,
414-
org.jetbrains.kotlinx.dataframe.plugin.impl.api.TypeApproximation(type)
415-
) }
420+
type?.let { type ->
421+
SimpleDataColumn(name, TypeApproximation(type))
422+
}
416423
}
417424
}
425+
}
418426

419427
private fun KotlinTypeFacade.shouldBeConvertedToColumnGroup(it: FirPropertySymbol) =
420428
isDataRow(it) ||

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ data class Record(
1010
)
1111

1212
fun box(): String {
13-
val df = dataFrameOf("a")(1).cast<Record>()
13+
val df = dataFrameOf("a")("1").cast<Record>()
1414
df.abc
15+
16+
val df1 = df.add("b") { 1 }
17+
df1.a
1518
return "OK"
1619
}

0 commit comments

Comments
 (0)