Skip to content
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

fixed structfield comparison when dataType is array #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 1 addition & 14 deletions chispa/schema_comparer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from chispa.prettytable import PrettyTable
from chispa.bcolors import *
import chispa.six as six
from chispa.structfield_comparer import are_structfields_equal
from chispa.structfield_comparer import are_structfields_equal, check_type_equal_ignore_nullable


class SchemasNotEqualError(Exception):
Expand Down Expand Up @@ -52,17 +52,4 @@ def are_schemas_equal_ignore_nullable(s1, s2):
return True


def check_type_equal_ignore_nullable(sf1, sf2):
"""Checks StructField data types ignoring nullables.

Handles array element types also.
"""
dt1, dt2 = sf1.dataType, sf2.dataType
if dt1.typeName() == dt2.typeName():
# Account for array types by inspecting elementType.
if dt1.typeName() == 'array':
return dt1.elementType == dt2.elementType
else:
return True
else:
return False
19 changes: 18 additions & 1 deletion chispa/structfield_comparer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@

def check_type_equal_ignore_nullable(sf1, sf2):
"""Checks StructField data types ignoring nullables.

Handles array element types also.
"""
dt1, dt2 = sf1.dataType, sf2.dataType
if dt1.typeName() == dt2.typeName():
# Account for array types by inspecting elementType.
if dt1.typeName() == 'array':
return dt1.elementType == dt2.elementType
else:
return True
else:
return False


def are_structfields_equal(sf1, sf2, ignore_nullability=False):
if ignore_nullability:
if sf1 is None and sf2 is not None:
return False
elif sf1 is not None and sf2 is None:
return False
elif sf1.name != sf2.name or sf1.dataType != sf2.dataType:
elif sf1.name != sf2.name or not check_type_equal_ignore_nullable(sf1, sf2):
return False
else:
return True
Expand Down
5 changes: 5 additions & 0 deletions tests/test_structfield_comparer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ def it_can_perform_nullability_insensitive_comparisons():
sf1 = StructField("hi", IntegerType(), False)
sf2 = StructField("hi", IntegerType(), True)
assert are_structfields_equal(sf1, sf2, ignore_nullability=True) == True

def it_can_perform_nullability_insensitive_comparisons_with_arrays():
sf1 = StructField("hi", ArrayType(IntegerType(), True), False)
sf2 = StructField("hi", ArrayType(IntegerType(), False), True)
assert are_structfields_equal(sf1, sf2, ignore_nullability=True) == True