From 74cefe1713020f30d9ef19ec07f6a59e56fc5047 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 21 Nov 2024 09:35:12 -0500 Subject: [PATCH] mibios/query: Fix iterate() over values_list w/o PKs and no cache. Added back code to remove PK from tuple row, needed in the no-cache case. --- mibios/query.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/mibios/query.py b/mibios/query.py index 95cf312d..af5752c2 100644 --- a/mibios/query.py +++ b/mibios/query.py @@ -474,7 +474,7 @@ class BaseIterable: Alternative iterable implementation for large table data export. To be used by our Queryset.iterate(). These iterators are to be used only - once. + once. Results are always ordered by primary key. """ DEFAULT_CHUNK_SIZE = 20000 @@ -497,7 +497,7 @@ def debug(self): def __iter__(self): # Attach the iterator as attribute to allow easier introspection. # Allow this to happen only once per object lifetime to reduce possible - # confusing. + # confusion. if self._it is None: self._it = self._iter() else: @@ -586,6 +586,7 @@ def __init__(self, queryset, chunk_size, cache): @staticmethod def _rm_pk(row): + """ helper to remove PK from a list row """ del row[0] # PK is always first elem if we have to remove it return row @@ -601,20 +602,21 @@ def _iter(self): while True: chunk = qs.filter(pk__gt=last_pk)[:chunk_size] - if cache: - # chunk is replaced, type is list now - chunk = cache.update_values_list(chunk) - # For non-empty chunk get last PK before they are removed. Must # also avoid negative indexing in no-cache case where chunk is # queryset, so calculate last row via length. if chunk_length := len(chunk): last_pk = chunk[chunk_length - 1][pk_pos] - if hide_pk: - # if we get here, then pk_pos is always 0 - # chunk = ((i[slice(1, None)] for i in chunk)) - chunk = ((rm_pk(row) for row in chunk)) + if cache: + # chunk is replaced, type is list now (was tuple) + chunk = cache.update_values_list(chunk) + if hide_pk: + # rm PK from list + chunk = ((rm_pk(row) for row in chunk)) + elif hide_pk: + # rm PK from tuple (get new tuple via slicing) + chunk = ((row[slice(1, None)] for row in chunk)) yield from chunk