Skip to content

refactor: drop the special cases in element masking for joins - #1332

Open
jan-glx wants to merge 2 commits into
scverse:mainfrom
jan-glx:refactor/join-masking-without-none
Open

jan-glx wants to merge 2 commits into
scverse:mainfrom
jan-glx:refactor/join-masking-without-none

Conversation

@jan-glx

@jan-glx jan-glx commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

TLDR: simplify code, avoid bugs

#1312 fixed #1226 with the smallest change, which adds special cases for "nothing matches" to the join code. This PR removes them, along with older ones, without changing behaviour:

  • _get_masked_element no longer uses None as a marker for "no table row matches". An empty array goes through the normal path, so .loc no longer needs a None fallback for shapes or points. With match_rows, the element rows to keep are the merge rows that have a table row (right_index.notna()). This replaces the length shortcut and the isin filter.
  • _match_rows returns the merge result as is. The empty-merge guard from fix: inner and right joins crashed or kept all element rows when nothing matched #1312 is no longer needed, and the .iloc[0] NaN check was dead code: the background label is dropped before the merge, and the left join drops NaN indices afterwards.

The tests from #1312 cover the no-match case for every how × match_rows combination and pass unchanged, as does the full suite.

The new asv benchmark TimeJoins times join_spatialelement_table on 200k instances, for the cases that go through the changed code. Results on a 16-core Linux server (Python 3.13), with main and this PR run alternately five times and the samples pooled (mean ± spread); asv marks none of the differences as significant:

case main this PR ratio
shapes left/left 59.8 ± 2 ms 61.4 ± 0.8 ms 1.03
shapes inner/left 77.6 ± 3 ms 75.2 ± 4 ms 0.97
shapes inner/right 82.0 ± 1 ms 81.8 ± 2 ms 1.00
shapes right/right 33.4 ± 0.9 ms 35.0 ± 1 ms 1.05
points inner/no 62.2 ± 3 ms 61.8 ± 2 ms 0.99
points right/right 28.6 ± 1 ms 29.5 ± 4 ms 1.03

Run it with asv run --python=same -b TimeJoins (about 20 s).

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.77%. Comparing base (dc164f7) to head (634ebe0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1332      +/-   ##
==========================================
- Coverage   91.77%   91.77%   -0.01%     
==========================================
  Files          53       53              
  Lines        8401     8388      -13     
==========================================
- Hits         7710     7698      -12     
+ Misses        691      690       -1     
Files with missing lines Coverage Δ
src/spatialdata/_core/query/relational_query.py 94.40% <100.00%> (+0.05%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jan-glx
jan-glx force-pushed the refactor/join-masking-without-none branch from 7476201 to 477e594 Compare November 23, 2026 16:33
jan-glx added a commit that referenced this pull request Nov 23, 2026
…ing matched (#1312)

Closes #1226.

#1266 fixed the left join when the element and the table share no
instance. The other join types still broke in that case:

- points with an `inner` or `right` join (`match_rows="no"` or
`"left"`): `KeyError: None`, because `_get_masked_element` passed `None`
to `.loc` for dask frames
- `inner` join with `match_rows="left"`: no error, but the element kept
**all** its rows next to a 0-row table
- `inner` or `right` join with `match_rows="right"`: `IndexError` from
`.iloc[0]` on the empty merge in `_match_rows`

This is the minimal fix, three changes in `relational_query.py`:

- `_get_masked_element` skips `_match_rows` when no table row matches
- for dask frames it falls back to `[]` when no table row matches, like
it already did for shapes
- `_match_rows` returns empty indices for an empty merge (the inner join
calls it again with the already emptied element)

The docstring of `join_spatialelement_table` now says what comes back
when nothing matches: an empty element or a 0-row table. `None` is only
returned for the side an exclusive join never returns (the table for
`left_exclusive`, the elements for `right_exclusive`), so the return
type depends on `how`, not on the data.

The new test runs every `how` × `match_rows` combination for shapes,
points and labels against a table that shares no instance with the
element; 9 of the shapes and points cases fail on `main`. Labels with an
`inner` join are a strict xfail: inner joins don't collect table rows
for labels (documented as unsupported), so they return no table instead
of a 0-row one.

#1332 removes the special cases this fix adds to.
_get_masked_element no longer uses None as a marker for "no table row
matches"; an empty array goes through the normal path. With match_rows, the
element rows to keep are the merge rows that have a table row
(right_index.notna()), which replaces the length shortcut and the isin filter.

_match_rows returns the merge result as is. The empty-merge guard is no longer
needed, and the .iloc[0] NaN check was dead code: the background label is
dropped before the merge and the left join drops NaN indices afterwards.
@jan-glx
jan-glx force-pushed the refactor/join-masking-without-none branch from 7168520 to 634ebe0 Compare November 23, 2026 16:38
@jan-glx
jan-glx marked this pull request as ready for review November 23, 2026 16:48

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Joins crash (IndexError / KeyError: None) when the element and the table share no instance

1 participant