Skip to content

Commit 4abf8f1

Browse files
committed
keep escape in identifier parts
1 parent 4aa5641 commit 4abf8f1

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

mindsdb_sql_parser/ast/select/identifier.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,21 @@ def path_str_to_parts(path_str: str):
2727
}
2828

2929

30-
def get_reserved_words():
31-
from mindsdb_sql_parser.lexer import MindsDBLexer
30+
_reserved_keywords: set[str] = None
3231

33-
reserved = RESERVED_KEYWORDS
34-
for word in MindsDBLexer.tokens:
35-
if '_' not in word:
36-
# exclude combinations
37-
reserved.add(word)
38-
return reserved
32+
33+
def get_reserved_words() -> set[str]:
34+
global _reserved_keywords
35+
36+
if _reserved_keywords is None:
37+
from mindsdb_sql_parser.lexer import MindsDBLexer
38+
39+
_reserved_keywords = RESERVED_KEYWORDS
40+
for word in MindsDBLexer.tokens:
41+
if '_' not in word:
42+
# exclude combinations
43+
_reserved_keywords.add(word)
44+
return _reserved_keywords
3945

4046

4147
class Identifier(ASTNode):
@@ -66,14 +72,14 @@ def from_path_str(self, value, *args, **kwargs):
6672
def parts_to_str(self):
6773
out_parts = []
6874
reserved_words = get_reserved_words()
69-
for part in self.parts:
75+
for part, is_quoted in zip(self.parts, self.is_quoted):
7076
if isinstance(part, Star):
7177
part = str(part)
7278
else:
7379
if (
74-
not no_wrap_identifier_regex.fullmatch(part)
75-
or
76-
part.upper() in reserved_words
80+
is_quoted
81+
or not no_wrap_identifier_regex.fullmatch(part)
82+
or part.upper() in reserved_words
7783
):
7884
part = f'`{part}`'
7985

tests/test_base_sql/test_ast.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ def test_identifier_deepcopy_is_quoted(self):
3131
ident = Identifier('`a`')
3232
ident2 = deepcopy(ident)
3333
assert ident2.is_quoted == [True]
34+
35+
def test_identifier_to_string(self):
36+
test_cases = [
37+
'test',
38+
'Test',
39+
'TEST',
40+
'`test`',
41+
'`Test`',
42+
'`TEST`'
43+
]
44+
45+
for test_case in test_cases:
46+
assert Identifier(test_case).to_string() == test_case
47+
48+
for i in range(len(test_cases)):
49+
for test_case in test_cases:
50+
test_str = f'{test_case}.{test_cases[i]}'
51+
assert Identifier(test_str).to_string() == test_str

0 commit comments

Comments
 (0)