Skip to content

Commit

Permalink
chore(spanner): Issue1178# [spanner_dbapi] While running a query that…
Browse files Browse the repository at this point in the history
… contains just comment, it causes an IndexError exception (#1181)

- returned ProgrammingError - Invalid statement
  • Loading branch information
bhatt4982 committed Aug 27, 2024
1 parent 44434aa commit bd62d7c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions google/cloud/spanner_dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ def _execute(self, sql, args=None, call_from_execute_many=False):
exception = None
try:
self._parsed_statement = parse_utils.classify_statement(sql, args)
if self._parsed_statement is None:
raise ProgrammingError("Invalid Statement.")

if self._parsed_statement.statement_type == StatementType.CLIENT_SIDE:
self._result_set = client_side_statement_executor.execute(
self, self._parsed_statement
Expand Down
2 changes: 2 additions & 0 deletions google/cloud/spanner_dbapi/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def classify_statement(query, args=None):
# PostgreSQL dollar quoted comments are not
# supported and will not be stripped.
query = sqlparse.format(query, strip_comments=True).strip()
if query == "":
return None
parsed_statement: ParsedStatement = client_side_statement_parser.parse_stmt(query)
if parsed_statement is not None:
return parsed_statement
Expand Down
4 changes: 4 additions & 0 deletions tests/system/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,3 +1598,7 @@ def test_list_tables(self, include_views):
assert "contacts_emails" in table_names
else: # if not include_views:
assert "contacts_emails" not in table_names

def test_invalid_statement_error(self):
with pytest.raises(ProgrammingError):
self._cursor.execute("-- comment only")

0 comments on commit bd62d7c

Please sign in to comment.