Skip to content

fix: Apply inner rules to the elements of multi-dimensional arrays - #427

Open
Fatima Laraib (fatimaLaraibBonk) wants to merge 4 commits into
Quantco:mainfrom
fatimaLaraibBonk:fix-multidim-array-inner-rules
Open

Fatima Laraib (fatimaLaraibBonk) wants to merge 4 commits into
Quantco:mainfrom
fatimaLaraibBonk:fix-multidim-array-inner-rules

Conversation

@fatimaLaraibBonk

Copy link
Copy Markdown
Contributor

Motivation

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:

import polars as pl
import dataframely as dy

class S(dy.Schema):
    a = dy.Array(dy.String(min_length=2), shape=(2, 2))

df = pl.DataFrame(
    {"a": [[["aa", "b"], ["cc", "dd"]]]},
    schema={"a": pl.Array(pl.String, (2, 2))},
)
S.filter(df)
# SchemaError: invalid series dtype: expected `String`, got `array[str, 2]`
  • 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

1 open issue
What changed in this PR

This PR fixes multidimensional dy.Array validation so inner-column rules apply to innermost elements rather than sub-arrays.

Changes:

  • Evaluate inner rules across every array dimension while preserving one-dimensional behavior.
  • Add regression coverage for string constraints, nullability, numeric bounds, nested-array equivalence, and sampling.
File Description
tests/​column_types/​test_array.py Adds multidimensional validation and sampling regression tests.
dataframely/​columns/​array.py Wraps inner-rule evaluation for each additional array dimension.

Review effort: Balanced

Comment thread tests/column_types/test_array.py Resolved
…markers to the new tests'

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 18:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread tests/column_types/test_array.py Resolved
…markers to numeric regression tests'

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 18:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Medium severity 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.

1 resolved since last review

Review effort: Balanced

Parametrize the numeric rule test with a (2, 2, 2) shape, so the inner rule
is wrapped twice and an invalid innermost value is still caught.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 08:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants