rowtypes.slt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. # Copyright 1994, Regents of the University of California.
  2. # Copyright 1996-2021 PostgreSQL Global Development Group.
  3. # Copyright Materialize, Inc. and contributors. All rights reserved.
  4. #
  5. # Use of this software is governed by the Business Source License
  6. # included in the LICENSE file at the root of this repository.
  7. #
  8. # As of the Change Date specified in that file, in accordance with
  9. # the Business Source License, use of this software will be governed
  10. # by the Apache License, Version 2.0.
  11. #
  12. # This file is derived from the regression test suite in PostgreSQL.
  13. # The original file was retrieved on February 17, 2022 from:
  14. #
  15. # https://github.com/postgres/postgres/blob/c30f54ad732ca5c8762bb68bbe0f51de9137dd72/src/test/regress/sql/rowtypes.sql
  16. #
  17. # The original source code is subject to the terms of the PostgreSQL
  18. # license, a copy of which can be found in the LICENSE file at the
  19. # root of this repository.
  20. ## tests omitted from the postgres test suite:
  21. ## * table rowtypes
  22. ## * `create function`
  23. ## * toasted references
  24. ## * functional notation
  25. ## * ordering of rowtypes with non-comparable fields
  26. ## * binary representation operators
  27. mode cockroach
  28. # make a standalone composite type
  29. statement ok
  30. create type complex as (r float8, i float8);
  31. # Nested composite
  32. statement ok
  33. create type quad as (c1 complex, c2 complex);
  34. # and with various container types as fields
  35. statement ok
  36. CREATE TYPE int4_list AS LIST (ELEMENT TYPE = int4);
  37. statement ok
  38. CREATE TYPE int4_map AS MAP (KEY TYPE = text, VALUE TYPE = int4);
  39. statement ok
  40. CREATE TYPE melange AS (a int[][], b text[], c int4_list, d int4_map);
  41. # Some simple tests of conversions and row construction
  42. query T
  43. select (1.1, 2.2)::complex
  44. ----
  45. (1.1,2.2)
  46. query T
  47. select ROW((1.1, 2.2), ROW(3.3, NULL))::quad
  48. ----
  49. ("(1.1,2.2)","(3.3,)")
  50. query TT
  51. select (1.1,2.2)::complex, row((3.3,4.4),(5.5,null))::quad;
  52. ----
  53. (1.1,2.2) ("(3.3,4.4)","(5.5,)")
  54. query T
  55. select ROW(ROW(1.1, 2.2), ROW(3.3, 4.4))::quad
  56. ----
  57. ("(1.1,2.2)","(3.3,4.4)")
  58. query T
  59. select ROW((1.1, 2.2), ROW(3.3, 4.4)::complex)::quad
  60. ----
  61. ("(1.1,2.2)","(3.3,4.4)")
  62. query T
  63. select ROW(ARRAY[[1, 2], [3, 4]], ARRAY['abc', 'def'], LIST[5, 6, 7], '{g=>8, h=>9}')::melange::text;
  64. ----
  65. ({{1,2},{3,4}},{abc,def},{5,6,7},{g=>8,h=>9})
  66. # implicit casting of nested records to a named composite type
  67. statement ok
  68. create table quadtable(f1 int, q quad);
  69. statement ok
  70. insert into quadtable values (1, ((3.3,4.4),(5.5,6.6)));
  71. statement ok
  72. insert into quadtable values (2, ((null,4.4),(5.5,6.6)));
  73. query IT rowsort
  74. select * from quadtable;
  75. ----
  76. 1 ("(3.3,4.4)","(5.5,6.6)")
  77. 2 ("(,4.4)","(5.5,6.6)")
  78. query error does not exist
  79. select f1, q.c1 from quadtable;
  80. query ITR rowsort
  81. select f1, (q).c1, (qq.q).c1.i from quadtable qq;
  82. ----
  83. 1 (3.3,4.4) 4.4
  84. 2 (,4.4) 4.4
  85. ## TODO: support inserts and updates for named composite type fields
  86. #statement ok
  87. #insert into quadtable (f1, q.c1.r, q.c2.i) values(44,55,66);
  88. #
  89. #statement ok
  90. #update quadtable set q.c1.r = 12 where f1 = 2;
  91. #
  92. #query error oh no
  93. #update quadtable set q.c1 = 12;
  94. #
  95. #query IT rowsort
  96. #select * from quadtable;
  97. #----
  98. #1 ("(3.3,4.4)","(5.5,6.6)")
  99. #2 ("(12,4.4)","(5.5,6.6)"
  100. #44 ("(55,)","(,66)")
  101. query B
  102. select ROW(1,2) < ROW(1,3) as true;
  103. ----
  104. true
  105. query B
  106. select ROW(1,2) < ROW(1,1) as false;
  107. ----
  108. false
  109. query B
  110. select ROW(1,2) < ROW(1,NULL) is null;
  111. ----
  112. true
  113. query B
  114. select ROW(1,2,3) < ROW(1,3,NULL) as true;
  115. ----
  116. true
  117. query B
  118. select ROW(11,'ABC') < ROW(11,'DEF') as true;
  119. ----
  120. true
  121. query B
  122. select ROW(11,'ABC') > ROW(11,'DEF') as false;
  123. ----
  124. false
  125. query B
  126. select ROW(12,'ABC') > ROW(11,'DEF') as true;
  127. ----
  128. true
  129. query B
  130. select ROW(1,2,3) < ROW(1,NULL,4) is null;
  131. ----
  132. true
  133. query B
  134. select ROW(1,2,3) = ROW(1,NULL,4) as false;
  135. ----
  136. false
  137. query B
  138. select ROW(1,2,3) <> ROW(1,NULL,4) as true;
  139. ----
  140. true
  141. query B
  142. select ROW(1,2) = ROW(1,2::int8);
  143. ----
  144. true
  145. query B
  146. select ROW(1,2) in (ROW(3,4), ROW(1,2));
  147. ----
  148. true
  149. query B
  150. select ROW(1,2) in (ROW(3,4), ROW(1,2::int8));
  151. ----
  152. true
  153. statement ok
  154. CREATE TABLE tenk1 (
  155. unique1 int4,
  156. unique2 int4,
  157. two int4,
  158. four int4,
  159. ten int4,
  160. twenty int4,
  161. hundred int4,
  162. thousand int4,
  163. twothousand int4,
  164. fivethous int4,
  165. tenthous int4,
  166. odd int4,
  167. even int4,
  168. stringu1 text,
  169. stringu2 text,
  170. string4 text
  171. )
  172. copy tenk1 test/sqllogictest/postgres/testdata/tenk.data
  173. # Check row comparison with a subselect
  174. query II
  175. select unique1, unique2 from tenk1
  176. where (unique1, unique2) < any (select ten, ten from tenk1 where hundred < 3)
  177. and unique1 <= 20
  178. order by 1;
  179. ----
  180. 0 9998
  181. 1 2838
  182. query II
  183. select thousand, tenthous from tenk1
  184. where (thousand, tenthous) >= (997, 5000)
  185. order by thousand, tenthous;
  186. ----
  187. 997 5997
  188. 997 6997
  189. 997 7997
  190. 997 8997
  191. 997 9997
  192. 998 998
  193. 998 1998
  194. 998 2998
  195. 998 3998
  196. 998 4998
  197. 998 5998
  198. 998 6998
  199. 998 7998
  200. 998 8998
  201. 998 9998
  202. 999 999
  203. 999 1999
  204. 999 2999
  205. 999 3999
  206. 999 4999
  207. 999 5999
  208. 999 6999
  209. 999 7999
  210. 999 8999
  211. 999 9999
  212. query III
  213. select thousand, tenthous, four from tenk1
  214. where (thousand, tenthous, four) > (998, 5000, 3)
  215. order by thousand, tenthous;
  216. ----
  217. 998 5998 2
  218. 998 6998 2
  219. 998 7998 2
  220. 998 8998 2
  221. 998 9998 2
  222. 999 999 3
  223. 999 1999 3
  224. 999 2999 3
  225. 999 3999 3
  226. 999 4999 3
  227. 999 5999 3
  228. 999 6999 3
  229. 999 7999 3
  230. 999 8999 3
  231. 999 9999 3
  232. query II
  233. select thousand, tenthous from tenk1
  234. where (998, 5000) < (thousand, tenthous)
  235. order by thousand, tenthous;
  236. ----
  237. 998 5998
  238. 998 6998
  239. 998 7998
  240. 998 8998
  241. 998 9998
  242. 999 999
  243. 999 1999
  244. 999 2999
  245. 999 3999
  246. 999 4999
  247. 999 5999
  248. 999 6999
  249. 999 7999
  250. 999 8999
  251. 999 9999
  252. query II
  253. select thousand, hundred from tenk1
  254. where (998, 5000) < (thousand, hundred)
  255. order by thousand, hundred;
  256. ----
  257. 999 99
  258. 999 99
  259. 999 99
  260. 999 99
  261. 999 99
  262. 999 99
  263. 999 99
  264. 999 99
  265. 999 99
  266. 999 99
  267. ## Check ability to select columns from an anonymous rowtype
  268. query I
  269. select (row(1, 2.0)).f1;
  270. ----
  271. 1
  272. # TODO: there's an incompatibility here, postgres returns 2.0 (this issue is not specific to rowtypes)
  273. query R
  274. select (row(1, 2.0)).f2;
  275. ----
  276. 2
  277. query error nosuch not found
  278. select (row(1, 2.0)).nosuch;
  279. query IR
  280. select (row(1, 2.0)).*;
  281. ----
  282. 1 2
  283. query I
  284. select (r).f1 from (select row(1, 2.0) as r) ss;
  285. ----
  286. 1
  287. query error f3 not found
  288. select (r).f3 from (select row(1, 2.0) as r) ss;
  289. query IR
  290. select (r).* from (select row(1, 2.0) as r) ss;
  291. ----
  292. 1 2
  293. ## Check some corner cases involving empty rowtypes
  294. query T
  295. select ROW();
  296. ----
  297. ()
  298. # TODO: empty row should be considered null
  299. #query B
  300. #select ROW() IS NULL;
  301. #----
  302. #true
  303. query error cannot compare rows of zero length
  304. select ROW() = ROW();
  305. ## TODO: support arrays of anonymous rowtypes
  306. # Check ability to create arrays of anonymous rowtypes
  307. #select array[ row(1,2), row(3,4), row(5,6) ];
  308. # Check ability to compare an anonymous row to elements of an array
  309. #select row(1,1.1) = any (array[ row(7,7.7), row(1,1.1), row(0,0.0) ]);
  310. #select row(1,1.1) = any (array[ row(7,7.7), row(1,1.0), row(0,0.0) ]);
  311. ## Binary comparison functions between row types
  312. statement ok
  313. create type testtype1 as (a int, b int);
  314. query B
  315. select row(1, 2)::testtype1 < row(1, 3)::testtype1;
  316. ----
  317. true
  318. query B
  319. select row(1, 2)::testtype1 <= row(1, 3)::testtype1;
  320. ----
  321. true
  322. query B
  323. select row(1, 2)::testtype1 = row(1, 2)::testtype1;
  324. ----
  325. true
  326. query B
  327. select row(1, 2)::testtype1 <> row(1, 3)::testtype1;
  328. ----
  329. true
  330. query B
  331. select row(1, 3)::testtype1 >= row(1, 2)::testtype1;
  332. ----
  333. true
  334. query B
  335. select row(1, 3)::testtype1 > row(1, 2)::testtype1;
  336. ----
  337. true
  338. query B
  339. select row(1, -2)::testtype1 < row(1, -3)::testtype1;
  340. ----
  341. false
  342. query B
  343. select row(1, -2)::testtype1 <= row(1, -3)::testtype1;
  344. ----
  345. false
  346. query B
  347. select row(1, -2)::testtype1 = row(1, -3)::testtype1;
  348. ----
  349. false
  350. query B
  351. select row(1, -2)::testtype1 <> row(1, -2)::testtype1;
  352. ----
  353. false
  354. query B
  355. select row(1, -3)::testtype1 >= row(1, -2)::testtype1;
  356. ----
  357. false
  358. query B
  359. select row(1, -3)::testtype1 > row(1, -2)::testtype1;
  360. ----
  361. false
  362. query B
  363. select row(1, -2)::testtype1 < row(1, 3)::testtype1;
  364. ----
  365. true
  366. statement ok
  367. create type testtype3 as (a int, b text);
  368. # note: error message here differs from postgres, which is
  369. # more specific about whether column type or count mismatched
  370. query error no overload
  371. select row(1, 2)::testtype1 < row(1, 'abc')::testtype3;
  372. query error no overload
  373. select row(1, 2)::testtype1 <> row(1, 'abc')::testtype3;
  374. statement ok
  375. create type testtype5 as (a int);
  376. query error no overload
  377. select row(1, 2)::testtype1 < row(1)::testtype5;
  378. query error no overload
  379. select row(1, 2)::testtype1 <> row(1)::testtype5;
  380. # just for fun, let's try everything at once. let's combine some
  381. # implicit + explicit casts + field access over nested structs
  382. # that are structurally equivalent, but not the same. at the end
  383. # of the day, postgres/we should be doing nothing more than
  384. # field-by-field comparison of each row, regardless of named type.
  385. statement ok
  386. create type testtype6 as (inner testtype1);
  387. statement ok
  388. create type testtype8 as (f1 int, f2 int);
  389. query B
  390. select row(4,2)::testtype8 > (row(row(4,1))::testtype6).inner;
  391. ----
  392. true
  393. query B
  394. select row(4,2)::testtype8 > (row(row(4,2))::testtype6).inner;
  395. ----
  396. false
  397. query B
  398. select row(4,2)::testtype8 = (row(row(4,2))::testtype6).inner;
  399. ----
  400. true
  401. query error input of anonymous composite types is not implemented
  402. select row(4,2)::testtype8 = '(1,2)';
  403. # TODO: we don't support aliasing into a rowtype
  404. # anonymous rowtypes in coldeflists (column definition lists)
  405. #select q.a, q.b = row(2), q.c = array[row(3)], q.d = row(row(4)) from
  406. # unnest(array[row(1, row(2), array[row(3)], row(row(4))),
  407. # row(2, row(3), array[row(4)], row(row(5)))])
  408. # as q(a int, b record, c record[], d record);
  409. # IS [NOT] NULL should not recurse into nested composites
  410. #select r, r is null as isnull, r is not null as isnotnull
  411. #from (values (1,row(1,2)), (1,row(null,null)), (1,null),
  412. # (null,row(1,2)), (null,row(null,null)), (null,null) ) r(a,b);
  413. #
  414. #select r, r is null as isnull, r is not null as isnotnull
  415. #from (values (1,row(1,2)), (1,row(null,null)), (1,null),
  416. # (null,row(1,2)), (null,row(null,null)), (null,null) ) r(a,b);
  417. #
  418. #explain (verbose, costs off)
  419. #with r(a,b) as materialized
  420. # (values (1,row(1,2)), (1,row(null,null)), (1,null),
  421. # (null,row(1,2)), (null,row(null,null)), (null,null) )
  422. #select r, r is null as isnull, r is not null as isnotnull from r;
  423. #
  424. #with r(a,b) as materialized
  425. # (values (1,row(1,2)), (1,row(null,null)), (1,null),
  426. # (null,row(1,2)), (null,row(null,null)), (null,null) )
  427. #select r, r is null as isnull, r is not null as isnotnull from r;