datafusion-physical-plan 56.0.0

Physical (ExecutionPlan) implementations for DataFusion query engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

//! `NOT IN` (null-aware) semantics of the hash join.
//!
//! `x NOT IN (subquery)` is `NOT (x IN (subquery))` under SQL three-valued
//! logic: `x IN (..)` is TRUE when some subquery value equals `x`, UNKNOWN
//! (NULL) when none does but a NULL on either side might, and FALSE
//! otherwise. `NOT IN` keeps a row only where `IN` is FALSE.
//!
//! The regular hash join finds the equal pairs; what NULLs change in the
//! rows a `NOT IN` emits is here. The join reaches it through:
//! - [`NotIn::try_new`] when the plan is built or executed: is this join
//!   legal, and which form is it;
//! - [`NotInBuild::try_new`] once per build, from `collect_left_input`;
//! - [`NotInProbe::before_lookup`] and [`NotInProbe::after_lookup`] around
//!   each probe batch's lookup, or [`NotInProbe::after_empty_build`] when
//!   the build map is empty and there is no lookup;
//! - [`NotInBuild::final_chunk`] on each chunk of build rows emitted after
//!   the probe side is exhausted.

use std::sync::Arc;

use parking_lot::Mutex;

use arrow::array::{
    Array, ArrayRef, AsArray, BooleanArray, BooleanBufferBuilder, RecordBatch,
    UInt32Array, UInt64Array,
};
use arrow::buffer::{BooleanBuffer, NullBuffer};
use arrow::compute::{filter, filter_record_batch, take};
use arrow::util::bit_util;
use datafusion_common::hash_utils::{RandomState, create_hashes};
use datafusion_common::{JoinSide, JoinType, NullEquality, Result, plan_err};
use datafusion_execution::memory_pool::MemoryReservation;
use datafusion_expr::{ColumnarValue, Operator};
use datafusion_physical_expr::PhysicalExprRef;
use datafusion_physical_expr_common::datum::apply_cmp;

use crate::joins::PartitionMode;
use crate::joins::SharedBitmapBuilder;
use crate::joins::hash_join::exec::{JoinLeftData, new_join_hashmap};
use crate::joins::join_hash_map::ProbeOffset::{AtRow, MidChain};
use crate::joins::join_hash_map::{JoinHashMapType, ProbeOffset};
use crate::joins::probe_completion::ProbeSideSummary;
use crate::joins::utils::{
    BuildProbeJoinMetrics, JoinFilter, apply_join_filter_to_indices,
    equal_rows_arr_with_normalized_left, matchable_join_keys, update_hash,
};

/// Which `NOT IN` a null-aware join computes, derived from
/// `HashJoinExec::null_aware`, the join type, the keys and the filter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum NotIn {
    /// `build.key NOT IN (probe.key)`: the final stage gives every build row
    /// its `IN` mark, from the visited bitmap and the NULLs seen.
    BuildRows {
        scope: NotInScope,
        output: NotInOutput,
    },
    /// `probe.key NOT IN (build.key)`, `CollectLeft` (`RightAnti`): emits the
    /// probe rows whose `IN` is FALSE, decided while streaming because every
    /// probe row sees the whole build side.
    ProbeRows { scope: NotInScope },
}

/// Which rows of the other side a NULL can make a row's `IN` UNKNOWN for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum NotInScope {
    /// One value key, no correlation keys, no filter: a NULL anywhere on the
    /// other side makes every unmatched row UNKNOWN. [`NotIn::BuildRows`]
    /// probe partitions share one summary of what they saw
    /// (`ProbeCompletion`); [`NotIn::ProbeRows`] emits nothing once any build
    /// key is NULL, and never a probe row whose key is NULL.
    Uncorrelated,
    /// Correlation scope keys (`on[value_keys..]`), a multi-column value key
    /// (`on[..value_keys]`), or a filter: only the rows in the same scope
    /// count, so UNKNOWN is decided per row (see
    /// [`mark_null_candidates_for_probe_batch`]).
    Correlated { value_keys: usize },
}

/// What a [`NotIn::BuildRows`] join emits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum NotInOutput {
    /// `LeftAnti`: the build rows whose mark is FALSE.
    Rows,
    /// `LeftMark`: every build row with its mark, a nullable Boolean column.
    Mark,
}

impl NotIn {
    /// Validates that `null_aware` may be set for this join and returns its
    /// form. The errors are those of the legality table this replaced.
    pub(super) fn try_new(
        join_type: JoinType,
        partition_mode: PartitionMode,
        num_keys: usize,
        num_value_keys: usize,
        has_filter: bool,
    ) -> Result<Self> {
        // The join type and mode come first: no value key count can make up
        // for them. `None` is `RightAnti`, which emits probe rows.
        let output = match (join_type, partition_mode) {
            (JoinType::LeftAnti, _) => Some(NotInOutput::Rows),
            (JoinType::LeftMark, _) => Some(NotInOutput::Mark),
            // `PartitionMode::CollectLeft` is safe because `RightAnti` is
            // probe-driven: every probe row sees the whole build side.
            (JoinType::RightAnti, PartitionMode::CollectLeft) => None,
            _ => {
                return plan_err!(
                    "null_aware can only be true for LeftAnti joins and RightAnti joins with `CollectLeft` `PartitionMode`, or LeftMark joins, got {join_type} with {partition_mode}"
                );
            }
        };
        // Build-driven joins decide in the final stage from state shared
        // across the probe partitions: the per-build-row null bitmap for
        // the correlated forms, and the probe-side NULL flag for the
        // uncorrelated ones. `Partitioned` gives each output partition its
        // own build side, so neither is global there.
        if partition_mode == PartitionMode::Partitioned {
            return plan_err!(
                "null_aware joins require PartitionMode::CollectLeft, got PartitionMode::Partitioned"
            );
        }
        // `0` is an unset count, which is only unambiguous with a single key:
        // a scalar `NOT IN`. With more keys, any default would silently pick
        // which of them are `NOT IN` value keys and which are correlation
        // scope keys.
        let num_value_keys = match num_value_keys {
            0 if num_keys == 1 => 1,
            0 => {
                return plan_err!(
                    "null_aware {join_type} join with {num_keys} keys needs the number of its `NOT IN` value keys, set it with `with_null_aware_value_keys`"
                );
            }
            n if n > num_keys => {
                return plan_err!(
                    "null_aware {join_type} join needs between 1 and {num_keys} `NOT IN` value keys, got {n}"
                );
            }
            n => n,
        };
        // `num_keys > 1` also covers a multi-column value key without any
        // correlation: a NULL in one tuple element leaves the comparison FALSE
        // whenever another element is a definite mismatch, so it too must be
        // decided per row.
        let scope = if num_keys > 1 || has_filter {
            NotInScope::Correlated {
                value_keys: num_value_keys,
            }
        } else {
            NotInScope::Uncorrelated
        };
        Ok(match output {
            Some(output) => Self::BuildRows { scope, output },
            None => Self::ProbeRows { scope },
        })
    }

    fn scope(self) -> NotInScope {
        match self {
            Self::BuildRows { scope, .. } | Self::ProbeRows { scope } => scope,
        }
    }

    /// Number of leading keys that are `NOT IN` value keys; the keys after
    /// them are correlation scope keys.
    pub(super) fn value_keys(self) -> usize {
        match self.scope() {
            NotInScope::Correlated { value_keys } => value_keys,
            NotInScope::Uncorrelated => 1,
        }
    }
}

/// What a build side keeps for its `NOT IN`, one variant per form.
pub(super) enum NotInBuild {
    /// Correlated [`NotIn::BuildRows`]: UNKNOWN is recorded per build row.
    BuildRowsCorrelated {
        value_keys: usize,
        output: NotInOutput,
        /// One bit per build row, set once its mark is UNKNOWN; shared by
        /// every probe partition, and never cleared.
        unknown: SharedBitmapBuilder,
        /// Hash table over the scope keys (`on[value_keys..]`) of all build
        /// rows, probed with NULL-valued probe rows. `None` without scope keys.
        scope_map: Option<Box<dyn JoinHashMapType>>,
        /// The build rows with a NULL value key, `None` when there are none.
        null_value_rows: Option<NullValueBuildRows>,
    },
    /// Uncorrelated [`NotIn::BuildRows`]: what every probe partition saw
    /// decides every unmatched mark.
    BuildRowsUncorrelated {
        output: NotInOutput,
        /// Logical NULLs of the value key, `None` when it has none. A NULL
        /// build key's mark is UNKNOWN once the probe side has a row.
        key_nulls: Option<NullBuffer>,
    },
    /// Correlated [`NotIn::ProbeRows`]: UNKNOWN is decided per probe row,
    /// within its probe batch, so nothing is shared across probe partitions.
    /// The fields are those of [`Self::BuildRowsCorrelated`].
    ProbeRowsCorrelated {
        value_keys: usize,
        scope_map: Option<Box<dyn JoinHashMapType>>,
        null_value_rows: Option<NullValueBuildRows>,
    },
    /// Uncorrelated [`NotIn::ProbeRows`].
    ProbeRowsUncorrelated {
        /// Whether a build key is NULL: then no probe row is emitted.
        build_has_null: bool,
    },
}

