Skip to content

Commit f37bb2b

Browse files
committed
added comparisonMode to ColumnSchema.Value
1 parent 03fb5e0 commit f37bb2b

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,29 @@ public class DataFrameSchemaImpl(override val columns: Map<String, ColumnSchema>
2323
var result: CompareResult = Equals
2424

2525
// check for each column in this schema if there is a column with the same name in the other schema
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)
26+
// - if so, check those schemas for equality, taking comparisonMode into account
27+
// - if not, consider the other schema derived from this (or unrelated (None) if comparisonMode == STRICT)
2828
this.columns.forEach { (thisColName, thisSchema) ->
2929
val otherSchema = other.columns[thisColName]
3030
result += when {
3131
otherSchema != null -> {
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
32+
// increase comparisonMode strictness when dealing with nested schemas of FrameColumns or ColumnGroups
33+
val newComparisonMode =
34+
if (comparisonMode == STRICT_FOR_NESTED_SCHEMAS && thisSchema !is ColumnSchema.Value) {
35+
STRICT
36+
} else {
37+
comparisonMode
38+
}
39+
40+
thisSchema.compare(other = otherSchema, comparisonMode = newComparisonMode)
3741
}
3842

3943
else -> if (comparisonMode == STRICT) None else IsDerived
4044
}
4145
if (result == None) return None
4246
}
4347
// then check for each column in the other schema if there is a column with the same name in this schema
44-
// if not, consider the other schema as super to this (or unrelated if comparisonMode == STRICT)
48+
// if not, consider the other schema as super to this (or unrelated (None) if comparisonMode == STRICT)
4549
other.columns.forEach { (otherColName, _) ->
4650
if (this.columns[otherColName] != null) return@forEach
4751
result += if (comparisonMode == STRICT) None else IsSuper

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import org.jetbrains.kotlinx.dataframe.DataFrame
66
import org.jetbrains.kotlinx.dataframe.DataRow
77
import org.jetbrains.kotlinx.dataframe.columns.ColumnKind
88
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.LENIENT
9-
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT_FOR_NESTED_SCHEMAS
9+
import org.jetbrains.kotlinx.dataframe.schema.ComparisonMode.STRICT
1010
import kotlin.reflect.KType
1111
import kotlin.reflect.full.isSubtypeOf
1212
import kotlin.reflect.full.isSupertypeOf
@@ -41,9 +41,10 @@ public sealed class ColumnSchema {
4141
override val nullable: Boolean = type.isMarkedNullable
4242
override val contentType: KType? = null
4343

44-
public fun compare(other: Value): CompareResult =
44+
public fun compare(other: Value, comparisonMode: ComparisonMode = LENIENT): CompareResult =
4545
when {
4646
type == other.type -> CompareResult.Equals
47+
comparisonMode == STRICT -> CompareResult.None
4748
type.isSubtypeOf(other.type) -> CompareResult.IsDerived
4849
type.isSupertypeOf(other.type) -> CompareResult.IsSuper
4950
else -> CompareResult.None
@@ -95,7 +96,7 @@ public sealed class ColumnSchema {
9596
if (kind != other.kind) return CompareResult.None
9697
if (this === other) return CompareResult.Equals
9798
return when (this) {
98-
is Value -> compare(other as Value)
99+
is Value -> compare(other as Value, comparisonMode)
99100
is Group -> compare(other as Group, comparisonMode)
100101
is Frame -> compare(other as Frame, comparisonMode)
101102
}

0 commit comments

Comments
 (0)