2424from databricks .sql .thrift_api .TCLIService import ttypes
2525from databricks .sql .types import Row
2626from databricks .sql .exc import Error , RequestError , CursorAlreadyClosedError
27- from databricks .sql .utils import (
28- ColumnTable ,
29- ColumnQueue ,
30- JsonQueue ,
31- SeaResultSetQueueFactory ,
32- )
27+ from databricks .sql .utils import ColumnTable , ColumnQueue , JsonQueue
3328from databricks .sql .backend .types import CommandId , CommandState , ExecuteResponse
3429
3530logger = logging .getLogger (__name__ )
@@ -480,7 +475,6 @@ def __init__(
480475 result_data ,
481476 manifest ,
482477 str (execute_response .command_id .to_sea_statement_id ()),
483- ssl_options = connection .session .ssl_options ,
484478 description = execute_response .description ,
485479 max_download_threads = sea_client .max_download_threads ,
486480 sea_client = sea_client ,
@@ -606,6 +600,43 @@ def fetchall_json(self) -> List:
606600
607601 return results
608602
603+ def _convert_complex_types_to_string (
604+ self , rows : "pyarrow.Table"
605+ ) -> "pyarrow.Table" :
606+ """
607+ Convert complex types (array, struct, map) to string representation.
608+
609+ Args:
610+ rows: Input PyArrow table
611+
612+ Returns:
613+ PyArrow table with complex types converted to strings
614+ """
615+
616+ if not pyarrow :
617+ return rows
618+
619+ def convert_complex_column_to_string (col : "pyarrow.Array" ) -> "pyarrow.Array" :
620+ python_values = col .to_pylist ()
621+ json_strings = [
622+ (None if val is None else json .dumps (val )) for val in python_values
623+ ]
624+ return pyarrow .array (json_strings , type = pyarrow .string ())
625+
626+ converted_columns = []
627+ for col in rows .columns :
628+ converted_col = col
629+ if (
630+ pyarrow .types .is_list (col .type )
631+ or pyarrow .types .is_large_list (col .type )
632+ or pyarrow .types .is_struct (col .type )
633+ or pyarrow .types .is_map (col .type )
634+ ):
635+ converted_col = convert_complex_column_to_string (col )
636+ converted_columns .append (converted_col )
637+
638+ return pyarrow .Table .from_arrays (converted_columns , names = rows .column_names )
639+
609640 def fetchmany_arrow (self , size : int ) -> "pyarrow.Table" :
610641 """
611642 Fetch the next set of rows as an Arrow table.
@@ -631,6 +662,9 @@ def fetchmany_arrow(self, size: int) -> "pyarrow.Table":
631662
632663 self ._next_row_index += results .num_rows
633664
665+ if not self .backend ._use_arrow_native_complex_types :
666+ results = self ._convert_complex_types_to_string (results )
667+
634668 return results
635669
636670 def fetchall_arrow (self ) -> "pyarrow.Table" :
@@ -645,6 +679,9 @@ def fetchall_arrow(self) -> "pyarrow.Table":
645679
646680 self ._next_row_index += results .num_rows
647681
682+ if not self .backend ._use_arrow_native_complex_types :
683+ results = self ._convert_complex_types_to_string (results )
684+
648685 return results
649686
650687 def fetchone (self ) -> Optional [Row ]:
0 commit comments