impl NotInBuild {
    /// Builds the `NOT IN` state of a collected build side and charges it to
    /// `reservation`. `keys` are the build keys as evaluated, `normalized`
    /// with `-0.0` rewritten (see `JoinLeftData::values`).
    #[expect(clippy::too_many_arguments)]
    pub(super) fn try_new(
        not_in: NotIn,
        on_left: &[PhysicalExprRef],
        batch: &RecordBatch,
        keys: &[ArrayRef],
        normalized: &[ArrayRef],
        random_state: &RandomState,
        reservation: &mut MemoryReservation,
        metrics: &BuildProbeJoinMetrics,
    ) -> Result<Self> {
        let num_rows = batch.num_rows();
        let correlated_row_state = |value_keys, reservation: &mut MemoryReservation| {
            correlated_row_state(
                on_left,
                batch,
                keys,
                normalized,
                value_keys,
                random_state,
                reservation,
                metrics,
            )
        };
        match not_in {
            NotIn::ProbeRows {
                scope: NotInScope::Uncorrelated,
            } => Ok(Self::ProbeRowsUncorrelated {
                build_has_null: keys[0].logical_null_count() > 0,
            }),
            NotIn::ProbeRows {
                scope: NotInScope::Correlated { value_keys },
            } => {
                let (scope_map, null_value_rows) =
                    correlated_row_state(value_keys, reservation)?;
                Ok(Self::ProbeRowsCorrelated {
                    value_keys,
                    scope_map,
                    null_value_rows,
                })
            }
            NotIn::BuildRows {
                scope: NotInScope::Uncorrelated,
                output,
            } => {
                let key_nulls = keys[0].logical_nulls().filter(|n| n.null_count() > 0);
                if let Some(nulls) = &key_nulls {
                    // `logical_nulls` reuses an array's physical null buffer
                    // when it can, which the batch reservation already holds.
                    // Charge only a newly materialized one (dictionaries).
                    let reuses_input_buffer = keys[0]
                        .nulls()
                        .is_some_and(|input| input.inner().ptr_eq(nulls.inner()));
                    if !reuses_input_buffer {
                        let size = nulls.buffer().capacity();
                        reservation.try_grow(size)?;
                        metrics.build_mem_used.add(size);
                    }
                }
                Ok(Self::BuildRowsUncorrelated { output, key_nulls })
            }
            NotIn::BuildRows {
                scope: NotInScope::Correlated { value_keys },
                output,
            } => {
                let bitmap_size = bit_util::ceil(num_rows, 8);
                reservation.try_grow(bitmap_size)?;
                metrics.build_mem_used.add(bitmap_size);
                let mut unknown = BooleanBufferBuilder::new(num_rows);
                unknown.append_n(num_rows, false);

                let (scope_map, null_value_rows) =
                    correlated_row_state(value_keys, reservation)?;
                Ok(Self::BuildRowsCorrelated {
                    value_keys,
                    output,
                    unknown: Mutex::new(unknown),
                    scope_map,
                    null_value_rows,
                })
            }
        }
    }

    /// The rows to emit for one chunk of final build rows, once every probe
    /// partition has finished (`summary`), with the mark column for
    /// [`NotInOutput::Mark`].
    ///
    /// A matched build row's `IN` mark is TRUE. An unmatched one's is UNKNOWN
    /// (NULL) or FALSE (the "null bucket" and `hadNull` of Neumann, Leis and
    /// Kemper, "The Complete Story of Joins (in HyPer)", Section 5.6):
    /// - correlated: UNKNOWN where the bitmap recorded it;
    /// - uncorrelated, a probe key was NULL: UNKNOWN;
    /// - uncorrelated, the probe side was empty: FALSE (`x IN (empty)`);
    /// - uncorrelated otherwise: UNKNOWN exactly for a NULL build key.
    pub(super) fn final_chunk(
        &self,
        summary: ProbeSideSummary,
        left_side: UInt64Array,
        right_side: UInt32Array,
    ) -> (UInt64Array, UInt32Array, Option<ArrayRef>) {
        debug_assert!(
            !summary.has_null || summary.non_empty,
            "a probe batch with a NULL key has rows"
        );
        match self {
            Self::ProbeRowsCorrelated { .. } | Self::ProbeRowsUncorrelated { .. } => {
                (left_side, right_side, None)
            }
            Self::BuildRowsCorrelated {
                output, unknown, ..
            } => {
                let unknown = unknown.lock();
                emit(*output, left_side, right_side, |row| unknown.get_bit(row))
            }
            // A probe batch with a NULL key also has rows: `has_null` implies
            // `non_empty`.
            Self::BuildRowsUncorrelated { output, .. } if summary.has_null => {
                emit_uniform(*output, left_side, right_side, true)
            }
            Self::BuildRowsUncorrelated { output, .. } if !summary.non_empty => {
                emit_uniform(*output, left_side, right_side, false)
            }
            Self::BuildRowsUncorrelated { output, key_nulls } => match key_nulls {
                Some(nulls) => {
                    emit(*output, left_side, right_side, |row| nulls.is_null(row))
                }
                None => emit_uniform(*output, left_side, right_side, false),
            },
        }
    }
}

/// Emits a chunk of final build rows given which unmatched rows have an
/// UNKNOWN mark (see [`NotInBuild::final_chunk`]). `LeftAnti` chunks hold
/// only unmatched rows and keep those whose mark is FALSE; `LeftMark` chunks
/// hold every row, a valid probe index marking a matched one.
fn emit(
    output: NotInOutput,
    left_side: UInt64Array,
    right_side: UInt32Array,
    is_unknown: impl Fn(usize) -> bool,
) -> (UInt64Array, UInt32Array, Option<ArrayRef>) {
    match output {
        NotInOutput::Rows => {
            let kept = UInt64Array::from_iter_values(
                left_side
                    .values()
                    .iter()
                    .copied()
                    .filter(|row| !is_unknown(*row as usize)),
            );
            let right_side = UInt32Array::new_null(kept.len());
            (kept, right_side, None)
        }
        NotInOutput::Mark => {
            let marks: BooleanArray = left_side
                .values()
                .iter()
                .zip(right_side.iter())
                .map(|(row, probe)| {
                    if probe.is_some() {
                        Some(true)
                    } else {
                        (!is_unknown(*row as usize)).then_some(false)
                    }
                })
                .collect();
            (left_side, right_side, Some(Arc::new(marks)))
        }
    }
}

/// [`emit`] when every unmatched row's mark is the same: an anti join then
/// keeps all of the chunk or none of it.
fn emit_uniform(
    output: NotInOutput,
    left_side: UInt64Array,
    right_side: UInt32Array,
    unknown: bool,
) -> (UInt64Array, UInt32Array, Option<ArrayRef>) {
    match (output, unknown) {
        (NotInOutput::Rows, true) => {
            (UInt64Array::new_null(0), UInt32Array::new_null(0), None)
        }
        (NotInOutput::Rows, false) => (left_side, right_side, None),
        (NotInOutput::Mark, _) => emit(output, left_side, right_side, |_| unknown),
    }
}

/// One probe stream's `NOT IN` work, with scratch space for the correlated
/// marking pass, reused across probe batches.
#[derive(Debug, Default)]
pub(super) struct NotInProbe {
    hashes: Vec<u64>,
    lookup: LookupBuffers,
    /// For [`NotInBuild::ProbeRowsCorrelated`], the rows of the current probe
    /// batch whose `NOT IN` is UNKNOWN, set by [`Self::before_lookup`] on the
    /// batch's first lookup; they are never emitted. `None` when there is no
    /// such row.
    unknown_probe_rows: Option<BooleanBuffer>,
}

impl NotInProbe {
    /// Before a lookup of a probe batch. `first_chunk` is whether this is the
    /// batch's first lookup: a batch may resume lookup several times, but
    /// what it contributes to `NOT IN` is recorded once.
    ///
    /// - Uncorrelated [`NotIn::ProbeRows`] emits probe rows, so a NULL build
    ///   key skips every batch.
    /// - Uncorrelated [`NotIn::BuildRows`]: record what the batch saw for the
    ///   final stage. The anti join skips once any partition saw a NULL probe
    ///   key: every unmatched mark is then UNKNOWN. This is a best-effort
    ///   early exit, checked on every resumed lookup; the final stage
    ///   re-checks.
    /// - Correlated: mark the rows this batch makes UNKNOWN, build rows for
    ///   [`NotIn::BuildRows`] and this batch's probe rows for
    ///   [`NotIn::ProbeRows`]. Must also run when the build map is empty: an
    ///   all-NULL-key build side still needs its marks.
    ///
    /// Returns whether the batch cannot contribute any output row, so its
    /// lookup is skipped. A no-op returning `false` for other joins.
    #[expect(clippy::too_many_arguments)]
    pub(super) fn before_lookup(
        &mut self,
        left_data: &JoinLeftData,
        probe_batch: &RecordBatch,
        probe_values: &[ArrayRef],
        first_chunk: bool,
        filter: Option<&JoinFilter>,
        random_state: &RandomState,
        batch_size: usize,
    ) -> Result<bool> {
        let Some(not_in) = left_data.not_in() else {
            return Ok(false);
        };
        match not_in {
            NotInBuild::ProbeRowsUncorrelated { build_has_null } => Ok(*build_has_null),
            NotInBuild::BuildRowsUncorrelated { output, .. } => {
                if first_chunk {
                    // `on[0]` is the `NOT IN` value key. Only batches with rows
                    // count: `NULL NOT IN (empty)` is TRUE.
                    let probe_has_null = probe_values[0].logical_null_count() > 0;
                    left_data
                        .record_probe_batch(probe_batch.num_rows() > 0, probe_has_null);
                }
                Ok(*output == NotInOutput::Rows && left_data.probe_side_has_null_hint())
            }
            NotInBuild::BuildRowsCorrelated {
                value_keys,
                output,
                unknown,
                scope_map,
                null_value_rows,
            } => {
                // The join filter needs the join type the output implies.
                let join_type = match output {
                    NotInOutput::Rows => JoinType::LeftAnti,
                    NotInOutput::Mark => JoinType::LeftMark,
                };
                if first_chunk {
                    mark_null_candidates_for_probe_batch(
                        left_data,
                        UnknownMarks::Build(unknown),
                        scope_map.as_deref(),
                        null_value_rows.as_ref(),
                        probe_batch,
                        probe_values,
                        *value_keys,
                        filter,
                        join_type,
                        random_state,
                        batch_size,
                        &mut self.hashes,
                        &mut self.lookup,
                    )?;
                }
                Ok(false)
            }
            NotInBuild::ProbeRowsCorrelated {
                value_keys,
                scope_map,
                null_value_rows,
            } => {
                if !first_chunk {
                    return Ok(false);
                }
                self.unknown_probe_rows = None;
                // Without a NULL-valued row on either side no probe row can be
                // UNKNOWN, so the bitmap is only allocated when one exists.
                let may_be_unknown = null_value_rows.is_some()
                    || probe_values[..*value_keys]
                        .iter()
                        .any(|values| values.logical_null_count() > 0);
                if may_be_unknown {
                    let num_rows = probe_batch.num_rows();
                    let mut unknown = BooleanBufferBuilder::new(num_rows);
                    unknown.append_n(num_rows, false);
                    let unknown = Mutex::new(unknown);
                    mark_null_candidates_for_probe_batch(
                        left_data,
                        UnknownMarks::Probe(&unknown),
                        scope_map.as_deref(),
                        null_value_rows.as_ref(),
                        probe_batch,
                        probe_values,
                        *value_keys,
                        filter,
                        JoinType::RightAnti,
                        random_state,
                        batch_size,
                        &mut self.hashes,
                        &mut self.lookup,
                    )?;
                    let unknown = unknown.into_inner().finish();
                    self.unknown_probe_rows =
                        (unknown.count_set_bits() > 0).then_some(unknown);
                }
                Ok(false)
            }
        }
    }

