MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / execute_chooser

Function execute_chooser

examples/sharding/asyncio.py:169–197  ·  view source on GitHub ↗

statement execution chooser. this also returns a list of shard ids, which can just be all of them. but here we'll search into the execution context in order to try to narrow down the list of shards to SELECT.

(context)

Source from the content-addressed store, hash-verified

167
168
169def execute_chooser(context):
170 """statement execution chooser.
171
172 this also returns a list of shard ids, which can just be all of them. but
173 here we'll search into the execution context in order to try to narrow down
174 the list of shards to SELECT.
175
176 """
177 ids = []
178
179 # we'll grab continent names as we find them
180 # and convert to shard ids
181 for column, operator, value in _get_select_comparisons(context.statement):
182 # "shares_lineage()" returns True if both columns refer to the same
183 # statement column, adjusting for any annotations present.
184 # (an annotation is an internal clone of a Column object
185 # and occur when using ORM-mapped attributes like
186 # "WeatherLocation.continent"). A simpler comparison, though less
187 # accurate, would be "column.key == 'continent'".
188 if column.shares_lineage(WeatherLocation.__table__.c.continent):
189 if operator == operators.eq:
190 ids.append(shard_lookup[value])
191 elif operator == operators.in_op:
192 ids.extend(shard_lookup[v] for v in value)
193
194 if len(ids) == 0:
195 return ["north_america", "asia", "europe", "south_america"]
196 else:
197 return ids
198
199
200def _get_select_comparisons(statement):

Callers

nothing calls this directly

Calls 4

shares_lineageMethod · 0.80
_get_select_comparisonsFunction · 0.70
appendMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected