Skip to content

Commit

Permalink
allow single-quote alias names
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed Sep 7, 2024
1 parent ea50e82 commit ea6e21c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions mo_sql_parsing/sql_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,14 @@ def matching(type):
+ RB
) / to_json_call

single_quote_name = Regex(r"\'(\'\'|[^'])*\'") / (lambda x: single_literal(x)['literal'])

alias = Optional((
(
(
AS + (ident("name") + Optional(LB + delimited_list(ident("col")) + RB))
AS + ((ident|single_quote_name)("name") + Optional(LB + delimited_list(ident("col")) + RB))
| (
identifier("name")
(identifier|single_quote_name)("name")
+ Optional((LB + delimited_list(ident("col")) + RB) | (AS + delimited_list(identifier("col"))))
)
)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,15 @@ def test_116(self):

def test_118a(self):
sql = "SELECT * FROM t3 UNION SELECT 3 AS 'a', 4 ORDER BY a"
self.assertRaises(Exception, parse, sql)
result = parse(sql)
expected = {
"from": {"union": [
{"from": "t3", "select": {"all_columns": {}}},
{"select": [{"value": 3, "name": "a"}, {"value": 4}]},
]},
"orderby": {"value": "a"},
}
self.assertEqual(result, expected)

def test_118b(self):
sql = 'SELECT * FROM t3 UNION SELECT 3 AS "a", 4 ORDER BY a'
Expand Down
6 changes: 6 additions & 0 deletions tests/test_sql_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,9 @@ def test_issue_237_declare_multiple_vars_with_code(self):
]},
}}
self.assertEqual(result, expected)

def test_issue247(self):
sql = "select 42 'some_alias'"
result = parse(sql)
expected = {'select': {'name': 'some_alias', 'value': 42}}
self.assertEqual(result, expected)

0 comments on commit ea6e21c

Please sign in to comment.