test_select.py 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. # testing/suite/test_select.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. import itertools
  8. from .. import AssertsCompiledSQL
  9. from .. import AssertsExecutionResults
  10. from .. import config
  11. from .. import fixtures
  12. from ..assertions import assert_raises
  13. from ..assertions import eq_
  14. from ..assertions import in_
  15. from ..assertsql import CursorSQL
  16. from ..schema import Column
  17. from ..schema import Table
  18. from ... import bindparam
  19. from ... import case
  20. from ... import column
  21. from ... import Computed
  22. from ... import exists
  23. from ... import false
  24. from ... import ForeignKey
  25. from ... import func
  26. from ... import Identity
  27. from ... import Integer
  28. from ... import literal
  29. from ... import literal_column
  30. from ... import null
  31. from ... import select
  32. from ... import String
  33. from ... import table
  34. from ... import testing
  35. from ... import text
  36. from ... import true
  37. from ... import tuple_
  38. from ... import TupleType
  39. from ... import union
  40. from ... import util
  41. from ... import values
  42. from ...exc import DatabaseError
  43. from ...exc import ProgrammingError
  44. from ...util import collections_abc
  45. class CollateTest(fixtures.TablesTest):
  46. __backend__ = True
  47. @classmethod
  48. def define_tables(cls, metadata):
  49. Table(
  50. "some_table",
  51. metadata,
  52. Column("id", Integer, primary_key=True),
  53. Column("data", String(100)),
  54. )
  55. @classmethod
  56. def insert_data(cls, connection):
  57. connection.execute(
  58. cls.tables.some_table.insert(),
  59. [
  60. {"id": 1, "data": "collate data1"},
  61. {"id": 2, "data": "collate data2"},
  62. ],
  63. )
  64. def _assert_result(self, select, result):
  65. with config.db.connect() as conn:
  66. eq_(conn.execute(select).fetchall(), result)
  67. @testing.requires.order_by_collation
  68. def test_collate_order_by(self):
  69. collation = testing.requires.get_order_by_collation(testing.config)
  70. self._assert_result(
  71. select(self.tables.some_table).order_by(
  72. self.tables.some_table.c.data.collate(collation).asc()
  73. ),
  74. [(1, "collate data1"), (2, "collate data2")],
  75. )
  76. class OrderByLabelTest(fixtures.TablesTest):
  77. """Test the dialect sends appropriate ORDER BY expressions when
  78. labels are used.
  79. This essentially exercises the "supports_simple_order_by_label"
  80. setting.
  81. """
  82. __backend__ = True
  83. @classmethod
  84. def define_tables(cls, metadata):
  85. Table(
  86. "some_table",
  87. metadata,
  88. Column("id", Integer, primary_key=True),
  89. Column("x", Integer),
  90. Column("y", Integer),
  91. Column("q", String(50)),
  92. Column("p", String(50)),
  93. )
  94. @classmethod
  95. def insert_data(cls, connection):
  96. connection.execute(
  97. cls.tables.some_table.insert(),
  98. [
  99. {"id": 1, "x": 1, "y": 2, "q": "q1", "p": "p3"},
  100. {"id": 2, "x": 2, "y": 3, "q": "q2", "p": "p2"},
  101. {"id": 3, "x": 3, "y": 4, "q": "q3", "p": "p1"},
  102. ],
  103. )
  104. def _assert_result(self, select, result):
  105. with config.db.connect() as conn:
  106. eq_(conn.execute(select).fetchall(), result)
  107. def test_plain(self):
  108. table = self.tables.some_table
  109. lx = table.c.x.label("lx")
  110. self._assert_result(select(lx).order_by(lx), [(1,), (2,), (3,)])
  111. def test_composed_int(self):
  112. table = self.tables.some_table
  113. lx = (table.c.x + table.c.y).label("lx")
  114. self._assert_result(select(lx).order_by(lx), [(3,), (5,), (7,)])
  115. def test_composed_multiple(self):
  116. table = self.tables.some_table
  117. lx = (table.c.x + table.c.y).label("lx")
  118. ly = (func.lower(table.c.q) + table.c.p).label("ly")
  119. self._assert_result(
  120. select(lx, ly).order_by(lx, ly.desc()),
  121. [(3, util.u("q1p3")), (5, util.u("q2p2")), (7, util.u("q3p1"))],
  122. )
  123. def test_plain_desc(self):
  124. table = self.tables.some_table
  125. lx = table.c.x.label("lx")
  126. self._assert_result(select(lx).order_by(lx.desc()), [(3,), (2,), (1,)])
  127. def test_composed_int_desc(self):
  128. table = self.tables.some_table
  129. lx = (table.c.x + table.c.y).label("lx")
  130. self._assert_result(select(lx).order_by(lx.desc()), [(7,), (5,), (3,)])
  131. @testing.requires.group_by_complex_expression
  132. def test_group_by_composed(self):
  133. table = self.tables.some_table
  134. expr = (table.c.x + table.c.y).label("lx")
  135. stmt = (
  136. select(func.count(table.c.id), expr).group_by(expr).order_by(expr)
  137. )
  138. self._assert_result(stmt, [(1, 3), (1, 5), (1, 7)])
  139. class ValuesExpressionTest(fixtures.TestBase):
  140. __requires__ = ("table_value_constructor",)
  141. __backend__ = True
  142. def test_tuples(self, connection):
  143. value_expr = values(
  144. column("id", Integer), column("name", String), name="my_values"
  145. ).data([(1, "name1"), (2, "name2"), (3, "name3")])
  146. eq_(
  147. connection.execute(select(value_expr)).all(),
  148. [(1, "name1"), (2, "name2"), (3, "name3")],
  149. )
  150. class FetchLimitOffsetTest(fixtures.TablesTest):
  151. __backend__ = True
  152. @classmethod
  153. def define_tables(cls, metadata):
  154. Table(
  155. "some_table",
  156. metadata,
  157. Column("id", Integer, primary_key=True),
  158. Column("x", Integer),
  159. Column("y", Integer),
  160. )
  161. @classmethod
  162. def insert_data(cls, connection):
  163. connection.execute(
  164. cls.tables.some_table.insert(),
  165. [
  166. {"id": 1, "x": 1, "y": 2},
  167. {"id": 2, "x": 2, "y": 3},
  168. {"id": 3, "x": 3, "y": 4},
  169. {"id": 4, "x": 4, "y": 5},
  170. {"id": 5, "x": 4, "y": 6},
  171. ],
  172. )
  173. def _assert_result(
  174. self, connection, select, result, params=(), set_=False
  175. ):
  176. if set_:
  177. query_res = connection.execute(select, params).fetchall()
  178. eq_(len(query_res), len(result))
  179. eq_(set(query_res), set(result))
  180. else:
  181. eq_(connection.execute(select, params).fetchall(), result)
  182. def _assert_result_str(self, select, result, params=()):
  183. conn = config.db.connect(close_with_result=True)
  184. eq_(conn.exec_driver_sql(select, params).fetchall(), result)
  185. def test_simple_limit(self, connection):
  186. table = self.tables.some_table
  187. stmt = select(table).order_by(table.c.id)
  188. self._assert_result(
  189. connection,
  190. stmt.limit(2),
  191. [(1, 1, 2), (2, 2, 3)],
  192. )
  193. self._assert_result(
  194. connection,
  195. stmt.limit(3),
  196. [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
  197. )
  198. def test_limit_render_multiple_times(self, connection):
  199. table = self.tables.some_table
  200. stmt = select(table.c.id).limit(1).scalar_subquery()
  201. u = union(select(stmt), select(stmt)).subquery().select()
  202. self._assert_result(
  203. connection,
  204. u,
  205. [
  206. (1,),
  207. ],
  208. )
  209. @testing.requires.fetch_first
  210. def test_simple_fetch(self, connection):
  211. table = self.tables.some_table
  212. self._assert_result(
  213. connection,
  214. select(table).order_by(table.c.id).fetch(2),
  215. [(1, 1, 2), (2, 2, 3)],
  216. )
  217. self._assert_result(
  218. connection,
  219. select(table).order_by(table.c.id).fetch(3),
  220. [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
  221. )
  222. @testing.requires.offset
  223. def test_simple_offset(self, connection):
  224. table = self.tables.some_table
  225. self._assert_result(
  226. connection,
  227. select(table).order_by(table.c.id).offset(2),
  228. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  229. )
  230. self._assert_result(
  231. connection,
  232. select(table).order_by(table.c.id).offset(3),
  233. [(4, 4, 5), (5, 4, 6)],
  234. )
  235. @testing.combinations(
  236. ([(2, 0), (2, 1), (3, 2)]),
  237. ([(2, 1), (2, 0), (3, 2)]),
  238. ([(3, 1), (2, 1), (3, 1)]),
  239. argnames="cases",
  240. )
  241. @testing.requires.offset
  242. def test_simple_limit_offset(self, connection, cases):
  243. table = self.tables.some_table
  244. connection = connection.execution_options(compiled_cache={})
  245. assert_data = [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)]
  246. for limit, offset in cases:
  247. expected = assert_data[offset : offset + limit]
  248. self._assert_result(
  249. connection,
  250. select(table).order_by(table.c.id).limit(limit).offset(offset),
  251. expected,
  252. )
  253. @testing.requires.fetch_first
  254. def test_simple_fetch_offset(self, connection):
  255. table = self.tables.some_table
  256. self._assert_result(
  257. connection,
  258. select(table).order_by(table.c.id).fetch(2).offset(1),
  259. [(2, 2, 3), (3, 3, 4)],
  260. )
  261. self._assert_result(
  262. connection,
  263. select(table).order_by(table.c.id).fetch(3).offset(2),
  264. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  265. )
  266. @testing.requires.fetch_no_order_by
  267. def test_fetch_offset_no_order(self, connection):
  268. table = self.tables.some_table
  269. self._assert_result(
  270. connection,
  271. select(table).fetch(10),
  272. [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
  273. set_=True,
  274. )
  275. @testing.requires.offset
  276. def test_simple_offset_zero(self, connection):
  277. table = self.tables.some_table
  278. self._assert_result(
  279. connection,
  280. select(table).order_by(table.c.id).offset(0),
  281. [(1, 1, 2), (2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
  282. )
  283. self._assert_result(
  284. connection,
  285. select(table).order_by(table.c.id).offset(1),
  286. [(2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
  287. )
  288. @testing.requires.offset
  289. def test_limit_offset_nobinds(self):
  290. """test that 'literal binds' mode works - no bound params."""
  291. table = self.tables.some_table
  292. stmt = select(table).order_by(table.c.id).limit(2).offset(1)
  293. sql = stmt.compile(
  294. dialect=config.db.dialect, compile_kwargs={"literal_binds": True}
  295. )
  296. sql = str(sql)
  297. self._assert_result_str(sql, [(2, 2, 3), (3, 3, 4)])
  298. @testing.requires.fetch_first
  299. def test_fetch_offset_nobinds(self):
  300. """test that 'literal binds' mode works - no bound params."""
  301. table = self.tables.some_table
  302. stmt = select(table).order_by(table.c.id).fetch(2).offset(1)
  303. sql = stmt.compile(
  304. dialect=config.db.dialect, compile_kwargs={"literal_binds": True}
  305. )
  306. sql = str(sql)
  307. self._assert_result_str(sql, [(2, 2, 3), (3, 3, 4)])
  308. @testing.requires.bound_limit_offset
  309. def test_bound_limit(self, connection):
  310. table = self.tables.some_table
  311. self._assert_result(
  312. connection,
  313. select(table).order_by(table.c.id).limit(bindparam("l")),
  314. [(1, 1, 2), (2, 2, 3)],
  315. params={"l": 2},
  316. )
  317. self._assert_result(
  318. connection,
  319. select(table).order_by(table.c.id).limit(bindparam("l")),
  320. [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
  321. params={"l": 3},
  322. )
  323. @testing.requires.bound_limit_offset
  324. def test_bound_offset(self, connection):
  325. table = self.tables.some_table
  326. self._assert_result(
  327. connection,
  328. select(table).order_by(table.c.id).offset(bindparam("o")),
  329. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  330. params={"o": 2},
  331. )
  332. self._assert_result(
  333. connection,
  334. select(table).order_by(table.c.id).offset(bindparam("o")),
  335. [(2, 2, 3), (3, 3, 4), (4, 4, 5), (5, 4, 6)],
  336. params={"o": 1},
  337. )
  338. @testing.requires.bound_limit_offset
  339. def test_bound_limit_offset(self, connection):
  340. table = self.tables.some_table
  341. self._assert_result(
  342. connection,
  343. select(table)
  344. .order_by(table.c.id)
  345. .limit(bindparam("l"))
  346. .offset(bindparam("o")),
  347. [(2, 2, 3), (3, 3, 4)],
  348. params={"l": 2, "o": 1},
  349. )
  350. self._assert_result(
  351. connection,
  352. select(table)
  353. .order_by(table.c.id)
  354. .limit(bindparam("l"))
  355. .offset(bindparam("o")),
  356. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  357. params={"l": 3, "o": 2},
  358. )
  359. @testing.requires.fetch_first
  360. def test_bound_fetch_offset(self, connection):
  361. table = self.tables.some_table
  362. self._assert_result(
  363. connection,
  364. select(table)
  365. .order_by(table.c.id)
  366. .fetch(bindparam("f"))
  367. .offset(bindparam("o")),
  368. [(2, 2, 3), (3, 3, 4)],
  369. params={"f": 2, "o": 1},
  370. )
  371. self._assert_result(
  372. connection,
  373. select(table)
  374. .order_by(table.c.id)
  375. .fetch(bindparam("f"))
  376. .offset(bindparam("o")),
  377. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  378. params={"f": 3, "o": 2},
  379. )
  380. @testing.requires.sql_expression_limit_offset
  381. def test_expr_offset(self, connection):
  382. table = self.tables.some_table
  383. self._assert_result(
  384. connection,
  385. select(table)
  386. .order_by(table.c.id)
  387. .offset(literal_column("1") + literal_column("2")),
  388. [(4, 4, 5), (5, 4, 6)],
  389. )
  390. @testing.requires.sql_expression_limit_offset
  391. def test_expr_limit(self, connection):
  392. table = self.tables.some_table
  393. self._assert_result(
  394. connection,
  395. select(table)
  396. .order_by(table.c.id)
  397. .limit(literal_column("1") + literal_column("2")),
  398. [(1, 1, 2), (2, 2, 3), (3, 3, 4)],
  399. )
  400. @testing.requires.sql_expression_limit_offset
  401. def test_expr_limit_offset(self, connection):
  402. table = self.tables.some_table
  403. self._assert_result(
  404. connection,
  405. select(table)
  406. .order_by(table.c.id)
  407. .limit(literal_column("1") + literal_column("1"))
  408. .offset(literal_column("1") + literal_column("1")),
  409. [(3, 3, 4), (4, 4, 5)],
  410. )
  411. @testing.requires.fetch_first
  412. @testing.requires.fetch_expression
  413. def test_expr_fetch_offset(self, connection):
  414. table = self.tables.some_table
  415. self._assert_result(
  416. connection,
  417. select(table)
  418. .order_by(table.c.id)
  419. .fetch(literal_column("1") + literal_column("1"))
  420. .offset(literal_column("1") + literal_column("1")),
  421. [(3, 3, 4), (4, 4, 5)],
  422. )
  423. @testing.requires.sql_expression_limit_offset
  424. def test_simple_limit_expr_offset(self, connection):
  425. table = self.tables.some_table
  426. self._assert_result(
  427. connection,
  428. select(table)
  429. .order_by(table.c.id)
  430. .limit(2)
  431. .offset(literal_column("1") + literal_column("1")),
  432. [(3, 3, 4), (4, 4, 5)],
  433. )
  434. self._assert_result(
  435. connection,
  436. select(table)
  437. .order_by(table.c.id)
  438. .limit(3)
  439. .offset(literal_column("1") + literal_column("1")),
  440. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  441. )
  442. @testing.requires.sql_expression_limit_offset
  443. def test_expr_limit_simple_offset(self, connection):
  444. table = self.tables.some_table
  445. self._assert_result(
  446. connection,
  447. select(table)
  448. .order_by(table.c.id)
  449. .limit(literal_column("1") + literal_column("1"))
  450. .offset(2),
  451. [(3, 3, 4), (4, 4, 5)],
  452. )
  453. self._assert_result(
  454. connection,
  455. select(table)
  456. .order_by(table.c.id)
  457. .limit(literal_column("1") + literal_column("1"))
  458. .offset(1),
  459. [(2, 2, 3), (3, 3, 4)],
  460. )
  461. @testing.requires.fetch_ties
  462. def test_simple_fetch_ties(self, connection):
  463. table = self.tables.some_table
  464. self._assert_result(
  465. connection,
  466. select(table).order_by(table.c.x.desc()).fetch(1, with_ties=True),
  467. [(4, 4, 5), (5, 4, 6)],
  468. set_=True,
  469. )
  470. self._assert_result(
  471. connection,
  472. select(table).order_by(table.c.x.desc()).fetch(3, with_ties=True),
  473. [(3, 3, 4), (4, 4, 5), (5, 4, 6)],
  474. set_=True,
  475. )
  476. @testing.requires.fetch_ties
  477. @testing.requires.fetch_offset_with_options
  478. def test_fetch_offset_ties(self, connection):
  479. table = self.tables.some_table
  480. fa = connection.execute(
  481. select(table)
  482. .order_by(table.c.x)
  483. .fetch(2, with_ties=True)
  484. .offset(2)
  485. ).fetchall()
  486. eq_(fa[0], (3, 3, 4))
  487. eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
  488. @testing.requires.fetch_ties
  489. @testing.requires.fetch_offset_with_options
  490. def test_fetch_offset_ties_exact_number(self, connection):
  491. table = self.tables.some_table
  492. self._assert_result(
  493. connection,
  494. select(table)
  495. .order_by(table.c.x)
  496. .fetch(2, with_ties=True)
  497. .offset(1),
  498. [(2, 2, 3), (3, 3, 4)],
  499. )
  500. self._assert_result(
  501. connection,
  502. select(table)
  503. .order_by(table.c.x)
  504. .fetch(3, with_ties=True)
  505. .offset(3),
  506. [(4, 4, 5), (5, 4, 6)],
  507. )
  508. @testing.requires.fetch_percent
  509. def test_simple_fetch_percent(self, connection):
  510. table = self.tables.some_table
  511. self._assert_result(
  512. connection,
  513. select(table).order_by(table.c.id).fetch(20, percent=True),
  514. [(1, 1, 2)],
  515. )
  516. @testing.requires.fetch_percent
  517. @testing.requires.fetch_offset_with_options
  518. def test_fetch_offset_percent(self, connection):
  519. table = self.tables.some_table
  520. self._assert_result(
  521. connection,
  522. select(table)
  523. .order_by(table.c.id)
  524. .fetch(40, percent=True)
  525. .offset(1),
  526. [(2, 2, 3), (3, 3, 4)],
  527. )
  528. @testing.requires.fetch_ties
  529. @testing.requires.fetch_percent
  530. def test_simple_fetch_percent_ties(self, connection):
  531. table = self.tables.some_table
  532. self._assert_result(
  533. connection,
  534. select(table)
  535. .order_by(table.c.x.desc())
  536. .fetch(20, percent=True, with_ties=True),
  537. [(4, 4, 5), (5, 4, 6)],
  538. set_=True,
  539. )
  540. @testing.requires.fetch_ties
  541. @testing.requires.fetch_percent
  542. @testing.requires.fetch_offset_with_options
  543. def test_fetch_offset_percent_ties(self, connection):
  544. table = self.tables.some_table
  545. fa = connection.execute(
  546. select(table)
  547. .order_by(table.c.x)
  548. .fetch(40, percent=True, with_ties=True)
  549. .offset(2)
  550. ).fetchall()
  551. eq_(fa[0], (3, 3, 4))
  552. eq_(set(fa), set([(3, 3, 4), (4, 4, 5), (5, 4, 6)]))
  553. class JoinTest(fixtures.TablesTest):
  554. __backend__ = True
  555. def _assert_result(self, select, result, params=()):
  556. with config.db.connect() as conn:
  557. eq_(conn.execute(select, params).fetchall(), result)
  558. @classmethod
  559. def define_tables(cls, metadata):
  560. Table("a", metadata, Column("id", Integer, primary_key=True))
  561. Table(
  562. "b",
  563. metadata,
  564. Column("id", Integer, primary_key=True),
  565. Column("a_id", ForeignKey("a.id"), nullable=False),
  566. )
  567. @classmethod
  568. def insert_data(cls, connection):
  569. connection.execute(
  570. cls.tables.a.insert(),
  571. [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}, {"id": 5}],
  572. )
  573. connection.execute(
  574. cls.tables.b.insert(),
  575. [
  576. {"id": 1, "a_id": 1},
  577. {"id": 2, "a_id": 1},
  578. {"id": 4, "a_id": 2},
  579. {"id": 5, "a_id": 3},
  580. ],
  581. )
  582. def test_inner_join_fk(self):
  583. a, b = self.tables("a", "b")
  584. stmt = select(a, b).select_from(a.join(b)).order_by(a.c.id, b.c.id)
  585. self._assert_result(stmt, [(1, 1, 1), (1, 2, 1), (2, 4, 2), (3, 5, 3)])
  586. def test_inner_join_true(self):
  587. a, b = self.tables("a", "b")
  588. stmt = (
  589. select(a, b)
  590. .select_from(a.join(b, true()))
  591. .order_by(a.c.id, b.c.id)
  592. )
  593. self._assert_result(
  594. stmt,
  595. [
  596. (a, b, c)
  597. for (a,), (b, c) in itertools.product(
  598. [(1,), (2,), (3,), (4,), (5,)],
  599. [(1, 1), (2, 1), (4, 2), (5, 3)],
  600. )
  601. ],
  602. )
  603. def test_inner_join_false(self):
  604. a, b = self.tables("a", "b")
  605. stmt = (
  606. select(a, b)
  607. .select_from(a.join(b, false()))
  608. .order_by(a.c.id, b.c.id)
  609. )
  610. self._assert_result(stmt, [])
  611. def test_outer_join_false(self):
  612. a, b = self.tables("a", "b")
  613. stmt = (
  614. select(a, b)
  615. .select_from(a.outerjoin(b, false()))
  616. .order_by(a.c.id, b.c.id)
  617. )
  618. self._assert_result(
  619. stmt,
  620. [
  621. (1, None, None),
  622. (2, None, None),
  623. (3, None, None),
  624. (4, None, None),
  625. (5, None, None),
  626. ],
  627. )
  628. def test_outer_join_fk(self):
  629. a, b = self.tables("a", "b")
  630. stmt = select(a, b).select_from(a.join(b)).order_by(a.c.id, b.c.id)
  631. self._assert_result(stmt, [(1, 1, 1), (1, 2, 1), (2, 4, 2), (3, 5, 3)])
  632. class CompoundSelectTest(fixtures.TablesTest):
  633. __backend__ = True
  634. @classmethod
  635. def define_tables(cls, metadata):
  636. Table(
  637. "some_table",
  638. metadata,
  639. Column("id", Integer, primary_key=True),
  640. Column("x", Integer),
  641. Column("y", Integer),
  642. )
  643. @classmethod
  644. def insert_data(cls, connection):
  645. connection.execute(
  646. cls.tables.some_table.insert(),
  647. [
  648. {"id": 1, "x": 1, "y": 2},
  649. {"id": 2, "x": 2, "y": 3},
  650. {"id": 3, "x": 3, "y": 4},
  651. {"id": 4, "x": 4, "y": 5},
  652. ],
  653. )
  654. def _assert_result(self, select, result, params=()):
  655. with config.db.connect() as conn:
  656. eq_(conn.execute(select, params).fetchall(), result)
  657. def test_plain_union(self):
  658. table = self.tables.some_table
  659. s1 = select(table).where(table.c.id == 2)
  660. s2 = select(table).where(table.c.id == 3)
  661. u1 = union(s1, s2)
  662. self._assert_result(
  663. u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
  664. )
  665. def test_select_from_plain_union(self):
  666. table = self.tables.some_table
  667. s1 = select(table).where(table.c.id == 2)
  668. s2 = select(table).where(table.c.id == 3)
  669. u1 = union(s1, s2).alias().select()
  670. self._assert_result(
  671. u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
  672. )
  673. @testing.requires.order_by_col_from_union
  674. @testing.requires.parens_in_union_contained_select_w_limit_offset
  675. def test_limit_offset_selectable_in_unions(self):
  676. table = self.tables.some_table
  677. s1 = select(table).where(table.c.id == 2).limit(1).order_by(table.c.id)
  678. s2 = select(table).where(table.c.id == 3).limit(1).order_by(table.c.id)
  679. u1 = union(s1, s2).limit(2)
  680. self._assert_result(
  681. u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
  682. )
  683. @testing.requires.parens_in_union_contained_select_wo_limit_offset
  684. def test_order_by_selectable_in_unions(self):
  685. table = self.tables.some_table
  686. s1 = select(table).where(table.c.id == 2).order_by(table.c.id)
  687. s2 = select(table).where(table.c.id == 3).order_by(table.c.id)
  688. u1 = union(s1, s2).limit(2)
  689. self._assert_result(
  690. u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
  691. )
  692. def test_distinct_selectable_in_unions(self):
  693. table = self.tables.some_table
  694. s1 = select(table).where(table.c.id == 2).distinct()
  695. s2 = select(table).where(table.c.id == 3).distinct()
  696. u1 = union(s1, s2).limit(2)
  697. self._assert_result(
  698. u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
  699. )
  700. @testing.requires.parens_in_union_contained_select_w_limit_offset
  701. def test_limit_offset_in_unions_from_alias(self):
  702. table = self.tables.some_table
  703. s1 = select(table).where(table.c.id == 2).limit(1).order_by(table.c.id)
  704. s2 = select(table).where(table.c.id == 3).limit(1).order_by(table.c.id)
  705. # this necessarily has double parens
  706. u1 = union(s1, s2).alias()
  707. self._assert_result(
  708. u1.select().limit(2).order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
  709. )
  710. def test_limit_offset_aliased_selectable_in_unions(self):
  711. table = self.tables.some_table
  712. s1 = (
  713. select(table)
  714. .where(table.c.id == 2)
  715. .limit(1)
  716. .order_by(table.c.id)
  717. .alias()
  718. .select()
  719. )
  720. s2 = (
  721. select(table)
  722. .where(table.c.id == 3)
  723. .limit(1)
  724. .order_by(table.c.id)
  725. .alias()
  726. .select()
  727. )
  728. u1 = union(s1, s2).limit(2)
  729. self._assert_result(
  730. u1.order_by(u1.selected_columns.id), [(2, 2, 3), (3, 3, 4)]
  731. )
  732. class PostCompileParamsTest(
  733. AssertsExecutionResults, AssertsCompiledSQL, fixtures.TablesTest
  734. ):
  735. __backend__ = True
  736. __requires__ = ("standard_cursor_sql",)
  737. @classmethod
  738. def define_tables(cls, metadata):
  739. Table(
  740. "some_table",
  741. metadata,
  742. Column("id", Integer, primary_key=True),
  743. Column("x", Integer),
  744. Column("y", Integer),
  745. Column("z", String(50)),
  746. )
  747. @classmethod
  748. def insert_data(cls, connection):
  749. connection.execute(
  750. cls.tables.some_table.insert(),
  751. [
  752. {"id": 1, "x": 1, "y": 2, "z": "z1"},
  753. {"id": 2, "x": 2, "y": 3, "z": "z2"},
  754. {"id": 3, "x": 3, "y": 4, "z": "z3"},
  755. {"id": 4, "x": 4, "y": 5, "z": "z4"},
  756. ],
  757. )
  758. def test_compile(self):
  759. table = self.tables.some_table
  760. stmt = select(table.c.id).where(
  761. table.c.x == bindparam("q", literal_execute=True)
  762. )
  763. self.assert_compile(
  764. stmt,
  765. "SELECT some_table.id FROM some_table "
  766. "WHERE some_table.x = __[POSTCOMPILE_q]",
  767. {},
  768. )
  769. def test_compile_literal_binds(self):
  770. table = self.tables.some_table
  771. stmt = select(table.c.id).where(
  772. table.c.x == bindparam("q", 10, literal_execute=True)
  773. )
  774. self.assert_compile(
  775. stmt,
  776. "SELECT some_table.id FROM some_table WHERE some_table.x = 10",
  777. {},
  778. literal_binds=True,
  779. )
  780. def test_execute(self):
  781. table = self.tables.some_table
  782. stmt = select(table.c.id).where(
  783. table.c.x == bindparam("q", literal_execute=True)
  784. )
  785. with self.sql_execution_asserter() as asserter:
  786. with config.db.connect() as conn:
  787. conn.execute(stmt, dict(q=10))
  788. asserter.assert_(
  789. CursorSQL(
  790. "SELECT some_table.id \nFROM some_table "
  791. "\nWHERE some_table.x = 10",
  792. () if config.db.dialect.positional else {},
  793. )
  794. )
  795. def test_execute_expanding_plus_literal_execute(self):
  796. table = self.tables.some_table
  797. stmt = select(table.c.id).where(
  798. table.c.x.in_(bindparam("q", expanding=True, literal_execute=True))
  799. )
  800. with self.sql_execution_asserter() as asserter:
  801. with config.db.connect() as conn:
  802. conn.execute(stmt, dict(q=[5, 6, 7]))
  803. asserter.assert_(
  804. CursorSQL(
  805. "SELECT some_table.id \nFROM some_table "
  806. "\nWHERE some_table.x IN (5, 6, 7)",
  807. () if config.db.dialect.positional else {},
  808. )
  809. )
  810. @testing.requires.tuple_in
  811. def test_execute_tuple_expanding_plus_literal_execute(self):
  812. table = self.tables.some_table
  813. stmt = select(table.c.id).where(
  814. tuple_(table.c.x, table.c.y).in_(
  815. bindparam("q", expanding=True, literal_execute=True)
  816. )
  817. )
  818. with self.sql_execution_asserter() as asserter:
  819. with config.db.connect() as conn:
  820. conn.execute(stmt, dict(q=[(5, 10), (12, 18)]))
  821. asserter.assert_(
  822. CursorSQL(
  823. "SELECT some_table.id \nFROM some_table "
  824. "\nWHERE (some_table.x, some_table.y) "
  825. "IN (%s(5, 10), (12, 18))"
  826. % ("VALUES " if config.db.dialect.tuple_in_values else ""),
  827. () if config.db.dialect.positional else {},
  828. )
  829. )
  830. @testing.requires.tuple_in
  831. def test_execute_tuple_expanding_plus_literal_heterogeneous_execute(self):
  832. table = self.tables.some_table
  833. stmt = select(table.c.id).where(
  834. tuple_(table.c.x, table.c.z).in_(
  835. bindparam("q", expanding=True, literal_execute=True)
  836. )
  837. )
  838. with self.sql_execution_asserter() as asserter:
  839. with config.db.connect() as conn:
  840. conn.execute(stmt, dict(q=[(5, "z1"), (12, "z3")]))
  841. asserter.assert_(
  842. CursorSQL(
  843. "SELECT some_table.id \nFROM some_table "
  844. "\nWHERE (some_table.x, some_table.z) "
  845. "IN (%s(5, 'z1'), (12, 'z3'))"
  846. % ("VALUES " if config.db.dialect.tuple_in_values else ""),
  847. () if config.db.dialect.positional else {},
  848. )
  849. )
  850. class ExpandingBoundInTest(fixtures.TablesTest):
  851. __backend__ = True
  852. @classmethod
  853. def define_tables(cls, metadata):
  854. Table(
  855. "some_table",
  856. metadata,
  857. Column("id", Integer, primary_key=True),
  858. Column("x", Integer),
  859. Column("y", Integer),
  860. Column("z", String(50)),
  861. )
  862. @classmethod
  863. def insert_data(cls, connection):
  864. connection.execute(
  865. cls.tables.some_table.insert(),
  866. [
  867. {"id": 1, "x": 1, "y": 2, "z": "z1"},
  868. {"id": 2, "x": 2, "y": 3, "z": "z2"},
  869. {"id": 3, "x": 3, "y": 4, "z": "z3"},
  870. {"id": 4, "x": 4, "y": 5, "z": "z4"},
  871. ],
  872. )
  873. def _assert_result(self, select, result, params=()):
  874. with config.db.connect() as conn:
  875. eq_(conn.execute(select, params).fetchall(), result)
  876. def test_multiple_empty_sets_bindparam(self):
  877. # test that any anonymous aliasing used by the dialect
  878. # is fine with duplicates
  879. table = self.tables.some_table
  880. stmt = (
  881. select(table.c.id)
  882. .where(table.c.x.in_(bindparam("q")))
  883. .where(table.c.y.in_(bindparam("p")))
  884. .order_by(table.c.id)
  885. )
  886. self._assert_result(stmt, [], params={"q": [], "p": []})
  887. def test_multiple_empty_sets_direct(self):
  888. # test that any anonymous aliasing used by the dialect
  889. # is fine with duplicates
  890. table = self.tables.some_table
  891. stmt = (
  892. select(table.c.id)
  893. .where(table.c.x.in_([]))
  894. .where(table.c.y.in_([]))
  895. .order_by(table.c.id)
  896. )
  897. self._assert_result(stmt, [])
  898. @testing.requires.tuple_in_w_empty
  899. def test_empty_heterogeneous_tuples_bindparam(self):
  900. table = self.tables.some_table
  901. stmt = (
  902. select(table.c.id)
  903. .where(tuple_(table.c.x, table.c.z).in_(bindparam("q")))
  904. .order_by(table.c.id)
  905. )
  906. self._assert_result(stmt, [], params={"q": []})
  907. @testing.requires.tuple_in_w_empty
  908. def test_empty_heterogeneous_tuples_direct(self):
  909. table = self.tables.some_table
  910. def go(val, expected):
  911. stmt = (
  912. select(table.c.id)
  913. .where(tuple_(table.c.x, table.c.z).in_(val))
  914. .order_by(table.c.id)
  915. )
  916. self._assert_result(stmt, expected)
  917. go([], [])
  918. go([(2, "z2"), (3, "z3"), (4, "z4")], [(2,), (3,), (4,)])
  919. go([], [])
  920. @testing.requires.tuple_in_w_empty
  921. def test_empty_homogeneous_tuples_bindparam(self):
  922. table = self.tables.some_table
  923. stmt = (
  924. select(table.c.id)
  925. .where(tuple_(table.c.x, table.c.y).in_(bindparam("q")))
  926. .order_by(table.c.id)
  927. )
  928. self._assert_result(stmt, [], params={"q": []})
  929. @testing.requires.tuple_in_w_empty
  930. def test_empty_homogeneous_tuples_direct(self):
  931. table = self.tables.some_table
  932. def go(val, expected):
  933. stmt = (
  934. select(table.c.id)
  935. .where(tuple_(table.c.x, table.c.y).in_(val))
  936. .order_by(table.c.id)
  937. )
  938. self._assert_result(stmt, expected)
  939. go([], [])
  940. go([(1, 2), (2, 3), (3, 4)], [(1,), (2,), (3,)])
  941. go([], [])
  942. def test_bound_in_scalar_bindparam(self):
  943. table = self.tables.some_table
  944. stmt = (
  945. select(table.c.id)
  946. .where(table.c.x.in_(bindparam("q")))
  947. .order_by(table.c.id)
  948. )
  949. self._assert_result(stmt, [(2,), (3,), (4,)], params={"q": [2, 3, 4]})
  950. def test_bound_in_scalar_direct(self):
  951. table = self.tables.some_table
  952. stmt = (
  953. select(table.c.id)
  954. .where(table.c.x.in_([2, 3, 4]))
  955. .order_by(table.c.id)
  956. )
  957. self._assert_result(stmt, [(2,), (3,), (4,)])
  958. def test_nonempty_in_plus_empty_notin(self):
  959. table = self.tables.some_table
  960. stmt = (
  961. select(table.c.id)
  962. .where(table.c.x.in_([2, 3]))
  963. .where(table.c.id.not_in([]))
  964. .order_by(table.c.id)
  965. )
  966. self._assert_result(stmt, [(2,), (3,)])
  967. def test_empty_in_plus_notempty_notin(self):
  968. table = self.tables.some_table
  969. stmt = (
  970. select(table.c.id)
  971. .where(table.c.x.in_([]))
  972. .where(table.c.id.not_in([2, 3]))
  973. .order_by(table.c.id)
  974. )
  975. self._assert_result(stmt, [])
  976. def test_typed_str_in(self):
  977. """test related to #7292.
  978. as a type is given to the bound param, there is no ambiguity
  979. to the type of element.
  980. """
  981. stmt = text(
  982. "select id FROM some_table WHERE z IN :q ORDER BY id"
  983. ).bindparams(bindparam("q", type_=String, expanding=True))
  984. self._assert_result(
  985. stmt,
  986. [(2,), (3,), (4,)],
  987. params={"q": ["z2", "z3", "z4"]},
  988. )
  989. def test_untyped_str_in(self):
  990. """test related to #7292.
  991. for untyped expression, we look at the types of elements.
  992. Test for Sequence to detect tuple in. but not strings or bytes!
  993. as always....
  994. """
  995. stmt = text(
  996. "select id FROM some_table WHERE z IN :q ORDER BY id"
  997. ).bindparams(bindparam("q", expanding=True))
  998. self._assert_result(
  999. stmt,
  1000. [(2,), (3,), (4,)],
  1001. params={"q": ["z2", "z3", "z4"]},
  1002. )
  1003. @testing.requires.tuple_in
  1004. def test_bound_in_two_tuple_bindparam(self):
  1005. table = self.tables.some_table
  1006. stmt = (
  1007. select(table.c.id)
  1008. .where(tuple_(table.c.x, table.c.y).in_(bindparam("q")))
  1009. .order_by(table.c.id)
  1010. )
  1011. self._assert_result(
  1012. stmt, [(2,), (3,), (4,)], params={"q": [(2, 3), (3, 4), (4, 5)]}
  1013. )
  1014. @testing.requires.tuple_in
  1015. def test_bound_in_two_tuple_direct(self):
  1016. table = self.tables.some_table
  1017. stmt = (
  1018. select(table.c.id)
  1019. .where(tuple_(table.c.x, table.c.y).in_([(2, 3), (3, 4), (4, 5)]))
  1020. .order_by(table.c.id)
  1021. )
  1022. self._assert_result(stmt, [(2,), (3,), (4,)])
  1023. @testing.requires.tuple_in
  1024. def test_bound_in_heterogeneous_two_tuple_bindparam(self):
  1025. table = self.tables.some_table
  1026. stmt = (
  1027. select(table.c.id)
  1028. .where(tuple_(table.c.x, table.c.z).in_(bindparam("q")))
  1029. .order_by(table.c.id)
  1030. )
  1031. self._assert_result(
  1032. stmt,
  1033. [(2,), (3,), (4,)],
  1034. params={"q": [(2, "z2"), (3, "z3"), (4, "z4")]},
  1035. )
  1036. @testing.requires.tuple_in
  1037. def test_bound_in_heterogeneous_two_tuple_direct(self):
  1038. table = self.tables.some_table
  1039. stmt = (
  1040. select(table.c.id)
  1041. .where(
  1042. tuple_(table.c.x, table.c.z).in_(
  1043. [(2, "z2"), (3, "z3"), (4, "z4")]
  1044. )
  1045. )
  1046. .order_by(table.c.id)
  1047. )
  1048. self._assert_result(
  1049. stmt,
  1050. [(2,), (3,), (4,)],
  1051. )
  1052. @testing.requires.tuple_in
  1053. def test_bound_in_heterogeneous_two_tuple_text_bindparam(self):
  1054. # note this becomes ARRAY if we dont use expanding
  1055. # explicitly right now
  1056. stmt = text(
  1057. "select id FROM some_table WHERE (x, z) IN :q ORDER BY id"
  1058. ).bindparams(bindparam("q", expanding=True))
  1059. self._assert_result(
  1060. stmt,
  1061. [(2,), (3,), (4,)],
  1062. params={"q": [(2, "z2"), (3, "z3"), (4, "z4")]},
  1063. )
  1064. @testing.requires.tuple_in
  1065. def test_bound_in_heterogeneous_two_tuple_typed_bindparam_non_tuple(self):
  1066. class LikeATuple(collections_abc.Sequence):
  1067. def __init__(self, *data):
  1068. self._data = data
  1069. def __iter__(self):
  1070. return iter(self._data)
  1071. def __getitem__(self, idx):
  1072. return self._data[idx]
  1073. def __len__(self):
  1074. return len(self._data)
  1075. stmt = text(
  1076. "select id FROM some_table WHERE (x, z) IN :q ORDER BY id"
  1077. ).bindparams(
  1078. bindparam(
  1079. "q", type_=TupleType(Integer(), String()), expanding=True
  1080. )
  1081. )
  1082. self._assert_result(
  1083. stmt,
  1084. [(2,), (3,), (4,)],
  1085. params={
  1086. "q": [
  1087. LikeATuple(2, "z2"),
  1088. LikeATuple(3, "z3"),
  1089. LikeATuple(4, "z4"),
  1090. ]
  1091. },
  1092. )
  1093. @testing.requires.tuple_in
  1094. def test_bound_in_heterogeneous_two_tuple_text_bindparam_non_tuple(self):
  1095. # note this becomes ARRAY if we dont use expanding
  1096. # explicitly right now
  1097. class LikeATuple(collections_abc.Sequence):
  1098. def __init__(self, *data):
  1099. self._data = data
  1100. def __iter__(self):
  1101. return iter(self._data)
  1102. def __getitem__(self, idx):
  1103. return self._data[idx]
  1104. def __len__(self):
  1105. return len(self._data)
  1106. stmt = text(
  1107. "select id FROM some_table WHERE (x, z) IN :q ORDER BY id"
  1108. ).bindparams(bindparam("q", expanding=True))
  1109. self._assert_result(
  1110. stmt,
  1111. [(2,), (3,), (4,)],
  1112. params={
  1113. "q": [
  1114. LikeATuple(2, "z2"),
  1115. LikeATuple(3, "z3"),
  1116. LikeATuple(4, "z4"),
  1117. ]
  1118. },
  1119. )
  1120. def test_empty_set_against_integer_bindparam(self):
  1121. table = self.tables.some_table
  1122. stmt = (
  1123. select(table.c.id)
  1124. .where(table.c.x.in_(bindparam("q")))
  1125. .order_by(table.c.id)
  1126. )
  1127. self._assert_result(stmt, [], params={"q": []})
  1128. def test_empty_set_against_integer_direct(self):
  1129. table = self.tables.some_table
  1130. stmt = select(table.c.id).where(table.c.x.in_([])).order_by(table.c.id)
  1131. self._assert_result(stmt, [])
  1132. def test_empty_set_against_integer_negation_bindparam(self):
  1133. table = self.tables.some_table
  1134. stmt = (
  1135. select(table.c.id)
  1136. .where(table.c.x.not_in(bindparam("q")))
  1137. .order_by(table.c.id)
  1138. )
  1139. self._assert_result(stmt, [(1,), (2,), (3,), (4,)], params={"q": []})
  1140. def test_empty_set_against_integer_negation_direct(self):
  1141. table = self.tables.some_table
  1142. stmt = (
  1143. select(table.c.id).where(table.c.x.not_in([])).order_by(table.c.id)
  1144. )
  1145. self._assert_result(stmt, [(1,), (2,), (3,), (4,)])
  1146. def test_empty_set_against_string_bindparam(self):
  1147. table = self.tables.some_table
  1148. stmt = (
  1149. select(table.c.id)
  1150. .where(table.c.z.in_(bindparam("q")))
  1151. .order_by(table.c.id)
  1152. )
  1153. self._assert_result(stmt, [], params={"q": []})
  1154. def test_empty_set_against_string_direct(self):
  1155. table = self.tables.some_table
  1156. stmt = select(table.c.id).where(table.c.z.in_([])).order_by(table.c.id)
  1157. self._assert_result(stmt, [])
  1158. def test_empty_set_against_string_negation_bindparam(self):
  1159. table = self.tables.some_table
  1160. stmt = (
  1161. select(table.c.id)
  1162. .where(table.c.z.not_in(bindparam("q")))
  1163. .order_by(table.c.id)
  1164. )
  1165. self._assert_result(stmt, [(1,), (2,), (3,), (4,)], params={"q": []})
  1166. def test_empty_set_against_string_negation_direct(self):
  1167. table = self.tables.some_table
  1168. stmt = (
  1169. select(table.c.id).where(table.c.z.not_in([])).order_by(table.c.id)
  1170. )
  1171. self._assert_result(stmt, [(1,), (2,), (3,), (4,)])
  1172. def test_null_in_empty_set_is_false_bindparam(self, connection):
  1173. stmt = select(
  1174. case(
  1175. (
  1176. null().in_(bindparam("foo", value=())),
  1177. true(),
  1178. ),
  1179. else_=false(),
  1180. )
  1181. )
  1182. in_(connection.execute(stmt).fetchone()[0], (False, 0))
  1183. def test_null_in_empty_set_is_false_direct(self, connection):
  1184. stmt = select(
  1185. case(
  1186. (
  1187. null().in_([]),
  1188. true(),
  1189. ),
  1190. else_=false(),
  1191. )
  1192. )
  1193. in_(connection.execute(stmt).fetchone()[0], (False, 0))
  1194. class LikeFunctionsTest(fixtures.TablesTest):
  1195. __backend__ = True
  1196. run_inserts = "once"
  1197. run_deletes = None
  1198. @classmethod
  1199. def define_tables(cls, metadata):
  1200. Table(
  1201. "some_table",
  1202. metadata,
  1203. Column("id", Integer, primary_key=True),
  1204. Column("data", String(50)),
  1205. )
  1206. @classmethod
  1207. def insert_data(cls, connection):
  1208. connection.execute(
  1209. cls.tables.some_table.insert(),
  1210. [
  1211. {"id": 1, "data": "abcdefg"},
  1212. {"id": 2, "data": "ab/cdefg"},
  1213. {"id": 3, "data": "ab%cdefg"},
  1214. {"id": 4, "data": "ab_cdefg"},
  1215. {"id": 5, "data": "abcde/fg"},
  1216. {"id": 6, "data": "abcde%fg"},
  1217. {"id": 7, "data": "ab#cdefg"},
  1218. {"id": 8, "data": "ab9cdefg"},
  1219. {"id": 9, "data": "abcde#fg"},
  1220. {"id": 10, "data": "abcd9fg"},
  1221. {"id": 11, "data": None},
  1222. ],
  1223. )
  1224. def _test(self, expr, expected):
  1225. some_table = self.tables.some_table
  1226. with config.db.connect() as conn:
  1227. rows = {
  1228. value
  1229. for value, in conn.execute(select(some_table.c.id).where(expr))
  1230. }
  1231. eq_(rows, expected)
  1232. def test_startswith_unescaped(self):
  1233. col = self.tables.some_table.c.data
  1234. self._test(col.startswith("ab%c"), {1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
  1235. def test_startswith_autoescape(self):
  1236. col = self.tables.some_table.c.data
  1237. self._test(col.startswith("ab%c", autoescape=True), {3})
  1238. def test_startswith_sqlexpr(self):
  1239. col = self.tables.some_table.c.data
  1240. self._test(
  1241. col.startswith(literal_column("'ab%c'")),
  1242. {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  1243. )
  1244. def test_startswith_escape(self):
  1245. col = self.tables.some_table.c.data
  1246. self._test(col.startswith("ab##c", escape="#"), {7})
  1247. def test_startswith_autoescape_escape(self):
  1248. col = self.tables.some_table.c.data
  1249. self._test(col.startswith("ab%c", autoescape=True, escape="#"), {3})
  1250. self._test(col.startswith("ab#c", autoescape=True, escape="#"), {7})
  1251. def test_endswith_unescaped(self):
  1252. col = self.tables.some_table.c.data
  1253. self._test(col.endswith("e%fg"), {1, 2, 3, 4, 5, 6, 7, 8, 9})
  1254. def test_endswith_sqlexpr(self):
  1255. col = self.tables.some_table.c.data
  1256. self._test(
  1257. col.endswith(literal_column("'e%fg'")), {1, 2, 3, 4, 5, 6, 7, 8, 9}
  1258. )
  1259. def test_endswith_autoescape(self):
  1260. col = self.tables.some_table.c.data
  1261. self._test(col.endswith("e%fg", autoescape=True), {6})
  1262. def test_endswith_escape(self):
  1263. col = self.tables.some_table.c.data
  1264. self._test(col.endswith("e##fg", escape="#"), {9})
  1265. def test_endswith_autoescape_escape(self):
  1266. col = self.tables.some_table.c.data
  1267. self._test(col.endswith("e%fg", autoescape=True, escape="#"), {6})
  1268. self._test(col.endswith("e#fg", autoescape=True, escape="#"), {9})
  1269. def test_contains_unescaped(self):
  1270. col = self.tables.some_table.c.data
  1271. self._test(col.contains("b%cde"), {1, 2, 3, 4, 5, 6, 7, 8, 9})
  1272. def test_contains_autoescape(self):
  1273. col = self.tables.some_table.c.data
  1274. self._test(col.contains("b%cde", autoescape=True), {3})
  1275. def test_contains_escape(self):
  1276. col = self.tables.some_table.c.data
  1277. self._test(col.contains("b##cde", escape="#"), {7})
  1278. def test_contains_autoescape_escape(self):
  1279. col = self.tables.some_table.c.data
  1280. self._test(col.contains("b%cd", autoescape=True, escape="#"), {3})
  1281. self._test(col.contains("b#cd", autoescape=True, escape="#"), {7})
  1282. @testing.requires.regexp_match
  1283. def test_not_regexp_match(self):
  1284. col = self.tables.some_table.c.data
  1285. self._test(~col.regexp_match("a.cde"), {2, 3, 4, 7, 8, 10})
  1286. @testing.requires.regexp_replace
  1287. def test_regexp_replace(self):
  1288. col = self.tables.some_table.c.data
  1289. self._test(
  1290. col.regexp_replace("a.cde", "FOO").contains("FOO"), {1, 5, 6, 9}
  1291. )
  1292. @testing.requires.regexp_match
  1293. @testing.combinations(
  1294. ("a.cde", {1, 5, 6, 9}),
  1295. ("abc", {1, 5, 6, 9, 10}),
  1296. ("^abc", {1, 5, 6, 9, 10}),
  1297. ("9cde", {8}),
  1298. ("^a", set(range(1, 11))),
  1299. ("(b|c)", set(range(1, 11))),
  1300. ("^(b|c)", set()),
  1301. )
  1302. def test_regexp_match(self, text, expected):
  1303. col = self.tables.some_table.c.data
  1304. self._test(col.regexp_match(text), expected)
  1305. class ComputedColumnTest(fixtures.TablesTest):
  1306. __backend__ = True
  1307. __requires__ = ("computed_columns",)
  1308. @classmethod
  1309. def define_tables(cls, metadata):
  1310. Table(
  1311. "square",
  1312. metadata,
  1313. Column("id", Integer, primary_key=True),
  1314. Column("side", Integer),
  1315. Column("area", Integer, Computed("side * side")),
  1316. Column("perimeter", Integer, Computed("4 * side")),
  1317. )
  1318. @classmethod
  1319. def insert_data(cls, connection):
  1320. connection.execute(
  1321. cls.tables.square.insert(),
  1322. [{"id": 1, "side": 10}, {"id": 10, "side": 42}],
  1323. )
  1324. def test_select_all(self):
  1325. with config.db.connect() as conn:
  1326. res = conn.execute(
  1327. select(text("*"))
  1328. .select_from(self.tables.square)
  1329. .order_by(self.tables.square.c.id)
  1330. ).fetchall()
  1331. eq_(res, [(1, 10, 100, 40), (10, 42, 1764, 168)])
  1332. def test_select_columns(self):
  1333. with config.db.connect() as conn:
  1334. res = conn.execute(
  1335. select(
  1336. self.tables.square.c.area, self.tables.square.c.perimeter
  1337. )
  1338. .select_from(self.tables.square)
  1339. .order_by(self.tables.square.c.id)
  1340. ).fetchall()
  1341. eq_(res, [(100, 40), (1764, 168)])
  1342. class IdentityColumnTest(fixtures.TablesTest):
  1343. __backend__ = True
  1344. __requires__ = ("identity_columns",)
  1345. run_inserts = "once"
  1346. run_deletes = "once"
  1347. @classmethod
  1348. def define_tables(cls, metadata):
  1349. Table(
  1350. "tbl_a",
  1351. metadata,
  1352. Column(
  1353. "id",
  1354. Integer,
  1355. Identity(
  1356. always=True, start=42, nominvalue=True, nomaxvalue=True
  1357. ),
  1358. primary_key=True,
  1359. ),
  1360. Column("desc", String(100)),
  1361. )
  1362. Table(
  1363. "tbl_b",
  1364. metadata,
  1365. Column(
  1366. "id",
  1367. Integer,
  1368. Identity(increment=-5, start=0, minvalue=-1000, maxvalue=0),
  1369. primary_key=True,
  1370. ),
  1371. Column("desc", String(100)),
  1372. )
  1373. @classmethod
  1374. def insert_data(cls, connection):
  1375. connection.execute(
  1376. cls.tables.tbl_a.insert(),
  1377. [{"desc": "a"}, {"desc": "b"}],
  1378. )
  1379. connection.execute(
  1380. cls.tables.tbl_b.insert(),
  1381. [{"desc": "a"}, {"desc": "b"}],
  1382. )
  1383. connection.execute(
  1384. cls.tables.tbl_b.insert(),
  1385. [{"id": 42, "desc": "c"}],
  1386. )
  1387. def test_select_all(self, connection):
  1388. res = connection.execute(
  1389. select(text("*"))
  1390. .select_from(self.tables.tbl_a)
  1391. .order_by(self.tables.tbl_a.c.id)
  1392. ).fetchall()
  1393. eq_(res, [(42, "a"), (43, "b")])
  1394. res = connection.execute(
  1395. select(text("*"))
  1396. .select_from(self.tables.tbl_b)
  1397. .order_by(self.tables.tbl_b.c.id)
  1398. ).fetchall()
  1399. eq_(res, [(-5, "b"), (0, "a"), (42, "c")])
  1400. def test_select_columns(self, connection):
  1401. res = connection.execute(
  1402. select(self.tables.tbl_a.c.id).order_by(self.tables.tbl_a.c.id)
  1403. ).fetchall()
  1404. eq_(res, [(42,), (43,)])
  1405. @testing.requires.identity_columns_standard
  1406. def test_insert_always_error(self, connection):
  1407. def fn():
  1408. connection.execute(
  1409. self.tables.tbl_a.insert(),
  1410. [{"id": 200, "desc": "a"}],
  1411. )
  1412. assert_raises((DatabaseError, ProgrammingError), fn)
  1413. class IdentityAutoincrementTest(fixtures.TablesTest):
  1414. __backend__ = True
  1415. __requires__ = ("autoincrement_without_sequence",)
  1416. @classmethod
  1417. def define_tables(cls, metadata):
  1418. Table(
  1419. "tbl",
  1420. metadata,
  1421. Column(
  1422. "id",
  1423. Integer,
  1424. Identity(),
  1425. primary_key=True,
  1426. autoincrement=True,
  1427. ),
  1428. Column("desc", String(100)),
  1429. )
  1430. def test_autoincrement_with_identity(self, connection):
  1431. res = connection.execute(self.tables.tbl.insert(), {"desc": "row"})
  1432. res = connection.execute(self.tables.tbl.select()).first()
  1433. eq_(res, (1, "row"))
  1434. class ExistsTest(fixtures.TablesTest):
  1435. __backend__ = True
  1436. @classmethod
  1437. def define_tables(cls, metadata):
  1438. Table(
  1439. "stuff",
  1440. metadata,
  1441. Column("id", Integer, primary_key=True),
  1442. Column("data", String(50)),
  1443. )
  1444. @classmethod
  1445. def insert_data(cls, connection):
  1446. connection.execute(
  1447. cls.tables.stuff.insert(),
  1448. [
  1449. {"id": 1, "data": "some data"},
  1450. {"id": 2, "data": "some data"},
  1451. {"id": 3, "data": "some data"},
  1452. {"id": 4, "data": "some other data"},
  1453. ],
  1454. )
  1455. def test_select_exists(self, connection):
  1456. stuff = self.tables.stuff
  1457. eq_(
  1458. connection.execute(
  1459. select(literal(1)).where(
  1460. exists().where(stuff.c.data == "some data")
  1461. )
  1462. ).fetchall(),
  1463. [(1,)],
  1464. )
  1465. def test_select_exists_false(self, connection):
  1466. stuff = self.tables.stuff
  1467. eq_(
  1468. connection.execute(
  1469. select(literal(1)).where(
  1470. exists().where(stuff.c.data == "no data")
  1471. )
  1472. ).fetchall(),
  1473. [],
  1474. )
  1475. class DistinctOnTest(AssertsCompiledSQL, fixtures.TablesTest):
  1476. __backend__ = True
  1477. @testing.fails_if(testing.requires.supports_distinct_on)
  1478. def test_distinct_on(self):
  1479. stm = select("*").distinct(column("q")).select_from(table("foo"))
  1480. with testing.expect_deprecated(
  1481. "DISTINCT ON is currently supported only by the PostgreSQL "
  1482. ):
  1483. self.assert_compile(stm, "SELECT DISTINCT * FROM foo")
  1484. class IsOrIsNotDistinctFromTest(fixtures.TablesTest):
  1485. __backend__ = True
  1486. __requires__ = ("supports_is_distinct_from",)
  1487. @classmethod
  1488. def define_tables(cls, metadata):
  1489. Table(
  1490. "is_distinct_test",
  1491. metadata,
  1492. Column("id", Integer, primary_key=True),
  1493. Column("col_a", Integer, nullable=True),
  1494. Column("col_b", Integer, nullable=True),
  1495. )
  1496. @testing.combinations(
  1497. ("both_int_different", 0, 1, 1),
  1498. ("both_int_same", 1, 1, 0),
  1499. ("one_null_first", None, 1, 1),
  1500. ("one_null_second", 0, None, 1),
  1501. ("both_null", None, None, 0),
  1502. id_="iaaa",
  1503. argnames="col_a_value, col_b_value, expected_row_count_for_is",
  1504. )
  1505. def test_is_or_is_not_distinct_from(
  1506. self, col_a_value, col_b_value, expected_row_count_for_is, connection
  1507. ):
  1508. tbl = self.tables.is_distinct_test
  1509. connection.execute(
  1510. tbl.insert(),
  1511. [{"id": 1, "col_a": col_a_value, "col_b": col_b_value}],
  1512. )
  1513. result = connection.execute(
  1514. tbl.select().where(tbl.c.col_a.is_distinct_from(tbl.c.col_b))
  1515. ).fetchall()
  1516. eq_(
  1517. len(result),
  1518. expected_row_count_for_is,
  1519. )
  1520. expected_row_count_for_is_not = (
  1521. 1 if expected_row_count_for_is == 0 else 0
  1522. )
  1523. result = connection.execute(
  1524. tbl.select().where(tbl.c.col_a.is_not_distinct_from(tbl.c.col_b))
  1525. ).fetchall()
  1526. eq_(
  1527. len(result),
  1528. expected_row_count_for_is_not,
  1529. )
  1530. class WindowFunctionTest(fixtures.TablesTest):
  1531. __requires__ = ("window_functions",)
  1532. __backend__ = True
  1533. @classmethod
  1534. def define_tables(cls, metadata):
  1535. Table(
  1536. "some_table",
  1537. metadata,
  1538. Column("id", Integer, primary_key=True),
  1539. Column("col1", Integer),
  1540. Column("col2", Integer),
  1541. )
  1542. @classmethod
  1543. def insert_data(cls, connection):
  1544. connection.execute(
  1545. cls.tables.some_table.insert(),
  1546. [{"id": i, "col1": i, "col2": i * 5} for i in range(1, 50)],
  1547. )
  1548. def test_window(self, connection):
  1549. some_table = self.tables.some_table
  1550. rows = connection.execute(
  1551. select(
  1552. func.max(some_table.c.col2).over(
  1553. order_by=[some_table.c.col1.desc()]
  1554. )
  1555. ).where(some_table.c.col1 < 20)
  1556. ).all()
  1557. eq_(rows, [(95,) for i in range(19)])
  1558. def test_window_rows_between(self, connection):
  1559. some_table = self.tables.some_table
  1560. # note the rows are part of the cache key right now, not handled
  1561. # as binds. this is issue #11515
  1562. rows = connection.execute(
  1563. select(
  1564. func.max(some_table.c.col2).over(
  1565. order_by=[some_table.c.col1],
  1566. rows=(-5, 0),
  1567. )
  1568. )
  1569. ).all()
  1570. eq_(rows, [(i,) for i in range(5, 250, 5)])