Skip to content

Commit 18ba918

Browse files
committed
replaced boolean strictlyEqualNestedSchemas by comparisonMode that can change state upon recursive calls
1 parent 30540af commit 18ba918

File tree

5 files changed

+70
-59
lines changed

5 files changed

+70
-59
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/schema.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,9 @@ internal fun MutableMap<ColumnPath, Int>.putColumnsOrder(schema: DataFrameSchema
2222
val columnPath = path + name
2323
this[columnPath] = i
2424
when (column) {
25-
is ColumnSchema.Frame -> {
26-
putColumnsOrder(column.schema, columnPath)
27-
}
28-
29-
is ColumnSchema.Group -> {
30-
putColumnsOrder(column.schema, columnPath)
31-
}
25+
is ColumnSchema.Frame -> putColumnsOrder(column.schema, columnPath)
26+
is ColumnSchema.Group -> putColumnsOrder(column.schema, columnPath)
27+
is ColumnSchema.Value -> Unit
3228
}
3329
}
3430
}

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/schema/DataFrameSchemaImpl.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,44 @@ import org.jetbrains.kotlinx.dataframe.schema.CompareResult.Equals
77
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.IsDerived
88
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.IsSuper
99
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.None
10+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode
11+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT
12+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT_FOR_NESTED_SCHEMAS
1013
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
1114
import org.jetbrains.kotlinx.dataframe.schema.plus
1215
import kotlin.collections.forEach
1316

