Skip to content

Potential bug fix for nested types codegen jupyter #1223

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -58,6 +58,7 @@ internal class SchemaProcessorImpl(
private fun generateFields(
schema: DataFrameSchema,
visibility: MarkerVisibility,
openNestedDataSchemas: Boolean,
requiredSuperMarkers: List<Marker> = emptyList(),
): List<GeneratedField> {
val usedFieldNames =
Expand All @@ -66,18 +67,26 @@ internal class SchemaProcessorImpl(
fun getFieldType(columnSchema: ColumnSchema): FieldType =
when (columnSchema) {
is ColumnSchema.Value ->
FieldType.ValueFieldType(columnSchema.type.toString())
FieldType.ValueFieldType(typeFqName = columnSchema.type.toString())

is ColumnSchema.Group ->
FieldType.GroupFieldType(
process(columnSchema.schema, false, visibility).name,
markerName = process(
schema = columnSchema.schema,
isOpen = openNestedDataSchemas,
visibility = visibility,
).name,
renderAsObject = true,
)

is ColumnSchema.Frame ->
FieldType.FrameFieldType(
process(columnSchema.schema, false, visibility).name,
columnSchema.nullable,
markerName = process(
schema = columnSchema.schema,
isOpen = openNestedDataSchemas,
visibility = visibility,
).name,
nullable = columnSchema.nullable,
renderAsList = true,
)

Expand Down Expand Up @@ -145,11 +154,24 @@ internal class SchemaProcessorImpl(
}
}
}
generateFields(scheme, visibility, baseMarkers)
generateFields(
schema = scheme,
visibility = visibility,
openNestedDataSchemas = isOpen,
requiredSuperMarkers = baseMarkers,
)
} else {
generateFields(scheme, visibility)
generateFields(schema = scheme, visibility = visibility, openNestedDataSchemas = isOpen)
}
return Marker(name, isOpen, fields, baseMarkers.onlyLeafs(), visibility, emptyList(), emptyList())
return Marker(
name = name,
isOpen = isOpen,
fields = fields,
superMarkers = baseMarkers.onlyLeafs(),
visibility = visibility,
typeParameters = emptyList(),
typeArguments = emptyList(),
)
}

private fun DataFrameSchema.getRequiredMarkers() = registeredMarkers.filterRequiredForSchema(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class CodeGenerationTests : BaseTest() {
val type2 = ReplCodeGeneratorImpl.markerInterfacePrefix
val declaration1 =
"""
@DataSchema(isOpen = false)
@DataSchema
interface $type1 {
val city: String?
val name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class ReplCodeGenTests : BaseTest() {
val c = repl.process(Test5.df, Test5::df)
c.declarations shouldBe
"""
@DataSchema(isOpen = false)
@DataSchema
interface _DataFrameType3 {
val a: Int
val c: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,27 @@ class CodeGenerationTests : DataFrameJupyterTest() {
df1.leaf.c
""".checkCompilation()
}

// https://github.com/Kotlin/dataframe/issues/1222
@Test
fun `reusing marker with nullable column`() {
@Language("kt")
val _1 = """
val df1 = dataFrameOf("group" to columnOf("a" to columnOf(1, null, 3)))
val df2 = dataFrameOf("group" to columnOf("a" to columnOf(1, 2, 3)))
df1.group.a
df2.group.a
""".checkCompilation()
}

@Test
fun `not reusing marker with non-nullable column`() {
@Language("kt")
val _1 = """
val df1 = dataFrameOf("group" to columnOf("a" to columnOf(1, 2, 3)))
val df2 = dataFrameOf("group" to columnOf("a" to columnOf(1, null, 3)))
df1.group.a
df2.group.a
""".checkCompilation()
}
}