2020
2121from databricks .sql .types import Row
2222from databricks .sql .exc import RequestError , CursorAlreadyClosedError
23- from databricks .sql .utils import ExecuteResponse , ColumnTable , ColumnQueue
23+ from databricks .sql .utils import (
24+ ExecuteResponse ,
25+ ColumnTable ,
26+ ColumnQueue ,
27+ concat_table_chunks ,
28+ )
2429
2530logger = logging .getLogger (__name__ )
2631
@@ -251,23 +256,6 @@ def _convert_arrow_table(self, table):
251256 res = df .to_numpy (na_value = None , dtype = "object" )
252257 return [ResultRow (* v ) for v in res ]
253258
254- def merge_columnar (self , result1 , result2 ) -> "ColumnTable" :
255- """
256- Function to merge / combining the columnar results into a single result
257- :param result1:
258- :param result2:
259- :return:
260- """
261-
262- if result1 .column_names != result2 .column_names :
263- raise ValueError ("The columns in the results don't match" )
264-
265- merged_result = [
266- result1 .column_table [i ] + result2 .column_table [i ]
267- for i in range (result1 .num_columns )
268- ]
269- return ColumnTable (merged_result , result1 .column_names )
270-
271259 def fetchmany_arrow (self , size : int ) -> "pyarrow.Table" :
272260 """
273261 Fetch the next set of rows of a query result, returning a PyArrow table.
@@ -292,7 +280,7 @@ def fetchmany_arrow(self, size: int) -> "pyarrow.Table":
292280 n_remaining_rows -= partial_results .num_rows
293281 self ._next_row_index += partial_results .num_rows
294282
295- return pyarrow . concat_tables (partial_result_chunks , use_threads = True )
283+ return concat_table_chunks (partial_result_chunks )
296284
297285 def fetchmany_columnar (self , size : int ):
298286 """
@@ -305,19 +293,19 @@ def fetchmany_columnar(self, size: int):
305293 results = self .results .next_n_rows (size )
306294 n_remaining_rows = size - results .num_rows
307295 self ._next_row_index += results .num_rows
308-
296+ partial_result_chunks = [ results ]
309297 while (
310298 n_remaining_rows > 0
311299 and not self .has_been_closed_server_side
312300 and self .has_more_rows
313301 ):
314302 self ._fill_results_buffer ()
315303 partial_results = self .results .next_n_rows (n_remaining_rows )
316- results = self . merge_columnar ( results , partial_results )
304+ partial_result_chunks . append ( partial_results )
317305 n_remaining_rows -= partial_results .num_rows
318306 self ._next_row_index += partial_results .num_rows
319307
320- return results
308+ return concat_table_chunks ( partial_result_chunks )
321309
322310 def fetchall_arrow (self ) -> "pyarrow.Table" :
323311 """Fetch all (remaining) rows of a query result, returning them as a PyArrow table."""
@@ -327,36 +315,34 @@ def fetchall_arrow(self) -> "pyarrow.Table":
327315 while not self .has_been_closed_server_side and self .has_more_rows :
328316 self ._fill_results_buffer ()
329317 partial_results = self .results .remaining_rows ()
330- if isinstance (results , ColumnTable ) and isinstance (
331- partial_results , ColumnTable
332- ):
333- results = self .merge_columnar (results , partial_results )
334- else :
335- partial_result_chunks .append (partial_results )
318+ partial_result_chunks .append (partial_results )
336319 self ._next_row_index += partial_results .num_rows
337320
321+ result_table = concat_table_chunks (partial_result_chunks )
338322 # If PyArrow is installed and we have a ColumnTable result, convert it to PyArrow Table
339323 # Valid only for metadata commands result set
340- if isinstance (results , ColumnTable ) and pyarrow :
324+ if isinstance (result_table , ColumnTable ) and pyarrow :
341325 data = {
342326 name : col
343- for name , col in zip (results .column_names , results .column_table )
327+ for name , col in zip (
328+ result_table .column_names , result_table .column_table
329+ )
344330 }
345331 return pyarrow .Table .from_pydict (data )
346- return pyarrow . concat_tables ( partial_result_chunks , use_threads = True )
332+ return result_table
347333
348334 def fetchall_columnar (self ):
349335 """Fetch all (remaining) rows of a query result, returning them as a Columnar table."""
350336 results = self .results .remaining_rows ()
351337 self ._next_row_index += results .num_rows
352-
338+ partial_result_chunks = [ results ]
353339 while not self .has_been_closed_server_side and self .has_more_rows :
354340 self ._fill_results_buffer ()
355341 partial_results = self .results .remaining_rows ()
356- results = self . merge_columnar ( results , partial_results )
342+ partial_result_chunks . append ( partial_results )
357343 self ._next_row_index += partial_results .num_rows
358344
359- return results
345+ return concat_table_chunks ( partial_result_chunks )
360346
361347 def fetchone (self ) -> Optional [Row ]:
362348 """
0 commit comments