1417
public class DataFrameSchemaImpl(override val columns: Map<String, ColumnSchema>) : DataFrameSchema {
1518

16-
override fun compare(other: DataFrameSchema, strictlyEqualNestedSchemas: Boolean): CompareResult {
19+
override fun compare(other: DataFrameSchema, comparisonMode: ComparisonMode): CompareResult {
1720
require(other is DataFrameSchemaImpl)
1821
if (this === other) return Equals
1922

2023
var result: CompareResult = Equals
2124

2225
// check for each column in this schema if there is a column with the same name in the other schema
23-
// - if so, strictly compare those schemas for equality, taking strictlyEqualNestedSchemas into account
24-
// - if not, consider the other schema derived from this (or unrelated if strictlyEqualNestedSchemas == true)
26+
// - if so, those schemas for equality, taking comparisonMode into account
27+
// - if not, consider the other schema derived from this (or unrelated if comparisonMode == STRICT)
2528
this.columns.forEach { (thisColName, thisSchema) ->
2629
val otherSchema = other.columns[thisColName]
2730
result += when {
2831
otherSchema != null -> {
29-
val comparison = thisSchema.compareStrictlyEqualNestedSchemas(otherSchema)
30-
when {
31-
comparison != Equals && strictlyEqualNestedSchemas -> None
32-
else -> comparison
33-
}
32+
val comparison = thisSchema.compare(
33+
other = otherSchema,
34+
comparisonMode = if (comparisonMode == STRICT_FOR_NESTED_SCHEMAS) STRICT else comparisonMode,
35+
)
36+
if (comparison != Equals && comparisonMode == STRICT) None else comparison
3437
}
3538

36-
else -> if (strictlyEqualNestedSchemas) None else IsDerived
39+
else -> if (comparisonMode == STRICT) None else IsDerived
3740
}
3841
if (result == None) return None
3942
}
4043
// then check for each column in the other schema if there is a column with the same name in this schema
41-
// if not, consider the other schema as super to this (or unrelated if strictlyEqualNestedSchemas == true)
44+
// if not, consider the other schema as super to this (or unrelated if comparisonMode == STRICT)
4245
other.columns.forEach { (otherColName, _) ->
4346
if (this.columns[otherColName] != null) return@forEach
44-
45-
result += when {
46-
strictlyEqualNestedSchemas -> None
47-
else -> IsSuper
48-
}
47+
result += if (comparisonMode == STRICT) None else IsSuper
4948
if (result == None) return None
5049
}
5150
return result

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/schema/ColumnSchema.kt

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import org.jetbrains.kotlinx.dataframe.AnyRow
55
import org.jetbrains.kotlinx.dataframe.DataFrame
66
import org.jetbrains.kotlinx.dataframe.DataRow
77
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
8+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.LENIENT
9+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT
10+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT_FOR_NESTED_SCHEMAS
811
import kotlin.reflect.KType
912
import kotlin.reflect.full.isSubtypeOf
1013
import kotlin.reflect.full.isSupertypeOf
1114
import kotlin.reflect.typeOf
1215

13-
public abstract class ColumnSchema {
16+
public sealed class ColumnSchema {
1417

1518
/** Either [Value] or [Group] or [Frame]. */
1619
public abstract val kind: ColumnKind
@@ -55,10 +58,11 @@ public abstract class ColumnSchema {
5558
override val nullable: Boolean = false
5659
override val type: KType get() = typeOf<AnyRow>()
5760

58-
public fun compare(other: Group): CompareResult = schema.compare(other.schema)
59-
60-
internal fun compareStrictlyEqualNestedSchemas(other: Group): CompareResult =
61-
schema.compare(other.schema, strictlyEqualNestedSchemas = true)
61+
public fun compare(other: Group, comparisonMode: ComparisonMode = LENIENT): CompareResult =
62+
schema.compare(
63+
other = other.schema,
64+
comparisonMode = comparisonMode,
65+
)
6266
}
6367

6468
public class Frame(
@@ -69,14 +73,11 @@ public abstract class ColumnSchema {
6973
public override val kind: ColumnKind = ColumnKind.Frame
7074
override val type: KType get() = typeOf<AnyFrame>()
7175

72-
public fun compare(other: Frame): CompareResult =
73-
schema.compare(other.schema).combine(CompareResult.compareNullability(nullable, other.nullable))
74-
75-
internal fun compareStrictlyEqualNestedSchemas(other: Frame): CompareResult =
76+
public fun compare(other: Frame, comparisonMode: ComparisonMode = LENIENT): CompareResult =
7677
schema.compare(
77-
other.schema,
78-
strictlyEqualNestedSchemas = true,
79-
).combine(CompareResult.compareNullability(nullable, other.nullable))
78+
other = other.schema,
79+
comparisonMode = comparisonMode,
80+
) + CompareResult.compareNullability(thisIsNullable = nullable, otherIsNullable = other.nullable)
8081
}
8182

8283
/** Checks equality just on kind, type, or schema. */
@@ -88,37 +89,27 @@ public abstract class ColumnSchema {
8889
is Value -> type == (otherType as Value).type
8990
is Group -> schema == (otherType as Group).schema
9091
is Frame -> schema == (otherType as Frame).schema
91-
else -> throw NotImplementedError()
9292
}
9393
}
9494

95-
public fun compare(other: ColumnSchema): CompareResult = compare(other, false)
96-
97-
internal fun compareStrictlyEqualNestedSchemas(other: ColumnSchema): CompareResult = compare(other, true)
98-
99-
private fun compare(other: ColumnSchema, strictlyEqualNestedSchemas: Boolean): CompareResult {
95+
public fun compare(other: ColumnSchema, comparisonMode: ComparisonMode = LENIENT): CompareResult {
10096
if (kind != other.kind) return CompareResult.None
10197
if (this === other) return CompareResult.Equals
10298
return when (this) {
10399
is Value -> compare(other as Value)
100+
is Group -> compare(other as Group, comparisonMode)
101+
is Frame -> compare(other as Frame, comparisonMode)
102+
}
103+
}
104104

105-
is Group -> if (strictlyEqualNestedSchemas) {
106-
compareStrictlyEqualNestedSchemas(
107-
other as Group,
108-
)
109-
} else {
110-
compare(other as Group)
111-
}
112-
113-
is Frame -> if (strictlyEqualNestedSchemas) {
114-
compareStrictlyEqualNestedSchemas(
115-
other as Frame,
116-
)
117-
} else {
118-
compare(other as Frame)
119-
}
120-
121-
else -> throw NotImplementedError()
105+
override fun hashCode(): Int {
106+
var result = nullable.hashCode()
107+
result = 31 * result + kind.hashCode()
108+
result = 31 * result + when (this) {
109+
is Value -> type.hashCode()
110+
is Group -> schema.hashCode()
111+
is Frame -> schema.hashCode()
122112
}
113+
return result
123114
}
124115
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.jetbrains.kotlinx.dataframe.schema
2+
3+
public enum class ComparisonMode {
4+
/**
5+
* In this mode, all [CompareResults][CompareResult] can occur.
6+
*
7+
* If this schema has columns the other has not, the other is considered [CompareResult.IsDerived].
8+
* If the other schema has columns this has not, this is considered [CompareResult.IsSuper].
9+
*/
10+
LENIENT,
11+
12+
/**
13+
* Columns must all be present in the other schema with the same name and type.
14+
* [CompareResult.IsDerived] and [CompareResult.IsSuper] will result in [CompareResult.None] in this mode.
15+
*/
16+
STRICT,
17+
18+
/** Works like [LENIENT] at the top-level, but turns to [STRICT] for nested schemas. */
19+
STRICT_FOR_NESTED_SCHEMAS,
20+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package org.jetbrains.kotlinx.dataframe.schema
22

3+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT_FOR_NESTED_SCHEMAS
4+
35
public interface DataFrameSchema {
46

57
public val columns: Map<String, ColumnSchema>
68

79
/**
810
* By default generated markers for leafs aren't used as supertypes: @DataSchema(isOpen = false)
9-
* strictlyEqualNestedSchemas = true takes this into account for internal codegen logic
11+
* [ComparisonMode.STRICT_FOR_NESTED_SCHEMAS] takes this into account for internal codegen logic
1012
*/
11-
public fun compare(other: DataFrameSchema, strictlyEqualNestedSchemas: Boolean = false): CompareResult
13+
public fun compare(
14+
other: DataFrameSchema,
15+
comparisonMode: ComparisonMode = STRICT_FOR_NESTED_SCHEMAS,
16+
): CompareResult
1217
}

0 commit comments

Comments
 (0)