Skip to content

Commit cb9d051

Browse files
committed
Enhance Dict Resultset
1 parent d567610 commit cb9d051

3 files changed

Lines changed: 47 additions & 15 deletions

File tree

pydynamodb/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
if TYPE_CHECKING:
77
from .connection import Connection
88

9-
__version__: str = "0.7.6"
9+
__version__: str = "0.8.0"
1010

1111
# Globals https://www.python.org/dev/peps/pep-0249/#globals
1212
apilevel: str = "2.0"

pydynamodb/executor.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,27 +309,33 @@ def __init__(
309309
**kwargs,
310310
)
311311

312-
def process_rows(self, response: Dict[str, Any]) -> None:
313-
rows = response.get("Items", None)
314-
if rows is None:
315-
raise DataError("KeyError `Items`")
316-
317-
processed_rows = list()
318-
for row in rows:
319-
row_ = self._process_row_item(row)
320-
processed_rows.append(row_)
321-
322-
self._rows.extend(processed_rows)
323-
self._next_token = response.get("NextToken", None)
324-
325-
def _process_row_item(self, row) -> Optional[Dict[str, Any]]:
312+
def _process_undef_row_item(self, row) -> Optional[Dict[str, Any]]:
326313
row_ = dict()
327314
for col, val in row.items():
328315
val_ = self._converter.deserialize(val)
329316
row_[col] = val_
330317

331318
return row_
332319

320+
def _process_predef_row_item(self, row) -> Optional[Dict[str, Any]]:
321+
row_ = dict()
322+
for col, val in row.items():
323+
col_info = self.metadata.get(col, None)
324+
if col_info:
325+
if col_info.function:
326+
val_ = self._converter.deserialize(
327+
val,
328+
function=col_info.function.name,
329+
function_params=col_info.function.params,
330+
)
331+
else:
332+
val_ = self._converter.deserialize(val)
333+
334+
col_name_ = col if col_info.alias is None else col_info.alias
335+
row_[col_name_] = val_
336+
337+
return row_
338+
333339

334340
class DmlBatchExecutor(DmlStatementExecutor):
335341
def __init__(

tests/test_cursor_dml_dict_rs.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def test_select(self, dict_cursor):
5959
)
6060
ret = dict_cursor.fetchall()
6161
assert len(ret) == 2
62+
assert ret[1]["col_nested_map"] == {
63+
"name": "test case 3",
64+
"version": 1.0,
65+
"list": ["Hello", "World", {1, 2, 3}, {"1", "2"}, 2],
66+
"map": {"str": "Best", "num": 1, "chinese": "你好"},
67+
}
6268

6369
dict_cursor.execute(
6470
"SELECT col_ss, col_nested_list[4] FROM %s WHERE key_partition='row_1'"
@@ -68,3 +74,23 @@ def test_select(self, dict_cursor):
6874
assert len(ret) == 2
6975
assert ret[0] == {"col_ss": {"A", "B", "C"}}
7076
assert ret[1] == {"col_nested_list[4]": {1, 2, 3}}
77+
78+
def test_select_with_alias(self, dict_cursor):
79+
dict_cursor.execute(
80+
"SELECT col_ns a, col_nested_map.version b FROM %s WHERE key_partition='row_1'"
81+
% TESTCASE05_TABLE
82+
)
83+
ret = dict_cursor.fetchall()
84+
assert len(ret) == 2
85+
assert ret[0] == {"a": {1, 2, 3.3, 4.0}}
86+
assert ret[1] == {"b": 1.0}
87+
88+
def test_select_with_function(self, dict_cursor):
89+
dict_cursor.execute(
90+
"SELECT SUBSTR(col_str, 0, 4) str, UPPER(col_nested_map.name) name FROM %s WHERE key_partition='row_1'"
91+
% TESTCASE05_TABLE
92+
)
93+
ret = dict_cursor.fetchall()
94+
assert len(ret) == 2
95+
assert ret[0] == {"str": "test"}
96+
assert ret[1] == {"name": "TEST CASE 3"}

0 commit comments

Comments
 (0)