Skip to content

Walk long OR chains iteratively instead of recursing - #232

Merged
stevevanhooser merged 1 commit into
mainfrom
claude/did-base-id-index
Oct 24, 2026
Merged

stevevanhooser merged 1 commit into
mainfrom
claude/did-base-id-index

Conversation

@stevevanhooser

Copy link
Copy Markdown
Contributor

MATLAB counterpart of VH-Lab/DID-python#100 / #103.

Problem

Building a query in a loop with q = q | did.query(...) nests one 'or' inside another for every term. Two places walked that nesting by calling themselves once per level. A few hundred terms therefore hit MATLAB's recursion limit (500 by default) before anything was matched:

did.query/to_searchstructure doesn't recurse into ORs, so it needed no change.

Change

  • New did.datastructures.orOperands. It flattens a chain of nested ORs into a list of operands, in left-to-right order, using an explicit stack. A nested negated OR (~or) stays a single operand, because its negation applies to its whole result.
  • search_doc_ids and fieldsearch now loop over those operands instead of recursing.
  • fieldsearch error check: it still raises an error when an operand isn't a structure, and now also for nested operands.
  • fieldsearch stops early: it returns at the first operand that matches. The old | evaluated both sides.

Tests

TestDeepOr uses chains of 3 × the recursion limit (1,500 terms), built in a loop. It checks:

  • a search with an OR of other terms;
  • the same OR ANDed with a number range;
  • an OR of base.id lookups;
  • fieldsearch returning true and returning false;
  • orOperands' order and its handling of a nested ~or, plus fieldsearch on not(c or d).

Not run here: this environment has no MATLAB.

DID-python

VH-Lab/DID-python#103 makes the same change. Its bridge entries point at 0de9cf2 here:

  • a new entry orOperands ↔ or_operands;
  • the fieldsearch entry;
  • the database entry.

Please merge this PR first, with a merge commit as before.


Generated by Claude Code

q = q | did.query(...) in a loop nests one 'or' per term. Two places
recursed once per level, so a few hundred terms (MATLAB's default recursion
limit is 500) hit MATLAB:recursionLimit before anything was matched:

- did.database/search_doc_ids, for an OR that is not all base.id lookups
  (ORed base.id lookups have been walked iteratively since #229);
- did.datastructures.fieldsearch, case 'or'.

New did.datastructures.orOperands flattens a chain of nested ORs into its
operands, left to right, with a stack; both now loop over those. A negated
'~or' nested in the chain is kept as one operand, since its negation
applies to its whole result. fieldsearch still errors if an operand is not
a structure, now for nested operands too, and stops at the first operand
that matches. did.query/to_searchstructure does not recurse into ORs and
needed no change. Mirrors DID-python's or_operands (DID-python#100/#103).

Test: TestDeepOr - with chains of 3x the recursion limit (1,500 terms),
built in a loop: search of an OR of other terms, the same ANDed with a
number range, an OR of base.id lookups, fieldsearch true and false; and
orOperands' order and '~or' handling, with fieldsearch on not(c or d).
Not run here: no MATLAB in this environment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dv7SxVR8MCu3MKpUvwwUia
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

455 tests  +5   455 ✅ +5   2m 32s ⏱️ +48s
 40 suites +1     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 0de9cf2. ± Comparison against base commit e4a4287.

@codecov

codecov Bot commented Oct 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.41%. Comparing base (e4a4287) to head (0de9cf2).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #232      +/-   ##
==========================================
+ Coverage   66.29%   66.41%   +0.11%     
==========================================
  Files          66       67       +1     
  Lines        4694     4710      +16     
==========================================
+ Hits         3112     3128      +16     
  Misses       1582     1582              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stevevanhooser
stevevanhooser merged commit 6a398d0 into main Oct 24, 2026
6 checks passed
@stevevanhooser
stevevanhooser deleted the claude/did-base-id-index branch October 24, 2026 18:30
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.

3 participants