diff --git a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/client_side_statement_parser.py b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/client_side_statement_parser.py index bce3842927e5..dee919e13309 100644 --- a/packages/google-cloud-spanner/google/cloud/spanner_dbapi/client_side_statement_parser.py +++ b/packages/google-cloud-spanner/google/cloud/spanner_dbapi/client_side_statement_parser.py @@ -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 ) 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_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 ) RE_SHOW_AUTO_PARTITION_MODE = re.compile( r"^\s*(SHOW)\s+(VARIABLE)\s+(AUTO_PARTITION_MODE)\s*;?\s*$", re.IGNORECASE diff --git a/packages/google-cloud-spanner/tests/mockserver_tests/test_dbapi_partition_query.py b/packages/google-cloud-spanner/tests/mockserver_tests/test_dbapi_partition_query.py index b3fd6fb4db1c..a10ac6d7f2aa 100644 --- a/packages/google-cloud-spanner/tests/mockserver_tests/test_dbapi_partition_query.py +++ b/packages/google-cloud-spanner/tests/mockserver_tests/test_dbapi_partition_query.py @@ -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) diff --git a/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_parse_utils.py b/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_parse_utils.py index 4587e2f0fa83..2a7844611efb 100644 --- a/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_parse_utils.py +++ b/packages/google-cloud-spanner/tests/unit/spanner_dbapi/test_parse_utils.py @@ -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]) + def test_set_autocommit_dml_mode_stmt(self): parsed_statement = classify_statement( " set autocommit_dml_mode = PARTITIONED_NON_ATOMIC "