Skip to content

Commit 1d072d9

Browse files
committed
[Compiler plugin] Use name from ColumnName in user declared data schemas to get columns
1 parent ff5b397 commit 1d072d9

File tree

8 files changed

+47
-11
lines changed

8 files changed

+47
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package org.jetbrains.kotlinx.dataframe.plugin.extensions
22

33
import org.jetbrains.kotlin.GeneratedDeclarationKey
44

5-
object DataFramePlugin : GeneratedDeclarationKey()
5+
class DataFramePlugin(val columnName: String?) : GeneratedDeclarationKey()

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package org.jetbrains.kotlinx.dataframe.plugin.extensions
22

33
import org.jetbrains.kotlin.fir.FirSession
44
import org.jetbrains.kotlin.fir.analysis.checkers.toClassLikeSymbol
5+
import org.jetbrains.kotlin.fir.declarations.getAnnotationByClassId
56
import org.jetbrains.kotlinx.dataframe.plugin.utils.Names
67
import org.jetbrains.kotlinx.dataframe.plugin.utils.generateExtensionProperty
78
import org.jetbrains.kotlinx.dataframe.plugin.utils.projectOverDataColumnType
89
import org.jetbrains.kotlin.fir.declarations.hasAnnotation
910
import org.jetbrains.kotlin.fir.declarations.utils.isLocal
11+
import org.jetbrains.kotlin.fir.expressions.FirLiteralExpression
1012
import org.jetbrains.kotlin.fir.extensions.ExperimentalTopLevelDeclarationsGenerationApi
1113
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension
1214
import org.jetbrains.kotlin.fir.extensions.FirDeclarationPredicateRegistrar
@@ -27,6 +29,7 @@ import org.jetbrains.kotlin.fir.types.toSymbol
2729
import org.jetbrains.kotlin.fir.types.toTypeProjection
2830
import org.jetbrains.kotlin.name.CallableId
2931
import org.jetbrains.kotlin.name.FqName
32+
import org.jetbrains.kotlin.name.Name
3033
import org.jetbrains.kotlin.types.Variance
3134
import org.jetbrains.kotlinx.dataframe.annotations.DataSchema
3235

@@ -77,7 +80,9 @@ class ExtensionsGenerator(session: FirSession) : FirDeclarationGenerationExtensi
7780
return when (owner) {
7881
null -> fields.filter { it.callableId == callableId }.flatMap { (owner, property, callableId) ->
7982
var resolvedReturnTypeRef = property.resolvedReturnTypeRef
80-
83+
val columnName = property.getAnnotationByClassId(Names.COLUMN_NAME_ANNOTATION, session)?.let { annotation ->
84+
(annotation.argumentMapping.mapping[Names.COLUMN_NAME_ARGUMENT] as? FirLiteralExpression)?.value as? String?
85+
}
8186
val propertyName = property.name
8287
val marker = owner.constructType(arrayOf(), isNullable = false).toTypeProjection(Variance.INVARIANT)
8388

@@ -109,7 +114,9 @@ class ExtensionsGenerator(session: FirSession) : FirDeclarationGenerationExtensi
109114
ConeClassLikeLookupTagImpl(Names.DATA_ROW_CLASS_ID),
110115
typeArguments = arrayOf(marker),
111116
isNullable = false
112-
), propertyName = propertyName,
117+
),
118+
propertyName = propertyName,
119+
columnName = columnName,
113120
returnTypeRef = resolvedReturnTypeRef
114121
)
115122

@@ -132,6 +139,7 @@ class ExtensionsGenerator(session: FirSession) : FirDeclarationGenerationExtensi
132139
isNullable = false
133140
),
134141
propertyName = propertyName,
142+
columnName = columnName,
135143
returnTypeRef = columnReturnType
136144
)
137145
listOf(rowExtension.symbol, columnsContainerExtension.symbol)

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private class DataFrameFileLowering(val context: IrPluginContext) : FileLowering
134134

