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

Function execute_chooser

examples/sharding/separate_databases.py:153–181  ·  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

151
152
153def execute_chooser(context):
154 """statement execution chooser.
155
156 this also returns a list of shard ids, which can just be all of them. but
157 here we'll search into the execution context in order to try to narrow down
158 the list of shards to SELECT.
159
160 """
161 ids = []
162
163 # we'll grab continent names as we find them
164 # and convert to shard ids
165 for column, operator, value in _get_select_comparisons(context.statement):
166 # "shares_lineage()" returns True if both columns refer to the same
167 # statement column, adjusting for any annotations present.
168 # (an annotation is an internal clone of a Column object
169 # and occur when using ORM-mapped attributes like
170 # "WeatherLocation.continent"). A simpler comparison, though less
171 # accurate, would be "column.key == 'continent'".
172 if column.shares_lineage(WeatherLocation.__table__.c.continent):
173 if operator == operators.eq:
174 ids.append(shard_lookup[value])
175 elif operator == operators.in_op:
176 ids.extend(shard_lookup[v] for v in value)
177
178 if len(ids) == 0:
179 return ["north_america", "asia", "europe", "south_america"]
180 else:
181 return ids
182
183
184def _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