type-promotion.slt 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. statement ok
  11. CREATE TABLE t (a float, b int)
  12. statement ok
  13. INSERT INTO t VALUES (4.7, 2)
  14. # Test that all arithmetic and comparison operators coalesce their arguments.
  15. # The goal is not to test every possible combination of arguments, but just a
  16. # basic sanity check. An old version of the code forgot to include modulus in
  17. # list of operators that should coalesce their inputs.
  18. query RRRRRBBBBBB
  19. SELECT
  20. a + b,
  21. a - b,
  22. a * b,
  23. a / b,
  24. a % b,
  25. a < b,
  26. a <= b,
  27. a > b,
  28. a >= b,
  29. a = b,
  30. a <> b
  31. FROM t
  32. ----
  33. 6.7 2.7 9.4 2.35 0.7000000000000002 false false true true false true
  34. # Do not allow int4 text comparisons
  35. query error db error: ERROR: operator does not exist: text < integer
  36. SELECT 'foo'::text < 5::int;
  37. query error db error: ERROR: operator does not exist: integer < text
  38. SELECT 1 < ALL(VALUES(NULL))
  39. # But string *literals* can coerce to anything.
  40. query T
  41. SELECT '1' < 2
  42. ----
  43. true
  44. query T
  45. SELECT 'true' OR 'false'
  46. ----
  47. true
  48. # Int literals <=32::MAX/>= MIN are int4
  49. query T
  50. SELECT pg_typeof(100)
  51. ----
  52. integer
  53. query T
  54. SELECT pg_typeof(-100)
  55. ----
  56. integer
  57. query T
  58. SELECT pg_typeof(2147483647)
  59. ----
  60. integer
  61. query T
  62. SELECT pg_typeof(-2147483648)
  63. ----
  64. integer
  65. # Int literals that exceed i32 are int8
  66. query T
  67. SELECT pg_typeof(2147483648)
  68. ----
  69. bigint
  70. query T
  71. SELECT pg_typeof(-2147483649)
  72. ----
  73. bigint
  74. query T
  75. SELECT pg_typeof(9223372036854775807)
  76. ----
  77. bigint
  78. query T
  79. SELECT pg_typeof(-9223372036854775808)
  80. ----
  81. bigint
  82. # Int literals that exceed i64 are numeric
  83. query T
  84. SELECT pg_typeof(9223372036854775808)
  85. ----
  86. numeric
  87. query T
  88. SELECT pg_typeof(-9223372036854775809)
  89. ----
  90. numeric
  91. # Use comparison ops to check for type promotion
  92. # int2 promotes to int4
  93. query T multiline
  94. EXPLAIN RAW PLAN FOR
  95. SELECT 1::smallint > 1;
  96. ----
  97. Map ((smallint_to_integer(integer_to_smallint(1)) > 1))
  98. Constant
  99. - ()
  100. Target cluster: quickstart
  101. EOF
  102. # Check int2 promotes to numeric
  103. query T multiline
  104. EXPLAIN RAW PLAN FOR
  105. SELECT 1::smallint > 1.1;
  106. ----
  107. Map ((smallint_to_numeric(integer_to_smallint(1)) > 1.1))
  108. Constant
  109. - ()
  110. Target cluster: quickstart
  111. EOF
  112. # Check int8 promotes to numeric
  113. query T multiline
  114. EXPLAIN RAW PLAN FOR
  115. SELECT 1::bigint > 1.11111
  116. ----
  117. Map ((bigint_to_numeric(integer_to_bigint(1)) > 1.11111))
  118. Constant
  119. - ()
  120. Target cluster: quickstart
  121. EOF
  122. # Check int8 promotes to double precision
  123. query T multiline
  124. EXPLAIN RAW PLAN FOR
  125. SELECT 1::bigint > 1.11111::float
  126. ----
  127. Map ((bigint_to_double(integer_to_bigint(1)) > numeric_to_double(1.11111)))
  128. Constant
  129. - ()
  130. Target cluster: quickstart
  131. EOF
  132. # Check numeric promotes to double precision
  133. query T multiline
  134. EXPLAIN RAW PLAN FOR
  135. SELECT 1.1 > 1::float;
  136. ----
  137. Map ((numeric_to_double(1.1) > integer_to_double(1)))
  138. Constant
  139. - ()
  140. Target cluster: quickstart
  141. EOF
  142. # Check numerics do not get promoted
  143. query T multiline
  144. EXPLAIN RAW PLAN FOR
  145. SELECT 1.1 > 1.1
  146. ----
  147. Map ((1.1 > 1.1))
  148. Constant
  149. - ()
  150. Target cluster: quickstart
  151. EOF
  152. # Check floats do not get promoted
  153. query T multiline
  154. EXPLAIN RAW PLAN FOR
  155. SELECT 1::float > 1::float
  156. ----
  157. Map ((integer_to_double(1) > integer_to_double(1)))
  158. Constant
  159. - ()
  160. Target cluster: quickstart
  161. EOF
  162. # Checks that float8 is preferred type for int4
  163. query T multiline
  164. EXPLAIN RAW PLAN FOR
  165. SELECT floor(1);
  166. ----
  167. Map (floorf64(integer_to_double(1)))
  168. Constant
  169. - ()
  170. Target cluster: quickstart
  171. EOF
  172. # Cannot implicitly cast int4 to string
  173. query error db error: ERROR: function char_length\(integer\) does not exist
  174. SELECT char_length(321);
  175. # Cannot implicitly cast double precision to numeric
  176. query error db error: ERROR: function round\(double precision, integer\) does not exist
  177. SELECT round(1.23::float, 1);
  178. # Check that float8 is the most common type
  179. query T multiline
  180. EXPLAIN RAW PLAN FOR
  181. SELECT coalesce(1::int, 1::numeric, 1::float);
  182. ----
  183. Map (coalesce(integer_to_double(1), numeric_to_double(integer_to_numeric(1)), integer_to_double(1)))
  184. Constant
  185. - ()
  186. Target cluster: quickstart
  187. EOF
  188. # Test bool->string explicit cast
  189. query T
  190. SELECT true::string;
  191. ----
  192. true
  193. # Test bool->string implicit cast
  194. query T
  195. SELECT concat(true);
  196. ----
  197. t
  198. # Check that date promotes to tstz
  199. query T multiline
  200. EXPLAIN RAW PLAN FOR
  201. SELECT TIMESTAMPTZ '2001 03-04' - DATE '2002 03-04';
  202. ----
  203. Map ((text_to_timestamp_with_time_zone("2001 03-04") - date_to_timestamp_with_timezone(text_to_date("2002 03-04"))))
  204. Constant
  205. - ()
  206. Target cluster: quickstart
  207. EOF
  208. # Check that ts promotes to tstz
  209. query T multiline
  210. EXPLAIN RAW PLAN FOR
  211. SELECT TIMESTAMPTZ '2001 03-04' - TIMESTAMP '2002 03-04';
  212. ----
  213. Map ((text_to_timestamp_with_time_zone("2001 03-04") - timestamp_to_timestamp_with_time_zone(text_to_timestamp("2002 03-04"))))
  214. Constant
  215. - ()
  216. Target cluster: quickstart
  217. EOF
  218. # Check that date promotes to ts
  219. query T multiline
  220. EXPLAIN RAW PLAN FOR
  221. SELECT TIMESTAMP '2001 03-04' - DATE '2002 03-04';
  222. ----
  223. Map ((text_to_timestamp("2001 03-04") - date_to_timestamp(text_to_date("2002 03-04"))))
  224. Constant
  225. - ()
  226. Target cluster: quickstart
  227. EOF
  228. # Check that tstz is most-common type
  229. query T multiline
  230. EXPLAIN RAW PLAN FOR
  231. SELECT coalesce(DATE '2001', TIMESTAMP '2002', TIMESTAMPTZ '2003');
  232. ----
  233. Map (coalesce(date_to_timestamp_with_timezone(text_to_date("2001")), timestamp_to_timestamp_with_time_zone(text_to_timestamp("2002")), text_to_timestamp_with_time_zone("2003")))
  234. Constant
  235. - ()
  236. Target cluster: quickstart
  237. EOF
  238. # Check that char casts to text for string functions
  239. query T multiline
  240. EXPLAIN RAW PLAN FOR
  241. SELECT lower('a'::char);
  242. ----
  243. Map (lower(char_to_text(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")))))
  244. Constant
  245. - ()
  246. Target cluster: quickstart
  247. EOF
  248. # Check that "char" vs varchar promotes to text
  249. query T multiline
  250. EXPLAIN RAW PLAN FOR
  251. SELECT 'a'::"char" < 'a'::varchar;
  252. ----
  253. Map (("char"_to_text(text_to_"char"("a")) < varchar_to_text(text_to_varchar[len=unbounded]("a"))))
  254. Constant
  255. - ()
  256. Target cluster: quickstart
  257. EOF
  258. # Check that "char" vs char promotes to text
  259. query T multiline
  260. EXPLAIN RAW PLAN FOR
  261. SELECT 'a'::"char" < 'a'::char;
  262. ----
  263. Map (("char"_to_text(text_to_"char"("a")) < char_to_text(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")))))
  264. Constant
  265. - ()
  266. Target cluster: quickstart
  267. EOF
  268. # Check that varchar promotes to char
  269. query T multiline
  270. EXPLAIN RAW PLAN FOR
  271. SELECT 'a'::char < 'a'::varchar;
  272. ----
  273. Map ((text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")) < text_to_char[len=unbounded](text_to_varchar[len=unbounded]("a"))))
  274. Constant
  275. - ()
  276. Target cluster: quickstart
  277. EOF
  278. # Check that char promotes to text
  279. query T multiline
  280. EXPLAIN RAW PLAN FOR
  281. SELECT 'a'::char < 'a'::text;
  282. ----
  283. Map ((char_to_text(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a"))) < "a"))
  284. Constant
  285. - ()
  286. Target cluster: quickstart
  287. EOF
  288. query T multiline
  289. EXPLAIN RAW PLAN FOR
  290. SELECT 'a'::varchar < 'a'::text;
  291. ----
  292. Map ((varchar_to_text(text_to_varchar[len=unbounded]("a")) < "a"))
  293. Constant
  294. - ()
  295. Target cluster: quickstart
  296. EOF
  297. # Check that text is most-common type
  298. query T multiline
  299. EXPLAIN RAW PLAN FOR
  300. SELECT coalesce('a'::char, 'a'::varchar, 'a'::text);
  301. ----
  302. Map (coalesce(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")), text_to_char[len=unbounded](text_to_varchar[len=unbounded]("a")), text_to_char[len=unbounded]("a")))
  303. Constant
  304. - ()
  305. Target cluster: quickstart
  306. EOF
  307. query T multiline
  308. EXPLAIN RAW PLAN FOR
  309. SELECT coalesce('a'::char, 'a'::text, 'a'::varchar);
  310. ----
  311. Map (coalesce(text_to_char[len=1, fail_on_len=false](text_to_char[len=unbounded]("a")), text_to_char[len=unbounded]("a"), text_to_char[len=unbounded](text_to_varchar[len=unbounded]("a"))))
  312. Constant
  313. - ()
  314. Target cluster: quickstart
  315. EOF
  316. query T
  317. SELECT 1::smallint::smallint;
  318. ----
  319. 1
  320. query T
  321. SELECT 1::smallint::bigint;
  322. ----
  323. 1
  324. query error CAST does not support casting from boolean to smallint
  325. SELECT TRUE::boolean::smallint
  326. query error CAST does not support casting from date to smallint
  327. SELECT '2001 02-03'::date::smallint
  328. query T
  329. SELECT 1.1::numeric::smallint;
  330. ----
  331. 1
  332. query T
  333. SELECT 1.2::double::smallint;
  334. ----
  335. 1
  336. query T
  337. SELECT 1.3::real::smallint;
  338. ----
  339. 1
  340. query T
  341. SELECT 2::int::smallint;
  342. ----
  343. 2
  344. query error CAST does not support casting from interval to smallint
  345. SELECT '1'::interval::smallint
  346. query error cannot cast jsonb object to type smallint
  347. SELECT '{}'::jsonb::smallint;
  348. query R
  349. SELECT '1'::jsonb::smallint;
  350. ----
  351. 1
  352. query R
  353. SELECT '1'::text::smallint
  354. ----
  355. 1
  356. query R
  357. SELECT '1'::char::smallint
  358. ----
  359. 1
  360. query R
  361. SELECT '1'::varchar::smallint
  362. ----
  363. 1
  364. query error CAST does not support casting from time to smallint
  365. SELECT '01:02:03'::time::smallint
  366. query error CAST does not support casting from timestamp without time zone to smallint
  367. SELECT '2002 03-04'::timestamp::smallint
  368. query error CAST does not support casting from timestamp with time zone to smallint
  369. SELECT '2003 04-05'::timestamptz::smallint
  370. query error AST does not support casting from smallint to bool
  371. SELECT 1::smallint::boolean;
  372. query T
  373. SELECT 1::bigint::smallint;
  374. ----
  375. 1
  376. query T
  377. SELECT 1::bigint::bigint;
  378. ----
  379. 1
  380. query T
  381. SELECT TRUE::boolean::bigint
  382. ----
  383. 1
  384. query error CAST does not support casting from date to bigint
  385. SELECT '2001 02-03'::date::bigint
  386. query T
  387. SELECT 1.1::numeric::bigint;
  388. ----
  389. 1
  390. query T
  391. SELECT 1.2::double::bigint;
  392. ----
  393. 1
  394. query T
  395. SELECT 1.3::real::bigint;
  396. ----
  397. 1
  398. query T
  399. SELECT 2::int::bigint;
  400. ----
  401. 2
  402. query error CAST does not support casting from interval to bigint
  403. SELECT '1'::interval::bigint
  404. query error cannot cast jsonb object to type bigint
  405. SELECT '{}'::jsonb::bigint;
  406. query R
  407. SELECT '1'::jsonb::bigint;
  408. ----
  409. 1
  410. query R
  411. SELECT '1'::text::bigint
  412. ----
  413. 1
  414. query R
  415. SELECT '1'::char::bigint
  416. ----
  417. 1
  418. query R
  419. SELECT '1'::varchar::bigint
  420. ----
  421. 1
  422. query error CAST does not support casting from time to bigint
  423. SELECT '01:02:03'::time::bigint
  424. query error CAST does not support casting from timestamp without time zone to bigint
  425. SELECT '2002 03-04'::timestamp::bigint
  426. query error CAST does not support casting from timestamp with time zone to bigint
  427. SELECT '2003 04-05'::timestamptz::bigint
  428. query T
  429. SELECT 1::bigint::boolean;
  430. ----
  431. true
  432. query T
  433. SELECT TRUE::boolean::boolean;
  434. ----
  435. true
  436. query error CAST does not support casting from date to boolean
  437. SELECT '2001 02-03'::date::boolean
  438. query error CAST does not support casting from numeric to boolean
  439. SELECT 1.1::numeric::boolean
  440. query error CAST does not support casting from double precision to boolean
  441. SELECT 1.2::double::boolean
  442. query error CAST does not support casting from real to boolean
  443. SELECT 1.3::real::boolean
  444. query T
  445. SELECT 2::int::boolean;
  446. ----
  447. true
  448. query error CAST does not support casting from interval to boolean
  449. SELECT '1'::interval::boolean
  450. query error cannot cast jsonb object to type boolean
  451. SELECT '{}'::jsonb::boolean;
  452. query error cannot cast jsonb number to type boolean
  453. SELECT '1'::jsonb::boolean;
  454. query T
  455. SELECT 'true'::text::boolean
  456. ----
  457. true
  458. query T
  459. SELECT 'true'::char(4)::boolean
  460. ----
  461. true
  462. query T
  463. SELECT 'true'::varchar::boolean
  464. ----
  465. true
  466. query error CAST does not support casting from time to boolean
  467. SELECT '01:02:03'::time::boolean
  468. query error CAST does not support casting from timestamp without time zone to boolean
  469. SELECT '2002 03-04'::timestamp::boolean
  470. query error CAST does not support casting from timestamp with time zone to boolean
  471. SELECT '2003 04-05'::timestamptz::boolean
  472. query error CAST does not support casting from smallint to date
  473. SELECT 1::smallint::date
  474. query error CAST does not support casting from bigint to date
  475. SELECT 1::bigint::date
  476. query error CAST does not support casting from boolean to date
  477. SELECT TRUE::boolean::date
  478. query T
  479. SELECT '2001 02-03'::date::date;
  480. ----
  481. 2001-02-03
  482. query error CAST does not support casting from numeric to date
  483. SELECT 1.1::numeric::date
  484. query error CAST does not support casting from double precision to date
  485. SELECT 1.2::double::date
  486. query error CAST does not support casting from real to date
  487. SELECT 1.3::real::date
  488. query error CAST does not support casting from integer to date
  489. SELECT 2::int::date
  490. query error CAST does not support casting from interval to date
  491. SELECT '1'::interval::date
  492. query error CAST does not support casting from jsonb to date
  493. SELECT '{}'::jsonb::date
  494. query T
  495. SELECT '2001 02-03'::text::date
  496. ----
  497. 2001-02-03
  498. query T
  499. SELECT '2001 02-03'::char(11)::date
  500. ----
  501. 2001-02-03
  502. query T
  503. SELECT '2001 02-03'::varchar::date
  504. ----
  505. 2001-02-03
  506. query error CAST does not support casting from time to date
  507. SELECT '01:02:03'::time::date
  508. query T
  509. SELECT '2002 03-04'::timestamp::date;
  510. ----
  511. 2002-03-04
  512. query T
  513. SELECT '2003 04-05'::timestamptz::date;
  514. ----
  515. 2003-04-05
  516. query R
  517. SELECT 1::smallint::numeric;
  518. ----
  519. 1
  520. query R
  521. SELECT 1::bigint::numeric;
  522. ----
  523. 1
  524. query error CAST does not support casting from boolean to numeric
  525. SELECT TRUE::boolean::numeric
  526. query error CAST does not support casting from date to numeric
  527. SELECT '2001 02-03'::date::numeric
  528. query R
  529. SELECT 1.1::numeric::numeric;
  530. ----
  531. 1.1
  532. query R
  533. SELECT 1.2::double::numeric;
  534. ----
  535. 1.2
  536. query R
  537. SELECT 1.3::real::numeric;
  538. ----
  539. 1.3
  540. query R
  541. SELECT 2::int::numeric;
  542. ----
  543. 2
  544. query error CAST does not support casting from interval to numeric
  545. SELECT '1'::interval::numeric
  546. query error cannot cast jsonb object to type numeric
  547. SELECT '{}'::jsonb::numeric;
  548. query R
  549. SELECT '1'::jsonb::numeric;
  550. ----
  551. 1
  552. query R
  553. SELECT '1'::text::numeric
  554. ----
  555. 1
  556. query R
  557. SELECT '1'::char::numeric
  558. ----
  559. 1
  560. query R
  561. SELECT '1'::varchar::numeric
  562. ----
  563. 1
  564. query error CAST does not support casting from time to numeric
  565. SELECT '01:02:03'::time::numeric
  566. query error CAST does not support casting from timestamp without time zone to numeric
  567. SELECT '2002 03-04'::timestamp::numeric
  568. query error CAST does not support casting from timestamp with time zone to numeric
  569. SELECT '2003 04-05'::timestamptz::numeric
  570. query T
  571. SELECT 1::smallint::double;
  572. ----
  573. 1.000
  574. query T
  575. SELECT 1::bigint::double;
  576. ----
  577. 1.000
  578. query error CAST does not support casting from boolean to double precision
  579. SELECT TRUE::boolean::double
  580. query error CAST does not support casting from date to double precision
  581. SELECT '2001 02-03'::date::double
  582. query T
  583. SELECT 1.1::numeric::double;
  584. ----
  585. 1.100
  586. query T
  587. SELECT 1.2::double::double;
  588. ----
  589. 1.200
  590. query T
  591. SELECT 1.3::real::double;
  592. ----
  593. 1.300
  594. query T
  595. SELECT 2::int::double;
  596. ----
  597. 2.000
  598. query error CAST does not support casting from interval to double precision
  599. SELECT '1'::interval::double
  600. query error cannot cast jsonb object to type double precision
  601. SELECT '{}'::jsonb::double;
  602. query T
  603. SELECT '1'::jsonb::double;
  604. ----
  605. 1.000
  606. query R
  607. SELECT '1'::text::double
  608. ----
  609. 1
  610. query R
  611. SELECT '1'::char::double
  612. ----
  613. 1
  614. query R
  615. SELECT '1'::varchar::double
  616. ----
  617. 1
  618. query error CAST does not support casting from time to double precision
  619. SELECT '01:02:03'::time::double
  620. query error CAST does not support casting from timestamp without time zone to double precision
  621. SELECT '2002 03-04'::timestamp::double
  622. query error CAST does not support casting from timestamp with time zone to double precision
  623. SELECT '2003 04-05'::timestamptz::double
  624. query T
  625. SELECT 1::smallint::real;
  626. ----
  627. 1.000
  628. query T
  629. SELECT 1::bigint::real;
  630. ----
  631. 1.000
  632. query error CAST does not support casting from boolean to real
  633. SELECT TRUE::boolean::real
  634. query error CAST does not support casting from date to real
  635. SELECT '2001 02-03'::date::real
  636. query T
  637. SELECT 1.1::numeric::real;
  638. ----
  639. 1.100
  640. query T
  641. SELECT 1.2::double::real;
  642. ----
  643. 1.200
  644. query T
  645. SELECT 1.3::real::real;
  646. ----
  647. 1.300
  648. query T
  649. SELECT 2::int::real;
  650. ----
  651. 2.000
  652. query error CAST does not support casting from interval to real
  653. SELECT '1'::interval::real
  654. query error cannot cast jsonb object to type real
  655. SELECT '{}'::jsonb::real;
  656. query R
  657. SELECT '2'::jsonb::real;
  658. ----
  659. 2
  660. query R
  661. SELECT '1'::text::real
  662. ----
  663. 1
  664. query R
  665. SELECT '1'::char::real
  666. ----
  667. 1
  668. query R
  669. SELECT '1'::varchar::real
  670. ----
  671. 1
  672. query error CAST does not support casting from time to real
  673. SELECT '01:02:03'::time::real
  674. query error CAST does not support casting from timestamp without time zone to real
  675. SELECT '2002 03-04'::timestamp::real
  676. query error CAST does not support casting from timestamp with time zone to real
  677. SELECT '2003 04-05'::timestamptz::real
  678. query T
  679. SELECT 1::smallint::integer;
  680. ----
  681. 1
  682. query T
  683. SELECT 1::bigint::integer;
  684. ----
  685. 1
  686. query error CAST does not support casting from date to integer
  687. SELECT '2001 02-03'::date::integer
  688. query T
  689. SELECT 1.1::numeric::integer;
  690. ----
  691. 1
  692. query T
  693. SELECT 1.2::double::integer;
  694. ----
  695. 1
  696. query T
  697. SELECT 1.3::real::integer;
  698. ----
  699. 1
  700. query T
  701. SELECT 2::int::integer;
  702. ----
  703. 2
  704. query error CAST does not support casting from interval to integer
  705. SELECT '1'::interval::integer
  706. query error cannot cast jsonb object to type integer
  707. SELECT '{}'::jsonb::integer;
  708. query R
  709. SELECT '1'::jsonb::integer;
  710. ----
  711. 1
  712. query R
  713. SELECT '1'::text::integer
  714. ----
  715. 1
  716. query R
  717. SELECT '1'::char::integer
  718. ----
  719. 1
  720. query R
  721. SELECT '1'::varchar::integer
  722. ----
  723. 1
  724. query error CAST does not support casting from time to integer
  725. SELECT '01:02:03'::time::integer
  726. query error CAST does not support casting from timestamp without time zone to integer
  727. SELECT '2002 03-04'::timestamp::integer
  728. query error CAST does not support casting from timestamp with time zone to integer
  729. SELECT '2003 04-05'::timestamptz::integer
  730. query error CAST does not support casting from smallint to interval
  731. SELECT 1::smallint::interval
  732. query error CAST does not support casting from bigint to interval
  733. SELECT 1::bigint::interval
  734. query error CAST does not support casting from boolean to interval
  735. SELECT TRUE::boolean::interval
  736. query error CAST does not support casting from date to interval
  737. SELECT '2001 02-03'::date::interval
  738. query error CAST does not support casting from numeric to interval
  739. SELECT 1.1::numeric::interval
  740. query error CAST does not support casting from double precision to interval
  741. SELECT 1.2::double::interval
  742. query error CAST does not support casting from real to interval
  743. SELECT 1.3::real::interval
  744. query error CAST does not support casting from integer to interval
  745. SELECT 2::int::interval
  746. query T
  747. SELECT '1'::interval::interval;
  748. ----
  749. 00:00:01
  750. query error CAST does not support casting from jsonb to interval
  751. SELECT '{}'::jsonb::interval
  752. query T
  753. SELECT '1'::text::interval
  754. ----
  755. 00:00:01
  756. query T
  757. SELECT '1'::char::interval
  758. ----
  759. 00:00:01
  760. query T
  761. SELECT '1'::varchar::interval
  762. ----
  763. 00:00:01
  764. query T
  765. SELECT '01:02:03'::time::interval;
  766. ----
  767. 01:02:03
  768. query error CAST does not support casting from timestamp without time zone to interval
  769. SELECT '2002 03-04'::timestamp::interval
  770. query error CAST does not support casting from timestamp with time zone to interval
  771. SELECT '2003 04-05'::timestamptz::interval
  772. query error CAST does not support casting from smallint to jsonb
  773. SELECT 1::smallint::jsonb
  774. query error CAST does not support casting from bigint to jsonb
  775. SELECT 1::bigint::jsonb
  776. #pginvalid
  777. query error CAST does not support casting from boolean to jsonb
  778. SELECT TRUE::boolean::jsonb;
  779. query error CAST does not support casting from date to jsonb
  780. SELECT '2001 02-03'::date::jsonb
  781. query error CAST does not support casting from numeric to jsonb
  782. SELECT 1.1::numeric::jsonb
  783. query error CAST does not support casting from double precision to jsonb
  784. SELECT 1.2::double::jsonb;
  785. query error CAST does not support casting from integer to jsonb
  786. SELECT 2::int::jsonb;
  787. query error CAST does not support casting from interval to jsonb
  788. SELECT '1'::interval::jsonb
  789. query T
  790. SELECT '{}'::jsonb::jsonb;
  791. ----
  792. {}
  793. query T
  794. SELECT '1'::jsonb::jsonb;
  795. ----
  796. 1
  797. query error CAST does not support casting from time to jsonb
  798. SELECT '01:02:03'::time::jsonb
  799. query error CAST does not support casting from timestamp without time zone to jsonb
  800. SELECT '2002 03-04'::timestamp::jsonb
  801. query error CAST does not support casting from timestamp with time zone to jsonb
  802. SELECT '2003 04-05'::timestamptz::jsonb
  803. query T
  804. SELECT 1::smallint::text;
  805. ----
  806. 1
  807. query T
  808. SELECT 1::bigint::text;
  809. ----
  810. 1
  811. query T
  812. SELECT TRUE::boolean::text;
  813. ----
  814. true
  815. query T
  816. SELECT '2001 02-03'::date::text;
  817. ----
  818. 2001-02-03
  819. query T
  820. SELECT 1.1::numeric::text;
  821. ----
  822. 1.1
  823. query T
  824. SELECT 1.2::double::text;
  825. ----
  826. 1.2
  827. query T
  828. SELECT 1.3::real::text;
  829. ----
  830. 1.3
  831. query T
  832. SELECT 2::int::text;
  833. ----
  834. 2
  835. query T
  836. SELECT '1'::interval::text;
  837. ----
  838. 00:00:01
  839. query T
  840. SELECT '{}'::jsonb::text;
  841. ----
  842. {}
  843. query T
  844. SELECT '1'::jsonb::text;
  845. ----
  846. 1
  847. query T
  848. SELECT 'dog'::text::text;
  849. ----
  850. dog
  851. query T
  852. SELECT 'dog'::char(3)::text;
  853. ----
  854. dog
  855. query T
  856. SELECT 'dog'::varchar::text;
  857. ----
  858. dog
  859. query T
  860. SELECT '01:02:03'::time::text;
  861. ----
  862. 01:02:03
  863. query T
  864. SELECT '2002 03-04'::timestamp::text;
  865. ----
  866. 2002-03-04 00:00:00
  867. query T
  868. SELECT '2003 04-05'::timestamptz::text;
  869. ----
  870. 2003-04-05 00:00:00+00
  871. query T
  872. SELECT trim(trailing from 1::bigint::char(20));
  873. ----
  874. 1
  875. query T
  876. SELECT trim(trailing from TRUE::boolean::char(20));
  877. ----
  878. true
  879. query T
  880. SELECT trim(trailing from '2001 02-03'::date::char(20));
  881. ----
  882. 2001-02-03
  883. query T
  884. SELECT trim(trailing from 1.1::numeric::char(20));
  885. ----
  886. 1.1
  887. query T
  888. SELECT trim(trailing from 1.2::double::char(20));
  889. ----
  890. 1.2
  891. query T
  892. SELECT trim(trailing from 1.3::real::char(20));
  893. ----
  894. 1.3
  895. query T
  896. SELECT trim(trailing from 2::int::char(20));
  897. ----
  898. 2
  899. query T
  900. SELECT trim(trailing from '1'::interval::char(20));
  901. ----
  902. 00:00:01
  903. query T
  904. SELECT trim(trailing from '{}'::jsonb::char(20));
  905. ----
  906. {}
  907. query T
  908. SELECT trim(trailing from '1'::jsonb::char(20));
  909. ----
  910. 1
  911. query T
  912. SELECT trim(trailing from 'dog'::text::char(20));
  913. ----
  914. dog
  915. query T
  916. SELECT trim(trailing from 'dog'::char(3)::char(20));
  917. ----
  918. dog
  919. query T
  920. SELECT trim(trailing from 'dog'::varchar::char(20));
  921. ----
  922. dog
  923. query T
  924. SELECT trim(trailing from '01:02:03'::time::char(20));
  925. ----
  926. 01:02:03
  927. query T
  928. SELECT trim(trailing from '2002 03-04'::timestamp::char(20));
  929. ----
  930. 2002-03-04 00:00:00
  931. query T
  932. SELECT trim(trailing from '2003 04-05'::timestamptz::char(20));
  933. ----
  934. 2003-04-05 00:00:00+
  935. query T
  936. SELECT 1::smallint::varchar;
  937. ----
  938. 1
  939. query T
  940. SELECT 1::bigint::varchar;
  941. ----
  942. 1
  943. query T
  944. SELECT TRUE::boolean::varchar;
  945. ----
  946. true
  947. query T
  948. SELECT '2001 02-03'::date::varchar;
  949. ----
  950. 2001-02-03
  951. query T
  952. SELECT 1.1::numeric::varchar;
  953. ----
  954. 1.1
  955. query T
  956. SELECT 1.2::double::varchar;
  957. ----
  958. 1.2
  959. query T
  960. SELECT 1.3::real::varchar;
  961. ----
  962. 1.3
  963. query T
  964. SELECT 2::int::varchar;
  965. ----
  966. 2
  967. query T
  968. SELECT '1'::interval::varchar;
  969. ----
  970. 00:00:01
  971. query T
  972. SELECT '{}'::jsonb::varchar;
  973. ----
  974. {}
  975. query T
  976. SELECT '1'::jsonb::varchar;
  977. ----
  978. 1
  979. query T
  980. SELECT 'dog'::text::varchar;
  981. ----
  982. dog
  983. query T
  984. SELECT 'dog'::char(3)::varchar;
  985. ----
  986. dog
  987. query T
  988. SELECT 'dog'::varchar::varchar;
  989. ----
  990. dog
  991. query T
  992. SELECT '01:02:03'::time::varchar;
  993. ----
  994. 01:02:03
  995. query T
  996. SELECT '2002 03-04'::timestamp::varchar;
  997. ----
  998. 2002-03-04 00:00:00
  999. query T
  1000. SELECT '2003 04-05'::timestamptz::varchar;
  1001. ----
  1002. 2003-04-05 00:00:00+00
  1003. query error CAST does not support casting from smallint to time
  1004. SELECT 1::smallint::time
  1005. query error CAST does not support casting from bigint to time
  1006. SELECT 1::bigint::time
  1007. query error CAST does not support casting from boolean to time
  1008. SELECT TRUE::boolean::time
  1009. query error CAST does not support casting from date to time
  1010. SELECT '2001 02-03'::date::time
  1011. query error CAST does not support casting from numeric to time
  1012. SELECT 1.1::numeric::time
  1013. query error CAST does not support casting from double precision to time
  1014. SELECT 1.2::double::time
  1015. query error CAST does not support casting from real to time
  1016. SELECT 1.3::real::time
  1017. query error CAST does not support casting from integer to time
  1018. SELECT 2::int::time
  1019. query T
  1020. SELECT '1'::interval::time;
  1021. ----
  1022. 00:00:01
  1023. query error CAST does not support casting from jsonb to time
  1024. SELECT '{}'::jsonb::time
  1025. query T
  1026. SELECT '01:02:03'::text::time
  1027. ----
  1028. 01:02:03
  1029. query T
  1030. SELECT '01:02:03'::char(9)::time
  1031. ----
  1032. 01:02:03
  1033. query T
  1034. SELECT '01:02:03'::varchar::time
  1035. ----
  1036. 01:02:03
  1037. query T
  1038. SELECT '01:02:03'::time::time;
  1039. ----
  1040. 01:02:03
  1041. query T
  1042. SELECT '2002 03-04'::timestamp::time
  1043. ----
  1044. 00:00:00
  1045. query T
  1046. SELECT '2002 03-04 05:06:07'::timestamp::time
  1047. ----
  1048. 05:06:07
  1049. query T
  1050. SELECT '2003 04-05'::timestamptz::time
  1051. ----
  1052. 00:00:00
  1053. query T
  1054. SELECT '2003 04-05 06:07:08+00'::timestamptz::time
  1055. ----
  1056. 06:07:08
  1057. query error CAST does not support casting from smallint to timestamp
  1058. SELECT 1::smallint::timestamp
  1059. query error CAST does not support casting from bigint to timestamp
  1060. SELECT 1::bigint::timestamp
  1061. query error CAST does not support casting from boolean to timestamp
  1062. SELECT TRUE::boolean::timestamp
  1063. query T
  1064. SELECT '2001 02-03'::date::timestamp;
  1065. ----
  1066. 2001-02-03 00:00:00
  1067. query error CAST does not support casting from numeric to timestamp
  1068. SELECT 1.1::numeric::timestamp
  1069. query error CAST does not support casting from double precision to timestamp
  1070. SELECT 1.2::double::timestamp
  1071. query error CAST does not support casting from real to timestamp
  1072. SELECT 1.3::real::timestamp
  1073. query error CAST does not support casting from integer to timestamp
  1074. SELECT 2::int::timestamp
  1075. query error CAST does not support casting from interval to timestamp
  1076. SELECT '1'::interval::timestamp
  1077. query error CAST does not support casting from jsonb to timestamp
  1078. SELECT '{}'::jsonb::timestamp
  1079. query T
  1080. SELECT '2001 02-03'::text::timestamp
  1081. ----
  1082. 2001-02-03 00:00:00
  1083. query T
  1084. SELECT '2001 02-03'::char(10)::timestamp
  1085. ----
  1086. 2001-02-03 00:00:00
  1087. query T
  1088. SELECT '2001 02-03'::varchar::timestamp
  1089. ----
  1090. 2001-02-03 00:00:00
  1091. query error CAST does not support casting from time to timestamp
  1092. SELECT '01:02:03'::time::timestamp
  1093. query T
  1094. SELECT '2002 03-04'::timestamp::timestamp;
  1095. ----
  1096. 2002-03-04 00:00:00
  1097. query T
  1098. SELECT '2003 04-05'::timestamptz::timestamp;
  1099. ----
  1100. 2003-04-05 00:00:00
  1101. query error CAST does not support casting from smallint to timestamp with time zone
  1102. SELECT 1::smallint::timestamptz
  1103. query error CAST does not support casting from bigint to timestamp with time zone
  1104. SELECT 1::bigint::timestamptz
  1105. query error CAST does not support casting from boolean to timestamp with time zone
  1106. SELECT TRUE::boolean::timestamptz
  1107. query T
  1108. SELECT '2001 02-03'::date::timestamptz;
  1109. ----
  1110. 2001-02-03 00:00:00+00
  1111. query error CAST does not support casting from numeric to timestamp with time zone
  1112. SELECT 1.1::numeric::timestamptz
  1113. query error CAST does not support casting from double precision to timestamp with time zone
  1114. SELECT 1.2::double::timestamptz
  1115. query error CAST does not support casting from real to timestamp with time zone
  1116. SELECT 1.3::real::timestamptz
  1117. query error CAST does not support casting from integer to timestamp with time zone
  1118. SELECT 2::int::timestamptz
  1119. query error CAST does not support casting from interval to timestamp with time zone
  1120. SELECT '1'::interval::timestamptz
  1121. query error CAST does not support casting from jsonb to timestamp with time zone
  1122. SELECT '{}'::jsonb::timestamptz
  1123. query T
  1124. SELECT '2001 02-03'::text::timestamptz
  1125. ----
  1126. 2001-02-03 00:00:00+00
  1127. query T
  1128. SELECT '2001 02-03'::char(10)::timestamptz
  1129. ----
  1130. 2001-02-03 00:00:00+00
  1131. query T
  1132. SELECT '2001 02-03'::varchar::timestamptz
  1133. ----
  1134. 2001-02-03 00:00:00+00
  1135. query error CAST does not support casting from time to timestamp with time zone
  1136. SELECT '01:02:03'::time::timestamptz
  1137. query T
  1138. SELECT '2002 03-04'::timestamp::timestamptz;
  1139. ----
  1140. 2002-03-04 00:00:00+00
  1141. query T
  1142. SELECT '2003 04-05'::timestamptz::timestamptz;
  1143. ----
  1144. 2003-04-05 00:00:00+00
  1145. query T
  1146. SELECT 14::smallint::oid;
  1147. ----
  1148. 14
  1149. query T
  1150. SELECT 14::bigint::oid;
  1151. ----
  1152. 14
  1153. query T
  1154. SELECT 14::oid::bigint;
  1155. ----
  1156. 14
  1157. query T
  1158. SELECT 14::oid = 14::bigint;
  1159. ----
  1160. true
  1161. query T
  1162. SELECT 14::oid = 14::smallint;
  1163. ----
  1164. true
  1165. query error "120129019392" OID out of range
  1166. SELECT 120129019392::bigint::oid;
  1167. # Check that our non-linear type promotion structure works as intended.
  1168. # Whenever you have a uint and an int, you unify their types by selecting the
  1169. # narrowest integer-like that the uint can be implicitly cast to. In the case
  1170. # of 64-bit uints, this means casting the values to numeric.
  1171. query TT
  1172. SELECT pg_typeof(1::smallint + 1::uint2), 1::smallint + 1::uint2;
  1173. ----
  1174. integer 2
  1175. query TT
  1176. SELECT pg_typeof(1::smallint + 1::uint4), 1::smallint + 1::uint4;
  1177. ----
  1178. bigint 2
  1179. query TT
  1180. SELECT pg_typeof(1::smallint + 1::uint8), 1::smallint + 1::uint8;
  1181. ----
  1182. numeric 2
  1183. query TT
  1184. SELECT pg_typeof(1::integer + 1::uint2), 1::integer + 1::uint2;
  1185. ----
  1186. integer 2
  1187. query TT
  1188. SELECT pg_typeof(1::integer + 1::uint4), 1::integer + 1::uint4;
  1189. ----
  1190. bigint 2
  1191. query TT
  1192. SELECT pg_typeof(1::integer + 1::uint8), 1::integer + 1::uint8;
  1193. ----
  1194. numeric 2
  1195. query TT
  1196. SELECT pg_typeof(1::bigint + 1::uint2), 1::bigint + 1::uint2;
  1197. ----
  1198. bigint 2
  1199. query TT
  1200. SELECT pg_typeof(1::bigint + 1::uint4), 1::bigint + 1::uint4;
  1201. ----
  1202. bigint 2
  1203. query TT
  1204. SELECT pg_typeof(1::bigint + 1::uint8), 1::bigint + 1::uint8;
  1205. ----
  1206. numeric 2
  1207. query T
  1208. SELECT 9223372036854775807::bigint / 18446744073709551615::uint8;
  1209. ----
  1210. 0.499999999999999999972894945687862389148
  1211. query T
  1212. SELECT 18446744073709551615::uint8 / 9223372036854775807::bigint;
  1213. ----
  1214. 2.00000000000000000010842021724855044341
  1215. query T
  1216. select pg_typeof(coalesce(1::uint4, 1::int));
  1217. ----
  1218. bigint
  1219. query T
  1220. select pg_typeof(coalesce(1::uint4, 1::int, 1::numeric));
  1221. ----
  1222. numeric
  1223. query T
  1224. select pg_typeof(coalesce(1::uint8, 1::int));
  1225. ----
  1226. numeric
  1227. query T
  1228. SELECT pg_typeof(coalesce(1::uint4, 1::int, 1::numeric, 1::double));
  1229. ----
  1230. double precision
  1231. # These types were not converted because the function's parameters are
  1232. # polymorphic without enfocing a relationship between the arguments, and we only
  1233. # aggressively cast ints and uints to a wider type in cases where it exactly
  1234. # selects a function.
  1235. query T multiline
  1236. EXPLAIN RAW PLAN FOR
  1237. SELECT jsonb_object_agg(1::uint8, 2::int);
  1238. ----
  1239. Reduce aggregates=[jsonb_object_agg[order_by=[]](row(row(uint8_to_text(integer_to_uint8(1)), coalesce(jsonbable_to_jsonb(integer_to_numeric(2)), json_null))))]
  1240. Constant
  1241. - ()
  1242. Target cluster: quickstart
  1243. EOF
  1244. # This would fail to plan if we blithely unified all args into the best common
  1245. # int/uint; the second argument to lag does not support numeric types.
  1246. query T
  1247. WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
  1248. SELECT lag(f1, 1::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1249. FROM t
  1250. ----
  1251. NULL
  1252. query T
  1253. WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
  1254. SELECT pg_typeof(lag(f1, 1::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1))
  1255. FROM t
  1256. ----
  1257. uint8
  1258. # However, polymorphic type resolution can still appropriately upcast values,
  1259. # while leaving other types alone.
  1260. query T
  1261. WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
  1262. SELECT lag(f1, 1::int, 0::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1)
  1263. FROM t
  1264. ----
  1265. 0
  1266. query T
  1267. WITH t (f1, f2, f3) AS (VALUES (1::uint8, 'a', 1.0))
  1268. SELECT pg_typeof(lag(f1, 1::int, 0::int) OVER (PARTITION BY f2 ORDER BY f3 DESC, f1))
  1269. FROM t
  1270. ----
  1271. numeric
  1272. # implicit timestamp_to_timestamptz cast
  1273. query T
  1274. values ('2023-01-01T00:00:00.666'::timestamp(6)) union all values ('2023-01-01T00:00:00.666666'::timestamptz(3));
  1275. ----
  1276. 2023-01-01 00:00:00.666+00
  1277. 2023-01-01 00:00:00.667+00
  1278. query error numeric field overflow
  1279. SELECT '1e-307'::float8::numeric
  1280. statement ok
  1281. CREATE TABLE t1(a varchar(3));
  1282. statement ok
  1283. CREATE TABLE t2(b varchar(4));
  1284. statement error db error: ERROR: value too long for type character varying\(3\)
  1285. INSERT INTO t1 VALUES ('123456');
  1286. # From the Postgres docs: https://www.postgresql.org/docs/current/datatype-character.html
  1287. # "An attempt to store a longer string into a column of these types will result in an error, unless the excess
  1288. # characters are all spaces, in which case the string will be truncated to the maximum length. (This somewhat bizarre
  1289. # exception is required by the SQL standard.)"
  1290. statement ok
  1291. INSERT INTO t1 VALUES ('111 ');
  1292. statement ok
  1293. INSERT INTO t1 VALUES ('123');
  1294. statement ok
  1295. INSERT INTO t2 VALUES ('1234');
  1296. query T multiline
  1297. EXPLAIN OPTIMIZED PLAN WITH (TYPES) AS VERBOSE TEXT FOR
  1298. (SELECT * FROM t1) UNION ALL (SELECT * FROM t2);
  1299. ----
  1300. Explained Query:
  1301. Union // { types: "(varchar?)" }
  1302. ReadStorage materialize.public.t1 // { types: "(varchar(3)?)" }
  1303. ReadStorage materialize.public.t2 // { types: "(varchar(4)?)" }
  1304. Source materialize.public.t1
  1305. Source materialize.public.t2
  1306. Target cluster: quickstart
  1307. EOF
  1308. query T
  1309. (SELECT * FROM t1) UNION ALL (SELECT * FROM t2);
  1310. ----
  1311. 111
  1312. 123
  1313. 1234
  1314. query T
  1315. (SELECT * FROM t2) UNION ALL (SELECT * FROM t1);
  1316. ----
  1317. 111
  1318. 123
  1319. 1234
  1320. statement ok
  1321. CREATE TABLE t3(b char(2));
  1322. statement ok
  1323. INSERT INTO t3 VALUES ('ab');
  1324. query T multiline
  1325. EXPLAIN OPTIMIZED PLAN WITH (TYPES) AS VERBOSE TEXT FOR
  1326. (SELECT * FROM t1) UNION ALL (SELECT * FROM t3);
  1327. ----
  1328. Explained Query:
  1329. Union // { types: "(varchar?)" }
  1330. ReadStorage materialize.public.t1 // { types: "(varchar(3)?)" }
  1331. Project (#1) // { types: "(varchar?)" }
  1332. Map (text_to_varchar[len=unbounded](#0)) // { types: "(char(2)?, varchar?)" }
  1333. ReadStorage materialize.public.t3 // { types: "(char(2)?)" }
  1334. Source materialize.public.t1
  1335. Source materialize.public.t3
  1336. Target cluster: quickstart
  1337. EOF
  1338. query T rowsort
  1339. (SELECT * FROM t1) UNION ALL (SELECT * FROM t3);
  1340. ----
  1341. ab
  1342. 111
  1343. 123
  1344. statement ok
  1345. CREATE TABLE t4(b char(3));
  1346. statement ok
  1347. INSERT INTO t4 VALUES ('ab ');
  1348. query T multiline
  1349. EXPLAIN OPTIMIZED PLAN WITH (TYPES) AS VERBOSE TEXT FOR
  1350. (SELECT * FROM t3) UNION (SELECT * FROM t4);
  1351. ----
  1352. Explained Query:
  1353. Distinct project=[#0] // { types: "(char?)" }
  1354. Union // { types: "(char?)" }
  1355. ReadStorage materialize.public.t3 // { types: "(char(2)?)" }
  1356. ReadStorage materialize.public.t4 // { types: "(char(3)?)" }
  1357. Source materialize.public.t3
  1358. Source materialize.public.t4
  1359. Target cluster: quickstart
  1360. EOF
  1361. # Trailing spaces are treated as semantically insignificant in both Postgres and Materialize, so the above
  1362. # 'ab' and 'ab ' end up deduplicated.
  1363. # See also in char-varchar-distinct.td
  1364. query T
  1365. (SELECT * FROM t3) UNION (SELECT * FROM t4);
  1366. ----
  1367. ab