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

feat: return list of dictionaries for execute streaming sql #1003

Merged
merged 8 commits into from
Oct 25, 2023
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
21 changes: 21 additions & 0 deletions google/cloud/spanner_v1/streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ def one_or_none(self):
except StopIteration:
return answer

def to_dict_list(self):
"""Return the result of a query as a list of dictionaries.
In each dictionary the key is the column name and the value is the
value of the that column in a given row.

:rtype:
:class:`list of dict`
:returns: result rows as a list of dictionaries
"""
rows = []
asthamohta marked this conversation as resolved.
Show resolved Hide resolved
for row in self:
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if rows are not fully processed when this method is called ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There will be a delay in returning the result in those cases but always the full result will be returned

rows.append(
{
column: value
for column, value in zip(
[column.name for column in self._metadata.row_type.fields], row
)
}
)
return rows


class Unmergeable(ValueError):
"""Unable to merge two values.
Expand Down
13 changes: 13 additions & 0 deletions tests/system/test_session_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,19 @@ def test_execute_sql_w_manual_consume(sessions_database):
assert streamed._pending_chunk is None


def test_execute_sql_w_to_dict_list(sessions_database):
sd = _sample_data
row_count = 40
_set_up_table(sessions_database, row_count)

with sessions_database.snapshot() as snapshot:
rows = snapshot.execute_sql(sd.SQL).to_dict_list()
all_data_rows = list(_row_data(row_count))
row_data = [list(row.values()) for row in rows]
sd._check_row_data(row_data, all_data_rows)
assert all(set(row.keys()) == set(sd.COLUMNS) for row in rows)


def _check_sql_results(
database,
sql,
Expand Down