Skip to content

Commit e07f56c

Browse files
remove utility functions from backend interface, fix circular import
Signed-off-by: varun-edachali-dbx <[email protected]>
1 parent 9800636 commit e07f56c

File tree

4 files changed

+56
-104
lines changed

4 files changed

+56
-104
lines changed

src/databricks/sql/backend/databricks_client.py

Lines changed: 37 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"""
1212

1313
from abc import ABC, abstractmethod
14-
from typing import Dict, Tuple, List, Optional, Any, Union
14+
from typing import Dict, Tuple, List, Optional, Any, Union, TYPE_CHECKING
15+
16+
if TYPE_CHECKING:
17+
from databricks.sql.client import Cursor
1518

16-
from databricks.sql.client import Cursor
1719
from databricks.sql.thrift_api.TCLIService import ttypes
1820
from databricks.sql.backend.types import SessionId, CommandId
1921
from databricks.sql.utils import ExecuteResponse
@@ -76,7 +78,7 @@ def execute_command(
7678
max_rows: int,
7779
max_bytes: int,
7880
lz4_compression: bool,
79-
cursor: Cursor,
81+
cursor: "Cursor",
8082
use_cloud_fetch: bool,
8183
parameters: List[ttypes.TSparkParameter],
8284
async_op: bool,
@@ -174,7 +176,7 @@ def get_query_state(self, command_id: CommandId) -> ttypes.TOperationState:
174176
def get_execution_result(
175177
self,
176178
command_id: CommandId,
177-
cursor: Cursor,
179+
cursor: "Cursor",
178180
) -> ExecuteResponse:
179181
"""
180182
Retrieves the results of a previously executed command.
@@ -202,13 +204,13 @@ def get_catalogs(
202204
session_id: SessionId,
203205
max_rows: int,
204206
max_bytes: int,
205-
cursor: Cursor,
207+
cursor: "Cursor",
206208
) -> ExecuteResponse:
207209
"""
208210
Retrieves a list of available catalogs.
209211
210-
This method fetches metadata about the catalogs that are available
211-
in the current session.
212+
This method fetches metadata about all catalogs available in the current
213+
session's context.
212214
213215
Args:
214216
session_id: The session identifier
@@ -231,23 +233,23 @@ def get_schemas(
231233
session_id: SessionId,
232234
max_rows: int,
233235
max_bytes: int,
234-
cursor: Cursor,
236+
cursor: "Cursor",
235237
catalog_name: Optional[str] = None,
236238
schema_name: Optional[str] = None,
237239
) -> ExecuteResponse:
238240
"""
239-
Retrieves a list of available schemas.
241+
Retrieves a list of schemas, optionally filtered by catalog and schema name patterns.
240242
241-
This method fetches metadata about the schemas that are available
242-
in the specified catalog.
243+
This method fetches metadata about schemas available in the specified catalog
244+
or all catalogs if no catalog is specified.
243245
244246
Args:
245247
session_id: The session identifier
246248
max_rows: Maximum number of rows to fetch in a single batch
247249
max_bytes: Maximum number of bytes to fetch in a single batch
248250
cursor: The cursor object that will handle the results
249-
catalog_name: Optional catalog name to filter schemas
250-
schema_name: Optional schema pattern to filter schemas by name
251+
catalog_name: Optional catalog name pattern to filter by
252+
schema_name: Optional schema name pattern to filter by
251253
252254
Returns:
253255
ExecuteResponse: An object containing the schema metadata
@@ -264,27 +266,27 @@ def get_tables(
264266
session_id: SessionId,
265267
max_rows: int,
266268
max_bytes: int,
267-
cursor: Cursor,
269+
cursor: "Cursor",
268270
catalog_name: Optional[str] = None,
269271
schema_name: Optional[str] = None,
270272
table_name: Optional[str] = None,
271273
table_types: Optional[List[str]] = None,
272274
) -> ExecuteResponse:
273275
"""
274-
Retrieves a list of available tables.
276+
Retrieves a list of tables, optionally filtered by catalog, schema, table name, and table types.
275277
276-
This method fetches metadata about the tables that are available
277-
in the specified catalog and schema.
278+
This method fetches metadata about tables available in the specified catalog
279+
and schema, or all catalogs and schemas if not specified.
278280
279281
Args:
280282
session_id: The session identifier
281283
max_rows: Maximum number of rows to fetch in a single batch
282284
max_bytes: Maximum number of bytes to fetch in a single batch
283285
cursor: The cursor object that will handle the results
284-
catalog_name: Optional catalog name to filter tables
285-
schema_name: Optional schema name to filter tables
286-
table_name: Optional table pattern to filter tables by name
287-
table_types: Optional list of table types to include (e.g., "TABLE", "VIEW")
286+
catalog_name: Optional catalog name pattern to filter by
287+
schema_name: Optional schema name pattern to filter by
288+
table_name: Optional table name pattern to filter by
289+
table_types: Optional list of table types to filter by (e.g., ['TABLE', 'VIEW'])
288290
289291
Returns:
290292
ExecuteResponse: An object containing the table metadata
@@ -301,27 +303,27 @@ def get_columns(
301303
session_id: SessionId,
302304
max_rows: int,
303305
max_bytes: int,
304-
cursor: Cursor,
306+
cursor: "Cursor",
305307
catalog_name: Optional[str] = None,
306308
schema_name: Optional[str] = None,
307309
table_name: Optional[str] = None,
308310
column_name: Optional[str] = None,
309311
) -> ExecuteResponse:
310312
"""
311-
Retrieves column metadata for tables.
313+
Retrieves a list of columns, optionally filtered by catalog, schema, table, and column name patterns.
312314
313-
This method fetches metadata about the columns in the specified
314-
catalog, schema, and table.
315+
This method fetches metadata about columns available in the specified table,
316+
or all tables if not specified.
315317
316318
Args:
317319
session_id: The session identifier
318320
max_rows: Maximum number of rows to fetch in a single batch
319321
max_bytes: Maximum number of bytes to fetch in a single batch
320322
cursor: The cursor object that will handle the results
321-
catalog_name: Optional catalog name to filter columns
322-
schema_name: Optional schema name to filter columns
323-
table_name: Optional table name to filter columns
324-
column_name: Optional column pattern to filter columns by name
323+
catalog_name: Optional catalog name pattern to filter by
324+
schema_name: Optional schema name pattern to filter by
325+
table_name: Optional table name pattern to filter by
326+
column_name: Optional column name pattern to filter by
325327
326328
Returns:
327329
ExecuteResponse: An object containing the column metadata
@@ -332,68 +334,24 @@ def get_columns(
332334
"""
333335
pass
334336

335-
# == Utility Methods ==
336-
@abstractmethod
337-
def handle_to_id(self, session_id: SessionId) -> Any:
338-
"""
339-
Gets the raw session ID from a SessionId object.
340-
341-
This method extracts the underlying protocol-specific identifier
342-
from the SessionId abstraction.
343-
344-
Args:
345-
session_id: The session identifier
346-
347-
Returns:
348-
The raw session ID as used by the underlying protocol
349-
350-
Raises:
351-
ValueError: If the session ID is not valid for this client's backend type
352-
"""
353-
pass
354-
355-
@abstractmethod
356-
def handle_to_hex_id(self, session_id: SessionId) -> str:
357-
"""
358-
Gets a hexadecimal string representation of a session ID.
359-
360-
This method converts the session ID to a human-readable hexadecimal string
361-
that can be used for logging and debugging.
362-
363-
Args:
364-
session_id: The session identifier
365-
366-
Returns:
367-
str: A hexadecimal string representation of the session ID
368-
369-
Raises:
370-
ValueError: If the session ID is not valid for this client's backend type
371-
"""
372-
pass
373-
374-
# Properties related to specific backend features
337+
# == Properties ==
375338
@property
376339
@abstractmethod
377340
def staging_allowed_local_path(self) -> Union[None, str, List[str]]:
378341
"""
379-
Gets the local path(s) allowed for staging data.
380-
381-
This property returns the path or paths on the local filesystem that
382-
are allowed to be used for staging data when uploading to the server.
342+
Gets the allowed local paths for staging operations.
383343
384344
Returns:
385-
Union[None, str, List[str]]: The allowed local path(s) or None if staging is not allowed
345+
Union[None, str, List[str]]: The allowed local paths for staging operations,
346+
or None if staging is not allowed
386347
"""
387348
pass
388349

389350
@property
390351
@abstractmethod
391352
def ssl_options(self) -> SSLOptions:
392353
"""
393-
Gets the SSL options used by this client.
394-
395-
This property returns the SSL configuration options that are used
396-
for secure communication with the server.
354+
Gets the SSL options for this client.
397355
398356
Returns:
399357
SSLOptions: The SSL configuration options
@@ -404,10 +362,7 @@ def ssl_options(self) -> SSLOptions:
404362
@abstractmethod
405363
def max_download_threads(self) -> int:
406364
"""
407-
Gets the maximum number of threads for handling cloud fetch downloads.
408-
409-
This property returns the maximum number of concurrent threads that
410-
can be used for downloading result data when using cloud fetch.
365+
Gets the maximum number of download threads for cloud fetch operations.
411366
412367
Returns:
413368
int: The maximum number of download threads

src/databricks/sql/backend/thrift_backend.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import time
66
import uuid
77
import threading
8-
from typing import List, Union, Any
8+
from typing import List, Union, Any, TYPE_CHECKING
9+
10+
if TYPE_CHECKING:
11+
from databricks.sql.client import Cursor
912

10-
from databricks.sql.client import Cursor
1113
from databricks.sql.thrift_api.TCLIService.ttypes import TOperationState
1214
from databricks.sql.backend.types import (
1315
SessionId,
@@ -813,7 +815,7 @@ def _results_message_to_execute_response(self, resp, operation_state):
813815
arrow_schema_bytes=schema_bytes,
814816
)
815817

816-
def get_execution_result(self, command_id: CommandId, cursor):
818+
def get_execution_result(self, command_id: CommandId, cursor: "Cursor"):
817819
thrift_handle = command_id.to_thrift_handle()
818820
if not thrift_handle:
819821
raise ValueError("Not a valid Thrift command ID")
@@ -929,7 +931,7 @@ def execute_command(
929931
max_rows: int,
930932
max_bytes: int,
931933
lz4_compression: bool,
932-
cursor: Cursor,
934+
cursor: "Cursor",
933935
use_cloud_fetch=True,
934936
parameters=[],
935937
async_op=False,
@@ -987,7 +989,7 @@ def get_catalogs(
987989
session_id: SessionId,
988990
max_rows: int,
989991
max_bytes: int,
990-
cursor: Cursor,
992+
cursor: "Cursor",
991993
) -> ExecuteResponse:
992994
thrift_handle = session_id.to_thrift_handle()
993995
if not thrift_handle:
@@ -1007,7 +1009,7 @@ def get_schemas(
10071009
session_id: SessionId,
10081010
max_rows: int,
10091011
max_bytes: int,
1010-
cursor: Cursor,
1012+
cursor: "Cursor",
10111013
catalog_name=None,
10121014
schema_name=None,
10131015
) -> ExecuteResponse:
@@ -1031,7 +1033,7 @@ def get_tables(
10311033
session_id: SessionId,
10321034
max_rows: int,
10331035
max_bytes: int,
1034-
cursor: Cursor,
1036+
cursor: "Cursor",
10351037
catalog_name=None,
10361038
schema_name=None,
10371039
table_name=None,
@@ -1059,7 +1061,7 @@ def get_columns(
10591061
session_id: SessionId,
10601062
max_rows: int,
10611063
max_bytes: int,
1062-
cursor: Cursor,
1064+
cursor: "Cursor",
10631065
catalog_name=None,
10641066
schema_name=None,
10651067
table_name=None,
@@ -1168,14 +1170,3 @@ def close_command(self, command_id: CommandId):
11681170
resp = self.make_request(self._client.CloseOperation, req)
11691171
return resp.status
11701172

1171-
def handle_to_id(self, session_id: SessionId) -> Any:
1172-
"""Get the raw session ID from a SessionId"""
1173-
if session_id.backend_type != BackendType.THRIFT:
1174-
raise ValueError("Not a valid Thrift session ID")
1175-
return session_id.guid
1176-
1177-
def handle_to_hex_id(self, session_id: SessionId) -> str:
1178-
"""Get the hex representation of a session ID"""
1179-
if session_id.backend_type != BackendType.THRIFT:
1180-
raise ValueError("Not a valid Thrift session ID")
1181-
return guid_to_hex_id(session_id.guid)

src/databricks/sql/backend/types.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ def to_sea_session_id(self):
144144

145145
return self.guid
146146

147-
def to_hex_id(self) -> str:
147+
def get_id(self) -> Any:
148+
"""
149+
Get the ID of the session.
150+
"""
151+
return self.guid
152+
153+
def get_hex_id(self) -> str:
148154
"""
149155
Get a hexadecimal string representation of the session ID.
150156

src/databricks/sql/session.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ def get_session_id(self) -> SessionId:
116116

117117
def get_id(self):
118118
"""Get the raw session ID (backend-specific)"""
119-
return self.backend.handle_to_id(self._session_id)
119+
return self._session_id.get_id()
120120

121121
def get_id_hex(self) -> str:
122122
"""Get the session ID in hex format"""
123-
return self.backend.handle_to_hex_id(self._session_id)
123+
return self._session_id.get_hex_id()
124124

125125
def close(self) -> None:
126126
"""Close the underlying session."""

0 commit comments

Comments
 (0)