expressions.out 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. --
  2. -- expression evaluation tests that don't fit into a more specific file
  3. --
  4. --
  5. -- Tests for SQLVAlueFunction
  6. --
  7. -- current_date (always matches because of transactional behaviour)
  8. SELECT date(now())::text = current_date::text;
  9. ?column?
  10. ----------
  11. t
  12. (1 row)
  13. -- current_time / localtime
  14. SELECT now()::timetz::text = current_time::text;
  15. ?column?
  16. ----------
  17. t
  18. (1 row)
  19. SELECT now()::timetz(4)::text = current_time(4)::text;
  20. ?column?
  21. ----------
  22. t
  23. (1 row)
  24. SELECT now()::time::text = localtime::text;
  25. ?column?
  26. ----------
  27. t
  28. (1 row)
  29. SELECT now()::time(3)::text = localtime(3)::text;
  30. ?column?
  31. ----------
  32. t
  33. (1 row)
  34. -- current_timestamp / localtimestamp (always matches because of transactional behaviour)
  35. SELECT current_timestamp = NOW();
  36. ?column?
  37. ----------
  38. t
  39. (1 row)
  40. -- precision
  41. SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text);
  42. ?column?
  43. ----------
  44. t
  45. (1 row)
  46. -- localtimestamp
  47. SELECT now()::timestamp::text = localtimestamp::text;
  48. ?column?
  49. ----------
  50. t
  51. (1 row)
  52. -- current_role/user/user is tested in rolnames.sql
  53. -- current database / catalog
  54. SELECT current_catalog = current_database();
  55. ?column?
  56. ----------
  57. t
  58. (1 row)
  59. -- current_schema
  60. SELECT current_schema;
  61. current_schema
  62. ----------------
  63. public
  64. (1 row)
  65. SET search_path = 'notme';
  66. SELECT current_schema;
  67. current_schema
  68. ----------------
  69. (1 row)
  70. SET search_path = 'pg_catalog';
  71. SELECT current_schema;
  72. current_schema
  73. ----------------
  74. pg_catalog
  75. (1 row)
  76. RESET search_path;
  77. --
  78. -- Tests for BETWEEN
  79. --
  80. explain (costs off)
  81. select count(*) from date_tbl
  82. where f1 between '1997-01-01' and '1998-01-01';
  83. QUERY PLAN
  84. -----------------------------------------------------------------------------
  85. Aggregate
  86. -> Seq Scan on date_tbl
  87. Filter: ((f1 >= '01-01-1997'::date) AND (f1 <= '01-01-1998'::date))
  88. (3 rows)
  89. select count(*) from date_tbl
  90. where f1 between '1997-01-01' and '1998-01-01';
  91. count
  92. -------
  93. 3
  94. (1 row)
  95. explain (costs off)
  96. select count(*) from date_tbl
  97. where f1 not between '1997-01-01' and '1998-01-01';
  98. QUERY PLAN
  99. --------------------------------------------------------------------------
  100. Aggregate
  101. -> Seq Scan on date_tbl
  102. Filter: ((f1 < '01-01-1997'::date) OR (f1 > '01-01-1998'::date))
  103. (3 rows)
  104. select count(*) from date_tbl
  105. where f1 not between '1997-01-01' and '1998-01-01';
  106. count
  107. -------
  108. 13
  109. (1 row)
  110. explain (costs off)
  111. select count(*) from date_tbl
  112. where f1 between symmetric '1997-01-01' and '1998-01-01';
  113. QUERY PLAN
  114. ----------------------------------------------------------------------------------------------------------------------------------------------
  115. Aggregate
  116. -> Seq Scan on date_tbl
  117. Filter: (((f1 >= '01-01-1997'::date) AND (f1 <= '01-01-1998'::date)) OR ((f1 >= '01-01-1998'::date) AND (f1 <= '01-01-1997'::date)))
  118. (3 rows)
  119. select count(*) from date_tbl
  120. where f1 between symmetric '1997-01-01' and '1998-01-01';
  121. count
  122. -------
  123. 3
  124. (1 row)
  125. explain (costs off)
  126. select count(*) from date_tbl
  127. where f1 not between symmetric '1997-01-01' and '1998-01-01';
  128. QUERY PLAN
  129. -----------------------------------------------------------------------------------------------------------------------------------------
  130. Aggregate
  131. -> Seq Scan on date_tbl
  132. Filter: (((f1 < '01-01-1997'::date) OR (f1 > '01-01-1998'::date)) AND ((f1 < '01-01-1998'::date) OR (f1 > '01-01-1997'::date)))
  133. (3 rows)
  134. select count(*) from date_tbl
  135. where f1 not between symmetric '1997-01-01' and '1998-01-01';
  136. count
  137. -------
  138. 13
  139. (1 row)
  140. --
  141. -- Test parsing of a no-op cast to a type with unspecified typmod
  142. --
  143. begin;
  144. create table numeric_tbl (f1 numeric(18,3), f2 numeric);
  145. create view numeric_view as
  146. select
  147. f1, f1::numeric(16,4) as f1164, f1::numeric as f1n,
  148. f2, f2::numeric(16,4) as f2164, f2::numeric as f2n
  149. from numeric_tbl;
  150. \d+ numeric_view
  151. View "public.numeric_view"
  152. Column | Type | Collation | Nullable | Default | Storage | Description
  153. --------+---------------+-----------+----------+---------+---------+-------------
  154. f1 | numeric(18,3) | | | | main |
  155. f1164 | numeric(16,4) | | | | main |
  156. f1n | numeric | | | | main |
  157. f2 | numeric | | | | main |
  158. f2164 | numeric(16,4) | | | | main |
  159. f2n | numeric | | | | main |
  160. View definition:
  161. SELECT numeric_tbl.f1,
  162. numeric_tbl.f1::numeric(16,4) AS f1164,
  163. numeric_tbl.f1::numeric AS f1n,
  164. numeric_tbl.f2,
  165. numeric_tbl.f2::numeric(16,4) AS f2164,
  166. numeric_tbl.f2 AS f2n
  167. FROM numeric_tbl;
  168. explain (verbose, costs off) select * from numeric_view;
  169. QUERY PLAN
  170. -------------------------------------------------------------------------------------------------------------------------------------------------------
  171. Seq Scan on public.numeric_tbl
  172. Output: numeric_tbl.f1, (numeric_tbl.f1)::numeric(16,4), (numeric_tbl.f1)::numeric, numeric_tbl.f2, (numeric_tbl.f2)::numeric(16,4), numeric_tbl.f2
  173. (2 rows)
  174. -- bpchar, lacking planner support for its length coercion function,
  175. -- could behave differently
  176. create table bpchar_tbl (f1 character(16) unique, f2 bpchar);
  177. create view bpchar_view as
  178. select
  179. f1, f1::character(14) as f114, f1::bpchar as f1n,
  180. f2, f2::character(14) as f214, f2::bpchar as f2n
  181. from bpchar_tbl;
  182. \d+ bpchar_view
  183. View "public.bpchar_view"
  184. Column | Type | Collation | Nullable | Default | Storage | Description
  185. --------+---------------+-----------+----------+---------+----------+-------------
  186. f1 | character(16) | | | | extended |
  187. f114 | character(14) | | | | extended |
  188. f1n | bpchar | | | | extended |
  189. f2 | bpchar | | | | extended |
  190. f214 | character(14) | | | | extended |
  191. f2n | bpchar | | | | extended |
  192. View definition:
  193. SELECT bpchar_tbl.f1,
  194. bpchar_tbl.f1::character(14) AS f114,
  195. bpchar_tbl.f1::bpchar AS f1n,
  196. bpchar_tbl.f2,
  197. bpchar_tbl.f2::character(14) AS f214,
  198. bpchar_tbl.f2 AS f2n
  199. FROM bpchar_tbl;
  200. explain (verbose, costs off) select * from bpchar_view
  201. where f1::bpchar = 'foo';
  202. QUERY PLAN
  203. ------------------------------------------------------------------------------------------------------------------------------------------------
  204. Index Scan using bpchar_tbl_f1_key on public.bpchar_tbl
  205. Output: bpchar_tbl.f1, (bpchar_tbl.f1)::character(14), (bpchar_tbl.f1)::bpchar, bpchar_tbl.f2, (bpchar_tbl.f2)::character(14), bpchar_tbl.f2
  206. Index Cond: ((bpchar_tbl.f1)::bpchar = 'foo'::bpchar)
  207. (3 rows)
  208. rollback;
  209. --
  210. -- Ordinarily, IN/NOT IN can be converted to a ScalarArrayOpExpr
  211. -- with a suitably-chosen array type.
  212. --
  213. explain (verbose, costs off)
  214. select random() IN (1, 4, 8.0);
  215. QUERY PLAN
  216. ------------------------------------------------------------
  217. Result
  218. Output: (random() = ANY ('{1,4,8}'::double precision[]))
  219. (2 rows)
  220. explain (verbose, costs off)
  221. select random()::int IN (1, 4, 8.0);
  222. QUERY PLAN
  223. ---------------------------------------------------------------------------
  224. Result
  225. Output: (((random())::integer)::numeric = ANY ('{1,4,8.0}'::numeric[]))
  226. (2 rows)
  227. -- However, if there's not a common supertype for the IN elements,
  228. -- we should instead try to produce "x = v1 OR x = v2 OR ...".
  229. -- In most cases that'll fail for lack of all the requisite = operators,
  230. -- but it can succeed sometimes. So this should complain about lack of
  231. -- an = operator, not about cast failure.
  232. select '(0,0)'::point in ('(0,0,0,0)'::box, point(0,0));
  233. ERROR: operator does not exist: point = box
  234. LINE 1: select '(0,0)'::point in ('(0,0,0,0)'::box, point(0,0));
  235. ^
  236. HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
  237. --
  238. -- Tests for ScalarArrayOpExpr with a hashfn
  239. --
  240. -- create a stable function so that the tests below are not
  241. -- evaluated using the planner's constant folding.
  242. begin;
  243. create function return_int_input(int) returns int as $$
  244. begin
  245. return $1;
  246. end;
  247. $$ language plpgsql stable;
  248. create function return_text_input(text) returns text as $$
  249. begin
  250. return $1;
  251. end;
  252. $$ language plpgsql stable;
  253. select return_int_input(1) in (10, 9, 2, 8, 3, 7, 4, 6, 5, 1);
  254. ?column?
  255. ----------
  256. t
  257. (1 row)
  258. select return_int_input(1) in (10, 9, 2, 8, 3, 7, 4, 6, 5, null);
  259. ?column?
  260. ----------
  261. (1 row)
  262. select return_int_input(1) in (null, null, null, null, null, null, null, null, null, null, null);
  263. ?column?
  264. ----------
  265. (1 row)
  266. select return_int_input(1) in (10, 9, 2, 8, 3, 7, 4, 6, 5, 1, null);
  267. ?column?
  268. ----------
  269. t
  270. (1 row)
  271. select return_int_input(null::int) in (10, 9, 2, 8, 3, 7, 4, 6, 5, 1);
  272. ?column?
  273. ----------
  274. (1 row)
  275. select return_int_input(null::int) in (10, 9, 2, 8, 3, 7, 4, 6, 5, null);
  276. ?column?
  277. ----------
  278. (1 row)
  279. select return_text_input('a') in ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
  280. ?column?
  281. ----------
  282. t
  283. (1 row)
  284. rollback;
  285. -- Test with non-strict equality function.
  286. -- We need to create our own type for this.
  287. begin;
  288. create type myint;
  289. create function myintin(cstring) returns myint strict immutable language
  290. internal as 'int4in';
  291. NOTICE: return type myint is only a shell
  292. create function myintout(myint) returns cstring strict immutable language
  293. internal as 'int4out';
  294. NOTICE: argument type myint is only a shell
  295. create function myinthash(myint) returns integer strict immutable language
  296. internal as 'hashint4';
  297. NOTICE: argument type myint is only a shell
  298. create type myint (input = myintin, output = myintout, like = int4);
  299. create cast (int4 as myint) without function;
  300. create cast (myint as int4) without function;
  301. create function myinteq(myint, myint) returns bool as $$
  302. begin
  303. if $1 is null and $2 is null then
  304. return true;
  305. else
  306. return $1::int = $2::int;
  307. end if;
  308. end;
  309. $$ language plpgsql immutable;
  310. create operator = (
  311. leftarg = myint,
  312. rightarg = myint,
  313. commutator = =,
  314. negator = <>,
  315. procedure = myinteq,
  316. restrict = eqsel,
  317. join = eqjoinsel,
  318. merges
  319. );
  320. create operator class myint_ops
  321. default for type myint using hash as
  322. operator 1 = (myint, myint),
  323. function 1 myinthash(myint);
  324. create table inttest (a myint);
  325. insert into inttest values(1::myint),(null);
  326. -- try an array with enough elements to cause hashing
  327. select * from inttest where a in (1::myint,2::myint,3::myint,4::myint,5::myint,6::myint,7::myint,8::myint,9::myint, null);
  328. a
  329. ---
  330. 1
  331. (2 rows)
  332. -- ensure the result matched with the non-hashed version. We simply remove
  333. -- some array elements so that we don't reach the hashing threshold.
  334. select * from inttest where a in (1::myint,2::myint,3::myint,4::myint,5::myint, null);
  335. a
  336. ---
  337. 1
  338. (2 rows)
  339. rollback;