int8.out 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. --
  2. -- INT8
  3. -- Test int8 64-bit integers.
  4. --
  5. CREATE TABLE INT8_TBL(q1 int8, q2 int8);
  6. INSERT INTO INT8_TBL VALUES(' 123 ',' 456');
  7. INSERT INTO INT8_TBL VALUES('123 ','4567890123456789');
  8. INSERT INTO INT8_TBL VALUES('4567890123456789','123');
  9. INSERT INTO INT8_TBL VALUES(+4567890123456789,'4567890123456789');
  10. INSERT INTO INT8_TBL VALUES('+4567890123456789','-4567890123456789');
  11. -- bad inputs
  12. INSERT INTO INT8_TBL(q1) VALUES (' ');
  13. ERROR: invalid input syntax for type bigint: " "
  14. LINE 1: INSERT INTO INT8_TBL(q1) VALUES (' ');
  15. ^
  16. INSERT INTO INT8_TBL(q1) VALUES ('xxx');
  17. ERROR: invalid input syntax for type bigint: "xxx"
  18. LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('xxx');
  19. ^
  20. INSERT INTO INT8_TBL(q1) VALUES ('3908203590239580293850293850329485');
  21. ERROR: value "3908203590239580293850293850329485" is out of range for type bigint
  22. LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('39082035902395802938502938...
  23. ^
  24. INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340329840934');
  25. ERROR: value "-1204982019841029840928340329840934" is out of range for type bigint
  26. LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('-1204982019841029840928340...
  27. ^
  28. INSERT INTO INT8_TBL(q1) VALUES ('- 123');
  29. ERROR: invalid input syntax for type bigint: "- 123"
  30. LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('- 123');
  31. ^
  32. INSERT INTO INT8_TBL(q1) VALUES (' 345 5');
  33. ERROR: invalid input syntax for type bigint: " 345 5"
  34. LINE 1: INSERT INTO INT8_TBL(q1) VALUES (' 345 5');
  35. ^
  36. INSERT INTO INT8_TBL(q1) VALUES ('');
  37. ERROR: invalid input syntax for type bigint: ""
  38. LINE 1: INSERT INTO INT8_TBL(q1) VALUES ('');
  39. ^
  40. SELECT * FROM INT8_TBL;
  41. q1 | q2
  42. ------------------+-------------------
  43. 123 | 456
  44. 123 | 4567890123456789
  45. 4567890123456789 | 123
  46. 4567890123456789 | 4567890123456789
  47. 4567890123456789 | -4567890123456789
  48. (5 rows)
  49. -- int8/int8 cmp
  50. SELECT * FROM INT8_TBL WHERE q2 = 4567890123456789;
  51. q1 | q2
  52. ------------------+------------------
  53. 123 | 4567890123456789
  54. 4567890123456789 | 4567890123456789
  55. (2 rows)
  56. SELECT * FROM INT8_TBL WHERE q2 <> 4567890123456789;
  57. q1 | q2
  58. ------------------+-------------------
  59. 123 | 456
  60. 4567890123456789 | 123
  61. 4567890123456789 | -4567890123456789
  62. (3 rows)
  63. SELECT * FROM INT8_TBL WHERE q2 < 4567890123456789;
  64. q1 | q2
  65. ------------------+-------------------
  66. 123 | 456
  67. 4567890123456789 | 123
  68. 4567890123456789 | -4567890123456789
  69. (3 rows)
  70. SELECT * FROM INT8_TBL WHERE q2 > 4567890123456789;
  71. q1 | q2
  72. ----+----
  73. (0 rows)
  74. SELECT * FROM INT8_TBL WHERE q2 <= 4567890123456789;
  75. q1 | q2
  76. ------------------+-------------------
  77. 123 | 456
  78. 123 | 4567890123456789
  79. 4567890123456789 | 123
  80. 4567890123456789 | 4567890123456789
  81. 4567890123456789 | -4567890123456789
  82. (5 rows)
  83. SELECT * FROM INT8_TBL WHERE q2 >= 4567890123456789;
  84. q1 | q2
  85. ------------------+------------------
  86. 123 | 4567890123456789
  87. 4567890123456789 | 4567890123456789
  88. (2 rows)
  89. -- int8/int4 cmp
  90. SELECT * FROM INT8_TBL WHERE q2 = 456;
  91. q1 | q2
  92. -----+-----
  93. 123 | 456
  94. (1 row)
  95. SELECT * FROM INT8_TBL WHERE q2 <> 456;
  96. q1 | q2
  97. ------------------+-------------------
  98. 123 | 4567890123456789
  99. 4567890123456789 | 123
  100. 4567890123456789 | 4567890123456789
  101. 4567890123456789 | -4567890123456789
  102. (4 rows)
  103. SELECT * FROM INT8_TBL WHERE q2 < 456;
  104. q1 | q2
  105. ------------------+-------------------
  106. 4567890123456789 | 123
  107. 4567890123456789 | -4567890123456789
  108. (2 rows)
  109. SELECT * FROM INT8_TBL WHERE q2 > 456;
  110. q1 | q2
  111. ------------------+------------------
  112. 123 | 4567890123456789
  113. 4567890123456789 | 4567890123456789
  114. (2 rows)
  115. SELECT * FROM INT8_TBL WHERE q2 <= 456;
  116. q1 | q2
  117. ------------------+-------------------
  118. 123 | 456
  119. 4567890123456789 | 123
  120. 4567890123456789 | -4567890123456789
  121. (3 rows)
  122. SELECT * FROM INT8_TBL WHERE q2 >= 456;
  123. q1 | q2
  124. ------------------+------------------
  125. 123 | 456
  126. 123 | 4567890123456789
  127. 4567890123456789 | 4567890123456789
  128. (3 rows)
  129. -- int4/int8 cmp
  130. SELECT * FROM INT8_TBL WHERE 123 = q1;
  131. q1 | q2
  132. -----+------------------
  133. 123 | 456
  134. 123 | 4567890123456789
  135. (2 rows)
  136. SELECT * FROM INT8_TBL WHERE 123 <> q1;
  137. q1 | q2
  138. ------------------+-------------------
  139. 4567890123456789 | 123
  140. 4567890123456789 | 4567890123456789
  141. 4567890123456789 | -4567890123456789
  142. (3 rows)
  143. SELECT * FROM INT8_TBL WHERE 123 < q1;
  144. q1 | q2
  145. ------------------+-------------------
  146. 4567890123456789 | 123
  147. 4567890123456789 | 4567890123456789
  148. 4567890123456789 | -4567890123456789
  149. (3 rows)
  150. SELECT * FROM INT8_TBL WHERE 123 > q1;
  151. q1 | q2
  152. ----+----
  153. (0 rows)
  154. SELECT * FROM INT8_TBL WHERE 123 <= q1;
  155. q1 | q2
  156. ------------------+-------------------
  157. 123 | 456
  158. 123 | 4567890123456789
  159. 4567890123456789 | 123
  160. 4567890123456789 | 4567890123456789
  161. 4567890123456789 | -4567890123456789
  162. (5 rows)
  163. SELECT * FROM INT8_TBL WHERE 123 >= q1;
  164. q1 | q2
  165. -----+------------------
  166. 123 | 456
  167. 123 | 4567890123456789
  168. (2 rows)
  169. -- int8/int2 cmp
  170. SELECT * FROM INT8_TBL WHERE q2 = '456'::int2;
  171. q1 | q2
  172. -----+-----
  173. 123 | 456
  174. (1 row)
  175. SELECT * FROM INT8_TBL WHERE q2 <> '456'::int2;
  176. q1 | q2
  177. ------------------+-------------------
  178. 123 | 4567890123456789
  179. 4567890123456789 | 123
  180. 4567890123456789 | 4567890123456789
  181. 4567890123456789 | -4567890123456789
  182. (4 rows)
  183. SELECT * FROM INT8_TBL WHERE q2 < '456'::int2;
  184. q1 | q2
  185. ------------------+-------------------
  186. 4567890123456789 | 123
  187. 4567890123456789 | -4567890123456789
  188. (2 rows)
  189. SELECT * FROM INT8_TBL WHERE q2 > '456'::int2;
  190. q1 | q2
  191. ------------------+------------------
  192. 123 | 4567890123456789
  193. 4567890123456789 | 4567890123456789
  194. (2 rows)
  195. SELECT * FROM INT8_TBL WHERE q2 <= '456'::int2;
  196. q1 | q2
  197. ------------------+-------------------
  198. 123 | 456
  199. 4567890123456789 | 123
  200. 4567890123456789 | -4567890123456789
  201. (3 rows)
  202. SELECT * FROM INT8_TBL WHERE q2 >= '456'::int2;
  203. q1 | q2
  204. ------------------+------------------
  205. 123 | 456
  206. 123 | 4567890123456789
  207. 4567890123456789 | 4567890123456789
  208. (3 rows)
  209. -- int2/int8 cmp
  210. SELECT * FROM INT8_TBL WHERE '123'::int2 = q1;
  211. q1 | q2
  212. -----+------------------
  213. 123 | 456
  214. 123 | 4567890123456789
  215. (2 rows)
  216. SELECT * FROM INT8_TBL WHERE '123'::int2 <> q1;
  217. q1 | q2
  218. ------------------+-------------------
  219. 4567890123456789 | 123
  220. 4567890123456789 | 4567890123456789
  221. 4567890123456789 | -4567890123456789
  222. (3 rows)
  223. SELECT * FROM INT8_TBL WHERE '123'::int2 < q1;
  224. q1 | q2
  225. ------------------+-------------------
  226. 4567890123456789 | 123
  227. 4567890123456789 | 4567890123456789
  228. 4567890123456789 | -4567890123456789
  229. (3 rows)
  230. SELECT * FROM INT8_TBL WHERE '123'::int2 > q1;
  231. q1 | q2
  232. ----+----
  233. (0 rows)
  234. SELECT * FROM INT8_TBL WHERE '123'::int2 <= q1;
  235. q1 | q2
  236. ------------------+-------------------
  237. 123 | 456
  238. 123 | 4567890123456789
  239. 4567890123456789 | 123
  240. 4567890123456789 | 4567890123456789
  241. 4567890123456789 | -4567890123456789
  242. (5 rows)
  243. SELECT * FROM INT8_TBL WHERE '123'::int2 >= q1;
  244. q1 | q2
  245. -----+------------------
  246. 123 | 456
  247. 123 | 4567890123456789
  248. (2 rows)
  249. SELECT q1 AS plus, -q1 AS minus FROM INT8_TBL;
  250. plus | minus
  251. ------------------+-------------------
  252. 123 | -123
  253. 123 | -123
  254. 4567890123456789 | -4567890123456789
  255. 4567890123456789 | -4567890123456789
  256. 4567890123456789 | -4567890123456789
  257. (5 rows)
  258. SELECT q1, q2, q1 + q2 AS plus FROM INT8_TBL;
  259. q1 | q2 | plus
  260. ------------------+-------------------+------------------
  261. 123 | 456 | 579
  262. 123 | 4567890123456789 | 4567890123456912
  263. 4567890123456789 | 123 | 4567890123456912
  264. 4567890123456789 | 4567890123456789 | 9135780246913578
  265. 4567890123456789 | -4567890123456789 | 0
  266. (5 rows)
  267. SELECT q1, q2, q1 - q2 AS minus FROM INT8_TBL;
  268. q1 | q2 | minus
  269. ------------------+-------------------+-------------------
  270. 123 | 456 | -333
  271. 123 | 4567890123456789 | -4567890123456666
  272. 4567890123456789 | 123 | 4567890123456666
  273. 4567890123456789 | 4567890123456789 | 0
  274. 4567890123456789 | -4567890123456789 | 9135780246913578
  275. (5 rows)
  276. SELECT q1, q2, q1 * q2 AS multiply FROM INT8_TBL;
  277. ERROR: bigint out of range
  278. SELECT q1, q2, q1 * q2 AS multiply FROM INT8_TBL
  279. WHERE q1 < 1000 or (q2 > 0 and q2 < 1000);
  280. q1 | q2 | multiply
  281. ------------------+------------------+--------------------
  282. 123 | 456 | 56088
  283. 123 | 4567890123456789 | 561850485185185047
  284. 4567890123456789 | 123 | 561850485185185047
  285. (3 rows)
  286. SELECT q1, q2, q1 / q2 AS divide, q1 % q2 AS mod FROM INT8_TBL;
  287. q1 | q2 | divide | mod
  288. ------------------+-------------------+----------------+-----
  289. 123 | 456 | 0 | 123
  290. 123 | 4567890123456789 | 0 | 123
  291. 4567890123456789 | 123 | 37137318076884 | 57
  292. 4567890123456789 | 4567890123456789 | 1 | 0
  293. 4567890123456789 | -4567890123456789 | -1 | 0
  294. (5 rows)
  295. SELECT 37 + q1 AS plus4 FROM INT8_TBL;
  296. plus4
  297. ------------------
  298. 160
  299. 160
  300. 4567890123456826
  301. 4567890123456826
  302. 4567890123456826
  303. (5 rows)
  304. SELECT 37 - q1 AS minus4 FROM INT8_TBL;
  305. minus4
  306. -------------------
  307. -86
  308. -86
  309. -4567890123456752
  310. -4567890123456752
  311. -4567890123456752
  312. (5 rows)
  313. SELECT 2 * q1 AS "twice int4" FROM INT8_TBL;
  314. twice int4
  315. ------------------
  316. 246
  317. 246
  318. 9135780246913578
  319. 9135780246913578
  320. 9135780246913578
  321. (5 rows)
  322. SELECT q1 * 2 AS "twice int4" FROM INT8_TBL;
  323. twice int4
  324. ------------------
  325. 246
  326. 246
  327. 9135780246913578
  328. 9135780246913578
  329. 9135780246913578
  330. (5 rows)
  331. -- int8 op int4
  332. SELECT q1 + 42::int4 AS "8plus4", q1 - 42::int4 AS "8minus4", q1 * 42::int4 AS "8mul4", q1 / 42::int4 AS "8div4" FROM INT8_TBL;
  333. 8plus4 | 8minus4 | 8mul4 | 8div4
  334. ------------------+------------------+--------------------+-----------------
  335. 165 | 81 | 5166 | 2
  336. 165 | 81 | 5166 | 2
  337. 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
  338. 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
  339. 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
  340. (5 rows)
  341. -- int4 op int8
  342. SELECT 246::int4 + q1 AS "4plus8", 246::int4 - q1 AS "4minus8", 246::int4 * q1 AS "4mul8", 246::int4 / q1 AS "4div8" FROM INT8_TBL;
  343. 4plus8 | 4minus8 | 4mul8 | 4div8
  344. ------------------+-------------------+---------------------+-------
  345. 369 | 123 | 30258 | 2
  346. 369 | 123 | 30258 | 2
  347. 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
  348. 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
  349. 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
  350. (5 rows)
  351. -- int8 op int2
  352. SELECT q1 + 42::int2 AS "8plus2", q1 - 42::int2 AS "8minus2", q1 * 42::int2 AS "8mul2", q1 / 42::int2 AS "8div2" FROM INT8_TBL;
  353. 8plus2 | 8minus2 | 8mul2 | 8div2
  354. ------------------+------------------+--------------------+-----------------
  355. 165 | 81 | 5166 | 2
  356. 165 | 81 | 5166 | 2
  357. 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
  358. 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
  359. 4567890123456831 | 4567890123456747 | 191851385185185138 | 108759288653733
  360. (5 rows)
  361. -- int2 op int8
  362. SELECT 246::int2 + q1 AS "2plus8", 246::int2 - q1 AS "2minus8", 246::int2 * q1 AS "2mul8", 246::int2 / q1 AS "2div8" FROM INT8_TBL;
  363. 2plus8 | 2minus8 | 2mul8 | 2div8
  364. ------------------+-------------------+---------------------+-------
  365. 369 | 123 | 30258 | 2
  366. 369 | 123 | 30258 | 2
  367. 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
  368. 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
  369. 4567890123457035 | -4567890123456543 | 1123700970370370094 | 0
  370. (5 rows)
  371. SELECT q2, abs(q2) FROM INT8_TBL;
  372. q2 | abs
  373. -------------------+------------------
  374. 456 | 456
  375. 4567890123456789 | 4567890123456789
  376. 123 | 123
  377. 4567890123456789 | 4567890123456789
  378. -4567890123456789 | 4567890123456789
  379. (5 rows)
  380. SELECT to_char(q2, 'MI9999999999999999') FROM INT8_TBL;
  381. to_char
  382. -------------------
  383. 456
  384. 4567890123456789
  385. 123
  386. 4567890123456789
  387. -4567890123456789
  388. (5 rows)
  389. SELECT to_char(q2, 'FMS9999999999999999') FROM INT8_TBL;
  390. to_char
  391. -------------------
  392. +456
  393. +4567890123456789
  394. +123
  395. +4567890123456789
  396. -4567890123456789
  397. (5 rows)
  398. SELECT to_char(q2, 'FM9999999999999999THPR') FROM INT8_TBL;
  399. to_char
  400. --------------------
  401. 456TH
  402. 4567890123456789TH
  403. 123RD
  404. 4567890123456789TH
  405. <4567890123456789>
  406. (5 rows)
  407. SELECT to_char(q2, 'SG9999999999999999th') FROM INT8_TBL;
  408. to_char
  409. ---------------------
  410. + 456th
  411. +4567890123456789th
  412. + 123rd
  413. +4567890123456789th
  414. -4567890123456789
  415. (5 rows)
  416. SELECT to_char(q2, '0999999999999999') FROM INT8_TBL;
  417. to_char
  418. -------------------
  419. 0000000000000456
  420. 4567890123456789
  421. 0000000000000123
  422. 4567890123456789
  423. -4567890123456789
  424. (5 rows)
  425. SELECT to_char(q2, 'S0999999999999999') FROM INT8_TBL;
  426. to_char
  427. -------------------
  428. +0000000000000456
  429. +4567890123456789
  430. +0000000000000123
  431. +4567890123456789
  432. -4567890123456789
  433. (5 rows)
  434. SELECT to_char(q2, 'FM0999999999999999') FROM INT8_TBL;
  435. to_char
  436. -------------------
  437. 0000000000000456
  438. 4567890123456789
  439. 0000000000000123
  440. 4567890123456789
  441. -4567890123456789
  442. (5 rows)
  443. SELECT to_char(q2, 'FM9999999999999999.000') FROM INT8_TBL;
  444. to_char
  445. -----------------------
  446. 456.000
  447. 4567890123456789.000
  448. 123.000
  449. 4567890123456789.000
  450. -4567890123456789.000
  451. (5 rows)
  452. SELECT to_char(q2, 'L9999999999999999.000') FROM INT8_TBL;
  453. to_char
  454. ------------------------
  455. 456.000
  456. 4567890123456789.000
  457. 123.000
  458. 4567890123456789.000
  459. -4567890123456789.000
  460. (5 rows)
  461. SELECT to_char(q2, 'FM9999999999999999.999') FROM INT8_TBL;
  462. to_char
  463. --------------------
  464. 456.
  465. 4567890123456789.
  466. 123.
  467. 4567890123456789.
  468. -4567890123456789.
  469. (5 rows)
  470. SELECT to_char(q2, 'S 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 . 9 9 9') FROM INT8_TBL;
  471. to_char
  472. -------------------------------------------
  473. +4 5 6 . 0 0 0
  474. +4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
  475. +1 2 3 . 0 0 0
  476. +4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
  477. -4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 . 0 0 0
  478. (5 rows)
  479. SELECT to_char(q2, E'99999 "text" 9999 "9999" 999 "\\"text between quote marks\\"" 9999') FROM INT8_TBL;
  480. to_char
  481. -----------------------------------------------------------
  482. text 9999 "text between quote marks" 456
  483. 45678 text 9012 9999 345 "text between quote marks" 6789
  484. text 9999 "text between quote marks" 123
  485. 45678 text 9012 9999 345 "text between quote marks" 6789
  486. -45678 text 9012 9999 345 "text between quote marks" 6789
  487. (5 rows)
  488. SELECT to_char(q2, '999999SG9999999999') FROM INT8_TBL;
  489. to_char
  490. -------------------
  491. + 456
  492. 456789+0123456789
  493. + 123
  494. 456789+0123456789
  495. 456789-0123456789
  496. (5 rows)
  497. -- check min/max values and overflow behavior
  498. select '-9223372036854775808'::int8;
  499. int8
  500. ----------------------
  501. -9223372036854775808
  502. (1 row)
  503. select '-9223372036854775809'::int8;
  504. ERROR: value "-9223372036854775809" is out of range for type bigint
  505. LINE 1: select '-9223372036854775809'::int8;
  506. ^
  507. select '9223372036854775807'::int8;
  508. int8
  509. ---------------------
  510. 9223372036854775807
  511. (1 row)
  512. select '9223372036854775808'::int8;
  513. ERROR: value "9223372036854775808" is out of range for type bigint
  514. LINE 1: select '9223372036854775808'::int8;
  515. ^
  516. select -('-9223372036854775807'::int8);
  517. ?column?
  518. ---------------------
  519. 9223372036854775807
  520. (1 row)
  521. select -('-9223372036854775808'::int8);
  522. ERROR: bigint out of range
  523. select '9223372036854775800'::int8 + '9223372036854775800'::int8;
  524. ERROR: bigint out of range
  525. select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;
  526. ERROR: bigint out of range
  527. select '9223372036854775800'::int8 - '-9223372036854775800'::int8;
  528. ERROR: bigint out of range
  529. select '-9223372036854775800'::int8 - '9223372036854775800'::int8;
  530. ERROR: bigint out of range
  531. select '9223372036854775800'::int8 * '9223372036854775800'::int8;
  532. ERROR: bigint out of range
  533. select '9223372036854775800'::int8 / '0'::int8;
  534. ERROR: division by zero
  535. select '9223372036854775800'::int8 % '0'::int8;
  536. ERROR: division by zero
  537. select abs('-9223372036854775808'::int8);
  538. ERROR: bigint out of range
  539. select '9223372036854775800'::int8 + '100'::int4;
  540. ERROR: bigint out of range
  541. select '-9223372036854775800'::int8 - '100'::int4;
  542. ERROR: bigint out of range
  543. select '9223372036854775800'::int8 * '100'::int4;
  544. ERROR: bigint out of range
  545. select '100'::int4 + '9223372036854775800'::int8;
  546. ERROR: bigint out of range
  547. select '-100'::int4 - '9223372036854775800'::int8;
  548. ERROR: bigint out of range
  549. select '100'::int4 * '9223372036854775800'::int8;
  550. ERROR: bigint out of range
  551. select '9223372036854775800'::int8 + '100'::int2;
  552. ERROR: bigint out of range
  553. select '-9223372036854775800'::int8 - '100'::int2;
  554. ERROR: bigint out of range
  555. select '9223372036854775800'::int8 * '100'::int2;
  556. ERROR: bigint out of range
  557. select '-9223372036854775808'::int8 / '0'::int2;
  558. ERROR: division by zero
  559. select '100'::int2 + '9223372036854775800'::int8;
  560. ERROR: bigint out of range
  561. select '-100'::int2 - '9223372036854775800'::int8;
  562. ERROR: bigint out of range
  563. select '100'::int2 * '9223372036854775800'::int8;
  564. ERROR: bigint out of range
  565. select '100'::int2 / '0'::int8;
  566. ERROR: division by zero
  567. SELECT CAST('42'::int2 AS int8), CAST('-37'::int2 AS int8);
  568. int8 | int8
  569. ------+------
  570. 42 | -37
  571. (1 row)
  572. SELECT CAST('36854775807.0'::float4 AS int8);
  573. int8
  574. -------------
  575. 36854775808
  576. (1 row)
  577. SELECT CAST('922337203685477580700.0'::float8 AS int8);
  578. ERROR: bigint out of range
  579. SELECT CAST(q1 AS oid) FROM INT8_TBL;
  580. ERROR: OID out of range
  581. SELECT oid::int8 FROM pg_class WHERE relname = 'pg_class';
  582. oid
  583. ------
  584. 1259
  585. (1 row)
  586. -- bit operations
  587. SELECT q1, q2, q1 & q2 AS "and", q1 | q2 AS "or", q1 # q2 AS "xor", ~q1 AS "not" FROM INT8_TBL;
  588. q1 | q2 | and | or | xor | not
  589. ------------------+-------------------+------------------+------------------+------------------+-------------------
  590. 123 | 456 | 72 | 507 | 435 | -124
  591. 123 | 4567890123456789 | 17 | 4567890123456895 | 4567890123456878 | -124
  592. 4567890123456789 | 123 | 17 | 4567890123456895 | 4567890123456878 | -4567890123456790
  593. 4567890123456789 | 4567890123456789 | 4567890123456789 | 4567890123456789 | 0 | -4567890123456790
  594. 4567890123456789 | -4567890123456789 | 1 | -1 | -2 | -4567890123456790
  595. (5 rows)
  596. SELECT q1, q1 << 2 AS "shl", q1 >> 3 AS "shr" FROM INT8_TBL;
  597. q1 | shl | shr
  598. ------------------+-------------------+-----------------
  599. 123 | 492 | 15
  600. 123 | 492 | 15
  601. 4567890123456789 | 18271560493827156 | 570986265432098
  602. 4567890123456789 | 18271560493827156 | 570986265432098
  603. 4567890123456789 | 18271560493827156 | 570986265432098
  604. (5 rows)
  605. -- generate_series
  606. SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8);
  607. generate_series
  608. ------------------
  609. 4567890123456789
  610. 4567890123456790
  611. 4567890123456791
  612. 4567890123456792
  613. 4567890123456793
  614. 4567890123456794
  615. 4567890123456795
  616. 4567890123456796
  617. 4567890123456797
  618. 4567890123456798
  619. 4567890123456799
  620. (11 rows)
  621. SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 0);
  622. ERROR: step size cannot equal zero
  623. SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 2);
  624. generate_series
  625. ------------------
  626. 4567890123456789
  627. 4567890123456791
  628. 4567890123456793
  629. 4567890123456795
  630. 4567890123456797
  631. 4567890123456799
  632. (6 rows)
  633. -- corner case
  634. SELECT (-1::int8<<63)::text;
  635. text
  636. ----------------------
  637. -9223372036854775808
  638. (1 row)
  639. SELECT ((-1::int8<<63)+1)::text;
  640. text
  641. ----------------------
  642. -9223372036854775807
  643. (1 row)
  644. -- check sane handling of INT64_MIN overflow cases
  645. SELECT (-9223372036854775808)::int8 * (-1)::int8;
  646. ERROR: bigint out of range
  647. SELECT (-9223372036854775808)::int8 / (-1)::int8;
  648. ERROR: bigint out of range
  649. SELECT (-9223372036854775808)::int8 % (-1)::int8;
  650. ?column?
  651. ----------
  652. 0
  653. (1 row)
  654. SELECT (-9223372036854775808)::int8 * (-1)::int4;
  655. ERROR: bigint out of range
  656. SELECT (-9223372036854775808)::int8 / (-1)::int4;
  657. ERROR: bigint out of range
  658. SELECT (-9223372036854775808)::int8 % (-1)::int4;
  659. ?column?
  660. ----------
  661. 0
  662. (1 row)
  663. SELECT (-9223372036854775808)::int8 * (-1)::int2;
  664. ERROR: bigint out of range
  665. SELECT (-9223372036854775808)::int8 / (-1)::int2;
  666. ERROR: bigint out of range
  667. SELECT (-9223372036854775808)::int8 % (-1)::int2;
  668. ?column?
  669. ----------
  670. 0
  671. (1 row)
  672. -- check rounding when casting from float
  673. SELECT x, x::int8 AS int8_value
  674. FROM (VALUES (-2.5::float8),
  675. (-1.5::float8),
  676. (-0.5::float8),
  677. (0.0::float8),
  678. (0.5::float8),
  679. (1.5::float8),
  680. (2.5::float8)) t(x);
  681. x | int8_value
  682. ------+------------
  683. -2.5 | -2
  684. -1.5 | -2
  685. -0.5 | 0
  686. 0 | 0
  687. 0.5 | 0
  688. 1.5 | 2
  689. 2.5 | 2
  690. (7 rows)
  691. -- check rounding when casting from numeric
  692. SELECT x, x::int8 AS int8_value
  693. FROM (VALUES (-2.5::numeric),
  694. (-1.5::numeric),
  695. (-0.5::numeric),
  696. (0.0::numeric),
  697. (0.5::numeric),
  698. (1.5::numeric),
  699. (2.5::numeric)) t(x);
  700. x | int8_value
  701. ------+------------
  702. -2.5 | -3
  703. -1.5 | -2
  704. -0.5 | -1
  705. 0.0 | 0
  706. 0.5 | 1
  707. 1.5 | 2
  708. 2.5 | 3
  709. (7 rows)
  710. SELECT gcd((-9223372036854775808)::int8, 0::int8); -- overflow
  711. ERROR: bigint out of range
  712. SELECT gcd((-9223372036854775808)::int8, (-9223372036854775808)::int8); -- overflow
  713. ERROR: bigint out of range
  714. SELECT lcm((-9223372036854775808)::int8, 1::int8); -- overflow
  715. ERROR: bigint out of range
  716. SELECT lcm(9223372036854775807::int8, 9223372036854775806::int8); -- overflow
  717. ERROR: bigint out of range