You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: Apply inner rules to the elements of multi-dimensional arrays - #427
For a dy.Array with more than one dimension, the inner column's rules are evaluated on the sub-arrays instead of the innermost elements. Depending on the rule, validation either crashes or silently passes invalid data:
dy.Array(dy.Int64(min=0), (2, 2)) fails the same way: it cannot compare Array(Int64, 2) with an integer literal.
dy.Array(dy.Int64(nullable=False), (2, 2)) accepts a null element, because is_not_null() is checked on the sub-array, which is never null.
Schema.sample for such a schema fails too, since every sample is then validated.
The equivalent nested form, dy.Array(dy.Array(inner, 2), 2), already validates correctly, because each nesting level applies its rules one level down.
Changes
In Array.validation_rules, wrap each inner rule in one more pl.element().arr.eval(...).arr.all() for every extra dimension. A shaped array now validates exactly like the nested form, and one-dimensional arrays build the same expression as before. Null handling matches the 1D case (a null array gives null, not False). I also checked the expression on polars 1.35.2, the oldest supported version.
Tests in tests/column_types/test_array.py:
test_multidimensional_array_with_rules checks nullability and min_length on a (2, 2) string array, for both the shaped and the nested form, so the two forms are pinned to the same result.
test_multidimensional_array_with_numeric_rules checks min on a (2, 3) integer array.
test_multidimensional_array_sample_is_valid checks that Schema.sample works for a shaped array with inner rules.
Results:
Without the change, 3 of the new tests fail. The nested-form case passes, as expected.
With the change, pytest tests: 2260 passed, 30 skipped, 5 xfailed.
ruff check, ruff format --check, typos, and mypy dataframely/columns/array.py are clean.
Out of scope: an inner primary_key on a multi-dimensional array still checks duplicates among sub-arrays. What that should mean is a separate question.
For an array with more than one dimension, `dy.Array` evaluated the inner
column's rules on the sub-arrays instead of the innermost elements. Rules
such as `min_length` or `min` then failed with a polars SchemaError, and
`nullable=False` on the inner column was never enforced. Apply the rules
one dimension further down per extra dimension, as nested `dy.Array`
columns already do.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The three new array tests lack the required Arrange, Act, and Assert markers, and the sampled frame must be assigned in the Act block before validation in the Assert block.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
The numeric regression and sampling tests lack required Arrange, Act, and Assert markers, and the sampling test must assign the sampled frame in Act before validating it in Assert.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The regression tests cover only two-dimensional arrays, leaving repeated wrapping for three or more dimensions untested with an invalid innermost value.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Add regression coverage for wrapping three-dimensional arrays
tests/column_types/test_array.py:197
All new regression cases use two dimensions, so the new wrapping loop is only exercised once. Arrays with three or more dimensions—the case that requires wrapping an already-wrapped expression—remain uncovered. Please parameterize this test with a three-dimensional shape and an invalid innermost value to pin that behavior.
The reason will be displayed to describe this comment to others. Learn more.
🟢 Approval recommended
No active review issues remain for the multi-dimensional array validation change.
No open issues 🎉
Review effort: Balanced
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
For a
dy.Arraywith more than one dimension, the inner column's rules are evaluated on the sub-arrays instead of the innermost elements. Depending on the rule, validation either crashes or silently passes invalid data:dy.Array(dy.Int64(min=0), (2, 2))fails the same way: it cannot compareArray(Int64, 2)with an integer literal.dy.Array(dy.Int64(nullable=False), (2, 2))accepts anullelement, becauseis_not_null()is checked on the sub-array, which is never null.Schema.samplefor such a schema fails too, since every sample is then validated.The equivalent nested form,
dy.Array(dy.Array(inner, 2), 2), already validates correctly, because each nesting level applies its rules one level down.Changes
Array.validation_rules, wrap each inner rule in one morepl.element().arr.eval(...).arr.all()for every extra dimension. A shaped array now validates exactly like the nested form, and one-dimensional arrays build the same expression as before. Null handling matches the 1D case (a null array givesnull, notFalse). I also checked the expression on polars 1.35.2, the oldest supported version.tests/column_types/test_array.py:test_multidimensional_array_with_ruleschecks nullability andmin_lengthon a(2, 2)string array, for both the shaped and the nested form, so the two forms are pinned to the same result.test_multidimensional_array_with_numeric_ruleschecksminon a(2, 3)integer array.test_multidimensional_array_sample_is_validchecks thatSchema.sampleworks for a shaped array with inner rules.Results:
pytest tests: 2260 passed, 30 skipped, 5 xfailed.ruff check,ruff format --check,typos, andmypy dataframely/columns/array.pyare clean.Out of scope: an inner
primary_keyon a multi-dimensional array still checks duplicates among sub-arrays. What that should mean is a separate question.