traversals.py 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. # sql/traversals.py
  2. # Copyright (C) 2005-2024 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. # mypy: allow-untyped-defs, allow-untyped-calls
  8. from __future__ import annotations
  9. from collections import deque
  10. import collections.abc as collections_abc
  11. import itertools
  12. from itertools import zip_longest
  13. import operator
  14. import typing
  15. from typing import Any
  16. from typing import Callable
  17. from typing import Deque
  18. from typing import Dict
  19. from typing import Iterable
  20. from typing import Optional
  21. from typing import Set
  22. from typing import Tuple
  23. from typing import Type
  24. from . import operators
  25. from .cache_key import HasCacheKey
  26. from .visitors import _TraverseInternalsType
  27. from .visitors import anon_map
  28. from .visitors import ExternallyTraversible
  29. from .visitors import HasTraversalDispatch
  30. from .visitors import HasTraverseInternals
  31. from .. import util
  32. from ..util import langhelpers
  33. from ..util.typing import Self
  34. SKIP_TRAVERSE = util.symbol("skip_traverse")
  35. COMPARE_FAILED = False
  36. COMPARE_SUCCEEDED = True
  37. def compare(obj1: Any, obj2: Any, **kw: Any) -> bool:
  38. strategy: TraversalComparatorStrategy
  39. if kw.get("use_proxies", False):
  40. strategy = ColIdentityComparatorStrategy()
  41. else:
  42. strategy = TraversalComparatorStrategy()
  43. return strategy.compare(obj1, obj2, **kw)
  44. def _preconfigure_traversals(target_hierarchy: Type[Any]) -> None:
  45. for cls in util.walk_subclasses(target_hierarchy):
  46. if hasattr(cls, "_generate_cache_attrs") and hasattr(
  47. cls, "_traverse_internals"
  48. ):
  49. cls._generate_cache_attrs()
  50. _copy_internals.generate_dispatch(
  51. cls,
  52. cls._traverse_internals,
  53. "_generated_copy_internals_traversal",
  54. )
  55. _get_children.generate_dispatch(
  56. cls,
  57. cls._traverse_internals,
  58. "_generated_get_children_traversal",
  59. )
  60. class HasShallowCopy(HasTraverseInternals):
  61. """attribute-wide operations that are useful for classes that use
  62. __slots__ and therefore can't operate on their attributes in a dictionary.
  63. """
  64. __slots__ = ()
  65. if typing.TYPE_CHECKING:
  66. def _generated_shallow_copy_traversal(self, other: Self) -> None:
  67. ...
  68. def _generated_shallow_from_dict_traversal(
  69. self, d: Dict[str, Any]
  70. ) -> None:
  71. ...
  72. def _generated_shallow_to_dict_traversal(self) -> Dict[str, Any]:
  73. ...
  74. @classmethod
  75. def _generate_shallow_copy(
  76. cls,
  77. internal_dispatch: _TraverseInternalsType,
  78. method_name: str,
  79. ) -> Callable[[Self, Self], None]:
  80. code = "\n".join(
  81. f" other.{attrname} = self.{attrname}"
  82. for attrname, _ in internal_dispatch
  83. )
  84. meth_text = f"def {method_name}(self, other):\n{code}\n"
  85. return langhelpers._exec_code_in_env(meth_text, {}, method_name)
  86. @classmethod
  87. def _generate_shallow_to_dict(
  88. cls,
  89. internal_dispatch: _TraverseInternalsType,
  90. method_name: str,
  91. ) -> Callable[[Self], Dict[str, Any]]:
  92. code = ",\n".join(
  93. f" '{attrname}': self.{attrname}"
  94. for attrname, _ in internal_dispatch
  95. )
  96. meth_text = f"def {method_name}(self):\n return {{{code}}}\n"
  97. return langhelpers._exec_code_in_env(meth_text, {}, method_name)
  98. @classmethod
  99. def _generate_shallow_from_dict(
  100. cls,
  101. internal_dispatch: _TraverseInternalsType,
  102. method_name: str,
  103. ) -> Callable[[Self, Dict[str, Any]], None]:
  104. code = "\n".join(
  105. f" self.{attrname} = d['{attrname}']"
  106. for attrname, _ in internal_dispatch
  107. )
  108. meth_text = f"def {method_name}(self, d):\n{code}\n"
  109. return langhelpers._exec_code_in_env(meth_text, {}, method_name)
  110. def _shallow_from_dict(self, d: Dict[str, Any]) -> None:
  111. cls = self.__class__
  112. shallow_from_dict: Callable[[HasShallowCopy, Dict[str, Any]], None]
  113. try:
  114. shallow_from_dict = cls.__dict__[
  115. "_generated_shallow_from_dict_traversal"
  116. ]
  117. except KeyError:
  118. shallow_from_dict = self._generate_shallow_from_dict(
  119. cls._traverse_internals,
  120. "_generated_shallow_from_dict_traversal",
  121. )
  122. cls._generated_shallow_from_dict_traversal = shallow_from_dict # type: ignore # noqa: E501
  123. shallow_from_dict(self, d)
  124. def _shallow_to_dict(self) -> Dict[str, Any]:
  125. cls = self.__class__
  126. shallow_to_dict: Callable[[HasShallowCopy], Dict[str, Any]]
  127. try:
  128. shallow_to_dict = cls.__dict__[
  129. "_generated_shallow_to_dict_traversal"
  130. ]
  131. except KeyError:
  132. shallow_to_dict = self._generate_shallow_to_dict(
  133. cls._traverse_internals, "_generated_shallow_to_dict_traversal"
  134. )
  135. cls._generated_shallow_to_dict_traversal = shallow_to_dict # type: ignore # noqa: E501
  136. return shallow_to_dict(self)
  137. def _shallow_copy_to(self, other: Self) -> None:
  138. cls = self.__class__
  139. shallow_copy: Callable[[Self, Self], None]
  140. try:
  141. shallow_copy = cls.__dict__["_generated_shallow_copy_traversal"]
  142. except KeyError:
  143. shallow_copy = self._generate_shallow_copy(
  144. cls._traverse_internals, "_generated_shallow_copy_traversal"
  145. )
  146. cls._generated_shallow_copy_traversal = shallow_copy # type: ignore # noqa: E501
  147. shallow_copy(self, other)
  148. def _clone(self, **kw: Any) -> Self:
  149. """Create a shallow copy"""
  150. c = self.__class__.__new__(self.__class__)
  151. self._shallow_copy_to(c)
  152. return c
  153. class GenerativeOnTraversal(HasShallowCopy):
  154. """Supplies Generative behavior but making use of traversals to shallow
  155. copy.
  156. .. seealso::
  157. :class:`sqlalchemy.sql.base.Generative`
  158. """
  159. __slots__ = ()
  160. def _generate(self) -> Self:
  161. cls = self.__class__
  162. s = cls.__new__(cls)
  163. self._shallow_copy_to(s)
  164. return s
  165. def _clone(element, **kw):
  166. return element._clone()
  167. class HasCopyInternals(HasTraverseInternals):
  168. __slots__ = ()
  169. def _clone(self, **kw):
  170. raise NotImplementedError()
  171. def _copy_internals(
  172. self, *, omit_attrs: Iterable[str] = (), **kw: Any
  173. ) -> None:
  174. """Reassign internal elements to be clones of themselves.
  175. Called during a copy-and-traverse operation on newly
  176. shallow-copied elements to create a deep copy.
  177. The given clone function should be used, which may be applying
  178. additional transformations to the element (i.e. replacement
  179. traversal, cloned traversal, annotations).
  180. """
  181. try:
  182. traverse_internals = self._traverse_internals
  183. except AttributeError:
  184. # user-defined classes may not have a _traverse_internals
  185. return
  186. for attrname, obj, meth in _copy_internals.run_generated_dispatch(
  187. self, traverse_internals, "_generated_copy_internals_traversal"
  188. ):
  189. if attrname in omit_attrs:
  190. continue
  191. if obj is not None:
  192. result = meth(attrname, self, obj, **kw)
  193. if result is not None:
  194. setattr(self, attrname, result)
  195. class _CopyInternalsTraversal(HasTraversalDispatch):
  196. """Generate a _copy_internals internal traversal dispatch for classes
  197. with a _traverse_internals collection."""
  198. def visit_clauseelement(
  199. self, attrname, parent, element, clone=_clone, **kw
  200. ):
  201. return clone(element, **kw)
  202. def visit_clauseelement_list(
  203. self, attrname, parent, element, clone=_clone, **kw
  204. ):
  205. return [clone(clause, **kw) for clause in element]
  206. def visit_clauseelement_tuple(
  207. self, attrname, parent, element, clone=_clone, **kw
  208. ):
  209. return tuple([clone(clause, **kw) for clause in element])
  210. def visit_executable_options(
  211. self, attrname, parent, element, clone=_clone, **kw
  212. ):
  213. return tuple([clone(clause, **kw) for clause in element])
  214. def visit_clauseelement_unordered_set(
  215. self, attrname, parent, element, clone=_clone, **kw
  216. ):
  217. return {clone(clause, **kw) for clause in element}
  218. def visit_clauseelement_tuples(
  219. self, attrname, parent, element, clone=_clone, **kw
  220. ):
  221. return [
  222. tuple(clone(tup_elem, **kw) for tup_elem in elem)
  223. for elem in element
  224. ]
  225. def visit_string_clauseelement_dict(
  226. self, attrname, parent, element, clone=_clone, **kw
  227. ):
  228. return {key: clone(value, **kw) for key, value in element.items()}
  229. def visit_setup_join_tuple(
  230. self, attrname, parent, element, clone=_clone, **kw
  231. ):
  232. return tuple(
  233. (
  234. clone(target, **kw) if target is not None else None,
  235. clone(onclause, **kw) if onclause is not None else None,
  236. clone(from_, **kw) if from_ is not None else None,
  237. flags,
  238. )
  239. for (target, onclause, from_, flags) in element
  240. )
  241. def visit_memoized_select_entities(self, attrname, parent, element, **kw):
  242. return self.visit_clauseelement_tuple(attrname, parent, element, **kw)
  243. def visit_dml_ordered_values(
  244. self, attrname, parent, element, clone=_clone, **kw
  245. ):
  246. # sequence of 2-tuples
  247. return [
  248. (
  249. clone(key, **kw)
  250. if hasattr(key, "__clause_element__")
  251. else key,
  252. clone(value, **kw),
  253. )
  254. for key, value in element
  255. ]
  256. def visit_dml_values(self, attrname, parent, element, clone=_clone, **kw):
  257. return {
  258. (
  259. clone(key, **kw) if hasattr(key, "__clause_element__") else key
  260. ): clone(value, **kw)
  261. for key, value in element.items()
  262. }
  263. def visit_dml_multi_values(
  264. self, attrname, parent, element, clone=_clone, **kw
  265. ):
  266. # sequence of sequences, each sequence contains a list/dict/tuple
  267. def copy(elem):
  268. if isinstance(elem, (list, tuple)):
  269. return [
  270. clone(value, **kw)
  271. if hasattr(value, "__clause_element__")
  272. else value
  273. for value in elem
  274. ]
  275. elif isinstance(elem, dict):
  276. return {
  277. (
  278. clone(key, **kw)
  279. if hasattr(key, "__clause_element__")
  280. else key
  281. ): (
  282. clone(value, **kw)
  283. if hasattr(value, "__clause_element__")
  284. else value
  285. )
  286. for key, value in elem.items()
  287. }
  288. else:
  289. # TODO: use abc classes
  290. assert False
  291. return [
  292. [copy(sub_element) for sub_element in sequence]
  293. for sequence in element
  294. ]
  295. def visit_propagate_attrs(
  296. self, attrname, parent, element, clone=_clone, **kw
  297. ):
  298. return element
  299. _copy_internals = _CopyInternalsTraversal()
  300. def _flatten_clauseelement(element):
  301. while hasattr(element, "__clause_element__") and not getattr(
  302. element, "is_clause_element", False
  303. ):
  304. element = element.__clause_element__()
  305. return element
  306. class _GetChildrenTraversal(HasTraversalDispatch):
  307. """Generate a _children_traversal internal traversal dispatch for classes
  308. with a _traverse_internals collection."""
  309. def visit_has_cache_key(self, element, **kw):
  310. # the GetChildren traversal refers explicitly to ClauseElement
  311. # structures. Within these, a plain HasCacheKey is not a
  312. # ClauseElement, so don't include these.
  313. return ()
  314. def visit_clauseelement(self, element, **kw):
  315. return (element,)
  316. def visit_clauseelement_list(self, element, **kw):
  317. return element
  318. def visit_clauseelement_tuple(self, element, **kw):
  319. return element
  320. def visit_clauseelement_tuples(self, element, **kw):
  321. return itertools.chain.from_iterable(element)
  322. def visit_fromclause_canonical_column_collection(self, element, **kw):
  323. return ()
  324. def visit_string_clauseelement_dict(self, element, **kw):
  325. return element.values()
  326. def visit_fromclause_ordered_set(self, element, **kw):
  327. return element
  328. def visit_clauseelement_unordered_set(self, element, **kw):
  329. return element
  330. def visit_setup_join_tuple(self, element, **kw):
  331. for target, onclause, from_, flags in element:
  332. if from_ is not None:
  333. yield from_
  334. if not isinstance(target, str):
  335. yield _flatten_clauseelement(target)
  336. if onclause is not None and not isinstance(onclause, str):
  337. yield _flatten_clauseelement(onclause)
  338. def visit_memoized_select_entities(self, element, **kw):
  339. return self.visit_clauseelement_tuple(element, **kw)
  340. def visit_dml_ordered_values(self, element, **kw):
  341. for k, v in element:
  342. if hasattr(k, "__clause_element__"):
  343. yield k
  344. yield v
  345. def visit_dml_values(self, element, **kw):
  346. expr_values = {k for k in element if hasattr(k, "__clause_element__")}
  347. str_values = expr_values.symmetric_difference(element)
  348. for k in sorted(str_values):
  349. yield element[k]
  350. for k in expr_values:
  351. yield k
  352. yield element[k]
  353. def visit_dml_multi_values(self, element, **kw):
  354. return ()
  355. def visit_propagate_attrs(self, element, **kw):
  356. return ()
  357. _get_children = _GetChildrenTraversal()
  358. @util.preload_module("sqlalchemy.sql.elements")
  359. def _resolve_name_for_compare(element, name, anon_map, **kw):
  360. if isinstance(name, util.preloaded.sql_elements._anonymous_label):
  361. name = name.apply_map(anon_map)
  362. return name
  363. class TraversalComparatorStrategy(HasTraversalDispatch, util.MemoizedSlots):
  364. __slots__ = "stack", "cache", "anon_map"
  365. def __init__(self):
  366. self.stack: Deque[
  367. Tuple[
  368. Optional[ExternallyTraversible],
  369. Optional[ExternallyTraversible],
  370. ]
  371. ] = deque()
  372. self.cache = set()
  373. def _memoized_attr_anon_map(self):
  374. return (anon_map(), anon_map())
  375. def compare(
  376. self,
  377. obj1: ExternallyTraversible,
  378. obj2: ExternallyTraversible,
  379. **kw: Any,
  380. ) -> bool:
  381. stack = self.stack
  382. cache = self.cache
  383. compare_annotations = kw.get("compare_annotations", False)
  384. stack.append((obj1, obj2))
  385. while stack:
  386. left, right = stack.popleft()
  387. if left is right:
  388. continue
  389. elif left is None or right is None:
  390. # we know they are different so no match
  391. return False
  392. elif (left, right) in cache:
  393. continue
  394. cache.add((left, right))
  395. visit_name = left.__visit_name__
  396. if visit_name != right.__visit_name__:
  397. return False
  398. meth = getattr(self, "compare_%s" % visit_name, None)
  399. if meth:
  400. attributes_compared = meth(left, right, **kw)
  401. if attributes_compared is COMPARE_FAILED:
  402. return False
  403. elif attributes_compared is SKIP_TRAVERSE:
  404. continue
  405. # attributes_compared is returned as a list of attribute
  406. # names that were "handled" by the comparison method above.
  407. # remaining attribute names in the _traverse_internals
  408. # will be compared.
  409. else:
  410. attributes_compared = ()
  411. for (
  412. (left_attrname, left_visit_sym),
  413. (right_attrname, right_visit_sym),
  414. ) in zip_longest(
  415. left._traverse_internals,
  416. right._traverse_internals,
  417. fillvalue=(None, None),
  418. ):
  419. if not compare_annotations and (
  420. (left_attrname == "_annotations")
  421. or (right_attrname == "_annotations")
  422. ):
  423. continue
  424. if (
  425. left_attrname != right_attrname
  426. or left_visit_sym is not right_visit_sym
  427. ):
  428. return False
  429. elif left_attrname in attributes_compared:
  430. continue
  431. assert left_visit_sym is not None
  432. assert left_attrname is not None
  433. assert right_attrname is not None
  434. dispatch = self.dispatch(left_visit_sym)
  435. assert dispatch is not None, (
  436. f"{self.__class__} has no dispatch for "
  437. f"'{self._dispatch_lookup[left_visit_sym]}'"
  438. )
  439. left_child = operator.attrgetter(left_attrname)(left)
  440. right_child = operator.attrgetter(right_attrname)(right)
  441. if left_child is None:
  442. if right_child is not None:
  443. return False
  444. else:
  445. continue
  446. comparison = dispatch(
  447. left_attrname, left, left_child, right, right_child, **kw
  448. )
  449. if comparison is COMPARE_FAILED:
  450. return False
  451. return True
  452. def compare_inner(self, obj1, obj2, **kw):
  453. comparator = self.__class__()
  454. return comparator.compare(obj1, obj2, **kw)
  455. def visit_has_cache_key(
  456. self, attrname, left_parent, left, right_parent, right, **kw
  457. ):
  458. if left._gen_cache_key(self.anon_map[0], []) != right._gen_cache_key(
  459. self.anon_map[1], []
  460. ):
  461. return COMPARE_FAILED
  462. def visit_propagate_attrs(
  463. self, attrname, left_parent, left, right_parent, right, **kw
  464. ):
  465. return self.compare_inner(
  466. left.get("plugin_subject", None), right.get("plugin_subject", None)
  467. )
  468. def visit_has_cache_key_list(
  469. self, attrname, left_parent, left, right_parent, right, **kw
  470. ):
  471. for l, r in zip_longest(left, right, fillvalue=None):
  472. if l is None:
  473. if r is not None:
  474. return COMPARE_FAILED
  475. else:
  476. continue
  477. elif r is None:
  478. return COMPARE_FAILED
  479. if l._gen_cache_key(self.anon_map[0], []) != r._gen_cache_key(
  480. self.anon_map[1], []
  481. ):
  482. return COMPARE_FAILED
  483. def visit_executable_options(
  484. self, attrname, left_parent, left, right_parent, right, **kw
  485. ):
  486. for l, r in zip_longest(left, right, fillvalue=None):
  487. if l is None:
  488. if r is not None:
  489. return COMPARE_FAILED
  490. else:
  491. continue
  492. elif r is None:
  493. return COMPARE_FAILED
  494. if (
  495. l._gen_cache_key(self.anon_map[0], [])
  496. if l._is_has_cache_key
  497. else l
  498. ) != (
  499. r._gen_cache_key(self.anon_map[1], [])
  500. if r._is_has_cache_key
  501. else r
  502. ):
  503. return COMPARE_FAILED
  504. def visit_clauseelement(
  505. self, attrname, left_parent, left, right_parent, right, **kw
  506. ):
  507. self.stack.append((left, right))
  508. def visit_fromclause_canonical_column_collection(
  509. self, attrname, left_parent, left, right_parent, right, **kw
  510. ):
  511. for lcol, rcol in zip_longest(left, right, fillvalue=None):
  512. self.stack.append((lcol, rcol))
  513. def visit_fromclause_derived_column_collection(
  514. self, attrname, left_parent, left, right_parent, right, **kw
  515. ):
  516. pass
  517. def visit_string_clauseelement_dict(
  518. self, attrname, left_parent, left, right_parent, right, **kw
  519. ):
  520. for lstr, rstr in zip_longest(
  521. sorted(left), sorted(right), fillvalue=None
  522. ):
  523. if lstr != rstr:
  524. return COMPARE_FAILED
  525. self.stack.append((left[lstr], right[rstr]))
  526. def visit_clauseelement_tuples(
  527. self, attrname, left_parent, left, right_parent, right, **kw
  528. ):
  529. for ltup, rtup in zip_longest(left, right, fillvalue=None):
  530. if ltup is None or rtup is None:
  531. return COMPARE_FAILED
  532. for l, r in zip_longest(ltup, rtup, fillvalue=None):
  533. self.stack.append((l, r))
  534. def visit_clauseelement_list(
  535. self, attrname, left_parent, left, right_parent, right, **kw
  536. ):
  537. for l, r in zip_longest(left, right, fillvalue=None):
  538. self.stack.append((l, r))
  539. def visit_clauseelement_tuple(
  540. self, attrname, left_parent, left, right_parent, right, **kw
  541. ):
  542. for l, r in zip_longest(left, right, fillvalue=None):
  543. self.stack.append((l, r))
  544. def _compare_unordered_sequences(self, seq1, seq2, **kw):
  545. if seq1 is None:
  546. return seq2 is None
  547. completed: Set[object] = set()
  548. for clause in seq1:
  549. for other_clause in set(seq2).difference(completed):
  550. if self.compare_inner(clause, other_clause, **kw):
  551. completed.add(other_clause)
  552. break
  553. return len(completed) == len(seq1) == len(seq2)
  554. def visit_clauseelement_unordered_set(
  555. self, attrname, left_parent, left, right_parent, right, **kw
  556. ):
  557. return self._compare_unordered_sequences(left, right, **kw)
  558. def visit_fromclause_ordered_set(
  559. self, attrname, left_parent, left, right_parent, right, **kw
  560. ):
  561. for l, r in zip_longest(left, right, fillvalue=None):
  562. self.stack.append((l, r))
  563. def visit_string(
  564. self, attrname, left_parent, left, right_parent, right, **kw
  565. ):
  566. return left == right
  567. def visit_string_list(
  568. self, attrname, left_parent, left, right_parent, right, **kw
  569. ):
  570. return left == right
  571. def visit_string_multi_dict(
  572. self, attrname, left_parent, left, right_parent, right, **kw
  573. ):
  574. for lk, rk in zip_longest(
  575. sorted(left.keys()), sorted(right.keys()), fillvalue=(None, None)
  576. ):
  577. if lk != rk:
  578. return COMPARE_FAILED
  579. lv, rv = left[lk], right[rk]
  580. lhc = isinstance(left, HasCacheKey)
  581. rhc = isinstance(right, HasCacheKey)
  582. if lhc and rhc:
  583. if lv._gen_cache_key(
  584. self.anon_map[0], []
  585. ) != rv._gen_cache_key(self.anon_map[1], []):
  586. return COMPARE_FAILED
  587. elif lhc != rhc:
  588. return COMPARE_FAILED
  589. elif lv != rv:
  590. return COMPARE_FAILED
  591. def visit_multi(
  592. self, attrname, left_parent, left, right_parent, right, **kw
  593. ):
  594. lhc = isinstance(left, HasCacheKey)
  595. rhc = isinstance(right, HasCacheKey)
  596. if lhc and rhc:
  597. if left._gen_cache_key(
  598. self.anon_map[0], []
  599. ) != right._gen_cache_key(self.anon_map[1], []):
  600. return COMPARE_FAILED
  601. elif lhc != rhc:
  602. return COMPARE_FAILED
  603. else:
  604. return left == right
  605. def visit_anon_name(
  606. self, attrname, left_parent, left, right_parent, right, **kw
  607. ):
  608. return _resolve_name_for_compare(
  609. left_parent, left, self.anon_map[0], **kw
  610. ) == _resolve_name_for_compare(
  611. right_parent, right, self.anon_map[1], **kw
  612. )
  613. def visit_boolean(
  614. self, attrname, left_parent, left, right_parent, right, **kw
  615. ):
  616. return left == right
  617. def visit_operator(
  618. self, attrname, left_parent, left, right_parent, right, **kw
  619. ):
  620. return left == right
  621. def visit_type(
  622. self, attrname, left_parent, left, right_parent, right, **kw
  623. ):
  624. return left._compare_type_affinity(right)
  625. def visit_plain_dict(
  626. self, attrname, left_parent, left, right_parent, right, **kw
  627. ):
  628. return left == right
  629. def visit_dialect_options(
  630. self, attrname, left_parent, left, right_parent, right, **kw
  631. ):
  632. return left == right
  633. def visit_annotations_key(
  634. self, attrname, left_parent, left, right_parent, right, **kw
  635. ):
  636. if left and right:
  637. return (
  638. left_parent._annotations_cache_key
  639. == right_parent._annotations_cache_key
  640. )
  641. else:
  642. return left == right
  643. def visit_with_context_options(
  644. self, attrname, left_parent, left, right_parent, right, **kw
  645. ):
  646. return tuple((fn.__code__, c_key) for fn, c_key in left) == tuple(
  647. (fn.__code__, c_key) for fn, c_key in right
  648. )
  649. def visit_plain_obj(
  650. self, attrname, left_parent, left, right_parent, right, **kw
  651. ):
  652. return left == right
  653. def visit_named_ddl_element(
  654. self, attrname, left_parent, left, right_parent, right, **kw
  655. ):
  656. if left is None:
  657. if right is not None:
  658. return COMPARE_FAILED
  659. return left.name == right.name
  660. def visit_prefix_sequence(
  661. self, attrname, left_parent, left, right_parent, right, **kw
  662. ):
  663. for (l_clause, l_str), (r_clause, r_str) in zip_longest(
  664. left, right, fillvalue=(None, None)
  665. ):
  666. if l_str != r_str:
  667. return COMPARE_FAILED
  668. else:
  669. self.stack.append((l_clause, r_clause))
  670. def visit_setup_join_tuple(
  671. self, attrname, left_parent, left, right_parent, right, **kw
  672. ):
  673. # TODO: look at attrname for "legacy_join" and use different structure
  674. for (
  675. (l_target, l_onclause, l_from, l_flags),
  676. (r_target, r_onclause, r_from, r_flags),
  677. ) in zip_longest(left, right, fillvalue=(None, None, None, None)):
  678. if l_flags != r_flags:
  679. return COMPARE_FAILED
  680. self.stack.append((l_target, r_target))
  681. self.stack.append((l_onclause, r_onclause))
  682. self.stack.append((l_from, r_from))
  683. def visit_memoized_select_entities(
  684. self, attrname, left_parent, left, right_parent, right, **kw
  685. ):
  686. return self.visit_clauseelement_tuple(
  687. attrname, left_parent, left, right_parent, right, **kw
  688. )
  689. def visit_table_hint_list(
  690. self, attrname, left_parent, left, right_parent, right, **kw
  691. ):
  692. left_keys = sorted(left, key=lambda elem: (elem[0].fullname, elem[1]))
  693. right_keys = sorted(
  694. right, key=lambda elem: (elem[0].fullname, elem[1])
  695. )
  696. for (ltable, ldialect), (rtable, rdialect) in zip_longest(
  697. left_keys, right_keys, fillvalue=(None, None)
  698. ):
  699. if ldialect != rdialect:
  700. return COMPARE_FAILED
  701. elif left[(ltable, ldialect)] != right[(rtable, rdialect)]:
  702. return COMPARE_FAILED
  703. else:
  704. self.stack.append((ltable, rtable))
  705. def visit_statement_hint_list(
  706. self, attrname, left_parent, left, right_parent, right, **kw
  707. ):
  708. return left == right
  709. def visit_unknown_structure(
  710. self, attrname, left_parent, left, right_parent, right, **kw
  711. ):
  712. raise NotImplementedError()
  713. def visit_dml_ordered_values(
  714. self, attrname, left_parent, left, right_parent, right, **kw
  715. ):
  716. # sequence of tuple pairs
  717. for (lk, lv), (rk, rv) in zip_longest(
  718. left, right, fillvalue=(None, None)
  719. ):
  720. if not self._compare_dml_values_or_ce(lk, rk, **kw):
  721. return COMPARE_FAILED
  722. def _compare_dml_values_or_ce(self, lv, rv, **kw):
  723. lvce = hasattr(lv, "__clause_element__")
  724. rvce = hasattr(rv, "__clause_element__")
  725. if lvce != rvce:
  726. return False
  727. elif lvce and not self.compare_inner(lv, rv, **kw):
  728. return False
  729. elif not lvce and lv != rv:
  730. return False
  731. elif not self.compare_inner(lv, rv, **kw):
  732. return False
  733. return True
  734. def visit_dml_values(
  735. self, attrname, left_parent, left, right_parent, right, **kw
  736. ):
  737. if left is None or right is None or len(left) != len(right):
  738. return COMPARE_FAILED
  739. if isinstance(left, collections_abc.Sequence):
  740. for lv, rv in zip(left, right):
  741. if not self._compare_dml_values_or_ce(lv, rv, **kw):
  742. return COMPARE_FAILED
  743. elif isinstance(right, collections_abc.Sequence):
  744. return COMPARE_FAILED
  745. else:
  746. # dictionaries guaranteed to support insert ordering in
  747. # py37 so that we can compare the keys in order. without
  748. # this, we can't compare SQL expression keys because we don't
  749. # know which key is which
  750. for (lk, lv), (rk, rv) in zip(left.items(), right.items()):
  751. if not self._compare_dml_values_or_ce(lk, rk, **kw):
  752. return COMPARE_FAILED
  753. if not self._compare_dml_values_or_ce(lv, rv, **kw):
  754. return COMPARE_FAILED
  755. def visit_dml_multi_values(
  756. self, attrname, left_parent, left, right_parent, right, **kw
  757. ):
  758. for lseq, rseq in zip_longest(left, right, fillvalue=None):
  759. if lseq is None or rseq is None:
  760. return COMPARE_FAILED
  761. for ld, rd in zip_longest(lseq, rseq, fillvalue=None):
  762. if (
  763. self.visit_dml_values(
  764. attrname, left_parent, ld, right_parent, rd, **kw
  765. )
  766. is COMPARE_FAILED
  767. ):
  768. return COMPARE_FAILED
  769. def compare_expression_clauselist(self, left, right, **kw):
  770. if left.operator is right.operator:
  771. if operators.is_associative(left.operator):
  772. if self._compare_unordered_sequences(
  773. left.clauses, right.clauses, **kw
  774. ):
  775. return ["operator", "clauses"]
  776. else:
  777. return COMPARE_FAILED
  778. else:
  779. return ["operator"]
  780. else:
  781. return COMPARE_FAILED
  782. def compare_clauselist(self, left, right, **kw):
  783. return self.compare_expression_clauselist(left, right, **kw)
  784. def compare_binary(self, left, right, **kw):
  785. if left.operator == right.operator:
  786. if operators.is_commutative(left.operator):
  787. if (
  788. self.compare_inner(left.left, right.left, **kw)
  789. and self.compare_inner(left.right, right.right, **kw)
  790. ) or (
  791. self.compare_inner(left.left, right.right, **kw)
  792. and self.compare_inner(left.right, right.left, **kw)
  793. ):
  794. return ["operator", "negate", "left", "right"]
  795. else:
  796. return COMPARE_FAILED
  797. else:
  798. return ["operator", "negate"]
  799. else:
  800. return COMPARE_FAILED
  801. def compare_bindparam(self, left, right, **kw):
  802. compare_keys = kw.pop("compare_keys", True)
  803. compare_values = kw.pop("compare_values", True)
  804. if compare_values:
  805. omit = []
  806. else:
  807. # this means, "skip these, we already compared"
  808. omit = ["callable", "value"]
  809. if not compare_keys:
  810. omit.append("key")
  811. return omit
  812. class ColIdentityComparatorStrategy(TraversalComparatorStrategy):
  813. def compare_column_element(
  814. self, left, right, use_proxies=True, equivalents=(), **kw
  815. ):
  816. """Compare ColumnElements using proxies and equivalent collections.
  817. This is a comparison strategy specific to the ORM.
  818. """
  819. to_compare = (right,)
  820. if equivalents and right in equivalents:
  821. to_compare = equivalents[right].union(to_compare)
  822. for oth in to_compare:
  823. if use_proxies and left.shares_lineage(oth):
  824. return SKIP_TRAVERSE
  825. elif hash(left) == hash(right):
  826. return SKIP_TRAVERSE
  827. else:
  828. return COMPARE_FAILED
  829. def compare_column(self, left, right, **kw):
  830. return self.compare_column_element(left, right, **kw)
  831. def compare_label(self, left, right, **kw):
  832. return self.compare_column_element(left, right, **kw)
  833. def compare_table(self, left, right, **kw):
  834. # tables compare on identity, since it's not really feasible to
  835. # compare them column by column with the above rules
  836. return SKIP_TRAVERSE if left is right else COMPARE_FAILED