Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory efficiency of process_results by iterating. #217

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20240516-105757.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Improve memory efficiency of process_results()
time: 2024-05-16T10:57:57.480672-04:00
custom:
Author: peterallenwebb
Issue: "217"
15 changes: 6 additions & 9 deletions dbt/adapters/sql/connections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import abc
import time
from typing import Any, Dict, Iterable, List, Optional, Tuple, TYPE_CHECKING
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, TYPE_CHECKING

from dbt_common.events.contextvars import get_node_info
from dbt_common.events.functions import fire_event
Expand Down Expand Up @@ -112,27 +112,24 @@ def get_response(cls, cursor: Any) -> AdapterResponse:
@classmethod
def process_results(
cls, column_names: Iterable[str], rows: Iterable[Any]
) -> List[Dict[str, Any]]:
# TODO CT-211
) -> Iterator[Dict[str, Any]]:
unique_col_names = dict() # type: ignore[var-annotated]
# TODO CT-211
for idx in range(len(column_names)): # type: ignore[arg-type]
# TODO CT-211
col_name = column_names[idx] # type: ignore[index]
if col_name in unique_col_names:
unique_col_names[col_name] += 1
# TODO CT-211
column_names[idx] = f"{col_name}_{unique_col_names[col_name]}" # type: ignore[index] # noqa
else:
# TODO CT-211
unique_col_names[column_names[idx]] = 1 # type: ignore[index]
return [dict(zip(column_names, row)) for row in rows]

for row in rows:
yield dict(zip(column_names, row))

@classmethod
def get_result_from_cursor(cls, cursor: Any, limit: Optional[int]) -> "agate.Table":
from dbt_common.clients.agate_helper import table_from_data_flat

data: List[Any] = []
data: Iterable[Any] = []
column_names: List[str] = []

if cursor.description is not None:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_sql_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def test_duplicated_columns(self):
cols_with_one_dupe = ["a", "b", "a", "d"]
rows = [(1, 2, 3, 4)]
self.assertEqual(
SQLConnectionManager.process_results(cols_with_one_dupe, rows),
list(SQLConnectionManager.process_results(cols_with_one_dupe, rows)),
[{"a": 1, "b": 2, "a_2": 3, "d": 4}],
)

cols_with_more_dupes = ["a", "a", "a", "b"]
rows = [(1, 2, 3, 4)]
self.assertEqual(
SQLConnectionManager.process_results(cols_with_more_dupes, rows),
list(SQLConnectionManager.process_results(cols_with_more_dupes, rows)),
[{"a": 1, "a_2": 2, "a_3": 3, "b": 4}],
)
Loading