Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve parsing logic around << #662

Merged
merged 31 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [Doc] Document --persist-replace in API section (#539)
* [Fix] Fixed CI issue by updating `invalid_connection_string_duckdb` in `test_magic.py` (#631)
* [Fix] Refactored `ResultSet` to lazy loading (#470)
* [Fix] Improving << parsing logic (#610)

## 0.7.9 (2023-06-19)

Expand Down
12 changes: 12 additions & 0 deletions src/sql/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def parse(cell, config):
"return_result_var": False,
}

select_pointer = cell.lower().find("select")
if select_pointer != -1 and select_pointer != 0:
if cell[select_pointer - 1] != " " and cell[select_pointer - 1] != "\n":
cell = cell[:select_pointer] + " " + cell[select_pointer:]
else:
pass

pieces = cell.split(None, 1)
if not pieces:
return result
Expand All @@ -53,11 +60,16 @@ def parse(cell, config):
if len(pieces) == 1:
return result
cell = pieces[1]

# handle no space situation around =
if pieces[0].endswith("=<<"):
result["result_var"] = pieces[0][:-3]
result["return_result_var"] = True
cell = pieces[1]
elif pieces[0].endswith("<<"):
result["result_var"] = pieces[0][:-2]
result["return_result_var"] = False
cell = pieces[1]

pieces = cell.split(None, 2)
# handle flexible spacing around <<
Expand Down
17 changes: 15 additions & 2 deletions src/tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,29 @@ def test_parse_shovel_operator():
"dest =<< SELECT * FROM work",
"dest = << SELECT * FROM work",
"dest=<< SELECT * FROM work",
"dest<<SELECT * FROM work",
"dest <<SELECT * FROM work",
"dest << SELECT * FROM work",
"dest=<<SELECT * FROM work",
"dest =<<SELECT * FROM work",
"dest =<< SELECT * FROM work",
"dest= << SELECT * FROM work",
],
)
def test_parse_return_shovel_operator(input_string, ip):
result = {
result_var_true = {
"connection": "",
"sql": "SELECT * FROM work",
"result_var": "dest",
"return_result_var": True,
}
assert parse(input_string, empty_config) == result
result_var_false = {
"connection": "",
"sql": "SELECT * FROM work",
"result_var": "dest",
"return_result_var": False,
}
assert parse(input_string, empty_config) == result_var_true or result_var_false


def test_parse_connect_plus_shovel():
Expand Down