    /// After a lookup, drops the probe rows a [`NotIn::ProbeRows`] join must
    /// not emit: when uncorrelated, those whose key is NULL, since
    /// `NULL NOT IN (..)` is never TRUE; when correlated, those that
    /// [`Self::before_lookup`] marked UNKNOWN. `valid_keys` is the probe keys'
    /// validity (`matchable_join_keys`), when the batch has one.
    pub(super) fn after_lookup(
        &self,
        not_in: &NotInBuild,
        probe_values: &[ArrayRef],
        valid_keys: Option<&NullBuffer>,
        right_indices: UInt32Array,
    ) -> UInt32Array {
        match not_in {
            NotInBuild::ProbeRowsUncorrelated { .. } => {
                let mask = valid_keys
                    .cloned()
                    .or_else(|| probe_values[0].logical_nulls());
                match mask.filter(|m| m.null_count() > 0) {
                    Some(mask) => {
                        retain_probe_rows(&right_indices, |idx| !mask.is_null(idx))
                    }
                    None => right_indices,
                }
            }
            NotInBuild::ProbeRowsCorrelated { .. } => match &self.unknown_probe_rows {
                Some(unknown) => {
                    retain_probe_rows(&right_indices, |idx| !unknown.value(idx))
                }
                None => right_indices,
            },
            NotInBuild::BuildRowsCorrelated { .. }
            | NotInBuild::BuildRowsUncorrelated { .. } => right_indices,
        }
    }

    /// With an empty build map the lookup is skipped and `output` holds the
    /// probe batch as the join type emits it. Drops the probe rows that
    /// [`Self::before_lookup`] marked UNKNOWN, as [`Self::after_lookup`] does.
    pub(super) fn after_empty_build(&self, output: RecordBatch) -> Result<RecordBatch> {
        match &self.unknown_probe_rows {
            Some(unknown) => Ok(filter_record_batch(
                &output,
                &BooleanArray::new(!unknown, None),
            )?),
            None => Ok(output),
        }
    }
}

/// Keeps the probe rows of `right_indices` for which `keep` returns true.
fn retain_probe_rows(
    right_indices: &UInt32Array,
    keep: impl Fn(usize) -> bool,
) -> UInt32Array {
    UInt32Array::from_iter_values(
        right_indices
            .values()
            .iter()
            .copied()
            .filter(|idx| keep(*idx as usize)),
    )
}

/// The scope map and NULL-valued build rows of a correlated `NOT IN`.
#[expect(clippy::too_many_arguments, clippy::type_complexity)]
fn correlated_row_state(
    on_left: &[PhysicalExprRef],
    batch: &RecordBatch,
    keys: &[ArrayRef],
    normalized: &[ArrayRef],
    value_keys: usize,
    random_state: &RandomState,
    reservation: &mut MemoryReservation,
    metrics: &BuildProbeJoinMetrics,
) -> Result<(Option<Box<dyn JoinHashMapType>>, Option<NullValueBuildRows>)> {
    let num_rows = batch.num_rows();
    // `on_left[..V]` are the value keys and `on_left[V..]` the (possibly
    // empty) correlation scope keys.
    let scope_keys = &on_left[value_keys..];
    let scope_map = if scope_keys.is_empty() {
        None
    } else {
        // Scope-only NULL marking uses a HashMap (the primary join map may
        // use ArrayMap for full-key matches, but scope keys have arbitrary
        // shape).
        let mut scope_map = new_join_hashmap(num_rows, reservation, metrics)?;
        let mut hashes_buffer = vec![0; num_rows];
        update_hash(
            scope_keys,
            batch,
            &mut *scope_map,
            0,
            random_state,
            &mut hashes_buffer,
            0,
            true,
            NullEquality::NullEqualsNothing,
        )?;
        Some(scope_map)
    };

    let Some(null_mask) = null_value_key_mask(&keys[..value_keys]) else {
        return Ok((scope_map, None));
    };
    let build_indices =
        UInt64Array::from_iter_values(null_mask.values().set_indices().map(|i| i as u64));
    // `build_indices` is retained for the lifetime of the join, so charge it
    // like the maps and bitmaps.
    let retained_size = build_indices.get_array_memory_size();
    reservation.try_grow(retained_size)?;
    metrics.build_mem_used.add(retained_size);

    let null_scope_map = if scope_keys.is_empty() {
        None
    } else {
        let scope_values = normalized[value_keys..]
            .iter()
            .map(|values| Ok(filter(values.as_ref(), &null_mask)?))
            .collect::<Result<Vec<_>>>()?;
        let null_rows = build_indices.len();
        let mut map = new_join_hashmap(null_rows, reservation, metrics)?;
        let mut hashes_buffer = vec![0; null_rows];
        create_hashes(&scope_values, random_state, &mut hashes_buffer)?;
        map.update_from_iter(Box::new(hashes_buffer.iter().enumerate().rev()), 0);
        Some(map)
    };
    Ok((
        scope_map,
        Some(NullValueBuildRows {
            scope_map: null_scope_map,
            build_indices,
        }),
    ))
}

/// The build rows with a NULL `NOT IN` value key (in any element of a
/// multi-column key), used by correlated `NOT IN` (see [`NotInScope::Correlated`]).
///
/// Such rows make `NOT IN` UNKNOWN whenever *any* probe row in their
/// correlation scope passes the join filter (unless another element is a
/// definite mismatch), so every probe row must be tested against them.
/// Restricting this lookup to the NULL-valued build rows keeps it
/// proportional to the number of NULLs instead of enumerating every scope
/// match of every probe row.
pub(super) struct NullValueBuildRows {
    /// Hash table keyed by the correlation scope values of the NULL-valued
    /// build rows. Stored positions index into `build_indices`, not the full
    /// build batch. `None` when the join has no correlation scope keys, so
    /// every probe row is in scope.
    scope_map: Option<Box<dyn JoinHashMapType>>,
    /// Maps positions in `scope_map` back to row indices in the full build
    /// batch.
    build_indices: UInt64Array,
}

/// Returns the rows where at least one of `value_keys` is NULL, or `None` when
/// there is no such row.
fn null_value_key_mask(value_keys: &[ArrayRef]) -> Option<BooleanArray> {
    let logical_nulls: Vec<_> = value_keys.iter().map(|v| v.logical_nulls()).collect();
    let valid = NullBuffer::union_many(logical_nulls.iter().map(Option::as_ref))?;
    (valid.null_count() > 0).then(|| BooleanArray::new(!valid.inner(), None))
}

/// Where a correlated null-aware join records the rows whose `NOT IN` is
/// UNKNOWN, see [`mark_null_candidates_for_probe_batch`].
#[derive(Clone, Copy)]
enum UnknownMarks<'a> {
    /// `LeftAnti` and `LeftMark` mark build rows, in the null-indices bitmap
    /// shared by all probe partitions.
    Build(&'a SharedBitmapBuilder),
    /// `RightAnti` marks the rows of the current probe batch.
    Probe(&'a SharedBitmapBuilder),
}

impl<'a> UnknownMarks<'a> {
    fn bitmap(self) -> &'a SharedBitmapBuilder {
        match self {
            Self::Build(bitmap) | Self::Probe(bitmap) => bitmap,
        }
    }

    /// The row of a (build, probe) candidate pair whose `NOT IN` the pair
    /// decides, and that is marked when the pair makes it UNKNOWN.
    fn unknown_row(self, build_idx: u64, probe_idx: u32) -> usize {
        match self {
            Self::Build(_) => build_idx as usize,
            Self::Probe(_) => probe_idx as usize,
        }
    }
}

