Back to home page

OSCL-LXR

 
 

    


0001 --
0002 -- Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
0003 --
0004 --
0005 -- SELECT
0006 -- Test int8 64-bit integers.
0007 -- https://github.com/postgres/postgres/blob/REL_12_BETA2/src/test/regress/sql/select.sql
0008 --
0009 create or replace temporary view onek2 as select * from onek;
0010 create or replace temporary view INT8_TBL as select * from values
0011   (cast(trim('  123   ') as bigint), cast(trim('  456') as bigint)),
0012   (cast(trim('123   ') as bigint),cast('4567890123456789' as bigint)),
0013   (cast('4567890123456789' as bigint),cast('123' as bigint)),
0014   (cast(+4567890123456789 as bigint),cast('4567890123456789' as bigint)),
0015   (cast('+4567890123456789' as bigint),cast('-4567890123456789' as bigint))
0016   as INT8_TBL(q1, q2);
0017 
0018 -- btree index
0019 -- awk '{if($1<10){print;}else{next;}}' onek.data | sort +0n -1
0020 --
0021 SELECT * FROM onek
0022    WHERE onek.unique1 < 10
0023    ORDER BY onek.unique1;
0024 
0025 -- [SPARK-28010] Support ORDER BY ... USING syntax
0026 --
0027 -- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
0028 --
0029 SELECT onek.unique1, onek.stringu1 FROM onek
0030    WHERE onek.unique1 < 20
0031    ORDER BY unique1 DESC;
0032 
0033 --
0034 -- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
0035 --
0036 SELECT onek.unique1, onek.stringu1 FROM onek
0037    WHERE onek.unique1 > 980
0038    ORDER BY stringu1 ASC;
0039 
0040 --
0041 -- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
0042 -- sort +1d -2 +0nr -1
0043 --
0044 SELECT onek.unique1, onek.string4 FROM onek
0045    WHERE onek.unique1 > 980
0046    ORDER BY string4 ASC, unique1 DESC;
0047 
0048 --
0049 -- awk '{if($1>980){print $1,$16;}else{next;}}' onek.data |
0050 -- sort +1dr -2 +0n -1
0051 --
0052 SELECT onek.unique1, onek.string4 FROM onek
0053    WHERE onek.unique1 > 980
0054    ORDER BY string4 DESC, unique1 ASC;
0055 
0056 --
0057 -- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
0058 -- sort +0nr -1 +1d -2
0059 --
0060 SELECT onek.unique1, onek.string4 FROM onek
0061    WHERE onek.unique1 < 20
0062    ORDER BY unique1 DESC, string4 ASC;
0063 
0064 --
0065 -- awk '{if($1<20){print $1,$16;}else{next;}}' onek.data |
0066 -- sort +0n -1 +1dr -2
0067 --
0068 SELECT onek.unique1, onek.string4 FROM onek
0069    WHERE onek.unique1 < 20
0070    ORDER BY unique1 ASC, string4 DESC;
0071 
0072 --
0073 -- test partial btree indexes
0074 --
0075 -- As of 7.2, planner probably won't pick an indexscan without stats,
0076 -- so ANALYZE first.  Also, we want to prevent it from picking a bitmapscan
0077 -- followed by sort, because that could hide index ordering problems.
0078 --
0079 -- ANALYZE onek2;
0080 
0081 -- SET enable_seqscan TO off;
0082 -- SET enable_bitmapscan TO off;
0083 -- SET enable_sort TO off;
0084 
0085 --
0086 -- awk '{if($1<10){print $0;}else{next;}}' onek.data | sort +0n -1
0087 --
0088 SELECT onek2.* FROM onek2 WHERE onek2.unique1 < 10;
0089 
0090 --
0091 -- awk '{if($1<20){print $1,$14;}else{next;}}' onek.data | sort +0nr -1
0092 --
0093 SELECT onek2.unique1, onek2.stringu1 FROM onek2
0094     WHERE onek2.unique1 < 20
0095     ORDER BY unique1 DESC;
0096 
0097 --
0098 -- awk '{if($1>980){print $1,$14;}else{next;}}' onek.data | sort +1d -2
0099 --
0100 SELECT onek2.unique1, onek2.stringu1 FROM onek2
0101    WHERE onek2.unique1 > 980;
0102 
0103 -- RESET enable_seqscan;
0104 -- RESET enable_bitmapscan;
0105 -- RESET enable_sort;
0106 
0107 -- [SPARK-28329] SELECT INTO syntax
0108 -- SELECT two, stringu1, ten, string4
0109 --    INTO TABLE tmp
0110 --    FROM onek;
0111 CREATE TABLE tmp USING parquet AS
0112 SELECT two, stringu1, ten, string4
0113 FROM onek;
0114 
0115 -- Skip the person table because there is a point data type that we don't support.
0116 --
0117 -- awk '{print $1,$2;}' person.data |
0118 -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
0119 -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
0120 -- awk 'BEGIN{FS="      ";}{if(NF!=2){print $4,$5;}else{print;}}' - stud_emp.data
0121 --
0122 -- SELECT name, age FROM person*; ??? check if different
0123 -- SELECT p.name, p.age FROM person* p;
0124 
0125 --
0126 -- awk '{print $1,$2;}' person.data |
0127 -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - emp.data |
0128 -- awk '{if(NF!=2){print $3,$2;}else{print;}}' - student.data |
0129 -- awk 'BEGIN{FS="      ";}{if(NF!=1){print $4,$5;}else{print;}}' - stud_emp.data |
0130 -- sort +1nr -2
0131 --
0132 -- SELECT p.name, p.age FROM person* p ORDER BY age DESC, name;
0133 
0134 -- [SPARK-28330] Enhance query limit
0135 --
0136 -- Test some cases involving whole-row Var referencing a subquery
0137 --
0138 select foo.* from (select 1) as foo;
0139 select foo.* from (select null) as foo;
0140 select foo.* from (select 'xyzzy',1,null) as foo;
0141 
0142 --
0143 -- Test VALUES lists
0144 --
0145 select * from onek, values(147, 'RFAAAA'), (931, 'VJAAAA') as v (i, j)
0146     WHERE onek.unique1 = v.i and onek.stringu1 = v.j;
0147 
0148 -- [SPARK-28296] Improved VALUES support
0149 -- a more complex case
0150 -- looks like we're coding lisp :-)
0151 -- select * from onek,
0152 --   (values ((select i from
0153 --     (values(10000), (2), (389), (1000), (2000), ((select 10029))) as foo(i)
0154 --     order by i asc limit 1))) bar (i)
0155 --   where onek.unique1 = bar.i;
0156 
0157 -- try VALUES in a subquery
0158 -- select * from onek
0159 --     where (unique1,ten) in (values (1,1), (20,0), (99,9), (17,99))
0160 --     order by unique1;
0161 
0162 -- VALUES is also legal as a standalone query or a set-operation member
0163 VALUES (1,2), (3,4+4), (7,77.7);
0164 
0165 VALUES (1,2), (3,4+4), (7,77.7)
0166 UNION ALL
0167 SELECT 2+2, 57
0168 UNION ALL
0169 TABLE int8_tbl;
0170 
0171 --
0172 -- Test ORDER BY options
0173 --
0174 
0175 CREATE OR REPLACE TEMPORARY VIEW foo AS
0176 SELECT * FROM (values(42),(3),(10),(7),(null),(null),(1)) as foo (f1);
0177 
0178 -- [SPARK-28333] NULLS FIRST for DESC and NULLS LAST for ASC
0179 SELECT * FROM foo ORDER BY f1;
0180 SELECT * FROM foo ORDER BY f1 ASC;      -- same thing
0181 SELECT * FROM foo ORDER BY f1 NULLS FIRST;
0182 SELECT * FROM foo ORDER BY f1 DESC;
0183 SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
0184 
0185 -- check if indexscans do the right things
0186 -- CREATE INDEX fooi ON foo (f1);
0187 -- SET enable_sort = false;
0188 
0189 -- SELECT * FROM foo ORDER BY f1;
0190 -- SELECT * FROM foo ORDER BY f1 NULLS FIRST;
0191 -- SELECT * FROM foo ORDER BY f1 DESC;
0192 -- SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
0193 
0194 -- DROP INDEX fooi;
0195 -- CREATE INDEX fooi ON foo (f1 DESC);
0196 
0197 -- SELECT * FROM foo ORDER BY f1;
0198 -- SELECT * FROM foo ORDER BY f1 NULLS FIRST;
0199 -- SELECT * FROM foo ORDER BY f1 DESC;
0200 -- SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
0201 
0202 -- DROP INDEX fooi;
0203 -- CREATE INDEX fooi ON foo (f1 DESC NULLS LAST);
0204 
0205 -- SELECT * FROM foo ORDER BY f1;
0206 -- SELECT * FROM foo ORDER BY f1 NULLS FIRST;
0207 -- SELECT * FROM foo ORDER BY f1 DESC;
0208 -- SELECT * FROM foo ORDER BY f1 DESC NULLS LAST;
0209 
0210 --
0211 -- Test planning of some cases with partial indexes
0212 --
0213 
0214 -- partial index is usable
0215 -- explain (costs off)
0216 -- select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
0217 select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
0218 -- actually run the query with an analyze to use the partial index
0219 -- explain (costs off, analyze on, timing off, summary off)
0220 -- select * from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
0221 -- explain (costs off)
0222 -- select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
0223 select unique2 from onek2 where unique2 = 11 and stringu1 = 'ATAAAA';
0224 -- partial index predicate implies clause, so no need for retest
0225 -- explain (costs off)
0226 -- select * from onek2 where unique2 = 11 and stringu1 < 'B';
0227 select * from onek2 where unique2 = 11 and stringu1 < 'B';
0228 -- explain (costs off)
0229 -- select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
0230 select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
0231 -- but if it's an update target, must retest anyway
0232 -- explain (costs off)
0233 -- select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
0234 -- select unique2 from onek2 where unique2 = 11 and stringu1 < 'B' for update;
0235 -- partial index is not applicable
0236 -- explain (costs off)
0237 -- select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
0238 select unique2 from onek2 where unique2 = 11 and stringu1 < 'C';
0239 -- partial index implies clause, but bitmap scan must recheck predicate anyway
0240 -- SET enable_indexscan TO off;
0241 -- explain (costs off)
0242 -- select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
0243 select unique2 from onek2 where unique2 = 11 and stringu1 < 'B';
0244 -- RESET enable_indexscan;
0245 -- check multi-index cases too
0246 -- explain (costs off)
0247 -- select unique1, unique2 from onek2
0248 --   where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
0249 select unique1, unique2 from onek2
0250   where (unique2 = 11 or unique1 = 0) and stringu1 < 'B';
0251 -- explain (costs off)
0252 -- select unique1, unique2 from onek2
0253 --   where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
0254 select unique1, unique2 from onek2
0255   where (unique2 = 11 and stringu1 < 'B') or unique1 = 0;
0256 
0257 --
0258 -- Test some corner cases that have been known to confuse the planner
0259 --
0260 
0261 -- ORDER BY on a constant doesn't really need any sorting
0262 SELECT 1 AS x ORDER BY x;
0263 
0264 -- But ORDER BY on a set-valued expression does
0265 -- create function sillysrf(int) returns setof int as
0266 --   'values (1),(10),(2),($1)' language sql immutable;
0267 
0268 -- select sillysrf(42);
0269 -- select sillysrf(-1) order by 1;
0270 
0271 -- drop function sillysrf(int);
0272 
0273 -- X = X isn't a no-op, it's effectively X IS NOT NULL assuming = is strict
0274 -- (see bug #5084)
0275 select * from (values (2),(null),(1)) v(k) where k = k order by k;
0276 select * from (values (2),(null),(1)) v(k) where k = k;
0277 
0278 -- Test partitioned tables with no partitions, which should be handled the
0279 -- same as the non-inheritance case when expanding its RTE.
0280 -- create table list_parted_tbl (a int,b int) partition by list (a);
0281 -- create table list_parted_tbl1 partition of list_parted_tbl
0282 --   for values in (1) partition by list(b);
0283 -- explain (costs off) select * from list_parted_tbl;
0284 -- drop table list_parted_tbl;
0285 drop table tmp;