Skip to content

fix(spanner): support multi-line client-side statements in dbapi - #19274

Open
uditdasari wants to merge 1 commit into
mainfrom
fix/spanner-dbapi-partition-multiline
Open

uditdasari wants to merge 1 commit into
mainfrom
fix/spanner-dbapi-partition-multiline

Conversation

@uditdasari

@uditdasari uditdasari commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Client-side statements such as PARTITION, RUN PARTITIONED QUERY and the SET statements now keep the full statement text when it spans multiple lines.

Testing:

  • Unit tests for multi-line client-side statements (tests/unit/spanner_dbapi/test_parse_utils.py).
  • Mock server tests that run multi-line PARTITION / RUN PARTITION and RUN PARTITIONED QUERY through cursor.execute and check the SQL sent to the server (tests/mockserver_tests/test_dbapi_partition_query.py).
  • End-to-end run against a real Spanner database (temporary instance and database, deleted afterwards). Each affected statement was written over multiple lines and run with the version before and after this change:
    • PARTITION, RUN PARTITION and RUN PARTITIONED QUERY now use the full query text and return the expected rows.
    • SET AUTOCOMMIT_DML_MODE, SET DATA_BOOST_ENABLED and SET AUTO_PARTITION_MODE with a value spanning lines are now rejected as invalid.
    • A single-line control query returned the same result with both versions.

Internal issue: b/588631179

@uditdasari
uditdasari requested a review from a team as a code owner November 30, 2026 12:53

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +40 to +42
RE_RUN_PARTITION = re.compile(
r"^\s*(RUN)\s+(PARTITION)\s+(.+)", re.IGNORECASE | re.DOTALL
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
)

Comment on lines 46 to 48
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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
)

Comment on lines 49 to 51
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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

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
)
RE_SET_DATA_BOOST_ENABLED = re.compile(
r"^\s*(SET)\s+(DATA_BOOST_ENABLED)\s+(=)\s+(.+)", re.IGNORECASE
)

Comment on lines 55 to 57
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
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
)

Comment on lines +272 to +301
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])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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])

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant