Repository navigation
Walk long OR chains iteratively instead of recursing - #232
Merged
Merged
Conversation
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
Contributor
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
stevewds
approved these changes
Oct 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.database/search_doc_ids, for an OR that isn't allbase.idlookups. ORedbase.idlookups already avoid recursion since sqlitedb: make id, isa and regexp searches fast (doc_data search indexes, restricted AND terms) #229.did.datastructures.fieldsearch,case 'or'.did.query/to_searchstructuredoesn't recurse into ORs, so it needed no change.Change
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_idsandfieldsearchnow loop over those operands instead of recursing.fieldsearcherror check: it still raises an error when an operand isn't a structure, and now also for nested operands.fieldsearchstops early: it returns at the first operand that matches. The old|evaluated both sides.Tests
TestDeepOruses chains of 3 × the recursion limit (1,500 terms), built in a loop. It checks:base.idlookups;fieldsearchreturning true and returning false;orOperands' order and its handling of a nested~or, plusfieldsearchonnot(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
0de9cf2here:orOperands↔or_operands;fieldsearchentry;databaseentry.Please merge this PR first, with a merge commit as before.
Generated by Claude Code