Skip to content

Commit 30540af

Browse files
committed
#1222: refactor, to understand how DataFrameSchemaImpl.compare is working. Changed behavior for nested schema comparison when strictlyEqualNestedSchemas == true
1 parent 2b9d951 commit 30540af

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,55 @@ package org.jetbrains.kotlinx.dataframe.impl.schema
33
import org.jetbrains.kotlinx.dataframe.impl.renderType
44
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
55
import org.jetbrains.kotlinx.dataframe.schema.CompareResult
6+
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.Equals
7+
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.IsDerived
8+
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.IsSuper
9+
import org.jetbrains.kotlinx.dataframe.schema.CompareResult.None
610
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
11+
import org.jetbrains.kotlinx.dataframe.schema.plus
12+
import kotlin.collections.forEach
713

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

1016
override fun compare(other: DataFrameSchema, strictlyEqualNestedSchemas: Boolean): CompareResult {
1117
require(other is DataFrameSchemaImpl)
12-
if (this === other) return CompareResult.Equals
13-
var result = CompareResult.Equals
14-
columns.forEach {
15-
val otherColumn = other.columns[it.key]
16-
if (otherColumn == null) {
17-
result = result.combine(if (strictlyEqualNestedSchemas) CompareResult.None else CompareResult.IsDerived)
18-
} else {
19-
result = result.combine(it.value.compareStrictlyEqualNestedSchemas(otherColumn))
18+
if (this === other) return Equals
19+
20+
var result: CompareResult = Equals
21+
22+
// 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)
25+
this.columns.forEach { (thisColName, thisSchema) ->
26+
val otherSchema = other.columns[thisColName]
27+
result += when {
28+
otherSchema != null -> {
29+
val comparison = thisSchema.compareStrictlyEqualNestedSchemas(otherSchema)
30+
when {
31+
comparison != Equals && strictlyEqualNestedSchemas -> None
32+
else -> comparison
33+
}
34+
}
35+
36+
else -> if (strictlyEqualNestedSchemas) None else IsDerived
2037
}
21-
if (result == CompareResult.None) return CompareResult.None
38+
if (result == None) return None
2239
}
23-
other.columns.forEach {
24-
val thisField = columns[it.key]
25-
if (thisField == null) {
26-
result = result.combine(if (strictlyEqualNestedSchemas) CompareResult.None else CompareResult.IsSuper)
27-
if (result == CompareResult.None) return CompareResult.None
40+
// 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)
42+
other.columns.forEach { (otherColName, _) ->
43+
if (this.columns[otherColName] != null) return@forEach
44+
45+
result += when {
46+
strictlyEqualNestedSchemas -> None
47+
else -> IsSuper
2848
}
49+
if (result == None) return None
2950
}
3051
return result
3152
}
3253

33-
override fun equals(other: Any?): Boolean = other is DataFrameSchema && compare(other).isEqual()
54+
override fun equals(other: Any?): Boolean = other is DataFrameSchema && this.compare(other).isEqual()
3455

3556
override fun toString(): String = render()
3657

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ public enum class CompareResult {
2828
}
2929
}
3030
}
31+
32+
public operator fun CompareResult.plus(other: CompareResult): CompareResult = this.combine(other)

0 commit comments

Comments
 (0)