Skip to content

Fix fmatch() matching logical NA to non-logical table values - #872

Merged
SebKrantz merged 1 commit into
masterfrom
claude/issue-870-20260810-1928
Aug 11, 2026
Merged

Fix fmatch() matching logical NA to non-logical table values#872
SebKrantz merged 1 commit into
masterfrom
claude/issue-870-20260810-1928

Conversation

@SebKrantz

Copy link
Copy Markdown
Member

Summary

  • Fixes a bug where fmatch() (and thus %in%/%iin% and joins) matched a logical NA in x to a non-logical integer value in table (e.g., 2L) instead of returning NA like base::match().
  • Root cause: in src/match.c::match_single(), a logical x always took a specialized 3-bucket hash path (reserving one bucket for NA) regardless of whether table was actually logical, causing collisions with real table values.
  • Fix: only use the specialized path when table is also logical; otherwise fall back to the generic, collision-safe integer hashing path.
  • Corrects a wrong test assertion from a prior attempt (PR Fix fmatch() matching logical NA to non-logical table values #871): NA %iin% table should be integer(0), not NA_integer_, since %iin% returns matched indices.

Closes #870.

Test plan

  • Corrected and expanded regression tests in tests/testthat/test-fmatch.R comparing against base::match()
  • CI/R CMD check should be run to confirm (could not run R locally in this sandbox)

Generated with Claude Code

The C fix in match_single() (only take the M=3 logical-specific hash
path when table is also logical, otherwise fall back to generic
integer hashing) was correct, but the regression test asserted
`NA %iin% table` == NA_integer_. `%iin%` returns indices (via
whichNA(..., invert = TRUE)), so a non-match correctly yields
integer(0), not NA_integer_. Corrected the assertion and added a few
more edge cases (larger table, table with a real NA, double table,
genuine logical-vs-logical matching).

Closes #870.

Co-authored-by: Sebastian Krantz <48053842+SebKrantz@users.noreply.github.com>
@SebKrantz
SebKrantz merged commit bb21c1c into master Aug 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] fmatch matches (logical) NA to 2L

1 participant