/// Records which rows of a correlated null-aware join are UNKNOWN candidates
/// for this probe batch: the build rows for `LeftAnti` and `LeftMark`, the
/// probe rows for `RightAnti` (see [`UnknownMarks`]).
///
/// Key layout: `on[..V]` are the `NOT IN` value keys, `on[V..]` the (possibly
/// empty) correlation scope keys (see [`NotInScope::Correlated`]). A row is
/// NULL-valued when any of its value keys is NULL. Each (build, probe) pair
/// decides one of its rows (see [`UnknownMarks::unknown_row`]), and makes that
/// row's `NOT IN` UNKNOWN instead of TRUE (a `LeftMark` mark NULL instead of
/// FALSE) when either:
/// 1. the build row is NULL-valued, or
/// 2. the probe row is NULL-valued,
///
/// the two rows are in the same correlation scope, the pair passes the join
/// filter, and, for a multi-column value key, the two rows' value tuples are
/// not a definite mismatch: every element pair is equal or involves a NULL.
/// With a single value key a NULL on either side already rules out a mismatch.
/// An exact match, which makes `NOT IN` FALSE, is found by the main hash lookup
/// instead and takes precedence over these marks.
///
/// Case 1 pairs the NULL-valued build rows with all probe rows; case 2 pairs
/// all build rows with the NULL-valued probe rows. Scope keys narrow these
/// pairs through a hash lookup; without scope keys every pair is a candidate.
/// The value tuples and then the join filter, if any, decide which candidates
/// count.
///
/// A row stays UNKNOWN once it is marked. So the pairing drops most pairs that
/// decide a row already known to be UNKNOWN before the join filter is
/// evaluated. Without scope keys this also ends the pairing as soon as every
/// row that can be decided is UNKNOWN. See [`UnmarkedPairs`], and
/// [`UnmarkedPairs::lookup_unmarked`] for the checks it skips.
#[expect(clippy::too_many_arguments)]
fn mark_null_candidates_for_probe_batch(
    left_data: &JoinLeftData,
    marks: UnknownMarks<'_>,
    scope_map: Option<&dyn JoinHashMapType>,
    null_value_build_rows: Option<&NullValueBuildRows>,
    probe_batch: &RecordBatch,
    probe_values: &[ArrayRef],
    num_value_keys: usize,
    filter: Option<&JoinFilter>,
    join_type: JoinType,
    random_state: &RandomState,
    batch_size: usize,
    hashes_buffer: &mut Vec<u64>,
    lookup: &mut LookupBuffers,
) -> Result<()> {
    let probe_null_value_mask = null_value_key_mask(&probe_values[..num_value_keys]);
    if null_value_build_rows.is_none() && probe_null_value_mask.is_none() {
        return Ok(());
    }

    debug_assert_eq!(
        left_data.values().len(),
        probe_values.len(),
        "build/probe key counts must match"
    );
    let (build_value_keys, build_scope_values) =
        left_data.values().split_at(num_value_keys);
    let (probe_value_keys, probe_scope_values) = probe_values.split_at(num_value_keys);

    // Keeps the candidate pairs whose value tuples are not a definite mismatch
    // and that pass the join filter, and marks their rows as UNKNOWN.
    let mark = |build_indices: UInt64Array, probe_indices: UInt32Array| {
        let (build_indices, probe_indices) = if num_value_keys > 1 {
            retain_value_mismatch_free(
                build_value_keys,
                probe_value_keys,
                build_indices,
                probe_indices,
            )?
        } else {
            (build_indices, probe_indices)
        };
        if build_indices.is_empty() {
            return Ok(());
        }
        let (build_indices, probe_indices) = match filter {
            Some(filter) => apply_join_filter_to_indices(
                left_data.batch(),
                probe_batch,
                build_indices,
                probe_indices,
                filter,
                JoinSide::Left,
                None,
                join_type,
            )?,
            None => (build_indices, probe_indices),
        };
        if !build_indices.is_empty() {
            let mut bitmap = marks.bitmap().lock();
            for (build_idx, probe_idx) in
                build_indices.values().iter().zip(probe_indices.values())
            {
                bitmap.set_bit(marks.unknown_row(*build_idx, *probe_idx), true);
            }
        }
        Ok(())
    };

    // The scope maps pair rows by the hash of their scope keys; keep only the
    // pairs whose scope keys are equal.
    let mut key_comparator = None;
    let mut mark_scope_matches =
        |build_indices: UInt64Array, probe_indices: UInt32Array| {
            let (build_indices, probe_indices) = equal_rows_arr_with_normalized_left(
                &build_indices,
                &probe_indices,
                build_scope_values,
                probe_scope_values,
                NullEquality::NullEqualsNothing,
                &mut key_comparator,
            )?;
            mark(build_indices, probe_indices)
        };

    // With scope keys, the probe rows whose scope keys hold a NULL are in no
    // scope. The others are looked up by the hash of their scope keys.
    let probe_rows_in_scope = if probe_scope_values.is_empty() {
        None
    } else {
        hashes_buffer.clear();
        hashes_buffer.resize(probe_batch.num_rows(), 0);
        create_hashes(probe_scope_values, random_state, hashes_buffer)?;
        matchable_join_keys(probe_scope_values, NullEquality::NullEqualsNothing)
    };
    let in_scope = |probe_row: &u32| match &probe_rows_in_scope {
        Some(valid) => valid.is_valid(*probe_row as usize),
        None => true,
    };

    // Case 1: NULL-valued build rows paired with the probe rows in their
    // correlation scope.
    if let Some(null_rows) = null_value_build_rows {
        let probe_rows = (0..probe_batch.num_rows() as u32).filter(in_scope);
        match &null_rows.scope_map {
            Some(scope_map) => {
                let mut pairs =
                    UnmarkedPairs::new(marks, batch_size, &mut mark_scope_matches);
                // The map indexes only the NULL-valued build rows; translate
                // its positions back to build row indices.
                pairs.scope_matches(
                    scope_map.as_ref(),
                    |position| null_rows.build_indices.value(position as usize),
                    probe_rows,
                    hashes_buffer,
                    lookup,
                )?;
                pairs.finish()?;
            }
            None => {
                let mut pairs = UnmarkedPairs::new(marks, batch_size, &mark);
                pairs.cross_product(
                    null_rows.build_indices.values().iter().copied(),
                    probe_rows,
                )?;
                pairs.finish()?;
            }
        }
    }

    // Case 2: NULL-valued probe rows paired with the build rows in their
    // correlation scope.
    if let Some(null_mask) = probe_null_value_mask {
        let null_probe_rows = null_mask
            .values()
            .set_indices()
            .map(|i| i as u32)
            .filter(in_scope);

        match scope_map {
            Some(scope_map) => {
                let mut pairs =
                    UnmarkedPairs::new(marks, batch_size, &mut mark_scope_matches);
                pairs.scope_matches(
                    scope_map,
                    |build_row| build_row,
                    null_probe_rows,
                    hashes_buffer,
                    lookup,
                )?;
                pairs.finish()?;
            }
            None => {
                let mut pairs = UnmarkedPairs::new(marks, batch_size, &mark);
                pairs.cross_product(
                    0..left_data.batch().num_rows() as u64,
                    null_probe_rows,
                )?;
                pairs.finish()?;
            }
        }
    }

    Ok(())
}

/// Keeps the candidate pairs whose multi-column `NOT IN` value tuples are not a
/// definite mismatch, i.e. every element pair is equal or involves a NULL.
///
/// Such a pair compares UNKNOWN when some element is NULL (TRUE pairs, with no
/// NULL, are found by the hash lookup instead), whereas a pair with a definite
/// mismatch compares FALSE whatever its NULLs are: `(NULL, 1) = (2, 3)` is
/// `UNKNOWN AND FALSE`, which is FALSE.
///
/// Elements are compared with SQL `=` (see [`apply_cmp`]), which, like the
/// join's own key equality, treats `-0.0` and `+0.0` as equal.
fn retain_value_mismatch_free(
    build_value_keys: &[ArrayRef],
    probe_value_keys: &[ArrayRef],
    build_indices: UInt64Array,
    probe_indices: UInt32Array,
) -> Result<(UInt64Array, UInt32Array)> {
    if build_indices.is_empty() {
        return Ok((build_indices, probe_indices));
    }
    let mut keep: Option<BooleanBuffer> = None;
    for (build, probe) in build_value_keys.iter().zip(probe_value_keys) {
        let build = take(build.as_ref(), &build_indices, None)?;
        let probe = take(probe.as_ref(), &probe_indices, None)?;
        let eq = apply_cmp(
            Operator::Eq,
            &ColumnarValue::Array(build),
            &ColumnarValue::Array(probe),
        )?
        .into_array(build_indices.len())?;
        let eq = eq.as_boolean();
        // A NULL comparison (an element involving NULL) is not a mismatch.
        let not_mismatch = match eq.nulls() {
            Some(nulls) => eq.values() | &!nulls.inner(),
            None => eq.values().clone(),
        };
        keep = Some(match keep {
            Some(keep) => &keep & &not_mismatch,
            None => not_mismatch,
        });
    }
    match keep {
        Some(keep) if keep.count_set_bits() < keep.len() => {
            let keep = BooleanArray::new(keep, None);
            Ok((
                filter(&build_indices, &keep)?.as_primitive().clone(),
                filter(&probe_indices, &keep)?.as_primitive().clone(),
            ))
        }
        _ => Ok((build_indices, probe_indices)),
    }
}

/// Reused buffers for the matches of a scope map lookup.
#[derive(Debug, Default)]
pub(super) struct LookupBuffers {
    probe_indices: Vec<u32>,
    build_indices: Vec<u64>,
}

