1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
ij> --
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
--
-- this tests derived column lists and derived tables
--
create table s (a int, b int, c int, d int, e int, f int);
0 rows inserted/updated/deleted
ij> create table t (aa int, bb int, cc int, dd int, ee int, ff int);
0 rows inserted/updated/deleted
ij> insert into s values (0,1,2,3,4,5);
1 row inserted/updated/deleted
ij> insert into s values (10,11,12,13,14,15);
1 row inserted/updated/deleted
ij> -- tests without a derived table
-- negative tests
-- # of columns does not match
select aa from s ss (aa);
ERROR 42X32: The number of columns in the derived column list must match the number of columns in table 'APP.S'.
ij> select aa from s ss (aa, bb, cc, dd, ee, ff, gg);
ERROR 42X32: The number of columns in the derived column list must match the number of columns in table 'APP.S'.
ij> -- duplicate names in derived column list
select aa from s ss (aa, ee, bb, cc, dd, aa);
ERROR 42X33: The derived column list contains a duplicate column name 'AA'.
ij> -- test case insensitivity
select aa from s ss (aa, bb, cc, dd, ee, AA);
ERROR 42X33: The derived column list contains a duplicate column name 'AA'.
ij> -- test uniqueness of names
select aa from s ss (aa, bb, cc, dd, ee, ff), t;
ERROR 42X03: Column name 'AA' is in more than one table in the FROM list.
ij> -- test uniqueness of names
insert into t select aa
from s aa (aa, bb, cc, dd, ee, ff), s bb (aa, bb, cc, dd, ee, ff);
ERROR 42X03: Column name 'AA' is in more than one table in the FROM list.
ij> -- verify using "exposed" names
select a from s ss (aa, bb, cc, dd, ee, ff);
ERROR 42X04: Column 'A' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'A' is not a column in the target table.
ij> -- positive tests
-- rename the columns
select * from s ss (f, e, d, c, b, a) where f = 0;
F |E |D |C |B |A
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
ij> -- delimited identifiers in list
select * from s ss ("a a", "b b", "c c", "d d", "e e", "f f") where "a a" = 0;
a a |b b |c c |d d |e e |f f
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
ij> -- uniqueness of "exposed" names
select a, aa from s a, s b (aa, bb, cc, dd, ee, ff) where f = ff and aa = 10;
A |AA
-----------------------
10 |10
ij> select a.a, b.aa from s a, s b (aa, bb, cc, dd, ee, ff) where f = ff and b.aa = 10;
A |AA
-----------------------
10 |10
ij> -- insert tests
insert into t select * from s ss (aa, bb, cc, dd, ee, ff);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t (aa,bb) select ff, aa from s ss (aa, bb, cc, dd, ee, ff);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
5 |0 |NULL |NULL |NULL |NULL
15 |10 |NULL |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- derived tables
-- negative tests
-- no correlation name
select * from (select * from s);
ERROR 42X01: Syntax error: Encountered "<EOF>" at line 5, column 31.
Issue the 'help' command for general information on IJ command syntax.
Any unrecognized commands are treated as potential SQL commands and executed directly.
Consult your DBMS server reference documentation for details of the SQL syntax supported by your server.
ij> -- # of columns does not match
select aa from (select * from s) ss (aa);
ERROR 42X32: The number of columns in the derived column list must match the number of columns in table 'SS'.
ij> select aa from (select * from s) ss (aa, bb, cc, dd, ee, ff, gg);
ERROR 42X32: The number of columns in the derived column list must match the number of columns in table 'SS'.
ij> -- duplicate names in derived column list
select aa from (select * from s) ss (aa, ee, bb, cc, dd, aa);
ERROR 42X33: The derived column list contains a duplicate column name 'AA'.
ij> -- test case insensitivity
select aa from (select * from s) ss (aa, bb, cc, dd, ee, AA);
ERROR 42X33: The derived column list contains a duplicate column name 'AA'.
ij> -- test uniqueness of names
select aa from (select * from s) ss (aa, bb, cc, dd, ee, ff), t;
ERROR 42X03: Column name 'AA' is in more than one table in the FROM list.
ij> -- test uniqueness of names
insert into t select aa
from (select * from s) aa (aa, bb, cc, dd, ee, ff),
(select * from s) bb (aa, bb, cc, dd, ee, ff);
ERROR 42X03: Column name 'AA' is in more than one table in the FROM list.
ij> -- verify using "exposed" names
select a from (select * from s) ss (aa, bb, cc, dd, ee, ff);
ERROR 42X04: Column 'A' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'A' is not a column in the target table.
ij> -- ambiguous column reference
select a from (select * from s a, s b) ss;
ERROR 42Y34: Column name 'A' matches more than one result column in table 'SS'.
ij> -- positive tests
-- simple derived table
select a from (select a from s) a;
A
-----------
0
10
ij> -- select * query's
select * from (select * from s) a;
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> select * from (select a, b, c, d, e, f from s) a;
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> select * from (select a, b, c from s) a;
A |B |C
-----------------------------------
0 |1 |2
10 |11 |12
ij> select a, b, c, d, e, f from (select * from s) a;
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> -- simple derived table
insert into t (aa) select a from (select a from s) a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |NULL |NULL |NULL |NULL |NULL
10 |NULL |NULL |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- select * query's
insert into t select * from (select * from s) a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t select * from (select a, b, c, d, e, f from s) a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t (aa, bb, cc) select * from (select a, b, c from s) a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |NULL |NULL |NULL
10 |11 |12 |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t select a, b, c, d, e, f from (select * from s) a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- simple derived table with derived column list
select a from (select a from s) a (a);
A
-----------
0
10
ij> -- select * query's with derived column lists
select * from (select * from s) a (f, e, d, c, b, a);
F |E |D |C |B |A
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> select * from (select a, b, c, d, e, f from s) a (f, e, d, c, b, a);
F |E |D |C |B |A
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> select * from (select a, b, c from s) a (c, f, e);
C |F |E
-----------------------------------
0 |1 |2
10 |11 |12
ij> select a, b, c, d, e, f from (select * from s) a (a, b, c, d, e, f);
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> -- simple derived table with derived column list
insert into t (aa) select a from (select a from s) a (a);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |NULL |NULL |NULL |NULL |NULL
10 |NULL |NULL |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- select * query's with derived column lists
insert into t select * from (select * from s) a (c, b, a, e, f, d);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t select * from (select a, b, c, d, e, f from s) a (f, a, c, b, e, d);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t (aa, bb, cc) select * from (select a, b, c from s) a (f, e, a);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |NULL |NULL |NULL
10 |11 |12 |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> insert into t select a, c, "a", "b", b, "c" from (select * from s) a (a, c, "a", "b", b, "c");
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- project and reorder derived column list
select a, f from (select * from s) a (b, c, d, e, f, a);
A |F
-----------------------
5 |4
15 |14
ij> insert into t (aa, bb) select a, f from (select * from s) a (b, c, d, e, f, a);
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
5 |4 |NULL |NULL |NULL |NULL
15 |14 |NULL |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- outer where clause references columns from derived table
select * from (select * from s) a (a, b, c, d, e, f) where a = 0;
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
ij> select * from (select * from s) a (f, e, d, c, b, a) where f = 0;
F |E |D |C |B |A
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
ij> insert into t select * from (select * from s) a (a, b, c, d, e, f) where a = 0;
1 row inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
ij> delete from t;
1 row inserted/updated/deleted
ij> insert into t select * from (select * from s) a (f, e, d, c, b, a) where f = 0;
1 row inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
ij> delete from t;
1 row inserted/updated/deleted
ij> -- join between 2 derived tables
select * from (select a from s) a, (select a from s) b;
A |A
-----------------------
0 |0
0 |10
10 |0
10 |10
ij> select * from (select a from s) a, (select a from s) b where a.a = b.a;
A |A
-----------------------
0 |0
10 |10
ij> insert into t (aa, bb) select * from (select a from s) a, (select a from s) b where a.a = b.a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |0 |NULL |NULL |NULL |NULL
10 |10 |NULL |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- join within a derived table
select * from (select a.a, b.a from s a, s b) a (b, a) where b = a;
B |A
-----------------------
0 |0
10 |10
ij> select * from (select a.a, b.a from s a, s b) a (b, a),
(select a.a, b.a from s a, s b) b (b, a) where a.b = b.b;
B |A |B |A
-----------------------------------------------
0 |0 |0 |0
0 |0 |0 |10
0 |10 |0 |0
0 |10 |0 |10
10 |0 |10 |0
10 |0 |10 |10
10 |10 |10 |0
10 |10 |10 |10
ij> select * from (select (select 1 from s where 1 = 0), b.a from s a, s b) a (b, a),
(select (select 1 from s where 1 = 0), b.a from s a, s b) b (b, a) where a.b = b.b;
B |A |B |A
-----------------------------------------------
ij> insert into t (aa, bb) select * from (select a.a, b.a from s a, s b) a (b, a) where b = a;
2 rows inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |0 |NULL |NULL |NULL |NULL
10 |10 |NULL |NULL |NULL |NULL
ij> delete from t;
2 rows inserted/updated/deleted
ij> -- join within a derived table, 2 predicates can be pushed all the way down
select * from (select a.a, b.a from s a, s b) a (b, a) where b = a and a = 0 and b = 0;
B |A
-----------------------
0 |0
ij> insert into t (aa, bb) select * from (select a.a, b.a from s a, s b) a (b, a) where b = a and a = 0 and b = 0;
1 row inserted/updated/deleted
ij> select * from t;
AA |BB |CC |DD |EE |FF
-----------------------------------------------------------------------
0 |0 |NULL |NULL |NULL |NULL
ij> delete from t;
1 row inserted/updated/deleted
ij> -- nested derived tables
select * from (select * from (select * from s) a ) a;
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from
(select * from s) a
) a
) a
) a
) a
) a
) a
) a
) a
) a
) a
) a
) a
) a
) a;
A |B |C |D |E |F
-----------------------------------------------------------------------
0 |1 |2 |3 |4 |5
10 |11 |12 |13 |14 |15
ij> -- test predicate push through
select * from
(select a.a as a1, b.a as a2 from s a, s b) a
where a.a1 = 0 and a.a2 = 10;
A1 |A2
-----------------------
0 |10
ij> -- push column = column through
select * from (select a, a from s) a (x, y) where x = y;
X |Y
-----------------------
0 |0
10 |10
ij> select * from (select a, a from s) a (x, y) where x + y = x * y;
X |Y
-----------------------
0 |0
ij> -- return contants and expressions from derived table
select * from (select 1 from s) a;
1
-----------
1
1
ij> select * from (select 1 from s) a (x) where x = 1;
X
-----------
1
1
ij> select * from (select 1 from s a, s b where a.a = b.a) a (x);
X
-----------
1
1
ij> select * from (select 1 from s a, s b where a.a = b.a) a (x) where x = 1;
X
-----------
1
1
ij> select * from (select a + 1 from s) a;
1
-----------
1
11
ij> select * from (select a + 1 from s) a (x) where x = 1;
X
-----------
1
ij> select * from (select a.a + 1 from s a, s b where a.a = b.a) a (x) where x = 1;
X
-----------
1
ij> -- Bug 2767, don't flatten derived table with join
create table tab1(tab1_c1 int, tab1_c2 int);
0 rows inserted/updated/deleted
ij> create table tab2(tab2_c1 int, tab2_c2 int);
0 rows inserted/updated/deleted
ij> insert into tab1 values (1, 1), (2, 2);
2 rows inserted/updated/deleted
ij> insert into tab2 values (1, 1), (2, 2);
2 rows inserted/updated/deleted
ij> select * from (select * from tab1, tab2) c where tab1_c1 in (1, 3);
TAB1_C1 |TAB1_C2 |TAB2_C1 |TAB2_C2
-----------------------------------------------
1 |1 |1 |1
1 |1 |2 |2
ij> drop table tab1;
0 rows inserted/updated/deleted
ij> drop table tab2;
0 rows inserted/updated/deleted
ij> drop table s;
0 rows inserted/updated/deleted
ij> drop table t;
0 rows inserted/updated/deleted
ij>
|