strategies.py 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141
  1. # orm/strategies.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """sqlalchemy.orm.interfaces.LoaderStrategy
  8. implementations, and related MapperOptions."""
  9. from __future__ import absolute_import
  10. import collections
  11. import itertools
  12. from . import attributes
  13. from . import exc as orm_exc
  14. from . import interfaces
  15. from . import loading
  16. from . import path_registry
  17. from . import properties
  18. from . import query
  19. from . import relationships
  20. from . import unitofwork
  21. from . import util as orm_util
  22. from .base import _DEFER_FOR_STATE
  23. from .base import _RAISE_FOR_STATE
  24. from .base import _SET_DEFERRED_EXPIRED
  25. from .context import _column_descriptions
  26. from .context import ORMCompileState
  27. from .context import ORMSelectCompileState
  28. from .context import QueryContext
  29. from .interfaces import LoaderStrategy
  30. from .interfaces import StrategizedProperty
  31. from .session import _state_session
  32. from .state import InstanceState
  33. from .util import _none_set
  34. from .util import aliased
  35. from .. import event
  36. from .. import exc as sa_exc
  37. from .. import inspect
  38. from .. import log
  39. from .. import sql
  40. from .. import util
  41. from ..sql import util as sql_util
  42. from ..sql import visitors
  43. from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  44. from ..sql.selectable import Select
  45. def _register_attribute(
  46. prop,
  47. mapper,
  48. useobject,
  49. compare_function=None,
  50. typecallable=None,
  51. callable_=None,
  52. proxy_property=None,
  53. active_history=False,
  54. impl_class=None,
  55. **kw
  56. ):
  57. listen_hooks = []
  58. uselist = useobject and prop.uselist
  59. if useobject and prop.single_parent:
  60. listen_hooks.append(single_parent_validator)
  61. if prop.key in prop.parent.validators:
  62. fn, opts = prop.parent.validators[prop.key]
  63. listen_hooks.append(
  64. lambda desc, prop: orm_util._validator_events(
  65. desc, prop.key, fn, **opts
  66. )
  67. )
  68. if useobject:
  69. listen_hooks.append(unitofwork.track_cascade_events)
  70. # need to assemble backref listeners
  71. # after the singleparentvalidator, mapper validator
  72. if useobject:
  73. backref = prop.back_populates
  74. if backref and prop._effective_sync_backref:
  75. listen_hooks.append(
  76. lambda desc, prop: attributes.backref_listeners(
  77. desc, backref, uselist
  78. )
  79. )
  80. # a single MapperProperty is shared down a class inheritance
  81. # hierarchy, so we set up attribute instrumentation and backref event
  82. # for each mapper down the hierarchy.
  83. # typically, "mapper" is the same as prop.parent, due to the way
  84. # the configure_mappers() process runs, however this is not strongly
  85. # enforced, and in the case of a second configure_mappers() run the
  86. # mapper here might not be prop.parent; also, a subclass mapper may
  87. # be called here before a superclass mapper. That is, can't depend
  88. # on mappers not already being set up so we have to check each one.
  89. for m in mapper.self_and_descendants:
  90. if prop is m._props.get(
  91. prop.key
  92. ) and not m.class_manager._attr_has_impl(prop.key):
  93. desc = attributes.register_attribute_impl(
  94. m.class_,
  95. prop.key,
  96. parent_token=prop,
  97. uselist=uselist,
  98. compare_function=compare_function,
  99. useobject=useobject,
  100. trackparent=useobject
  101. and (
  102. prop.single_parent
  103. or prop.direction is interfaces.ONETOMANY
  104. ),
  105. typecallable=typecallable,
  106. callable_=callable_,
  107. active_history=active_history,
  108. impl_class=impl_class,
  109. send_modified_events=not useobject or not prop.viewonly,
  110. doc=prop.doc,
  111. **kw
  112. )
  113. for hook in listen_hooks:
  114. hook(desc, prop)
  115. @properties.ColumnProperty.strategy_for(instrument=False, deferred=False)
  116. class UninstrumentedColumnLoader(LoaderStrategy):
  117. """Represent a non-instrumented MapperProperty.
  118. The polymorphic_on argument of mapper() often results in this,
  119. if the argument is against the with_polymorphic selectable.
  120. """
  121. __slots__ = ("columns",)
  122. def __init__(self, parent, strategy_key):
  123. super(UninstrumentedColumnLoader, self).__init__(parent, strategy_key)
  124. self.columns = self.parent_property.columns
  125. def setup_query(
  126. self,
  127. compile_state,
  128. query_entity,
  129. path,
  130. loadopt,
  131. adapter,
  132. column_collection=None,
  133. **kwargs
  134. ):
  135. for c in self.columns:
  136. if adapter:
  137. c = adapter.columns[c]
  138. compile_state._append_dedupe_col_collection(c, column_collection)
  139. def create_row_processor(
  140. self,
  141. context,
  142. query_entity,
  143. path,
  144. loadopt,
  145. mapper,
  146. result,
  147. adapter,
  148. populators,
  149. ):
  150. pass
  151. @log.class_logger
  152. @properties.ColumnProperty.strategy_for(instrument=True, deferred=False)
  153. class ColumnLoader(LoaderStrategy):
  154. """Provide loading behavior for a :class:`.ColumnProperty`."""
  155. __slots__ = "columns", "is_composite"
  156. def __init__(self, parent, strategy_key):
  157. super(ColumnLoader, self).__init__(parent, strategy_key)
  158. self.columns = self.parent_property.columns
  159. self.is_composite = hasattr(self.parent_property, "composite_class")
  160. def setup_query(
  161. self,
  162. compile_state,
  163. query_entity,
  164. path,
  165. loadopt,
  166. adapter,
  167. column_collection,
  168. memoized_populators,
  169. check_for_adapt=False,
  170. **kwargs
  171. ):
  172. for c in self.columns:
  173. if adapter:
  174. if check_for_adapt:
  175. c = adapter.adapt_check_present(c)
  176. if c is None:
  177. return
  178. else:
  179. c = adapter.columns[c]
  180. compile_state._append_dedupe_col_collection(c, column_collection)
  181. fetch = self.columns[0]
  182. if adapter:
  183. fetch = adapter.columns[fetch]
  184. memoized_populators[self.parent_property] = fetch
  185. def init_class_attribute(self, mapper):
  186. self.is_class_level = True
  187. coltype = self.columns[0].type
  188. # TODO: check all columns ? check for foreign key as well?
  189. active_history = (
  190. self.parent_property.active_history
  191. or self.columns[0].primary_key
  192. or (
  193. mapper.version_id_col is not None
  194. and mapper._columntoproperty.get(mapper.version_id_col, None)
  195. is self.parent_property
  196. )
  197. )
  198. _register_attribute(
  199. self.parent_property,
  200. mapper,
  201. useobject=False,
  202. compare_function=coltype.compare_values,
  203. active_history=active_history,
  204. )
  205. def create_row_processor(
  206. self,
  207. context,
  208. query_entity,
  209. path,
  210. loadopt,
  211. mapper,
  212. result,
  213. adapter,
  214. populators,
  215. ):
  216. # look through list of columns represented here
  217. # to see which, if any, is present in the row.
  218. for col in self.columns:
  219. if adapter:
  220. col = adapter.columns[col]
  221. getter = result._getter(col, False)
  222. if getter:
  223. populators["quick"].append((self.key, getter))
  224. break
  225. else:
  226. populators["expire"].append((self.key, True))
  227. @log.class_logger
  228. @properties.ColumnProperty.strategy_for(query_expression=True)
  229. class ExpressionColumnLoader(ColumnLoader):
  230. def __init__(self, parent, strategy_key):
  231. super(ExpressionColumnLoader, self).__init__(parent, strategy_key)
  232. # compare to the "default" expression that is mapped in
  233. # the column. If it's sql.null, we don't need to render
  234. # unless an expr is passed in the options.
  235. null = sql.null().label(None)
  236. self._have_default_expression = any(
  237. not c.compare(null) for c in self.parent_property.columns
  238. )
  239. def setup_query(
  240. self,
  241. compile_state,
  242. query_entity,
  243. path,
  244. loadopt,
  245. adapter,
  246. column_collection,
  247. memoized_populators,
  248. **kwargs
  249. ):
  250. columns = None
  251. if loadopt and "expression" in loadopt.local_opts:
  252. columns = [loadopt.local_opts["expression"]]
  253. elif self._have_default_expression:
  254. columns = self.parent_property.columns
  255. if columns is None:
  256. return
  257. for c in columns:
  258. if adapter:
  259. c = adapter.columns[c]
  260. compile_state._append_dedupe_col_collection(c, column_collection)
  261. fetch = columns[0]
  262. if adapter:
  263. fetch = adapter.columns[fetch]
  264. memoized_populators[self.parent_property] = fetch
  265. def create_row_processor(
  266. self,
  267. context,
  268. query_entity,
  269. path,
  270. loadopt,
  271. mapper,
  272. result,
  273. adapter,
  274. populators,
  275. ):
  276. # look through list of columns represented here
  277. # to see which, if any, is present in the row.
  278. if loadopt and "expression" in loadopt.local_opts:
  279. columns = [loadopt.local_opts["expression"]]
  280. for col in columns:
  281. if adapter:
  282. col = adapter.columns[col]
  283. getter = result._getter(col, False)
  284. if getter:
  285. populators["quick"].append((self.key, getter))
  286. break
  287. else:
  288. populators["expire"].append((self.key, True))
  289. def init_class_attribute(self, mapper):
  290. self.is_class_level = True
  291. _register_attribute(
  292. self.parent_property,
  293. mapper,
  294. useobject=False,
  295. compare_function=self.columns[0].type.compare_values,
  296. accepts_scalar_loader=False,
  297. )
  298. @log.class_logger
  299. @properties.ColumnProperty.strategy_for(deferred=True, instrument=True)
  300. @properties.ColumnProperty.strategy_for(
  301. deferred=True, instrument=True, raiseload=True
  302. )
  303. @properties.ColumnProperty.strategy_for(do_nothing=True)
  304. class DeferredColumnLoader(LoaderStrategy):
  305. """Provide loading behavior for a deferred :class:`.ColumnProperty`."""
  306. __slots__ = "columns", "group", "raiseload"
  307. def __init__(self, parent, strategy_key):
  308. super(DeferredColumnLoader, self).__init__(parent, strategy_key)
  309. if hasattr(self.parent_property, "composite_class"):
  310. raise NotImplementedError(
  311. "Deferred loading for composite " "types not implemented yet"
  312. )
  313. self.raiseload = self.strategy_opts.get("raiseload", False)
  314. self.columns = self.parent_property.columns
  315. self.group = self.parent_property.group
  316. def create_row_processor(
  317. self,
  318. context,
  319. query_entity,
  320. path,
  321. loadopt,
  322. mapper,
  323. result,
  324. adapter,
  325. populators,
  326. ):
  327. # for a DeferredColumnLoader, this method is only used during a
  328. # "row processor only" query; see test_deferred.py ->
  329. # tests with "rowproc_only" in their name. As of the 1.0 series,
  330. # loading._instance_processor doesn't use a "row processing" function
  331. # to populate columns, instead it uses data in the "populators"
  332. # dictionary. Normally, the DeferredColumnLoader.setup_query()
  333. # sets up that data in the "memoized_populators" dictionary
  334. # and "create_row_processor()" here is never invoked.
  335. if (
  336. context.refresh_state
  337. and context.query._compile_options._only_load_props
  338. and self.key in context.query._compile_options._only_load_props
  339. ):
  340. self.parent_property._get_strategy(
  341. (("deferred", False), ("instrument", True))
  342. ).create_row_processor(
  343. context,
  344. query_entity,
  345. path,
  346. loadopt,
  347. mapper,
  348. result,
  349. adapter,
  350. populators,
  351. )
  352. elif not self.is_class_level:
  353. if self.raiseload:
  354. set_deferred_for_local_state = (
  355. self.parent_property._raise_column_loader
  356. )
  357. else:
  358. set_deferred_for_local_state = (
  359. self.parent_property._deferred_column_loader
  360. )
  361. populators["new"].append((self.key, set_deferred_for_local_state))
  362. else:
  363. populators["expire"].append((self.key, False))
  364. def init_class_attribute(self, mapper):
  365. self.is_class_level = True
  366. _register_attribute(
  367. self.parent_property,
  368. mapper,
  369. useobject=False,
  370. compare_function=self.columns[0].type.compare_values,
  371. callable_=self._load_for_state,
  372. load_on_unexpire=False,
  373. )
  374. def setup_query(
  375. self,
  376. compile_state,
  377. query_entity,
  378. path,
  379. loadopt,
  380. adapter,
  381. column_collection,
  382. memoized_populators,
  383. only_load_props=None,
  384. **kw
  385. ):
  386. if (
  387. (
  388. compile_state.compile_options._render_for_subquery
  389. and self.parent_property._renders_in_subqueries
  390. )
  391. or (
  392. loadopt
  393. and "undefer_pks" in loadopt.local_opts
  394. and set(self.columns).intersection(
  395. self.parent._should_undefer_in_wildcard
  396. )
  397. )
  398. or (
  399. loadopt
  400. and self.group
  401. and loadopt.local_opts.get(
  402. "undefer_group_%s" % self.group, False
  403. )
  404. )
  405. or (only_load_props and self.key in only_load_props)
  406. ):
  407. self.parent_property._get_strategy(
  408. (("deferred", False), ("instrument", True))
  409. ).setup_query(
  410. compile_state,
  411. query_entity,
  412. path,
  413. loadopt,
  414. adapter,
  415. column_collection,
  416. memoized_populators,
  417. **kw
  418. )
  419. elif self.is_class_level:
  420. memoized_populators[self.parent_property] = _SET_DEFERRED_EXPIRED
  421. elif not self.raiseload:
  422. memoized_populators[self.parent_property] = _DEFER_FOR_STATE
  423. else:
  424. memoized_populators[self.parent_property] = _RAISE_FOR_STATE
  425. def _load_for_state(self, state, passive):
  426. if not state.key:
  427. return attributes.ATTR_EMPTY
  428. if not passive & attributes.SQL_OK:
  429. return attributes.PASSIVE_NO_RESULT
  430. localparent = state.manager.mapper
  431. if self.group:
  432. toload = [
  433. p.key
  434. for p in localparent.iterate_properties
  435. if isinstance(p, StrategizedProperty)
  436. and isinstance(p.strategy, DeferredColumnLoader)
  437. and p.group == self.group
  438. ]
  439. else:
  440. toload = [self.key]
  441. # narrow the keys down to just those which have no history
  442. group = [k for k in toload if k in state.unmodified]
  443. session = _state_session(state)
  444. if session is None:
  445. raise orm_exc.DetachedInstanceError(
  446. "Parent instance %s is not bound to a Session; "
  447. "deferred load operation of attribute '%s' cannot proceed"
  448. % (orm_util.state_str(state), self.key)
  449. )
  450. if self.raiseload:
  451. self._invoke_raise_load(state, passive, "raise")
  452. if (
  453. loading.load_on_ident(
  454. session,
  455. sql.select(localparent).set_label_style(
  456. LABEL_STYLE_TABLENAME_PLUS_COL
  457. ),
  458. state.key,
  459. only_load_props=group,
  460. refresh_state=state,
  461. )
  462. is None
  463. ):
  464. raise orm_exc.ObjectDeletedError(state)
  465. return attributes.ATTR_WAS_SET
  466. def _invoke_raise_load(self, state, passive, lazy):
  467. raise sa_exc.InvalidRequestError(
  468. "'%s' is not available due to raiseload=True" % (self,)
  469. )
  470. class LoadDeferredColumns(object):
  471. """serializable loader object used by DeferredColumnLoader"""
  472. def __init__(self, key, raiseload=False):
  473. self.key = key
  474. self.raiseload = raiseload
  475. def __call__(self, state, passive=attributes.PASSIVE_OFF):
  476. key = self.key
  477. localparent = state.manager.mapper
  478. prop = localparent._props[key]
  479. if self.raiseload:
  480. strategy_key = (
  481. ("deferred", True),
  482. ("instrument", True),
  483. ("raiseload", True),
  484. )
  485. else:
  486. strategy_key = (("deferred", True), ("instrument", True))
  487. strategy = prop._get_strategy(strategy_key)
  488. return strategy._load_for_state(state, passive)
  489. class AbstractRelationshipLoader(LoaderStrategy):
  490. """LoaderStratgies which deal with related objects."""
  491. __slots__ = "mapper", "target", "uselist", "entity"
  492. def __init__(self, parent, strategy_key):
  493. super(AbstractRelationshipLoader, self).__init__(parent, strategy_key)
  494. self.mapper = self.parent_property.mapper
  495. self.entity = self.parent_property.entity
  496. self.target = self.parent_property.target
  497. self.uselist = self.parent_property.uselist
  498. @log.class_logger
  499. @relationships.RelationshipProperty.strategy_for(do_nothing=True)
  500. class DoNothingLoader(LoaderStrategy):
  501. """Relationship loader that makes no change to the object's state.
  502. Compared to NoLoader, this loader does not initialize the
  503. collection/attribute to empty/none; the usual default LazyLoader will
  504. take effect.
  505. """
  506. @log.class_logger
  507. @relationships.RelationshipProperty.strategy_for(lazy="noload")
  508. @relationships.RelationshipProperty.strategy_for(lazy=None)
  509. class NoLoader(AbstractRelationshipLoader):
  510. """Provide loading behavior for a :class:`.RelationshipProperty`
  511. with "lazy=None".
  512. """
  513. __slots__ = ()
  514. def init_class_attribute(self, mapper):
  515. self.is_class_level = True
  516. _register_attribute(
  517. self.parent_property,
  518. mapper,
  519. useobject=True,
  520. typecallable=self.parent_property.collection_class,
  521. )
  522. def create_row_processor(
  523. self,
  524. context,
  525. query_entity,
  526. path,
  527. loadopt,
  528. mapper,
  529. result,
  530. adapter,
  531. populators,
  532. ):
  533. def invoke_no_load(state, dict_, row):
  534. if self.uselist:
  535. attributes.init_state_collection(state, dict_, self.key)
  536. else:
  537. dict_[self.key] = None
  538. populators["new"].append((self.key, invoke_no_load))
  539. @log.class_logger
  540. @relationships.RelationshipProperty.strategy_for(lazy=True)
  541. @relationships.RelationshipProperty.strategy_for(lazy="select")
  542. @relationships.RelationshipProperty.strategy_for(lazy="raise")
  543. @relationships.RelationshipProperty.strategy_for(lazy="raise_on_sql")
  544. @relationships.RelationshipProperty.strategy_for(lazy="baked_select")
  545. class LazyLoader(AbstractRelationshipLoader, util.MemoizedSlots):
  546. """Provide loading behavior for a :class:`.RelationshipProperty`
  547. with "lazy=True", that is loads when first accessed.
  548. """
  549. __slots__ = (
  550. "_lazywhere",
  551. "_rev_lazywhere",
  552. "_lazyload_reverse_option",
  553. "_order_by",
  554. "use_get",
  555. "is_aliased_class",
  556. "_bind_to_col",
  557. "_equated_columns",
  558. "_rev_bind_to_col",
  559. "_rev_equated_columns",
  560. "_simple_lazy_clause",
  561. "_raise_always",
  562. "_raise_on_sql",
  563. )
  564. def __init__(self, parent, strategy_key):
  565. super(LazyLoader, self).__init__(parent, strategy_key)
  566. self._raise_always = self.strategy_opts["lazy"] == "raise"
  567. self._raise_on_sql = self.strategy_opts["lazy"] == "raise_on_sql"
  568. self.is_aliased_class = inspect(self.entity).is_aliased_class
  569. join_condition = self.parent_property._join_condition
  570. (
  571. self._lazywhere,
  572. self._bind_to_col,
  573. self._equated_columns,
  574. ) = join_condition.create_lazy_clause()
  575. (
  576. self._rev_lazywhere,
  577. self._rev_bind_to_col,
  578. self._rev_equated_columns,
  579. ) = join_condition.create_lazy_clause(reverse_direction=True)
  580. if self.parent_property.order_by:
  581. self._order_by = [
  582. sql_util._deep_annotate(elem, {"_orm_adapt": True})
  583. for elem in util.to_list(self.parent_property.order_by)
  584. ]
  585. else:
  586. self._order_by = None
  587. self.logger.info("%s lazy loading clause %s", self, self._lazywhere)
  588. # determine if our "lazywhere" clause is the same as the mapper's
  589. # get() clause. then we can just use mapper.get()
  590. #
  591. # TODO: the "not self.uselist" can be taken out entirely; a m2o
  592. # load that populates for a list (very unusual, but is possible with
  593. # the API) can still set for "None" and the attribute system will
  594. # populate as an empty list.
  595. self.use_get = (
  596. not self.is_aliased_class
  597. and not self.uselist
  598. and self.entity._get_clause[0].compare(
  599. self._lazywhere,
  600. use_proxies=True,
  601. compare_keys=False,
  602. equivalents=self.mapper._equivalent_columns,
  603. )
  604. )
  605. if self.use_get:
  606. for col in list(self._equated_columns):
  607. if col in self.mapper._equivalent_columns:
  608. for c in self.mapper._equivalent_columns[col]:
  609. self._equated_columns[c] = self._equated_columns[col]
  610. self.logger.info(
  611. "%s will use Session.get() to " "optimize instance loads", self
  612. )
  613. def init_class_attribute(self, mapper):
  614. self.is_class_level = True
  615. _legacy_inactive_history_style = (
  616. self.parent_property._legacy_inactive_history_style
  617. )
  618. if self.parent_property.active_history:
  619. active_history = True
  620. _deferred_history = False
  621. elif (
  622. self.parent_property.direction is not interfaces.MANYTOONE
  623. or not self.use_get
  624. ):
  625. if _legacy_inactive_history_style:
  626. active_history = True
  627. _deferred_history = False
  628. else:
  629. active_history = False
  630. _deferred_history = True
  631. else:
  632. active_history = _deferred_history = False
  633. _register_attribute(
  634. self.parent_property,
  635. mapper,
  636. useobject=True,
  637. callable_=self._load_for_state,
  638. typecallable=self.parent_property.collection_class,
  639. active_history=active_history,
  640. _deferred_history=_deferred_history,
  641. )
  642. def _memoized_attr__simple_lazy_clause(self):
  643. lazywhere = sql_util._deep_annotate(
  644. self._lazywhere, {"_orm_adapt": True}
  645. )
  646. criterion, bind_to_col = (lazywhere, self._bind_to_col)
  647. params = []
  648. def visit_bindparam(bindparam):
  649. bindparam.unique = False
  650. visitors.traverse(criterion, {}, {"bindparam": visit_bindparam})
  651. def visit_bindparam(bindparam):
  652. if bindparam._identifying_key in bind_to_col:
  653. params.append(
  654. (
  655. bindparam.key,
  656. bind_to_col[bindparam._identifying_key],
  657. None,
  658. )
  659. )
  660. elif bindparam.callable is None:
  661. params.append((bindparam.key, None, bindparam.value))
  662. criterion = visitors.cloned_traverse(
  663. criterion, {}, {"bindparam": visit_bindparam}
  664. )
  665. return criterion, params
  666. def _generate_lazy_clause(self, state, passive):
  667. criterion, param_keys = self._simple_lazy_clause
  668. if state is None:
  669. return sql_util.adapt_criterion_to_null(
  670. criterion, [key for key, ident, value in param_keys]
  671. )
  672. mapper = self.parent_property.parent
  673. o = state.obj() # strong ref
  674. dict_ = attributes.instance_dict(o)
  675. if passive & attributes.INIT_OK:
  676. passive ^= attributes.INIT_OK
  677. params = {}
  678. for key, ident, value in param_keys:
  679. if ident is not None:
  680. if passive and passive & attributes.LOAD_AGAINST_COMMITTED:
  681. value = mapper._get_committed_state_attr_by_column(
  682. state, dict_, ident, passive
  683. )
  684. else:
  685. value = mapper._get_state_attr_by_column(
  686. state, dict_, ident, passive
  687. )
  688. params[key] = value
  689. return criterion, params
  690. def _invoke_raise_load(self, state, passive, lazy):
  691. raise sa_exc.InvalidRequestError(
  692. "'%s' is not available due to lazy='%s'" % (self, lazy)
  693. )
  694. def _load_for_state(self, state, passive, loadopt=None, extra_criteria=()):
  695. if not state.key and (
  696. (
  697. not self.parent_property.load_on_pending
  698. and not state._load_pending
  699. )
  700. or not state.session_id
  701. ):
  702. return attributes.ATTR_EMPTY
  703. pending = not state.key
  704. primary_key_identity = None
  705. use_get = self.use_get and (not loadopt or not loadopt._extra_criteria)
  706. if (not passive & attributes.SQL_OK and not use_get) or (
  707. not passive & attributes.NON_PERSISTENT_OK and pending
  708. ):
  709. return attributes.PASSIVE_NO_RESULT
  710. if (
  711. # we were given lazy="raise"
  712. self._raise_always
  713. # the no_raise history-related flag was not passed
  714. and not passive & attributes.NO_RAISE
  715. and (
  716. # if we are use_get and related_object_ok is disabled,
  717. # which means we are at most looking in the identity map
  718. # for history purposes or otherwise returning
  719. # PASSIVE_NO_RESULT, don't raise. This is also a
  720. # history-related flag
  721. not use_get
  722. or passive & attributes.RELATED_OBJECT_OK
  723. )
  724. ):
  725. self._invoke_raise_load(state, passive, "raise")
  726. session = _state_session(state)
  727. if not session:
  728. if passive & attributes.NO_RAISE:
  729. return attributes.PASSIVE_NO_RESULT
  730. raise orm_exc.DetachedInstanceError(
  731. "Parent instance %s is not bound to a Session; "
  732. "lazy load operation of attribute '%s' cannot proceed"
  733. % (orm_util.state_str(state), self.key)
  734. )
  735. # if we have a simple primary key load, check the
  736. # identity map without generating a Query at all
  737. if use_get:
  738. primary_key_identity = self._get_ident_for_use_get(
  739. session, state, passive
  740. )
  741. if attributes.PASSIVE_NO_RESULT in primary_key_identity:
  742. return attributes.PASSIVE_NO_RESULT
  743. elif attributes.NEVER_SET in primary_key_identity:
  744. return attributes.NEVER_SET
  745. if _none_set.issuperset(primary_key_identity):
  746. return None
  747. if (
  748. self.key in state.dict
  749. and not passive & attributes.DEFERRED_HISTORY_LOAD
  750. ):
  751. return attributes.ATTR_WAS_SET
  752. # look for this identity in the identity map. Delegate to the
  753. # Query class in use, as it may have special rules for how it
  754. # does this, including how it decides what the correct
  755. # identity_token would be for this identity.
  756. instance = session._identity_lookup(
  757. self.entity,
  758. primary_key_identity,
  759. passive=passive,
  760. lazy_loaded_from=state,
  761. )
  762. if instance is not None:
  763. if instance is attributes.PASSIVE_CLASS_MISMATCH:
  764. return None
  765. else:
  766. return instance
  767. elif (
  768. not passive & attributes.SQL_OK
  769. or not passive & attributes.RELATED_OBJECT_OK
  770. ):
  771. return attributes.PASSIVE_NO_RESULT
  772. return self._emit_lazyload(
  773. session,
  774. state,
  775. primary_key_identity,
  776. passive,
  777. loadopt,
  778. extra_criteria,
  779. )
  780. def _get_ident_for_use_get(self, session, state, passive):
  781. instance_mapper = state.manager.mapper
  782. if passive & attributes.LOAD_AGAINST_COMMITTED:
  783. get_attr = instance_mapper._get_committed_state_attr_by_column
  784. else:
  785. get_attr = instance_mapper._get_state_attr_by_column
  786. dict_ = state.dict
  787. return [
  788. get_attr(state, dict_, self._equated_columns[pk], passive=passive)
  789. for pk in self.mapper.primary_key
  790. ]
  791. @util.preload_module("sqlalchemy.orm.strategy_options")
  792. def _emit_lazyload(
  793. self,
  794. session,
  795. state,
  796. primary_key_identity,
  797. passive,
  798. loadopt,
  799. extra_criteria,
  800. ):
  801. strategy_options = util.preloaded.orm_strategy_options
  802. clauseelement = self.entity.__clause_element__()
  803. stmt = Select._create_raw_select(
  804. _raw_columns=[clauseelement],
  805. _propagate_attrs=clauseelement._propagate_attrs,
  806. _label_style=LABEL_STYLE_TABLENAME_PLUS_COL,
  807. _compile_options=ORMCompileState.default_compile_options,
  808. )
  809. load_options = QueryContext.default_load_options
  810. load_options += {
  811. "_invoke_all_eagers": False,
  812. "_lazy_loaded_from": state,
  813. }
  814. if self.parent_property.secondary is not None:
  815. stmt = stmt.select_from(
  816. self.mapper, self.parent_property.secondary
  817. )
  818. pending = not state.key
  819. # don't autoflush on pending
  820. if pending or passive & attributes.NO_AUTOFLUSH:
  821. stmt._execution_options = util.immutabledict({"autoflush": False})
  822. use_get = self.use_get
  823. if state.load_options or (loadopt and loadopt._extra_criteria):
  824. effective_path = state.load_path[self.parent_property]
  825. opts = tuple(state.load_options)
  826. if loadopt and loadopt._extra_criteria:
  827. use_get = False
  828. opts += (
  829. orm_util.LoaderCriteriaOption(self.entity, extra_criteria),
  830. )
  831. stmt._with_options = opts
  832. else:
  833. # this path is used if there are not already any options
  834. # in the query, but an event may want to add them
  835. effective_path = state.mapper._path_registry[self.parent_property]
  836. stmt._compile_options += {"_current_path": effective_path}
  837. if use_get:
  838. if self._raise_on_sql and not passive & attributes.NO_RAISE:
  839. self._invoke_raise_load(state, passive, "raise_on_sql")
  840. return loading.load_on_pk_identity(
  841. session, stmt, primary_key_identity, load_options=load_options
  842. )
  843. if self._order_by:
  844. stmt._order_by_clauses = self._order_by
  845. def _lazyload_reverse(compile_context):
  846. for rev in self.parent_property._reverse_property:
  847. # reverse props that are MANYTOONE are loading *this*
  848. # object from get(), so don't need to eager out to those.
  849. if (
  850. rev.direction is interfaces.MANYTOONE
  851. and rev._use_get
  852. and not isinstance(rev.strategy, LazyLoader)
  853. ):
  854. strategy_options.Load.for_existing_path(
  855. compile_context.compile_options._current_path[
  856. rev.parent
  857. ]
  858. ).lazyload(rev).process_compile_state(compile_context)
  859. stmt._with_context_options += (
  860. (_lazyload_reverse, self.parent_property),
  861. )
  862. lazy_clause, params = self._generate_lazy_clause(state, passive)
  863. execution_options = {
  864. "_sa_orm_load_options": load_options,
  865. }
  866. if (
  867. self.key in state.dict
  868. and not passive & attributes.DEFERRED_HISTORY_LOAD
  869. ):
  870. return attributes.ATTR_WAS_SET
  871. if pending:
  872. if util.has_intersection(orm_util._none_set, params.values()):
  873. return None
  874. elif util.has_intersection(orm_util._never_set, params.values()):
  875. return None
  876. if self._raise_on_sql and not passive & attributes.NO_RAISE:
  877. self._invoke_raise_load(state, passive, "raise_on_sql")
  878. stmt._where_criteria = (lazy_clause,)
  879. result = session.execute(
  880. stmt, params, execution_options=execution_options
  881. )
  882. result = result.unique().scalars().all()
  883. if self.uselist:
  884. return result
  885. else:
  886. l = len(result)
  887. if l:
  888. if l > 1:
  889. util.warn(
  890. "Multiple rows returned with "
  891. "uselist=False for lazily-loaded attribute '%s' "
  892. % self.parent_property
  893. )
  894. return result[0]
  895. else:
  896. return None
  897. def create_row_processor(
  898. self,
  899. context,
  900. query_entity,
  901. path,
  902. loadopt,
  903. mapper,
  904. result,
  905. adapter,
  906. populators,
  907. ):
  908. key = self.key
  909. if not self.is_class_level or (loadopt and loadopt._extra_criteria):
  910. # we are not the primary manager for this attribute
  911. # on this class - set up a
  912. # per-instance lazyloader, which will override the
  913. # class-level behavior.
  914. # this currently only happens when using a
  915. # "lazyload" option on a "no load"
  916. # attribute - "eager" attributes always have a
  917. # class-level lazyloader installed.
  918. set_lazy_callable = (
  919. InstanceState._instance_level_callable_processor
  920. )(
  921. mapper.class_manager,
  922. LoadLazyAttribute(
  923. key,
  924. self,
  925. loadopt,
  926. loadopt._generate_extra_criteria(context)
  927. if loadopt._extra_criteria
  928. else None,
  929. ),
  930. key,
  931. )
  932. populators["new"].append((self.key, set_lazy_callable))
  933. elif context.populate_existing or mapper.always_refresh:
  934. def reset_for_lazy_callable(state, dict_, row):
  935. # we are the primary manager for this attribute on
  936. # this class - reset its
  937. # per-instance attribute state, so that the class-level
  938. # lazy loader is
  939. # executed when next referenced on this instance.
  940. # this is needed in
  941. # populate_existing() types of scenarios to reset
  942. # any existing state.
  943. state._reset(dict_, key)
  944. populators["new"].append((self.key, reset_for_lazy_callable))
  945. class LoadLazyAttribute(object):
  946. """semi-serializable loader object used by LazyLoader
  947. Historically, this object would be carried along with instances that
  948. needed to run lazyloaders, so it had to be serializable to support
  949. cached instances.
  950. this is no longer a general requirement, and the case where this object
  951. is used is exactly the case where we can't really serialize easily,
  952. which is when extra criteria in the loader option is present.
  953. We can't reliably serialize that as it refers to mapped entities and
  954. AliasedClass objects that are local to the current process, which would
  955. need to be matched up on deserialize e.g. the sqlalchemy.ext.serializer
  956. approach.
  957. """
  958. def __init__(self, key, initiating_strategy, loadopt, extra_criteria):
  959. self.key = key
  960. self.strategy_key = initiating_strategy.strategy_key
  961. self.loadopt = loadopt
  962. self.extra_criteria = extra_criteria
  963. def __getstate__(self):
  964. if self.extra_criteria is not None:
  965. util.warn(
  966. "Can't reliably serialize a lazyload() option that "
  967. "contains additional criteria; please use eager loading "
  968. "for this case"
  969. )
  970. return {
  971. "key": self.key,
  972. "strategy_key": self.strategy_key,
  973. "loadopt": self.loadopt,
  974. "extra_criteria": (),
  975. }
  976. def __call__(self, state, passive=attributes.PASSIVE_OFF):
  977. key = self.key
  978. instance_mapper = state.manager.mapper
  979. prop = instance_mapper._props[key]
  980. strategy = prop._strategies[self.strategy_key]
  981. return strategy._load_for_state(
  982. state,
  983. passive,
  984. loadopt=self.loadopt,
  985. extra_criteria=self.extra_criteria,
  986. )
  987. class PostLoader(AbstractRelationshipLoader):
  988. """A relationship loader that emits a second SELECT statement."""
  989. def _check_recursive_postload(self, context, path, join_depth=None):
  990. effective_path = (
  991. context.compile_state.current_path or orm_util.PathRegistry.root
  992. ) + path
  993. if loading.PostLoad.path_exists(
  994. context, effective_path, self.parent_property
  995. ):
  996. return True
  997. path_w_prop = path[self.parent_property]
  998. effective_path_w_prop = effective_path[self.parent_property]
  999. if not path_w_prop.contains(context.attributes, "loader"):
  1000. if join_depth:
  1001. if effective_path_w_prop.length / 2 > join_depth:
  1002. return True
  1003. elif effective_path_w_prop.contains_mapper(self.mapper):
  1004. return True
  1005. return False
  1006. def _immediateload_create_row_processor(
  1007. self,
  1008. context,
  1009. query_entity,
  1010. path,
  1011. loadopt,
  1012. mapper,
  1013. result,
  1014. adapter,
  1015. populators,
  1016. ):
  1017. return self.parent_property._get_strategy(
  1018. (("lazy", "immediate"),)
  1019. ).create_row_processor(
  1020. context,
  1021. query_entity,
  1022. path,
  1023. loadopt,
  1024. mapper,
  1025. result,
  1026. adapter,
  1027. populators,
  1028. )
  1029. @relationships.RelationshipProperty.strategy_for(lazy="immediate")
  1030. class ImmediateLoader(PostLoader):
  1031. __slots__ = ()
  1032. def init_class_attribute(self, mapper):
  1033. self.parent_property._get_strategy(
  1034. (("lazy", "select"),)
  1035. ).init_class_attribute(mapper)
  1036. def create_row_processor(
  1037. self,
  1038. context,
  1039. query_entity,
  1040. path,
  1041. loadopt,
  1042. mapper,
  1043. result,
  1044. adapter,
  1045. populators,
  1046. ):
  1047. def load_immediate(state, dict_, row):
  1048. state.get_impl(self.key).get(state, dict_, flags)
  1049. if self._check_recursive_postload(context, path):
  1050. # this will not emit SQL and will only emit for a many-to-one
  1051. # "use get" load. the "_RELATED" part means it may return
  1052. # instance even if its expired, since this is a mutually-recursive
  1053. # load operation.
  1054. flags = attributes.PASSIVE_NO_FETCH_RELATED | attributes.NO_RAISE
  1055. else:
  1056. flags = attributes.PASSIVE_OFF | attributes.NO_RAISE
  1057. populators["delayed"].append((self.key, load_immediate))
  1058. @log.class_logger
  1059. @relationships.RelationshipProperty.strategy_for(lazy="subquery")
  1060. class SubqueryLoader(PostLoader):
  1061. __slots__ = ("join_depth",)
  1062. def __init__(self, parent, strategy_key):
  1063. super(SubqueryLoader, self).__init__(parent, strategy_key)
  1064. self.join_depth = self.parent_property.join_depth
  1065. def init_class_attribute(self, mapper):
  1066. self.parent_property._get_strategy(
  1067. (("lazy", "select"),)
  1068. ).init_class_attribute(mapper)
  1069. def _get_leftmost(
  1070. self,
  1071. orig_query_entity_index,
  1072. subq_path,
  1073. current_compile_state,
  1074. is_root,
  1075. ):
  1076. given_subq_path = subq_path
  1077. subq_path = subq_path.path
  1078. subq_mapper = orm_util._class_to_mapper(subq_path[0])
  1079. # determine attributes of the leftmost mapper
  1080. if (
  1081. self.parent.isa(subq_mapper)
  1082. and self.parent_property is subq_path[1]
  1083. ):
  1084. leftmost_mapper, leftmost_prop = self.parent, self.parent_property
  1085. else:
  1086. leftmost_mapper, leftmost_prop = subq_mapper, subq_path[1]
  1087. if is_root:
  1088. # the subq_path is also coming from cached state, so when we start
  1089. # building up this path, it has to also be converted to be in terms
  1090. # of the current state. this is for the specific case of the entity
  1091. # is an AliasedClass against a subquery that's not otherwise going
  1092. # to adapt
  1093. new_subq_path = current_compile_state._entities[
  1094. orig_query_entity_index
  1095. ].entity_zero._path_registry[leftmost_prop]
  1096. additional = len(subq_path) - len(new_subq_path)
  1097. if additional:
  1098. new_subq_path += path_registry.PathRegistry.coerce(
  1099. subq_path[-additional:]
  1100. )
  1101. else:
  1102. new_subq_path = given_subq_path
  1103. leftmost_cols = leftmost_prop.local_columns
  1104. leftmost_attr = [
  1105. getattr(
  1106. new_subq_path.path[0].entity,
  1107. leftmost_mapper._columntoproperty[c].key,
  1108. )
  1109. for c in leftmost_cols
  1110. ]
  1111. return leftmost_mapper, leftmost_attr, leftmost_prop, new_subq_path
  1112. def _generate_from_original_query(
  1113. self,
  1114. orig_compile_state,
  1115. orig_query,
  1116. leftmost_mapper,
  1117. leftmost_attr,
  1118. leftmost_relationship,
  1119. orig_entity,
  1120. ):
  1121. # reformat the original query
  1122. # to look only for significant columns
  1123. q = orig_query._clone().correlate(None)
  1124. # LEGACY: make a Query back from the select() !!
  1125. # This suits at least two legacy cases:
  1126. # 1. applications which expect before_compile() to be called
  1127. # below when we run .subquery() on this query (Keystone)
  1128. # 2. applications which are doing subqueryload with complex
  1129. # from_self() queries, as query.subquery() / .statement
  1130. # has to do the full compile context for multiply-nested
  1131. # from_self() (Neutron) - see test_subqload_from_self
  1132. # for demo.
  1133. q2 = query.Query.__new__(query.Query)
  1134. q2.__dict__.update(q.__dict__)
  1135. q = q2
  1136. # set the query's "FROM" list explicitly to what the
  1137. # FROM list would be in any case, as we will be limiting
  1138. # the columns in the SELECT list which may no longer include
  1139. # all entities mentioned in things like WHERE, JOIN, etc.
  1140. if not q._from_obj:
  1141. q._enable_assertions = False
  1142. q.select_from.non_generative(
  1143. q,
  1144. *{
  1145. ent["entity"]
  1146. for ent in _column_descriptions(
  1147. orig_query, compile_state=orig_compile_state
  1148. )
  1149. if ent["entity"] is not None
  1150. }
  1151. )
  1152. # select from the identity columns of the outer (specifically, these
  1153. # are the 'local_cols' of the property). This will remove other
  1154. # columns from the query that might suggest the right entity which is
  1155. # why we do set select_from above. The attributes we have are
  1156. # coerced and adapted using the original query's adapter, which is
  1157. # needed only for the case of adapting a subclass column to
  1158. # that of a polymorphic selectable, e.g. we have
  1159. # Engineer.primary_language and the entity is Person. All other
  1160. # adaptations, e.g. from_self, select_entity_from(), will occur
  1161. # within the new query when it compiles, as the compile_state we are
  1162. # using here is only a partial one. If the subqueryload is from a
  1163. # with_polymorphic() or other aliased() object, left_attr will already
  1164. # be the correct attributes so no adaptation is needed.
  1165. target_cols = orig_compile_state._adapt_col_list(
  1166. [
  1167. sql.coercions.expect(sql.roles.ColumnsClauseRole, o)
  1168. for o in leftmost_attr
  1169. ],
  1170. orig_compile_state._get_current_adapter(),
  1171. )
  1172. q._raw_columns = target_cols
  1173. distinct_target_key = leftmost_relationship.distinct_target_key
  1174. if distinct_target_key is True:
  1175. q._distinct = True
  1176. elif distinct_target_key is None:
  1177. # if target_cols refer to a non-primary key or only
  1178. # part of a composite primary key, set the q as distinct
  1179. for t in set(c.table for c in target_cols):
  1180. if not set(target_cols).issuperset(t.primary_key):
  1181. q._distinct = True
  1182. break
  1183. # don't need ORDER BY if no limit/offset
  1184. if not q._has_row_limiting_clause:
  1185. q._order_by_clauses = ()
  1186. if q._distinct is True and q._order_by_clauses:
  1187. # the logic to automatically add the order by columns to the query
  1188. # when distinct is True is deprecated in the query
  1189. to_add = sql_util.expand_column_list_from_order_by(
  1190. target_cols, q._order_by_clauses
  1191. )
  1192. if to_add:
  1193. q._set_entities(target_cols + to_add)
  1194. # the original query now becomes a subquery
  1195. # which we'll join onto.
  1196. # LEGACY: as "q" is a Query, the before_compile() event is invoked
  1197. # here.
  1198. embed_q = q.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL).subquery()
  1199. left_alias = orm_util.AliasedClass(
  1200. leftmost_mapper, embed_q, use_mapper_path=True
  1201. )
  1202. return left_alias
  1203. def _prep_for_joins(self, left_alias, subq_path):
  1204. # figure out what's being joined. a.k.a. the fun part
  1205. to_join = []
  1206. pairs = list(subq_path.pairs())
  1207. for i, (mapper, prop) in enumerate(pairs):
  1208. if i > 0:
  1209. # look at the previous mapper in the chain -
  1210. # if it is as or more specific than this prop's
  1211. # mapper, use that instead.
  1212. # note we have an assumption here that
  1213. # the non-first element is always going to be a mapper,
  1214. # not an AliasedClass
  1215. prev_mapper = pairs[i - 1][1].mapper
  1216. to_append = prev_mapper if prev_mapper.isa(mapper) else mapper
  1217. else:
  1218. to_append = mapper
  1219. to_join.append((to_append, prop.key))
  1220. # determine the immediate parent class we are joining from,
  1221. # which needs to be aliased.
  1222. if len(to_join) < 2:
  1223. # in the case of a one level eager load, this is the
  1224. # leftmost "left_alias".
  1225. parent_alias = left_alias
  1226. else:
  1227. info = inspect(to_join[-1][0])
  1228. if info.is_aliased_class:
  1229. parent_alias = info.entity
  1230. else:
  1231. # alias a plain mapper as we may be
  1232. # joining multiple times
  1233. parent_alias = orm_util.AliasedClass(
  1234. info.entity, use_mapper_path=True
  1235. )
  1236. local_cols = self.parent_property.local_columns
  1237. local_attr = [
  1238. getattr(parent_alias, self.parent._columntoproperty[c].key)
  1239. for c in local_cols
  1240. ]
  1241. return to_join, local_attr, parent_alias
  1242. def _apply_joins(
  1243. self, q, to_join, left_alias, parent_alias, effective_entity
  1244. ):
  1245. ltj = len(to_join)
  1246. if ltj == 1:
  1247. to_join = [
  1248. getattr(left_alias, to_join[0][1]).of_type(effective_entity)
  1249. ]
  1250. elif ltj == 2:
  1251. to_join = [
  1252. getattr(left_alias, to_join[0][1]).of_type(parent_alias),
  1253. getattr(parent_alias, to_join[-1][1]).of_type(
  1254. effective_entity
  1255. ),
  1256. ]
  1257. elif ltj > 2:
  1258. middle = [
  1259. (
  1260. orm_util.AliasedClass(item[0])
  1261. if not inspect(item[0]).is_aliased_class
  1262. else item[0].entity,
  1263. item[1],
  1264. )
  1265. for item in to_join[1:-1]
  1266. ]
  1267. inner = []
  1268. while middle:
  1269. item = middle.pop(0)
  1270. attr = getattr(item[0], item[1])
  1271. if middle:
  1272. attr = attr.of_type(middle[0][0])
  1273. else:
  1274. attr = attr.of_type(parent_alias)
  1275. inner.append(attr)
  1276. to_join = (
  1277. [getattr(left_alias, to_join[0][1]).of_type(inner[0].parent)]
  1278. + inner
  1279. + [
  1280. getattr(parent_alias, to_join[-1][1]).of_type(
  1281. effective_entity
  1282. )
  1283. ]
  1284. )
  1285. for attr in to_join:
  1286. q = q.join(attr)
  1287. return q
  1288. def _setup_options(
  1289. self,
  1290. context,
  1291. q,
  1292. subq_path,
  1293. rewritten_path,
  1294. orig_query,
  1295. effective_entity,
  1296. loadopt,
  1297. ):
  1298. # note that because the subqueryload object
  1299. # does not re-use the cached query, instead always making
  1300. # use of the current invoked query, while we have two queries
  1301. # here (orig and context.query), they are both non-cached
  1302. # queries and we can transfer the options as is without
  1303. # adjusting for new criteria. Some work on #6881 / #6889
  1304. # brought this into question.
  1305. new_options = orig_query._with_options
  1306. if loadopt and loadopt._extra_criteria:
  1307. new_options += (
  1308. orm_util.LoaderCriteriaOption(
  1309. self.entity,
  1310. loadopt._generate_extra_criteria(context),
  1311. ),
  1312. )
  1313. # propagate loader options etc. to the new query.
  1314. # these will fire relative to subq_path.
  1315. q = q._with_current_path(rewritten_path)
  1316. q = q.options(*new_options)
  1317. return q
  1318. def _setup_outermost_orderby(self, q):
  1319. if self.parent_property.order_by:
  1320. def _setup_outermost_orderby(compile_context):
  1321. compile_context.eager_order_by += tuple(
  1322. util.to_list(self.parent_property.order_by)
  1323. )
  1324. q = q._add_context_option(
  1325. _setup_outermost_orderby, self.parent_property
  1326. )
  1327. return q
  1328. class _SubqCollections(object):
  1329. """Given a :class:`_query.Query` used to emit the "subquery load",
  1330. provide a load interface that executes the query at the
  1331. first moment a value is needed.
  1332. """
  1333. __slots__ = (
  1334. "session",
  1335. "execution_options",
  1336. "load_options",
  1337. "params",
  1338. "subq",
  1339. "_data",
  1340. )
  1341. def __init__(self, context, subq):
  1342. # avoid creating a cycle by storing context
  1343. # even though that's preferable
  1344. self.session = context.session
  1345. self.execution_options = context.execution_options
  1346. self.load_options = context.load_options
  1347. self.params = context.params or {}
  1348. self.subq = subq
  1349. self._data = None
  1350. def get(self, key, default):
  1351. if self._data is None:
  1352. self._load()
  1353. return self._data.get(key, default)
  1354. def _load(self):
  1355. self._data = collections.defaultdict(list)
  1356. q = self.subq
  1357. assert q.session is None
  1358. q = q.with_session(self.session)
  1359. if self.load_options._populate_existing:
  1360. q = q.populate_existing()
  1361. # to work with baked query, the parameters may have been
  1362. # updated since this query was created, so take these into account
  1363. rows = list(q.params(self.params))
  1364. for k, v in itertools.groupby(rows, lambda x: x[1:]):
  1365. self._data[k].extend(vv[0] for vv in v)
  1366. def loader(self, state, dict_, row):
  1367. if self._data is None:
  1368. self._load()
  1369. def _setup_query_from_rowproc(
  1370. self,
  1371. context,
  1372. query_entity,
  1373. path,
  1374. entity,
  1375. loadopt,
  1376. adapter,
  1377. ):
  1378. compile_state = context.compile_state
  1379. if (
  1380. not compile_state.compile_options._enable_eagerloads
  1381. or compile_state.compile_options._for_refresh_state
  1382. ):
  1383. return
  1384. orig_query_entity_index = compile_state._entities.index(query_entity)
  1385. context.loaders_require_buffering = True
  1386. path = path[self.parent_property]
  1387. # build up a path indicating the path from the leftmost
  1388. # entity to the thing we're subquery loading.
  1389. with_poly_entity = path.get(
  1390. compile_state.attributes, "path_with_polymorphic", None
  1391. )
  1392. if with_poly_entity is not None:
  1393. effective_entity = with_poly_entity
  1394. else:
  1395. effective_entity = self.entity
  1396. subq_path, rewritten_path = context.query._execution_options.get(
  1397. ("subquery_paths", None),
  1398. (orm_util.PathRegistry.root, orm_util.PathRegistry.root),
  1399. )
  1400. is_root = subq_path is orm_util.PathRegistry.root
  1401. subq_path = subq_path + path
  1402. rewritten_path = rewritten_path + path
  1403. # if not via query option, check for
  1404. # a cycle
  1405. # TODO: why is this here??? this is now handled
  1406. # by the _check_recursive_postload call
  1407. if not path.contains(compile_state.attributes, "loader"):
  1408. if self.join_depth:
  1409. if (
  1410. (
  1411. compile_state.current_path.length
  1412. if compile_state.current_path
  1413. else 0
  1414. )
  1415. + path.length
  1416. ) / 2 > self.join_depth:
  1417. return
  1418. elif subq_path.contains_mapper(self.mapper):
  1419. return
  1420. # use the current query being invoked, not the compile state
  1421. # one. this is so that we get the current parameters. however,
  1422. # it means we can't use the existing compile state, we have to make
  1423. # a new one. other approaches include possibly using the
  1424. # compiled query but swapping the params, seems only marginally
  1425. # less time spent but more complicated
  1426. orig_query = context.query._execution_options.get(
  1427. ("orig_query", SubqueryLoader), context.query
  1428. )
  1429. # make a new compile_state for the query that's probably cached, but
  1430. # we're sort of undoing a bit of that caching :(
  1431. compile_state_cls = ORMCompileState._get_plugin_class_for_plugin(
  1432. orig_query, "orm"
  1433. )
  1434. if orig_query._is_lambda_element:
  1435. if context.load_options._lazy_loaded_from is None:
  1436. util.warn(
  1437. 'subqueryloader for "%s" must invoke lambda callable '
  1438. "at %r in "
  1439. "order to produce a new query, decreasing the efficiency "
  1440. "of caching for this statement. Consider using "
  1441. "selectinload() for more effective full-lambda caching"
  1442. % (self, orig_query)
  1443. )
  1444. orig_query = orig_query._resolved
  1445. # this is the more "quick" version, however it's not clear how
  1446. # much of this we need. in particular I can't get a test to
  1447. # fail if the "set_base_alias" is missing and not sure why that is.
  1448. orig_compile_state = compile_state_cls._create_entities_collection(
  1449. orig_query, legacy=False
  1450. )
  1451. (
  1452. leftmost_mapper,
  1453. leftmost_attr,
  1454. leftmost_relationship,
  1455. rewritten_path,
  1456. ) = self._get_leftmost(
  1457. orig_query_entity_index,
  1458. rewritten_path,
  1459. orig_compile_state,
  1460. is_root,
  1461. )
  1462. # generate a new Query from the original, then
  1463. # produce a subquery from it.
  1464. left_alias = self._generate_from_original_query(
  1465. orig_compile_state,
  1466. orig_query,
  1467. leftmost_mapper,
  1468. leftmost_attr,
  1469. leftmost_relationship,
  1470. entity,
  1471. )
  1472. # generate another Query that will join the
  1473. # left alias to the target relationships.
  1474. # basically doing a longhand
  1475. # "from_self()". (from_self() itself not quite industrial
  1476. # strength enough for all contingencies...but very close)
  1477. q = query.Query(effective_entity)
  1478. q._execution_options = q._execution_options.union(
  1479. {
  1480. ("orig_query", SubqueryLoader): orig_query,
  1481. ("subquery_paths", None): (subq_path, rewritten_path),
  1482. }
  1483. )
  1484. q = q._set_enable_single_crit(False)
  1485. to_join, local_attr, parent_alias = self._prep_for_joins(
  1486. left_alias, subq_path
  1487. )
  1488. q = q.add_columns(*local_attr)
  1489. q = self._apply_joins(
  1490. q, to_join, left_alias, parent_alias, effective_entity
  1491. )
  1492. q = self._setup_options(
  1493. context,
  1494. q,
  1495. subq_path,
  1496. rewritten_path,
  1497. orig_query,
  1498. effective_entity,
  1499. loadopt,
  1500. )
  1501. q = self._setup_outermost_orderby(q)
  1502. return q
  1503. def create_row_processor(
  1504. self,
  1505. context,
  1506. query_entity,
  1507. path,
  1508. loadopt,
  1509. mapper,
  1510. result,
  1511. adapter,
  1512. populators,
  1513. ):
  1514. if context.refresh_state:
  1515. return self._immediateload_create_row_processor(
  1516. context,
  1517. query_entity,
  1518. path,
  1519. loadopt,
  1520. mapper,
  1521. result,
  1522. adapter,
  1523. populators,
  1524. )
  1525. # the subqueryloader does a similar check in setup_query() unlike
  1526. # the other post loaders, however we have this here for consistency
  1527. elif self._check_recursive_postload(context, path, self.join_depth):
  1528. return
  1529. elif not isinstance(context.compile_state, ORMSelectCompileState):
  1530. # issue 7505 - subqueryload() in 1.3 and previous would silently
  1531. # degrade for from_statement() without warning. this behavior
  1532. # is restored here
  1533. return
  1534. if not self.parent.class_manager[self.key].impl.supports_population:
  1535. raise sa_exc.InvalidRequestError(
  1536. "'%s' does not support object "
  1537. "population - eager loading cannot be applied." % self
  1538. )
  1539. # a little dance here as the "path" is still something that only
  1540. # semi-tracks the exact series of things we are loading, still not
  1541. # telling us about with_polymorphic() and stuff like that when it's at
  1542. # the root.. the initial MapperEntity is more accurate for this case.
  1543. if len(path) == 1:
  1544. if not orm_util._entity_isa(query_entity.entity_zero, self.parent):
  1545. return
  1546. elif not orm_util._entity_isa(path[-1], self.parent):
  1547. return
  1548. subq = self._setup_query_from_rowproc(
  1549. context,
  1550. query_entity,
  1551. path,
  1552. path[-1],
  1553. loadopt,
  1554. adapter,
  1555. )
  1556. if subq is None:
  1557. return
  1558. assert subq.session is None
  1559. path = path[self.parent_property]
  1560. local_cols = self.parent_property.local_columns
  1561. # cache the loaded collections in the context
  1562. # so that inheriting mappers don't re-load when they
  1563. # call upon create_row_processor again
  1564. collections = path.get(context.attributes, "collections")
  1565. if collections is None:
  1566. collections = self._SubqCollections(context, subq)
  1567. path.set(context.attributes, "collections", collections)
  1568. if adapter:
  1569. local_cols = [adapter.columns[c] for c in local_cols]
  1570. if self.uselist:
  1571. self._create_collection_loader(
  1572. context, result, collections, local_cols, populators
  1573. )
  1574. else:
  1575. self._create_scalar_loader(
  1576. context, result, collections, local_cols, populators
  1577. )
  1578. def _create_collection_loader(
  1579. self, context, result, collections, local_cols, populators
  1580. ):
  1581. tuple_getter = result._tuple_getter(local_cols)
  1582. def load_collection_from_subq(state, dict_, row):
  1583. collection = collections.get(tuple_getter(row), ())
  1584. state.get_impl(self.key).set_committed_value(
  1585. state, dict_, collection
  1586. )
  1587. def load_collection_from_subq_existing_row(state, dict_, row):
  1588. if self.key not in dict_:
  1589. load_collection_from_subq(state, dict_, row)
  1590. populators["new"].append((self.key, load_collection_from_subq))
  1591. populators["existing"].append(
  1592. (self.key, load_collection_from_subq_existing_row)
  1593. )
  1594. if context.invoke_all_eagers:
  1595. populators["eager"].append((self.key, collections.loader))
  1596. def _create_scalar_loader(
  1597. self, context, result, collections, local_cols, populators
  1598. ):
  1599. tuple_getter = result._tuple_getter(local_cols)
  1600. def load_scalar_from_subq(state, dict_, row):
  1601. collection = collections.get(tuple_getter(row), (None,))
  1602. if len(collection) > 1:
  1603. util.warn(
  1604. "Multiple rows returned with "
  1605. "uselist=False for eagerly-loaded attribute '%s' " % self
  1606. )
  1607. scalar = collection[0]
  1608. state.get_impl(self.key).set_committed_value(state, dict_, scalar)
  1609. def load_scalar_from_subq_existing_row(state, dict_, row):
  1610. if self.key not in dict_:
  1611. load_scalar_from_subq(state, dict_, row)
  1612. populators["new"].append((self.key, load_scalar_from_subq))
  1613. populators["existing"].append(
  1614. (self.key, load_scalar_from_subq_existing_row)
  1615. )
  1616. if context.invoke_all_eagers:
  1617. populators["eager"].append((self.key, collections.loader))
  1618. @log.class_logger
  1619. @relationships.RelationshipProperty.strategy_for(lazy="joined")
  1620. @relationships.RelationshipProperty.strategy_for(lazy=False)
  1621. class JoinedLoader(AbstractRelationshipLoader):
  1622. """Provide loading behavior for a :class:`.RelationshipProperty`
  1623. using joined eager loading.
  1624. """
  1625. __slots__ = "join_depth", "_aliased_class_pool"
  1626. def __init__(self, parent, strategy_key):
  1627. super(JoinedLoader, self).__init__(parent, strategy_key)
  1628. self.join_depth = self.parent_property.join_depth
  1629. self._aliased_class_pool = []
  1630. def init_class_attribute(self, mapper):
  1631. self.parent_property._get_strategy(
  1632. (("lazy", "select"),)
  1633. ).init_class_attribute(mapper)
  1634. def setup_query(
  1635. self,
  1636. compile_state,
  1637. query_entity,
  1638. path,
  1639. loadopt,
  1640. adapter,
  1641. column_collection=None,
  1642. parentmapper=None,
  1643. chained_from_outerjoin=False,
  1644. **kwargs
  1645. ):
  1646. """Add a left outer join to the statement that's being constructed."""
  1647. if not compile_state.compile_options._enable_eagerloads:
  1648. return
  1649. elif self.uselist:
  1650. compile_state.multi_row_eager_loaders = True
  1651. path = path[self.parent_property]
  1652. with_polymorphic = None
  1653. user_defined_adapter = (
  1654. self._init_user_defined_eager_proc(
  1655. loadopt, compile_state, compile_state.attributes
  1656. )
  1657. if loadopt
  1658. else False
  1659. )
  1660. if user_defined_adapter is not False:
  1661. (
  1662. clauses,
  1663. adapter,
  1664. add_to_collection,
  1665. ) = self._setup_query_on_user_defined_adapter(
  1666. compile_state,
  1667. query_entity,
  1668. path,
  1669. adapter,
  1670. user_defined_adapter,
  1671. )
  1672. else:
  1673. # if not via query option, check for
  1674. # a cycle
  1675. if not path.contains(compile_state.attributes, "loader"):
  1676. if self.join_depth:
  1677. if path.length / 2 > self.join_depth:
  1678. return
  1679. elif path.contains_mapper(self.mapper):
  1680. return
  1681. (
  1682. clauses,
  1683. adapter,
  1684. add_to_collection,
  1685. chained_from_outerjoin,
  1686. ) = self._generate_row_adapter(
  1687. compile_state,
  1688. query_entity,
  1689. path,
  1690. loadopt,
  1691. adapter,
  1692. column_collection,
  1693. parentmapper,
  1694. chained_from_outerjoin,
  1695. )
  1696. with_poly_entity = path.get(
  1697. compile_state.attributes, "path_with_polymorphic", None
  1698. )
  1699. if with_poly_entity is not None:
  1700. with_polymorphic = inspect(
  1701. with_poly_entity
  1702. ).with_polymorphic_mappers
  1703. else:
  1704. with_polymorphic = None
  1705. path = path[self.entity]
  1706. loading._setup_entity_query(
  1707. compile_state,
  1708. self.mapper,
  1709. query_entity,
  1710. path,
  1711. clauses,
  1712. add_to_collection,
  1713. with_polymorphic=with_polymorphic,
  1714. parentmapper=self.mapper,
  1715. chained_from_outerjoin=chained_from_outerjoin,
  1716. )
  1717. if with_poly_entity is not None and None in set(
  1718. compile_state.secondary_columns
  1719. ):
  1720. raise sa_exc.InvalidRequestError(
  1721. "Detected unaliased columns when generating joined "
  1722. "load. Make sure to use aliased=True or flat=True "
  1723. "when using joined loading with with_polymorphic()."
  1724. )
  1725. def _init_user_defined_eager_proc(
  1726. self, loadopt, compile_state, target_attributes
  1727. ):
  1728. # check if the opt applies at all
  1729. if "eager_from_alias" not in loadopt.local_opts:
  1730. # nope
  1731. return False
  1732. path = loadopt.path.parent
  1733. # the option applies. check if the "user_defined_eager_row_processor"
  1734. # has been built up.
  1735. adapter = path.get(
  1736. compile_state.attributes, "user_defined_eager_row_processor", False
  1737. )
  1738. if adapter is not False:
  1739. # just return it
  1740. return adapter
  1741. # otherwise figure it out.
  1742. alias = loadopt.local_opts["eager_from_alias"]
  1743. root_mapper, prop = path[-2:]
  1744. if alias is not None:
  1745. if isinstance(alias, str):
  1746. alias = prop.target.alias(alias)
  1747. adapter = sql_util.ColumnAdapter(
  1748. alias, equivalents=prop.mapper._equivalent_columns
  1749. )
  1750. else:
  1751. if path.contains(
  1752. compile_state.attributes, "path_with_polymorphic"
  1753. ):
  1754. with_poly_entity = path.get(
  1755. compile_state.attributes, "path_with_polymorphic"
  1756. )
  1757. adapter = orm_util.ORMAdapter(
  1758. with_poly_entity,
  1759. equivalents=prop.mapper._equivalent_columns,
  1760. )
  1761. else:
  1762. adapter = compile_state._polymorphic_adapters.get(
  1763. prop.mapper, None
  1764. )
  1765. path.set(
  1766. target_attributes,
  1767. "user_defined_eager_row_processor",
  1768. adapter,
  1769. )
  1770. return adapter
  1771. def _setup_query_on_user_defined_adapter(
  1772. self, context, entity, path, adapter, user_defined_adapter
  1773. ):
  1774. # apply some more wrapping to the "user defined adapter"
  1775. # if we are setting up the query for SQL render.
  1776. adapter = entity._get_entity_clauses(context)
  1777. if adapter and user_defined_adapter:
  1778. user_defined_adapter = user_defined_adapter.wrap(adapter)
  1779. path.set(
  1780. context.attributes,
  1781. "user_defined_eager_row_processor",
  1782. user_defined_adapter,
  1783. )
  1784. elif adapter:
  1785. user_defined_adapter = adapter
  1786. path.set(
  1787. context.attributes,
  1788. "user_defined_eager_row_processor",
  1789. user_defined_adapter,
  1790. )
  1791. add_to_collection = context.primary_columns
  1792. return user_defined_adapter, adapter, add_to_collection
  1793. def _gen_pooled_aliased_class(self, context):
  1794. # keep a local pool of AliasedClass objects that get re-used.
  1795. # we need one unique AliasedClass per query per appearance of our
  1796. # entity in the query.
  1797. if inspect(self.entity).is_aliased_class:
  1798. alt_selectable = inspect(self.entity).selectable
  1799. else:
  1800. alt_selectable = None
  1801. key = ("joinedloader_ac", self)
  1802. if key not in context.attributes:
  1803. context.attributes[key] = idx = 0
  1804. else:
  1805. context.attributes[key] = idx = context.attributes[key] + 1
  1806. if idx >= len(self._aliased_class_pool):
  1807. to_adapt = orm_util.AliasedClass(
  1808. self.mapper,
  1809. alias=alt_selectable._anonymous_fromclause(flat=True)
  1810. if alt_selectable is not None
  1811. else None,
  1812. flat=True,
  1813. use_mapper_path=True,
  1814. )
  1815. # load up the .columns collection on the Alias() before
  1816. # the object becomes shared among threads. this prevents
  1817. # races for column identities.
  1818. inspect(to_adapt).selectable.c
  1819. self._aliased_class_pool.append(to_adapt)
  1820. return self._aliased_class_pool[idx]
  1821. def _generate_row_adapter(
  1822. self,
  1823. compile_state,
  1824. entity,
  1825. path,
  1826. loadopt,
  1827. adapter,
  1828. column_collection,
  1829. parentmapper,
  1830. chained_from_outerjoin,
  1831. ):
  1832. with_poly_entity = path.get(
  1833. compile_state.attributes, "path_with_polymorphic", None
  1834. )
  1835. if with_poly_entity:
  1836. to_adapt = with_poly_entity
  1837. else:
  1838. to_adapt = self._gen_pooled_aliased_class(compile_state)
  1839. clauses = inspect(to_adapt)._memo(
  1840. ("joinedloader_ormadapter", self),
  1841. orm_util.ORMAdapter,
  1842. to_adapt,
  1843. equivalents=self.mapper._equivalent_columns,
  1844. adapt_required=True,
  1845. allow_label_resolve=False,
  1846. anonymize_labels=True,
  1847. )
  1848. assert clauses.aliased_class is not None
  1849. innerjoin = (
  1850. loadopt.local_opts.get("innerjoin", self.parent_property.innerjoin)
  1851. if loadopt is not None
  1852. else self.parent_property.innerjoin
  1853. )
  1854. if not innerjoin:
  1855. # if this is an outer join, all non-nested eager joins from
  1856. # this path must also be outer joins
  1857. chained_from_outerjoin = True
  1858. compile_state.create_eager_joins.append(
  1859. (
  1860. self._create_eager_join,
  1861. entity,
  1862. path,
  1863. adapter,
  1864. parentmapper,
  1865. clauses,
  1866. innerjoin,
  1867. chained_from_outerjoin,
  1868. loadopt._extra_criteria if loadopt else (),
  1869. )
  1870. )
  1871. add_to_collection = compile_state.secondary_columns
  1872. path.set(compile_state.attributes, "eager_row_processor", clauses)
  1873. return clauses, adapter, add_to_collection, chained_from_outerjoin
  1874. def _create_eager_join(
  1875. self,
  1876. compile_state,
  1877. query_entity,
  1878. path,
  1879. adapter,
  1880. parentmapper,
  1881. clauses,
  1882. innerjoin,
  1883. chained_from_outerjoin,
  1884. extra_criteria,
  1885. ):
  1886. if parentmapper is None:
  1887. localparent = query_entity.mapper
  1888. else:
  1889. localparent = parentmapper
  1890. # whether or not the Query will wrap the selectable in a subquery,
  1891. # and then attach eager load joins to that (i.e., in the case of
  1892. # LIMIT/OFFSET etc.)
  1893. should_nest_selectable = (
  1894. compile_state.multi_row_eager_loaders
  1895. and compile_state._should_nest_selectable
  1896. )
  1897. query_entity_key = None
  1898. if (
  1899. query_entity not in compile_state.eager_joins
  1900. and not should_nest_selectable
  1901. and compile_state.from_clauses
  1902. ):
  1903. indexes = sql_util.find_left_clause_that_matches_given(
  1904. compile_state.from_clauses, query_entity.selectable
  1905. )
  1906. if len(indexes) > 1:
  1907. # for the eager load case, I can't reproduce this right
  1908. # now. For query.join() I can.
  1909. raise sa_exc.InvalidRequestError(
  1910. "Can't identify which query entity in which to joined "
  1911. "eager load from. Please use an exact match when "
  1912. "specifying the join path."
  1913. )
  1914. if indexes:
  1915. clause = compile_state.from_clauses[indexes[0]]
  1916. # join to an existing FROM clause on the query.
  1917. # key it to its list index in the eager_joins dict.
  1918. # Query._compile_context will adapt as needed and
  1919. # append to the FROM clause of the select().
  1920. query_entity_key, default_towrap = indexes[0], clause
  1921. if query_entity_key is None:
  1922. query_entity_key, default_towrap = (
  1923. query_entity,
  1924. query_entity.selectable,
  1925. )
  1926. towrap = compile_state.eager_joins.setdefault(
  1927. query_entity_key, default_towrap
  1928. )
  1929. if adapter:
  1930. if getattr(adapter, "aliased_class", None):
  1931. # joining from an adapted entity. The adapted entity
  1932. # might be a "with_polymorphic", so resolve that to our
  1933. # specific mapper's entity before looking for our attribute
  1934. # name on it.
  1935. efm = inspect(adapter.aliased_class)._entity_for_mapper(
  1936. localparent
  1937. if localparent.isa(self.parent)
  1938. else self.parent
  1939. )
  1940. # look for our attribute on the adapted entity, else fall back
  1941. # to our straight property
  1942. onclause = getattr(efm.entity, self.key, self.parent_property)
  1943. else:
  1944. onclause = getattr(
  1945. orm_util.AliasedClass(
  1946. self.parent, adapter.selectable, use_mapper_path=True
  1947. ),
  1948. self.key,
  1949. self.parent_property,
  1950. )
  1951. else:
  1952. onclause = self.parent_property
  1953. assert clauses.aliased_class is not None
  1954. attach_on_outside = (
  1955. not chained_from_outerjoin
  1956. or not innerjoin
  1957. or innerjoin == "unnested"
  1958. or query_entity.entity_zero.represents_outer_join
  1959. )
  1960. extra_join_criteria = extra_criteria
  1961. additional_entity_criteria = compile_state.global_attributes.get(
  1962. ("additional_entity_criteria", self.mapper), ()
  1963. )
  1964. if additional_entity_criteria:
  1965. extra_join_criteria += tuple(
  1966. ae._resolve_where_criteria(self.mapper)
  1967. for ae in additional_entity_criteria
  1968. if ae.propagate_to_loaders
  1969. )
  1970. if attach_on_outside:
  1971. # this is the "classic" eager join case.
  1972. eagerjoin = orm_util._ORMJoin(
  1973. towrap,
  1974. clauses.aliased_class,
  1975. onclause,
  1976. isouter=not innerjoin
  1977. or query_entity.entity_zero.represents_outer_join
  1978. or (chained_from_outerjoin and isinstance(towrap, sql.Join)),
  1979. _left_memo=self.parent,
  1980. _right_memo=self.mapper,
  1981. _extra_criteria=extra_join_criteria,
  1982. )
  1983. else:
  1984. # all other cases are innerjoin=='nested' approach
  1985. eagerjoin = self._splice_nested_inner_join(
  1986. path, towrap, clauses, onclause, extra_join_criteria
  1987. )
  1988. compile_state.eager_joins[query_entity_key] = eagerjoin
  1989. # send a hint to the Query as to where it may "splice" this join
  1990. eagerjoin.stop_on = query_entity.selectable
  1991. if not parentmapper:
  1992. # for parentclause that is the non-eager end of the join,
  1993. # ensure all the parent cols in the primaryjoin are actually
  1994. # in the
  1995. # columns clause (i.e. are not deferred), so that aliasing applied
  1996. # by the Query propagates those columns outward.
  1997. # This has the effect
  1998. # of "undefering" those columns.
  1999. for col in sql_util._find_columns(
  2000. self.parent_property.primaryjoin
  2001. ):
  2002. if localparent.persist_selectable.c.contains_column(col):
  2003. if adapter:
  2004. col = adapter.columns[col]
  2005. compile_state._append_dedupe_col_collection(
  2006. col, compile_state.primary_columns
  2007. )
  2008. if self.parent_property.order_by:
  2009. compile_state.eager_order_by += tuple(
  2010. (eagerjoin._target_adapter.copy_and_process)(
  2011. util.to_list(self.parent_property.order_by)
  2012. )
  2013. )
  2014. def _splice_nested_inner_join(
  2015. self, path, join_obj, clauses, onclause, extra_criteria, splicing=False
  2016. ):
  2017. if splicing is False:
  2018. # first call is always handed a join object
  2019. # from the outside
  2020. assert isinstance(join_obj, orm_util._ORMJoin)
  2021. elif isinstance(join_obj, sql.selectable.FromGrouping):
  2022. return self._splice_nested_inner_join(
  2023. path,
  2024. join_obj.element,
  2025. clauses,
  2026. onclause,
  2027. extra_criteria,
  2028. splicing,
  2029. )
  2030. elif not isinstance(join_obj, orm_util._ORMJoin):
  2031. if path[-2] is splicing:
  2032. return orm_util._ORMJoin(
  2033. join_obj,
  2034. clauses.aliased_class,
  2035. onclause,
  2036. isouter=False,
  2037. _left_memo=splicing,
  2038. _right_memo=path[-1].mapper,
  2039. _extra_criteria=extra_criteria,
  2040. )
  2041. else:
  2042. # only here if splicing == True
  2043. return None
  2044. target_join = self._splice_nested_inner_join(
  2045. path,
  2046. join_obj.right,
  2047. clauses,
  2048. onclause,
  2049. extra_criteria,
  2050. join_obj._right_memo,
  2051. )
  2052. if target_join is None:
  2053. right_splice = False
  2054. target_join = self._splice_nested_inner_join(
  2055. path,
  2056. join_obj.left,
  2057. clauses,
  2058. onclause,
  2059. extra_criteria,
  2060. join_obj._left_memo,
  2061. )
  2062. if target_join is None:
  2063. # should only return None when recursively called,
  2064. # e.g. splicing==True
  2065. assert (
  2066. splicing is not False
  2067. ), "assertion failed attempting to produce joined eager loads"
  2068. return None
  2069. else:
  2070. right_splice = True
  2071. if right_splice:
  2072. # for a right splice, attempt to flatten out
  2073. # a JOIN b JOIN c JOIN .. to avoid needless
  2074. # parenthesis nesting
  2075. if not join_obj.isouter and not target_join.isouter:
  2076. eagerjoin = join_obj._splice_into_center(target_join)
  2077. else:
  2078. eagerjoin = orm_util._ORMJoin(
  2079. join_obj.left,
  2080. target_join,
  2081. join_obj.onclause,
  2082. isouter=join_obj.isouter,
  2083. _left_memo=join_obj._left_memo,
  2084. )
  2085. else:
  2086. eagerjoin = orm_util._ORMJoin(
  2087. target_join,
  2088. join_obj.right,
  2089. join_obj.onclause,
  2090. isouter=join_obj.isouter,
  2091. _right_memo=join_obj._right_memo,
  2092. )
  2093. eagerjoin._target_adapter = target_join._target_adapter
  2094. return eagerjoin
  2095. def _create_eager_adapter(self, context, result, adapter, path, loadopt):
  2096. compile_state = context.compile_state
  2097. user_defined_adapter = (
  2098. self._init_user_defined_eager_proc(
  2099. loadopt, compile_state, context.attributes
  2100. )
  2101. if loadopt
  2102. else False
  2103. )
  2104. if user_defined_adapter is not False:
  2105. decorator = user_defined_adapter
  2106. # user defined eagerloads are part of the "primary"
  2107. # portion of the load.
  2108. # the adapters applied to the Query should be honored.
  2109. if compile_state.compound_eager_adapter and decorator:
  2110. decorator = decorator.wrap(
  2111. compile_state.compound_eager_adapter
  2112. )
  2113. elif compile_state.compound_eager_adapter:
  2114. decorator = compile_state.compound_eager_adapter
  2115. else:
  2116. decorator = path.get(
  2117. compile_state.attributes, "eager_row_processor"
  2118. )
  2119. if decorator is None:
  2120. return False
  2121. if self.mapper._result_has_identity_key(result, decorator):
  2122. return decorator
  2123. else:
  2124. # no identity key - don't return a row
  2125. # processor, will cause a degrade to lazy
  2126. return False
  2127. def create_row_processor(
  2128. self,
  2129. context,
  2130. query_entity,
  2131. path,
  2132. loadopt,
  2133. mapper,
  2134. result,
  2135. adapter,
  2136. populators,
  2137. ):
  2138. if not self.parent.class_manager[self.key].impl.supports_population:
  2139. raise sa_exc.InvalidRequestError(
  2140. "'%s' does not support object "
  2141. "population - eager loading cannot be applied." % self
  2142. )
  2143. if self.uselist:
  2144. context.loaders_require_uniquing = True
  2145. our_path = path[self.parent_property]
  2146. eager_adapter = self._create_eager_adapter(
  2147. context, result, adapter, our_path, loadopt
  2148. )
  2149. if eager_adapter is not False:
  2150. key = self.key
  2151. _instance = loading._instance_processor(
  2152. query_entity,
  2153. self.mapper,
  2154. context,
  2155. result,
  2156. our_path[self.entity],
  2157. eager_adapter,
  2158. )
  2159. if not self.uselist:
  2160. self._create_scalar_loader(context, key, _instance, populators)
  2161. else:
  2162. self._create_collection_loader(
  2163. context, key, _instance, populators
  2164. )
  2165. else:
  2166. self.parent_property._get_strategy(
  2167. (("lazy", "select"),)
  2168. ).create_row_processor(
  2169. context,
  2170. query_entity,
  2171. path,
  2172. loadopt,
  2173. mapper,
  2174. result,
  2175. adapter,
  2176. populators,
  2177. )
  2178. def _create_collection_loader(self, context, key, _instance, populators):
  2179. def load_collection_from_joined_new_row(state, dict_, row):
  2180. # note this must unconditionally clear out any existing collection.
  2181. # an existing collection would be present only in the case of
  2182. # populate_existing().
  2183. collection = attributes.init_state_collection(state, dict_, key)
  2184. result_list = util.UniqueAppender(
  2185. collection, "append_without_event"
  2186. )
  2187. context.attributes[(state, key)] = result_list
  2188. inst = _instance(row)
  2189. if inst is not None:
  2190. result_list.append(inst)
  2191. def load_collection_from_joined_existing_row(state, dict_, row):
  2192. if (state, key) in context.attributes:
  2193. result_list = context.attributes[(state, key)]
  2194. else:
  2195. # appender_key can be absent from context.attributes
  2196. # with isnew=False when self-referential eager loading
  2197. # is used; the same instance may be present in two
  2198. # distinct sets of result columns
  2199. collection = attributes.init_state_collection(
  2200. state, dict_, key
  2201. )
  2202. result_list = util.UniqueAppender(
  2203. collection, "append_without_event"
  2204. )
  2205. context.attributes[(state, key)] = result_list
  2206. inst = _instance(row)
  2207. if inst is not None:
  2208. result_list.append(inst)
  2209. def load_collection_from_joined_exec(state, dict_, row):
  2210. _instance(row)
  2211. populators["new"].append(
  2212. (self.key, load_collection_from_joined_new_row)
  2213. )
  2214. populators["existing"].append(
  2215. (self.key, load_collection_from_joined_existing_row)
  2216. )
  2217. if context.invoke_all_eagers:
  2218. populators["eager"].append(
  2219. (self.key, load_collection_from_joined_exec)
  2220. )
  2221. def _create_scalar_loader(self, context, key, _instance, populators):
  2222. def load_scalar_from_joined_new_row(state, dict_, row):
  2223. # set a scalar object instance directly on the parent
  2224. # object, bypassing InstrumentedAttribute event handlers.
  2225. dict_[key] = _instance(row)
  2226. def load_scalar_from_joined_existing_row(state, dict_, row):
  2227. # call _instance on the row, even though the object has
  2228. # been created, so that we further descend into properties
  2229. existing = _instance(row)
  2230. # conflicting value already loaded, this shouldn't happen
  2231. if key in dict_:
  2232. if existing is not dict_[key]:
  2233. util.warn(
  2234. "Multiple rows returned with "
  2235. "uselist=False for eagerly-loaded attribute '%s' "
  2236. % self
  2237. )
  2238. else:
  2239. # this case is when one row has multiple loads of the
  2240. # same entity (e.g. via aliasing), one has an attribute
  2241. # that the other doesn't.
  2242. dict_[key] = existing
  2243. def load_scalar_from_joined_exec(state, dict_, row):
  2244. _instance(row)
  2245. populators["new"].append((self.key, load_scalar_from_joined_new_row))
  2246. populators["existing"].append(
  2247. (self.key, load_scalar_from_joined_existing_row)
  2248. )
  2249. if context.invoke_all_eagers:
  2250. populators["eager"].append(
  2251. (self.key, load_scalar_from_joined_exec)
  2252. )
  2253. @log.class_logger
  2254. @relationships.RelationshipProperty.strategy_for(lazy="selectin")
  2255. class SelectInLoader(PostLoader, util.MemoizedSlots):
  2256. __slots__ = (
  2257. "join_depth",
  2258. "omit_join",
  2259. "_parent_alias",
  2260. "_query_info",
  2261. "_fallback_query_info",
  2262. )
  2263. query_info = collections.namedtuple(
  2264. "queryinfo",
  2265. [
  2266. "load_only_child",
  2267. "load_with_join",
  2268. "in_expr",
  2269. "pk_cols",
  2270. "zero_idx",
  2271. "child_lookup_cols",
  2272. ],
  2273. )
  2274. _chunksize = 500
  2275. def __init__(self, parent, strategy_key):
  2276. super(SelectInLoader, self).__init__(parent, strategy_key)
  2277. self.join_depth = self.parent_property.join_depth
  2278. is_m2o = self.parent_property.direction is interfaces.MANYTOONE
  2279. if self.parent_property.omit_join is not None:
  2280. self.omit_join = self.parent_property.omit_join
  2281. else:
  2282. lazyloader = self.parent_property._get_strategy(
  2283. (("lazy", "select"),)
  2284. )
  2285. if is_m2o:
  2286. self.omit_join = lazyloader.use_get
  2287. else:
  2288. self.omit_join = self.parent._get_clause[0].compare(
  2289. lazyloader._rev_lazywhere,
  2290. use_proxies=True,
  2291. compare_keys=False,
  2292. equivalents=self.parent._equivalent_columns,
  2293. )
  2294. if self.omit_join:
  2295. if is_m2o:
  2296. self._query_info = self._init_for_omit_join_m2o()
  2297. self._fallback_query_info = self._init_for_join()
  2298. else:
  2299. self._query_info = self._init_for_omit_join()
  2300. else:
  2301. self._query_info = self._init_for_join()
  2302. def _init_for_omit_join(self):
  2303. pk_to_fk = dict(
  2304. self.parent_property._join_condition.local_remote_pairs
  2305. )
  2306. pk_to_fk.update(
  2307. (equiv, pk_to_fk[k])
  2308. for k in list(pk_to_fk)
  2309. for equiv in self.parent._equivalent_columns.get(k, ())
  2310. )
  2311. pk_cols = fk_cols = [
  2312. pk_to_fk[col] for col in self.parent.primary_key if col in pk_to_fk
  2313. ]
  2314. if len(fk_cols) > 1:
  2315. in_expr = sql.tuple_(*fk_cols)
  2316. zero_idx = False
  2317. else:
  2318. in_expr = fk_cols[0]
  2319. zero_idx = True
  2320. return self.query_info(False, False, in_expr, pk_cols, zero_idx, None)
  2321. def _init_for_omit_join_m2o(self):
  2322. pk_cols = self.mapper.primary_key
  2323. if len(pk_cols) > 1:
  2324. in_expr = sql.tuple_(*pk_cols)
  2325. zero_idx = False
  2326. else:
  2327. in_expr = pk_cols[0]
  2328. zero_idx = True
  2329. lazyloader = self.parent_property._get_strategy((("lazy", "select"),))
  2330. lookup_cols = [lazyloader._equated_columns[pk] for pk in pk_cols]
  2331. return self.query_info(
  2332. True, False, in_expr, pk_cols, zero_idx, lookup_cols
  2333. )
  2334. def _init_for_join(self):
  2335. self._parent_alias = aliased(self.parent.class_)
  2336. pa_insp = inspect(self._parent_alias)
  2337. pk_cols = [
  2338. pa_insp._adapt_element(col) for col in self.parent.primary_key
  2339. ]
  2340. if len(pk_cols) > 1:
  2341. in_expr = sql.tuple_(*pk_cols)
  2342. zero_idx = False
  2343. else:
  2344. in_expr = pk_cols[0]
  2345. zero_idx = True
  2346. return self.query_info(False, True, in_expr, pk_cols, zero_idx, None)
  2347. def init_class_attribute(self, mapper):
  2348. self.parent_property._get_strategy(
  2349. (("lazy", "select"),)
  2350. ).init_class_attribute(mapper)
  2351. def create_row_processor(
  2352. self,
  2353. context,
  2354. query_entity,
  2355. path,
  2356. loadopt,
  2357. mapper,
  2358. result,
  2359. adapter,
  2360. populators,
  2361. ):
  2362. if context.refresh_state:
  2363. return self._immediateload_create_row_processor(
  2364. context,
  2365. query_entity,
  2366. path,
  2367. loadopt,
  2368. mapper,
  2369. result,
  2370. adapter,
  2371. populators,
  2372. )
  2373. elif self._check_recursive_postload(context, path, self.join_depth):
  2374. return
  2375. if not self.parent.class_manager[self.key].impl.supports_population:
  2376. raise sa_exc.InvalidRequestError(
  2377. "'%s' does not support object "
  2378. "population - eager loading cannot be applied." % self
  2379. )
  2380. # a little dance here as the "path" is still something that only
  2381. # semi-tracks the exact series of things we are loading, still not
  2382. # telling us about with_polymorphic() and stuff like that when it's at
  2383. # the root.. the initial MapperEntity is more accurate for this case.
  2384. if len(path) == 1:
  2385. if not orm_util._entity_isa(query_entity.entity_zero, self.parent):
  2386. return
  2387. elif not orm_util._entity_isa(path[-1], self.parent):
  2388. return
  2389. selectin_path = (
  2390. context.compile_state.current_path or orm_util.PathRegistry.root
  2391. ) + path
  2392. path_w_prop = path[self.parent_property]
  2393. # build up a path indicating the path from the leftmost
  2394. # entity to the thing we're subquery loading.
  2395. with_poly_entity = path_w_prop.get(
  2396. context.attributes, "path_with_polymorphic", None
  2397. )
  2398. if with_poly_entity is not None:
  2399. effective_entity = inspect(with_poly_entity)
  2400. else:
  2401. effective_entity = self.entity
  2402. loading.PostLoad.callable_for_path(
  2403. context,
  2404. selectin_path,
  2405. self.parent,
  2406. self.parent_property,
  2407. self._load_for_path,
  2408. effective_entity,
  2409. loadopt,
  2410. )
  2411. def _load_for_path(
  2412. self, context, path, states, load_only, effective_entity, loadopt
  2413. ):
  2414. if load_only and self.key not in load_only:
  2415. return
  2416. query_info = self._query_info
  2417. if query_info.load_only_child:
  2418. our_states = collections.defaultdict(list)
  2419. none_states = []
  2420. mapper = self.parent
  2421. for state, overwrite in states:
  2422. state_dict = state.dict
  2423. related_ident = tuple(
  2424. mapper._get_state_attr_by_column(
  2425. state,
  2426. state_dict,
  2427. lk,
  2428. passive=attributes.PASSIVE_NO_FETCH,
  2429. )
  2430. for lk in query_info.child_lookup_cols
  2431. )
  2432. # if the loaded parent objects do not have the foreign key
  2433. # to the related item loaded, then degrade into the joined
  2434. # version of selectinload
  2435. if attributes.PASSIVE_NO_RESULT in related_ident:
  2436. query_info = self._fallback_query_info
  2437. break
  2438. # organize states into lists keyed to particular foreign
  2439. # key values.
  2440. if None not in related_ident:
  2441. our_states[related_ident].append(
  2442. (state, state_dict, overwrite)
  2443. )
  2444. else:
  2445. # For FK values that have None, add them to a
  2446. # separate collection that will be populated separately
  2447. none_states.append((state, state_dict, overwrite))
  2448. # note the above conditional may have changed query_info
  2449. if not query_info.load_only_child:
  2450. our_states = [
  2451. (state.key[1], state, state.dict, overwrite)
  2452. for state, overwrite in states
  2453. ]
  2454. pk_cols = query_info.pk_cols
  2455. in_expr = query_info.in_expr
  2456. if not query_info.load_with_join:
  2457. # in "omit join" mode, the primary key column and the
  2458. # "in" expression are in terms of the related entity. So
  2459. # if the related entity is polymorphic or otherwise aliased,
  2460. # we need to adapt our "pk_cols" and "in_expr" to that
  2461. # entity. in non-"omit join" mode, these are against the
  2462. # parent entity and do not need adaption.
  2463. if effective_entity.is_aliased_class:
  2464. pk_cols = [
  2465. effective_entity._adapt_element(col) for col in pk_cols
  2466. ]
  2467. in_expr = effective_entity._adapt_element(in_expr)
  2468. bundle_ent = orm_util.Bundle("pk", *pk_cols)
  2469. bundle_sql = bundle_ent.__clause_element__()
  2470. entity_sql = effective_entity.__clause_element__()
  2471. q = Select._create_raw_select(
  2472. _raw_columns=[bundle_sql, entity_sql],
  2473. _label_style=LABEL_STYLE_TABLENAME_PLUS_COL,
  2474. _compile_options=ORMCompileState.default_compile_options,
  2475. _propagate_attrs={
  2476. "compile_state_plugin": "orm",
  2477. "plugin_subject": effective_entity,
  2478. },
  2479. )
  2480. if not query_info.load_with_join:
  2481. # the Bundle we have in the "omit_join" case is against raw, non
  2482. # annotated columns, so to ensure the Query knows its primary
  2483. # entity, we add it explicitly. If we made the Bundle against
  2484. # annotated columns, we hit a performance issue in this specific
  2485. # case, which is detailed in issue #4347.
  2486. q = q.select_from(effective_entity)
  2487. else:
  2488. # in the non-omit_join case, the Bundle is against the annotated/
  2489. # mapped column of the parent entity, but the #4347 issue does not
  2490. # occur in this case.
  2491. q = q.select_from(self._parent_alias).join(
  2492. getattr(self._parent_alias, self.parent_property.key).of_type(
  2493. effective_entity
  2494. )
  2495. )
  2496. q = q.filter(in_expr.in_(sql.bindparam("primary_keys")))
  2497. # a test which exercises what these comments talk about is
  2498. # test_selectin_relations.py -> test_twolevel_selectin_w_polymorphic
  2499. #
  2500. # effective_entity above is given to us in terms of the cached
  2501. # statement, namely this one:
  2502. orig_query = context.compile_state.select_statement
  2503. # the actual statement that was requested is this one:
  2504. # context_query = context.query
  2505. #
  2506. # that's not the cached one, however. So while it is of the identical
  2507. # structure, if it has entities like AliasedInsp, which we get from
  2508. # aliased() or with_polymorphic(), the AliasedInsp will likely be a
  2509. # different object identity each time, and will not match up
  2510. # hashing-wise to the corresponding AliasedInsp that's in the
  2511. # cached query, meaning it won't match on paths and loader lookups
  2512. # and loaders like this one will be skipped if it is used in options.
  2513. #
  2514. # Now we want to transfer loader options from the parent query to the
  2515. # "selectinload" query we're about to run. Which query do we transfer
  2516. # the options from? We use the cached query, because the options in
  2517. # that query will be in terms of the effective entity we were just
  2518. # handed.
  2519. #
  2520. # But now the selectinload query we are running is *also*
  2521. # cached. What if it's cached and running from some previous iteration
  2522. # of that AliasedInsp? Well in that case it will also use the previous
  2523. # iteration of the loader options. If the query expires and
  2524. # gets generated again, it will be handed the current effective_entity
  2525. # and the current _with_options, again in terms of whatever
  2526. # compile_state.select_statement happens to be right now, so the
  2527. # query will still be internally consistent and loader callables
  2528. # will be correctly invoked.
  2529. effective_path = path[self.parent_property]
  2530. if orig_query is context.query:
  2531. options = new_options = orig_query._with_options
  2532. user_defined_options = []
  2533. else:
  2534. options = orig_query._with_options
  2535. # propagate compile state options from the original query,
  2536. # updating their "extra_criteria" as necessary.
  2537. # note this will create a different cache key than
  2538. # "orig" options if extra_criteria is present, because the copy
  2539. # of extra_criteria will have different boundparam than that of
  2540. # the QueryableAttribute in the path
  2541. new_options = [
  2542. orig_opt._adjust_for_extra_criteria(context)
  2543. if orig_opt._is_strategy_option
  2544. else orig_opt
  2545. for orig_opt in options
  2546. if orig_opt._is_compile_state or orig_opt._is_legacy_option
  2547. ]
  2548. # propagate user defined options from the current query
  2549. user_defined_options = [
  2550. opt
  2551. for opt in context.query._with_options
  2552. if not opt._is_compile_state and not opt._is_legacy_option
  2553. ]
  2554. if loadopt and loadopt._extra_criteria:
  2555. new_options += (
  2556. orm_util.LoaderCriteriaOption(
  2557. effective_entity,
  2558. loadopt._generate_extra_criteria(context),
  2559. ),
  2560. )
  2561. q = q.options(*new_options)._update_compile_options(
  2562. {"_current_path": effective_path}
  2563. )
  2564. if user_defined_options:
  2565. q = q.options(*user_defined_options)
  2566. if context.populate_existing:
  2567. q = q.execution_options(populate_existing=True)
  2568. if self.parent_property.order_by:
  2569. if not query_info.load_with_join:
  2570. eager_order_by = self.parent_property.order_by
  2571. if effective_entity.is_aliased_class:
  2572. eager_order_by = [
  2573. effective_entity._adapt_element(elem)
  2574. for elem in eager_order_by
  2575. ]
  2576. q = q.order_by(*eager_order_by)
  2577. else:
  2578. def _setup_outermost_orderby(compile_context):
  2579. compile_context.eager_order_by += tuple(
  2580. util.to_list(self.parent_property.order_by)
  2581. )
  2582. q = q._add_context_option(
  2583. _setup_outermost_orderby, self.parent_property
  2584. )
  2585. if query_info.load_only_child:
  2586. self._load_via_child(
  2587. our_states, none_states, query_info, q, context
  2588. )
  2589. else:
  2590. self._load_via_parent(our_states, query_info, q, context)
  2591. def _load_via_child(self, our_states, none_states, query_info, q, context):
  2592. uselist = self.uselist
  2593. # this sort is really for the benefit of the unit tests
  2594. our_keys = sorted(our_states)
  2595. while our_keys:
  2596. chunk = our_keys[0 : self._chunksize]
  2597. our_keys = our_keys[self._chunksize :]
  2598. data = {
  2599. k: v
  2600. for k, v in context.session.execute(
  2601. q,
  2602. params={
  2603. "primary_keys": [
  2604. key[0] if query_info.zero_idx else key
  2605. for key in chunk
  2606. ]
  2607. },
  2608. ).unique()
  2609. }
  2610. for key in chunk:
  2611. # for a real foreign key and no concurrent changes to the
  2612. # DB while running this method, "key" is always present in
  2613. # data. However, for primaryjoins without real foreign keys
  2614. # a non-None primaryjoin condition may still refer to no
  2615. # related object.
  2616. related_obj = data.get(key, None)
  2617. for state, dict_, overwrite in our_states[key]:
  2618. if not overwrite and self.key in dict_:
  2619. continue
  2620. state.get_impl(self.key).set_committed_value(
  2621. state,
  2622. dict_,
  2623. related_obj if not uselist else [related_obj],
  2624. )
  2625. # populate none states with empty value / collection
  2626. for state, dict_, overwrite in none_states:
  2627. if not overwrite and self.key in dict_:
  2628. continue
  2629. # note it's OK if this is a uselist=True attribute, the empty
  2630. # collection will be populated
  2631. state.get_impl(self.key).set_committed_value(state, dict_, None)
  2632. def _load_via_parent(self, our_states, query_info, q, context):
  2633. uselist = self.uselist
  2634. _empty_result = () if uselist else None
  2635. while our_states:
  2636. chunk = our_states[0 : self._chunksize]
  2637. our_states = our_states[self._chunksize :]
  2638. primary_keys = [
  2639. key[0] if query_info.zero_idx else key
  2640. for key, state, state_dict, overwrite in chunk
  2641. ]
  2642. data = collections.defaultdict(list)
  2643. for k, v in itertools.groupby(
  2644. context.session.execute(
  2645. q, params={"primary_keys": primary_keys}
  2646. ).unique(),
  2647. lambda x: x[0],
  2648. ):
  2649. data[k].extend(vv[1] for vv in v)
  2650. for key, state, state_dict, overwrite in chunk:
  2651. if not overwrite and self.key in state_dict:
  2652. continue
  2653. collection = data.get(key, _empty_result)
  2654. if not uselist and collection:
  2655. if len(collection) > 1:
  2656. util.warn(
  2657. "Multiple rows returned with "
  2658. "uselist=False for eagerly-loaded "
  2659. "attribute '%s' " % self
  2660. )
  2661. state.get_impl(self.key).set_committed_value(
  2662. state, state_dict, collection[0]
  2663. )
  2664. else:
  2665. # note that empty tuple set on uselist=False sets the
  2666. # value to None
  2667. state.get_impl(self.key).set_committed_value(
  2668. state, state_dict, collection
  2669. )
  2670. def single_parent_validator(desc, prop):
  2671. def _do_check(state, value, oldvalue, initiator):
  2672. if value is not None and initiator.key == prop.key:
  2673. hasparent = initiator.hasparent(attributes.instance_state(value))
  2674. if hasparent and oldvalue is not value:
  2675. raise sa_exc.InvalidRequestError(
  2676. "Instance %s is already associated with an instance "
  2677. "of %s via its %s attribute, and is only allowed a "
  2678. "single parent."
  2679. % (orm_util.instance_str(value), state.class_, prop),
  2680. code="bbf1",
  2681. )
  2682. return value
  2683. def append(state, value, initiator):
  2684. return _do_check(state, value, None, initiator)
  2685. def set_(state, value, oldvalue, initiator):
  2686. return _do_check(state, value, oldvalue, initiator)
  2687. event.listen(
  2688. desc, "append", append, raw=True, retval=True, active_history=True
  2689. )
  2690. event.listen(desc, "set", set_, raw=True, retval=True, active_history=True)