Repository navigation
fix(spanner): support multi-line client-side statements in dbapi #19274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,22 +36,24 @@ | |||||||||||||||
| RE_START_BATCH_DML = re.compile(r"^\s*(START)\s+(BATCH)\s+(DML)\s*$", re.IGNORECASE) | ||||||||||||||||
| RE_RUN_BATCH = re.compile(r"^\s*(RUN)\s+(BATCH)\s*$", re.IGNORECASE) | ||||||||||||||||
| RE_ABORT_BATCH = re.compile(r"^\s*(ABORT)\s+(BATCH)\s*$", re.IGNORECASE) | ||||||||||||||||
| RE_PARTITION_QUERY = re.compile(r"^\s*(PARTITION)\s+(.+)", re.IGNORECASE) | ||||||||||||||||
| RE_RUN_PARTITION = re.compile(r"^\s*(RUN)\s+(PARTITION)\s+(.+)", re.IGNORECASE) | ||||||||||||||||
| RE_PARTITION_QUERY = re.compile(r"^\s*(PARTITION)\s+(.+)", re.IGNORECASE | re.DOTALL) | ||||||||||||||||
| RE_RUN_PARTITION = re.compile( | ||||||||||||||||
| r"^\s*(RUN)\s+(PARTITION)\s+(.+)", re.IGNORECASE | re.DOTALL | ||||||||||||||||
| ) | ||||||||||||||||
| RE_RUN_PARTITIONED_QUERY = re.compile( | ||||||||||||||||
| r"^\s*(RUN)\s+(PARTITIONED)\s+(QUERY)\s+(.+)", re.IGNORECASE | ||||||||||||||||
| r"^\s*(RUN)\s+(PARTITIONED)\s+(QUERY)\s+(.+)", re.IGNORECASE | re.DOTALL | ||||||||||||||||
| ) | ||||||||||||||||
| RE_SET_AUTOCOMMIT_DML_MODE = re.compile( | ||||||||||||||||
| r"^\s*(SET)\s+(AUTOCOMMIT_DML_MODE)\s+(=)\s+(.+)", re.IGNORECASE | ||||||||||||||||
| r"^\s*(SET)\s+(AUTOCOMMIT_DML_MODE)\s+(=)\s+(.+)", re.IGNORECASE | re.DOTALL | ||||||||||||||||
| ) | ||||||||||||||||
|
Comment on lines
46
to
48
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||
| RE_SET_DATA_BOOST_ENABLED = re.compile( | ||||||||||||||||
| r"^\s*(SET)\s+(DATA_BOOST_ENABLED)\s+(=)\s+(.+)", re.IGNORECASE | ||||||||||||||||
| r"^\s*(SET)\s+(DATA_BOOST_ENABLED)\s+(=)\s+(.+)", re.IGNORECASE | re.DOTALL | ||||||||||||||||
| ) | ||||||||||||||||
|
Comment on lines
49
to
51
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||
| RE_SHOW_DATA_BOOST_ENABLED = re.compile( | ||||||||||||||||
| r"^\s*(SHOW)\s+(VARIABLE)\s+(DATA_BOOST_ENABLED)\s*;?\s*$", re.IGNORECASE | ||||||||||||||||
| ) | ||||||||||||||||
| RE_SET_AUTO_PARTITION_MODE = re.compile( | ||||||||||||||||
| r"^\s*(SET)\s+(AUTO_PARTITION_MODE)\s+(=)\s+(.+)", re.IGNORECASE | ||||||||||||||||
| r"^\s*(SET)\s+(AUTO_PARTITION_MODE)\s+(=)\s+(.+)", re.IGNORECASE | re.DOTALL | ||||||||||||||||
| ) | ||||||||||||||||
|
Comment on lines
55
to
57
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||||
| RE_SHOW_AUTO_PARTITION_MODE = re.compile( | ||||||||||||||||
| r"^\s*(SHOW)\s+(VARIABLE)\s+(AUTO_PARTITION_MODE)\s*;?\s*$", re.IGNORECASE | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -244,6 +244,62 @@ def test_run_partitioned_query_classify_stmt(self): | |
| ), | ||
| ) | ||
|
|
||
| def test_multi_line_client_side_statements(self): | ||
| sql = "SELECT s.SongName\nFROM Songs AS s\nWHERE s.SingerId = @id\nLIMIT 10" | ||
| cases = [ | ||
| ("PARTITION", ClientSideStatementType.PARTITION_QUERY), | ||
| ("RUN PARTITIONED QUERY", ClientSideStatementType.RUN_PARTITIONED_QUERY), | ||
| ("PARTITION\n", ClientSideStatementType.PARTITION_QUERY), | ||
| ( | ||
| "RUN\nPARTITIONED\nQUERY\n", | ||
| ClientSideStatementType.RUN_PARTITIONED_QUERY, | ||
| ), | ||
| ] | ||
| for prefix, statement_type in cases: | ||
| with self.subTest(prefix=prefix): | ||
| query = f"{prefix} {sql}" | ||
| parsed_statement = classify_statement(query) | ||
| self.assertEqual( | ||
| parsed_statement, | ||
| ParsedStatement( | ||
| StatementType.CLIENT_SIDE, | ||
| Statement(query), | ||
| statement_type, | ||
| [sql], | ||
| ), | ||
| ) | ||
|
|
||
| def test_run_partition_multi_line_id(self): | ||
| query = "RUN PARTITION abc\ndef" | ||
| parsed_statement = classify_statement(query) | ||
| self.assertEqual(parsed_statement.client_side_statement_params, ["abc\ndef"]) | ||
|
|
||
| def test_set_statements_multi_line_value(self): | ||
| cases = [ | ||
| ( | ||
| "SET AUTOCOMMIT_DML_MODE = PARTITIONED_NON_ATOMIC\nTRANSACTIONAL", | ||
| ClientSideStatementType.SET_AUTOCOMMIT_DML_MODE, | ||
| "PARTITIONED_NON_ATOMIC\nTRANSACTIONAL", | ||
| ), | ||
| ( | ||
| "SET DATA_BOOST_ENABLED = TRUE\nFALSE", | ||
| ClientSideStatementType.SET_DATA_BOOST_ENABLED, | ||
| "TRUE\nFALSE", | ||
| ), | ||
| ( | ||
| "SET AUTO_PARTITION_MODE = TRUE\nFALSE", | ||
| ClientSideStatementType.SET_AUTO_PARTITION_MODE, | ||
| "TRUE\nFALSE", | ||
| ), | ||
| ] | ||
| for query, statement_type, value in cases: | ||
| with self.subTest(query=query): | ||
| parsed_statement = classify_statement(query) | ||
| self.assertEqual( | ||
| parsed_statement.client_side_statement_type, statement_type | ||
| ) | ||
| self.assertEqual(parsed_statement.client_side_statement_params, [value]) | ||
|
Comment on lines
+272
to
+301
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests assert that the parameter values themselves can contain newlines (e.g., def test_run_partition_multi_line_whitespace(self):
query = "RUN\nPARTITION\nabc"
parsed_statement = classify_statement(query)
self.assertEqual(parsed_statement.client_side_statement_params, ["abc"])
def test_set_statements_multi_line_whitespace(self):
cases = [
(
"SET\nAUTOCOMMIT_DML_MODE\n=\nPARTITIONED_NON_ATOMIC",
ClientSideStatementType.SET_AUTOCOMMIT_DML_MODE,
"PARTITIONED_NON_ATOMIC",
),
(
"SET\nDATA_BOOST_ENABLED\n=\nTRUE",
ClientSideStatementType.SET_DATA_BOOST_ENABLED,
"TRUE",
),
(
"SET\nAUTO_PARTITION_MODE\n=\nTRUE",
ClientSideStatementType.SET_AUTO_PARTITION_MODE,
"TRUE",
),
]
for query, statement_type, value in cases:
with self.subTest(query=query):
parsed_statement = classify_statement(query)
self.assertEqual(
parsed_statement.client_side_statement_type, statement_type
)
self.assertEqual(parsed_statement.client_side_statement_params, [value]) |
||
|
|
||
| def test_set_autocommit_dml_mode_stmt(self): | ||
| parsed_statement = classify_statement( | ||
| " set autocommit_dml_mode = PARTITIONED_NON_ATOMIC " | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
re.DOTALLonRE_RUN_PARTITIONallows the value group(.+)to match newlines. However, a partition ID is a single-line token and should not contain newlines. If a user includes a trailing comment or another statement on a subsequent line,re.DOTALLwill cause the regex to greedily swallow it into the partition ID value, leading to parsing or validation failures. Since\s+already matches newlines, multi-line formatting of the statement itself is supported withoutre.DOTALLon the value group.