Repository navigation
fix(spanner): support multi-line client-side statements in dbapi - #19274
uditdasari wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates several client-side statement regular expressions in client_side_statement_parser.py to use re.DOTALL, enabling support for multi-line client-side statements, and adds corresponding tests. The reviewer noted that applying re.DOTALL to regexes like RE_RUN_PARTITION and various SET statements allows their trailing value groups (.+) to match newlines. Since partition IDs and configuration values are single-line tokens, this greedy matching could swallow trailing comments or subsequent statements. The reviewer recommended removing re.DOTALL from these specific regexes and updating the unit tests to verify that the statement structure can span multiple lines while the extracted parameter values remain single-line tokens.
| RE_RUN_PARTITION = re.compile( | ||
| r"^\s*(RUN)\s+(PARTITION)\s+(.+)", re.IGNORECASE | re.DOTALL | ||
| ) |
There was a problem hiding this comment.
Using re.DOTALL on RE_RUN_PARTITION allows 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.DOTALL will 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 without re.DOTALL on the value group.
| RE_RUN_PARTITION = re.compile( | |
| r"^\s*(RUN)\s+(PARTITION)\s+(.+)", re.IGNORECASE | re.DOTALL | |
| ) | |
| RE_RUN_PARTITION = re.compile( | |
| r"^\s*(RUN)\s+(PARTITION)\s+(.+)", re.IGNORECASE | |
| ) |
| 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 | ||
| ) |
There was a problem hiding this comment.
Using re.DOTALL on RE_SET_AUTOCOMMIT_DML_MODE allows the value group (.+) to match newlines. However, the autocommit DML mode is a single-line token (e.g., PARTITIONED_NON_ATOMIC) and should not contain newlines. If a user includes a trailing comment or another statement on a subsequent line, re.DOTALL will cause the regex to greedily swallow it into the mode value, leading to validation failures. Since \s+ already matches newlines, multi-line formatting of the statement itself is supported without re.DOTALL on the value group.
| 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 | |
| ) | |
| RE_SET_AUTOCOMMIT_DML_MODE = re.compile( | |
| r"^\s*(SET)\s+(AUTOCOMMIT_DML_MODE)\s+(=)\s+(.+)", re.IGNORECASE | |
| ) |
| 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 | ||
| ) |
There was a problem hiding this comment.
Using re.DOTALL on RE_SET_DATA_BOOST_ENABLED allows the value group (.+) to match newlines. However, the data boost enabled value is a single-line token (e.g., TRUE or FALSE) and should not contain newlines. If a user includes a trailing comment or another statement on a subsequent line, re.DOTALL will cause the regex to greedily swallow it into the value, leading to validation failures. Since \s+ already matches newlines, multi-line formatting of the statement itself is supported without re.DOTALL on the value group.
| 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 | |
| ) | |
| RE_SET_DATA_BOOST_ENABLED = re.compile( | |
| r"^\s*(SET)\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 | ||
| ) |
There was a problem hiding this comment.
Using re.DOTALL on RE_SET_AUTO_PARTITION_MODE allows the value group (.+) to match newlines. However, the auto partition mode value is a single-line token (e.g., TRUE or FALSE) and should not contain newlines. If a user includes a trailing comment or another statement on a subsequent line, re.DOTALL will cause the regex to greedily swallow it into the value, leading to validation failures. Since \s+ already matches newlines, multi-line formatting of the statement itself is supported without re.DOTALL on the value group.
| 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 | |
| ) | |
| RE_SET_AUTO_PARTITION_MODE = re.compile( | |
| r"^\s*(SET)\s+(AUTO_PARTITION_MODE)\s+(=)\s+(.+)", re.IGNORECASE | |
| ) |
| 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]) |
There was a problem hiding this comment.
These tests assert that the parameter values themselves can contain newlines (e.g., PARTITIONED_NON_ATOMIC\nTRANSACTIONAL or TRUE\nFALSE). However, these values are single-line tokens and should not contain newlines. Instead, we should test that the statement itself can be formatted across multiple lines (e.g., with newlines around keywords and operators) while the extracted parameter value remains a valid single-line token.
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])
Client-side statements such as
PARTITION,RUN PARTITIONED QUERYand theSETstatements now keep the full statement text when it spans multiple lines.Testing:
tests/unit/spanner_dbapi/test_parse_utils.py).PARTITION/RUN PARTITIONandRUN PARTITIONED QUERYthroughcursor.executeand check the SQL sent to the server (tests/mockserver_tests/test_dbapi_partition_query.py).PARTITION,RUN PARTITIONandRUN PARTITIONED QUERYnow use the full query text and return the expected rows.SET AUTOCOMMIT_DML_MODE,SET DATA_BOOST_ENABLEDandSET AUTO_PARTITION_MODEwith a value spanning lines are now rejected as invalid.Internal issue: b/588631179