/// Pairs build rows with probe rows for
/// [`mark_null_candidates_for_probe_batch`], and calls `f` with the pairs, as
/// chunks of at most `batch_size` pairs.
///
/// `f` marks rows (see [`UnknownMarks`]), and a marked row cannot become
/// unmarked. So the pairing drops most pairs that decide a marked row before
/// it sends them, and drops the marked rows again after each chunk. Some
/// pairs of a marked row still reach `f` (see [`Self::lookup_unmarked`]).
/// Such a pair only sets a mark that is already set.
struct UnmarkedPairs<'a, F> {
    marks: UnknownMarks<'a>,
    batch_size: usize,
    build_chunk: Vec<u64>,
    probe_chunk: Vec<u32>,
    /// Whether the rows that the pairs decide may hold rows marked since they
    /// were last refreshed.
    needs_refresh: bool,
    /// The lookups to skip the check of the build row marks for, after the
    /// last check (see [`Self::lookup_unmarked`]).
    unchecked_lookups: usize,
    /// The lookups left before the next check of the build row marks.
    lookups_until_check: usize,
    /// The matches of the current [`Self::scope_matches`] that a lookup per
    /// probe row pays for without need (see there).
    wasted_matches: usize,
    /// All the matches that the scope map lookups returned.
    #[cfg(test)]
    looked_up_matches: usize,
    /// Whether a [`Self::scope_matches`] wasted enough matches to consider
    /// groups.
    #[cfg(test)]
    considered_groups: bool,
    /// Whether a [`Self::scope_matches`] switched to groups.
    #[cfg(test)]
    grouped: bool,
    /// The lookups with marks on the build rows.
    #[cfg(test)]
    build_lookups: usize,
    /// The lookups that checked the build row marks.
    #[cfg(test)]
    mark_checks: usize,
    f: F,
}

/// A sort costs about as much as this many wasted matches per probe row (see
/// [`UnmarkedPairs::scope_matches`]).
const WASTED_MATCHES_PER_SORTED_ROW: usize = 8;

/// The most lookups that skip the check of the build row marks in a row (see
/// [`UnmarkedPairs::lookup_unmarked`]).
const MAX_UNCHECKED_LOOKUPS: usize = 32;

/// The probe rows sampled to estimate the groups of equal hashes before a
/// sort (see [`UnmarkedPairs::scope_matches`]).
const GROUP_SAMPLE_ROWS: usize = 256;

/// Estimates how many different values `hashes` holds, from a sample of at
/// most [`GROUP_SAMPLE_ROWS`] of them. The count is exact when `hashes` fits
/// in the sample.
fn estimate_groups(hashes: &[u64]) -> usize {
    let mut sample: Vec<u64> = if hashes.len() <= GROUP_SAMPLE_ROWS {
        hashes.to_vec()
    } else {
        // Pseudo-random positions, so that the sample sees a hash twice about
        // as often as a random sample would. A fixed stride follows the
        // period of keys such as `value % n`.
        (0..GROUP_SAMPLE_ROWS as u64)
            .map(|i| hashes[(splitmix64(i) % hashes.len() as u64) as usize])
            .collect()
    };
    sample.sort_unstable();
    let mut seen = 0;
    let mut seen_once: usize = 0;
    let mut seen_twice = 0;
    for run in sample.chunk_by(|a, b| a == b) {
        seen += 1;
        match run.len() {
            1 => seen_once += 1,
            2 => seen_twice += 1,
            _ => {}
        }
    }
    if hashes.len() <= GROUP_SAMPLE_ROWS {
        return seen;
    }
    // The bias-corrected Chao1 estimate: the hashes that the sample saw once
    // suggest how many it did not see.
    let unseen = seen_once * seen_once.saturating_sub(1) / (2 * (seen_twice + 1));
    (seen + unseen).min(hashes.len())
}

/// The SplitMix64 mix of `x`, a fixed pseudo-random sequence.
fn splitmix64(x: u64) -> u64 {
    let x = x.wrapping_add(0x9E37_79B9_7F4A_7C15);
    let x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    let x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    x ^ (x >> 31)
}

/// Whether `rows` probe rows in `groups` groups of equal hashes save work when
/// each group is paired at once (see [`UnmarkedPairs::scope_matches`]).
/// `matches_per_row` estimates the build rows of a hash.
fn groups_save_work(
    mark_build_rows: bool,
    rows: usize,
    groups: usize,
    matches_per_row: usize,
    batch_size: usize,
) -> bool {
    if mark_build_rows {
        rows >= 2 * groups
    } else {
        rows * matches_per_row > 2 * batch_size * groups
    }
}

/// The lookups to skip the check of the build row marks for, after a check
/// that followed `unchecked` skipped lookups, and that dropped a match or not
/// (see [`UnmarkedPairs::lookup_unmarked`]).
fn next_unchecked_lookups(unchecked: usize, dropped: bool) -> usize {
    if dropped {
        0
    } else {
        (2 * unchecked).clamp(1, MAX_UNCHECKED_LOOKUPS)
    }
}

