Avoid quadratic accumulation when parsing long SQL expression lists - #963
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe parser now accumulates expressions with linked lists and materializes them when building AST children. Clause and set-operation children use lazy getters. The large-query memory test is enabled. ChangesParser expression handling
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
nene
left a comment
There was a problem hiding this comment.
Thanks for the PR. The fix seems to work, but the code is somewhat cryptic and seems to contain various extra stuff that's not really part of the core optimization proposed in this PR.
| get children(): AstNode[] { | ||
| return [exp, ...materializeExpressionList(expressions)]; |
There was a problem hiding this comment.
What's the purpose of this wrapping of children property into a getter function?
It looks like some an additional performance optimization. But not sure it's really adding anything. At least when I removed these, the code still seemed to perform similarly.
If this is a separate unrelated optimization, then please make a separate PR with it. If it's tightly related to the current optimization and really needed for it, please provide some explanation.
There was a problem hiding this comment.
Yeah this does help in cases like SELECT column_1, column_2, column_3, ..., column_1000 FROM my_table or SELECT * FROM my_table WHERE id = 1 OR id = 2 OR id = 3 ... OR id = 1000;. Nearley creates an intermediate clause for every prefix, so eagerly constructing children repeatedly copies the entire prefix and makes this quadratic.
The test from eb187fe mostly demonstrates this case. It does "pass" without this change because peak/final memory is lowered (by only the linkedlist change), but it still takes ~20x longer without this (using quadratic total memory, even if not peak memory)
I could split this into a separate follow-up PR if that helps, was just broadly tackling the several related issues of "quadratic costs"
There was a problem hiding this comment.
OK. Thanks for the explanation. Makes sense now.
6544625 to
e2ed5ec
Compare
nene
left a comment
There was a problem hiding this comment.
Thanks for the cleanup and clarifications.
Summary
INlists, longORchains, and wideSELECTlists.Why
Nearley expands
free_form_sql:*into an append postprocessor equivalent toitems.concat([item]). Repeatedly copying every prefix produces quadratic allocation. For a representative 1,000-valueINquery, these generated repetition postprocessors copied 1,997,014 prefix entries before this change and 10 afterward. Final AST construction still performs the necessary linear work: 3,004 list entries are materialized and another 3,000 entries are copied into completed arrays. Mutating the shared array with.push()is unsafe because alternative parses share prefixes.free_form_sqlremains the existing rule for one SQL element. The newexpression_listrecognizes exactly the same zero-or-more sequence asfree_form_sql:*:Each append instead creates an immutable
{ previous, value }node. Consequently,expressions_or_clauses -> expression_list clause:*preserves the original grammar: free-form elements followed by structured clauses. Clause materialization is deferred because eagerly converting every intermediate parser candidate would reintroduce quadratic copying.Benchmarks
Apple M4 Max, Node.js 24.12.0. Values show median formatting time and whole-process peak RSS. Stress processes used a 384 MB V8 old-space limit.
INININORchainSELECTValidation
pnpm run grammarpnpm run ts:checkpnpm run pretty:checkpnpm run lintpnpm run buildpnpm exec jest --runInBand --silent --coverage=false: 27 suites, 5,841 tests, and 63 snapshots passed.Closes #840.