Skip to content

Commit 2a36900

Browse files
authored
Merge pull request #6 from mindsdb/is-qouted
Memorize the qouted parts of Identifier
2 parents 707861f + 4770d1c commit 2a36900

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

mindsdb_sql_parser/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__title__ = 'mindsdb_sql_parser'
22
__package_name__ = 'mindsdb_sql_parser'
3-
__version__ = '0.2.0'
3+
__version__ = '0.3.0'
44
__description__ = "Mindsdb SQL parser"
55
__email__ = "[email protected]"
66
__author__ = 'MindsDB Inc'

mindsdb_sql_parser/ast/select/identifier.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
from copy import copy, deepcopy
3+
from typing import List
34

45
from mindsdb_sql_parser.ast.base import ASTNode
56
from mindsdb_sql_parser.utils import indent
@@ -11,9 +12,13 @@
1112

1213

1314
def path_str_to_parts(path_str: str):
14-
match = re.finditer(path_str_parts_regex, path_str)
15-
parts = [x[0].strip('`') for x in match]
16-
return parts
15+
parts, is_quoted = [], []
16+
for x in re.finditer(path_str_parts_regex, path_str):
17+
part = x[0].strip('`')
18+
parts.append(part)
19+
is_quoted.append(x[0] != part)
20+
21+
return parts, is_quoted
1722

1823

1924
RESERVED_KEYWORDS = {
@@ -42,13 +47,17 @@ def __init__(self, path_str=None, parts=None, *args, **kwargs):
4247
parts = [Star()]
4348

4449
if path_str and not parts:
45-
parts = path_str_to_parts(path_str)
50+
parts, is_quoted = path_str_to_parts(path_str)
51+
else:
52+
is_quoted = [False] * len(parts)
4653
assert isinstance(parts, list)
4754
self.parts = parts
55+
# parts which were quoted
56+
self.is_quoted: List[bool] = is_quoted
4857

4958
@classmethod
5059
def from_path_str(self, value, *args, **kwargs):
51-
parts = path_str_to_parts(value)
60+
parts, _ = path_str_to_parts(value)
5261
return Identifier(parts=parts, *args, **kwargs)
5362

5463
def parts_to_str(self):

mindsdb_sql_parser/parser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ def from_table(self, p):
12641264
@_('PLUGINS',
12651265
'ENGINES')
12661266
def from_table(self, p):
1267-
return Identifier.from_path_str(p[0])
1267+
return Identifier(p[0])
12681268

12691269
@_('identifier')
12701270
def from_table(self, p):
@@ -1739,6 +1739,7 @@ def identifier(self, p):
17391739
node.parts.append(p[2])
17401740
else:
17411741
node.parts += p[2].parts
1742+
node.is_quoted.append(p[2].is_quoted[0])
17421743
return node
17431744

17441745
@_('quote_string',
@@ -1749,7 +1750,7 @@ def string(self, p):
17491750
@_('id', 'dquote_string')
17501751
def identifier(self, p):
17511752
value = p[0]
1752-
return Identifier.from_path_str(value)
1753+
return Identifier(value)
17531754

17541755
@_('PARAMETER')
17551756
def parameter(self, p):

tests/test_base_sql/test_select_structure.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,43 @@ def test_table_double_quote(self):
11821182
ast = parse_sql(sql)
11831183
assert str(ast) == str(expected_ast)
11841184

1185+
def test_double_quote_render_skip(self):
1186+
sql = 'select `KEY_ID` from `Table1` where `id`=2'
1187+
1188+
expected_ast = Select(
1189+
targets=[Identifier('KEY_ID')],
1190+
from_table=Identifier(parts=['Table1']),
1191+
where=BinaryOperation(op='=', args=[
1192+
Identifier('id'), Constant(2)
1193+
])
1194+
)
1195+
1196+
ast = parse_sql(sql)
1197+
assert str(ast) == str(expected_ast)
1198+
1199+
# check is quoted
1200+
assert ast.targets[0].is_quoted == [True]
1201+
assert ast.from_table.is_quoted == [True]
1202+
assert ast.where.args[0].is_quoted == [True]
1203+
1204+
sql = 'select KEY_ID from Table1 where id=2'
1205+
1206+
expected_ast = Select(
1207+
targets=[Identifier('KEY_ID')],
1208+
from_table=Identifier(parts=['Table1']),
1209+
where=BinaryOperation(op='=', args=[
1210+
Identifier('id'), Constant(2)
1211+
])
1212+
)
1213+
1214+
ast = parse_sql(sql)
1215+
assert str(ast) == str(expected_ast)
1216+
1217+
# check is not quoted
1218+
assert ast.targets[0].is_quoted == [False]
1219+
assert ast.from_table.is_quoted == [False]
1220+
assert ast.where.args[0].is_quoted == [False]
1221+
11851222
def test_window_function_mindsdb(self):
11861223

11871224
# modifier

tests/test_standard_render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def check_module(module):
2828

2929
tests = klass()
3030
for test_name, test_method in inspect.getmembers(tests, predicate=inspect.ismethod):
31-
if not test_name.startswith('test_') or test_name.endswith('_error'):
31+
if not test_name.startswith('test_') or test_name.endswith('_error') or test_name.endswith('_render_skip'):
3232
# skip tests that expected error
3333
continue
3434
sig = inspect.signature(test_method)

0 commit comments

Comments
 (0)