Skip to content

Fix candidate collection for query with many Statements containing WITHs#635

Open
anderson4j wants to merge 4 commits intomainfrom
fixSlowQuery
Open

Fix candidate collection for query with many Statements containing WITHs#635
anderson4j wants to merge 4 commits intomainfrom
fixSlowQuery

Conversation

@anderson4j
Copy link
Collaborator

@anderson4j anderson4j commented Feb 3, 2026

Inspired by this PR on the db repo.

Certain queries with very many clauses caused the antlr4-c3 library to enter a deep recursion which quickly (in terms of growing query size) became very slow.
ex.

MATCH (n1) WITH * LIMIT 1
MATCH (n2) WITH * LIMIT 1
...
MATCH (n23) WITH * LIMIT 1
MATCH (n) RET

would before take more than 5 seconds to complete. Adding just a few more lines made it slower than I had patience to wait for.

Our fix finds the latest clause and uses this to modify the tokenStartIndex in the candidate collection of antlr4-c3. Doing this, we get back to normal time for candidate collection, even for queries with more than a 100 statements which before would have been completely untenable.

@changeset-bot
Copy link

changeset-bot bot commented Feb 3, 2026

⚠️ No Changeset found

Latest commit: ac50461

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

…their own file and warn about limitations of the new approach
@anderson4j
Copy link
Collaborator Author

I had a discussion with Oskar, and we decided to put fixing the slow query on hold, since we either need to
A: Fix candidate collection in our vendored ANTLR library manually, which can make updating to a "real" dependency more difficult
B: Pass in a context to the candidate collection, which can cause us to miss certain candidates.

We already plan to update the ANTLR dependency at some point, so let's settle that and then reassess how this issue can be fixed

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.

1 participant