1
+ """
2
+ Abstract client interface for interacting with Databricks SQL services.
3
+
4
+ Implementations of this class are responsible for:
5
+ - Managing connections to Databricks SQL services
6
+ - Handling authentication
7
+ - Executing SQL queries and commands
8
+ - Retrieving query results
9
+ - Fetching metadata about catalogs, schemas, tables, and columns
10
+ - Managing error handling and retries
11
+ """
12
+
1
13
from abc import ABC , abstractmethod
2
14
from typing import Dict , Tuple , List , Optional , Any , Union
3
15
@@ -16,10 +28,42 @@ def open_session(
16
28
catalog : Optional [str ],
17
29
schema : Optional [str ],
18
30
) -> SessionId :
31
+ """
32
+ Opens a new session with the Databricks SQL service.
33
+
34
+ This method establishes a new session with the server and returns a session
35
+ identifier that can be used for subsequent operations.
36
+
37
+ Args:
38
+ session_configuration: Optional dictionary of configuration parameters for the session
39
+ catalog: Optional catalog name to use as the initial catalog for the session
40
+ schema: Optional schema name to use as the initial schema for the session
41
+
42
+ Returns:
43
+ SessionId: A session identifier object that can be used for subsequent operations
44
+
45
+ Raises:
46
+ Error: If the session configuration is invalid
47
+ OperationalError: If there's an error establishing the session
48
+ InvalidServerResponseError: If the server response is invalid or unexpected
49
+ """
19
50
pass
20
51
21
52
@abstractmethod
22
53
def close_session (self , session_id : SessionId ) -> None :
54
+ """
55
+ Closes an existing session with the Databricks SQL service.
56
+
57
+ This method terminates the session identified by the given session ID and
58
+ releases any resources associated with it.
59
+
60
+ Args:
61
+ session_id: The session identifier returned by open_session()
62
+
63
+ Raises:
64
+ ValueError: If the session ID is invalid
65
+ OperationalError: If there's an error closing the session
66
+ """
23
67
pass
24
68
25
69
# == Query Execution, Command Management ==
@@ -37,18 +81,92 @@ def execute_command(
37
81
async_op : bool ,
38
82
enforce_embedded_schema_correctness : bool ,
39
83
) -> Any :
84
+ """
85
+ Executes a SQL command or query within the specified session.
86
+
87
+ This method sends a SQL command to the server for execution and handles
88
+ the response. It can operate in both synchronous and asynchronous modes.
89
+
90
+ Args:
91
+ operation: The SQL command or query to execute
92
+ session_id: The session identifier in which to execute the command
93
+ max_rows: Maximum number of rows to fetch in a single batch
94
+ max_bytes: Maximum number of bytes to fetch in a single batch
95
+ lz4_compression: Whether to use LZ4 compression for result data
96
+ cursor: The cursor object that will handle the results
97
+ use_cloud_fetch: Whether to use cloud fetch for retrieving large result sets
98
+ parameters: List of parameters to bind to the query
99
+ async_op: Whether to execute the command asynchronously
100
+ enforce_embedded_schema_correctness: Whether to enforce schema correctness
101
+
102
+ Returns:
103
+ If async_op is False, returns an ExecuteResponse object containing the
104
+ query results and metadata. If async_op is True, returns None and the
105
+ results must be fetched later using get_execution_result().
106
+
107
+ Raises:
108
+ ValueError: If the session ID is invalid
109
+ OperationalError: If there's an error executing the command
110
+ ServerOperationError: If the server encounters an error during execution
111
+ """
40
112
pass
41
113
42
114
@abstractmethod
43
115
def cancel_command (self , command_id : CommandId ) -> None :
116
+ """
117
+ Cancels a running command or query.
118
+
119
+ This method attempts to cancel a command that is currently being executed.
120
+ It can be called from a different thread than the one executing the command.
121
+
122
+ Args:
123
+ command_id: The command identifier to cancel
124
+
125
+ Raises:
126
+ ValueError: If the command ID is invalid
127
+ OperationalError: If there's an error canceling the command
128
+ """
44
129
pass
45
130
46
131
@abstractmethod
47
132
def close_command (self , command_id : CommandId ) -> ttypes .TStatus :
133
+ """
134
+ Closes a command and releases associated resources.
135
+
136
+ This method informs the server that the client is done with the command
137
+ and any resources associated with it can be released.
138
+
139
+ Args:
140
+ command_id: The command identifier to close
141
+
142
+ Returns:
143
+ ttypes.TStatus: The status of the close operation
144
+
145
+ Raises:
146
+ ValueError: If the command ID is invalid
147
+ OperationalError: If there's an error closing the command
148
+ """
48
149
pass
49
150
50
151
@abstractmethod
51
152
def get_query_state (self , command_id : CommandId ) -> ttypes .TOperationState :
153
+ """
154
+ Gets the current state of a query or command.
155
+
156
+ This method retrieves the current execution state of a command from the server.
157
+
158
+ Args:
159
+ command_id: The command identifier to check
160
+
161
+ Returns:
162
+ ttypes.TOperationState: The current state of the command
163
+
164
+ Raises:
165
+ ValueError: If the command ID is invalid
166
+ OperationalError: If there's an error retrieving the state
167
+ ServerOperationError: If the command is in an error state
168
+ DatabaseError: If the command has been closed unexpectedly
169
+ """
52
170
pass
53
171
54
172
@abstractmethod
@@ -57,6 +175,23 @@ def get_execution_result(
57
175
command_id : CommandId ,
58
176
cursor : Any ,
59
177
) -> ExecuteResponse :
178
+ """
179
+ Retrieves the results of a previously executed command.
180
+
181
+ This method fetches the results of a command that was executed asynchronously
182
+ or retrieves additional results from a command that has more rows available.
183
+
184
+ Args:
185
+ command_id: The command identifier for which to retrieve results
186
+ cursor: The cursor object that will handle the results
187
+
188
+ Returns:
189
+ ExecuteResponse: An object containing the query results and metadata
190
+
191
+ Raises:
192
+ ValueError: If the command ID is invalid
193
+ OperationalError: If there's an error retrieving the results
194
+ """
60
195
pass
61
196
62
197
# == Metadata Operations ==
@@ -68,6 +203,25 @@ def get_catalogs(
68
203
max_bytes : int ,
69
204
cursor : Any ,
70
205
) -> Any :
206
+ """
207
+ Retrieves a list of available catalogs.
208
+
209
+ This method fetches metadata about the catalogs that are available
210
+ in the current session.
211
+
212
+ Args:
213
+ session_id: The session identifier
214
+ max_rows: Maximum number of rows to fetch in a single batch
215
+ max_bytes: Maximum number of bytes to fetch in a single batch
216
+ cursor: The cursor object that will handle the results
217
+
218
+ Returns:
219
+ ExecuteResponse: An object containing the catalog metadata
220
+
221
+ Raises:
222
+ ValueError: If the session ID is invalid
223
+ OperationalError: If there's an error retrieving the catalogs
224
+ """
71
225
pass
72
226
73
227
@abstractmethod
@@ -80,6 +234,27 @@ def get_schemas(
80
234
catalog_name : Optional [str ] = None ,
81
235
schema_name : Optional [str ] = None ,
82
236
) -> Any :
237
+ """
238
+ Retrieves a list of available schemas.
239
+
240
+ This method fetches metadata about the schemas that are available
241
+ in the specified catalog.
242
+
243
+ Args:
244
+ session_id: The session identifier
245
+ max_rows: Maximum number of rows to fetch in a single batch
246
+ max_bytes: Maximum number of bytes to fetch in a single batch
247
+ cursor: The cursor object that will handle the results
248
+ catalog_name: Optional catalog name to filter schemas
249
+ schema_name: Optional schema pattern to filter schemas by name
250
+
251
+ Returns:
252
+ ExecuteResponse: An object containing the schema metadata
253
+
254
+ Raises:
255
+ ValueError: If the session ID is invalid
256
+ OperationalError: If there's an error retrieving the schemas
257
+ """
83
258
pass
84
259
85
260
@abstractmethod
@@ -94,6 +269,29 @@ def get_tables(
94
269
table_name : Optional [str ] = None ,
95
270
table_types : Optional [List [str ]] = None ,
96
271
) -> Any :
272
+ """
273
+ Retrieves a list of available tables.
274
+
275
+ This method fetches metadata about the tables that are available
276
+ in the specified catalog and schema.
277
+
278
+ Args:
279
+ session_id: The session identifier
280
+ max_rows: Maximum number of rows to fetch in a single batch
281
+ max_bytes: Maximum number of bytes to fetch in a single batch
282
+ cursor: The cursor object that will handle the results
283
+ catalog_name: Optional catalog name to filter tables
284
+ schema_name: Optional schema name to filter tables
285
+ table_name: Optional table pattern to filter tables by name
286
+ table_types: Optional list of table types to include (e.g., "TABLE", "VIEW")
287
+
288
+ Returns:
289
+ ExecuteResponse: An object containing the table metadata
290
+
291
+ Raises:
292
+ ValueError: If the session ID is invalid
293
+ OperationalError: If there's an error retrieving the tables
294
+ """
97
295
pass
98
296
99
297
@abstractmethod
@@ -108,29 +306,109 @@ def get_columns(
108
306
table_name : Optional [str ] = None ,
109
307
column_name : Optional [str ] = None ,
110
308
) -> Any :
309
+ """
310
+ Retrieves column metadata for tables.
311
+
312
+ This method fetches metadata about the columns in the specified
313
+ catalog, schema, and table.
314
+
315
+ Args:
316
+ session_id: The session identifier
317
+ max_rows: Maximum number of rows to fetch in a single batch
318
+ max_bytes: Maximum number of bytes to fetch in a single batch
319
+ cursor: The cursor object that will handle the results
320
+ catalog_name: Optional catalog name to filter columns
321
+ schema_name: Optional schema name to filter columns
322
+ table_name: Optional table name to filter columns
323
+ column_name: Optional column pattern to filter columns by name
324
+
325
+ Returns:
326
+ ExecuteResponse: An object containing the column metadata
327
+
328
+ Raises:
329
+ ValueError: If the session ID is invalid
330
+ OperationalError: If there's an error retrieving the columns
331
+ """
111
332
pass
112
333
113
334
# == Utility Methods ==
114
335
@abstractmethod
115
336
def handle_to_id (self , session_id : SessionId ) -> Any :
337
+ """
338
+ Gets the raw session ID from a SessionId object.
339
+
340
+ This method extracts the underlying protocol-specific identifier
341
+ from the SessionId abstraction.
342
+
343
+ Args:
344
+ session_id: The session identifier
345
+
346
+ Returns:
347
+ The raw session ID as used by the underlying protocol
348
+
349
+ Raises:
350
+ ValueError: If the session ID is not valid for this client's backend type
351
+ """
116
352
pass
117
353
118
354
@abstractmethod
119
355
def handle_to_hex_id (self , session_id : SessionId ) -> str :
356
+ """
357
+ Gets a hexadecimal string representation of a session ID.
358
+
359
+ This method converts the session ID to a human-readable hexadecimal string
360
+ that can be used for logging and debugging.
361
+
362
+ Args:
363
+ session_id: The session identifier
364
+
365
+ Returns:
366
+ str: A hexadecimal string representation of the session ID
367
+
368
+ Raises:
369
+ ValueError: If the session ID is not valid for this client's backend type
370
+ """
120
371
pass
121
372
122
373
# Properties related to specific backend features
123
374
@property
124
375
@abstractmethod
125
376
def staging_allowed_local_path (self ) -> Union [None , str , List [str ]]:
377
+ """
378
+ Gets the local path(s) allowed for staging data.
379
+
380
+ This property returns the path or paths on the local filesystem that
381
+ are allowed to be used for staging data when uploading to the server.
382
+
383
+ Returns:
384
+ Union[None, str, List[str]]: The allowed local path(s) or None if staging is not allowed
385
+ """
126
386
pass
127
387
128
388
@property
129
389
@abstractmethod
130
390
def ssl_options (self ) -> SSLOptions :
391
+ """
392
+ Gets the SSL options used by this client.
393
+
394
+ This property returns the SSL configuration options that are used
395
+ for secure communication with the server.
396
+
397
+ Returns:
398
+ SSLOptions: The SSL configuration options
399
+ """
131
400
pass
132
401
133
402
@property
134
403
@abstractmethod
135
404
def max_download_threads (self ) -> int :
136
- pass
405
+ """
406
+ Gets the maximum number of threads for handling cloud fetch downloads.
407
+
408
+ This property returns the maximum number of concurrent threads that
409
+ can be used for downloading result data when using cloud fetch.
410
+
411
+ Returns:
412
+ int: The maximum number of download threads
413
+ """
414
+ pass
0 commit comments