|
| 1 | +""" |
| 2 | +Direct tests for the compiled Cython extension `astropy.table._np_utils`. |
| 3 | +
|
| 4 | +`_np_utils` contains a single function, `join_inner()`, which is the core |
| 5 | +index-computation engine for all table join operations in astropy. |
| 6 | +These tests bypass the public `astropy.table.join()` API to test the |
| 7 | +compiled extension directly, fulfilling the goal in #19249 of building |
| 8 | +standalone test coverage for performance-critical extensions. |
| 9 | +
|
| 10 | +Valid inputs to `join_inner()` are manually constructed here, reproducing |
| 11 | +the pre-processing logic from `astropy/table/operations.py`. |
| 12 | +The join_type integer encoding is: |
| 13 | +0 = inner |
| 14 | +1 = outer |
| 15 | +2 = left |
| 16 | +3 = right |
| 17 | +""" |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +import pytest |
| 21 | + |
| 22 | +from astropy.table import _np_utils |
| 23 | + |
| 24 | +def _make_join_inputs(left_keys, right_keys): |
| 25 | + """ |
| 26 | + Reproduces the pre-processing from `operations.py` that prepares |
| 27 | + index arrays for `join_inner()`. |
| 28 | + """ |
| 29 | + combined = np.concatenate([left_keys, right_keys]) |
| 30 | + idx_sort = combined.argsort(kind="stable") |
| 31 | + sorted_keys = combined[idx_sort] |
| 32 | + diffs = np.concatenate(([True], sorted_keys[1:] != sorted_keys[:-1], [True])) |
| 33 | + idxs = np.flatnonzero(diffs) |
| 34 | + |
| 35 | + # idxs and idx_sort must be passed as intp arrays |
| 36 | + idxs = np.asarray(idxs, dtype=np.intp) |
| 37 | + idx_sort = np.asarray(idx_sort, dtype=np.intp) |
| 38 | + |
| 39 | + return idxs, idx_sort, len(left_keys) |
| 40 | + |
| 41 | +def test_module_importable(): |
| 42 | + assert _np_utils is not None |
| 43 | + |
| 44 | +def test_join_inner_exists_and_is_callable(): |
| 45 | + assert hasattr(_np_utils, "join_inner") |
| 46 | + assert callable(_np_utils.join_inner) |
| 47 | + |
| 48 | +def test_single_public_symbol(): |
| 49 | + # Only 'join_inner' is expected to be public |
| 50 | + public_symbols = [s for s in dir(_np_utils) if not s.startswith("_")] |
| 51 | + assert public_symbols == ["join_inner"] |
| 52 | + |
| 53 | +def test_returns_six_values(): |
| 54 | + idxs, idx_sort, len_left = _make_join_inputs([1], [1]) |
| 55 | + result = _np_utils.join_inner(idxs, idx_sort, len_left, 0) |
| 56 | + assert isinstance(result, tuple) |
| 57 | + assert len(result) == 6 |
| 58 | + |
| 59 | +class TestInnerJoin: |
| 60 | + join_type = 0 |
| 61 | + |
| 62 | + def test_perfect_match(self): |
| 63 | + left, right = [1, 2, 3], [1, 2, 3] |
| 64 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 65 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 66 | + idxs, idx_sort, len_left, self.join_type |
| 67 | + ) |
| 68 | + assert n_out == len(left) |
| 69 | + assert not masked |
| 70 | + |
| 71 | + def test_partial_overlap(self): |
| 72 | + left, right = [1, 2], [2, 3] |
| 73 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 74 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 75 | + idxs, idx_sort, len_left, self.join_type |
| 76 | + ) |
| 77 | + assert n_out == 1 |
| 78 | + |
| 79 | + def test_no_overlap(self): |
| 80 | + left, right = [1, 2], [3, 4] |
| 81 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 82 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 83 | + idxs, idx_sort, len_left, self.join_type |
| 84 | + ) |
| 85 | + assert n_out == 0 |
| 86 | + |
| 87 | + def test_duplicate_keys_cartesian(self): |
| 88 | + left, right = [1, 1], [1, 1] |
| 89 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 90 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 91 | + idxs, idx_sort, len_left, self.join_type |
| 92 | + ) |
| 93 | + assert n_out == 4 |
| 94 | + |
| 95 | + def test_large_cartesian(self): |
| 96 | + left, right = [7] * 5, [7] * 5 |
| 97 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 98 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 99 | + idxs, idx_sort, len_left, self.join_type |
| 100 | + ) |
| 101 | + assert n_out == 25 |
| 102 | + |
| 103 | + |
| 104 | +class TestOuterJoin: |
| 105 | + join_type = 1 |
| 106 | + |
| 107 | + def test_disjoint_all_rows_kept(self): |
| 108 | + left, right = [1, 2], [3, 4] |
| 109 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 110 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 111 | + idxs, idx_sort, len_left, self.join_type |
| 112 | + ) |
| 113 | + assert n_out == 4 |
| 114 | + assert masked |
| 115 | + |
| 116 | + def test_partial_overlap(self): |
| 117 | + left, right = [1, 2], [2, 3] |
| 118 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 119 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 120 | + idxs, idx_sort, len_left, self.join_type |
| 121 | + ) |
| 122 | + assert n_out == 3 |
| 123 | + assert masked |
| 124 | + |
| 125 | + def test_perfect_match_no_masking(self): |
| 126 | + left, right = [1, 2, 3], [1, 2, 3] |
| 127 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 128 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 129 | + idxs, idx_sort, len_left, self.join_type |
| 130 | + ) |
| 131 | + assert not masked |
| 132 | + |
| 133 | + |
| 134 | +class TestLeftJoin: |
| 135 | + join_type = 2 |
| 136 | + |
| 137 | + def test_all_left_rows_kept(self): |
| 138 | + left, right = [1, 2, 3], [2] |
| 139 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 140 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 141 | + idxs, idx_sort, len_left, self.join_type |
| 142 | + ) |
| 143 | + assert n_out == 3 |
| 144 | + assert masked |
| 145 | + |
| 146 | + def test_perfect_match_no_masking(self): |
| 147 | + left, right = [1, 2, 3], [1, 2, 3] |
| 148 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 149 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 150 | + idxs, idx_sort, len_left, self.join_type |
| 151 | + ) |
| 152 | + assert not masked |
| 153 | + |
| 154 | + def test_right_only_keys_ignored(self): |
| 155 | + left, right = [1], [1, 2, 3] |
| 156 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 157 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 158 | + idxs, idx_sort, len_left, self.join_type |
| 159 | + ) |
| 160 | + assert n_out == 1 |
| 161 | + |
| 162 | + |
| 163 | +class TestRightJoin: |
| 164 | + join_type = 3 |
| 165 | + |
| 166 | + def test_all_right_rows_kept(self): |
| 167 | + left, right = [2], [1, 2, 3] |
| 168 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 169 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 170 | + idxs, idx_sort, len_left, self.join_type |
| 171 | + ) |
| 172 | + assert n_out == 3 |
| 173 | + assert masked |
| 174 | + |
| 175 | + def test_perfect_match_no_masking(self): |
| 176 | + left, right = [1, 2], [1, 2] |
| 177 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 178 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 179 | + idxs, idx_sort, len_left, self.join_type |
| 180 | + ) |
| 181 | + assert not masked |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.parametrize("join_type", [0, 1, 2, 3]) |
| 185 | +def test_output_array_lengths_match_n_out(join_type): |
| 186 | + left, right = [1, 2], [2, 3] |
| 187 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 188 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 189 | + idxs, idx_sort, len_left, join_type |
| 190 | + ) |
| 191 | + assert len(left_out) == n_out |
| 192 | + assert len(right_out) == n_out |
| 193 | + |
| 194 | +@pytest.mark.parametrize("join_type", [0, 1, 2, 3]) |
| 195 | +def test_mask_array_lengths_match_n_out(join_type): |
| 196 | + left, right = [1, 2], [2, 3] |
| 197 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 198 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 199 | + idxs, idx_sort, len_left, join_type |
| 200 | + ) |
| 201 | + assert len(left_mask) == n_out |
| 202 | + assert len(right_mask) == n_out |
| 203 | + |
| 204 | + |
| 205 | +def test_single_matching_row(): |
| 206 | + left, right = [42], [42] |
| 207 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 208 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 209 | + idxs, idx_sort, len_left, 0 |
| 210 | + ) |
| 211 | + assert n_out == 1 |
| 212 | + assert not masked |
| 213 | + |
| 214 | +def test_single_non_matching_row_inner(): |
| 215 | + left, right = [1], [2] |
| 216 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 217 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 218 | + idxs, idx_sort, len_left, 0 |
| 219 | + ) |
| 220 | + assert n_out == 0 |
| 221 | + |
| 222 | +def test_many_unique_keys_inner(): |
| 223 | + left = list(range(100)) |
| 224 | + right = list(range(100)) |
| 225 | + idxs, idx_sort, len_left = _make_join_inputs(left, right) |
| 226 | + masked, n_out, left_out, left_mask, right_out, right_mask = _np_utils.join_inner( |
| 227 | + idxs, idx_sort, len_left, 0 |
| 228 | + ) |
| 229 | + assert n_out == 100 |
| 230 | + assert not masked |
0 commit comments