impl<'a, F> UnmarkedPairs<'a, F>
where
    F: FnMut(UInt64Array, UInt32Array) -> Result<()>,
{
    fn new(marks: UnknownMarks<'a>, batch_size: usize, f: F) -> Self {
        Self {
            marks,
            batch_size,
            build_chunk: Vec::with_capacity(batch_size),
            probe_chunk: Vec::with_capacity(batch_size),
            needs_refresh: false,
            unchecked_lookups: 0,
            lookups_until_check: 0,
            wasted_matches: 0,
            #[cfg(test)]
            looked_up_matches: 0,
            #[cfg(test)]
            considered_groups: false,
            #[cfg(test)]
            grouped: false,
            #[cfg(test)]
            build_lookups: 0,
            #[cfg(test)]
            mark_checks: 0,
            f,
        }
    }

    /// Pairs each of `probe_rows` with each of `build_rows`. The pairing stops
    /// when every row that the pairs decide is marked.
    fn cross_product(
        &mut self,
        build_rows: impl Iterator<Item = u64>,
        probe_rows: impl Iterator<Item = u32>,
    ) -> Result<()> {
        // Drop the rows that are already marked before the first pair.
        self.needs_refresh = true;
        match self.marks {
            UnknownMarks::Build(_) => {
                self.pair_unmarked(&mut build_rows.collect(), probe_rows, |b, p| (b, p))
            }
            UnknownMarks::Probe(_) => {
                self.pair_unmarked(&mut probe_rows.collect(), build_rows, |p, b| (b, p))
            }
        }
    }

    /// Pairs each of `probe_rows` with the build rows that share the hash of
    /// its correlation scope keys in `scope_map`. `probe_hashes` holds the
    /// hashes of all rows of the probe batch. `to_build_row` translates the
    /// positions stored in `scope_map` to build row indices. Rows with equal
    /// hashes can have different scope keys, so `f` must still check that the
    /// scope keys are equal.
    ///
    /// The pairing starts with a lookup per probe row, like the join itself.
    /// But each probe row finds all the build rows in its scope:
    /// - When the marks are on the build rows, a marked build row costs a
    ///   little for every later probe row in its scope.
    /// - When the marks are on the probe rows, a probe row is paired with all
    ///   the build rows in its scope, even when its first pair marks it.
    ///
    /// When those costs add up to more than a sort of the probe rows, the
    /// join considers groups of the rest of the probe rows with equal hashes.
    /// A sample of the hashes estimates the groups first. If the groups can
    /// save work, the join sorts the rest of the probe rows by hash. Each hash
    /// is then looked up once, and its group is paired like a cross product,
    /// which drops the rows as they are marked. A group saves work when:
    /// - with marks on the build rows, it holds more than one probe row, which
    ///   then share one walk of the hash chain;
    /// - with marks on the probe rows, its pairs fill more than a chunk.
    ///   Within a chunk every pair is sent, so only the later chunks can skip
    ///   the marked probe rows.
    ///
    /// Otherwise the lookup per probe row goes on.
    fn scope_matches(
        &mut self,
        scope_map: &dyn JoinHashMapType,
        to_build_row: impl Fn(u64) -> u64,
        probe_rows: impl Iterator<Item = u32>,
        probe_hashes: &[u64],
        lookup: &mut LookupBuffers,
    ) -> Result<()> {
        let probe_rows: Vec<u32> = probe_rows.collect();
        // `probe_rows` is increasing, so it holds every row exactly when it is
        // as long as the batch. Then the positions are the probe rows.
        let all_rows = probe_rows.len() == probe_hashes.len();
        let gathered: Vec<u64>;
        let hashes = if all_rows {
            probe_hashes
        } else {
            gathered = probe_rows
                .iter()
                .map(|row| probe_hashes[*row as usize])
                .collect();
            &gathered
        };

        self.wasted_matches = 0;
        let max_wasted_matches = WASTED_MATCHES_PER_SORTED_ROW * probe_rows.len();
        let Some((offset, matches)) = self.lookup_each_row(
            scope_map,
            hashes,
            (!all_rows).then_some(probe_rows.as_slice()),
            AtRow { row: 0 },
            &to_build_row,
            max_wasted_matches,
            lookup,
        )?
        else {
            return Ok(());
        };
        // Group the probe rows from the one being looked up. Its build rows
        // that were already paired are paired again, which changes no mark.
        let group_from = match offset {
            AtRow { row } | MidChain { row, .. } => row,
        };

        #[cfg(test)]
        {
            self.considered_groups = true;
        }
        let mark_build_rows = matches!(self.marks, UnknownMarks::Build(_));
        // The matches per probe row so far estimate the build rows of a hash.
        let matches_per_row = matches / group_from.max(1);
        let saves_work = |rows: usize, groups: usize| {
            groups_save_work(
                mark_build_rows,
                rows,
                groups,
                matches_per_row,
                self.batch_size,
            )
        };
        let rest_hashes = &hashes[group_from..];
        // Estimate the groups from a sample first, because a sort of all the
        // rest that finds no group worth it costs as much as the wasted
        // matches did.
        let worth_sorting = saves_work(rest_hashes.len(), estimate_groups(rest_hashes));
        let mut rest: Vec<(u64, u32)> = vec![];
        let mut groups = vec![];
        if worth_sorting {
            rest = rest_hashes
                .iter()
                .copied()
                .zip(probe_rows[group_from..].iter().copied())
                .collect();
            rest.sort_unstable_by_key(|(hash, _)| *hash);
            groups = rest.chunk_by(|a, b| a.0 == b.0).collect();
        }
        if !worth_sorting || !saves_work(rest.len(), groups.len()) {
            self.lookup_each_row(
                scope_map,
                hashes,
                (!all_rows).then_some(probe_rows.as_slice()),
                offset,
                &to_build_row,
                usize::MAX,
                lookup,
            )?;
            return Ok(());
        }

        #[cfg(test)]
        {
            self.grouped = true;
        }
        // Send the pairs that are left, so the lookups below see their marks.
        self.send()?;
        let group_hashes: Vec<u64> = groups.iter().map(|group| group[0].0).collect();
        match self.marks {
            UnknownMarks::Build(_) => self.pair_build_groups(
                scope_map,
                &groups,
                &group_hashes,
                &to_build_row,
                lookup,
            ),
            UnknownMarks::Probe(_) => {
                for (group, hash) in groups.iter().zip(&group_hashes) {
                    self.pair_probe_group(
                        scope_map,
                        group,
                        *hash,
                        &to_build_row,
                        lookup,
                    )?;
                }
                Ok(())
            }
        }
    }

    /// Looks up each of `probe_rows`, whose hashes are `hashes`, from `offset`,
    /// and pairs it with the unmarked build rows of its hash. `None` stands
    /// for every row of the probe batch. Stops once more
    /// than `max_wasted_matches` matches are wasted, and then returns where
    /// the lookup stopped and how many matches it walked.
    #[expect(clippy::too_many_arguments)]
    fn lookup_each_row(
        &mut self,
        scope_map: &dyn JoinHashMapType,
        hashes: &[u64],
        probe_rows: Option<&[u32]>,
        mut offset: ProbeOffset,
        to_build_row: impl Fn(u64) -> u64,
        max_wasted_matches: usize,
        lookup: &mut LookupBuffers,
    ) -> Result<Option<(ProbeOffset, usize)>> {
        let mut matches = 0;
        loop {
            let next_offset = self.lookup_unmarked(
                scope_map,
                hashes,
                |position| match probe_rows {
                    Some(rows) => rows[position as usize],
                    None => position,
                },
                offset,
                &to_build_row,
                &mut matches,
                lookup,
            );
            self.push_all(&lookup.build_indices, &lookup.probe_indices)?;
            let Some(next_offset) = next_offset else {
                return Ok(None);
            };
            offset = next_offset;
            if self.wasted_matches > max_wasted_matches {
                return Ok(Some((offset, matches)));
            }
        }
    }

    /// Pairs each group of probe rows with the build rows of its hash, when
    /// the marks are on the build rows. The groups are looked up together,
    /// and the build rows of a group are dropped as they are marked.
    fn pair_build_groups(
        &mut self,
        scope_map: &dyn JoinHashMapType,
        groups: &[&[(u64, u32)]],
        group_hashes: &[u64],
        to_build_row: impl Fn(u64) -> u64,
        lookup: &mut LookupBuffers,
    ) -> Result<()> {
        let mut build_rows = vec![];
        let mut offset = AtRow { row: 0 };
        loop {
            // A lookup chunk holds the build rows of one or more groups, in
            // group order.
            let next_offset = self.lookup_unmarked(
                scope_map,
                group_hashes,
                |group| group,
                offset,
                &to_build_row,
                &mut 0,
                lookup,
            );
            let mut matches = lookup.build_indices.as_slice();
            for group in lookup.probe_indices.chunk_by(|a, b| a == b) {
                let group_matches;
                (group_matches, matches) = matches.split_at(group.len());
                build_rows.clear();
                build_rows.extend_from_slice(group_matches);
                // A build row belongs to one group only, so the marks made
                // for the earlier groups do not concern these build rows.
                self.needs_refresh = false;
                self.pair_unmarked(
                    &mut build_rows,
                    groups[group[0] as usize].iter().map(|(_, row)| *row),
                    |b, p| (b, p),
                )?;
            }
            let Some(next_offset) = next_offset else {
                return Ok(());
            };
            offset = next_offset;
        }
    }

    /// Pairs one group of probe rows with the build rows of its hash, when
    /// the marks are on the probe rows. The probe rows of the group are
    /// dropped as they are marked, and the walk of the hash chain stops when
    /// none is left.
    fn pair_probe_group(
        &mut self,
        scope_map: &dyn JoinHashMapType,
        group: &[(u64, u32)],
        hash: u64,
        to_build_row: impl Fn(u64) -> u64,
        lookup: &mut LookupBuffers,
    ) -> Result<()> {
        let mut probe_rows: Vec<u32> = group.iter().map(|(_, row)| *row).collect();
        let mut build_rows = vec![];
        let mut offset = AtRow { row: 0 };
        loop {
            let next_offset = scope_map.get_matched_indices_with_limit_offset(
                &[hash],
                None,
                self.batch_size,
                offset,
                &mut lookup.probe_indices,
                &mut lookup.build_indices,
            );
            #[cfg(test)]
            {
                self.looked_up_matches += lookup.build_indices.len();
            }
            build_rows.clear();
            build_rows.extend(lookup.build_indices.iter().map(|row| to_build_row(*row)));
            // Drop the probe rows that the earlier chunks marked.
            self.needs_refresh = true;
            self.pair_unmarked(&mut probe_rows, build_rows.iter().copied(), |p, b| {
                (b, p)
            })?;
            if probe_rows.is_empty() {
                return Ok(());
            }
            let Some(next_offset) = next_offset else {
                return Ok(());
            };
            offset = next_offset;
        }
    }

    /// Looks up the next chunk of matches of `hashes` in `scope_map`, from
    /// `offset`, into the buffers: the position in `hashes` translated with
    /// `to_probe_row`, and the build row translated with `to_build_row`. Drops
    /// the matches that decide a marked row, counts the wasted matches (see
    /// [`Self::scope_matches`]), and adds the matches walked to `matches`.
    #[expect(clippy::too_many_arguments)]
    fn lookup_unmarked(
        &mut self,
        scope_map: &dyn JoinHashMapType,
        hashes: &[u64],
        to_probe_row: impl Fn(u32) -> u32,
        offset: ProbeOffset,
        to_build_row: impl Fn(u64) -> u64,
        matches: &mut usize,
        lookup: &mut LookupBuffers,
    ) -> Option<ProbeOffset> {
        let LookupBuffers {
            probe_indices,
            build_indices,
        } = lookup;
        let next_offset = scope_map.get_matched_indices_with_limit_offset(
            hashes,
            None,
            self.batch_size,
            offset,
            probe_indices,
            build_indices,
        );
        for build_row in build_indices.iter_mut() {
            *build_row = to_build_row(*build_row);
        }
        for probe_row in probe_indices.iter_mut() {
            *probe_row = to_probe_row(*probe_row);
        }
        let len = build_indices.len();
        // With marks on the probe rows, only the lookup per probe row comes
        // here, and the matches of a probe row are all pushed before any of
        // them is sent. So the check could drop only the rest of a hash chain
        // that spans lookup chunks, and it is skipped.
        //
        // With marks on the build rows, a check that drops nothing makes the
        // next checks less frequent, up to one in `MAX_UNCHECKED_LOOKUPS + 1`
        // lookups. The pairs of a marked build row that a skipped check lets
        // through only cost a filter evaluation, and change no mark.
        if let UnknownMarks::Build(bitmap) = self.marks {
            #[cfg(test)]
            {
                self.build_lookups += 1;
            }
            if self.lookups_until_check > 0 {
                self.lookups_until_check -= 1;
            } else {
                let bitmap = bitmap.lock();
                let mut kept = 0;
                for i in 0..len {
                    let (build, probe) = (build_indices[i], probe_indices[i]);
                    if !bitmap.get_bit(build as usize) {
                        build_indices[kept] = build;
                        probe_indices[kept] = probe;
                        kept += 1;
                    }
                }
                build_indices.truncate(kept);
                probe_indices.truncate(kept);
                self.unchecked_lookups =
                    next_unchecked_lookups(self.unchecked_lookups, kept < len);
                #[cfg(test)]
                {
                    self.mark_checks += 1;
                }
                self.lookups_until_check = self.unchecked_lookups;
            }
        }
        *matches += len;
        #[cfg(test)]
        {
            self.looked_up_matches += len;
        }
        self.wasted_matches += match self.marks {
            // The matches whose build row is marked.
            UnknownMarks::Build(_) => len - build_indices.len(),
            // Every match pairs its probe row again, unless it is the first
            // match of that probe row. Counting all matches overcounts by at
            // most one per probe row.
            UnknownMarks::Probe(_) => len,
        };
        next_offset
    }

    /// Sends the pairs that are left.
    fn finish(mut self) -> Result<()> {
        self.send()
    }

    /// Pairs each of `decided_rows` with each of `other_rows`. `to_pair`
    /// orders a pair as (build row, probe row), and `decided_rows` are the
    /// rows that the pairs decide (see [`UnknownMarks::unknown_row`]).
    /// `decided_rows` must hold no row that was marked before the last chunk
    /// was sent, unless `needs_refresh` is set.
    fn pair_unmarked<D: Copy + Into<u64>, O: Copy>(
        &mut self,
        decided_rows: &mut Vec<D>,
        other_rows: impl Iterator<Item = O>,
        to_pair: impl Fn(D, O) -> (u64, u32),
    ) -> Result<()> {
        for other_row in other_rows {
            // Refresh only after a chunk was sent, so the cost of the refresh
            // stays proportional to the pairs already evaluated.
            if self.needs_refresh {
                let bitmap = self.marks.bitmap().lock();
                decided_rows.retain(|row| !bitmap.get_bit((*row).into() as usize));
                self.needs_refresh = false;
            }
            if decided_rows.is_empty() {
                break;
            }
            for decided_row in decided_rows.iter() {
                let (build_row, probe_row) = to_pair(*decided_row, other_row);
                self.push(build_row, probe_row)?;
            }
        }
        Ok(())
    }

    /// Pushes the pairs of `build_rows` and `probe_rows`.
    fn push_all(&mut self, mut build_rows: &[u64], mut probe_rows: &[u32]) -> Result<()> {
        while !build_rows.is_empty() {
            let n = build_rows
                .len()
                .min(self.batch_size - self.build_chunk.len());
            let (build_now, build_later) = build_rows.split_at(n);
            let (probe_now, probe_later) = probe_rows.split_at(n);
            self.build_chunk.extend_from_slice(build_now);
            self.probe_chunk.extend_from_slice(probe_now);
            (build_rows, probe_rows) = (build_later, probe_later);
            if self.build_chunk.len() == self.batch_size {
                self.send()?;
            }
        }
        Ok(())
    }

    fn push(&mut self, build_row: u64, probe_row: u32) -> Result<()> {
        self.build_chunk.push(build_row);
        self.probe_chunk.push(probe_row);
        if self.build_chunk.len() == self.batch_size {
            self.send()?;
        }
        Ok(())
    }

    fn send(&mut self) -> Result<()> {
        if self.build_chunk.is_empty() {
            return Ok(());
        }
        let build_chunk =
            std::mem::replace(&mut self.build_chunk, Vec::with_capacity(self.batch_size));
        let probe_chunk =
            std::mem::replace(&mut self.probe_chunk, Vec::with_capacity(self.batch_size));
        (self.f)(build_chunk.into(), probe_chunk.into())?;
        self.needs_refresh = true;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::joins::join_hash_map::JoinHashMapU64;
    use rand::rngs::StdRng;
    use rand::{Rng, SeedableRng};

    /// The input of one run of [`UnmarkedPairs`]. Each row has a scope key,
    /// and the hash of a key is `key % hash_modulus`, so different scopes can
    /// share a hash.
    #[derive(Debug)]
    struct Case {
        mark_build_rows: bool,
        /// Whether to pair through a scope map, or as a cross product.
        through_scope_map: bool,
        batch_size: usize,
        hash_modulus: u64,
        /// The scope key of each build row.
        build_keys: Vec<u64>,
        /// The scope key of each probe row.
        probe_keys: Vec<u64>,
        /// The probe rows to pair, a subset of all the probe rows.
        probe_rows: Vec<u32>,
        /// The build rows to pair, a subset of all the build rows. The scope
        /// map stores positions in this list, as for the NULL-valued build
        /// rows.
        build_rows: Vec<u64>,
        /// The rows marked before the pairing starts, as by earlier probe
        /// batches.
        marked_before: Vec<bool>,
        /// Seed of the pairs that pass the join filter.
        filter_seed: u64,
        /// One in this many pairs passes the join filter. `u64::MAX` passes
        /// none.
        filter_one_in: u64,
        /// Whether each chunk of pairs also marks a random build row, as
        /// another probe partition can while this one pairs.
        foreign_marks: bool,
    }

    impl Case {
        fn random(rng: &mut StdRng) -> Self {
            let mark_build_rows = rng.random_bool(0.5);
            let num_build = rng.random_range(0..80);
            let num_probe = rng.random_range(0..300);
            // Few keys give large scopes, which make the pairing switch to
            // groups.
            let num_keys = rng.random_range(1..12);
            let build_keys: Vec<u64> = (0..num_build)
                .map(|_| rng.random_range(0..num_keys))
                .collect();
            let probe_keys: Vec<u64> = (0..num_probe)
                .map(|_| rng.random_range(0..num_keys))
                .collect();
            let probe_rows = (0..num_probe as u32)
                .filter(|_| rng.random_bool(0.9))
                .collect();
            let build_rows = (0..num_build as u64)
                .filter(|_| rng.random_bool(0.8))
                .collect();
            let num_marked = if mark_build_rows {
                num_build
            } else {
                num_probe
            };
            let marked_before = (0..num_marked).map(|_| rng.random_bool(0.1)).collect();
            Self {
                mark_build_rows,
                through_scope_map: rng.random_bool(0.8),
                batch_size: [1, 2, 3, 7, 64, 8192][rng.random_range(0..6)],
                hash_modulus: rng.random_range(1..=num_keys),
                build_keys,
                probe_keys,
                probe_rows,
                build_rows,
                marked_before,
                filter_seed: rng.random(),
                filter_one_in: [1, 2, 5, 50, u64::MAX][rng.random_range(0..5)],
                foreign_marks: mark_build_rows && rng.random_bool(0.3),
            }
        }

        fn passes_filter(&self, build_row: u64, probe_row: u32) -> bool {
            let mixed = (build_row ^ self.filter_seed)
                .wrapping_mul(0x9E37_79B9_7F4A_7C15)
                .wrapping_add(probe_row as u64)
                .wrapping_mul(0xC2B2_AE3D_27D4_EB4F);
            self.filter_one_in != u64::MAX
                && (mixed >> 7).is_multiple_of(self.filter_one_in)
        }

        fn decided_row(&self, build_row: u64, probe_row: u32) -> usize {
            if self.mark_build_rows {
                build_row as usize
            } else {
                probe_row as usize
            }
        }

        /// The marks after every pair of the same scope that passes the
        /// filter has marked its row.
        fn expected_marks(&self) -> Vec<bool> {
            let mut marks = self.marked_before.clone();
            for build_row in &self.build_rows {
                for probe_row in &self.probe_rows {
                    let same_scope = self.build_keys[*build_row as usize]
                        == self.probe_keys[*probe_row as usize];
                    let paired = !self.through_scope_map || same_scope;
                    if paired && self.passes_filter(*build_row, *probe_row) {
                        marks[self.decided_row(*build_row, *probe_row)] = true;
                    }
                }
            }
            marks
        }

        /// Runs [`UnmarkedPairs`] like [`mark_null_candidates_for_probe_batch`]
        /// does, and returns the marks and the pairing.
        fn run(&self) -> Result<(Vec<bool>, Stats)> {
            let mut bitmap = BooleanBufferBuilder::new(self.marked_before.len());
            for marked in &self.marked_before {
                bitmap.append(*marked);
            }
            let bitmap = Mutex::new(bitmap);
            let marks = if self.mark_build_rows {
                UnknownMarks::Build(&bitmap)
            } else {
                UnknownMarks::Probe(&bitmap)
            };
            let hash = |key: u64| key % self.hash_modulus;

            let mut foreign_marks = vec![];
            let mark = |build: UInt64Array, probe: UInt32Array| {
                assert!(build.len() <= self.batch_size);
                if self.foreign_marks {
                    let row = (self.filter_seed as usize)
                        .wrapping_add(foreign_marks.len().wrapping_mul(0x9E37_79B9))
                        % self.marked_before.len();
                    bitmap.lock().set_bit(row, true);
                    foreign_marks.push(row);
                }
                // Like `mark_scope_matches` and `mark`: drop the pairs of
                // different scopes, then apply the filter.
                let mut bitmap = bitmap.lock();
                for (build_row, probe_row) in build.values().iter().zip(probe.values()) {
                    let build_key = self.build_keys[*build_row as usize];
                    let probe_key = self.probe_keys[*probe_row as usize];
                    if self.through_scope_map {
                        assert_eq!(hash(build_key), hash(probe_key));
                        if build_key != probe_key {
                            continue;
                        }
                    }
                    if self.passes_filter(*build_row, *probe_row) {
                        bitmap.set_bit(self.decided_row(*build_row, *probe_row), true);
                    }
                }
                Ok(())
            };

            let mut pairs = UnmarkedPairs::new(marks, self.batch_size, mark);
            if self.through_scope_map {
                let mut scope_map = JoinHashMapU64::with_capacity(self.build_rows.len());
                let build_hashes: Vec<u64> = self
                    .build_rows
                    .iter()
                    .map(|row| hash(self.build_keys[*row as usize]))
                    .collect();
                scope_map
                    .update_from_iter(Box::new(build_hashes.iter().enumerate().rev()), 0);
                let probe_hashes: Vec<u64> =
                    self.probe_keys.iter().map(|key| hash(*key)).collect();
                pairs.scope_matches(
                    &scope_map,
                    |position| self.build_rows[position as usize],
                    self.probe_rows.iter().copied(),
                    &probe_hashes,
                    &mut LookupBuffers::default(),
                )?;
            } else {
                pairs.cross_product(
                    self.build_rows.iter().copied(),
                    self.probe_rows.iter().copied(),
                )?;
            }
            let (considered_groups, grouped) = (pairs.considered_groups, pairs.grouped);
            let looked_up_matches = pairs.looked_up_matches;
            let (build_lookups, mark_checks) = (pairs.build_lookups, pairs.mark_checks);
            pairs.finish()?;
            let bitmap = bitmap.into_inner().finish();
            let marks = (0..bitmap.len()).map(|row| bitmap.value(row)).collect();
            Ok((
                marks,
                Stats {
                    considered_groups,
                    grouped,
                    looked_up_matches,
                    build_lookups,
                    mark_checks,
                    foreign_marks,
                },
            ))
        }
    }

    #[derive(Debug)]
    struct Stats {
        considered_groups: bool,
        grouped: bool,
        looked_up_matches: usize,
        build_lookups: usize,
        mark_checks: usize,
        /// The rows that `foreign_marks` marked.
        foreign_marks: Vec<usize>,
    }

    /// Compares the marks of [`UnmarkedPairs`] with those of every pair, on
    /// random inputs: hash collisions between scopes, rows marked before or
    /// meanwhile by other partitions, filters that pass none to all of the
    /// pairs, marks on build or probe rows, and lookups that switch to groups
    /// or not.
    #[test]
    fn unmarked_pairs_match_brute_force() -> Result<()> {
        let mut rng = StdRng::seed_from_u64(0x5C0BE);
        let mut grouped = [0; 2];
        for i in 0..3000 {
            let case = Case::random(&mut rng);
            let (marks, stats) = case.run()?;
            let mut expected = case.expected_marks();
            for row in &stats.foreign_marks {
                expected[*row] = true;
            }
            assert_eq!(marks, expected, "case {i}: {case:?}");
            if stats.grouped {
                grouped[case.mark_build_rows as usize] += 1;
            }
        }
        // Both kinds of marks reach the grouped pairing.
        assert!(grouped.iter().all(|n| *n > 100), "{grouped:?}");
        Ok(())
    }

    /// Every scope holds `rows_per_scope` build rows and as many probe rows.
    /// Returns the pairing of all of them.
    fn equal_scopes(
        mark_build_rows: bool,
        rows_per_scope: usize,
        num_scopes: usize,
        filter_one_in: u64,
    ) -> Result<Stats> {
        let num_rows = rows_per_scope * num_scopes;
        let keys: Vec<u64> = (0..num_rows as u64)
            .map(|row| row % num_scopes as u64)
            .collect();
        let case = Case {
            mark_build_rows,
            through_scope_map: true,
            batch_size: 8192,
            hash_modulus: num_scopes as u64,
            build_keys: keys.clone(),
            probe_keys: keys,
            probe_rows: (0..num_rows as u32).collect(),
            build_rows: (0..num_rows as u64).collect(),
            marked_before: vec![false; num_rows],
            filter_seed: 0,
            filter_one_in,
            foreign_marks: false,
        };
        let (marks, stats) = case.run()?;
        assert_eq!(marks, case.expected_marks());
        Ok(stats)
    }

    /// Scopes of a few rows cost less to look up per probe row than to sort.
    /// So the pairing must not switch to groups for them, whether every pair
    /// marks its row or none does.
    #[test]
    fn small_scopes_are_not_grouped() -> Result<()> {
        for (mark_build_rows, rows_per_scope, filter_one_in) in [
            (true, 1, 1),
            (true, 5, u64::MAX),
            (false, 1, 1),
            (false, 5, 1),
            (false, 5, u64::MAX),
        ] {
            let stats =
                equal_scopes(mark_build_rows, rows_per_scope, 400, filter_one_in)?;
            assert!(
                !stats.considered_groups,
                "marks on build rows: {mark_build_rows}, {rows_per_scope} rows per scope, one in {filter_one_in} pairs passes"
            );
        }
        // Probe rows marked by their first pair waste the other matches of
        // their scope. With 16 build rows per scope that is enough to consider
        // groups, but a group of 16 probe rows fills no chunk.
        let stats = equal_scopes(false, 16, 400, 1)?;
        assert!(stats.considered_groups && !stats.grouped);
        // A large scope does switch.
        for mark_build_rows in [true, false] {
            assert!(equal_scopes(mark_build_rows, 400, 1, 1)?.grouped);
        }
        Ok(())
    }

    /// One scope holds every row, and every pair passes. Returns the matches
    /// that the scope map lookups walked.
    fn walked_matches_in_one_scope(
        mark_build_rows: bool,
        num_build: usize,
        num_probe: usize,
        batch_size: usize,
    ) -> Result<usize> {
        let num_marked = if mark_build_rows {
            num_build
        } else {
            num_probe
        };
        let case = Case {
            mark_build_rows,
            through_scope_map: true,
            batch_size,
            hash_modulus: 1,
            build_keys: vec![0; num_build],
            probe_keys: vec![0; num_probe],
            probe_rows: (0..num_probe as u32).collect(),
            build_rows: (0..num_build as u64).collect(),
            marked_before: vec![false; num_marked],
            filter_seed: 0,
            filter_one_in: 1,
            foreign_marks: false,
        };
        let (marks, stats) = case.run()?;
        assert!(marks.iter().all(|marked| *marked));
        Ok(stats.looked_up_matches)
    }

    #[test]
    fn estimate_groups_from_a_sample() {
        let estimate = |hashes: Vec<u64>| estimate_groups(&hashes);
        // Exact when the rows fit in the sample.
        assert_eq!(estimate(vec![]), 0);
        assert_eq!(estimate((0..100).collect()), 100);
        assert_eq!(estimate((0..256).map(|row| row % 7).collect()), 7);
        // A sample that sees every hash several times.
        assert_eq!(estimate(vec![7; 8192]), 1);
        assert_eq!(estimate((0..8192).map(|row| row % 4).collect()), 4);
        // The sample is fixed, so the estimates are too.
        let periodic = |rows: u64, groups: u64| {
            estimate((0..rows).map(|row| row % groups).collect())
        };
        assert_eq!(periodic(1000, 300), 262);
        assert_eq!(periodic(8192, 1000), 698);
        assert_eq!(periodic(8192, 100), 100);
        assert_eq!(periodic(8192, 8192), 6377);
        // Between those, and with periodic keys as from `value % n`, the
        // estimate stays within a factor of two.
        for (rows, groups) in [
            (1000, 300),
            (8192, 1000),
            (8192, 100),
            (8192, 32),
            (8192, 8192),
        ] {
            let estimated = estimate((0..rows).map(|row| row % groups).collect());
            assert!(
                (groups / 2..=2 * groups).contains(&(estimated as u64)),
                "{rows} rows, {groups} groups: {estimated}"
            );
        }
    }

    #[test]
    fn groups_save_work_when_they_share_walks_or_chunks() {
        // Marks on build rows: a group shares one walk of the hash chain.
        assert!(groups_save_work(true, 10, 5, 0, 8192));
        assert!(!groups_save_work(true, 9, 5, 1_000, 8192));
        // Marks on probe rows: the pairs of a group must fill more than a
        // chunk, here 4 groups of 2048 probe rows.
        assert!(!groups_save_work(false, 8192, 4, 8, 8192));
        assert!(groups_save_work(false, 8192, 4, 9, 8192));
        assert!(groups_save_work(false, 8192, 4, 2, 1024));
        assert!(!groups_save_work(false, 8192, 4, 2, 4096));
    }

    #[test]
    fn mark_checks_back_off_while_they_drop_nothing() {
        assert_eq!(next_unchecked_lookups(0, false), 1);
        assert_eq!(next_unchecked_lookups(1, false), 2);
        assert_eq!(next_unchecked_lookups(5, false), 10);
        assert_eq!(next_unchecked_lookups(20, false), MAX_UNCHECKED_LOOKUPS);
        assert_eq!(
            next_unchecked_lookups(MAX_UNCHECKED_LOOKUPS, false),
            MAX_UNCHECKED_LOOKUPS
        );
        assert_eq!(next_unchecked_lookups(MAX_UNCHECKED_LOOKUPS, true), 0);
        assert_eq!(next_unchecked_lookups(0, true), 0);
    }

    /// With marks on the build rows and a filter that no pair passes, no
    /// build row is ever marked. So the lookups must skip most checks of the
    /// marks.
    #[test]
    fn lookups_skip_checks_while_nothing_is_marked() -> Result<()> {
        let stats = equal_scopes(true, 2_000, 1, u64::MAX)?;
        let (checks, lookups) = (stats.mark_checks, stats.build_lookups);
        assert!(lookups > 100, "{lookups} lookups");
        assert!(checks > 0);
        assert!(
            checks * MAX_UNCHECKED_LOOKUPS / 2 <= lookups,
            "{checks} checks in {lookups} lookups"
        );
        Ok(())
    }

    /// With marks on the probe rows, a scope with far more build rows than
    /// probe rows: once every probe row is marked, the walk of the hash chain
    /// must stop. So the walk stays within the matches before the switch to
    /// groups and a few lookup chunks, whatever the number of build rows.
    #[test]
    fn marked_probe_groups_stop_walking() -> Result<()> {
        for batch_size in [16, 8192] {
            let (num_build, num_probe) = (50_000, 500);
            let walked =
                walked_matches_in_one_scope(false, num_build, num_probe, batch_size)?;
            let bound = WASTED_MATCHES_PER_SORTED_ROW * num_probe + 4 * batch_size;
            assert!(
                walked <= bound,
                "batch_size {batch_size}: {walked} matches walked, bound {bound}"
            );
        }
        Ok(())
    }

    /// One scope with many rows, where every pair passes: once a row is
    /// marked, it must not be paired again. So the scope map lookups walk
    /// about as many matches as there are rows, not one per pair of rows. A
    /// lookup per probe row walks every build row of the scope for every
    /// probe row.
    #[test]
    fn scope_lookups_stay_linear_in_rows() -> Result<()> {
        for mark_build_rows in [true, false] {
            for batch_size in [1, 16, 8192] {
                let (num_build, num_probe) = (2_000, 2_000);
                let walked = walked_matches_in_one_scope(
                    mark_build_rows,
                    num_build,
                    num_probe,
                    batch_size,
                )?;
                // The checks of the build row marks that are skipped can
                // delay the switch to groups by that many lookups.
                let bound = WASTED_MATCHES_PER_SORTED_ROW * num_probe
                    + 2 * num_build
                    + (MAX_UNCHECKED_LOOKUPS + 2) * batch_size;
                // Every build row is found at least once.
                assert!(walked >= num_build, "{walked} matches walked");
                assert!(
                    walked <= bound,
                    "marks on build rows: {mark_build_rows}, batch_size {batch_size}: {walked} matches walked, bound {bound}"
                );
            }
        }
        Ok(())
    }
}