135135
override fun visitClass(declaration: IrClass): IrStatement {
136136
val origin = declaration.origin
137-
return if (origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey == DataFramePlugin) {
137+
return if (origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey is DataFramePlugin) {
138138
declaration.transformChildren(this, null)
139139
declaration
140140
} else {
@@ -144,7 +144,7 @@ private class DataFrameFileLowering(val context: IrPluginContext) : FileLowering
144144

145145
override fun visitConstructor(declaration: IrConstructor): IrStatement {
146146
val origin = declaration.origin
147-
if (!(origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey == DataFramePlugin)) return declaration
147+
if (!(origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey is TokenGenerator.Key)) return declaration
148148
declaration.body = generateBodyForDefaultConstructor(declaration)
149149
return declaration
150150
}
@@ -176,7 +176,8 @@ private class DataFrameFileLowering(val context: IrPluginContext) : FileLowering
176176
@OptIn(UnsafeDuringIrConstructionAPI::class)
177177
override fun visitProperty(declaration: IrProperty): IrStatement {
178178
val origin = declaration.origin
179-
if (!(origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey == DataFramePlugin)) {
179+
val pluginKey = (origin as? IrDeclarationOrigin.GeneratedByPlugin)?.pluginKey as? DataFramePlugin
180+
if (pluginKey == null) {
180181
declaration.transformChildren(this, null)
181182
return declaration
182183
}
@@ -214,7 +215,8 @@ private class DataFrameFileLowering(val context: IrPluginContext) : FileLowering
214215
val call = IrCallImpl(-1, -1, context.irBuiltIns.anyNType, get, 0, 1).also {
215216
val thisSymbol: IrValueSymbol = getter.extensionReceiverParameter?.symbol!!
216217
it.dispatchReceiver = IrGetValueImpl(-1, -1, thisSymbol)
217-
it.putValueArgument(0, IrConstImpl.string(-1, -1, context.irBuiltIns.stringType, declaration.name.identifier))
218+
val columName = pluginKey.columnName ?: declaration.name.identifier
219+
it.putValueArgument(0, IrConstImpl.string(-1, -1, context.irBuiltIns.stringType, columName))
218220
}
219221

220222
val typeOp = IrTypeOperatorCallImpl(-1, -1, returnType, IrTypeOperator.CAST, returnType, call)
@@ -229,7 +231,7 @@ private class DataFrameFileLowering(val context: IrPluginContext) : FileLowering
229231
@OptIn(UnsafeDuringIrConstructionAPI::class)
230232
override fun visitErrorCallExpression(expression: IrErrorCallExpression): IrExpression {
231233
val origin = (expression.type.classifierOrNull?.owner as? IrClass)?.origin ?: return expression
232-
val fromPlugin = origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey == DataFramePlugin
234+
val fromPlugin = origin is IrDeclarationOrigin.GeneratedByPlugin && origin.pluginKey is DataFramePlugin
233235
val scopeReference = expression.type.classFqName?.shortName()?.asString()?.startsWith("Scope") ?: false
234236
if (!(fromPlugin || scopeReference)) {
235237
return expression

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,6 @@ class TokenGenerator(session: FirSession) : FirDeclarationGenerationExtension(se
140140
}
141141

142142
override fun generateConstructors(context: MemberGenerationContext): List<FirConstructorSymbol> {
143-
return listOf(createConstructor(context.owner, DataFramePlugin, isPrimary = true).symbol)
143+
return listOf(createConstructor(context.owner, Key, isPrimary = true).symbol)
144144
}
145145
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package org.jetbrains.kotlinx.dataframe.plugin.utils
88
import org.jetbrains.kotlin.name.ClassId
99
import org.jetbrains.kotlin.name.FqName
1010
import org.jetbrains.kotlin.name.Name
11+
import org.jetbrains.kotlinx.dataframe.annotations.ColumnName
1112
import org.jetbrains.kotlinx.dataframe.annotations.Order
1213
import org.jetbrains.kotlinx.dataframe.annotations.ScopeProperty
1314
import kotlin.reflect.KClass
@@ -37,6 +38,8 @@ object Names {
3738
val ORDER_ANNOTATION = ClassId(annotationsPackage, Name.identifier(Order::class.simpleName!!))
3839
val ORDER_ARGUMENT = Name.identifier(Order::order.name)
3940
val SCOPE_PROPERTY_ANNOTATION = ClassId(annotationsPackage, Name.identifier(ScopeProperty::class.simpleName!!))
41+
val COLUMN_NAME_ANNOTATION = ClassId(annotationsPackage, Name.identifier(ColumnName::class.simpleName!!))
42+
val COLUMN_NAME_ARGUMENT = Name.identifier(ColumnName::name.name)
4043

4144
val DATA_SCHEMA_CLASS_ID = ClassId(annotationsPackage, Name.identifier("DataSchema"))
4245
val LIST = ClassId(FqName("kotlin.collections"), Name.identifier("List"))

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ internal fun FirDeclarationGenerationExtension.generateExtensionProperty(
2929
receiverType: ConeClassLikeTypeImpl,
3030
propertyName: Name,
3131
returnTypeRef: FirResolvedTypeRef,
32+
columnName: String? = null,
3233
symbol: FirClassSymbol<*>? = null,
3334
effectiveVisibility: EffectiveVisibility = EffectiveVisibility.Public
3435
): FirProperty {
3536
val firPropertySymbol = FirPropertySymbol(callableId)
3637
return buildProperty {
3738
moduleData = session.moduleData
3839
resolvePhase = FirResolvePhase.BODY_RESOLVE
39-
origin = FirDeclarationOrigin.Plugin(DataFramePlugin)
40+
origin = FirDeclarationOrigin.Plugin(DataFramePlugin(columnName))
4041
status = FirResolvedDeclarationStatusImpl(
4142
Visibilities.Public,
4243
Modality.FINAL,
@@ -66,7 +67,7 @@ internal fun FirDeclarationGenerationExtension.generateExtensionProperty(
6667
getter = buildPropertyAccessor {
6768
moduleData = session.moduleData
6869
resolvePhase = FirResolvePhase.BODY_RESOLVE
69-
origin = FirDeclarationOrigin.Plugin(DataFramePlugin)
70+
origin = FirDeclarationOrigin.Plugin(DataFramePlugin(columnName))
7071
this.returnTypeRef = returnTypeRef
7172
dispatchReceiverType = receiverType
7273
this.symbol = firPropertyAccessorSymbol
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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 Record(
8+
@ColumnName("a")
9+
val abc: String,
10+
)
11+
12+
fun box(): String {
13+
val df = dataFrameOf("a")(1).cast<Record>()
14+
df.abc
15+
return "OK"
16+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public void testColumnGroupApi() {
3434
runTest("testData/box/columnGroupApi.kt");
3535
}
3636

37+
@Test
38+
@TestMetadata("columnName.kt")
39+
public void testColumnName() {
40+
runTest("testData/box/columnName.kt");
41+
}
42+
3743
@Test
3844
@TestMetadata("columnWithStarProjection.kt")
3945
public void testColumnWithStarProjection() {

0 commit comments

Comments
 (0)