Skip to content

Commit bd62d7c

Browse files
authored
chore(spanner): Issue1178# [spanner_dbapi] While running a query that contains just comment, it causes an IndexError exception (#1181)
- returned ProgrammingError - Invalid statement
1 parent 44434aa commit bd62d7c

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

google/cloud/spanner_dbapi/cursor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ def _execute(self, sql, args=None, call_from_execute_many=False):
251251
exception = None
252252
try:
253253
self._parsed_statement = parse_utils.classify_statement(sql, args)
254+
if self._parsed_statement is None:
255+
raise ProgrammingError("Invalid Statement.")
256+
254257
if self._parsed_statement.statement_type == StatementType.CLIENT_SIDE:
255258
self._result_set = client_side_statement_executor.execute(
256259
self, self._parsed_statement

google/cloud/spanner_dbapi/parse_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ def classify_statement(query, args=None):
226226
# PostgreSQL dollar quoted comments are not
227227
# supported and will not be stripped.
228228
query = sqlparse.format(query, strip_comments=True).strip()
229+
if query == "":
230+
return None
229231
parsed_statement: ParsedStatement = client_side_statement_parser.parse_stmt(query)
230232
if parsed_statement is not None:
231233
return parsed_statement

tests/system/test_dbapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,3 +1598,7 @@ def test_list_tables(self, include_views):
15981598
assert "contacts_emails" in table_names
15991599
else: # if not include_views:
16001600
assert "contacts_emails" not in table_names
1601+
1602+
def test_invalid_statement_error(self):
1603+
with pytest.raises(ProgrammingError):
1604+
self._cursor.execute("-- comment only")

0 commit comments

Comments
 (0)