Skip to content

Commit

Permalink
Merge pull request #65 from B612-Asteroid-Institute/ak/validate-fix
Browse files Browse the repository at this point in the history
Subtables do not validate during concatenation
  • Loading branch information
akoumjian authored Sep 18, 2024
2 parents fb6d029 + 9443bcb commit d5dc6c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
7 changes: 6 additions & 1 deletion quivr/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ def __get__(self, obj: Optional[tables.Table], objtype: type) -> Union[Self, T]:
schema = self.schema.with_metadata(metadata)

subtable = pa.Table.from_arrays(array.flatten(), schema=schema)
return self.table_type.from_pyarrow(subtable, permit_nulls=self.nullable)
# We don't validate the subtable. If the parent table were validated,
# then the subtable would have been validated as part of that process.
# If the parent table is intentionally not validated, then we don't
# want to validate the subtable either as it will throw an error
# when accessing the subtable as a column (e.g. during concatenation).
return self.table_type.from_pyarrow(subtable, permit_nulls=self.nullable, validate=False)

def _nulls(self, n: int) -> pa.Array:
"""Return an array of nulls of the appropriate size."""
Expand Down
23 changes: 19 additions & 4 deletions test/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,31 @@ def test_concatenate_empty_tables():


def test_concatenate_no_validate():

class OtherTable(qv.Table):
y = qv.Int64Column(validator=qv.le(0))

class ValidationTable(qv.Table):
x = qv.Int64Column(validator=qv.ge(0))
subtable = OtherTable.as_column(nullable=True)

t1 = ValidationTable.from_kwargs(x=[-1], validate=False)
t2 = ValidationTable.from_kwargs(x=[1], validate=False)
valid = ValidationTable.from_kwargs(
x=[1], subtable=OtherTable.from_kwargs(y=[-1], validate=False), validate=False
)
invalid_x = ValidationTable.from_kwargs(x=[-1], subtable=[None], validate=False)
invalid_subtable = ValidationTable.from_kwargs(
x=[1], subtable=OtherTable.from_kwargs(y=[1], validate=False), validate=False
)

with pytest.raises(qv.ValidationError, match="Column x failed validation"):
qv.concatenate([t1, t2])
qv.concatenate([invalid_x, valid])

have = qv.concatenate([invalid_x, valid], validate=False)
assert len(have) == 2

have = qv.concatenate([t1, t2], validate=False)
# Subtables are not validated during concatenation, as the main table
# was initialized with validate=False
have = qv.concatenate([valid, invalid_subtable])
assert len(have) == 2


Expand Down

0 comments on commit d5dc6c8

Please sign in to comment.