2020from databricks .sql .utils import (
2121ColumnTable ,
2222ColumnQueue ,
23+ concat_table_chunks ,
2324)
2425from databricks .sql .backend .types import CommandId ,CommandState ,ExecuteResponse
2526from databricks .sql .telemetry .models .event import StatementType
@@ -296,23 +297,6 @@ def _convert_columnar_table(self, table):
296297
297298return result
298299
299- def merge_columnar (self ,result1 ,result2 )-> "ColumnTable" :
300- """
301- Function to merge / combining the columnar results into a single result
302- :param result1:
303- :param result2:
304- :return:
305- """
306-
307- if result1 .column_names != result2 .column_names :
308- raise ValueError ("The columns in the results don't match" )
309-
310- merged_result = [
311- result1 .column_table [i ]+ result2 .column_table [i ]
312- for i in range (result1 .num_columns )
313- ]
314- return ColumnTable (merged_result ,result1 .column_names )
315-
316300def fetchmany_arrow (self ,size :int )-> "pyarrow.Table" :
317301"""
318302 Fetch the next set of rows of a query result, returning a PyArrow table.
@@ -337,7 +321,7 @@ def fetchmany_arrow(self, size: int) -> "pyarrow.Table":
337321n_remaining_rows -= partial_results .num_rows
338322self ._next_row_index += partial_results .num_rows
339323
340- return pyarrow . concat_tables (partial_result_chunks , use_threads = True )
324+ return concat_table_chunks (partial_result_chunks )
341325
342326def fetchmany_columnar (self ,size :int ):
343327"""
@@ -350,19 +334,19 @@ def fetchmany_columnar(self, size: int):
350334results = self .results .next_n_rows (size )
351335n_remaining_rows = size - results .num_rows
352336self ._next_row_index += results .num_rows
353-
337+ partial_result_chunks = [ results ]
354338while (
355339n_remaining_rows > 0
356340and not self .has_been_closed_server_side
357341and self .has_more_rows
358342 ):
359343self ._fill_results_buffer ()
360344partial_results = self .results .next_n_rows (n_remaining_rows )
361- results = self . merge_columnar ( results , partial_results )
345+ partial_result_chunks . append ( partial_results )
362346n_remaining_rows -= partial_results .num_rows
363347self ._next_row_index += partial_results .num_rows
364348
365- return results
349+ return concat_table_chunks ( partial_result_chunks )
366350
367351def fetchall_arrow (self )-> "pyarrow.Table" :
368352"""Fetch all (remaining) rows of a query result, returning them as a PyArrow table."""
@@ -372,36 +356,34 @@ def fetchall_arrow(self) -> "pyarrow.Table":
372356while not self .has_been_closed_server_side and self .has_more_rows :
373357self ._fill_results_buffer ()
374358partial_results = self .results .remaining_rows ()
375- if isinstance (results ,ColumnTable )and isinstance (
376- partial_results ,ColumnTable
377- ):
378- results = self .merge_columnar (results ,partial_results )
379- else :
380- partial_result_chunks .append (partial_results )
359+ partial_result_chunks .append (partial_results )
381360self ._next_row_index += partial_results .num_rows
382361
362+ result_table = concat_table_chunks (partial_result_chunks )
383363# If PyArrow is installed and we have a ColumnTable result, convert it to PyArrow Table
384364# Valid only for metadata commands result set
385- if isinstance (results ,ColumnTable )and pyarrow :
365+ if isinstance (result_table ,ColumnTable )and pyarrow :
386366data = {
387367name :col
388- for name ,col in zip (results .column_names ,results .column_table )
368+ for name ,col in zip (
369+ result_table .column_names ,result_table .column_table
370+ )
389371 }
390372return pyarrow .Table .from_pydict (data )
391- return pyarrow . concat_tables ( partial_result_chunks , use_threads = True )
373+ return result_table
392374
393375def fetchall_columnar (self ):
394376"""Fetch all (remaining) rows of a query result, returning them as a Columnar table."""
395377results = self .results .remaining_rows ()
396378self ._next_row_index += results .num_rows
397-
379+ partial_result_chunks = [ results ]
398380while not self .has_been_closed_server_side and self .has_more_rows :
399381self ._fill_results_buffer ()
400382partial_results = self .results .remaining_rows ()
401- results = self . merge_columnar ( results , partial_results )
383+ partial_result_chunks . append ( partial_results )
402384self ._next_row_index += partial_results .num_rows
403385
404- return results
386+ return concat_table_chunks ( partial_result_chunks )
405387
406388def fetchone (self )-> Optional [Row ]:
407389"""