55import time
66import pandas
77
8+ from databricks .sql .backend .sea .backend import SeaDatabricksClient
9+
810try :
911 import pyarrow
1012except ImportError :
1315if TYPE_CHECKING :
1416 from databricks .sql .backend .thrift_backend import ThriftDatabricksClient
1517 from databricks .sql .client import Connection
18+ from databricks .sql .backend .databricks_client import DatabricksClient
1619from databricks .sql .thrift_api .TCLIService import ttypes
1720from databricks .sql .types import Row
1821from databricks .sql .exc import Error , RequestError , CursorAlreadyClosedError
@@ -31,21 +34,37 @@ class ResultSet(ABC):
3134
3235 def __init__ (
3336 self ,
34- connection ,
35- backend ,
37+ connection : "Connection" ,
38+ backend : "DatabricksClient" ,
3639 arraysize : int ,
3740 buffer_size_bytes : int ,
38- command_id = None ,
39- status = None ,
41+ command_id : CommandId ,
42+ status : CommandState ,
4043 has_been_closed_server_side : bool = False ,
4144 has_more_rows : bool = False ,
4245 results_queue = None ,
4346 description = None ,
4447 is_staging_operation : bool = False ,
4548 ):
46- """Initialize the base ResultSet with common properties."""
49+ """
50+ A ResultSet manages the results of a single command.
51+
52+ Args:
53+ connection: The parent connection
54+ backend: The backend client
55+ arraysize: The max number of rows to fetch at a time (PEP-249)
56+ buffer_size_bytes: The size (in bytes) of the internal buffer + max fetch
57+ command_id: The command ID
58+ status: The command status
59+ has_been_closed_server_side: Whether the command has been closed on the server
60+ has_more_rows: Whether the command has more rows
61+ results_queue: The results queue
62+ description: column description of the results
63+ is_staging_operation: Whether the command is a staging operation
64+ """
65+
4766 self .connection = connection
48- self .backend = backend # Store the backend client directly
67+ self .backend = backend
4968 self .arraysize = arraysize
5069 self .buffer_size_bytes = buffer_size_bytes
5170 self ._next_row_index = 0
@@ -240,7 +259,7 @@ def _convert_arrow_table(self, table):
240259 res = df .to_numpy (na_value = None , dtype = "object" )
241260 return [ResultRow (* v ) for v in res ]
242261
243- def merge_columnar (self , result1 , result2 ):
262+ def merge_columnar (self , result1 , result2 ) -> "ColumnTable" :
244263 """
245264 Function to merge / combining the columnar results into a single result
246265 :param result1:
@@ -387,12 +406,11 @@ class SeaResultSet(ResultSet):
387406
388407 def __init__ (
389408 self ,
390- connection ,
391- sea_client ,
409+ connection : "Connection" ,
410+ execute_response : "ExecuteResponse" ,
411+ sea_client : "SeaDatabricksClient" ,
392412 buffer_size_bytes : int = 104857600 ,
393413 arraysize : int = 10000 ,
394- execute_response = None ,
395- sea_response = None ,
396414 ):
397415 """
398416 Initialize a SeaResultSet with the response from a SEA query execution.
@@ -402,56 +420,21 @@ def __init__(
402420 sea_client: The SeaDatabricksClient instance for direct access
403421 buffer_size_bytes: Buffer size for fetching results
404422 arraysize: Default number of rows to fetch
405- execute_response: Response from the execute command (new style)
406- sea_response: Direct SEA response (legacy style)
423+ execute_response: Response from the execute command
407424 """
408- # Handle both initialization styles
409- if execute_response is not None :
410- # New style with ExecuteResponse
411- command_id = execute_response .command_id
412- status = execute_response .status
413- has_been_closed_server_side = execute_response .has_been_closed_server_side
414- has_more_rows = execute_response .has_more_rows
415- results_queue = execute_response .results_queue
416- description = execute_response .description
417- is_staging_operation = execute_response .is_staging_operation
418- self ._response = getattr (execute_response , "sea_response" , {})
419- self .statement_id = command_id .to_sea_statement_id () if command_id else None
420- elif sea_response is not None :
421- # Legacy style with direct sea_response
422- self ._response = sea_response
423- # Extract values from sea_response
424- command_id = CommandId .from_sea_statement_id (
425- sea_response .get ("statement_id" , "" )
426- )
427- self .statement_id = sea_response .get ("statement_id" , "" )
428-
429- # Extract status
430- status_data = sea_response .get ("status" , {})
431- status = CommandState .from_sea_state (status_data .get ("state" , "PENDING" ))
432-
433- # Set defaults for other fields
434- has_been_closed_server_side = False
435- has_more_rows = False
436- results_queue = None
437- description = None
438- is_staging_operation = False
439- else :
440- raise ValueError ("Either execute_response or sea_response must be provided" )
441425
442- # Call parent constructor with common attributes
443426 super ().__init__ (
444427 connection = connection ,
445428 backend = sea_client ,
446429 arraysize = arraysize ,
447430 buffer_size_bytes = buffer_size_bytes ,
448- command_id = command_id ,
449- status = status ,
450- has_been_closed_server_side = has_been_closed_server_side ,
451- has_more_rows = has_more_rows ,
452- results_queue = results_queue ,
453- description = description ,
454- is_staging_operation = is_staging_operation ,
431+ command_id = execute_response . command_id ,
432+ status = execute_response . status ,
433+ has_been_closed_server_side = execute_response . has_been_closed_server_side ,
434+ has_more_rows = execute_response . has_more_rows ,
435+ results_queue = execute_response . results_queue ,
436+ description = execute_response . description ,
437+ is_staging_operation = execute_response . is_staging_operation ,
455438 )
456439
457440 def _fill_results_buffer (self ):
0 commit comments