Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Comment on lines +40 to +42

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
)

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

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
)

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

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
)

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

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
)

RE_SHOW_AUTO_PARTITION_MODE = re.compile(
r"^\s*(SHOW)\s+(VARIABLE)\s+(AUTO_PARTITION_MODE)\s*;?\s*$", re.IGNORECASE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,45 @@ def test_partition_query_with_complex_parameters(self):
all_names.append(row[0])

self.assertEqual(["Charlie"], all_names)

def _add_multi_line_partition_results(self, sql):
partition_response = spanner_types.PartitionResponse()
partition_response.partitions.extend(
[spanner_types.Partition(partition_token=b"multi-line-token")]
)
self.spanner_service.mock_spanner.add_partition_result(sql, partition_response)
add_single_result(sql, "name", TypeCode.STRING, [("Alice",)])

def _assert_sql_sent(self, request_type, sql):
requests = [
r for r in self.spanner_service.requests if isinstance(r, request_type)
]
self.assertEqual(1, len(requests))
self.assertEqual(sql, requests[0].sql)

def test_multi_line_partition_and_run_partition(self):
sql = "SELECT name FROM users\nWHERE active = true\nAND age > 18"
self._add_multi_line_partition_results(sql)
connection = Connection(self.instance, self.database, read_only=True)

with connection.cursor() as cursor:
cursor.execute(f"PARTITION\n{sql}")
partition_ids = cursor.fetchall()
self.assertEqual(1, len(partition_ids))
cursor.execute(f"RUN PARTITION {partition_ids[0][0]}")
self.assertEqual([["Alice"]], [list(row) for row in cursor.fetchall()])

self._assert_sql_sent(spanner_types.PartitionQueryRequest, sql)
self._assert_sql_sent(spanner_types.ExecuteSqlRequest, sql)

def test_multi_line_run_partitioned_query(self):
sql = "SELECT name FROM users\nWHERE active = true\nAND age > 18"
self._add_multi_line_partition_results(sql)
connection = Connection(self.instance, self.database, read_only=True)

with connection.cursor() as cursor:
cursor.execute(f"RUN PARTITIONED QUERY\n{sql}")
self.assertEqual([["Alice"]], [list(row) for row in cursor.fetchall()])

self._assert_sql_sent(spanner_types.PartitionQueryRequest, sql)
self._assert_sql_sent(spanner_types.ExecuteSqlRequest, sql)
Original file line number Diff line number Diff line change
Expand Up @@ -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

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


def test_set_autocommit_dml_mode_stmt(self):
parsed_statement = classify_statement(
" set autocommit_dml_mode = PARTITIONED_NON_ATOMIC "
Expand Down
Loading