20
20
from databricks .sql .utils import (
21
21
ColumnTable ,
22
22
ColumnQueue ,
23
+ concat_table_chunks ,
23
24
)
24
25
from databricks .sql .backend .types import CommandId , CommandState , ExecuteResponse
25
26
from databricks .sql .telemetry .models .event import StatementType
@@ -296,23 +297,6 @@ def _convert_columnar_table(self, table):
296
297
297
298
return result
298
299
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
-
316
300
def fetchmany_arrow (self , size : int ) -> "pyarrow.Table" :
317
301
"""
318
302
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":
337
321
n_remaining_rows -= partial_results .num_rows
338
322
self ._next_row_index += partial_results .num_rows
339
323
340
- return pyarrow . concat_tables (partial_result_chunks , use_threads = True )
324
+ return concat_table_chunks (partial_result_chunks )
341
325
342
326
def fetchmany_columnar (self , size : int ):
343
327
"""
@@ -350,19 +334,19 @@ def fetchmany_columnar(self, size: int):
350
334
results = self .results .next_n_rows (size )
351
335
n_remaining_rows = size - results .num_rows
352
336
self ._next_row_index += results .num_rows
353
-
337
+ partial_result_chunks = [ results ]
354
338
while (
355
339
n_remaining_rows > 0
356
340
and not self .has_been_closed_server_side
357
341
and self .has_more_rows
358
342
):
359
343
self ._fill_results_buffer ()
360
344
partial_results = self .results .next_n_rows (n_remaining_rows )
361
- results = self . merge_columnar ( results , partial_results )
345
+ partial_result_chunks . append ( partial_results )
362
346
n_remaining_rows -= partial_results .num_rows
363
347
self ._next_row_index += partial_results .num_rows
364
348
365
- return results
349
+ return concat_table_chunks ( partial_result_chunks )
366
350
367
351
def fetchall_arrow (self ) -> "pyarrow.Table" :
368
352
"""Fetch all (remaining) rows of a query result, returning them as a PyArrow table."""
@@ -372,36 +356,34 @@ def fetchall_arrow(self) -> "pyarrow.Table":
372
356
while not self .has_been_closed_server_side and self .has_more_rows :
373
357
self ._fill_results_buffer ()
374
358
partial_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 )
381
360
self ._next_row_index += partial_results .num_rows
382
361
362
+ result_table = concat_table_chunks (partial_result_chunks )
383
363
# If PyArrow is installed and we have a ColumnTable result, convert it to PyArrow Table
384
364
# Valid only for metadata commands result set
385
- if isinstance (results , ColumnTable ) and pyarrow :
365
+ if isinstance (result_table , ColumnTable ) and pyarrow :
386
366
data = {
387
367
name : 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
+ )
389
371
}
390
372
return pyarrow .Table .from_pydict (data )
391
- return pyarrow . concat_tables ( partial_result_chunks , use_threads = True )
373
+ return result_table
392
374
393
375
def fetchall_columnar (self ):
394
376
"""Fetch all (remaining) rows of a query result, returning them as a Columnar table."""
395
377
results = self .results .remaining_rows ()
396
378
self ._next_row_index += results .num_rows
397
-
379
+ partial_result_chunks = [ results ]
398
380
while not self .has_been_closed_server_side and self .has_more_rows :
399
381
self ._fill_results_buffer ()
400
382
partial_results = self .results .remaining_rows ()
401
- results = self . merge_columnar ( results , partial_results )
383
+ partial_result_chunks . append ( partial_results )
402
384
self ._next_row_index += partial_results .num_rows
403
385
404
- return results
386
+ return concat_table_chunks ( partial_result_chunks )
405
387
406
388
def fetchone (self ) -> Optional [Row ]:
407
389
"""
0 commit comments