Skip to content

Commit 311f039

Browse files
Skip type hint path when no complex type columns exist
Check column metadata types against _COMPLEX_TYPES (array, map, row, struct) in _process_metadata. Only compute and store column type hints when the result set actually contains complex type columns with matching hints. This eliminates all hint-related overhead in the hot loop for queries that return only scalar types. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2ff410c commit 311f039

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

pyathena/result_set.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class AthenaResultSet(CursorIterator):
5353
https://docs.aws.amazon.com/athena/latest/APIReference/API_GetQueryResults.html
5454
"""
5555

56+
# https://docs.aws.amazon.com/athena/latest/ug/data-types.html
57+
# Athena complex types that benefit from type hint conversion.
58+
_COMPLEX_TYPES: frozenset[str] = frozenset({"array", "map", "row", "struct"})
59+
5660
def __init__(
5761
self,
5862
connection: Connection[Any],
@@ -425,10 +429,17 @@ def _process_metadata(self, response: dict[str, Any]) -> None:
425429
if column_info is None:
426430
raise DataError("KeyError `ColumnInfo`")
427431
self._metadata = tuple(column_info)
428-
if self._result_set_type_hints:
429-
self._column_type_hints = tuple(
430-
self._result_set_type_hints.get(m.get("Name", "").lower()) for m in self._metadata
432+
if self._result_set_type_hints and any(
433+
m.get("Type", "").lower() in self._COMPLEX_TYPES for m in self._metadata
434+
):
435+
hints = tuple(
436+
self._result_set_type_hints.get(m.get("Name", "").lower())
437+
if m.get("Type", "").lower() in self._COMPLEX_TYPES
438+
else None
439+
for m in self._metadata
431440
)
441+
if any(hints):
442+
self._column_type_hints = hints
432443

433444
def _process_update_count(self, response: dict[str, Any]) -> None:
434445
update_count = response.get("UpdateCount")

0 commit comments

Comments
 (0)