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 the memory efficiency of process_results() override #1050

Merged
merged 3 commits into from
May 21, 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-20240517-143743.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Improve memory efficiency of the process_results() override.
time: 2024-05-17T14:37:43.7414-04:00
custom:
Author: peterallenwebb
Issue: "1053"
23 changes: 13 additions & 10 deletions dbt/adapters/snowflake/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dataclasses import dataclass
from io import StringIO
from time import sleep
from typing import Optional, Tuple, Union, Any, List
from typing import Any, List, Iterable, Optional, Tuple, Union

import agate
from dbt_common.clients.agate_helper import empty_table
Expand Down Expand Up @@ -443,25 +443,28 @@ def _split_queries(cls, sql):
split_query = snowflake.connector.util_text.split_statements(sql_buf)
return [part[0] for part in split_query]

@classmethod
def process_results(cls, column_names, rows):
# Override for Snowflake. The datetime objects returned by
# snowflake-connector-python are not pickleable, so we need
# to replace them with sane timezones
fixed = []
@staticmethod
def _fix_rows(rows: Iterable[Iterable]) -> Iterable[Iterable]:
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
# See note in process_results().
for row in rows:
fixed_row = []
for col in row:
if isinstance(col, datetime.datetime) and col.tzinfo:
offset = col.utcoffset()
assert offset is not None
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
offset_seconds = offset.total_seconds()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: offset_seconds = offset.total_seconds() or 0

new_timezone = pytz.FixedOffset(offset_seconds // 60)
new_timezone = pytz.FixedOffset(int(offset_seconds // 60))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytz is no longer required as of 3.9+, let's try to remove the dependency here?

https://github.com/stub42/pytz/blob/master/src/README.rst#introduction

col = col.astimezone(tz=new_timezone)
fixed_row.append(col)

fixed.append(fixed_row)
yield fixed_row

return super().process_results(column_names, fixed)
@classmethod
def process_results(cls, column_names, rows):
mikealfare marked this conversation as resolved.
Show resolved Hide resolved
# Override for Snowflake. The datetime objects returned by
# snowflake-connector-python are not pickleable, so we need
# to replace them with sane timezones.
return super().process_results(column_names, cls._fix_rows(rows))

def execute(
self, sql: str, auto_begin: bool = False, fetch: bool = False, limit: Optional[int] = None
Expand Down
Loading