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

Function execute_chooser

examples/sharding/separate_tables.py:167–195  ·  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

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