expressions.sql 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. -- current_time / localtime
  10. SELECT now()::timetz::text = current_time::text;
  11. SELECT now()::timetz(4)::text = current_time(4)::text;
  12. SELECT now()::time::text = localtime::text;
  13. SELECT now()::time(3)::text = localtime(3)::text;
  14. -- current_timestamp / localtimestamp (always matches because of transactional behaviour)
  15. SELECT current_timestamp = NOW();
  16. -- precision
  17. SELECT length(current_timestamp::text) >= length(current_timestamp(0)::text);
  18. -- localtimestamp
  19. SELECT now()::timestamp::text = localtimestamp::text;
  20. -- current_role/user/user is tested in rolnames.sql
  21. -- current database / catalog
  22. SELECT current_catalog = current_database();
  23. -- current_schema
  24. SELECT current_schema;
  25. SET search_path = 'notme';
  26. SELECT current_schema;
  27. SET search_path = 'pg_catalog';
  28. SELECT current_schema;
  29. RESET search_path;
  30. --
  31. -- Tests for BETWEEN
  32. --
  33. explain (costs off)
  34. select count(*) from date_tbl
  35. where f1 between '1997-01-01' and '1998-01-01';
  36. select count(*) from date_tbl
  37. where f1 between '1997-01-01' and '1998-01-01';
  38. explain (costs off)
  39. select count(*) from date_tbl
  40. where f1 not between '1997-01-01' and '1998-01-01';
  41. select count(*) from date_tbl
  42. where f1 not between '1997-01-01' and '1998-01-01';
  43. explain (costs off)
  44. select count(*) from date_tbl
  45. where f1 between symmetric '1997-01-01' and '1998-01-01';
  46. select count(*) from date_tbl
  47. where f1 between symmetric '1997-01-01' and '1998-01-01';
  48. explain (costs off)
  49. select count(*) from date_tbl
  50. where f1 not between symmetric '1997-01-01' and '1998-01-01';
  51. select count(*) from date_tbl
  52. where f1 not between symmetric '1997-01-01' and '1998-01-01';
  53. --
  54. -- Test parsing of a no-op cast to a type with unspecified typmod
  55. --
  56. begin;
  57. create table numeric_tbl (f1 numeric(18,3), f2 numeric);
  58. create view numeric_view as
  59. select
  60. f1, f1::numeric(16,4) as f1164, f1::numeric as f1n,
  61. f2, f2::numeric(16,4) as f2164, f2::numeric as f2n
  62. from numeric_tbl;
  63. \d+ numeric_view
  64. explain (verbose, costs off) select * from numeric_view;
  65. -- bpchar, lacking planner support for its length coercion function,
  66. -- could behave differently
  67. create table bpchar_tbl (f1 character(16) unique, f2 bpchar);
  68. create view bpchar_view as
  69. select
  70. f1, f1::character(14) as f114, f1::bpchar as f1n,
  71. f2, f2::character(14) as f214, f2::bpchar as f2n
  72. from bpchar_tbl;
  73. \d+ bpchar_view
  74. explain (verbose, costs off) select * from bpchar_view
  75. where f1::bpchar = 'foo';
  76. rollback;
  77. --
  78. -- Ordinarily, IN/NOT IN can be converted to a ScalarArrayOpExpr
  79. -- with a suitably-chosen array type.
  80. --
  81. explain (verbose, costs off)
  82. select random() IN (1, 4, 8.0);
  83. explain (verbose, costs off)
  84. select random()::int IN (1, 4, 8.0);
  85. -- However, if there's not a common supertype for the IN elements,
  86. -- we should instead try to produce "x = v1 OR x = v2 OR ...".
  87. -- In most cases that'll fail for lack of all the requisite = operators,
  88. -- but it can succeed sometimes. So this should complain about lack of
  89. -- an = operator, not about cast failure.
  90. select '(0,0)'::point in ('(0,0,0,0)'::box, point(0,0));
  91. --
  92. -- Tests for ScalarArrayOpExpr with a hashfn
  93. --
  94. -- create a stable function so that the tests below are not
  95. -- evaluated using the planner's constant folding.
  96. begin;
  97. create function return_int_input(int) returns int as $$
  98. begin
  99. return $1;
  100. end;
  101. $$ language plpgsql stable;
  102. create function return_text_input(text) returns text as $$
  103. begin
  104. return $1;
  105. end;
  106. $$ language plpgsql stable;
  107. select return_int_input(1) in (10, 9, 2, 8, 3, 7, 4, 6, 5, 1);
  108. select return_int_input(1) in (10, 9, 2, 8, 3, 7, 4, 6, 5, null);
  109. select return_int_input(1) in (null, null, null, null, null, null, null, null, null, null, null);
  110. select return_int_input(1) in (10, 9, 2, 8, 3, 7, 4, 6, 5, 1, null);
  111. select return_int_input(null::int) in (10, 9, 2, 8, 3, 7, 4, 6, 5, 1);
  112. select return_int_input(null::int) in (10, 9, 2, 8, 3, 7, 4, 6, 5, null);
  113. select return_text_input('a') in ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j');
  114. rollback;
  115. -- Test with non-strict equality function.
  116. -- We need to create our own type for this.
  117. begin;
  118. create type myint;
  119. create function myintin(cstring) returns myint strict immutable language
  120. internal as 'int4in';
  121. create function myintout(myint) returns cstring strict immutable language
  122. internal as 'int4out';
  123. create function myinthash(myint) returns integer strict immutable language
  124. internal as 'hashint4';
  125. create type myint (input = myintin, output = myintout, like = int4);
  126. create cast (int4 as myint) without function;
  127. create cast (myint as int4) without function;
  128. create function myinteq(myint, myint) returns bool as $$
  129. begin
  130. if $1 is null and $2 is null then
  131. return true;
  132. else
  133. return $1::int = $2::int;
  134. end if;
  135. end;
  136. $$ language plpgsql immutable;
  137. create operator = (
  138. leftarg = myint,
  139. rightarg = myint,
  140. commutator = =,
  141. negator = <>,
  142. procedure = myinteq,
  143. restrict = eqsel,
  144. join = eqjoinsel,
  145. merges
  146. );
  147. create operator class myint_ops
  148. default for type myint using hash as
  149. operator 1 = (myint, myint),
  150. function 1 myinthash(myint);
  151. create table inttest (a myint);
  152. insert into inttest values(1::myint),(null);
  153. -- try an array with enough elements to cause hashing
  154. 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);
  155. -- ensure the result matched with the non-hashed version. We simply remove
  156. -- some array elements so that we don't reach the hashing threshold.
  157. select * from inttest where a in (1::myint,2::myint,3::myint,4::myint,5::myint, null);
  158. rollback;