Skip to content

Commit 6df6313

Browse files
regex for warehouse_id instead of .split, remove excess imports and behaviour
Signed-off-by: varun-edachali-dbx <[email protected]>
1 parent 5a44c48 commit 6df6313

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

src/databricks/sql/backend/sea_backend.py

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import logging
2-
import uuid
3-
from typing import Dict, Tuple, List, Optional, Any, Union, TYPE_CHECKING
2+
import re
3+
from typing import Dict, Tuple, List, Optional, Union, TYPE_CHECKING
44

55
if TYPE_CHECKING:
66
from databricks.sql.client import Cursor
77

88
from databricks.sql.backend.databricks_client import DatabricksClient
99
from databricks.sql.backend.types import SessionId, CommandId, CommandState, BackendType
10-
from databricks.sql.exc import Error, NotSupportedError, ServerOperationError
10+
from databricks.sql.exc import ServerOperationError
1111
from databricks.sql.backend.utils.http_client import SeaHttpClient
1212
from databricks.sql.thrift_api.TCLIService import ttypes
1313
from databricks.sql.types import SSLOptions
@@ -88,49 +88,35 @@ def _extract_warehouse_id(self, http_path: str) -> str:
8888
"""
8989
Extract the warehouse ID from the HTTP path.
9090
91-
The warehouse ID is expected to be the last segment of the path when the
92-
second-to-last segment is either 'warehouses' or 'endpoints'.
93-
This matches the JDBC implementation which supports both formats.
94-
9591
Args:
9692
http_path: The HTTP path from which to extract the warehouse ID
9793
9894
Returns:
9995
The extracted warehouse ID
10096
10197
Raises:
102-
Error: If the warehouse ID cannot be extracted from the path
98+
ValueError: If the warehouse ID cannot be extracted from the path
10399
"""
104-
105-
path_parts = http_path.strip("/").split("/")
106-
warehouse_id = None
107-
108-
if len(path_parts) >= 3 and path_parts[-2] in ["warehouses", "endpoints"]:
109-
warehouse_id = path_parts[-1]
110-
logger.debug(
111-
f"Extracted warehouse ID: {warehouse_id} from path: {http_path}"
112-
)
113-
114-
if not warehouse_id:
115-
error_message = (
116-
f"Could not extract warehouse ID from http_path: {http_path}. "
117-
f"Expected format: /path/to/warehouses/{{warehouse_id}} or "
118-
f"/path/to/endpoints/{{warehouse_id}}"
119-
)
120-
logger.error(error_message)
121-
raise ValueError(error_message)
122-
123-
return warehouse_id
124-
125-
@property
126-
def staging_allowed_local_path(self) -> Union[None, str, List[str]]:
127-
"""Get the allowed local paths for staging operations."""
128-
return self._staging_allowed_local_path
129-
130-
@property
131-
def ssl_options(self) -> SSLOptions:
132-
"""Get the SSL options for this client."""
133-
return self._ssl_options
100+
warehouse_pattern = re.compile(r".*/warehouses/(.+)")
101+
endpoint_pattern = re.compile(r".*/endpoints/(.+)")
102+
103+
for pattern in [warehouse_pattern, endpoint_pattern]:
104+
match = pattern.match(http_path)
105+
if match:
106+
warehouse_id = match.group(1)
107+
logger.debug(
108+
f"Extracted warehouse ID: {warehouse_id} from path: {http_path}"
109+
)
110+
return warehouse_id
111+
112+
# If no match found, raise error
113+
error_message = (
114+
f"Could not extract warehouse ID from http_path: {http_path}. "
115+
f"Expected format: /path/to/warehouses/{{warehouse_id}} or "
116+
f"/path/to/endpoints/{{warehouse_id}}"
117+
)
118+
logger.error(error_message)
119+
raise ValueError(error_message)
134120

135121
@property
136122
def max_download_threads(self) -> int:
@@ -236,21 +222,21 @@ def execute_command(
236222
enforce_embedded_schema_correctness: bool,
237223
):
238224
"""Not implemented yet."""
239-
raise NotSupportedError(
225+
raise NotImplementedError(
240226
"execute_command is not yet implemented for SEA backend"
241227
)
242228

243229
def cancel_command(self, command_id: CommandId) -> None:
244230
"""Not implemented yet."""
245-
raise NotSupportedError("cancel_command is not yet implemented for SEA backend")
231+
raise NotImplementedError("cancel_command is not yet implemented for SEA backend")
246232

247233
def close_command(self, command_id: CommandId) -> None:
248234
"""Not implemented yet."""
249-
raise NotSupportedError("close_command is not yet implemented for SEA backend")
235+
raise NotImplementedError("close_command is not yet implemented for SEA backend")
250236

251237
def get_query_state(self, command_id: CommandId) -> CommandState:
252238
"""Not implemented yet."""
253-
raise NotSupportedError(
239+
raise NotImplementedError(
254240
"get_query_state is not yet implemented for SEA backend"
255241
)
256242

@@ -260,7 +246,7 @@ def get_execution_result(
260246
cursor: "Cursor",
261247
):
262248
"""Not implemented yet."""
263-
raise NotSupportedError(
249+
raise NotImplementedError(
264250
"get_execution_result is not yet implemented for SEA backend"
265251
)
266252

@@ -274,7 +260,7 @@ def get_catalogs(
274260
cursor: "Cursor",
275261
):
276262
"""Not implemented yet."""
277-
raise NotSupportedError("get_catalogs is not yet implemented for SEA backend")
263+
raise NotImplementedError("get_catalogs is not yet implemented for SEA backend")
278264

279265
def get_schemas(
280266
self,
@@ -286,7 +272,7 @@ def get_schemas(
286272
schema_name: Optional[str] = None,
287273
):
288274
"""Not implemented yet."""
289-
raise NotSupportedError("get_schemas is not yet implemented for SEA backend")
275+
raise NotImplementedError("get_schemas is not yet implemented for SEA backend")
290276

291277
def get_tables(
292278
self,
@@ -300,7 +286,7 @@ def get_tables(
300286
table_types: Optional[List[str]] = None,
301287
):
302288
"""Not implemented yet."""
303-
raise NotSupportedError("get_tables is not yet implemented for SEA backend")
289+
raise NotImplementedError("get_tables is not yet implemented for SEA backend")
304290

305291
def get_columns(
306292
self,
@@ -314,4 +300,4 @@ def get_columns(
314300
column_name: Optional[str] = None,
315301
):
316302
"""Not implemented yet."""
317-
raise NotSupportedError("get_columns is not yet implemented for SEA backend")
303+
raise NotImplementedError("get_columns is not yet implemented for SEA backend")

0 commit comments

Comments
 (0)