MCPcopy Create free account
hub / github.com/getsentry/sentry / RangeQuerySetWrapper

Class RangeQuerySetWrapper

src/sentry/utils/query.py:109–283  ·  view source on GitHub ↗

Iterates through a queryset by chunking results by ``step`` and using GREATER THAN and LESS THAN queries on the primary key. Very efficient, but ORDER BY statements will not work.

Source from the content-addressed store, hash-verified

107
108
109class RangeQuerySetWrapper[V]:
110 """
111 Iterates through a queryset by chunking results by ``step`` and using GREATER THAN
112 and LESS THAN queries on the primary key.
113
114 Very efficient, but ORDER BY statements will not work.
115 """
116
117 def __init__[M: Model](
118 self,
119 queryset: QuerySet[M, V],
120 *,
121 step: int = 1000,
122 limit: int | None = None,
123 min_id: int | None = None,
124 order_by: str | Sequence[str] = "pk",
125 use_compound_keyset_pagination: bool = False,
126 callbacks: Sequence[Callable[[list[V]], None]] = (),
127 result_value_getter: Callable[[V], Any] | None = None,
128 override_unique_safety_check: bool = False,
129 query_timeout_retries: int | None = None,
130 retry_delay_seconds: float = 0.5,
131 ):
132 # Support for slicing
133 if queryset.query.low_mark == 0 and not (
134 queryset.query.order_by or queryset.query.extra_order_by
135 ):
136 if limit is None:
137 limit = queryset.query.high_mark
138 queryset.query.clear_limits()
139 else:
140 raise InvalidQuerySetError
141
142 self.limit = limit
143 self.desc = step < 0
144 self.step = min(limit, abs(step)) if limit else abs(step)
145 self.queryset = queryset
146 self.min_value = min_id
147 self.use_compound_keyset_pagination = use_compound_keyset_pagination
148 self.callbacks = callbacks
149 self.result_value_getter = result_value_getter
150 self.query_timeout_retries = query_timeout_retries
151 self.retry_delay_seconds = retry_delay_seconds
152
153 # Normalize order_by to a list, converting "pk" to "id" for raw SQL compatibility
154 if isinstance(order_by, str):
155 fields = [order_by]
156 else:
157 fields = list(order_by)
158 self.order_by_fields = ["id" if f == "pk" else f for f in fields]
159
160 if use_compound_keyset_pagination and min_id is not None:
161 raise InvalidQuerySetError("min_id is not supported with compound keyset pagination")
162
163 # Validate that the last order_by field is unique to prevent infinite loops
164 if not override_unique_safety_check:
165 last_field = self.order_by_fields[-1]
166 order_by_col = queryset.model._meta.get_field(last_field)

Calls

no outgoing calls