util.py 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. # sql/util.py
  2. # Copyright (C) 2005-2025 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. """High level utilities which build upon other modules here."""
  9. from __future__ import annotations
  10. from collections import deque
  11. import copy
  12. from itertools import chain
  13. import typing
  14. from typing import AbstractSet
  15. from typing import Any
  16. from typing import Callable
  17. from typing import cast
  18. from typing import Collection
  19. from typing import Dict
  20. from typing import Iterable
  21. from typing import Iterator
  22. from typing import List
  23. from typing import Optional
  24. from typing import overload
  25. from typing import Sequence
  26. from typing import Tuple
  27. from typing import TYPE_CHECKING
  28. from typing import TypeVar
  29. from typing import Union
  30. from . import coercions
  31. from . import operators
  32. from . import roles
  33. from . import visitors
  34. from ._typing import is_text_clause
  35. from .annotation import _deep_annotate as _deep_annotate # noqa: F401
  36. from .annotation import _deep_deannotate as _deep_deannotate # noqa: F401
  37. from .annotation import _shallow_annotate as _shallow_annotate # noqa: F401
  38. from .base import _expand_cloned
  39. from .base import _from_objects
  40. from .cache_key import HasCacheKey as HasCacheKey # noqa: F401
  41. from .ddl import sort_tables as sort_tables # noqa: F401
  42. from .elements import _find_columns as _find_columns
  43. from .elements import _label_reference
  44. from .elements import _textual_label_reference
  45. from .elements import BindParameter
  46. from .elements import ClauseElement
  47. from .elements import ColumnClause
  48. from .elements import ColumnElement
  49. from .elements import Grouping
  50. from .elements import KeyedColumnElement
  51. from .elements import Label
  52. from .elements import NamedColumn
  53. from .elements import Null
  54. from .elements import UnaryExpression
  55. from .schema import Column
  56. from .selectable import Alias
  57. from .selectable import FromClause
  58. from .selectable import FromGrouping
  59. from .selectable import Join
  60. from .selectable import ScalarSelect
  61. from .selectable import SelectBase
  62. from .selectable import TableClause
  63. from .visitors import _ET
  64. from .. import exc
  65. from .. import util
  66. from ..util.typing import Literal
  67. from ..util.typing import Protocol
  68. if typing.TYPE_CHECKING:
  69. from ._typing import _EquivalentColumnMap
  70. from ._typing import _LimitOffsetType
  71. from ._typing import _TypeEngineArgument
  72. from .elements import BinaryExpression
  73. from .elements import TextClause
  74. from .selectable import _JoinTargetElement
  75. from .selectable import _SelectIterable
  76. from .selectable import Selectable
  77. from .visitors import _TraverseCallableType
  78. from .visitors import ExternallyTraversible
  79. from .visitors import ExternalTraversal
  80. from ..engine.interfaces import _AnyExecuteParams
  81. from ..engine.interfaces import _AnyMultiExecuteParams
  82. from ..engine.interfaces import _AnySingleExecuteParams
  83. from ..engine.interfaces import _CoreSingleExecuteParams
  84. from ..engine.row import Row
  85. _CE = TypeVar("_CE", bound="ColumnElement[Any]")
  86. def join_condition(
  87. a: FromClause,
  88. b: FromClause,
  89. a_subset: Optional[FromClause] = None,
  90. consider_as_foreign_keys: Optional[AbstractSet[ColumnClause[Any]]] = None,
  91. ) -> ColumnElement[bool]:
  92. """Create a join condition between two tables or selectables.
  93. e.g.::
  94. join_condition(tablea, tableb)
  95. would produce an expression along the lines of::
  96. tablea.c.id == tableb.c.tablea_id
  97. The join is determined based on the foreign key relationships
  98. between the two selectables. If there are multiple ways
  99. to join, or no way to join, an error is raised.
  100. :param a_subset: An optional expression that is a sub-component
  101. of ``a``. An attempt will be made to join to just this sub-component
  102. first before looking at the full ``a`` construct, and if found
  103. will be successful even if there are other ways to join to ``a``.
  104. This allows the "right side" of a join to be passed thereby
  105. providing a "natural join".
  106. """
  107. return Join._join_condition(
  108. a,
  109. b,
  110. a_subset=a_subset,
  111. consider_as_foreign_keys=consider_as_foreign_keys,
  112. )
  113. def find_join_source(
  114. clauses: List[FromClause], join_to: FromClause
  115. ) -> List[int]:
  116. """Given a list of FROM clauses and a selectable,
  117. return the first index and element from the list of
  118. clauses which can be joined against the selectable. returns
  119. None, None if no match is found.
  120. e.g.::
  121. clause1 = table1.join(table2)
  122. clause2 = table4.join(table5)
  123. join_to = table2.join(table3)
  124. find_join_source([clause1, clause2], join_to) == clause1
  125. """
  126. selectables = list(_from_objects(join_to))
  127. idx = []
  128. for i, f in enumerate(clauses):
  129. for s in selectables:
  130. if f.is_derived_from(s):
  131. idx.append(i)
  132. return idx
  133. def find_left_clause_that_matches_given(
  134. clauses: Sequence[FromClause], join_from: FromClause
  135. ) -> List[int]:
  136. """Given a list of FROM clauses and a selectable,
  137. return the indexes from the list of
  138. clauses which is derived from the selectable.
  139. """
  140. selectables = list(_from_objects(join_from))
  141. liberal_idx = []
  142. for i, f in enumerate(clauses):
  143. for s in selectables:
  144. # basic check, if f is derived from s.
  145. # this can be joins containing a table, or an aliased table
  146. # or select statement matching to a table. This check
  147. # will match a table to a selectable that is adapted from
  148. # that table. With Query, this suits the case where a join
  149. # is being made to an adapted entity
  150. if f.is_derived_from(s):
  151. liberal_idx.append(i)
  152. break
  153. # in an extremely small set of use cases, a join is being made where
  154. # there are multiple FROM clauses where our target table is represented
  155. # in more than one, such as embedded or similar. in this case, do
  156. # another pass where we try to get a more exact match where we aren't
  157. # looking at adaption relationships.
  158. if len(liberal_idx) > 1:
  159. conservative_idx = []
  160. for idx in liberal_idx:
  161. f = clauses[idx]
  162. for s in selectables:
  163. if set(surface_selectables(f)).intersection(
  164. surface_selectables(s)
  165. ):
  166. conservative_idx.append(idx)
  167. break
  168. if conservative_idx:
  169. return conservative_idx
  170. return liberal_idx
  171. def find_left_clause_to_join_from(
  172. clauses: Sequence[FromClause],
  173. join_to: _JoinTargetElement,
  174. onclause: Optional[ColumnElement[Any]],
  175. ) -> List[int]:
  176. """Given a list of FROM clauses, a selectable,
  177. and optional ON clause, return a list of integer indexes from the
  178. clauses list indicating the clauses that can be joined from.
  179. The presence of an "onclause" indicates that at least one clause can
  180. definitely be joined from; if the list of clauses is of length one
  181. and the onclause is given, returns that index. If the list of clauses
  182. is more than length one, and the onclause is given, attempts to locate
  183. which clauses contain the same columns.
  184. """
  185. idx = []
  186. selectables = set(_from_objects(join_to))
  187. # if we are given more than one target clause to join
  188. # from, use the onclause to provide a more specific answer.
  189. # otherwise, don't try to limit, after all, "ON TRUE" is a valid
  190. # on clause
  191. if len(clauses) > 1 and onclause is not None:
  192. resolve_ambiguity = True
  193. cols_in_onclause = _find_columns(onclause)
  194. else:
  195. resolve_ambiguity = False
  196. cols_in_onclause = None
  197. for i, f in enumerate(clauses):
  198. for s in selectables.difference([f]):
  199. if resolve_ambiguity:
  200. assert cols_in_onclause is not None
  201. if set(f.c).union(s.c).issuperset(cols_in_onclause):
  202. idx.append(i)
  203. break
  204. elif onclause is not None or Join._can_join(f, s):
  205. idx.append(i)
  206. break
  207. if len(idx) > 1:
  208. # this is the same "hide froms" logic from
  209. # Selectable._get_display_froms
  210. toremove = set(
  211. chain(*[_expand_cloned(f._hide_froms) for f in clauses])
  212. )
  213. idx = [i for i in idx if clauses[i] not in toremove]
  214. # onclause was given and none of them resolved, so assume
  215. # all indexes can match
  216. if not idx and onclause is not None:
  217. return list(range(len(clauses)))
  218. else:
  219. return idx
  220. def visit_binary_product(
  221. fn: Callable[
  222. [BinaryExpression[Any], ColumnElement[Any], ColumnElement[Any]], None
  223. ],
  224. expr: ColumnElement[Any],
  225. ) -> None:
  226. """Produce a traversal of the given expression, delivering
  227. column comparisons to the given function.
  228. The function is of the form::
  229. def my_fn(binary, left, right): ...
  230. For each binary expression located which has a
  231. comparison operator, the product of "left" and
  232. "right" will be delivered to that function,
  233. in terms of that binary.
  234. Hence an expression like::
  235. and_((a + b) == q + func.sum(e + f), j == r)
  236. would have the traversal:
  237. .. sourcecode:: text
  238. a <eq> q
  239. a <eq> e
  240. a <eq> f
  241. b <eq> q
  242. b <eq> e
  243. b <eq> f
  244. j <eq> r
  245. That is, every combination of "left" and
  246. "right" that doesn't further contain
  247. a binary comparison is passed as pairs.
  248. """
  249. stack: List[BinaryExpression[Any]] = []
  250. def visit(element: ClauseElement) -> Iterator[ColumnElement[Any]]:
  251. if isinstance(element, ScalarSelect):
  252. # we don't want to dig into correlated subqueries,
  253. # those are just column elements by themselves
  254. yield element
  255. elif element.__visit_name__ == "binary" and operators.is_comparison(
  256. element.operator # type: ignore
  257. ):
  258. stack.insert(0, element) # type: ignore
  259. for l in visit(element.left): # type: ignore
  260. for r in visit(element.right): # type: ignore
  261. fn(stack[0], l, r)
  262. stack.pop(0)
  263. for elem in element.get_children():
  264. visit(elem)
  265. else:
  266. if isinstance(element, ColumnClause):
  267. yield element
  268. for elem in element.get_children():
  269. yield from visit(elem)
  270. list(visit(expr))
  271. visit = None # type: ignore # remove gc cycles
  272. def find_tables(
  273. clause: ClauseElement,
  274. *,
  275. check_columns: bool = False,
  276. include_aliases: bool = False,
  277. include_joins: bool = False,
  278. include_selects: bool = False,
  279. include_crud: bool = False,
  280. ) -> List[TableClause]:
  281. """locate Table objects within the given expression."""
  282. tables: List[TableClause] = []
  283. _visitors: Dict[str, _TraverseCallableType[Any]] = {}
  284. if include_selects:
  285. _visitors["select"] = _visitors["compound_select"] = tables.append
  286. if include_joins:
  287. _visitors["join"] = tables.append
  288. if include_aliases:
  289. _visitors["alias"] = _visitors["subquery"] = _visitors[
  290. "tablesample"
  291. ] = _visitors["lateral"] = tables.append
  292. if include_crud:
  293. _visitors["insert"] = _visitors["update"] = _visitors["delete"] = (
  294. lambda ent: tables.append(ent.table)
  295. )
  296. if check_columns:
  297. def visit_column(column):
  298. tables.append(column.table)
  299. _visitors["column"] = visit_column
  300. _visitors["table"] = tables.append
  301. visitors.traverse(clause, {}, _visitors)
  302. return tables
  303. def unwrap_order_by(clause: Any) -> Any:
  304. """Break up an 'order by' expression into individual column-expressions,
  305. without DESC/ASC/NULLS FIRST/NULLS LAST"""
  306. cols = util.column_set()
  307. result = []
  308. stack = deque([clause])
  309. # examples
  310. # column -> ASC/DESC == column
  311. # column -> ASC/DESC -> label == column
  312. # column -> label -> ASC/DESC -> label == column
  313. # scalar_select -> label -> ASC/DESC == scalar_select -> label
  314. while stack:
  315. t = stack.popleft()
  316. if isinstance(t, ColumnElement) and (
  317. not isinstance(t, UnaryExpression)
  318. or not operators.is_ordering_modifier(t.modifier) # type: ignore
  319. ):
  320. if isinstance(t, Label) and not isinstance(
  321. t.element, ScalarSelect
  322. ):
  323. t = t.element
  324. if isinstance(t, Grouping):
  325. t = t.element
  326. stack.append(t)
  327. continue
  328. elif isinstance(t, _label_reference):
  329. t = t.element
  330. stack.append(t)
  331. continue
  332. if isinstance(t, (_textual_label_reference)):
  333. continue
  334. if t not in cols:
  335. cols.add(t)
  336. result.append(t)
  337. else:
  338. for c in t.get_children():
  339. stack.append(c)
  340. return result
  341. def unwrap_label_reference(element):
  342. def replace(
  343. element: ExternallyTraversible, **kw: Any
  344. ) -> Optional[ExternallyTraversible]:
  345. if isinstance(element, _label_reference):
  346. return element.element
  347. elif isinstance(element, _textual_label_reference):
  348. assert False, "can't unwrap a textual label reference"
  349. return None
  350. return visitors.replacement_traverse(element, {}, replace)
  351. def expand_column_list_from_order_by(collist, order_by):
  352. """Given the columns clause and ORDER BY of a selectable,
  353. return a list of column expressions that can be added to the collist
  354. corresponding to the ORDER BY, without repeating those already
  355. in the collist.
  356. """
  357. cols_already_present = {
  358. col.element if col._order_by_label_element is not None else col
  359. for col in collist
  360. }
  361. to_look_for = list(chain(*[unwrap_order_by(o) for o in order_by]))
  362. return [col for col in to_look_for if col not in cols_already_present]
  363. def clause_is_present(clause, search):
  364. """Given a target clause and a second to search within, return True
  365. if the target is plainly present in the search without any
  366. subqueries or aliases involved.
  367. Basically descends through Joins.
  368. """
  369. for elem in surface_selectables(search):
  370. if clause == elem: # use == here so that Annotated's compare
  371. return True
  372. else:
  373. return False
  374. def tables_from_leftmost(clause: FromClause) -> Iterator[FromClause]:
  375. if isinstance(clause, Join):
  376. yield from tables_from_leftmost(clause.left)
  377. yield from tables_from_leftmost(clause.right)
  378. elif isinstance(clause, FromGrouping):
  379. yield from tables_from_leftmost(clause.element)
  380. else:
  381. yield clause
  382. def surface_selectables(clause):
  383. stack = [clause]
  384. while stack:
  385. elem = stack.pop()
  386. yield elem
  387. if isinstance(elem, Join):
  388. stack.extend((elem.left, elem.right))
  389. elif isinstance(elem, FromGrouping):
  390. stack.append(elem.element)
  391. def surface_selectables_only(clause: ClauseElement) -> Iterator[ClauseElement]:
  392. stack = [clause]
  393. while stack:
  394. elem = stack.pop()
  395. if isinstance(elem, (TableClause, Alias)):
  396. yield elem
  397. if isinstance(elem, Join):
  398. stack.extend((elem.left, elem.right))
  399. elif isinstance(elem, FromGrouping):
  400. stack.append(elem.element)
  401. elif isinstance(elem, ColumnClause):
  402. if elem.table is not None:
  403. stack.append(elem.table)
  404. else:
  405. yield elem
  406. elif elem is not None:
  407. yield elem
  408. def extract_first_column_annotation(column, annotation_name):
  409. filter_ = (FromGrouping, SelectBase)
  410. stack = deque([column])
  411. while stack:
  412. elem = stack.popleft()
  413. if annotation_name in elem._annotations:
  414. return elem._annotations[annotation_name]
  415. for sub in elem.get_children():
  416. if isinstance(sub, filter_):
  417. continue
  418. stack.append(sub)
  419. return None
  420. def selectables_overlap(left: FromClause, right: FromClause) -> bool:
  421. """Return True if left/right have some overlapping selectable"""
  422. return bool(
  423. set(surface_selectables(left)).intersection(surface_selectables(right))
  424. )
  425. def bind_values(clause):
  426. """Return an ordered list of "bound" values in the given clause.
  427. E.g.::
  428. >>> expr = and_(table.c.foo == 5, table.c.foo == 7)
  429. >>> bind_values(expr)
  430. [5, 7]
  431. """
  432. v = []
  433. def visit_bindparam(bind):
  434. v.append(bind.effective_value)
  435. visitors.traverse(clause, {}, {"bindparam": visit_bindparam})
  436. return v
  437. def _quote_ddl_expr(element):
  438. if isinstance(element, str):
  439. element = element.replace("'", "''")
  440. return "'%s'" % element
  441. else:
  442. return repr(element)
  443. class _repr_base:
  444. _LIST: int = 0
  445. _TUPLE: int = 1
  446. _DICT: int = 2
  447. __slots__ = ("max_chars",)
  448. max_chars: int
  449. def trunc(self, value: Any) -> str:
  450. rep = repr(value)
  451. lenrep = len(rep)
  452. if lenrep > self.max_chars:
  453. segment_length = self.max_chars // 2
  454. rep = (
  455. rep[0:segment_length]
  456. + (
  457. " ... (%d characters truncated) ... "
  458. % (lenrep - self.max_chars)
  459. )
  460. + rep[-segment_length:]
  461. )
  462. return rep
  463. def _repr_single_value(value):
  464. rp = _repr_base()
  465. rp.max_chars = 300
  466. return rp.trunc(value)
  467. class _repr_row(_repr_base):
  468. """Provide a string view of a row."""
  469. __slots__ = ("row",)
  470. def __init__(self, row: Row[Any], max_chars: int = 300):
  471. self.row = row
  472. self.max_chars = max_chars
  473. def __repr__(self) -> str:
  474. trunc = self.trunc
  475. return "(%s%s)" % (
  476. ", ".join(trunc(value) for value in self.row),
  477. "," if len(self.row) == 1 else "",
  478. )
  479. class _long_statement(str):
  480. def __str__(self) -> str:
  481. lself = len(self)
  482. if lself > 500:
  483. lleft = 250
  484. lright = 100
  485. trunc = lself - lleft - lright
  486. return (
  487. f"{self[0:lleft]} ... {trunc} "
  488. f"characters truncated ... {self[-lright:]}"
  489. )
  490. else:
  491. return str.__str__(self)
  492. class _repr_params(_repr_base):
  493. """Provide a string view of bound parameters.
  494. Truncates display to a given number of 'multi' parameter sets,
  495. as well as long values to a given number of characters.
  496. """
  497. __slots__ = "params", "batches", "ismulti", "max_params"
  498. def __init__(
  499. self,
  500. params: Optional[_AnyExecuteParams],
  501. batches: int,
  502. max_params: int = 100,
  503. max_chars: int = 300,
  504. ismulti: Optional[bool] = None,
  505. ):
  506. self.params = params
  507. self.ismulti = ismulti
  508. self.batches = batches
  509. self.max_chars = max_chars
  510. self.max_params = max_params
  511. def __repr__(self) -> str:
  512. if self.ismulti is None:
  513. return self.trunc(self.params)
  514. if isinstance(self.params, list):
  515. typ = self._LIST
  516. elif isinstance(self.params, tuple):
  517. typ = self._TUPLE
  518. elif isinstance(self.params, dict):
  519. typ = self._DICT
  520. else:
  521. return self.trunc(self.params)
  522. if self.ismulti:
  523. multi_params = cast(
  524. "_AnyMultiExecuteParams",
  525. self.params,
  526. )
  527. if len(self.params) > self.batches:
  528. msg = (
  529. " ... displaying %i of %i total bound parameter sets ... "
  530. )
  531. return " ".join(
  532. (
  533. self._repr_multi(
  534. multi_params[: self.batches - 2],
  535. typ,
  536. )[0:-1],
  537. msg % (self.batches, len(self.params)),
  538. self._repr_multi(multi_params[-2:], typ)[1:],
  539. )
  540. )
  541. else:
  542. return self._repr_multi(multi_params, typ)
  543. else:
  544. return self._repr_params(
  545. cast(
  546. "_AnySingleExecuteParams",
  547. self.params,
  548. ),
  549. typ,
  550. )
  551. def _repr_multi(
  552. self,
  553. multi_params: _AnyMultiExecuteParams,
  554. typ: int,
  555. ) -> str:
  556. if multi_params:
  557. if isinstance(multi_params[0], list):
  558. elem_type = self._LIST
  559. elif isinstance(multi_params[0], tuple):
  560. elem_type = self._TUPLE
  561. elif isinstance(multi_params[0], dict):
  562. elem_type = self._DICT
  563. else:
  564. assert False, "Unknown parameter type %s" % (
  565. type(multi_params[0])
  566. )
  567. elements = ", ".join(
  568. self._repr_params(params, elem_type) for params in multi_params
  569. )
  570. else:
  571. elements = ""
  572. if typ == self._LIST:
  573. return "[%s]" % elements
  574. else:
  575. return "(%s)" % elements
  576. def _get_batches(self, params: Iterable[Any]) -> Any:
  577. lparams = list(params)
  578. lenparams = len(lparams)
  579. if lenparams > self.max_params:
  580. lleft = self.max_params // 2
  581. return (
  582. lparams[0:lleft],
  583. lparams[-lleft:],
  584. lenparams - self.max_params,
  585. )
  586. else:
  587. return lparams, None, None
  588. def _repr_params(
  589. self,
  590. params: _AnySingleExecuteParams,
  591. typ: int,
  592. ) -> str:
  593. if typ is self._DICT:
  594. return self._repr_param_dict(
  595. cast("_CoreSingleExecuteParams", params)
  596. )
  597. elif typ is self._TUPLE:
  598. return self._repr_param_tuple(cast("Sequence[Any]", params))
  599. else:
  600. return self._repr_param_list(params)
  601. def _repr_param_dict(self, params: _CoreSingleExecuteParams) -> str:
  602. trunc = self.trunc
  603. (
  604. items_first_batch,
  605. items_second_batch,
  606. trunclen,
  607. ) = self._get_batches(params.items())
  608. if items_second_batch:
  609. text = "{%s" % (
  610. ", ".join(
  611. f"{key!r}: {trunc(value)}"
  612. for key, value in items_first_batch
  613. )
  614. )
  615. text += f" ... {trunclen} parameters truncated ... "
  616. text += "%s}" % (
  617. ", ".join(
  618. f"{key!r}: {trunc(value)}"
  619. for key, value in items_second_batch
  620. )
  621. )
  622. else:
  623. text = "{%s}" % (
  624. ", ".join(
  625. f"{key!r}: {trunc(value)}"
  626. for key, value in items_first_batch
  627. )
  628. )
  629. return text
  630. def _repr_param_tuple(self, params: Sequence[Any]) -> str:
  631. trunc = self.trunc
  632. (
  633. items_first_batch,
  634. items_second_batch,
  635. trunclen,
  636. ) = self._get_batches(params)
  637. if items_second_batch:
  638. text = "(%s" % (
  639. ", ".join(trunc(value) for value in items_first_batch)
  640. )
  641. text += f" ... {trunclen} parameters truncated ... "
  642. text += "%s)" % (
  643. ", ".join(trunc(value) for value in items_second_batch),
  644. )
  645. else:
  646. text = "(%s%s)" % (
  647. ", ".join(trunc(value) for value in items_first_batch),
  648. "," if len(items_first_batch) == 1 else "",
  649. )
  650. return text
  651. def _repr_param_list(self, params: _AnySingleExecuteParams) -> str:
  652. trunc = self.trunc
  653. (
  654. items_first_batch,
  655. items_second_batch,
  656. trunclen,
  657. ) = self._get_batches(params)
  658. if items_second_batch:
  659. text = "[%s" % (
  660. ", ".join(trunc(value) for value in items_first_batch)
  661. )
  662. text += f" ... {trunclen} parameters truncated ... "
  663. text += "%s]" % (
  664. ", ".join(trunc(value) for value in items_second_batch)
  665. )
  666. else:
  667. text = "[%s]" % (
  668. ", ".join(trunc(value) for value in items_first_batch)
  669. )
  670. return text
  671. def adapt_criterion_to_null(crit: _CE, nulls: Collection[Any]) -> _CE:
  672. """given criterion containing bind params, convert selected elements
  673. to IS NULL.
  674. """
  675. def visit_binary(binary):
  676. if (
  677. isinstance(binary.left, BindParameter)
  678. and binary.left._identifying_key in nulls
  679. ):
  680. # reverse order if the NULL is on the left side
  681. binary.left = binary.right
  682. binary.right = Null()
  683. binary.operator = operators.is_
  684. binary.negate = operators.is_not
  685. elif (
  686. isinstance(binary.right, BindParameter)
  687. and binary.right._identifying_key in nulls
  688. ):
  689. binary.right = Null()
  690. binary.operator = operators.is_
  691. binary.negate = operators.is_not
  692. return visitors.cloned_traverse(crit, {}, {"binary": visit_binary})
  693. def splice_joins(
  694. left: Optional[FromClause],
  695. right: Optional[FromClause],
  696. stop_on: Optional[FromClause] = None,
  697. ) -> Optional[FromClause]:
  698. if left is None:
  699. return right
  700. stack: List[Tuple[Optional[FromClause], Optional[Join]]] = [(right, None)]
  701. adapter = ClauseAdapter(left)
  702. ret = None
  703. while stack:
  704. (right, prevright) = stack.pop()
  705. if isinstance(right, Join) and right is not stop_on:
  706. right = right._clone()
  707. right.onclause = adapter.traverse(right.onclause)
  708. stack.append((right.left, right))
  709. else:
  710. right = adapter.traverse(right)
  711. if prevright is not None:
  712. assert right is not None
  713. prevright.left = right
  714. if ret is None:
  715. ret = right
  716. return ret
  717. @overload
  718. def reduce_columns(
  719. columns: Iterable[ColumnElement[Any]],
  720. *clauses: Optional[ClauseElement],
  721. **kw: bool,
  722. ) -> Sequence[ColumnElement[Any]]: ...
  723. @overload
  724. def reduce_columns(
  725. columns: _SelectIterable,
  726. *clauses: Optional[ClauseElement],
  727. **kw: bool,
  728. ) -> Sequence[Union[ColumnElement[Any], TextClause]]: ...
  729. def reduce_columns(
  730. columns: _SelectIterable,
  731. *clauses: Optional[ClauseElement],
  732. **kw: bool,
  733. ) -> Collection[Union[ColumnElement[Any], TextClause]]:
  734. r"""given a list of columns, return a 'reduced' set based on natural
  735. equivalents.
  736. the set is reduced to the smallest list of columns which have no natural
  737. equivalent present in the list. A "natural equivalent" means that two
  738. columns will ultimately represent the same value because they are related
  739. by a foreign key.
  740. \*clauses is an optional list of join clauses which will be traversed
  741. to further identify columns that are "equivalent".
  742. \**kw may specify 'ignore_nonexistent_tables' to ignore foreign keys
  743. whose tables are not yet configured, or columns that aren't yet present.
  744. This function is primarily used to determine the most minimal "primary
  745. key" from a selectable, by reducing the set of primary key columns present
  746. in the selectable to just those that are not repeated.
  747. """
  748. ignore_nonexistent_tables = kw.pop("ignore_nonexistent_tables", False)
  749. only_synonyms = kw.pop("only_synonyms", False)
  750. column_set = util.OrderedSet(columns)
  751. cset_no_text: util.OrderedSet[ColumnElement[Any]] = column_set.difference(
  752. c for c in column_set if is_text_clause(c) # type: ignore
  753. )
  754. omit = util.column_set()
  755. for col in cset_no_text:
  756. for fk in chain(*[c.foreign_keys for c in col.proxy_set]):
  757. for c in cset_no_text:
  758. if c is col:
  759. continue
  760. try:
  761. fk_col = fk.column
  762. except exc.NoReferencedColumnError:
  763. # TODO: add specific coverage here
  764. # to test/sql/test_selectable ReduceTest
  765. if ignore_nonexistent_tables:
  766. continue
  767. else:
  768. raise
  769. except exc.NoReferencedTableError:
  770. # TODO: add specific coverage here
  771. # to test/sql/test_selectable ReduceTest
  772. if ignore_nonexistent_tables:
  773. continue
  774. else:
  775. raise
  776. if fk_col.shares_lineage(c) and (
  777. not only_synonyms or c.name == col.name
  778. ):
  779. omit.add(col)
  780. break
  781. if clauses:
  782. def visit_binary(binary):
  783. if binary.operator == operators.eq:
  784. cols = util.column_set(
  785. chain(
  786. *[c.proxy_set for c in cset_no_text.difference(omit)]
  787. )
  788. )
  789. if binary.left in cols and binary.right in cols:
  790. for c in reversed(cset_no_text):
  791. if c.shares_lineage(binary.right) and (
  792. not only_synonyms or c.name == binary.left.name
  793. ):
  794. omit.add(c)
  795. break
  796. for clause in clauses:
  797. if clause is not None:
  798. visitors.traverse(clause, {}, {"binary": visit_binary})
  799. return column_set.difference(omit)
  800. def criterion_as_pairs(
  801. expression,
  802. consider_as_foreign_keys=None,
  803. consider_as_referenced_keys=None,
  804. any_operator=False,
  805. ):
  806. """traverse an expression and locate binary criterion pairs."""
  807. if consider_as_foreign_keys and consider_as_referenced_keys:
  808. raise exc.ArgumentError(
  809. "Can only specify one of "
  810. "'consider_as_foreign_keys' or "
  811. "'consider_as_referenced_keys'"
  812. )
  813. def col_is(a, b):
  814. # return a is b
  815. return a.compare(b)
  816. def visit_binary(binary):
  817. if not any_operator and binary.operator is not operators.eq:
  818. return
  819. if not isinstance(binary.left, ColumnElement) or not isinstance(
  820. binary.right, ColumnElement
  821. ):
  822. return
  823. if consider_as_foreign_keys:
  824. if binary.left in consider_as_foreign_keys and (
  825. col_is(binary.right, binary.left)
  826. or binary.right not in consider_as_foreign_keys
  827. ):
  828. pairs.append((binary.right, binary.left))
  829. elif binary.right in consider_as_foreign_keys and (
  830. col_is(binary.left, binary.right)
  831. or binary.left not in consider_as_foreign_keys
  832. ):
  833. pairs.append((binary.left, binary.right))
  834. elif consider_as_referenced_keys:
  835. if binary.left in consider_as_referenced_keys and (
  836. col_is(binary.right, binary.left)
  837. or binary.right not in consider_as_referenced_keys
  838. ):
  839. pairs.append((binary.left, binary.right))
  840. elif binary.right in consider_as_referenced_keys and (
  841. col_is(binary.left, binary.right)
  842. or binary.left not in consider_as_referenced_keys
  843. ):
  844. pairs.append((binary.right, binary.left))
  845. else:
  846. if isinstance(binary.left, Column) and isinstance(
  847. binary.right, Column
  848. ):
  849. if binary.left.references(binary.right):
  850. pairs.append((binary.right, binary.left))
  851. elif binary.right.references(binary.left):
  852. pairs.append((binary.left, binary.right))
  853. pairs: List[Tuple[ColumnElement[Any], ColumnElement[Any]]] = []
  854. visitors.traverse(expression, {}, {"binary": visit_binary})
  855. return pairs
  856. class ClauseAdapter(visitors.ReplacingExternalTraversal):
  857. """Clones and modifies clauses based on column correspondence.
  858. E.g.::
  859. table1 = Table(
  860. "sometable",
  861. metadata,
  862. Column("col1", Integer),
  863. Column("col2", Integer),
  864. )
  865. table2 = Table(
  866. "someothertable",
  867. metadata,
  868. Column("col1", Integer),
  869. Column("col2", Integer),
  870. )
  871. condition = table1.c.col1 == table2.c.col1
  872. make an alias of table1::
  873. s = table1.alias("foo")
  874. calling ``ClauseAdapter(s).traverse(condition)`` converts
  875. condition to read::
  876. s.c.col1 == table2.c.col1
  877. """
  878. __slots__ = (
  879. "__traverse_options__",
  880. "selectable",
  881. "include_fn",
  882. "exclude_fn",
  883. "equivalents",
  884. "adapt_on_names",
  885. "adapt_from_selectables",
  886. )
  887. def __init__(
  888. self,
  889. selectable: Selectable,
  890. equivalents: Optional[_EquivalentColumnMap] = None,
  891. include_fn: Optional[Callable[[ClauseElement], bool]] = None,
  892. exclude_fn: Optional[Callable[[ClauseElement], bool]] = None,
  893. adapt_on_names: bool = False,
  894. anonymize_labels: bool = False,
  895. adapt_from_selectables: Optional[AbstractSet[FromClause]] = None,
  896. ):
  897. self.__traverse_options__ = {
  898. "stop_on": [selectable],
  899. "anonymize_labels": anonymize_labels,
  900. }
  901. self.selectable = selectable
  902. self.include_fn = include_fn
  903. self.exclude_fn = exclude_fn
  904. self.equivalents = util.column_dict(equivalents or {})
  905. self.adapt_on_names = adapt_on_names
  906. self.adapt_from_selectables = adapt_from_selectables
  907. if TYPE_CHECKING:
  908. @overload
  909. def traverse(self, obj: Literal[None]) -> None: ...
  910. # note this specializes the ReplacingExternalTraversal.traverse()
  911. # method to state
  912. # that we will return the same kind of ExternalTraversal object as
  913. # we were given. This is probably not 100% true, such as it's
  914. # possible for us to swap out Alias for Table at the top level.
  915. # Ideally there could be overloads specific to ColumnElement and
  916. # FromClause but Mypy is not accepting those as compatible with
  917. # the base ReplacingExternalTraversal
  918. @overload
  919. def traverse(self, obj: _ET) -> _ET: ...
  920. def traverse(
  921. self, obj: Optional[ExternallyTraversible]
  922. ) -> Optional[ExternallyTraversible]: ...
  923. def _corresponding_column(
  924. self, col, require_embedded, _seen=util.EMPTY_SET
  925. ):
  926. newcol = self.selectable.corresponding_column(
  927. col, require_embedded=require_embedded
  928. )
  929. if newcol is None and col in self.equivalents and col not in _seen:
  930. for equiv in self.equivalents[col]:
  931. newcol = self._corresponding_column(
  932. equiv,
  933. require_embedded=require_embedded,
  934. _seen=_seen.union([col]),
  935. )
  936. if newcol is not None:
  937. return newcol
  938. if (
  939. self.adapt_on_names
  940. and newcol is None
  941. and isinstance(col, NamedColumn)
  942. ):
  943. newcol = self.selectable.exported_columns.get(col.name)
  944. return newcol
  945. @util.preload_module("sqlalchemy.sql.functions")
  946. def replace(
  947. self, col: _ET, _include_singleton_constants: bool = False
  948. ) -> Optional[_ET]:
  949. functions = util.preloaded.sql_functions
  950. # TODO: cython candidate
  951. if self.include_fn and not self.include_fn(col): # type: ignore
  952. return None
  953. elif self.exclude_fn and self.exclude_fn(col): # type: ignore
  954. return None
  955. if isinstance(col, FromClause) and not isinstance(
  956. col, functions.FunctionElement
  957. ):
  958. if self.selectable.is_derived_from(col):
  959. if self.adapt_from_selectables:
  960. for adp in self.adapt_from_selectables:
  961. if adp.is_derived_from(col):
  962. break
  963. else:
  964. return None
  965. return self.selectable # type: ignore
  966. elif isinstance(col, Alias) and isinstance(
  967. col.element, TableClause
  968. ):
  969. # we are a SELECT statement and not derived from an alias of a
  970. # table (which nonetheless may be a table our SELECT derives
  971. # from), so return the alias to prevent further traversal
  972. # or
  973. # we are an alias of a table and we are not derived from an
  974. # alias of a table (which nonetheless may be the same table
  975. # as ours) so, same thing
  976. return col
  977. else:
  978. # other cases where we are a selectable and the element
  979. # is another join or selectable that contains a table which our
  980. # selectable derives from, that we want to process
  981. return None
  982. elif not isinstance(col, ColumnElement):
  983. return None
  984. elif not _include_singleton_constants and col._is_singleton_constant:
  985. # dont swap out NULL, TRUE, FALSE for a label name
  986. # in a SQL statement that's being rewritten,
  987. # leave them as the constant. This is first noted in #6259,
  988. # however the logic to check this moved here as of #7154 so that
  989. # it is made specific to SQL rewriting and not all column
  990. # correspondence
  991. return None
  992. if "adapt_column" in col._annotations:
  993. col = col._annotations["adapt_column"]
  994. if TYPE_CHECKING:
  995. assert isinstance(col, KeyedColumnElement)
  996. if self.adapt_from_selectables and col not in self.equivalents:
  997. for adp in self.adapt_from_selectables:
  998. if adp.c.corresponding_column(col, False) is not None:
  999. break
  1000. else:
  1001. return None
  1002. if TYPE_CHECKING:
  1003. assert isinstance(col, KeyedColumnElement)
  1004. return self._corresponding_column( # type: ignore
  1005. col, require_embedded=True
  1006. )
  1007. class _ColumnLookup(Protocol):
  1008. @overload
  1009. def __getitem__(self, key: None) -> None: ...
  1010. @overload
  1011. def __getitem__(self, key: ColumnClause[Any]) -> ColumnClause[Any]: ...
  1012. @overload
  1013. def __getitem__(self, key: ColumnElement[Any]) -> ColumnElement[Any]: ...
  1014. @overload
  1015. def __getitem__(self, key: _ET) -> _ET: ...
  1016. def __getitem__(self, key: Any) -> Any: ...
  1017. class ColumnAdapter(ClauseAdapter):
  1018. """Extends ClauseAdapter with extra utility functions.
  1019. Key aspects of ColumnAdapter include:
  1020. * Expressions that are adapted are stored in a persistent
  1021. .columns collection; so that an expression E adapted into
  1022. an expression E1, will return the same object E1 when adapted
  1023. a second time. This is important in particular for things like
  1024. Label objects that are anonymized, so that the ColumnAdapter can
  1025. be used to present a consistent "adapted" view of things.
  1026. * Exclusion of items from the persistent collection based on
  1027. include/exclude rules, but also independent of hash identity.
  1028. This because "annotated" items all have the same hash identity as their
  1029. parent.
  1030. * "wrapping" capability is added, so that the replacement of an expression
  1031. E can proceed through a series of adapters. This differs from the
  1032. visitor's "chaining" feature in that the resulting object is passed
  1033. through all replacing functions unconditionally, rather than stopping
  1034. at the first one that returns non-None.
  1035. * An adapt_required option, used by eager loading to indicate that
  1036. We don't trust a result row column that is not translated.
  1037. This is to prevent a column from being interpreted as that
  1038. of the child row in a self-referential scenario, see
  1039. inheritance/test_basic.py->EagerTargetingTest.test_adapt_stringency
  1040. """
  1041. __slots__ = (
  1042. "columns",
  1043. "adapt_required",
  1044. "allow_label_resolve",
  1045. "_wrap",
  1046. "__weakref__",
  1047. )
  1048. columns: _ColumnLookup
  1049. def __init__(
  1050. self,
  1051. selectable: Selectable,
  1052. equivalents: Optional[_EquivalentColumnMap] = None,
  1053. adapt_required: bool = False,
  1054. include_fn: Optional[Callable[[ClauseElement], bool]] = None,
  1055. exclude_fn: Optional[Callable[[ClauseElement], bool]] = None,
  1056. adapt_on_names: bool = False,
  1057. allow_label_resolve: bool = True,
  1058. anonymize_labels: bool = False,
  1059. adapt_from_selectables: Optional[AbstractSet[FromClause]] = None,
  1060. ):
  1061. super().__init__(
  1062. selectable,
  1063. equivalents,
  1064. include_fn=include_fn,
  1065. exclude_fn=exclude_fn,
  1066. adapt_on_names=adapt_on_names,
  1067. anonymize_labels=anonymize_labels,
  1068. adapt_from_selectables=adapt_from_selectables,
  1069. )
  1070. self.columns = util.WeakPopulateDict(self._locate_col) # type: ignore
  1071. if self.include_fn or self.exclude_fn:
  1072. self.columns = self._IncludeExcludeMapping(self, self.columns)
  1073. self.adapt_required = adapt_required
  1074. self.allow_label_resolve = allow_label_resolve
  1075. self._wrap = None
  1076. class _IncludeExcludeMapping:
  1077. def __init__(self, parent, columns):
  1078. self.parent = parent
  1079. self.columns = columns
  1080. def __getitem__(self, key):
  1081. if (
  1082. self.parent.include_fn and not self.parent.include_fn(key)
  1083. ) or (self.parent.exclude_fn and self.parent.exclude_fn(key)):
  1084. if self.parent._wrap:
  1085. return self.parent._wrap.columns[key]
  1086. else:
  1087. return key
  1088. return self.columns[key]
  1089. def wrap(self, adapter):
  1090. ac = copy.copy(self)
  1091. ac._wrap = adapter
  1092. ac.columns = util.WeakPopulateDict(ac._locate_col) # type: ignore
  1093. if ac.include_fn or ac.exclude_fn:
  1094. ac.columns = self._IncludeExcludeMapping(ac, ac.columns)
  1095. return ac
  1096. @overload
  1097. def traverse(self, obj: Literal[None]) -> None: ...
  1098. @overload
  1099. def traverse(self, obj: _ET) -> _ET: ...
  1100. def traverse(
  1101. self, obj: Optional[ExternallyTraversible]
  1102. ) -> Optional[ExternallyTraversible]:
  1103. return self.columns[obj]
  1104. def chain(self, visitor: ExternalTraversal) -> ColumnAdapter:
  1105. assert isinstance(visitor, ColumnAdapter)
  1106. return super().chain(visitor)
  1107. if TYPE_CHECKING:
  1108. @property
  1109. def visitor_iterator(self) -> Iterator[ColumnAdapter]: ...
  1110. adapt_clause = traverse
  1111. adapt_list = ClauseAdapter.copy_and_process
  1112. def adapt_check_present(
  1113. self, col: ColumnElement[Any]
  1114. ) -> Optional[ColumnElement[Any]]:
  1115. newcol = self.columns[col]
  1116. if newcol is col and self._corresponding_column(col, True) is None:
  1117. return None
  1118. return newcol
  1119. def _locate_col(
  1120. self, col: ColumnElement[Any]
  1121. ) -> Optional[ColumnElement[Any]]:
  1122. # both replace and traverse() are overly complicated for what
  1123. # we are doing here and we would do better to have an inlined
  1124. # version that doesn't build up as much overhead. the issue is that
  1125. # sometimes the lookup does in fact have to adapt the insides of
  1126. # say a labeled scalar subquery. However, if the object is an
  1127. # Immutable, i.e. Column objects, we can skip the "clone" /
  1128. # "copy internals" part since those will be no-ops in any case.
  1129. # additionally we want to catch singleton objects null/true/false
  1130. # and make sure they are adapted as well here.
  1131. if col._is_immutable:
  1132. for vis in self.visitor_iterator:
  1133. c = vis.replace(col, _include_singleton_constants=True)
  1134. if c is not None:
  1135. break
  1136. else:
  1137. c = col
  1138. else:
  1139. c = ClauseAdapter.traverse(self, col)
  1140. if self._wrap:
  1141. c2 = self._wrap._locate_col(c)
  1142. if c2 is not None:
  1143. c = c2
  1144. if self.adapt_required and c is col:
  1145. return None
  1146. # allow_label_resolve is consumed by one case for joined eager loading
  1147. # as part of its logic to prevent its own columns from being affected
  1148. # by .order_by(). Before full typing were applied to the ORM, this
  1149. # logic would set this attribute on the incoming object (which is
  1150. # typically a column, but we have a test for it being a non-column
  1151. # object) if no column were found. While this seemed to
  1152. # have no negative effects, this adjustment should only occur on the
  1153. # new column which is assumed to be local to an adapted selectable.
  1154. if c is not col:
  1155. c._allow_label_resolve = self.allow_label_resolve
  1156. return c
  1157. def _offset_or_limit_clause(
  1158. element: _LimitOffsetType,
  1159. name: Optional[str] = None,
  1160. type_: Optional[_TypeEngineArgument[int]] = None,
  1161. ) -> ColumnElement[int]:
  1162. """Convert the given value to an "offset or limit" clause.
  1163. This handles incoming integers and converts to an expression; if
  1164. an expression is already given, it is passed through.
  1165. """
  1166. return coercions.expect(
  1167. roles.LimitOffsetRole, element, name=name, type_=type_
  1168. )
  1169. def _offset_or_limit_clause_asint_if_possible(
  1170. clause: _LimitOffsetType,
  1171. ) -> _LimitOffsetType:
  1172. """Return the offset or limit clause as a simple integer if possible,
  1173. else return the clause.
  1174. """
  1175. if clause is None:
  1176. return None
  1177. if hasattr(clause, "_limit_offset_value"):
  1178. value = clause._limit_offset_value
  1179. return util.asint(value)
  1180. else:
  1181. return clause
  1182. def _make_slice(
  1183. limit_clause: _LimitOffsetType,
  1184. offset_clause: _LimitOffsetType,
  1185. start: int,
  1186. stop: int,
  1187. ) -> Tuple[Optional[ColumnElement[int]], Optional[ColumnElement[int]]]:
  1188. """Compute LIMIT/OFFSET in terms of slice start/end"""
  1189. # for calculated limit/offset, try to do the addition of
  1190. # values to offset in Python, however if a SQL clause is present
  1191. # then the addition has to be on the SQL side.
  1192. # TODO: typing is finding a few gaps in here, see if they can be
  1193. # closed up
  1194. if start is not None and stop is not None:
  1195. offset_clause = _offset_or_limit_clause_asint_if_possible(
  1196. offset_clause
  1197. )
  1198. if offset_clause is None:
  1199. offset_clause = 0
  1200. if start != 0:
  1201. offset_clause = offset_clause + start # type: ignore
  1202. if offset_clause == 0:
  1203. offset_clause = None
  1204. else:
  1205. assert offset_clause is not None
  1206. offset_clause = _offset_or_limit_clause(offset_clause)
  1207. limit_clause = _offset_or_limit_clause(stop - start)
  1208. elif start is None and stop is not None:
  1209. limit_clause = _offset_or_limit_clause(stop)
  1210. elif start is not None and stop is None:
  1211. offset_clause = _offset_or_limit_clause_asint_if_possible(
  1212. offset_clause
  1213. )
  1214. if offset_clause is None:
  1215. offset_clause = 0
  1216. if start != 0:
  1217. offset_clause = offset_clause + start
  1218. if offset_clause == 0:
  1219. offset_clause = None
  1220. else:
  1221. offset_clause = _offset_or_limit_clause(offset_clause)
  1222. return limit_clause, offset_clause