Repository navigation
avoid quadratic getToken(i) rescans in lookahead token walks - #2862
Merged
manticore-projects merged 1 commit intoOct 27, 2026
Merged
Conversation
Contributor
|
Thank you very much! |
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.
Several semantic lookahead helpers in
JSqlParserCC.jjtscan forward withgetToken(i)inside an unbounded loop, andgetToken(i)walksitokens from the current position on every call, so each scan is quadratic in the number of tokens it covers (the same problem #2780 fixed inisNestedSetOperationAhead). The worst case is the memoizing pass ofisFunctionAheadWalk/isAllTableColumnsWalk, which any dotted name with more than eight parts reaches:SELECT a0.a1.....a7999 FROM t(47 KB) spends about 16.5 s inCCJSqlParserUtil.parse, well past the default 8 s timeout. I found it while profiling parse time on long generated statements, where the samples sat inisAllTableColumnsWalkongetToken.isTernaryAhead(probed fromisComparisonOperatorAheadduring lookahead),isObjectPrivilegeAhead,isStatementBlockAhead,isInsertOnConflictAhead,isPostgreSqlTriggerAhead,isNamedExprListAhead,isPostgreSqlRoutineCharacteristicAheadand the role loop inisMySqlSetRoleAheaduse the same pattern, so they now followToken.nextthrough the existingnextWithItemToken()helper as well, visiting the same tokens in the same order. With the change the 8000-part chain parses in about 30 ms and 64000 parts (437 KB) in about 0.4 s, and a 16000-privilegeGRANTdrops from 4.4 s to 0.14 s. The newLongChainPredicateTestcase parses 10000-part column,.*and function chains under a 5 s preemptive timeout; it fails on master and passes in 0.4 s here, and./gradlew checkis green apart fromStringValueTest's timing test, which timed out once during the full run and passes on its own.