@@ -103,7 +103,8 @@ def _to_array(varchar_value: Optional[str]) -> Optional[List[Any]]:
103103 # Optimize: Try JSON parsing first (most reliable)
104104 try :
105105 result = json .loads (varchar_value )
106- return result if isinstance (result , list ) else None
106+ if isinstance (result , list ):
107+ return result
107108 except json .JSONDecodeError :
108109 # If JSON parsing fails, fall back to basic parsing for simple cases
109110 pass
@@ -113,11 +114,11 @@ def _to_array(varchar_value: Optional[str]) -> Optional[List[Any]]:
113114 return []
114115
115116 try :
116- # Simple array format: [1, 2, 3] or [a, b, c]
117- # For complex structures, return None to keep as string
118- if any (char in inner for char in "{}()[]" ):
119- # Contains complex structures, skip parsing
117+ # For nested arrays, too complex for basic parsing
118+ if "[" in inner :
119+ # Contains nested arrays - too complex for basic parsing
120120 return None
121+ # Try native parsing (including struct arrays)
121122 return _parse_array_native (inner )
122123 except Exception :
123124 return None
@@ -225,7 +226,7 @@ def _to_struct(varchar_value: Optional[str]) -> Optional[Dict[str, Any]]:
225226
226227
227228def _parse_array_native (inner : str ) -> Optional [List [Any ]]:
228- """Parse array native format: 1, 2, 3 or a, b, c .
229+ """Parse array native format: 1, 2, 3 or { a, b}, {c, d} .
229230
230231 Args:
231232 inner: Interior content of array without brackets.
@@ -235,15 +236,23 @@ def _parse_array_native(inner: str) -> Optional[List[Any]]:
235236 """
236237 result = []
237238
238- # Simple split by comma for basic cases
239- items = [ item . strip () for item in inner . split ( "," )]
239+ # Smart split by comma - respect brace groupings
240+ items = _split_array_items ( inner )
240241
241242 for item in items :
242243 if not item :
243244 continue
244245
245- # Skip items with special characters (safety check)
246- if any (char in item for char in '{}[]()="' ):
246+ # Handle struct (ROW) values in format {a, b, c} or {key=value, ...}
247+ if item .strip ().startswith ("{" ) and item .strip ().endswith ("}" ):
248+ # This is a struct value - parse it as a struct
249+ struct_value = _to_struct (item .strip ())
250+ if struct_value is not None :
251+ result .append (struct_value )
252+ continue
253+
254+ # Skip items with nested arrays or complex quoting (safety check)
255+ if any (char in item for char in '[]="' ):
247256 continue
248257
249258 # Convert item to appropriate type
@@ -253,6 +262,44 @@ def _parse_array_native(inner: str) -> Optional[List[Any]]:
253262 return result if result else None
254263
255264
265+ def _split_array_items (inner : str ) -> List [str ]:
266+ """Split array items by comma, respecting brace and bracket groupings.
267+
268+ Args:
269+ inner: Interior content of array without brackets.
270+
271+ Returns:
272+ List of item strings.
273+ """
274+ items = []
275+ current_item = ""
276+ brace_depth = 0
277+ bracket_depth = 0
278+
279+ for char in inner :
280+ if char == "{" :
281+ brace_depth += 1
282+ elif char == "}" :
283+ brace_depth -= 1
284+ elif char == "[" :
285+ bracket_depth += 1
286+ elif char == "]" :
287+ bracket_depth -= 1
288+ elif char == "," and brace_depth == 0 and bracket_depth == 0 :
289+ # Top-level comma - end current item
290+ items .append (current_item .strip ())
291+ current_item = ""
292+ continue
293+
294+ current_item += char
295+
296+ # Add the last item
297+ if current_item .strip ():
298+ items .append (current_item .strip ())
299+
300+ return items
301+
302+
256303def _parse_map_native (inner : str ) -> Optional [Dict [str , Any ]]:
257304 """Parse map native format: key1=value1, key2=value2.
258305
0 commit comments