@@ -3,34 +3,55 @@ package org.jetbrains.kotlinx.dataframe.impl.schema
3
3
import org.jetbrains.kotlinx.dataframe.impl.renderType
4
4
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
5
5
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
6
10
import org.jetbrains.kotlinx.dataframe.schema.DataFrameSchema
11
+ import org.jetbrains.kotlinx.dataframe.schema.plus
12
+ import kotlin.collections.forEach
7
13
8
14
public class DataFrameSchemaImpl (override val columns : Map <String , ColumnSchema >) : DataFrameSchema {
9
15
10
16
override fun compare (other : DataFrameSchema , strictlyEqualNestedSchemas : Boolean ): CompareResult {
11
17
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
20
37
}
21
- if (result == CompareResult . None ) return CompareResult . None
38
+ if (result == None ) return None
22
39
}
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
28
48
}
49
+ if (result == None ) return None
29
50
}
30
51
return result
31
52
}
32
53
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()
34
55
35
56
override fun toString (): String = render()
36
57
0 commit comments