refactor: add support for distinct in get_docs

This commit is contained in:
Shrihari Mahabal 2026-03-10 14:00:08 +05:30
parent c174881534
commit 7ff564c227

View file

@ -166,6 +166,7 @@ def get_docs(
order_by: str = "creation asc", order_by: str = "creation asc",
as_generator: bool = False, as_generator: bool = False,
for_update: bool = False, for_update: bool = False,
distinct: bool = False,
) -> list["Document"] | Generator[list["Document"]]: ) -> list["Document"] | Generator[list["Document"]]:
"""Fetch fully instantiated Document objects from the database. """Fetch fully instantiated Document objects from the database.
@ -180,6 +181,7 @@ def get_docs(
:param order_by: Order By string, e.g. `creation desc`. :param order_by: Order By string, e.g. `creation desc`.
:param as_generator: If True, returns a generator yielding lists of Documents. :param as_generator: If True, returns a generator yielding lists of Documents.
:param for_update: If True, locks the fetched rows for update. :param for_update: If True, locks the fetched rows for update.
:param distinct: If True, return distinct rows.
""" """
if is_virtual_doctype(doctype): if is_virtual_doctype(doctype):
frappe.throw(_("Virtual DocType {0} cannot be fetched in bulk.").format(doctype)) frappe.throw(_("Virtual DocType {0} cannot be fetched in bulk.").format(doctype))
@ -210,6 +212,7 @@ def get_docs(
order_by=order_by, order_by=order_by,
lock_rows=lock_rows, lock_rows=lock_rows,
for_update=for_update, for_update=for_update,
distinct=distinct,
) )
# Eagerly fetch all docs # Eagerly fetch all docs
@ -221,6 +224,7 @@ def get_docs(
offset=limit_start, offset=limit_start,
lock_rows=lock_rows, lock_rows=lock_rows,
child_tables=child_tables, child_tables=child_tables,
distinct=distinct,
) )
return _build_document_objects(controller, all_data, for_update) return _build_document_objects(controller, all_data, for_update)
@ -238,6 +242,7 @@ def _get_docs_generator(
order_by, order_by,
lock_rows, lock_rows,
for_update, for_update,
distinct,
) -> Generator[list["Document"]]: ) -> Generator[list["Document"]]:
fetched_count = 0 fetched_count = 0
current_offset = limit_start current_offset = limit_start
@ -258,6 +263,7 @@ def _get_docs_generator(
offset=current_offset, offset=current_offset,
lock_rows=lock_rows, lock_rows=lock_rows,
child_tables=child_tables, child_tables=child_tables,
distinct=distinct,
) )
if not chunk_data: if not chunk_data:
@ -270,7 +276,7 @@ def _get_docs_generator(
current_offset += len(chunk_data) current_offset += len(chunk_data)
def _fetch_rows(doctype, *, filters, order_by, limit, offset, lock_rows, child_tables): def _fetch_rows(doctype, *, filters, order_by, limit, offset, lock_rows, child_tables, distinct=False):
kwargs = {} kwargs = {}
if limit is not None: if limit is not None:
kwargs["limit"] = limit kwargs["limit"] = limit
@ -283,6 +289,7 @@ def _fetch_rows(doctype, *, filters, order_by, limit, offset, lock_rows, child_t
fields=["*"], fields=["*"],
order_by=order_by, order_by=order_by,
for_update=lock_rows, for_update=lock_rows,
distinct=distinct,
**kwargs, **kwargs,
).run(as_dict=True) ).run(as_dict=True)