From 848973f63de36ff3f90a8b7412dc3ee55fbd95f0 Mon Sep 17 00:00:00 2001 From: Ryan Wooden Date: Wed, 21 Oct 2015 07:33:56 -0300 Subject: Limit number of tokens checked in group_identifier. This significantly improves performance when grouping a large list of IDs. --- sqlparse/engine/grouping.py | 7 +++++-- sqlparse/sql.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'sqlparse') diff --git a/sqlparse/engine/grouping.py b/sqlparse/engine/grouping.py index a317044..b30e564 100644 --- a/sqlparse/engine/grouping.py +++ b/sqlparse/engine/grouping.py @@ -188,9 +188,12 @@ def group_identifier(tlist): t1 = tl.token_next_by_type( i, (T.String.Symbol, T.Name, T.Literal.Number.Integer, T.Literal.Number.Float)) - t2 = tl.token_next_by_instance(i, (sql.Function, sql.Parenthesis)) + + i1 = tl.token_index(t1) if t1 else None + t2_end = None if i1 is None else i1 + 1 + t2 = tl.token_next_by_instance(i, (sql.Function, sql.Parenthesis), end=t2_end) + if t1 and t2: - i1 = tl.token_index(t1) i2 = tl.token_index(t2) if i1 > i2: return t2 diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 5ecfbdc..f965f03 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -256,7 +256,7 @@ class TokenList(Token): continue return token - def token_next_by_instance(self, idx, clss): + def token_next_by_instance(self, idx, clss, end=None): """Returns the next token matching a class. *idx* is where to start searching in the list of child tokens. @@ -267,7 +267,7 @@ class TokenList(Token): if not isinstance(clss, (list, tuple)): clss = (clss,) - for token in self.tokens[idx:]: + for token in self.tokens[idx:end]: if isinstance(token, clss): return token -- cgit v1.2.1