[Commits] 11d1b9b0eb3: MDEV-23867: insert... select crash in compute_window_func
by varun 06 Oct '20
by varun 06 Oct '20
06 Oct '20
revision-id: 11d1b9b0eb337ac0f95177c0fb9b9eadf7a2222d (mariadb-10.4.11-416-g11d1b9b0eb3)
parent(s): 55fefe1a68af779eacedc7c189fb606a8fcd9d96
author: Varun Gupta
committer: Varun Gupta
timestamp: 2020-10-06 22:46:20 +0530
message:
MDEV-23867: insert... select crash in compute_window_func
Part #1 : Copy the key used for encryption for the slave IO_CACHE
---
.../suite/encryption/r/tempfiles_encrypted.result | 5002 ++++++++++++++++++++
.../suite/encryption/t/tempfiles_encrypted.test | 1 +
mysys/mf_iocache.c | 13 +-
sql/sql_window.cc | 2 +
4 files changed, 5016 insertions(+), 2 deletions(-)
diff --git a/mysql-test/suite/encryption/r/tempfiles_encrypted.result b/mysql-test/suite/encryption/r/tempfiles_encrypted.result
index d08cb33e0fc..559d16dd174 100644
--- a/mysql-test/suite/encryption/r/tempfiles_encrypted.result
+++ b/mysql-test/suite/encryption/r/tempfiles_encrypted.result
@@ -14,5 +14,5007 @@ insert into t1 select seq, seq, seq from seq_1_to_5000;
select count(*) from (select a, b, c, ROW_NUMBER() OVER (PARTITION BY a) FROM t1)q;
count(*)
5000
+select a, b, c, sum(a) OVER () FROM t1;
+a b c sum(a) OVER ()
+1 1 1 12502500
+2 2 2 12502500
+3 3 3 12502500
+4 4 4 12502500
+5 5 5 12502500
+6 6 6 12502500
+7 7 7 12502500
+8 8 8 12502500
+9 9 9 12502500
+10 10 10 12502500
+11 11 11 12502500
+12 12 12 12502500
+13 13 13 12502500
+14 14 14 12502500
+15 15 15 12502500
+16 16 16 12502500
+17 17 17 12502500
+18 18 18 12502500
+19 19 19 12502500
+20 20 20 12502500
+21 21 21 12502500
+22 22 22 12502500
+23 23 23 12502500
+24 24 24 12502500
+25 25 25 12502500
+26 26 26 12502500
+27 27 27 12502500
+28 28 28 12502500
+29 29 29 12502500
+30 30 30 12502500
+31 31 31 12502500
+32 32 32 12502500
+33 33 33 12502500
+34 34 34 12502500
+35 35 35 12502500
+36 36 36 12502500
+37 37 37 12502500
+38 38 38 12502500
+39 39 39 12502500
+40 40 40 12502500
+41 41 41 12502500
+42 42 42 12502500
+43 43 43 12502500
+44 44 44 12502500
+45 45 45 12502500
+46 46 46 12502500
+47 47 47 12502500
+48 48 48 12502500
+49 49 49 12502500
+50 50 50 12502500
+51 51 51 12502500
+52 52 52 12502500
+53 53 53 12502500
+54 54 54 12502500
+55 55 55 12502500
+56 56 56 12502500
+57 57 57 12502500
+58 58 58 12502500
+59 59 59 12502500
+60 60 60 12502500
+61 61 61 12502500
+62 62 62 12502500
+63 63 63 12502500
+64 64 64 12502500
+65 65 65 12502500
+66 66 66 12502500
+67 67 67 12502500
+68 68 68 12502500
+69 69 69 12502500
+70 70 70 12502500
+71 71 71 12502500
+72 72 72 12502500
+73 73 73 12502500
+74 74 74 12502500
+75 75 75 12502500
+76 76 76 12502500
+77 77 77 12502500
+78 78 78 12502500
+79 79 79 12502500
+80 80 80 12502500
+81 81 81 12502500
+82 82 82 12502500
+83 83 83 12502500
+84 84 84 12502500
+85 85 85 12502500
+86 86 86 12502500
+87 87 87 12502500
+88 88 88 12502500
+89 89 89 12502500
+90 90 90 12502500
+91 91 91 12502500
+92 92 92 12502500
+93 93 93 12502500
+94 94 94 12502500
+95 95 95 12502500
+96 96 96 12502500
+97 97 97 12502500
+98 98 98 12502500
+99 99 99 12502500
+100 100 100 12502500
+101 101 101 12502500
+102 102 102 12502500
+103 103 103 12502500
+104 104 104 12502500
+105 105 105 12502500
+106 106 106 12502500
+107 107 107 12502500
+108 108 108 12502500
+109 109 109 12502500
+110 110 110 12502500
+111 111 111 12502500
+112 112 112 12502500
+113 113 113 12502500
+114 114 114 12502500
+115 115 115 12502500
+116 116 116 12502500
+117 117 117 12502500
+118 118 118 12502500
+119 119 119 12502500
+120 120 120 12502500
+121 121 121 12502500
+122 122 122 12502500
+123 123 123 12502500
+124 124 124 12502500
+125 125 125 12502500
+126 126 126 12502500
+127 127 127 12502500
+128 128 128 12502500
+129 129 129 12502500
+130 130 130 12502500
+131 131 131 12502500
+132 132 132 12502500
+133 133 133 12502500
+134 134 134 12502500
+135 135 135 12502500
+136 136 136 12502500
+137 137 137 12502500
+138 138 138 12502500
+139 139 139 12502500
+140 140 140 12502500
+141 141 141 12502500
+142 142 142 12502500
+143 143 143 12502500
+144 144 144 12502500
+145 145 145 12502500
+146 146 146 12502500
+147 147 147 12502500
+148 148 148 12502500
+149 149 149 12502500
+150 150 150 12502500
+151 151 151 12502500
+152 152 152 12502500
+153 153 153 12502500
+154 154 154 12502500
+155 155 155 12502500
+156 156 156 12502500
+157 157 157 12502500
+158 158 158 12502500
+159 159 159 12502500
+160 160 160 12502500
+161 161 161 12502500
+162 162 162 12502500
+163 163 163 12502500
+164 164 164 12502500
+165 165 165 12502500
+166 166 166 12502500
+167 167 167 12502500
+168 168 168 12502500
+169 169 169 12502500
+170 170 170 12502500
+171 171 171 12502500
+172 172 172 12502500
+173 173 173 12502500
+174 174 174 12502500
+175 175 175 12502500
+176 176 176 12502500
+177 177 177 12502500
+178 178 178 12502500
+179 179 179 12502500
+180 180 180 12502500
+181 181 181 12502500
+182 182 182 12502500
+183 183 183 12502500
+184 184 184 12502500
+185 185 185 12502500
+186 186 186 12502500
+187 187 187 12502500
+188 188 188 12502500
+189 189 189 12502500
+190 190 190 12502500
+191 191 191 12502500
+192 192 192 12502500
+193 193 193 12502500
+194 194 194 12502500
+195 195 195 12502500
+196 196 196 12502500
+197 197 197 12502500
+198 198 198 12502500
+199 199 199 12502500
+200 200 200 12502500
+201 201 201 12502500
+202 202 202 12502500
+203 203 203 12502500
+204 204 204 12502500
+205 205 205 12502500
+206 206 206 12502500
+207 207 207 12502500
+208 208 208 12502500
+209 209 209 12502500
+210 210 210 12502500
+211 211 211 12502500
+212 212 212 12502500
+213 213 213 12502500
+214 214 214 12502500
+215 215 215 12502500
+216 216 216 12502500
+217 217 217 12502500
+218 218 218 12502500
+219 219 219 12502500
+220 220 220 12502500
+221 221 221 12502500
+222 222 222 12502500
+223 223 223 12502500
+224 224 224 12502500
+225 225 225 12502500
+226 226 226 12502500
+227 227 227 12502500
+228 228 228 12502500
+229 229 229 12502500
+230 230 230 12502500
+231 231 231 12502500
+232 232 232 12502500
+233 233 233 12502500
+234 234 234 12502500
+235 235 235 12502500
+236 236 236 12502500
+237 237 237 12502500
+238 238 238 12502500
+239 239 239 12502500
+240 240 240 12502500
+241 241 241 12502500
+242 242 242 12502500
+243 243 243 12502500
+244 244 244 12502500
+245 245 245 12502500
+246 246 246 12502500
+247 247 247 12502500
+248 248 248 12502500
+249 249 249 12502500
+250 250 250 12502500
+251 251 251 12502500
+252 252 252 12502500
+253 253 253 12502500
+254 254 254 12502500
+255 255 255 12502500
+256 256 256 12502500
+257 257 257 12502500
+258 258 258 12502500
+259 259 259 12502500
+260 260 260 12502500
+261 261 261 12502500
+262 262 262 12502500
+263 263 263 12502500
+264 264 264 12502500
+265 265 265 12502500
+266 266 266 12502500
+267 267 267 12502500
+268 268 268 12502500
+269 269 269 12502500
+270 270 270 12502500
+271 271 271 12502500
+272 272 272 12502500
+273 273 273 12502500
+274 274 274 12502500
+275 275 275 12502500
+276 276 276 12502500
+277 277 277 12502500
+278 278 278 12502500
+279 279 279 12502500
+280 280 280 12502500
+281 281 281 12502500
+282 282 282 12502500
+283 283 283 12502500
+284 284 284 12502500
+285 285 285 12502500
+286 286 286 12502500
+287 287 287 12502500
+288 288 288 12502500
+289 289 289 12502500
+290 290 290 12502500
+291 291 291 12502500
+292 292 292 12502500
+293 293 293 12502500
+294 294 294 12502500
+295 295 295 12502500
+296 296 296 12502500
+297 297 297 12502500
+298 298 298 12502500
+299 299 299 12502500
+300 300 300 12502500
+301 301 301 12502500
+302 302 302 12502500
+303 303 303 12502500
+304 304 304 12502500
+305 305 305 12502500
+306 306 306 12502500
+307 307 307 12502500
+308 308 308 12502500
+309 309 309 12502500
+310 310 310 12502500
+311 311 311 12502500
+312 312 312 12502500
+313 313 313 12502500
+314 314 314 12502500
+315 315 315 12502500
+316 316 316 12502500
+317 317 317 12502500
+318 318 318 12502500
+319 319 319 12502500
+320 320 320 12502500
+321 321 321 12502500
+322 322 322 12502500
+323 323 323 12502500
+324 324 324 12502500
+325 325 325 12502500
+326 326 326 12502500
+327 327 327 12502500
+328 328 328 12502500
+329 329 329 12502500
+330 330 330 12502500
+331 331 331 12502500
+332 332 332 12502500
+333 333 333 12502500
+334 334 334 12502500
+335 335 335 12502500
+336 336 336 12502500
+337 337 337 12502500
+338 338 338 12502500
+339 339 339 12502500
+340 340 340 12502500
+341 341 341 12502500
+342 342 342 12502500
+343 343 343 12502500
+344 344 344 12502500
+345 345 345 12502500
+346 346 346 12502500
+347 347 347 12502500
+348 348 348 12502500
+349 349 349 12502500
+350 350 350 12502500
+351 351 351 12502500
+352 352 352 12502500
+353 353 353 12502500
+354 354 354 12502500
+355 355 355 12502500
+356 356 356 12502500
+357 357 357 12502500
+358 358 358 12502500
+359 359 359 12502500
+360 360 360 12502500
+361 361 361 12502500
+362 362 362 12502500
+363 363 363 12502500
+364 364 364 12502500
+365 365 365 12502500
+366 366 366 12502500
+367 367 367 12502500
+368 368 368 12502500
+369 369 369 12502500
+370 370 370 12502500
+371 371 371 12502500
+372 372 372 12502500
+373 373 373 12502500
+374 374 374 12502500
+375 375 375 12502500
+376 376 376 12502500
+377 377 377 12502500
+378 378 378 12502500
+379 379 379 12502500
+380 380 380 12502500
+381 381 381 12502500
+382 382 382 12502500
+383 383 383 12502500
+384 384 384 12502500
+385 385 385 12502500
+386 386 386 12502500
+387 387 387 12502500
+388 388 388 12502500
+389 389 389 12502500
+390 390 390 12502500
+391 391 391 12502500
+392 392 392 12502500
+393 393 393 12502500
+394 394 394 12502500
+395 395 395 12502500
+396 396 396 12502500
+397 397 397 12502500
+398 398 398 12502500
+399 399 399 12502500
+400 400 400 12502500
+401 401 401 12502500
+402 402 402 12502500
+403 403 403 12502500
+404 404 404 12502500
+405 405 405 12502500
+406 406 406 12502500
+407 407 407 12502500
+408 408 408 12502500
+409 409 409 12502500
+410 410 410 12502500
+411 411 411 12502500
+412 412 412 12502500
+413 413 413 12502500
+414 414 414 12502500
+415 415 415 12502500
+416 416 416 12502500
+417 417 417 12502500
+418 418 418 12502500
+419 419 419 12502500
+420 420 420 12502500
+421 421 421 12502500
+422 422 422 12502500
+423 423 423 12502500
+424 424 424 12502500
+425 425 425 12502500
+426 426 426 12502500
+427 427 427 12502500
+428 428 428 12502500
+429 429 429 12502500
+430 430 430 12502500
+431 431 431 12502500
+432 432 432 12502500
+433 433 433 12502500
+434 434 434 12502500
+435 435 435 12502500
+436 436 436 12502500
+437 437 437 12502500
+438 438 438 12502500
+439 439 439 12502500
+440 440 440 12502500
+441 441 441 12502500
+442 442 442 12502500
+443 443 443 12502500
+444 444 444 12502500
+445 445 445 12502500
+446 446 446 12502500
+447 447 447 12502500
+448 448 448 12502500
+449 449 449 12502500
+450 450 450 12502500
+451 451 451 12502500
+452 452 452 12502500
+453 453 453 12502500
+454 454 454 12502500
+455 455 455 12502500
+456 456 456 12502500
+457 457 457 12502500
+458 458 458 12502500
+459 459 459 12502500
+460 460 460 12502500
+461 461 461 12502500
+462 462 462 12502500
+463 463 463 12502500
+464 464 464 12502500
+465 465 465 12502500
+466 466 466 12502500
+467 467 467 12502500
+468 468 468 12502500
+469 469 469 12502500
+470 470 470 12502500
+471 471 471 12502500
+472 472 472 12502500
+473 473 473 12502500
+474 474 474 12502500
+475 475 475 12502500
+476 476 476 12502500
+477 477 477 12502500
+478 478 478 12502500
+479 479 479 12502500
+480 480 480 12502500
+481 481 481 12502500
+482 482 482 12502500
+483 483 483 12502500
+484 484 484 12502500
+485 485 485 12502500
+486 486 486 12502500
+487 487 487 12502500
+488 488 488 12502500
+489 489 489 12502500
+490 490 490 12502500
+491 491 491 12502500
+492 492 492 12502500
+493 493 493 12502500
+494 494 494 12502500
+495 495 495 12502500
+496 496 496 12502500
+497 497 497 12502500
+498 498 498 12502500
+499 499 499 12502500
+500 500 500 12502500
+501 501 501 12502500
+502 502 502 12502500
+503 503 503 12502500
+504 504 504 12502500
+505 505 505 12502500
+506 506 506 12502500
+507 507 507 12502500
+508 508 508 12502500
+509 509 509 12502500
+510 510 510 12502500
+511 511 511 12502500
+512 512 512 12502500
+513 513 513 12502500
+514 514 514 12502500
+515 515 515 12502500
+516 516 516 12502500
+517 517 517 12502500
+518 518 518 12502500
+519 519 519 12502500
+520 520 520 12502500
+521 521 521 12502500
+522 522 522 12502500
+523 523 523 12502500
+524 524 524 12502500
+525 525 525 12502500
+526 526 526 12502500
+527 527 527 12502500
+528 528 528 12502500
+529 529 529 12502500
+530 530 530 12502500
+531 531 531 12502500
+532 532 532 12502500
+533 533 533 12502500
+534 534 534 12502500
+535 535 535 12502500
+536 536 536 12502500
+537 537 537 12502500
+538 538 538 12502500
+539 539 539 12502500
+540 540 540 12502500
+541 541 541 12502500
+542 542 542 12502500
+543 543 543 12502500
+544 544 544 12502500
+545 545 545 12502500
+546 546 546 12502500
+547 547 547 12502500
+548 548 548 12502500
+549 549 549 12502500
+550 550 550 12502500
+551 551 551 12502500
+552 552 552 12502500
+553 553 553 12502500
+554 554 554 12502500
+555 555 555 12502500
+556 556 556 12502500
+557 557 557 12502500
+558 558 558 12502500
+559 559 559 12502500
+560 560 560 12502500
+561 561 561 12502500
+562 562 562 12502500
+563 563 563 12502500
+564 564 564 12502500
+565 565 565 12502500
+566 566 566 12502500
+567 567 567 12502500
+568 568 568 12502500
+569 569 569 12502500
+570 570 570 12502500
+571 571 571 12502500
+572 572 572 12502500
+573 573 573 12502500
+574 574 574 12502500
+575 575 575 12502500
+576 576 576 12502500
+577 577 577 12502500
+578 578 578 12502500
+579 579 579 12502500
+580 580 580 12502500
+581 581 581 12502500
+582 582 582 12502500
+583 583 583 12502500
+584 584 584 12502500
+585 585 585 12502500
+586 586 586 12502500
+587 587 587 12502500
+588 588 588 12502500
+589 589 589 12502500
+590 590 590 12502500
+591 591 591 12502500
+592 592 592 12502500
+593 593 593 12502500
+594 594 594 12502500
+595 595 595 12502500
+596 596 596 12502500
+597 597 597 12502500
+598 598 598 12502500
+599 599 599 12502500
+600 600 600 12502500
+601 601 601 12502500
+602 602 602 12502500
+603 603 603 12502500
+604 604 604 12502500
+605 605 605 12502500
+606 606 606 12502500
+607 607 607 12502500
+608 608 608 12502500
+609 609 609 12502500
+610 610 610 12502500
+611 611 611 12502500
+612 612 612 12502500
+613 613 613 12502500
+614 614 614 12502500
+615 615 615 12502500
+616 616 616 12502500
+617 617 617 12502500
+618 618 618 12502500
+619 619 619 12502500
+620 620 620 12502500
+621 621 621 12502500
+622 622 622 12502500
+623 623 623 12502500
+624 624 624 12502500
+625 625 625 12502500
+626 626 626 12502500
+627 627 627 12502500
+628 628 628 12502500
+629 629 629 12502500
+630 630 630 12502500
+631 631 631 12502500
+632 632 632 12502500
+633 633 633 12502500
+634 634 634 12502500
+635 635 635 12502500
+636 636 636 12502500
+637 637 637 12502500
+638 638 638 12502500
+639 639 639 12502500
+640 640 640 12502500
+641 641 641 12502500
+642 642 642 12502500
+643 643 643 12502500
+644 644 644 12502500
+645 645 645 12502500
+646 646 646 12502500
+647 647 647 12502500
+648 648 648 12502500
+649 649 649 12502500
+650 650 650 12502500
+651 651 651 12502500
+652 652 652 12502500
+653 653 653 12502500
+654 654 654 12502500
+655 655 655 12502500
+656 656 656 12502500
+657 657 657 12502500
+658 658 658 12502500
+659 659 659 12502500
+660 660 660 12502500
+661 661 661 12502500
+662 662 662 12502500
+663 663 663 12502500
+664 664 664 12502500
+665 665 665 12502500
+666 666 666 12502500
+667 667 667 12502500
+668 668 668 12502500
+669 669 669 12502500
+670 670 670 12502500
+671 671 671 12502500
+672 672 672 12502500
+673 673 673 12502500
+674 674 674 12502500
+675 675 675 12502500
+676 676 676 12502500
+677 677 677 12502500
+678 678 678 12502500
+679 679 679 12502500
+680 680 680 12502500
+681 681 681 12502500
+682 682 682 12502500
+683 683 683 12502500
+684 684 684 12502500
+685 685 685 12502500
+686 686 686 12502500
+687 687 687 12502500
+688 688 688 12502500
+689 689 689 12502500
+690 690 690 12502500
+691 691 691 12502500
+692 692 692 12502500
+693 693 693 12502500
+694 694 694 12502500
+695 695 695 12502500
+696 696 696 12502500
+697 697 697 12502500
+698 698 698 12502500
+699 699 699 12502500
+700 700 700 12502500
+701 701 701 12502500
+702 702 702 12502500
+703 703 703 12502500
+704 704 704 12502500
+705 705 705 12502500
+706 706 706 12502500
+707 707 707 12502500
+708 708 708 12502500
+709 709 709 12502500
+710 710 710 12502500
+711 711 711 12502500
+712 712 712 12502500
+713 713 713 12502500
+714 714 714 12502500
+715 715 715 12502500
+716 716 716 12502500
+717 717 717 12502500
+718 718 718 12502500
+719 719 719 12502500
+720 720 720 12502500
+721 721 721 12502500
+722 722 722 12502500
+723 723 723 12502500
+724 724 724 12502500
+725 725 725 12502500
+726 726 726 12502500
+727 727 727 12502500
+728 728 728 12502500
+729 729 729 12502500
+730 730 730 12502500
+731 731 731 12502500
+732 732 732 12502500
+733 733 733 12502500
+734 734 734 12502500
+735 735 735 12502500
+736 736 736 12502500
+737 737 737 12502500
+738 738 738 12502500
+739 739 739 12502500
+740 740 740 12502500
+741 741 741 12502500
+742 742 742 12502500
+743 743 743 12502500
+744 744 744 12502500
+745 745 745 12502500
+746 746 746 12502500
+747 747 747 12502500
+748 748 748 12502500
+749 749 749 12502500
+750 750 750 12502500
+751 751 751 12502500
+752 752 752 12502500
+753 753 753 12502500
+754 754 754 12502500
+755 755 755 12502500
+756 756 756 12502500
+757 757 757 12502500
+758 758 758 12502500
+759 759 759 12502500
+760 760 760 12502500
+761 761 761 12502500
+762 762 762 12502500
+763 763 763 12502500
+764 764 764 12502500
+765 765 765 12502500
+766 766 766 12502500
+767 767 767 12502500
+768 768 768 12502500
+769 769 769 12502500
+770 770 770 12502500
+771 771 771 12502500
+772 772 772 12502500
+773 773 773 12502500
+774 774 774 12502500
+775 775 775 12502500
+776 776 776 12502500
+777 777 777 12502500
+778 778 778 12502500
+779 779 779 12502500
+780 780 780 12502500
+781 781 781 12502500
+782 782 782 12502500
+783 783 783 12502500
+784 784 784 12502500
+785 785 785 12502500
+786 786 786 12502500
+787 787 787 12502500
+788 788 788 12502500
+789 789 789 12502500
+790 790 790 12502500
+791 791 791 12502500
+792 792 792 12502500
+793 793 793 12502500
+794 794 794 12502500
+795 795 795 12502500
+796 796 796 12502500
+797 797 797 12502500
+798 798 798 12502500
+799 799 799 12502500
+800 800 800 12502500
+801 801 801 12502500
+802 802 802 12502500
+803 803 803 12502500
+804 804 804 12502500
+805 805 805 12502500
+806 806 806 12502500
+807 807 807 12502500
+808 808 808 12502500
+809 809 809 12502500
+810 810 810 12502500
+811 811 811 12502500
+812 812 812 12502500
+813 813 813 12502500
+814 814 814 12502500
+815 815 815 12502500
+816 816 816 12502500
+817 817 817 12502500
+818 818 818 12502500
+819 819 819 12502500
+820 820 820 12502500
+821 821 821 12502500
+822 822 822 12502500
+823 823 823 12502500
+824 824 824 12502500
+825 825 825 12502500
+826 826 826 12502500
+827 827 827 12502500
+828 828 828 12502500
+829 829 829 12502500
+830 830 830 12502500
+831 831 831 12502500
+832 832 832 12502500
+833 833 833 12502500
+834 834 834 12502500
+835 835 835 12502500
+836 836 836 12502500
+837 837 837 12502500
+838 838 838 12502500
+839 839 839 12502500
+840 840 840 12502500
+841 841 841 12502500
+842 842 842 12502500
+843 843 843 12502500
+844 844 844 12502500
+845 845 845 12502500
+846 846 846 12502500
+847 847 847 12502500
+848 848 848 12502500
+849 849 849 12502500
+850 850 850 12502500
+851 851 851 12502500
+852 852 852 12502500
+853 853 853 12502500
+854 854 854 12502500
+855 855 855 12502500
+856 856 856 12502500
+857 857 857 12502500
+858 858 858 12502500
+859 859 859 12502500
+860 860 860 12502500
+861 861 861 12502500
+862 862 862 12502500
+863 863 863 12502500
+864 864 864 12502500
+865 865 865 12502500
+866 866 866 12502500
+867 867 867 12502500
+868 868 868 12502500
+869 869 869 12502500
+870 870 870 12502500
+871 871 871 12502500
+872 872 872 12502500
+873 873 873 12502500
+874 874 874 12502500
+875 875 875 12502500
+876 876 876 12502500
+877 877 877 12502500
+878 878 878 12502500
+879 879 879 12502500
+880 880 880 12502500
+881 881 881 12502500
+882 882 882 12502500
+883 883 883 12502500
+884 884 884 12502500
+885 885 885 12502500
+886 886 886 12502500
+887 887 887 12502500
+888 888 888 12502500
+889 889 889 12502500
+890 890 890 12502500
+891 891 891 12502500
+892 892 892 12502500
+893 893 893 12502500
+894 894 894 12502500
+895 895 895 12502500
+896 896 896 12502500
+897 897 897 12502500
+898 898 898 12502500
+899 899 899 12502500
+900 900 900 12502500
+901 901 901 12502500
+902 902 902 12502500
+903 903 903 12502500
+904 904 904 12502500
+905 905 905 12502500
+906 906 906 12502500
+907 907 907 12502500
+908 908 908 12502500
+909 909 909 12502500
+910 910 910 12502500
+911 911 911 12502500
+912 912 912 12502500
+913 913 913 12502500
+914 914 914 12502500
+915 915 915 12502500
+916 916 916 12502500
+917 917 917 12502500
+918 918 918 12502500
+919 919 919 12502500
+920 920 920 12502500
+921 921 921 12502500
+922 922 922 12502500
+923 923 923 12502500
+924 924 924 12502500
+925 925 925 12502500
+926 926 926 12502500
+927 927 927 12502500
+928 928 928 12502500
+929 929 929 12502500
+930 930 930 12502500
+931 931 931 12502500
+932 932 932 12502500
+933 933 933 12502500
+934 934 934 12502500
+935 935 935 12502500
+936 936 936 12502500
+937 937 937 12502500
+938 938 938 12502500
+939 939 939 12502500
+940 940 940 12502500
+941 941 941 12502500
+942 942 942 12502500
+943 943 943 12502500
+944 944 944 12502500
+945 945 945 12502500
+946 946 946 12502500
+947 947 947 12502500
+948 948 948 12502500
+949 949 949 12502500
+950 950 950 12502500
+951 951 951 12502500
+952 952 952 12502500
+953 953 953 12502500
+954 954 954 12502500
+955 955 955 12502500
+956 956 956 12502500
+957 957 957 12502500
+958 958 958 12502500
+959 959 959 12502500
+960 960 960 12502500
+961 961 961 12502500
+962 962 962 12502500
+963 963 963 12502500
+964 964 964 12502500
+965 965 965 12502500
+966 966 966 12502500
+967 967 967 12502500
+968 968 968 12502500
+969 969 969 12502500
+970 970 970 12502500
+971 971 971 12502500
+972 972 972 12502500
+973 973 973 12502500
+974 974 974 12502500
+975 975 975 12502500
+976 976 976 12502500
+977 977 977 12502500
+978 978 978 12502500
+979 979 979 12502500
+980 980 980 12502500
+981 981 981 12502500
+982 982 982 12502500
+983 983 983 12502500
+984 984 984 12502500
+985 985 985 12502500
+986 986 986 12502500
+987 987 987 12502500
+988 988 988 12502500
+989 989 989 12502500
+990 990 990 12502500
+991 991 991 12502500
+992 992 992 12502500
+993 993 993 12502500
+994 994 994 12502500
+995 995 995 12502500
+996 996 996 12502500
+997 997 997 12502500
+998 998 998 12502500
+999 999 999 12502500
+1000 1000 1000 12502500
+1001 1001 1001 12502500
+1002 1002 1002 12502500
+1003 1003 1003 12502500
+1004 1004 1004 12502500
+1005 1005 1005 12502500
+1006 1006 1006 12502500
+1007 1007 1007 12502500
+1008 1008 1008 12502500
+1009 1009 1009 12502500
+1010 1010 1010 12502500
+1011 1011 1011 12502500
+1012 1012 1012 12502500
+1013 1013 1013 12502500
+1014 1014 1014 12502500
+1015 1015 1015 12502500
+1016 1016 1016 12502500
+1017 1017 1017 12502500
+1018 1018 1018 12502500
+1019 1019 1019 12502500
+1020 1020 1020 12502500
+1021 1021 1021 12502500
+1022 1022 1022 12502500
+1023 1023 1023 12502500
+1024 1024 1024 12502500
+1025 1025 1025 12502500
+1026 1026 1026 12502500
+1027 1027 1027 12502500
+1028 1028 1028 12502500
+1029 1029 1029 12502500
+1030 1030 1030 12502500
+1031 1031 1031 12502500
+1032 1032 1032 12502500
+1033 1033 1033 12502500
+1034 1034 1034 12502500
+1035 1035 1035 12502500
+1036 1036 1036 12502500
+1037 1037 1037 12502500
+1038 1038 1038 12502500
+1039 1039 1039 12502500
+1040 1040 1040 12502500
+1041 1041 1041 12502500
+1042 1042 1042 12502500
+1043 1043 1043 12502500
+1044 1044 1044 12502500
+1045 1045 1045 12502500
+1046 1046 1046 12502500
+1047 1047 1047 12502500
+1048 1048 1048 12502500
+1049 1049 1049 12502500
+1050 1050 1050 12502500
+1051 1051 1051 12502500
+1052 1052 1052 12502500
+1053 1053 1053 12502500
+1054 1054 1054 12502500
+1055 1055 1055 12502500
+1056 1056 1056 12502500
+1057 1057 1057 12502500
+1058 1058 1058 12502500
+1059 1059 1059 12502500
+1060 1060 1060 12502500
+1061 1061 1061 12502500
+1062 1062 1062 12502500
+1063 1063 1063 12502500
+1064 1064 1064 12502500
+1065 1065 1065 12502500
+1066 1066 1066 12502500
+1067 1067 1067 12502500
+1068 1068 1068 12502500
+1069 1069 1069 12502500
+1070 1070 1070 12502500
+1071 1071 1071 12502500
+1072 1072 1072 12502500
+1073 1073 1073 12502500
+1074 1074 1074 12502500
+1075 1075 1075 12502500
+1076 1076 1076 12502500
+1077 1077 1077 12502500
+1078 1078 1078 12502500
+1079 1079 1079 12502500
+1080 1080 1080 12502500
+1081 1081 1081 12502500
+1082 1082 1082 12502500
+1083 1083 1083 12502500
+1084 1084 1084 12502500
+1085 1085 1085 12502500
+1086 1086 1086 12502500
+1087 1087 1087 12502500
+1088 1088 1088 12502500
+1089 1089 1089 12502500
+1090 1090 1090 12502500
+1091 1091 1091 12502500
+1092 1092 1092 12502500
+1093 1093 1093 12502500
+1094 1094 1094 12502500
+1095 1095 1095 12502500
+1096 1096 1096 12502500
+1097 1097 1097 12502500
+1098 1098 1098 12502500
+1099 1099 1099 12502500
+1100 1100 1100 12502500
+1101 1101 1101 12502500
+1102 1102 1102 12502500
+1103 1103 1103 12502500
+1104 1104 1104 12502500
+1105 1105 1105 12502500
+1106 1106 1106 12502500
+1107 1107 1107 12502500
+1108 1108 1108 12502500
+1109 1109 1109 12502500
+1110 1110 1110 12502500
+1111 1111 1111 12502500
+1112 1112 1112 12502500
+1113 1113 1113 12502500
+1114 1114 1114 12502500
+1115 1115 1115 12502500
+1116 1116 1116 12502500
+1117 1117 1117 12502500
+1118 1118 1118 12502500
+1119 1119 1119 12502500
+1120 1120 1120 12502500
+1121 1121 1121 12502500
+1122 1122 1122 12502500
+1123 1123 1123 12502500
+1124 1124 1124 12502500
+1125 1125 1125 12502500
+1126 1126 1126 12502500
+1127 1127 1127 12502500
+1128 1128 1128 12502500
+1129 1129 1129 12502500
+1130 1130 1130 12502500
+1131 1131 1131 12502500
+1132 1132 1132 12502500
+1133 1133 1133 12502500
+1134 1134 1134 12502500
+1135 1135 1135 12502500
+1136 1136 1136 12502500
+1137 1137 1137 12502500
+1138 1138 1138 12502500
+1139 1139 1139 12502500
+1140 1140 1140 12502500
+1141 1141 1141 12502500
+1142 1142 1142 12502500
+1143 1143 1143 12502500
+1144 1144 1144 12502500
+1145 1145 1145 12502500
+1146 1146 1146 12502500
+1147 1147 1147 12502500
+1148 1148 1148 12502500
+1149 1149 1149 12502500
+1150 1150 1150 12502500
+1151 1151 1151 12502500
+1152 1152 1152 12502500
+1153 1153 1153 12502500
+1154 1154 1154 12502500
+1155 1155 1155 12502500
+1156 1156 1156 12502500
+1157 1157 1157 12502500
+1158 1158 1158 12502500
+1159 1159 1159 12502500
+1160 1160 1160 12502500
+1161 1161 1161 12502500
+1162 1162 1162 12502500
+1163 1163 1163 12502500
+1164 1164 1164 12502500
+1165 1165 1165 12502500
+1166 1166 1166 12502500
+1167 1167 1167 12502500
+1168 1168 1168 12502500
+1169 1169 1169 12502500
+1170 1170 1170 12502500
+1171 1171 1171 12502500
+1172 1172 1172 12502500
+1173 1173 1173 12502500
+1174 1174 1174 12502500
+1175 1175 1175 12502500
+1176 1176 1176 12502500
+1177 1177 1177 12502500
+1178 1178 1178 12502500
+1179 1179 1179 12502500
+1180 1180 1180 12502500
+1181 1181 1181 12502500
+1182 1182 1182 12502500
+1183 1183 1183 12502500
+1184 1184 1184 12502500
+1185 1185 1185 12502500
+1186 1186 1186 12502500
+1187 1187 1187 12502500
+1188 1188 1188 12502500
+1189 1189 1189 12502500
+1190 1190 1190 12502500
+1191 1191 1191 12502500
+1192 1192 1192 12502500
+1193 1193 1193 12502500
+1194 1194 1194 12502500
+1195 1195 1195 12502500
+1196 1196 1196 12502500
+1197 1197 1197 12502500
+1198 1198 1198 12502500
+1199 1199 1199 12502500
+1200 1200 1200 12502500
+1201 1201 1201 12502500
+1202 1202 1202 12502500
+1203 1203 1203 12502500
+1204 1204 1204 12502500
+1205 1205 1205 12502500
+1206 1206 1206 12502500
+1207 1207 1207 12502500
+1208 1208 1208 12502500
+1209 1209 1209 12502500
+1210 1210 1210 12502500
+1211 1211 1211 12502500
+1212 1212 1212 12502500
+1213 1213 1213 12502500
+1214 1214 1214 12502500
+1215 1215 1215 12502500
+1216 1216 1216 12502500
+1217 1217 1217 12502500
+1218 1218 1218 12502500
+1219 1219 1219 12502500
+1220 1220 1220 12502500
+1221 1221 1221 12502500
+1222 1222 1222 12502500
+1223 1223 1223 12502500
+1224 1224 1224 12502500
+1225 1225 1225 12502500
+1226 1226 1226 12502500
+1227 1227 1227 12502500
+1228 1228 1228 12502500
+1229 1229 1229 12502500
+1230 1230 1230 12502500
+1231 1231 1231 12502500
+1232 1232 1232 12502500
+1233 1233 1233 12502500
+1234 1234 1234 12502500
+1235 1235 1235 12502500
+1236 1236 1236 12502500
+1237 1237 1237 12502500
+1238 1238 1238 12502500
+1239 1239 1239 12502500
+1240 1240 1240 12502500
+1241 1241 1241 12502500
+1242 1242 1242 12502500
+1243 1243 1243 12502500
+1244 1244 1244 12502500
+1245 1245 1245 12502500
+1246 1246 1246 12502500
+1247 1247 1247 12502500
+1248 1248 1248 12502500
+1249 1249 1249 12502500
+1250 1250 1250 12502500
+1251 1251 1251 12502500
+1252 1252 1252 12502500
+1253 1253 1253 12502500
+1254 1254 1254 12502500
+1255 1255 1255 12502500
+1256 1256 1256 12502500
+1257 1257 1257 12502500
+1258 1258 1258 12502500
+1259 1259 1259 12502500
+1260 1260 1260 12502500
+1261 1261 1261 12502500
+1262 1262 1262 12502500
+1263 1263 1263 12502500
+1264 1264 1264 12502500
+1265 1265 1265 12502500
+1266 1266 1266 12502500
+1267 1267 1267 12502500
+1268 1268 1268 12502500
+1269 1269 1269 12502500
+1270 1270 1270 12502500
+1271 1271 1271 12502500
+1272 1272 1272 12502500
+1273 1273 1273 12502500
+1274 1274 1274 12502500
+1275 1275 1275 12502500
+1276 1276 1276 12502500
+1277 1277 1277 12502500
+1278 1278 1278 12502500
+1279 1279 1279 12502500
+1280 1280 1280 12502500
+1281 1281 1281 12502500
+1282 1282 1282 12502500
+1283 1283 1283 12502500
+1284 1284 1284 12502500
+1285 1285 1285 12502500
+1286 1286 1286 12502500
+1287 1287 1287 12502500
+1288 1288 1288 12502500
+1289 1289 1289 12502500
+1290 1290 1290 12502500
+1291 1291 1291 12502500
+1292 1292 1292 12502500
+1293 1293 1293 12502500
+1294 1294 1294 12502500
+1295 1295 1295 12502500
+1296 1296 1296 12502500
+1297 1297 1297 12502500
+1298 1298 1298 12502500
+1299 1299 1299 12502500
+1300 1300 1300 12502500
+1301 1301 1301 12502500
+1302 1302 1302 12502500
+1303 1303 1303 12502500
+1304 1304 1304 12502500
+1305 1305 1305 12502500
+1306 1306 1306 12502500
+1307 1307 1307 12502500
+1308 1308 1308 12502500
+1309 1309 1309 12502500
+1310 1310 1310 12502500
+1311 1311 1311 12502500
+1312 1312 1312 12502500
+1313 1313 1313 12502500
+1314 1314 1314 12502500
+1315 1315 1315 12502500
+1316 1316 1316 12502500
+1317 1317 1317 12502500
+1318 1318 1318 12502500
+1319 1319 1319 12502500
+1320 1320 1320 12502500
+1321 1321 1321 12502500
+1322 1322 1322 12502500
+1323 1323 1323 12502500
+1324 1324 1324 12502500
+1325 1325 1325 12502500
+1326 1326 1326 12502500
+1327 1327 1327 12502500
+1328 1328 1328 12502500
+1329 1329 1329 12502500
+1330 1330 1330 12502500
+1331 1331 1331 12502500
+1332 1332 1332 12502500
+1333 1333 1333 12502500
+1334 1334 1334 12502500
+1335 1335 1335 12502500
+1336 1336 1336 12502500
+1337 1337 1337 12502500
+1338 1338 1338 12502500
+1339 1339 1339 12502500
+1340 1340 1340 12502500
+1341 1341 1341 12502500
+1342 1342 1342 12502500
+1343 1343 1343 12502500
+1344 1344 1344 12502500
+1345 1345 1345 12502500
+1346 1346 1346 12502500
+1347 1347 1347 12502500
+1348 1348 1348 12502500
+1349 1349 1349 12502500
+1350 1350 1350 12502500
+1351 1351 1351 12502500
+1352 1352 1352 12502500
+1353 1353 1353 12502500
+1354 1354 1354 12502500
+1355 1355 1355 12502500
+1356 1356 1356 12502500
+1357 1357 1357 12502500
+1358 1358 1358 12502500
+1359 1359 1359 12502500
+1360 1360 1360 12502500
+1361 1361 1361 12502500
+1362 1362 1362 12502500
+1363 1363 1363 12502500
+1364 1364 1364 12502500
+1365 1365 1365 12502500
+1366 1366 1366 12502500
+1367 1367 1367 12502500
+1368 1368 1368 12502500
+1369 1369 1369 12502500
+1370 1370 1370 12502500
+1371 1371 1371 12502500
+1372 1372 1372 12502500
+1373 1373 1373 12502500
+1374 1374 1374 12502500
+1375 1375 1375 12502500
+1376 1376 1376 12502500
+1377 1377 1377 12502500
+1378 1378 1378 12502500
+1379 1379 1379 12502500
+1380 1380 1380 12502500
+1381 1381 1381 12502500
+1382 1382 1382 12502500
+1383 1383 1383 12502500
+1384 1384 1384 12502500
+1385 1385 1385 12502500
+1386 1386 1386 12502500
+1387 1387 1387 12502500
+1388 1388 1388 12502500
+1389 1389 1389 12502500
+1390 1390 1390 12502500
+1391 1391 1391 12502500
+1392 1392 1392 12502500
+1393 1393 1393 12502500
+1394 1394 1394 12502500
+1395 1395 1395 12502500
+1396 1396 1396 12502500
+1397 1397 1397 12502500
+1398 1398 1398 12502500
+1399 1399 1399 12502500
+1400 1400 1400 12502500
+1401 1401 1401 12502500
+1402 1402 1402 12502500
+1403 1403 1403 12502500
+1404 1404 1404 12502500
+1405 1405 1405 12502500
+1406 1406 1406 12502500
+1407 1407 1407 12502500
+1408 1408 1408 12502500
+1409 1409 1409 12502500
+1410 1410 1410 12502500
+1411 1411 1411 12502500
+1412 1412 1412 12502500
+1413 1413 1413 12502500
+1414 1414 1414 12502500
+1415 1415 1415 12502500
+1416 1416 1416 12502500
+1417 1417 1417 12502500
+1418 1418 1418 12502500
+1419 1419 1419 12502500
+1420 1420 1420 12502500
+1421 1421 1421 12502500
+1422 1422 1422 12502500
+1423 1423 1423 12502500
+1424 1424 1424 12502500
+1425 1425 1425 12502500
+1426 1426 1426 12502500
+1427 1427 1427 12502500
+1428 1428 1428 12502500
+1429 1429 1429 12502500
+1430 1430 1430 12502500
+1431 1431 1431 12502500
+1432 1432 1432 12502500
+1433 1433 1433 12502500
+1434 1434 1434 12502500
+1435 1435 1435 12502500
+1436 1436 1436 12502500
+1437 1437 1437 12502500
+1438 1438 1438 12502500
+1439 1439 1439 12502500
+1440 1440 1440 12502500
+1441 1441 1441 12502500
+1442 1442 1442 12502500
+1443 1443 1443 12502500
+1444 1444 1444 12502500
+1445 1445 1445 12502500
+1446 1446 1446 12502500
+1447 1447 1447 12502500
+1448 1448 1448 12502500
+1449 1449 1449 12502500
+1450 1450 1450 12502500
+1451 1451 1451 12502500
+1452 1452 1452 12502500
+1453 1453 1453 12502500
+1454 1454 1454 12502500
+1455 1455 1455 12502500
+1456 1456 1456 12502500
+1457 1457 1457 12502500
+1458 1458 1458 12502500
+1459 1459 1459 12502500
+1460 1460 1460 12502500
+1461 1461 1461 12502500
+1462 1462 1462 12502500
+1463 1463 1463 12502500
+1464 1464 1464 12502500
+1465 1465 1465 12502500
+1466 1466 1466 12502500
+1467 1467 1467 12502500
+1468 1468 1468 12502500
+1469 1469 1469 12502500
+1470 1470 1470 12502500
+1471 1471 1471 12502500
+1472 1472 1472 12502500
+1473 1473 1473 12502500
+1474 1474 1474 12502500
+1475 1475 1475 12502500
+1476 1476 1476 12502500
+1477 1477 1477 12502500
+1478 1478 1478 12502500
+1479 1479 1479 12502500
+1480 1480 1480 12502500
+1481 1481 1481 12502500
+1482 1482 1482 12502500
+1483 1483 1483 12502500
+1484 1484 1484 12502500
+1485 1485 1485 12502500
+1486 1486 1486 12502500
+1487 1487 1487 12502500
+1488 1488 1488 12502500
+1489 1489 1489 12502500
+1490 1490 1490 12502500
+1491 1491 1491 12502500
+1492 1492 1492 12502500
+1493 1493 1493 12502500
+1494 1494 1494 12502500
+1495 1495 1495 12502500
+1496 1496 1496 12502500
+1497 1497 1497 12502500
+1498 1498 1498 12502500
+1499 1499 1499 12502500
+1500 1500 1500 12502500
+1501 1501 1501 12502500
+1502 1502 1502 12502500
+1503 1503 1503 12502500
+1504 1504 1504 12502500
+1505 1505 1505 12502500
+1506 1506 1506 12502500
+1507 1507 1507 12502500
+1508 1508 1508 12502500
+1509 1509 1509 12502500
+1510 1510 1510 12502500
+1511 1511 1511 12502500
+1512 1512 1512 12502500
+1513 1513 1513 12502500
+1514 1514 1514 12502500
+1515 1515 1515 12502500
+1516 1516 1516 12502500
+1517 1517 1517 12502500
+1518 1518 1518 12502500
+1519 1519 1519 12502500
+1520 1520 1520 12502500
+1521 1521 1521 12502500
+1522 1522 1522 12502500
+1523 1523 1523 12502500
+1524 1524 1524 12502500
+1525 1525 1525 12502500
+1526 1526 1526 12502500
+1527 1527 1527 12502500
+1528 1528 1528 12502500
+1529 1529 1529 12502500
+1530 1530 1530 12502500
+1531 1531 1531 12502500
+1532 1532 1532 12502500
+1533 1533 1533 12502500
+1534 1534 1534 12502500
+1535 1535 1535 12502500
+1536 1536 1536 12502500
+1537 1537 1537 12502500
+1538 1538 1538 12502500
+1539 1539 1539 12502500
+1540 1540 1540 12502500
+1541 1541 1541 12502500
+1542 1542 1542 12502500
+1543 1543 1543 12502500
+1544 1544 1544 12502500
+1545 1545 1545 12502500
+1546 1546 1546 12502500
+1547 1547 1547 12502500
+1548 1548 1548 12502500
+1549 1549 1549 12502500
+1550 1550 1550 12502500
+1551 1551 1551 12502500
+1552 1552 1552 12502500
+1553 1553 1553 12502500
+1554 1554 1554 12502500
+1555 1555 1555 12502500
+1556 1556 1556 12502500
+1557 1557 1557 12502500
+1558 1558 1558 12502500
+1559 1559 1559 12502500
+1560 1560 1560 12502500
+1561 1561 1561 12502500
+1562 1562 1562 12502500
+1563 1563 1563 12502500
+1564 1564 1564 12502500
+1565 1565 1565 12502500
+1566 1566 1566 12502500
+1567 1567 1567 12502500
+1568 1568 1568 12502500
+1569 1569 1569 12502500
+1570 1570 1570 12502500
+1571 1571 1571 12502500
+1572 1572 1572 12502500
+1573 1573 1573 12502500
+1574 1574 1574 12502500
+1575 1575 1575 12502500
+1576 1576 1576 12502500
+1577 1577 1577 12502500
+1578 1578 1578 12502500
+1579 1579 1579 12502500
+1580 1580 1580 12502500
+1581 1581 1581 12502500
+1582 1582 1582 12502500
+1583 1583 1583 12502500
+1584 1584 1584 12502500
+1585 1585 1585 12502500
+1586 1586 1586 12502500
+1587 1587 1587 12502500
+1588 1588 1588 12502500
+1589 1589 1589 12502500
+1590 1590 1590 12502500
+1591 1591 1591 12502500
+1592 1592 1592 12502500
+1593 1593 1593 12502500
+1594 1594 1594 12502500
+1595 1595 1595 12502500
+1596 1596 1596 12502500
+1597 1597 1597 12502500
+1598 1598 1598 12502500
+1599 1599 1599 12502500
+1600 1600 1600 12502500
+1601 1601 1601 12502500
+1602 1602 1602 12502500
+1603 1603 1603 12502500
+1604 1604 1604 12502500
+1605 1605 1605 12502500
+1606 1606 1606 12502500
+1607 1607 1607 12502500
+1608 1608 1608 12502500
+1609 1609 1609 12502500
+1610 1610 1610 12502500
+1611 1611 1611 12502500
+1612 1612 1612 12502500
+1613 1613 1613 12502500
+1614 1614 1614 12502500
+1615 1615 1615 12502500
+1616 1616 1616 12502500
+1617 1617 1617 12502500
+1618 1618 1618 12502500
+1619 1619 1619 12502500
+1620 1620 1620 12502500
+1621 1621 1621 12502500
+1622 1622 1622 12502500
+1623 1623 1623 12502500
+1624 1624 1624 12502500
+1625 1625 1625 12502500
+1626 1626 1626 12502500
+1627 1627 1627 12502500
+1628 1628 1628 12502500
+1629 1629 1629 12502500
+1630 1630 1630 12502500
+1631 1631 1631 12502500
+1632 1632 1632 12502500
+1633 1633 1633 12502500
+1634 1634 1634 12502500
+1635 1635 1635 12502500
+1636 1636 1636 12502500
+1637 1637 1637 12502500
+1638 1638 1638 12502500
+1639 1639 1639 12502500
+1640 1640 1640 12502500
+1641 1641 1641 12502500
+1642 1642 1642 12502500
+1643 1643 1643 12502500
+1644 1644 1644 12502500
+1645 1645 1645 12502500
+1646 1646 1646 12502500
+1647 1647 1647 12502500
+1648 1648 1648 12502500
+1649 1649 1649 12502500
+1650 1650 1650 12502500
+1651 1651 1651 12502500
+1652 1652 1652 12502500
+1653 1653 1653 12502500
+1654 1654 1654 12502500
+1655 1655 1655 12502500
+1656 1656 1656 12502500
+1657 1657 1657 12502500
+1658 1658 1658 12502500
+1659 1659 1659 12502500
+1660 1660 1660 12502500
+1661 1661 1661 12502500
+1662 1662 1662 12502500
+1663 1663 1663 12502500
+1664 1664 1664 12502500
+1665 1665 1665 12502500
+1666 1666 1666 12502500
+1667 1667 1667 12502500
+1668 1668 1668 12502500
+1669 1669 1669 12502500
+1670 1670 1670 12502500
+1671 1671 1671 12502500
+1672 1672 1672 12502500
+1673 1673 1673 12502500
+1674 1674 1674 12502500
+1675 1675 1675 12502500
+1676 1676 1676 12502500
+1677 1677 1677 12502500
+1678 1678 1678 12502500
+1679 1679 1679 12502500
+1680 1680 1680 12502500
+1681 1681 1681 12502500
+1682 1682 1682 12502500
+1683 1683 1683 12502500
+1684 1684 1684 12502500
+1685 1685 1685 12502500
+1686 1686 1686 12502500
+1687 1687 1687 12502500
+1688 1688 1688 12502500
+1689 1689 1689 12502500
+1690 1690 1690 12502500
+1691 1691 1691 12502500
+1692 1692 1692 12502500
+1693 1693 1693 12502500
+1694 1694 1694 12502500
+1695 1695 1695 12502500
+1696 1696 1696 12502500
+1697 1697 1697 12502500
+1698 1698 1698 12502500
+1699 1699 1699 12502500
+1700 1700 1700 12502500
+1701 1701 1701 12502500
+1702 1702 1702 12502500
+1703 1703 1703 12502500
+1704 1704 1704 12502500
+1705 1705 1705 12502500
+1706 1706 1706 12502500
+1707 1707 1707 12502500
+1708 1708 1708 12502500
+1709 1709 1709 12502500
+1710 1710 1710 12502500
+1711 1711 1711 12502500
+1712 1712 1712 12502500
+1713 1713 1713 12502500
+1714 1714 1714 12502500
+1715 1715 1715 12502500
+1716 1716 1716 12502500
+1717 1717 1717 12502500
+1718 1718 1718 12502500
+1719 1719 1719 12502500
+1720 1720 1720 12502500
+1721 1721 1721 12502500
+1722 1722 1722 12502500
+1723 1723 1723 12502500
+1724 1724 1724 12502500
+1725 1725 1725 12502500
+1726 1726 1726 12502500
+1727 1727 1727 12502500
+1728 1728 1728 12502500
+1729 1729 1729 12502500
+1730 1730 1730 12502500
+1731 1731 1731 12502500
+1732 1732 1732 12502500
+1733 1733 1733 12502500
+1734 1734 1734 12502500
+1735 1735 1735 12502500
+1736 1736 1736 12502500
+1737 1737 1737 12502500
+1738 1738 1738 12502500
+1739 1739 1739 12502500
+1740 1740 1740 12502500
+1741 1741 1741 12502500
+1742 1742 1742 12502500
+1743 1743 1743 12502500
+1744 1744 1744 12502500
+1745 1745 1745 12502500
+1746 1746 1746 12502500
+1747 1747 1747 12502500
+1748 1748 1748 12502500
+1749 1749 1749 12502500
+1750 1750 1750 12502500
+1751 1751 1751 12502500
+1752 1752 1752 12502500
+1753 1753 1753 12502500
+1754 1754 1754 12502500
+1755 1755 1755 12502500
+1756 1756 1756 12502500
+1757 1757 1757 12502500
+1758 1758 1758 12502500
+1759 1759 1759 12502500
+1760 1760 1760 12502500
+1761 1761 1761 12502500
+1762 1762 1762 12502500
+1763 1763 1763 12502500
+1764 1764 1764 12502500
+1765 1765 1765 12502500
+1766 1766 1766 12502500
+1767 1767 1767 12502500
+1768 1768 1768 12502500
+1769 1769 1769 12502500
+1770 1770 1770 12502500
+1771 1771 1771 12502500
+1772 1772 1772 12502500
+1773 1773 1773 12502500
+1774 1774 1774 12502500
+1775 1775 1775 12502500
+1776 1776 1776 12502500
+1777 1777 1777 12502500
+1778 1778 1778 12502500
+1779 1779 1779 12502500
+1780 1780 1780 12502500
+1781 1781 1781 12502500
+1782 1782 1782 12502500
+1783 1783 1783 12502500
+1784 1784 1784 12502500
+1785 1785 1785 12502500
+1786 1786 1786 12502500
+1787 1787 1787 12502500
+1788 1788 1788 12502500
+1789 1789 1789 12502500
+1790 1790 1790 12502500
+1791 1791 1791 12502500
+1792 1792 1792 12502500
+1793 1793 1793 12502500
+1794 1794 1794 12502500
+1795 1795 1795 12502500
+1796 1796 1796 12502500
+1797 1797 1797 12502500
+1798 1798 1798 12502500
+1799 1799 1799 12502500
+1800 1800 1800 12502500
+1801 1801 1801 12502500
+1802 1802 1802 12502500
+1803 1803 1803 12502500
+1804 1804 1804 12502500
+1805 1805 1805 12502500
+1806 1806 1806 12502500
+1807 1807 1807 12502500
+1808 1808 1808 12502500
+1809 1809 1809 12502500
+1810 1810 1810 12502500
+1811 1811 1811 12502500
+1812 1812 1812 12502500
+1813 1813 1813 12502500
+1814 1814 1814 12502500
+1815 1815 1815 12502500
+1816 1816 1816 12502500
+1817 1817 1817 12502500
+1818 1818 1818 12502500
+1819 1819 1819 12502500
+1820 1820 1820 12502500
+1821 1821 1821 12502500
+1822 1822 1822 12502500
+1823 1823 1823 12502500
+1824 1824 1824 12502500
+1825 1825 1825 12502500
+1826 1826 1826 12502500
+1827 1827 1827 12502500
+1828 1828 1828 12502500
+1829 1829 1829 12502500
+1830 1830 1830 12502500
+1831 1831 1831 12502500
+1832 1832 1832 12502500
+1833 1833 1833 12502500
+1834 1834 1834 12502500
+1835 1835 1835 12502500
+1836 1836 1836 12502500
+1837 1837 1837 12502500
+1838 1838 1838 12502500
+1839 1839 1839 12502500
+1840 1840 1840 12502500
+1841 1841 1841 12502500
+1842 1842 1842 12502500
+1843 1843 1843 12502500
+1844 1844 1844 12502500
+1845 1845 1845 12502500
+1846 1846 1846 12502500
+1847 1847 1847 12502500
+1848 1848 1848 12502500
+1849 1849 1849 12502500
+1850 1850 1850 12502500
+1851 1851 1851 12502500
+1852 1852 1852 12502500
+1853 1853 1853 12502500
+1854 1854 1854 12502500
+1855 1855 1855 12502500
+1856 1856 1856 12502500
+1857 1857 1857 12502500
+1858 1858 1858 12502500
+1859 1859 1859 12502500
+1860 1860 1860 12502500
+1861 1861 1861 12502500
+1862 1862 1862 12502500
+1863 1863 1863 12502500
+1864 1864 1864 12502500
+1865 1865 1865 12502500
+1866 1866 1866 12502500
+1867 1867 1867 12502500
+1868 1868 1868 12502500
+1869 1869 1869 12502500
+1870 1870 1870 12502500
+1871 1871 1871 12502500
+1872 1872 1872 12502500
+1873 1873 1873 12502500
+1874 1874 1874 12502500
+1875 1875 1875 12502500
+1876 1876 1876 12502500
+1877 1877 1877 12502500
+1878 1878 1878 12502500
+1879 1879 1879 12502500
+1880 1880 1880 12502500
+1881 1881 1881 12502500
+1882 1882 1882 12502500
+1883 1883 1883 12502500
+1884 1884 1884 12502500
+1885 1885 1885 12502500
+1886 1886 1886 12502500
+1887 1887 1887 12502500
+1888 1888 1888 12502500
+1889 1889 1889 12502500
+1890 1890 1890 12502500
+1891 1891 1891 12502500
+1892 1892 1892 12502500
+1893 1893 1893 12502500
+1894 1894 1894 12502500
+1895 1895 1895 12502500
+1896 1896 1896 12502500
+1897 1897 1897 12502500
+1898 1898 1898 12502500
+1899 1899 1899 12502500
+1900 1900 1900 12502500
+1901 1901 1901 12502500
+1902 1902 1902 12502500
+1903 1903 1903 12502500
+1904 1904 1904 12502500
+1905 1905 1905 12502500
+1906 1906 1906 12502500
+1907 1907 1907 12502500
+1908 1908 1908 12502500
+1909 1909 1909 12502500
+1910 1910 1910 12502500
+1911 1911 1911 12502500
+1912 1912 1912 12502500
+1913 1913 1913 12502500
+1914 1914 1914 12502500
+1915 1915 1915 12502500
+1916 1916 1916 12502500
+1917 1917 1917 12502500
+1918 1918 1918 12502500
+1919 1919 1919 12502500
+1920 1920 1920 12502500
+1921 1921 1921 12502500
+1922 1922 1922 12502500
+1923 1923 1923 12502500
+1924 1924 1924 12502500
+1925 1925 1925 12502500
+1926 1926 1926 12502500
+1927 1927 1927 12502500
+1928 1928 1928 12502500
+1929 1929 1929 12502500
+1930 1930 1930 12502500
+1931 1931 1931 12502500
+1932 1932 1932 12502500
+1933 1933 1933 12502500
+1934 1934 1934 12502500
+1935 1935 1935 12502500
+1936 1936 1936 12502500
+1937 1937 1937 12502500
+1938 1938 1938 12502500
+1939 1939 1939 12502500
+1940 1940 1940 12502500
+1941 1941 1941 12502500
+1942 1942 1942 12502500
+1943 1943 1943 12502500
+1944 1944 1944 12502500
+1945 1945 1945 12502500
+1946 1946 1946 12502500
+1947 1947 1947 12502500
+1948 1948 1948 12502500
+1949 1949 1949 12502500
+1950 1950 1950 12502500
+1951 1951 1951 12502500
+1952 1952 1952 12502500
+1953 1953 1953 12502500
+1954 1954 1954 12502500
+1955 1955 1955 12502500
+1956 1956 1956 12502500
+1957 1957 1957 12502500
+1958 1958 1958 12502500
+1959 1959 1959 12502500
+1960 1960 1960 12502500
+1961 1961 1961 12502500
+1962 1962 1962 12502500
+1963 1963 1963 12502500
+1964 1964 1964 12502500
+1965 1965 1965 12502500
+1966 1966 1966 12502500
+1967 1967 1967 12502500
+1968 1968 1968 12502500
+1969 1969 1969 12502500
+1970 1970 1970 12502500
+1971 1971 1971 12502500
+1972 1972 1972 12502500
+1973 1973 1973 12502500
+1974 1974 1974 12502500
+1975 1975 1975 12502500
+1976 1976 1976 12502500
+1977 1977 1977 12502500
+1978 1978 1978 12502500
+1979 1979 1979 12502500
+1980 1980 1980 12502500
+1981 1981 1981 12502500
+1982 1982 1982 12502500
+1983 1983 1983 12502500
+1984 1984 1984 12502500
+1985 1985 1985 12502500
+1986 1986 1986 12502500
+1987 1987 1987 12502500
+1988 1988 1988 12502500
+1989 1989 1989 12502500
+1990 1990 1990 12502500
+1991 1991 1991 12502500
+1992 1992 1992 12502500
+1993 1993 1993 12502500
+1994 1994 1994 12502500
+1995 1995 1995 12502500
+1996 1996 1996 12502500
+1997 1997 1997 12502500
+1998 1998 1998 12502500
+1999 1999 1999 12502500
+2000 2000 2000 12502500
+2001 2001 2001 12502500
+2002 2002 2002 12502500
+2003 2003 2003 12502500
+2004 2004 2004 12502500
+2005 2005 2005 12502500
+2006 2006 2006 12502500
+2007 2007 2007 12502500
+2008 2008 2008 12502500
+2009 2009 2009 12502500
+2010 2010 2010 12502500
+2011 2011 2011 12502500
+2012 2012 2012 12502500
+2013 2013 2013 12502500
+2014 2014 2014 12502500
+2015 2015 2015 12502500
+2016 2016 2016 12502500
+2017 2017 2017 12502500
+2018 2018 2018 12502500
+2019 2019 2019 12502500
+2020 2020 2020 12502500
+2021 2021 2021 12502500
+2022 2022 2022 12502500
+2023 2023 2023 12502500
+2024 2024 2024 12502500
+2025 2025 2025 12502500
+2026 2026 2026 12502500
+2027 2027 2027 12502500
+2028 2028 2028 12502500
+2029 2029 2029 12502500
+2030 2030 2030 12502500
+2031 2031 2031 12502500
+2032 2032 2032 12502500
+2033 2033 2033 12502500
+2034 2034 2034 12502500
+2035 2035 2035 12502500
+2036 2036 2036 12502500
+2037 2037 2037 12502500
+2038 2038 2038 12502500
+2039 2039 2039 12502500
+2040 2040 2040 12502500
+2041 2041 2041 12502500
+2042 2042 2042 12502500
+2043 2043 2043 12502500
+2044 2044 2044 12502500
+2045 2045 2045 12502500
+2046 2046 2046 12502500
+2047 2047 2047 12502500
+2048 2048 2048 12502500
+2049 2049 2049 12502500
+2050 2050 2050 12502500
+2051 2051 2051 12502500
+2052 2052 2052 12502500
+2053 2053 2053 12502500
+2054 2054 2054 12502500
+2055 2055 2055 12502500
+2056 2056 2056 12502500
+2057 2057 2057 12502500
+2058 2058 2058 12502500
+2059 2059 2059 12502500
+2060 2060 2060 12502500
+2061 2061 2061 12502500
+2062 2062 2062 12502500
+2063 2063 2063 12502500
+2064 2064 2064 12502500
+2065 2065 2065 12502500
+2066 2066 2066 12502500
+2067 2067 2067 12502500
+2068 2068 2068 12502500
+2069 2069 2069 12502500
+2070 2070 2070 12502500
+2071 2071 2071 12502500
+2072 2072 2072 12502500
+2073 2073 2073 12502500
+2074 2074 2074 12502500
+2075 2075 2075 12502500
+2076 2076 2076 12502500
+2077 2077 2077 12502500
+2078 2078 2078 12502500
+2079 2079 2079 12502500
+2080 2080 2080 12502500
+2081 2081 2081 12502500
+2082 2082 2082 12502500
+2083 2083 2083 12502500
+2084 2084 2084 12502500
+2085 2085 2085 12502500
+2086 2086 2086 12502500
+2087 2087 2087 12502500
+2088 2088 2088 12502500
+2089 2089 2089 12502500
+2090 2090 2090 12502500
+2091 2091 2091 12502500
+2092 2092 2092 12502500
+2093 2093 2093 12502500
+2094 2094 2094 12502500
+2095 2095 2095 12502500
+2096 2096 2096 12502500
+2097 2097 2097 12502500
+2098 2098 2098 12502500
+2099 2099 2099 12502500
+2100 2100 2100 12502500
+2101 2101 2101 12502500
+2102 2102 2102 12502500
+2103 2103 2103 12502500
+2104 2104 2104 12502500
+2105 2105 2105 12502500
+2106 2106 2106 12502500
+2107 2107 2107 12502500
+2108 2108 2108 12502500
+2109 2109 2109 12502500
+2110 2110 2110 12502500
+2111 2111 2111 12502500
+2112 2112 2112 12502500
+2113 2113 2113 12502500
+2114 2114 2114 12502500
+2115 2115 2115 12502500
+2116 2116 2116 12502500
+2117 2117 2117 12502500
+2118 2118 2118 12502500
+2119 2119 2119 12502500
+2120 2120 2120 12502500
+2121 2121 2121 12502500
+2122 2122 2122 12502500
+2123 2123 2123 12502500
+2124 2124 2124 12502500
+2125 2125 2125 12502500
+2126 2126 2126 12502500
+2127 2127 2127 12502500
+2128 2128 2128 12502500
+2129 2129 2129 12502500
+2130 2130 2130 12502500
+2131 2131 2131 12502500
+2132 2132 2132 12502500
+2133 2133 2133 12502500
+2134 2134 2134 12502500
+2135 2135 2135 12502500
+2136 2136 2136 12502500
+2137 2137 2137 12502500
+2138 2138 2138 12502500
+2139 2139 2139 12502500
+2140 2140 2140 12502500
+2141 2141 2141 12502500
+2142 2142 2142 12502500
+2143 2143 2143 12502500
+2144 2144 2144 12502500
+2145 2145 2145 12502500
+2146 2146 2146 12502500
+2147 2147 2147 12502500
+2148 2148 2148 12502500
+2149 2149 2149 12502500
+2150 2150 2150 12502500
+2151 2151 2151 12502500
+2152 2152 2152 12502500
+2153 2153 2153 12502500
+2154 2154 2154 12502500
+2155 2155 2155 12502500
+2156 2156 2156 12502500
+2157 2157 2157 12502500
+2158 2158 2158 12502500
+2159 2159 2159 12502500
+2160 2160 2160 12502500
+2161 2161 2161 12502500
+2162 2162 2162 12502500
+2163 2163 2163 12502500
+2164 2164 2164 12502500
+2165 2165 2165 12502500
+2166 2166 2166 12502500
+2167 2167 2167 12502500
+2168 2168 2168 12502500
+2169 2169 2169 12502500
+2170 2170 2170 12502500
+2171 2171 2171 12502500
+2172 2172 2172 12502500
+2173 2173 2173 12502500
+2174 2174 2174 12502500
+2175 2175 2175 12502500
+2176 2176 2176 12502500
+2177 2177 2177 12502500
+2178 2178 2178 12502500
+2179 2179 2179 12502500
+2180 2180 2180 12502500
+2181 2181 2181 12502500
+2182 2182 2182 12502500
+2183 2183 2183 12502500
+2184 2184 2184 12502500
+2185 2185 2185 12502500
+2186 2186 2186 12502500
+2187 2187 2187 12502500
+2188 2188 2188 12502500
+2189 2189 2189 12502500
+2190 2190 2190 12502500
+2191 2191 2191 12502500
+2192 2192 2192 12502500
+2193 2193 2193 12502500
+2194 2194 2194 12502500
+2195 2195 2195 12502500
+2196 2196 2196 12502500
+2197 2197 2197 12502500
+2198 2198 2198 12502500
+2199 2199 2199 12502500
+2200 2200 2200 12502500
+2201 2201 2201 12502500
+2202 2202 2202 12502500
+2203 2203 2203 12502500
+2204 2204 2204 12502500
+2205 2205 2205 12502500
+2206 2206 2206 12502500
+2207 2207 2207 12502500
+2208 2208 2208 12502500
+2209 2209 2209 12502500
+2210 2210 2210 12502500
+2211 2211 2211 12502500
+2212 2212 2212 12502500
+2213 2213 2213 12502500
+2214 2214 2214 12502500
+2215 2215 2215 12502500
+2216 2216 2216 12502500
+2217 2217 2217 12502500
+2218 2218 2218 12502500
+2219 2219 2219 12502500
+2220 2220 2220 12502500
+2221 2221 2221 12502500
+2222 2222 2222 12502500
+2223 2223 2223 12502500
+2224 2224 2224 12502500
+2225 2225 2225 12502500
+2226 2226 2226 12502500
+2227 2227 2227 12502500
+2228 2228 2228 12502500
+2229 2229 2229 12502500
+2230 2230 2230 12502500
+2231 2231 2231 12502500
+2232 2232 2232 12502500
+2233 2233 2233 12502500
+2234 2234 2234 12502500
+2235 2235 2235 12502500
+2236 2236 2236 12502500
+2237 2237 2237 12502500
+2238 2238 2238 12502500
+2239 2239 2239 12502500
+2240 2240 2240 12502500
+2241 2241 2241 12502500
+2242 2242 2242 12502500
+2243 2243 2243 12502500
+2244 2244 2244 12502500
+2245 2245 2245 12502500
+2246 2246 2246 12502500
+2247 2247 2247 12502500
+2248 2248 2248 12502500
+2249 2249 2249 12502500
+2250 2250 2250 12502500
+2251 2251 2251 12502500
+2252 2252 2252 12502500
+2253 2253 2253 12502500
+2254 2254 2254 12502500
+2255 2255 2255 12502500
+2256 2256 2256 12502500
+2257 2257 2257 12502500
+2258 2258 2258 12502500
+2259 2259 2259 12502500
+2260 2260 2260 12502500
+2261 2261 2261 12502500
+2262 2262 2262 12502500
+2263 2263 2263 12502500
+2264 2264 2264 12502500
+2265 2265 2265 12502500
+2266 2266 2266 12502500
+2267 2267 2267 12502500
+2268 2268 2268 12502500
+2269 2269 2269 12502500
+2270 2270 2270 12502500
+2271 2271 2271 12502500
+2272 2272 2272 12502500
+2273 2273 2273 12502500
+2274 2274 2274 12502500
+2275 2275 2275 12502500
+2276 2276 2276 12502500
+2277 2277 2277 12502500
+2278 2278 2278 12502500
+2279 2279 2279 12502500
+2280 2280 2280 12502500
+2281 2281 2281 12502500
+2282 2282 2282 12502500
+2283 2283 2283 12502500
+2284 2284 2284 12502500
+2285 2285 2285 12502500
+2286 2286 2286 12502500
+2287 2287 2287 12502500
+2288 2288 2288 12502500
+2289 2289 2289 12502500
+2290 2290 2290 12502500
+2291 2291 2291 12502500
+2292 2292 2292 12502500
+2293 2293 2293 12502500
+2294 2294 2294 12502500
+2295 2295 2295 12502500
+2296 2296 2296 12502500
+2297 2297 2297 12502500
+2298 2298 2298 12502500
+2299 2299 2299 12502500
+2300 2300 2300 12502500
+2301 2301 2301 12502500
+2302 2302 2302 12502500
+2303 2303 2303 12502500
+2304 2304 2304 12502500
+2305 2305 2305 12502500
+2306 2306 2306 12502500
+2307 2307 2307 12502500
+2308 2308 2308 12502500
+2309 2309 2309 12502500
+2310 2310 2310 12502500
+2311 2311 2311 12502500
+2312 2312 2312 12502500
+2313 2313 2313 12502500
+2314 2314 2314 12502500
+2315 2315 2315 12502500
+2316 2316 2316 12502500
+2317 2317 2317 12502500
+2318 2318 2318 12502500
+2319 2319 2319 12502500
+2320 2320 2320 12502500
+2321 2321 2321 12502500
+2322 2322 2322 12502500
+2323 2323 2323 12502500
+2324 2324 2324 12502500
+2325 2325 2325 12502500
+2326 2326 2326 12502500
+2327 2327 2327 12502500
+2328 2328 2328 12502500
+2329 2329 2329 12502500
+2330 2330 2330 12502500
+2331 2331 2331 12502500
+2332 2332 2332 12502500
+2333 2333 2333 12502500
+2334 2334 2334 12502500
+2335 2335 2335 12502500
+2336 2336 2336 12502500
+2337 2337 2337 12502500
+2338 2338 2338 12502500
+2339 2339 2339 12502500
+2340 2340 2340 12502500
+2341 2341 2341 12502500
+2342 2342 2342 12502500
+2343 2343 2343 12502500
+2344 2344 2344 12502500
+2345 2345 2345 12502500
+2346 2346 2346 12502500
+2347 2347 2347 12502500
+2348 2348 2348 12502500
+2349 2349 2349 12502500
+2350 2350 2350 12502500
+2351 2351 2351 12502500
+2352 2352 2352 12502500
+2353 2353 2353 12502500
+2354 2354 2354 12502500
+2355 2355 2355 12502500
+2356 2356 2356 12502500
+2357 2357 2357 12502500
+2358 2358 2358 12502500
+2359 2359 2359 12502500
+2360 2360 2360 12502500
+2361 2361 2361 12502500
+2362 2362 2362 12502500
+2363 2363 2363 12502500
+2364 2364 2364 12502500
+2365 2365 2365 12502500
+2366 2366 2366 12502500
+2367 2367 2367 12502500
+2368 2368 2368 12502500
+2369 2369 2369 12502500
+2370 2370 2370 12502500
+2371 2371 2371 12502500
+2372 2372 2372 12502500
+2373 2373 2373 12502500
+2374 2374 2374 12502500
+2375 2375 2375 12502500
+2376 2376 2376 12502500
+2377 2377 2377 12502500
+2378 2378 2378 12502500
+2379 2379 2379 12502500
+2380 2380 2380 12502500
+2381 2381 2381 12502500
+2382 2382 2382 12502500
+2383 2383 2383 12502500
+2384 2384 2384 12502500
+2385 2385 2385 12502500
+2386 2386 2386 12502500
+2387 2387 2387 12502500
+2388 2388 2388 12502500
+2389 2389 2389 12502500
+2390 2390 2390 12502500
+2391 2391 2391 12502500
+2392 2392 2392 12502500
+2393 2393 2393 12502500
+2394 2394 2394 12502500
+2395 2395 2395 12502500
+2396 2396 2396 12502500
+2397 2397 2397 12502500
+2398 2398 2398 12502500
+2399 2399 2399 12502500
+2400 2400 2400 12502500
+2401 2401 2401 12502500
+2402 2402 2402 12502500
+2403 2403 2403 12502500
+2404 2404 2404 12502500
+2405 2405 2405 12502500
+2406 2406 2406 12502500
+2407 2407 2407 12502500
+2408 2408 2408 12502500
+2409 2409 2409 12502500
+2410 2410 2410 12502500
+2411 2411 2411 12502500
+2412 2412 2412 12502500
+2413 2413 2413 12502500
+2414 2414 2414 12502500
+2415 2415 2415 12502500
+2416 2416 2416 12502500
+2417 2417 2417 12502500
+2418 2418 2418 12502500
+2419 2419 2419 12502500
+2420 2420 2420 12502500
+2421 2421 2421 12502500
+2422 2422 2422 12502500
+2423 2423 2423 12502500
+2424 2424 2424 12502500
+2425 2425 2425 12502500
+2426 2426 2426 12502500
+2427 2427 2427 12502500
+2428 2428 2428 12502500
+2429 2429 2429 12502500
+2430 2430 2430 12502500
+2431 2431 2431 12502500
+2432 2432 2432 12502500
+2433 2433 2433 12502500
+2434 2434 2434 12502500
+2435 2435 2435 12502500
+2436 2436 2436 12502500
+2437 2437 2437 12502500
+2438 2438 2438 12502500
+2439 2439 2439 12502500
+2440 2440 2440 12502500
+2441 2441 2441 12502500
+2442 2442 2442 12502500
+2443 2443 2443 12502500
+2444 2444 2444 12502500
+2445 2445 2445 12502500
+2446 2446 2446 12502500
+2447 2447 2447 12502500
+2448 2448 2448 12502500
+2449 2449 2449 12502500
+2450 2450 2450 12502500
+2451 2451 2451 12502500
+2452 2452 2452 12502500
+2453 2453 2453 12502500
+2454 2454 2454 12502500
+2455 2455 2455 12502500
+2456 2456 2456 12502500
+2457 2457 2457 12502500
+2458 2458 2458 12502500
+2459 2459 2459 12502500
+2460 2460 2460 12502500
+2461 2461 2461 12502500
+2462 2462 2462 12502500
+2463 2463 2463 12502500
+2464 2464 2464 12502500
+2465 2465 2465 12502500
+2466 2466 2466 12502500
+2467 2467 2467 12502500
+2468 2468 2468 12502500
+2469 2469 2469 12502500
+2470 2470 2470 12502500
+2471 2471 2471 12502500
+2472 2472 2472 12502500
+2473 2473 2473 12502500
+2474 2474 2474 12502500
+2475 2475 2475 12502500
+2476 2476 2476 12502500
+2477 2477 2477 12502500
+2478 2478 2478 12502500
+2479 2479 2479 12502500
+2480 2480 2480 12502500
+2481 2481 2481 12502500
+2482 2482 2482 12502500
+2483 2483 2483 12502500
+2484 2484 2484 12502500
+2485 2485 2485 12502500
+2486 2486 2486 12502500
+2487 2487 2487 12502500
+2488 2488 2488 12502500
+2489 2489 2489 12502500
+2490 2490 2490 12502500
+2491 2491 2491 12502500
+2492 2492 2492 12502500
+2493 2493 2493 12502500
+2494 2494 2494 12502500
+2495 2495 2495 12502500
+2496 2496 2496 12502500
+2497 2497 2497 12502500
+2498 2498 2498 12502500
+2499 2499 2499 12502500
+2500 2500 2500 12502500
+2501 2501 2501 12502500
+2502 2502 2502 12502500
+2503 2503 2503 12502500
+2504 2504 2504 12502500
+2505 2505 2505 12502500
+2506 2506 2506 12502500
+2507 2507 2507 12502500
+2508 2508 2508 12502500
+2509 2509 2509 12502500
+2510 2510 2510 12502500
+2511 2511 2511 12502500
+2512 2512 2512 12502500
+2513 2513 2513 12502500
+2514 2514 2514 12502500
+2515 2515 2515 12502500
+2516 2516 2516 12502500
+2517 2517 2517 12502500
+2518 2518 2518 12502500
+2519 2519 2519 12502500
+2520 2520 2520 12502500
+2521 2521 2521 12502500
+2522 2522 2522 12502500
+2523 2523 2523 12502500
+2524 2524 2524 12502500
+2525 2525 2525 12502500
+2526 2526 2526 12502500
+2527 2527 2527 12502500
+2528 2528 2528 12502500
+2529 2529 2529 12502500
+2530 2530 2530 12502500
+2531 2531 2531 12502500
+2532 2532 2532 12502500
+2533 2533 2533 12502500
+2534 2534 2534 12502500
+2535 2535 2535 12502500
+2536 2536 2536 12502500
+2537 2537 2537 12502500
+2538 2538 2538 12502500
+2539 2539 2539 12502500
+2540 2540 2540 12502500
+2541 2541 2541 12502500
+2542 2542 2542 12502500
+2543 2543 2543 12502500
+2544 2544 2544 12502500
+2545 2545 2545 12502500
+2546 2546 2546 12502500
+2547 2547 2547 12502500
+2548 2548 2548 12502500
+2549 2549 2549 12502500
+2550 2550 2550 12502500
+2551 2551 2551 12502500
+2552 2552 2552 12502500
+2553 2553 2553 12502500
+2554 2554 2554 12502500
+2555 2555 2555 12502500
+2556 2556 2556 12502500
+2557 2557 2557 12502500
+2558 2558 2558 12502500
+2559 2559 2559 12502500
+2560 2560 2560 12502500
+2561 2561 2561 12502500
+2562 2562 2562 12502500
+2563 2563 2563 12502500
+2564 2564 2564 12502500
+2565 2565 2565 12502500
+2566 2566 2566 12502500
+2567 2567 2567 12502500
+2568 2568 2568 12502500
+2569 2569 2569 12502500
+2570 2570 2570 12502500
+2571 2571 2571 12502500
+2572 2572 2572 12502500
+2573 2573 2573 12502500
+2574 2574 2574 12502500
+2575 2575 2575 12502500
+2576 2576 2576 12502500
+2577 2577 2577 12502500
+2578 2578 2578 12502500
+2579 2579 2579 12502500
+2580 2580 2580 12502500
+2581 2581 2581 12502500
+2582 2582 2582 12502500
+2583 2583 2583 12502500
+2584 2584 2584 12502500
+2585 2585 2585 12502500
+2586 2586 2586 12502500
+2587 2587 2587 12502500
+2588 2588 2588 12502500
+2589 2589 2589 12502500
+2590 2590 2590 12502500
+2591 2591 2591 12502500
+2592 2592 2592 12502500
+2593 2593 2593 12502500
+2594 2594 2594 12502500
+2595 2595 2595 12502500
+2596 2596 2596 12502500
+2597 2597 2597 12502500
+2598 2598 2598 12502500
+2599 2599 2599 12502500
+2600 2600 2600 12502500
+2601 2601 2601 12502500
+2602 2602 2602 12502500
+2603 2603 2603 12502500
+2604 2604 2604 12502500
+2605 2605 2605 12502500
+2606 2606 2606 12502500
+2607 2607 2607 12502500
+2608 2608 2608 12502500
+2609 2609 2609 12502500
+2610 2610 2610 12502500
+2611 2611 2611 12502500
+2612 2612 2612 12502500
+2613 2613 2613 12502500
+2614 2614 2614 12502500
+2615 2615 2615 12502500
+2616 2616 2616 12502500
+2617 2617 2617 12502500
+2618 2618 2618 12502500
+2619 2619 2619 12502500
+2620 2620 2620 12502500
+2621 2621 2621 12502500
+2622 2622 2622 12502500
+2623 2623 2623 12502500
+2624 2624 2624 12502500
+2625 2625 2625 12502500
+2626 2626 2626 12502500
+2627 2627 2627 12502500
+2628 2628 2628 12502500
+2629 2629 2629 12502500
+2630 2630 2630 12502500
+2631 2631 2631 12502500
+2632 2632 2632 12502500
+2633 2633 2633 12502500
+2634 2634 2634 12502500
+2635 2635 2635 12502500
+2636 2636 2636 12502500
+2637 2637 2637 12502500
+2638 2638 2638 12502500
+2639 2639 2639 12502500
+2640 2640 2640 12502500
+2641 2641 2641 12502500
+2642 2642 2642 12502500
+2643 2643 2643 12502500
+2644 2644 2644 12502500
+2645 2645 2645 12502500
+2646 2646 2646 12502500
+2647 2647 2647 12502500
+2648 2648 2648 12502500
+2649 2649 2649 12502500
+2650 2650 2650 12502500
+2651 2651 2651 12502500
+2652 2652 2652 12502500
+2653 2653 2653 12502500
+2654 2654 2654 12502500
+2655 2655 2655 12502500
+2656 2656 2656 12502500
+2657 2657 2657 12502500
+2658 2658 2658 12502500
+2659 2659 2659 12502500
+2660 2660 2660 12502500
+2661 2661 2661 12502500
+2662 2662 2662 12502500
+2663 2663 2663 12502500
+2664 2664 2664 12502500
+2665 2665 2665 12502500
+2666 2666 2666 12502500
+2667 2667 2667 12502500
+2668 2668 2668 12502500
+2669 2669 2669 12502500
+2670 2670 2670 12502500
+2671 2671 2671 12502500
+2672 2672 2672 12502500
+2673 2673 2673 12502500
+2674 2674 2674 12502500
+2675 2675 2675 12502500
+2676 2676 2676 12502500
+2677 2677 2677 12502500
+2678 2678 2678 12502500
+2679 2679 2679 12502500
+2680 2680 2680 12502500
+2681 2681 2681 12502500
+2682 2682 2682 12502500
+2683 2683 2683 12502500
+2684 2684 2684 12502500
+2685 2685 2685 12502500
+2686 2686 2686 12502500
+2687 2687 2687 12502500
+2688 2688 2688 12502500
+2689 2689 2689 12502500
+2690 2690 2690 12502500
+2691 2691 2691 12502500
+2692 2692 2692 12502500
+2693 2693 2693 12502500
+2694 2694 2694 12502500
+2695 2695 2695 12502500
+2696 2696 2696 12502500
+2697 2697 2697 12502500
+2698 2698 2698 12502500
+2699 2699 2699 12502500
+2700 2700 2700 12502500
+2701 2701 2701 12502500
+2702 2702 2702 12502500
+2703 2703 2703 12502500
+2704 2704 2704 12502500
+2705 2705 2705 12502500
+2706 2706 2706 12502500
+2707 2707 2707 12502500
+2708 2708 2708 12502500
+2709 2709 2709 12502500
+2710 2710 2710 12502500
+2711 2711 2711 12502500
+2712 2712 2712 12502500
+2713 2713 2713 12502500
+2714 2714 2714 12502500
+2715 2715 2715 12502500
+2716 2716 2716 12502500
+2717 2717 2717 12502500
+2718 2718 2718 12502500
+2719 2719 2719 12502500
+2720 2720 2720 12502500
+2721 2721 2721 12502500
+2722 2722 2722 12502500
+2723 2723 2723 12502500
+2724 2724 2724 12502500
+2725 2725 2725 12502500
+2726 2726 2726 12502500
+2727 2727 2727 12502500
+2728 2728 2728 12502500
+2729 2729 2729 12502500
+2730 2730 2730 12502500
+2731 2731 2731 12502500
+2732 2732 2732 12502500
+2733 2733 2733 12502500
+2734 2734 2734 12502500
+2735 2735 2735 12502500
+2736 2736 2736 12502500
+2737 2737 2737 12502500
+2738 2738 2738 12502500
+2739 2739 2739 12502500
+2740 2740 2740 12502500
+2741 2741 2741 12502500
+2742 2742 2742 12502500
+2743 2743 2743 12502500
+2744 2744 2744 12502500
+2745 2745 2745 12502500
+2746 2746 2746 12502500
+2747 2747 2747 12502500
+2748 2748 2748 12502500
+2749 2749 2749 12502500
+2750 2750 2750 12502500
+2751 2751 2751 12502500
+2752 2752 2752 12502500
+2753 2753 2753 12502500
+2754 2754 2754 12502500
+2755 2755 2755 12502500
+2756 2756 2756 12502500
+2757 2757 2757 12502500
+2758 2758 2758 12502500
+2759 2759 2759 12502500
+2760 2760 2760 12502500
+2761 2761 2761 12502500
+2762 2762 2762 12502500
+2763 2763 2763 12502500
+2764 2764 2764 12502500
+2765 2765 2765 12502500
+2766 2766 2766 12502500
+2767 2767 2767 12502500
+2768 2768 2768 12502500
+2769 2769 2769 12502500
+2770 2770 2770 12502500
+2771 2771 2771 12502500
+2772 2772 2772 12502500
+2773 2773 2773 12502500
+2774 2774 2774 12502500
+2775 2775 2775 12502500
+2776 2776 2776 12502500
+2777 2777 2777 12502500
+2778 2778 2778 12502500
+2779 2779 2779 12502500
+2780 2780 2780 12502500
+2781 2781 2781 12502500
+2782 2782 2782 12502500
+2783 2783 2783 12502500
+2784 2784 2784 12502500
+2785 2785 2785 12502500
+2786 2786 2786 12502500
+2787 2787 2787 12502500
+2788 2788 2788 12502500
+2789 2789 2789 12502500
+2790 2790 2790 12502500
+2791 2791 2791 12502500
+2792 2792 2792 12502500
+2793 2793 2793 12502500
+2794 2794 2794 12502500
+2795 2795 2795 12502500
+2796 2796 2796 12502500
+2797 2797 2797 12502500
+2798 2798 2798 12502500
+2799 2799 2799 12502500
+2800 2800 2800 12502500
+2801 2801 2801 12502500
+2802 2802 2802 12502500
+2803 2803 2803 12502500
+2804 2804 2804 12502500
+2805 2805 2805 12502500
+2806 2806 2806 12502500
+2807 2807 2807 12502500
+2808 2808 2808 12502500
+2809 2809 2809 12502500
+2810 2810 2810 12502500
+2811 2811 2811 12502500
+2812 2812 2812 12502500
+2813 2813 2813 12502500
+2814 2814 2814 12502500
+2815 2815 2815 12502500
+2816 2816 2816 12502500
+2817 2817 2817 12502500
+2818 2818 2818 12502500
+2819 2819 2819 12502500
+2820 2820 2820 12502500
+2821 2821 2821 12502500
+2822 2822 2822 12502500
+2823 2823 2823 12502500
+2824 2824 2824 12502500
+2825 2825 2825 12502500
+2826 2826 2826 12502500
+2827 2827 2827 12502500
+2828 2828 2828 12502500
+2829 2829 2829 12502500
+2830 2830 2830 12502500
+2831 2831 2831 12502500
+2832 2832 2832 12502500
+2833 2833 2833 12502500
+2834 2834 2834 12502500
+2835 2835 2835 12502500
+2836 2836 2836 12502500
+2837 2837 2837 12502500
+2838 2838 2838 12502500
+2839 2839 2839 12502500
+2840 2840 2840 12502500
+2841 2841 2841 12502500
+2842 2842 2842 12502500
+2843 2843 2843 12502500
+2844 2844 2844 12502500
+2845 2845 2845 12502500
+2846 2846 2846 12502500
+2847 2847 2847 12502500
+2848 2848 2848 12502500
+2849 2849 2849 12502500
+2850 2850 2850 12502500
+2851 2851 2851 12502500
+2852 2852 2852 12502500
+2853 2853 2853 12502500
+2854 2854 2854 12502500
+2855 2855 2855 12502500
+2856 2856 2856 12502500
+2857 2857 2857 12502500
+2858 2858 2858 12502500
+2859 2859 2859 12502500
+2860 2860 2860 12502500
+2861 2861 2861 12502500
+2862 2862 2862 12502500
+2863 2863 2863 12502500
+2864 2864 2864 12502500
+2865 2865 2865 12502500
+2866 2866 2866 12502500
+2867 2867 2867 12502500
+2868 2868 2868 12502500
+2869 2869 2869 12502500
+2870 2870 2870 12502500
+2871 2871 2871 12502500
+2872 2872 2872 12502500
+2873 2873 2873 12502500
+2874 2874 2874 12502500
+2875 2875 2875 12502500
+2876 2876 2876 12502500
+2877 2877 2877 12502500
+2878 2878 2878 12502500
+2879 2879 2879 12502500
+2880 2880 2880 12502500
+2881 2881 2881 12502500
+2882 2882 2882 12502500
+2883 2883 2883 12502500
+2884 2884 2884 12502500
+2885 2885 2885 12502500
+2886 2886 2886 12502500
+2887 2887 2887 12502500
+2888 2888 2888 12502500
+2889 2889 2889 12502500
+2890 2890 2890 12502500
+2891 2891 2891 12502500
+2892 2892 2892 12502500
+2893 2893 2893 12502500
+2894 2894 2894 12502500
+2895 2895 2895 12502500
+2896 2896 2896 12502500
+2897 2897 2897 12502500
+2898 2898 2898 12502500
+2899 2899 2899 12502500
+2900 2900 2900 12502500
+2901 2901 2901 12502500
+2902 2902 2902 12502500
+2903 2903 2903 12502500
+2904 2904 2904 12502500
+2905 2905 2905 12502500
+2906 2906 2906 12502500
+2907 2907 2907 12502500
+2908 2908 2908 12502500
+2909 2909 2909 12502500
+2910 2910 2910 12502500
+2911 2911 2911 12502500
+2912 2912 2912 12502500
+2913 2913 2913 12502500
+2914 2914 2914 12502500
+2915 2915 2915 12502500
+2916 2916 2916 12502500
+2917 2917 2917 12502500
+2918 2918 2918 12502500
+2919 2919 2919 12502500
+2920 2920 2920 12502500
+2921 2921 2921 12502500
+2922 2922 2922 12502500
+2923 2923 2923 12502500
+2924 2924 2924 12502500
+2925 2925 2925 12502500
+2926 2926 2926 12502500
+2927 2927 2927 12502500
+2928 2928 2928 12502500
+2929 2929 2929 12502500
+2930 2930 2930 12502500
+2931 2931 2931 12502500
+2932 2932 2932 12502500
+2933 2933 2933 12502500
+2934 2934 2934 12502500
+2935 2935 2935 12502500
+2936 2936 2936 12502500
+2937 2937 2937 12502500
+2938 2938 2938 12502500
+2939 2939 2939 12502500
+2940 2940 2940 12502500
+2941 2941 2941 12502500
+2942 2942 2942 12502500
+2943 2943 2943 12502500
+2944 2944 2944 12502500
+2945 2945 2945 12502500
+2946 2946 2946 12502500
+2947 2947 2947 12502500
+2948 2948 2948 12502500
+2949 2949 2949 12502500
+2950 2950 2950 12502500
+2951 2951 2951 12502500
+2952 2952 2952 12502500
+2953 2953 2953 12502500
+2954 2954 2954 12502500
+2955 2955 2955 12502500
+2956 2956 2956 12502500
+2957 2957 2957 12502500
+2958 2958 2958 12502500
+2959 2959 2959 12502500
+2960 2960 2960 12502500
+2961 2961 2961 12502500
+2962 2962 2962 12502500
+2963 2963 2963 12502500
+2964 2964 2964 12502500
+2965 2965 2965 12502500
+2966 2966 2966 12502500
+2967 2967 2967 12502500
+2968 2968 2968 12502500
+2969 2969 2969 12502500
+2970 2970 2970 12502500
+2971 2971 2971 12502500
+2972 2972 2972 12502500
+2973 2973 2973 12502500
+2974 2974 2974 12502500
+2975 2975 2975 12502500
+2976 2976 2976 12502500
+2977 2977 2977 12502500
+2978 2978 2978 12502500
+2979 2979 2979 12502500
+2980 2980 2980 12502500
+2981 2981 2981 12502500
+2982 2982 2982 12502500
+2983 2983 2983 12502500
+2984 2984 2984 12502500
+2985 2985 2985 12502500
+2986 2986 2986 12502500
+2987 2987 2987 12502500
+2988 2988 2988 12502500
+2989 2989 2989 12502500
+2990 2990 2990 12502500
+2991 2991 2991 12502500
+2992 2992 2992 12502500
+2993 2993 2993 12502500
+2994 2994 2994 12502500
+2995 2995 2995 12502500
+2996 2996 2996 12502500
+2997 2997 2997 12502500
+2998 2998 2998 12502500
+2999 2999 2999 12502500
+3000 3000 3000 12502500
+3001 3001 3001 12502500
+3002 3002 3002 12502500
+3003 3003 3003 12502500
+3004 3004 3004 12502500
+3005 3005 3005 12502500
+3006 3006 3006 12502500
+3007 3007 3007 12502500
+3008 3008 3008 12502500
+3009 3009 3009 12502500
+3010 3010 3010 12502500
+3011 3011 3011 12502500
+3012 3012 3012 12502500
+3013 3013 3013 12502500
+3014 3014 3014 12502500
+3015 3015 3015 12502500
+3016 3016 3016 12502500
+3017 3017 3017 12502500
+3018 3018 3018 12502500
+3019 3019 3019 12502500
+3020 3020 3020 12502500
+3021 3021 3021 12502500
+3022 3022 3022 12502500
+3023 3023 3023 12502500
+3024 3024 3024 12502500
+3025 3025 3025 12502500
+3026 3026 3026 12502500
+3027 3027 3027 12502500
+3028 3028 3028 12502500
+3029 3029 3029 12502500
+3030 3030 3030 12502500
+3031 3031 3031 12502500
+3032 3032 3032 12502500
+3033 3033 3033 12502500
+3034 3034 3034 12502500
+3035 3035 3035 12502500
+3036 3036 3036 12502500
+3037 3037 3037 12502500
+3038 3038 3038 12502500
+3039 3039 3039 12502500
+3040 3040 3040 12502500
+3041 3041 3041 12502500
+3042 3042 3042 12502500
+3043 3043 3043 12502500
+3044 3044 3044 12502500
+3045 3045 3045 12502500
+3046 3046 3046 12502500
+3047 3047 3047 12502500
+3048 3048 3048 12502500
+3049 3049 3049 12502500
+3050 3050 3050 12502500
+3051 3051 3051 12502500
+3052 3052 3052 12502500
+3053 3053 3053 12502500
+3054 3054 3054 12502500
+3055 3055 3055 12502500
+3056 3056 3056 12502500
+3057 3057 3057 12502500
+3058 3058 3058 12502500
+3059 3059 3059 12502500
+3060 3060 3060 12502500
+3061 3061 3061 12502500
+3062 3062 3062 12502500
+3063 3063 3063 12502500
+3064 3064 3064 12502500
+3065 3065 3065 12502500
+3066 3066 3066 12502500
+3067 3067 3067 12502500
+3068 3068 3068 12502500
+3069 3069 3069 12502500
+3070 3070 3070 12502500
+3071 3071 3071 12502500
+3072 3072 3072 12502500
+3073 3073 3073 12502500
+3074 3074 3074 12502500
+3075 3075 3075 12502500
+3076 3076 3076 12502500
+3077 3077 3077 12502500
+3078 3078 3078 12502500
+3079 3079 3079 12502500
+3080 3080 3080 12502500
+3081 3081 3081 12502500
+3082 3082 3082 12502500
+3083 3083 3083 12502500
+3084 3084 3084 12502500
+3085 3085 3085 12502500
+3086 3086 3086 12502500
+3087 3087 3087 12502500
+3088 3088 3088 12502500
+3089 3089 3089 12502500
+3090 3090 3090 12502500
+3091 3091 3091 12502500
+3092 3092 3092 12502500
+3093 3093 3093 12502500
+3094 3094 3094 12502500
+3095 3095 3095 12502500
+3096 3096 3096 12502500
+3097 3097 3097 12502500
+3098 3098 3098 12502500
+3099 3099 3099 12502500
+3100 3100 3100 12502500
+3101 3101 3101 12502500
+3102 3102 3102 12502500
+3103 3103 3103 12502500
+3104 3104 3104 12502500
+3105 3105 3105 12502500
+3106 3106 3106 12502500
+3107 3107 3107 12502500
+3108 3108 3108 12502500
+3109 3109 3109 12502500
+3110 3110 3110 12502500
+3111 3111 3111 12502500
+3112 3112 3112 12502500
+3113 3113 3113 12502500
+3114 3114 3114 12502500
+3115 3115 3115 12502500
+3116 3116 3116 12502500
+3117 3117 3117 12502500
+3118 3118 3118 12502500
+3119 3119 3119 12502500
+3120 3120 3120 12502500
+3121 3121 3121 12502500
+3122 3122 3122 12502500
+3123 3123 3123 12502500
+3124 3124 3124 12502500
+3125 3125 3125 12502500
+3126 3126 3126 12502500
+3127 3127 3127 12502500
+3128 3128 3128 12502500
+3129 3129 3129 12502500
+3130 3130 3130 12502500
+3131 3131 3131 12502500
+3132 3132 3132 12502500
+3133 3133 3133 12502500
+3134 3134 3134 12502500
+3135 3135 3135 12502500
+3136 3136 3136 12502500
+3137 3137 3137 12502500
+3138 3138 3138 12502500
+3139 3139 3139 12502500
+3140 3140 3140 12502500
+3141 3141 3141 12502500
+3142 3142 3142 12502500
+3143 3143 3143 12502500
+3144 3144 3144 12502500
+3145 3145 3145 12502500
+3146 3146 3146 12502500
+3147 3147 3147 12502500
+3148 3148 3148 12502500
+3149 3149 3149 12502500
+3150 3150 3150 12502500
+3151 3151 3151 12502500
+3152 3152 3152 12502500
+3153 3153 3153 12502500
+3154 3154 3154 12502500
+3155 3155 3155 12502500
+3156 3156 3156 12502500
+3157 3157 3157 12502500
+3158 3158 3158 12502500
+3159 3159 3159 12502500
+3160 3160 3160 12502500
+3161 3161 3161 12502500
+3162 3162 3162 12502500
+3163 3163 3163 12502500
+3164 3164 3164 12502500
+3165 3165 3165 12502500
+3166 3166 3166 12502500
+3167 3167 3167 12502500
+3168 3168 3168 12502500
+3169 3169 3169 12502500
+3170 3170 3170 12502500
+3171 3171 3171 12502500
+3172 3172 3172 12502500
+3173 3173 3173 12502500
+3174 3174 3174 12502500
+3175 3175 3175 12502500
+3176 3176 3176 12502500
+3177 3177 3177 12502500
+3178 3178 3178 12502500
+3179 3179 3179 12502500
+3180 3180 3180 12502500
+3181 3181 3181 12502500
+3182 3182 3182 12502500
+3183 3183 3183 12502500
+3184 3184 3184 12502500
+3185 3185 3185 12502500
+3186 3186 3186 12502500
+3187 3187 3187 12502500
+3188 3188 3188 12502500
+3189 3189 3189 12502500
+3190 3190 3190 12502500
+3191 3191 3191 12502500
+3192 3192 3192 12502500
+3193 3193 3193 12502500
+3194 3194 3194 12502500
+3195 3195 3195 12502500
+3196 3196 3196 12502500
+3197 3197 3197 12502500
+3198 3198 3198 12502500
+3199 3199 3199 12502500
+3200 3200 3200 12502500
+3201 3201 3201 12502500
+3202 3202 3202 12502500
+3203 3203 3203 12502500
+3204 3204 3204 12502500
+3205 3205 3205 12502500
+3206 3206 3206 12502500
+3207 3207 3207 12502500
+3208 3208 3208 12502500
+3209 3209 3209 12502500
+3210 3210 3210 12502500
+3211 3211 3211 12502500
+3212 3212 3212 12502500
+3213 3213 3213 12502500
+3214 3214 3214 12502500
+3215 3215 3215 12502500
+3216 3216 3216 12502500
+3217 3217 3217 12502500
+3218 3218 3218 12502500
+3219 3219 3219 12502500
+3220 3220 3220 12502500
+3221 3221 3221 12502500
+3222 3222 3222 12502500
+3223 3223 3223 12502500
+3224 3224 3224 12502500
+3225 3225 3225 12502500
+3226 3226 3226 12502500
+3227 3227 3227 12502500
+3228 3228 3228 12502500
+3229 3229 3229 12502500
+3230 3230 3230 12502500
+3231 3231 3231 12502500
+3232 3232 3232 12502500
+3233 3233 3233 12502500
+3234 3234 3234 12502500
+3235 3235 3235 12502500
+3236 3236 3236 12502500
+3237 3237 3237 12502500
+3238 3238 3238 12502500
+3239 3239 3239 12502500
+3240 3240 3240 12502500
+3241 3241 3241 12502500
+3242 3242 3242 12502500
+3243 3243 3243 12502500
+3244 3244 3244 12502500
+3245 3245 3245 12502500
+3246 3246 3246 12502500
+3247 3247 3247 12502500
+3248 3248 3248 12502500
+3249 3249 3249 12502500
+3250 3250 3250 12502500
+3251 3251 3251 12502500
+3252 3252 3252 12502500
+3253 3253 3253 12502500
+3254 3254 3254 12502500
+3255 3255 3255 12502500
+3256 3256 3256 12502500
+3257 3257 3257 12502500
+3258 3258 3258 12502500
+3259 3259 3259 12502500
+3260 3260 3260 12502500
+3261 3261 3261 12502500
+3262 3262 3262 12502500
+3263 3263 3263 12502500
+3264 3264 3264 12502500
+3265 3265 3265 12502500
+3266 3266 3266 12502500
+3267 3267 3267 12502500
+3268 3268 3268 12502500
+3269 3269 3269 12502500
+3270 3270 3270 12502500
+3271 3271 3271 12502500
+3272 3272 3272 12502500
+3273 3273 3273 12502500
+3274 3274 3274 12502500
+3275 3275 3275 12502500
+3276 3276 3276 12502500
+3277 3277 3277 12502500
+3278 3278 3278 12502500
+3279 3279 3279 12502500
+3280 3280 3280 12502500
+3281 3281 3281 12502500
+3282 3282 3282 12502500
+3283 3283 3283 12502500
+3284 3284 3284 12502500
+3285 3285 3285 12502500
+3286 3286 3286 12502500
+3287 3287 3287 12502500
+3288 3288 3288 12502500
+3289 3289 3289 12502500
+3290 3290 3290 12502500
+3291 3291 3291 12502500
+3292 3292 3292 12502500
+3293 3293 3293 12502500
+3294 3294 3294 12502500
+3295 3295 3295 12502500
+3296 3296 3296 12502500
+3297 3297 3297 12502500
+3298 3298 3298 12502500
+3299 3299 3299 12502500
+3300 3300 3300 12502500
+3301 3301 3301 12502500
+3302 3302 3302 12502500
+3303 3303 3303 12502500
+3304 3304 3304 12502500
+3305 3305 3305 12502500
+3306 3306 3306 12502500
+3307 3307 3307 12502500
+3308 3308 3308 12502500
+3309 3309 3309 12502500
+3310 3310 3310 12502500
+3311 3311 3311 12502500
+3312 3312 3312 12502500
+3313 3313 3313 12502500
+3314 3314 3314 12502500
+3315 3315 3315 12502500
+3316 3316 3316 12502500
+3317 3317 3317 12502500
+3318 3318 3318 12502500
+3319 3319 3319 12502500
+3320 3320 3320 12502500
+3321 3321 3321 12502500
+3322 3322 3322 12502500
+3323 3323 3323 12502500
+3324 3324 3324 12502500
+3325 3325 3325 12502500
+3326 3326 3326 12502500
+3327 3327 3327 12502500
+3328 3328 3328 12502500
+3329 3329 3329 12502500
+3330 3330 3330 12502500
+3331 3331 3331 12502500
+3332 3332 3332 12502500
+3333 3333 3333 12502500
+3334 3334 3334 12502500
+3335 3335 3335 12502500
+3336 3336 3336 12502500
+3337 3337 3337 12502500
+3338 3338 3338 12502500
+3339 3339 3339 12502500
+3340 3340 3340 12502500
+3341 3341 3341 12502500
+3342 3342 3342 12502500
+3343 3343 3343 12502500
+3344 3344 3344 12502500
+3345 3345 3345 12502500
+3346 3346 3346 12502500
+3347 3347 3347 12502500
+3348 3348 3348 12502500
+3349 3349 3349 12502500
+3350 3350 3350 12502500
+3351 3351 3351 12502500
+3352 3352 3352 12502500
+3353 3353 3353 12502500
+3354 3354 3354 12502500
+3355 3355 3355 12502500
+3356 3356 3356 12502500
+3357 3357 3357 12502500
+3358 3358 3358 12502500
+3359 3359 3359 12502500
+3360 3360 3360 12502500
+3361 3361 3361 12502500
+3362 3362 3362 12502500
+3363 3363 3363 12502500
+3364 3364 3364 12502500
+3365 3365 3365 12502500
+3366 3366 3366 12502500
+3367 3367 3367 12502500
+3368 3368 3368 12502500
+3369 3369 3369 12502500
+3370 3370 3370 12502500
+3371 3371 3371 12502500
+3372 3372 3372 12502500
+3373 3373 3373 12502500
+3374 3374 3374 12502500
+3375 3375 3375 12502500
+3376 3376 3376 12502500
+3377 3377 3377 12502500
+3378 3378 3378 12502500
+3379 3379 3379 12502500
+3380 3380 3380 12502500
+3381 3381 3381 12502500
+3382 3382 3382 12502500
+3383 3383 3383 12502500
+3384 3384 3384 12502500
+3385 3385 3385 12502500
+3386 3386 3386 12502500
+3387 3387 3387 12502500
+3388 3388 3388 12502500
+3389 3389 3389 12502500
+3390 3390 3390 12502500
+3391 3391 3391 12502500
+3392 3392 3392 12502500
+3393 3393 3393 12502500
+3394 3394 3394 12502500
+3395 3395 3395 12502500
+3396 3396 3396 12502500
+3397 3397 3397 12502500
+3398 3398 3398 12502500
+3399 3399 3399 12502500
+3400 3400 3400 12502500
+3401 3401 3401 12502500
+3402 3402 3402 12502500
+3403 3403 3403 12502500
+3404 3404 3404 12502500
+3405 3405 3405 12502500
+3406 3406 3406 12502500
+3407 3407 3407 12502500
+3408 3408 3408 12502500
+3409 3409 3409 12502500
+3410 3410 3410 12502500
+3411 3411 3411 12502500
+3412 3412 3412 12502500
+3413 3413 3413 12502500
+3414 3414 3414 12502500
+3415 3415 3415 12502500
+3416 3416 3416 12502500
+3417 3417 3417 12502500
+3418 3418 3418 12502500
+3419 3419 3419 12502500
+3420 3420 3420 12502500
+3421 3421 3421 12502500
+3422 3422 3422 12502500
+3423 3423 3423 12502500
+3424 3424 3424 12502500
+3425 3425 3425 12502500
+3426 3426 3426 12502500
+3427 3427 3427 12502500
+3428 3428 3428 12502500
+3429 3429 3429 12502500
+3430 3430 3430 12502500
+3431 3431 3431 12502500
+3432 3432 3432 12502500
+3433 3433 3433 12502500
+3434 3434 3434 12502500
+3435 3435 3435 12502500
+3436 3436 3436 12502500
+3437 3437 3437 12502500
+3438 3438 3438 12502500
+3439 3439 3439 12502500
+3440 3440 3440 12502500
+3441 3441 3441 12502500
+3442 3442 3442 12502500
+3443 3443 3443 12502500
+3444 3444 3444 12502500
+3445 3445 3445 12502500
+3446 3446 3446 12502500
+3447 3447 3447 12502500
+3448 3448 3448 12502500
+3449 3449 3449 12502500
+3450 3450 3450 12502500
+3451 3451 3451 12502500
+3452 3452 3452 12502500
+3453 3453 3453 12502500
+3454 3454 3454 12502500
+3455 3455 3455 12502500
+3456 3456 3456 12502500
+3457 3457 3457 12502500
+3458 3458 3458 12502500
+3459 3459 3459 12502500
+3460 3460 3460 12502500
+3461 3461 3461 12502500
+3462 3462 3462 12502500
+3463 3463 3463 12502500
+3464 3464 3464 12502500
+3465 3465 3465 12502500
+3466 3466 3466 12502500
+3467 3467 3467 12502500
+3468 3468 3468 12502500
+3469 3469 3469 12502500
+3470 3470 3470 12502500
+3471 3471 3471 12502500
+3472 3472 3472 12502500
+3473 3473 3473 12502500
+3474 3474 3474 12502500
+3475 3475 3475 12502500
+3476 3476 3476 12502500
+3477 3477 3477 12502500
+3478 3478 3478 12502500
+3479 3479 3479 12502500
+3480 3480 3480 12502500
+3481 3481 3481 12502500
+3482 3482 3482 12502500
+3483 3483 3483 12502500
+3484 3484 3484 12502500
+3485 3485 3485 12502500
+3486 3486 3486 12502500
+3487 3487 3487 12502500
+3488 3488 3488 12502500
+3489 3489 3489 12502500
+3490 3490 3490 12502500
+3491 3491 3491 12502500
+3492 3492 3492 12502500
+3493 3493 3493 12502500
+3494 3494 3494 12502500
+3495 3495 3495 12502500
+3496 3496 3496 12502500
+3497 3497 3497 12502500
+3498 3498 3498 12502500
+3499 3499 3499 12502500
+3500 3500 3500 12502500
+3501 3501 3501 12502500
+3502 3502 3502 12502500
+3503 3503 3503 12502500
+3504 3504 3504 12502500
+3505 3505 3505 12502500
+3506 3506 3506 12502500
+3507 3507 3507 12502500
+3508 3508 3508 12502500
+3509 3509 3509 12502500
+3510 3510 3510 12502500
+3511 3511 3511 12502500
+3512 3512 3512 12502500
+3513 3513 3513 12502500
+3514 3514 3514 12502500
+3515 3515 3515 12502500
+3516 3516 3516 12502500
+3517 3517 3517 12502500
+3518 3518 3518 12502500
+3519 3519 3519 12502500
+3520 3520 3520 12502500
+3521 3521 3521 12502500
+3522 3522 3522 12502500
+3523 3523 3523 12502500
+3524 3524 3524 12502500
+3525 3525 3525 12502500
+3526 3526 3526 12502500
+3527 3527 3527 12502500
+3528 3528 3528 12502500
+3529 3529 3529 12502500
+3530 3530 3530 12502500
+3531 3531 3531 12502500
+3532 3532 3532 12502500
+3533 3533 3533 12502500
+3534 3534 3534 12502500
+3535 3535 3535 12502500
+3536 3536 3536 12502500
+3537 3537 3537 12502500
+3538 3538 3538 12502500
+3539 3539 3539 12502500
+3540 3540 3540 12502500
+3541 3541 3541 12502500
+3542 3542 3542 12502500
+3543 3543 3543 12502500
+3544 3544 3544 12502500
+3545 3545 3545 12502500
+3546 3546 3546 12502500
+3547 3547 3547 12502500
+3548 3548 3548 12502500
+3549 3549 3549 12502500
+3550 3550 3550 12502500
+3551 3551 3551 12502500
+3552 3552 3552 12502500
+3553 3553 3553 12502500
+3554 3554 3554 12502500
+3555 3555 3555 12502500
+3556 3556 3556 12502500
+3557 3557 3557 12502500
+3558 3558 3558 12502500
+3559 3559 3559 12502500
+3560 3560 3560 12502500
+3561 3561 3561 12502500
+3562 3562 3562 12502500
+3563 3563 3563 12502500
+3564 3564 3564 12502500
+3565 3565 3565 12502500
+3566 3566 3566 12502500
+3567 3567 3567 12502500
+3568 3568 3568 12502500
+3569 3569 3569 12502500
+3570 3570 3570 12502500
+3571 3571 3571 12502500
+3572 3572 3572 12502500
+3573 3573 3573 12502500
+3574 3574 3574 12502500
+3575 3575 3575 12502500
+3576 3576 3576 12502500
+3577 3577 3577 12502500
+3578 3578 3578 12502500
+3579 3579 3579 12502500
+3580 3580 3580 12502500
+3581 3581 3581 12502500
+3582 3582 3582 12502500
+3583 3583 3583 12502500
+3584 3584 3584 12502500
+3585 3585 3585 12502500
+3586 3586 3586 12502500
+3587 3587 3587 12502500
+3588 3588 3588 12502500
+3589 3589 3589 12502500
+3590 3590 3590 12502500
+3591 3591 3591 12502500
+3592 3592 3592 12502500
+3593 3593 3593 12502500
+3594 3594 3594 12502500
+3595 3595 3595 12502500
+3596 3596 3596 12502500
+3597 3597 3597 12502500
+3598 3598 3598 12502500
+3599 3599 3599 12502500
+3600 3600 3600 12502500
+3601 3601 3601 12502500
+3602 3602 3602 12502500
+3603 3603 3603 12502500
+3604 3604 3604 12502500
+3605 3605 3605 12502500
+3606 3606 3606 12502500
+3607 3607 3607 12502500
+3608 3608 3608 12502500
+3609 3609 3609 12502500
+3610 3610 3610 12502500
+3611 3611 3611 12502500
+3612 3612 3612 12502500
+3613 3613 3613 12502500
+3614 3614 3614 12502500
+3615 3615 3615 12502500
+3616 3616 3616 12502500
+3617 3617 3617 12502500
+3618 3618 3618 12502500
+3619 3619 3619 12502500
+3620 3620 3620 12502500
+3621 3621 3621 12502500
+3622 3622 3622 12502500
+3623 3623 3623 12502500
+3624 3624 3624 12502500
+3625 3625 3625 12502500
+3626 3626 3626 12502500
+3627 3627 3627 12502500
+3628 3628 3628 12502500
+3629 3629 3629 12502500
+3630 3630 3630 12502500
+3631 3631 3631 12502500
+3632 3632 3632 12502500
+3633 3633 3633 12502500
+3634 3634 3634 12502500
+3635 3635 3635 12502500
+3636 3636 3636 12502500
+3637 3637 3637 12502500
+3638 3638 3638 12502500
+3639 3639 3639 12502500
+3640 3640 3640 12502500
+3641 3641 3641 12502500
+3642 3642 3642 12502500
+3643 3643 3643 12502500
+3644 3644 3644 12502500
+3645 3645 3645 12502500
+3646 3646 3646 12502500
+3647 3647 3647 12502500
+3648 3648 3648 12502500
+3649 3649 3649 12502500
+3650 3650 3650 12502500
+3651 3651 3651 12502500
+3652 3652 3652 12502500
+3653 3653 3653 12502500
+3654 3654 3654 12502500
+3655 3655 3655 12502500
+3656 3656 3656 12502500
+3657 3657 3657 12502500
+3658 3658 3658 12502500
+3659 3659 3659 12502500
+3660 3660 3660 12502500
+3661 3661 3661 12502500
+3662 3662 3662 12502500
+3663 3663 3663 12502500
+3664 3664 3664 12502500
+3665 3665 3665 12502500
+3666 3666 3666 12502500
+3667 3667 3667 12502500
+3668 3668 3668 12502500
+3669 3669 3669 12502500
+3670 3670 3670 12502500
+3671 3671 3671 12502500
+3672 3672 3672 12502500
+3673 3673 3673 12502500
+3674 3674 3674 12502500
+3675 3675 3675 12502500
+3676 3676 3676 12502500
+3677 3677 3677 12502500
+3678 3678 3678 12502500
+3679 3679 3679 12502500
+3680 3680 3680 12502500
+3681 3681 3681 12502500
+3682 3682 3682 12502500
+3683 3683 3683 12502500
+3684 3684 3684 12502500
+3685 3685 3685 12502500
+3686 3686 3686 12502500
+3687 3687 3687 12502500
+3688 3688 3688 12502500
+3689 3689 3689 12502500
+3690 3690 3690 12502500
+3691 3691 3691 12502500
+3692 3692 3692 12502500
+3693 3693 3693 12502500
+3694 3694 3694 12502500
+3695 3695 3695 12502500
+3696 3696 3696 12502500
+3697 3697 3697 12502500
+3698 3698 3698 12502500
+3699 3699 3699 12502500
+3700 3700 3700 12502500
+3701 3701 3701 12502500
+3702 3702 3702 12502500
+3703 3703 3703 12502500
+3704 3704 3704 12502500
+3705 3705 3705 12502500
+3706 3706 3706 12502500
+3707 3707 3707 12502500
+3708 3708 3708 12502500
+3709 3709 3709 12502500
+3710 3710 3710 12502500
+3711 3711 3711 12502500
+3712 3712 3712 12502500
+3713 3713 3713 12502500
+3714 3714 3714 12502500
+3715 3715 3715 12502500
+3716 3716 3716 12502500
+3717 3717 3717 12502500
+3718 3718 3718 12502500
+3719 3719 3719 12502500
+3720 3720 3720 12502500
+3721 3721 3721 12502500
+3722 3722 3722 12502500
+3723 3723 3723 12502500
+3724 3724 3724 12502500
+3725 3725 3725 12502500
+3726 3726 3726 12502500
+3727 3727 3727 12502500
+3728 3728 3728 12502500
+3729 3729 3729 12502500
+3730 3730 3730 12502500
+3731 3731 3731 12502500
+3732 3732 3732 12502500
+3733 3733 3733 12502500
+3734 3734 3734 12502500
+3735 3735 3735 12502500
+3736 3736 3736 12502500
+3737 3737 3737 12502500
+3738 3738 3738 12502500
+3739 3739 3739 12502500
+3740 3740 3740 12502500
+3741 3741 3741 12502500
+3742 3742 3742 12502500
+3743 3743 3743 12502500
+3744 3744 3744 12502500
+3745 3745 3745 12502500
+3746 3746 3746 12502500
+3747 3747 3747 12502500
+3748 3748 3748 12502500
+3749 3749 3749 12502500
+3750 3750 3750 12502500
+3751 3751 3751 12502500
+3752 3752 3752 12502500
+3753 3753 3753 12502500
+3754 3754 3754 12502500
+3755 3755 3755 12502500
+3756 3756 3756 12502500
+3757 3757 3757 12502500
+3758 3758 3758 12502500
+3759 3759 3759 12502500
+3760 3760 3760 12502500
+3761 3761 3761 12502500
+3762 3762 3762 12502500
+3763 3763 3763 12502500
+3764 3764 3764 12502500
+3765 3765 3765 12502500
+3766 3766 3766 12502500
+3767 3767 3767 12502500
+3768 3768 3768 12502500
+3769 3769 3769 12502500
+3770 3770 3770 12502500
+3771 3771 3771 12502500
+3772 3772 3772 12502500
+3773 3773 3773 12502500
+3774 3774 3774 12502500
+3775 3775 3775 12502500
+3776 3776 3776 12502500
+3777 3777 3777 12502500
+3778 3778 3778 12502500
+3779 3779 3779 12502500
+3780 3780 3780 12502500
+3781 3781 3781 12502500
+3782 3782 3782 12502500
+3783 3783 3783 12502500
+3784 3784 3784 12502500
+3785 3785 3785 12502500
+3786 3786 3786 12502500
+3787 3787 3787 12502500
+3788 3788 3788 12502500
+3789 3789 3789 12502500
+3790 3790 3790 12502500
+3791 3791 3791 12502500
+3792 3792 3792 12502500
+3793 3793 3793 12502500
+3794 3794 3794 12502500
+3795 3795 3795 12502500
+3796 3796 3796 12502500
+3797 3797 3797 12502500
+3798 3798 3798 12502500
+3799 3799 3799 12502500
+3800 3800 3800 12502500
+3801 3801 3801 12502500
+3802 3802 3802 12502500
+3803 3803 3803 12502500
+3804 3804 3804 12502500
+3805 3805 3805 12502500
+3806 3806 3806 12502500
+3807 3807 3807 12502500
+3808 3808 3808 12502500
+3809 3809 3809 12502500
+3810 3810 3810 12502500
+3811 3811 3811 12502500
+3812 3812 3812 12502500
+3813 3813 3813 12502500
+3814 3814 3814 12502500
+3815 3815 3815 12502500
+3816 3816 3816 12502500
+3817 3817 3817 12502500
+3818 3818 3818 12502500
+3819 3819 3819 12502500
+3820 3820 3820 12502500
+3821 3821 3821 12502500
+3822 3822 3822 12502500
+3823 3823 3823 12502500
+3824 3824 3824 12502500
+3825 3825 3825 12502500
+3826 3826 3826 12502500
+3827 3827 3827 12502500
+3828 3828 3828 12502500
+3829 3829 3829 12502500
+3830 3830 3830 12502500
+3831 3831 3831 12502500
+3832 3832 3832 12502500
+3833 3833 3833 12502500
+3834 3834 3834 12502500
+3835 3835 3835 12502500
+3836 3836 3836 12502500
+3837 3837 3837 12502500
+3838 3838 3838 12502500
+3839 3839 3839 12502500
+3840 3840 3840 12502500
+3841 3841 3841 12502500
+3842 3842 3842 12502500
+3843 3843 3843 12502500
+3844 3844 3844 12502500
+3845 3845 3845 12502500
+3846 3846 3846 12502500
+3847 3847 3847 12502500
+3848 3848 3848 12502500
+3849 3849 3849 12502500
+3850 3850 3850 12502500
+3851 3851 3851 12502500
+3852 3852 3852 12502500
+3853 3853 3853 12502500
+3854 3854 3854 12502500
+3855 3855 3855 12502500
+3856 3856 3856 12502500
+3857 3857 3857 12502500
+3858 3858 3858 12502500
+3859 3859 3859 12502500
+3860 3860 3860 12502500
+3861 3861 3861 12502500
+3862 3862 3862 12502500
+3863 3863 3863 12502500
+3864 3864 3864 12502500
+3865 3865 3865 12502500
+3866 3866 3866 12502500
+3867 3867 3867 12502500
+3868 3868 3868 12502500
+3869 3869 3869 12502500
+3870 3870 3870 12502500
+3871 3871 3871 12502500
+3872 3872 3872 12502500
+3873 3873 3873 12502500
+3874 3874 3874 12502500
+3875 3875 3875 12502500
+3876 3876 3876 12502500
+3877 3877 3877 12502500
+3878 3878 3878 12502500
+3879 3879 3879 12502500
+3880 3880 3880 12502500
+3881 3881 3881 12502500
+3882 3882 3882 12502500
+3883 3883 3883 12502500
+3884 3884 3884 12502500
+3885 3885 3885 12502500
+3886 3886 3886 12502500
+3887 3887 3887 12502500
+3888 3888 3888 12502500
+3889 3889 3889 12502500
+3890 3890 3890 12502500
+3891 3891 3891 12502500
+3892 3892 3892 12502500
+3893 3893 3893 12502500
+3894 3894 3894 12502500
+3895 3895 3895 12502500
+3896 3896 3896 12502500
+3897 3897 3897 12502500
+3898 3898 3898 12502500
+3899 3899 3899 12502500
+3900 3900 3900 12502500
+3901 3901 3901 12502500
+3902 3902 3902 12502500
+3903 3903 3903 12502500
+3904 3904 3904 12502500
+3905 3905 3905 12502500
+3906 3906 3906 12502500
+3907 3907 3907 12502500
+3908 3908 3908 12502500
+3909 3909 3909 12502500
+3910 3910 3910 12502500
+3911 3911 3911 12502500
+3912 3912 3912 12502500
+3913 3913 3913 12502500
+3914 3914 3914 12502500
+3915 3915 3915 12502500
+3916 3916 3916 12502500
+3917 3917 3917 12502500
+3918 3918 3918 12502500
+3919 3919 3919 12502500
+3920 3920 3920 12502500
+3921 3921 3921 12502500
+3922 3922 3922 12502500
+3923 3923 3923 12502500
+3924 3924 3924 12502500
+3925 3925 3925 12502500
+3926 3926 3926 12502500
+3927 3927 3927 12502500
+3928 3928 3928 12502500
+3929 3929 3929 12502500
+3930 3930 3930 12502500
+3931 3931 3931 12502500
+3932 3932 3932 12502500
+3933 3933 3933 12502500
+3934 3934 3934 12502500
+3935 3935 3935 12502500
+3936 3936 3936 12502500
+3937 3937 3937 12502500
+3938 3938 3938 12502500
+3939 3939 3939 12502500
+3940 3940 3940 12502500
+3941 3941 3941 12502500
+3942 3942 3942 12502500
+3943 3943 3943 12502500
+3944 3944 3944 12502500
+3945 3945 3945 12502500
+3946 3946 3946 12502500
+3947 3947 3947 12502500
+3948 3948 3948 12502500
+3949 3949 3949 12502500
+3950 3950 3950 12502500
+3951 3951 3951 12502500
+3952 3952 3952 12502500
+3953 3953 3953 12502500
+3954 3954 3954 12502500
+3955 3955 3955 12502500
+3956 3956 3956 12502500
+3957 3957 3957 12502500
+3958 3958 3958 12502500
+3959 3959 3959 12502500
+3960 3960 3960 12502500
+3961 3961 3961 12502500
+3962 3962 3962 12502500
+3963 3963 3963 12502500
+3964 3964 3964 12502500
+3965 3965 3965 12502500
+3966 3966 3966 12502500
+3967 3967 3967 12502500
+3968 3968 3968 12502500
+3969 3969 3969 12502500
+3970 3970 3970 12502500
+3971 3971 3971 12502500
+3972 3972 3972 12502500
+3973 3973 3973 12502500
+3974 3974 3974 12502500
+3975 3975 3975 12502500
+3976 3976 3976 12502500
+3977 3977 3977 12502500
+3978 3978 3978 12502500
+3979 3979 3979 12502500
+3980 3980 3980 12502500
+3981 3981 3981 12502500
+3982 3982 3982 12502500
+3983 3983 3983 12502500
+3984 3984 3984 12502500
+3985 3985 3985 12502500
+3986 3986 3986 12502500
+3987 3987 3987 12502500
+3988 3988 3988 12502500
+3989 3989 3989 12502500
+3990 3990 3990 12502500
+3991 3991 3991 12502500
+3992 3992 3992 12502500
+3993 3993 3993 12502500
+3994 3994 3994 12502500
+3995 3995 3995 12502500
+3996 3996 3996 12502500
+3997 3997 3997 12502500
+3998 3998 3998 12502500
+3999 3999 3999 12502500
+4000 4000 4000 12502500
+4001 4001 4001 12502500
+4002 4002 4002 12502500
+4003 4003 4003 12502500
+4004 4004 4004 12502500
+4005 4005 4005 12502500
+4006 4006 4006 12502500
+4007 4007 4007 12502500
+4008 4008 4008 12502500
+4009 4009 4009 12502500
+4010 4010 4010 12502500
+4011 4011 4011 12502500
+4012 4012 4012 12502500
+4013 4013 4013 12502500
+4014 4014 4014 12502500
+4015 4015 4015 12502500
+4016 4016 4016 12502500
+4017 4017 4017 12502500
+4018 4018 4018 12502500
+4019 4019 4019 12502500
+4020 4020 4020 12502500
+4021 4021 4021 12502500
+4022 4022 4022 12502500
+4023 4023 4023 12502500
+4024 4024 4024 12502500
+4025 4025 4025 12502500
+4026 4026 4026 12502500
+4027 4027 4027 12502500
+4028 4028 4028 12502500
+4029 4029 4029 12502500
+4030 4030 4030 12502500
+4031 4031 4031 12502500
+4032 4032 4032 12502500
+4033 4033 4033 12502500
+4034 4034 4034 12502500
+4035 4035 4035 12502500
+4036 4036 4036 12502500
+4037 4037 4037 12502500
+4038 4038 4038 12502500
+4039 4039 4039 12502500
+4040 4040 4040 12502500
+4041 4041 4041 12502500
+4042 4042 4042 12502500
+4043 4043 4043 12502500
+4044 4044 4044 12502500
+4045 4045 4045 12502500
+4046 4046 4046 12502500
+4047 4047 4047 12502500
+4048 4048 4048 12502500
+4049 4049 4049 12502500
+4050 4050 4050 12502500
+4051 4051 4051 12502500
+4052 4052 4052 12502500
+4053 4053 4053 12502500
+4054 4054 4054 12502500
+4055 4055 4055 12502500
+4056 4056 4056 12502500
+4057 4057 4057 12502500
+4058 4058 4058 12502500
+4059 4059 4059 12502500
+4060 4060 4060 12502500
+4061 4061 4061 12502500
+4062 4062 4062 12502500
+4063 4063 4063 12502500
+4064 4064 4064 12502500
+4065 4065 4065 12502500
+4066 4066 4066 12502500
+4067 4067 4067 12502500
+4068 4068 4068 12502500
+4069 4069 4069 12502500
+4070 4070 4070 12502500
+4071 4071 4071 12502500
+4072 4072 4072 12502500
+4073 4073 4073 12502500
+4074 4074 4074 12502500
+4075 4075 4075 12502500
+4076 4076 4076 12502500
+4077 4077 4077 12502500
+4078 4078 4078 12502500
+4079 4079 4079 12502500
+4080 4080 4080 12502500
+4081 4081 4081 12502500
+4082 4082 4082 12502500
+4083 4083 4083 12502500
+4084 4084 4084 12502500
+4085 4085 4085 12502500
+4086 4086 4086 12502500
+4087 4087 4087 12502500
+4088 4088 4088 12502500
+4089 4089 4089 12502500
+4090 4090 4090 12502500
+4091 4091 4091 12502500
+4092 4092 4092 12502500
+4093 4093 4093 12502500
+4094 4094 4094 12502500
+4095 4095 4095 12502500
+4096 4096 4096 12502500
+4097 4097 4097 12502500
+4098 4098 4098 12502500
+4099 4099 4099 12502500
+4100 4100 4100 12502500
+4101 4101 4101 12502500
+4102 4102 4102 12502500
+4103 4103 4103 12502500
+4104 4104 4104 12502500
+4105 4105 4105 12502500
+4106 4106 4106 12502500
+4107 4107 4107 12502500
+4108 4108 4108 12502500
+4109 4109 4109 12502500
+4110 4110 4110 12502500
+4111 4111 4111 12502500
+4112 4112 4112 12502500
+4113 4113 4113 12502500
+4114 4114 4114 12502500
+4115 4115 4115 12502500
+4116 4116 4116 12502500
+4117 4117 4117 12502500
+4118 4118 4118 12502500
+4119 4119 4119 12502500
+4120 4120 4120 12502500
+4121 4121 4121 12502500
+4122 4122 4122 12502500
+4123 4123 4123 12502500
+4124 4124 4124 12502500
+4125 4125 4125 12502500
+4126 4126 4126 12502500
+4127 4127 4127 12502500
+4128 4128 4128 12502500
+4129 4129 4129 12502500
+4130 4130 4130 12502500
+4131 4131 4131 12502500
+4132 4132 4132 12502500
+4133 4133 4133 12502500
+4134 4134 4134 12502500
+4135 4135 4135 12502500
+4136 4136 4136 12502500
+4137 4137 4137 12502500
+4138 4138 4138 12502500
+4139 4139 4139 12502500
+4140 4140 4140 12502500
+4141 4141 4141 12502500
+4142 4142 4142 12502500
+4143 4143 4143 12502500
+4144 4144 4144 12502500
+4145 4145 4145 12502500
+4146 4146 4146 12502500
+4147 4147 4147 12502500
+4148 4148 4148 12502500
+4149 4149 4149 12502500
+4150 4150 4150 12502500
+4151 4151 4151 12502500
+4152 4152 4152 12502500
+4153 4153 4153 12502500
+4154 4154 4154 12502500
+4155 4155 4155 12502500
+4156 4156 4156 12502500
+4157 4157 4157 12502500
+4158 4158 4158 12502500
+4159 4159 4159 12502500
+4160 4160 4160 12502500
+4161 4161 4161 12502500
+4162 4162 4162 12502500
+4163 4163 4163 12502500
+4164 4164 4164 12502500
+4165 4165 4165 12502500
+4166 4166 4166 12502500
+4167 4167 4167 12502500
+4168 4168 4168 12502500
+4169 4169 4169 12502500
+4170 4170 4170 12502500
+4171 4171 4171 12502500
+4172 4172 4172 12502500
+4173 4173 4173 12502500
+4174 4174 4174 12502500
+4175 4175 4175 12502500
+4176 4176 4176 12502500
+4177 4177 4177 12502500
+4178 4178 4178 12502500
+4179 4179 4179 12502500
+4180 4180 4180 12502500
+4181 4181 4181 12502500
+4182 4182 4182 12502500
+4183 4183 4183 12502500
+4184 4184 4184 12502500
+4185 4185 4185 12502500
+4186 4186 4186 12502500
+4187 4187 4187 12502500
+4188 4188 4188 12502500
+4189 4189 4189 12502500
+4190 4190 4190 12502500
+4191 4191 4191 12502500
+4192 4192 4192 12502500
+4193 4193 4193 12502500
+4194 4194 4194 12502500
+4195 4195 4195 12502500
+4196 4196 4196 12502500
+4197 4197 4197 12502500
+4198 4198 4198 12502500
+4199 4199 4199 12502500
+4200 4200 4200 12502500
+4201 4201 4201 12502500
+4202 4202 4202 12502500
+4203 4203 4203 12502500
+4204 4204 4204 12502500
+4205 4205 4205 12502500
+4206 4206 4206 12502500
+4207 4207 4207 12502500
+4208 4208 4208 12502500
+4209 4209 4209 12502500
+4210 4210 4210 12502500
+4211 4211 4211 12502500
+4212 4212 4212 12502500
+4213 4213 4213 12502500
+4214 4214 4214 12502500
+4215 4215 4215 12502500
+4216 4216 4216 12502500
+4217 4217 4217 12502500
+4218 4218 4218 12502500
+4219 4219 4219 12502500
+4220 4220 4220 12502500
+4221 4221 4221 12502500
+4222 4222 4222 12502500
+4223 4223 4223 12502500
+4224 4224 4224 12502500
+4225 4225 4225 12502500
+4226 4226 4226 12502500
+4227 4227 4227 12502500
+4228 4228 4228 12502500
+4229 4229 4229 12502500
+4230 4230 4230 12502500
+4231 4231 4231 12502500
+4232 4232 4232 12502500
+4233 4233 4233 12502500
+4234 4234 4234 12502500
+4235 4235 4235 12502500
+4236 4236 4236 12502500
+4237 4237 4237 12502500
+4238 4238 4238 12502500
+4239 4239 4239 12502500
+4240 4240 4240 12502500
+4241 4241 4241 12502500
+4242 4242 4242 12502500
+4243 4243 4243 12502500
+4244 4244 4244 12502500
+4245 4245 4245 12502500
+4246 4246 4246 12502500
+4247 4247 4247 12502500
+4248 4248 4248 12502500
+4249 4249 4249 12502500
+4250 4250 4250 12502500
+4251 4251 4251 12502500
+4252 4252 4252 12502500
+4253 4253 4253 12502500
+4254 4254 4254 12502500
+4255 4255 4255 12502500
+4256 4256 4256 12502500
+4257 4257 4257 12502500
+4258 4258 4258 12502500
+4259 4259 4259 12502500
+4260 4260 4260 12502500
+4261 4261 4261 12502500
+4262 4262 4262 12502500
+4263 4263 4263 12502500
+4264 4264 4264 12502500
+4265 4265 4265 12502500
+4266 4266 4266 12502500
+4267 4267 4267 12502500
+4268 4268 4268 12502500
+4269 4269 4269 12502500
+4270 4270 4270 12502500
+4271 4271 4271 12502500
+4272 4272 4272 12502500
+4273 4273 4273 12502500
+4274 4274 4274 12502500
+4275 4275 4275 12502500
+4276 4276 4276 12502500
+4277 4277 4277 12502500
+4278 4278 4278 12502500
+4279 4279 4279 12502500
+4280 4280 4280 12502500
+4281 4281 4281 12502500
+4282 4282 4282 12502500
+4283 4283 4283 12502500
+4284 4284 4284 12502500
+4285 4285 4285 12502500
+4286 4286 4286 12502500
+4287 4287 4287 12502500
+4288 4288 4288 12502500
+4289 4289 4289 12502500
+4290 4290 4290 12502500
+4291 4291 4291 12502500
+4292 4292 4292 12502500
+4293 4293 4293 12502500
+4294 4294 4294 12502500
+4295 4295 4295 12502500
+4296 4296 4296 12502500
+4297 4297 4297 12502500
+4298 4298 4298 12502500
+4299 4299 4299 12502500
+4300 4300 4300 12502500
+4301 4301 4301 12502500
+4302 4302 4302 12502500
+4303 4303 4303 12502500
+4304 4304 4304 12502500
+4305 4305 4305 12502500
+4306 4306 4306 12502500
+4307 4307 4307 12502500
+4308 4308 4308 12502500
+4309 4309 4309 12502500
+4310 4310 4310 12502500
+4311 4311 4311 12502500
+4312 4312 4312 12502500
+4313 4313 4313 12502500
+4314 4314 4314 12502500
+4315 4315 4315 12502500
+4316 4316 4316 12502500
+4317 4317 4317 12502500
+4318 4318 4318 12502500
+4319 4319 4319 12502500
+4320 4320 4320 12502500
+4321 4321 4321 12502500
+4322 4322 4322 12502500
+4323 4323 4323 12502500
+4324 4324 4324 12502500
+4325 4325 4325 12502500
+4326 4326 4326 12502500
+4327 4327 4327 12502500
+4328 4328 4328 12502500
+4329 4329 4329 12502500
+4330 4330 4330 12502500
+4331 4331 4331 12502500
+4332 4332 4332 12502500
+4333 4333 4333 12502500
+4334 4334 4334 12502500
+4335 4335 4335 12502500
+4336 4336 4336 12502500
+4337 4337 4337 12502500
+4338 4338 4338 12502500
+4339 4339 4339 12502500
+4340 4340 4340 12502500
+4341 4341 4341 12502500
+4342 4342 4342 12502500
+4343 4343 4343 12502500
+4344 4344 4344 12502500
+4345 4345 4345 12502500
+4346 4346 4346 12502500
+4347 4347 4347 12502500
+4348 4348 4348 12502500
+4349 4349 4349 12502500
+4350 4350 4350 12502500
+4351 4351 4351 12502500
+4352 4352 4352 12502500
+4353 4353 4353 12502500
+4354 4354 4354 12502500
+4355 4355 4355 12502500
+4356 4356 4356 12502500
+4357 4357 4357 12502500
+4358 4358 4358 12502500
+4359 4359 4359 12502500
+4360 4360 4360 12502500
+4361 4361 4361 12502500
+4362 4362 4362 12502500
+4363 4363 4363 12502500
+4364 4364 4364 12502500
+4365 4365 4365 12502500
+4366 4366 4366 12502500
+4367 4367 4367 12502500
+4368 4368 4368 12502500
+4369 4369 4369 12502500
+4370 4370 4370 12502500
+4371 4371 4371 12502500
+4372 4372 4372 12502500
+4373 4373 4373 12502500
+4374 4374 4374 12502500
+4375 4375 4375 12502500
+4376 4376 4376 12502500
+4377 4377 4377 12502500
+4378 4378 4378 12502500
+4379 4379 4379 12502500
+4380 4380 4380 12502500
+4381 4381 4381 12502500
+4382 4382 4382 12502500
+4383 4383 4383 12502500
+4384 4384 4384 12502500
+4385 4385 4385 12502500
+4386 4386 4386 12502500
+4387 4387 4387 12502500
+4388 4388 4388 12502500
+4389 4389 4389 12502500
+4390 4390 4390 12502500
+4391 4391 4391 12502500
+4392 4392 4392 12502500
+4393 4393 4393 12502500
+4394 4394 4394 12502500
+4395 4395 4395 12502500
+4396 4396 4396 12502500
+4397 4397 4397 12502500
+4398 4398 4398 12502500
+4399 4399 4399 12502500
+4400 4400 4400 12502500
+4401 4401 4401 12502500
+4402 4402 4402 12502500
+4403 4403 4403 12502500
+4404 4404 4404 12502500
+4405 4405 4405 12502500
+4406 4406 4406 12502500
+4407 4407 4407 12502500
+4408 4408 4408 12502500
+4409 4409 4409 12502500
+4410 4410 4410 12502500
+4411 4411 4411 12502500
+4412 4412 4412 12502500
+4413 4413 4413 12502500
+4414 4414 4414 12502500
+4415 4415 4415 12502500
+4416 4416 4416 12502500
+4417 4417 4417 12502500
+4418 4418 4418 12502500
+4419 4419 4419 12502500
+4420 4420 4420 12502500
+4421 4421 4421 12502500
+4422 4422 4422 12502500
+4423 4423 4423 12502500
+4424 4424 4424 12502500
+4425 4425 4425 12502500
+4426 4426 4426 12502500
+4427 4427 4427 12502500
+4428 4428 4428 12502500
+4429 4429 4429 12502500
+4430 4430 4430 12502500
+4431 4431 4431 12502500
+4432 4432 4432 12502500
+4433 4433 4433 12502500
+4434 4434 4434 12502500
+4435 4435 4435 12502500
+4436 4436 4436 12502500
+4437 4437 4437 12502500
+4438 4438 4438 12502500
+4439 4439 4439 12502500
+4440 4440 4440 12502500
+4441 4441 4441 12502500
+4442 4442 4442 12502500
+4443 4443 4443 12502500
+4444 4444 4444 12502500
+4445 4445 4445 12502500
+4446 4446 4446 12502500
+4447 4447 4447 12502500
+4448 4448 4448 12502500
+4449 4449 4449 12502500
+4450 4450 4450 12502500
+4451 4451 4451 12502500
+4452 4452 4452 12502500
+4453 4453 4453 12502500
+4454 4454 4454 12502500
+4455 4455 4455 12502500
+4456 4456 4456 12502500
+4457 4457 4457 12502500
+4458 4458 4458 12502500
+4459 4459 4459 12502500
+4460 4460 4460 12502500
+4461 4461 4461 12502500
+4462 4462 4462 12502500
+4463 4463 4463 12502500
+4464 4464 4464 12502500
+4465 4465 4465 12502500
+4466 4466 4466 12502500
+4467 4467 4467 12502500
+4468 4468 4468 12502500
+4469 4469 4469 12502500
+4470 4470 4470 12502500
+4471 4471 4471 12502500
+4472 4472 4472 12502500
+4473 4473 4473 12502500
+4474 4474 4474 12502500
+4475 4475 4475 12502500
+4476 4476 4476 12502500
+4477 4477 4477 12502500
+4478 4478 4478 12502500
+4479 4479 4479 12502500
+4480 4480 4480 12502500
+4481 4481 4481 12502500
+4482 4482 4482 12502500
+4483 4483 4483 12502500
+4484 4484 4484 12502500
+4485 4485 4485 12502500
+4486 4486 4486 12502500
+4487 4487 4487 12502500
+4488 4488 4488 12502500
+4489 4489 4489 12502500
+4490 4490 4490 12502500
+4491 4491 4491 12502500
+4492 4492 4492 12502500
+4493 4493 4493 12502500
+4494 4494 4494 12502500
+4495 4495 4495 12502500
+4496 4496 4496 12502500
+4497 4497 4497 12502500
+4498 4498 4498 12502500
+4499 4499 4499 12502500
+4500 4500 4500 12502500
+4501 4501 4501 12502500
+4502 4502 4502 12502500
+4503 4503 4503 12502500
+4504 4504 4504 12502500
+4505 4505 4505 12502500
+4506 4506 4506 12502500
+4507 4507 4507 12502500
+4508 4508 4508 12502500
+4509 4509 4509 12502500
+4510 4510 4510 12502500
+4511 4511 4511 12502500
+4512 4512 4512 12502500
+4513 4513 4513 12502500
+4514 4514 4514 12502500
+4515 4515 4515 12502500
+4516 4516 4516 12502500
+4517 4517 4517 12502500
+4518 4518 4518 12502500
+4519 4519 4519 12502500
+4520 4520 4520 12502500
+4521 4521 4521 12502500
+4522 4522 4522 12502500
+4523 4523 4523 12502500
+4524 4524 4524 12502500
+4525 4525 4525 12502500
+4526 4526 4526 12502500
+4527 4527 4527 12502500
+4528 4528 4528 12502500
+4529 4529 4529 12502500
+4530 4530 4530 12502500
+4531 4531 4531 12502500
+4532 4532 4532 12502500
+4533 4533 4533 12502500
+4534 4534 4534 12502500
+4535 4535 4535 12502500
+4536 4536 4536 12502500
+4537 4537 4537 12502500
+4538 4538 4538 12502500
+4539 4539 4539 12502500
+4540 4540 4540 12502500
+4541 4541 4541 12502500
+4542 4542 4542 12502500
+4543 4543 4543 12502500
+4544 4544 4544 12502500
+4545 4545 4545 12502500
+4546 4546 4546 12502500
+4547 4547 4547 12502500
+4548 4548 4548 12502500
+4549 4549 4549 12502500
+4550 4550 4550 12502500
+4551 4551 4551 12502500
+4552 4552 4552 12502500
+4553 4553 4553 12502500
+4554 4554 4554 12502500
+4555 4555 4555 12502500
+4556 4556 4556 12502500
+4557 4557 4557 12502500
+4558 4558 4558 12502500
+4559 4559 4559 12502500
+4560 4560 4560 12502500
+4561 4561 4561 12502500
+4562 4562 4562 12502500
+4563 4563 4563 12502500
+4564 4564 4564 12502500
+4565 4565 4565 12502500
+4566 4566 4566 12502500
+4567 4567 4567 12502500
+4568 4568 4568 12502500
+4569 4569 4569 12502500
+4570 4570 4570 12502500
+4571 4571 4571 12502500
+4572 4572 4572 12502500
+4573 4573 4573 12502500
+4574 4574 4574 12502500
+4575 4575 4575 12502500
+4576 4576 4576 12502500
+4577 4577 4577 12502500
+4578 4578 4578 12502500
+4579 4579 4579 12502500
+4580 4580 4580 12502500
+4581 4581 4581 12502500
+4582 4582 4582 12502500
+4583 4583 4583 12502500
+4584 4584 4584 12502500
+4585 4585 4585 12502500
+4586 4586 4586 12502500
+4587 4587 4587 12502500
+4588 4588 4588 12502500
+4589 4589 4589 12502500
+4590 4590 4590 12502500
+4591 4591 4591 12502500
+4592 4592 4592 12502500
+4593 4593 4593 12502500
+4594 4594 4594 12502500
+4595 4595 4595 12502500
+4596 4596 4596 12502500
+4597 4597 4597 12502500
+4598 4598 4598 12502500
+4599 4599 4599 12502500
+4600 4600 4600 12502500
+4601 4601 4601 12502500
+4602 4602 4602 12502500
+4603 4603 4603 12502500
+4604 4604 4604 12502500
+4605 4605 4605 12502500
+4606 4606 4606 12502500
+4607 4607 4607 12502500
+4608 4608 4608 12502500
+4609 4609 4609 12502500
+4610 4610 4610 12502500
+4611 4611 4611 12502500
+4612 4612 4612 12502500
+4613 4613 4613 12502500
+4614 4614 4614 12502500
+4615 4615 4615 12502500
+4616 4616 4616 12502500
+4617 4617 4617 12502500
+4618 4618 4618 12502500
+4619 4619 4619 12502500
+4620 4620 4620 12502500
+4621 4621 4621 12502500
+4622 4622 4622 12502500
+4623 4623 4623 12502500
+4624 4624 4624 12502500
+4625 4625 4625 12502500
+4626 4626 4626 12502500
+4627 4627 4627 12502500
+4628 4628 4628 12502500
+4629 4629 4629 12502500
+4630 4630 4630 12502500
+4631 4631 4631 12502500
+4632 4632 4632 12502500
+4633 4633 4633 12502500
+4634 4634 4634 12502500
+4635 4635 4635 12502500
+4636 4636 4636 12502500
+4637 4637 4637 12502500
+4638 4638 4638 12502500
+4639 4639 4639 12502500
+4640 4640 4640 12502500
+4641 4641 4641 12502500
+4642 4642 4642 12502500
+4643 4643 4643 12502500
+4644 4644 4644 12502500
+4645 4645 4645 12502500
+4646 4646 4646 12502500
+4647 4647 4647 12502500
+4648 4648 4648 12502500
+4649 4649 4649 12502500
+4650 4650 4650 12502500
+4651 4651 4651 12502500
+4652 4652 4652 12502500
+4653 4653 4653 12502500
+4654 4654 4654 12502500
+4655 4655 4655 12502500
+4656 4656 4656 12502500
+4657 4657 4657 12502500
+4658 4658 4658 12502500
+4659 4659 4659 12502500
+4660 4660 4660 12502500
+4661 4661 4661 12502500
+4662 4662 4662 12502500
+4663 4663 4663 12502500
+4664 4664 4664 12502500
+4665 4665 4665 12502500
+4666 4666 4666 12502500
+4667 4667 4667 12502500
+4668 4668 4668 12502500
+4669 4669 4669 12502500
+4670 4670 4670 12502500
+4671 4671 4671 12502500
+4672 4672 4672 12502500
+4673 4673 4673 12502500
+4674 4674 4674 12502500
+4675 4675 4675 12502500
+4676 4676 4676 12502500
+4677 4677 4677 12502500
+4678 4678 4678 12502500
+4679 4679 4679 12502500
+4680 4680 4680 12502500
+4681 4681 4681 12502500
+4682 4682 4682 12502500
+4683 4683 4683 12502500
+4684 4684 4684 12502500
+4685 4685 4685 12502500
+4686 4686 4686 12502500
+4687 4687 4687 12502500
+4688 4688 4688 12502500
+4689 4689 4689 12502500
+4690 4690 4690 12502500
+4691 4691 4691 12502500
+4692 4692 4692 12502500
+4693 4693 4693 12502500
+4694 4694 4694 12502500
+4695 4695 4695 12502500
+4696 4696 4696 12502500
+4697 4697 4697 12502500
+4698 4698 4698 12502500
+4699 4699 4699 12502500
+4700 4700 4700 12502500
+4701 4701 4701 12502500
+4702 4702 4702 12502500
+4703 4703 4703 12502500
+4704 4704 4704 12502500
+4705 4705 4705 12502500
+4706 4706 4706 12502500
+4707 4707 4707 12502500
+4708 4708 4708 12502500
+4709 4709 4709 12502500
+4710 4710 4710 12502500
+4711 4711 4711 12502500
+4712 4712 4712 12502500
+4713 4713 4713 12502500
+4714 4714 4714 12502500
+4715 4715 4715 12502500
+4716 4716 4716 12502500
+4717 4717 4717 12502500
+4718 4718 4718 12502500
+4719 4719 4719 12502500
+4720 4720 4720 12502500
+4721 4721 4721 12502500
+4722 4722 4722 12502500
+4723 4723 4723 12502500
+4724 4724 4724 12502500
+4725 4725 4725 12502500
+4726 4726 4726 12502500
+4727 4727 4727 12502500
+4728 4728 4728 12502500
+4729 4729 4729 12502500
+4730 4730 4730 12502500
+4731 4731 4731 12502500
+4732 4732 4732 12502500
+4733 4733 4733 12502500
+4734 4734 4734 12502500
+4735 4735 4735 12502500
+4736 4736 4736 12502500
+4737 4737 4737 12502500
+4738 4738 4738 12502500
+4739 4739 4739 12502500
+4740 4740 4740 12502500
+4741 4741 4741 12502500
+4742 4742 4742 12502500
+4743 4743 4743 12502500
+4744 4744 4744 12502500
+4745 4745 4745 12502500
+4746 4746 4746 12502500
+4747 4747 4747 12502500
+4748 4748 4748 12502500
+4749 4749 4749 12502500
+4750 4750 4750 12502500
+4751 4751 4751 12502500
+4752 4752 4752 12502500
+4753 4753 4753 12502500
+4754 4754 4754 12502500
+4755 4755 4755 12502500
+4756 4756 4756 12502500
+4757 4757 4757 12502500
+4758 4758 4758 12502500
+4759 4759 4759 12502500
+4760 4760 4760 12502500
+4761 4761 4761 12502500
+4762 4762 4762 12502500
+4763 4763 4763 12502500
+4764 4764 4764 12502500
+4765 4765 4765 12502500
+4766 4766 4766 12502500
+4767 4767 4767 12502500
+4768 4768 4768 12502500
+4769 4769 4769 12502500
+4770 4770 4770 12502500
+4771 4771 4771 12502500
+4772 4772 4772 12502500
+4773 4773 4773 12502500
+4774 4774 4774 12502500
+4775 4775 4775 12502500
+4776 4776 4776 12502500
+4777 4777 4777 12502500
+4778 4778 4778 12502500
+4779 4779 4779 12502500
+4780 4780 4780 12502500
+4781 4781 4781 12502500
+4782 4782 4782 12502500
+4783 4783 4783 12502500
+4784 4784 4784 12502500
+4785 4785 4785 12502500
+4786 4786 4786 12502500
+4787 4787 4787 12502500
+4788 4788 4788 12502500
+4789 4789 4789 12502500
+4790 4790 4790 12502500
+4791 4791 4791 12502500
+4792 4792 4792 12502500
+4793 4793 4793 12502500
+4794 4794 4794 12502500
+4795 4795 4795 12502500
+4796 4796 4796 12502500
+4797 4797 4797 12502500
+4798 4798 4798 12502500
+4799 4799 4799 12502500
+4800 4800 4800 12502500
+4801 4801 4801 12502500
+4802 4802 4802 12502500
+4803 4803 4803 12502500
+4804 4804 4804 12502500
+4805 4805 4805 12502500
+4806 4806 4806 12502500
+4807 4807 4807 12502500
+4808 4808 4808 12502500
+4809 4809 4809 12502500
+4810 4810 4810 12502500
+4811 4811 4811 12502500
+4812 4812 4812 12502500
+4813 4813 4813 12502500
+4814 4814 4814 12502500
+4815 4815 4815 12502500
+4816 4816 4816 12502500
+4817 4817 4817 12502500
+4818 4818 4818 12502500
+4819 4819 4819 12502500
+4820 4820 4820 12502500
+4821 4821 4821 12502500
+4822 4822 4822 12502500
+4823 4823 4823 12502500
+4824 4824 4824 12502500
+4825 4825 4825 12502500
+4826 4826 4826 12502500
+4827 4827 4827 12502500
+4828 4828 4828 12502500
+4829 4829 4829 12502500
+4830 4830 4830 12502500
+4831 4831 4831 12502500
+4832 4832 4832 12502500
+4833 4833 4833 12502500
+4834 4834 4834 12502500
+4835 4835 4835 12502500
+4836 4836 4836 12502500
+4837 4837 4837 12502500
+4838 4838 4838 12502500
+4839 4839 4839 12502500
+4840 4840 4840 12502500
+4841 4841 4841 12502500
+4842 4842 4842 12502500
+4843 4843 4843 12502500
+4844 4844 4844 12502500
+4845 4845 4845 12502500
+4846 4846 4846 12502500
+4847 4847 4847 12502500
+4848 4848 4848 12502500
+4849 4849 4849 12502500
+4850 4850 4850 12502500
+4851 4851 4851 12502500
+4852 4852 4852 12502500
+4853 4853 4853 12502500
+4854 4854 4854 12502500
+4855 4855 4855 12502500
+4856 4856 4856 12502500
+4857 4857 4857 12502500
+4858 4858 4858 12502500
+4859 4859 4859 12502500
+4860 4860 4860 12502500
+4861 4861 4861 12502500
+4862 4862 4862 12502500
+4863 4863 4863 12502500
+4864 4864 4864 12502500
+4865 4865 4865 12502500
+4866 4866 4866 12502500
+4867 4867 4867 12502500
+4868 4868 4868 12502500
+4869 4869 4869 12502500
+4870 4870 4870 12502500
+4871 4871 4871 12502500
+4872 4872 4872 12502500
+4873 4873 4873 12502500
+4874 4874 4874 12502500
+4875 4875 4875 12502500
+4876 4876 4876 12502500
+4877 4877 4877 12502500
+4878 4878 4878 12502500
+4879 4879 4879 12502500
+4880 4880 4880 12502500
+4881 4881 4881 12502500
+4882 4882 4882 12502500
+4883 4883 4883 12502500
+4884 4884 4884 12502500
+4885 4885 4885 12502500
+4886 4886 4886 12502500
+4887 4887 4887 12502500
+4888 4888 4888 12502500
+4889 4889 4889 12502500
+4890 4890 4890 12502500
+4891 4891 4891 12502500
+4892 4892 4892 12502500
+4893 4893 4893 12502500
+4894 4894 4894 12502500
+4895 4895 4895 12502500
+4896 4896 4896 12502500
+4897 4897 4897 12502500
+4898 4898 4898 12502500
+4899 4899 4899 12502500
+4900 4900 4900 12502500
+4901 4901 4901 12502500
+4902 4902 4902 12502500
+4903 4903 4903 12502500
+4904 4904 4904 12502500
+4905 4905 4905 12502500
+4906 4906 4906 12502500
+4907 4907 4907 12502500
+4908 4908 4908 12502500
+4909 4909 4909 12502500
+4910 4910 4910 12502500
+4911 4911 4911 12502500
+4912 4912 4912 12502500
+4913 4913 4913 12502500
+4914 4914 4914 12502500
+4915 4915 4915 12502500
+4916 4916 4916 12502500
+4917 4917 4917 12502500
+4918 4918 4918 12502500
+4919 4919 4919 12502500
+4920 4920 4920 12502500
+4921 4921 4921 12502500
+4922 4922 4922 12502500
+4923 4923 4923 12502500
+4924 4924 4924 12502500
+4925 4925 4925 12502500
+4926 4926 4926 12502500
+4927 4927 4927 12502500
+4928 4928 4928 12502500
+4929 4929 4929 12502500
+4930 4930 4930 12502500
+4931 4931 4931 12502500
+4932 4932 4932 12502500
+4933 4933 4933 12502500
+4934 4934 4934 12502500
+4935 4935 4935 12502500
+4936 4936 4936 12502500
+4937 4937 4937 12502500
+4938 4938 4938 12502500
+4939 4939 4939 12502500
+4940 4940 4940 12502500
+4941 4941 4941 12502500
+4942 4942 4942 12502500
+4943 4943 4943 12502500
+4944 4944 4944 12502500
+4945 4945 4945 12502500
+4946 4946 4946 12502500
+4947 4947 4947 12502500
+4948 4948 4948 12502500
+4949 4949 4949 12502500
+4950 4950 4950 12502500
+4951 4951 4951 12502500
+4952 4952 4952 12502500
+4953 4953 4953 12502500
+4954 4954 4954 12502500
+4955 4955 4955 12502500
+4956 4956 4956 12502500
+4957 4957 4957 12502500
+4958 4958 4958 12502500
+4959 4959 4959 12502500
+4960 4960 4960 12502500
+4961 4961 4961 12502500
+4962 4962 4962 12502500
+4963 4963 4963 12502500
+4964 4964 4964 12502500
+4965 4965 4965 12502500
+4966 4966 4966 12502500
+4967 4967 4967 12502500
+4968 4968 4968 12502500
+4969 4969 4969 12502500
+4970 4970 4970 12502500
+4971 4971 4971 12502500
+4972 4972 4972 12502500
+4973 4973 4973 12502500
+4974 4974 4974 12502500
+4975 4975 4975 12502500
+4976 4976 4976 12502500
+4977 4977 4977 12502500
+4978 4978 4978 12502500
+4979 4979 4979 12502500
+4980 4980 4980 12502500
+4981 4981 4981 12502500
+4982 4982 4982 12502500
+4983 4983 4983 12502500
+4984 4984 4984 12502500
+4985 4985 4985 12502500
+4986 4986 4986 12502500
+4987 4987 4987 12502500
+4988 4988 4988 12502500
+4989 4989 4989 12502500
+4990 4990 4990 12502500
+4991 4991 4991 12502500
+4992 4992 4992 12502500
+4993 4993 4993 12502500
+4994 4994 4994 12502500
+4995 4995 4995 12502500
+4996 4996 4996 12502500
+4997 4997 4997 12502500
+4998 4998 4998 12502500
+4999 4999 4999 12502500
+5000 5000 5000 12502500
set @@sort_buffer_size=@save_sort_buffer_size;
drop table t1;
diff --git a/mysql-test/suite/encryption/t/tempfiles_encrypted.test b/mysql-test/suite/encryption/t/tempfiles_encrypted.test
index 96b981c1c06..e2052249ef9 100644
--- a/mysql-test/suite/encryption/t/tempfiles_encrypted.test
+++ b/mysql-test/suite/encryption/t/tempfiles_encrypted.test
@@ -17,6 +17,7 @@ set sort_buffer_size= 2000;
create table t1( a DECIMAL(12,0) DEFAULT NULL, b VARCHAR(20) DEFAULT NULL, c DECIMAL(12,0) DEFAULT NULL)engine=INNODB;
insert into t1 select seq, seq, seq from seq_1_to_5000;
select count(*) from (select a, b, c, ROW_NUMBER() OVER (PARTITION BY a) FROM t1)q;
+select a, b, c, sum(a) OVER () FROM t1;
set @@sort_buffer_size=@save_sort_buffer_size;
diff --git a/mysys/mf_iocache.c b/mysys/mf_iocache.c
index 448a2a42390..cf5daaabc60 100644
--- a/mysys/mf_iocache.c
+++ b/mysys/mf_iocache.c
@@ -320,20 +320,29 @@ int init_io_cache(IO_CACHE *info, File file, size_t cachesize,
int init_slave_io_cache(IO_CACHE *master, IO_CACHE *slave)
{
uchar *slave_buf;
+ size_t buffer_length;
+
DBUG_ASSERT(master->type == READ_CACHE);
DBUG_ASSERT(!master->share);
DBUG_ASSERT(master->alloced_buffer);
- if (!(slave_buf= (uchar*)my_malloc(master->buffer_length, MYF(0))))
+ buffer_length= master->buffer_length;
+ if (master->type == SEQ_READ_APPEND)
+ buffer_length *= 2;
+ else if (master->myflags & MY_ENCRYPT)
+ buffer_length= 2*(buffer_length + MY_AES_BLOCK_SIZE) + sizeof(IO_CACHE_CRYPT);
+
+ if (!(slave_buf= (uchar*)my_malloc(buffer_length, MYF(0))))
{
return 1;
}
memcpy(slave, master, sizeof(IO_CACHE));
slave->buffer= slave_buf;
- memcpy(slave->buffer, master->buffer, master->buffer_length);
+ memcpy(slave->buffer, master->buffer, buffer_length);
slave->read_pos= slave->buffer + (master->read_pos - master->buffer);
slave->read_end= slave->buffer + (master->read_end - master->buffer);
+ slave->request_pos= slave->read_pos;
if (master->next_file_user)
{
diff --git a/sql/sql_window.cc b/sql/sql_window.cc
index abb3937096f..6fc2f65b299 100644
--- a/sql/sql_window.cc
+++ b/sql/sql_window.cc
@@ -2865,6 +2865,8 @@ bool compute_window_func(THD *thd,
save_window_function_values(window_functions, tbl, rowid_buf);
rownum++;
+ if (info.io_cache)
+ info.io_cache->seek_not_done= true;
}
my_free(rowid_buf);
1
0
[Commits] 02fbac7: MDEV-23811: With large number of indexes optimizer chooses an inefficient plan
by IgorBabaev 06 Oct '20
by IgorBabaev 06 Oct '20
06 Oct '20
revision-id: 02fbac71f9b15c5559bbcf0eb54d7a0e0821a6e5 (mariadb-10.2.31-478-g02fbac7)
parent(s): 01ffccd6a4ec2af645b81bcfef8a02f031a492e5
author: Igor Babaev
committer: Igor Babaev
timestamp: 2020-10-06 10:00:29 -0700
message:
MDEV-23811: With large number of indexes optimizer chooses an inefficient plan
This bug could manifest itself for a query with WHERE condition containing
top level OR formula such that each conjunct contained a single-range
condition supported by the same index. One of these range conditions must
be fully covered by another range condition that is used later in the OR
formula. Additionally at least one of these condition should be ANDed with
a sargable range condition supported by a different index.
There were several attempts to fix related problems for OR conditions after
the backport of range optimizer code from MySQL (commit
0e19f3e36f7842583feb6bead2c2600cd620bced). Unfortunately the first of these
fixes contained typo remained unnoticed until recently. This typo bug led
to rejection of valid range accesses. This patch fixed this typo bug.
The fix revealed another two bugs: one in a constructor for SEL_ARG,
the other in the function tree_or(). Both are fixed in this patch.
---
mysql-test/r/range.result | 72 +++++++++++++++++++++++++
mysql-test/r/range_mrr_icp.result | 72 +++++++++++++++++++++++++
mysql-test/r/range_vs_index_merge_innodb.result | 2 +-
mysql-test/t/range.test | 42 +++++++++++++++
sql/opt_range.cc | 10 ++--
5 files changed, 194 insertions(+), 4 deletions(-)
diff --git a/mysql-test/r/range.result b/mysql-test/r/range.result
index 26ea2c6..98328cf 100644
--- a/mysql-test/r/range.result
+++ b/mysql-test/r/range.result
@@ -3054,5 +3054,77 @@ a b
set eq_range_index_dive_limit=default;
drop table t1;
#
+# MDEV-23811: Both disjunct of WHERE condition contain range conditions
+# for the same index such that the second range condition
+# fully covers the first one. Additionally one of the disjuncts
+# contains a range condition for the other index.
+#
+create table t1 (
+pk int primary key auto_increment, a int, b int,
+index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+(5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status OK
+explain select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+drop table t1;
+#
# End of 10.2 tests
#
diff --git a/mysql-test/r/range_mrr_icp.result b/mysql-test/r/range_mrr_icp.result
index fe4eb99..0664897 100644
--- a/mysql-test/r/range_mrr_icp.result
+++ b/mysql-test/r/range_mrr_icp.result
@@ -3066,6 +3066,78 @@ a b
set eq_range_index_dive_limit=default;
drop table t1;
#
+# MDEV-23811: Both disjunct of WHERE condition contain range conditions
+# for the same index such that the second range condition
+# fully covers the first one. Additionally one of the disjuncts
+# contains a range condition for the other index.
+#
+create table t1 (
+pk int primary key auto_increment, a int, b int,
+index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+(5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status OK
+explain select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+drop table t1;
+#
# End of 10.2 tests
#
set optimizer_switch=@mrr_icp_extra_tmp;
diff --git a/mysql-test/r/range_vs_index_merge_innodb.result b/mysql-test/r/range_vs_index_merge_innodb.result
index 581f512..916c30b 100644
--- a/mysql-test/r/range_vs_index_merge_innodb.result
+++ b/mysql-test/r/range_vs_index_merge_innodb.result
@@ -369,7 +369,7 @@ WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 200) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Population,PRIMARY 39,4,4 NULL 307 Using sort_union(Name,Population,PRIMARY); Using where
+1 SIMPLE City range PRIMARY,Population,Country,Name PRIMARY 4 NULL 200 Using where
SELECT * FROM City USE INDEX ()
WHERE ((ID < 10) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
diff --git a/mysql-test/t/range.test b/mysql-test/t/range.test
index 67d876d..0d2fbe2 100644
--- a/mysql-test/t/range.test
+++ b/mysql-test/t/range.test
@@ -2096,6 +2096,48 @@ set eq_range_index_dive_limit=default;
drop table t1;
--echo #
+--echo # MDEV-23811: Both disjunct of WHERE condition contain range conditions
+--echo # for the same index such that the second range condition
+--echo # fully covers the first one. Additionally one of the disjuncts
+--echo # contains a range condition for the other index.
+--echo #
+
+create table t1 (
+ pk int primary key auto_increment, a int, b int,
+ index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+ (5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+
+analyze table t1;
+
+let $q1=
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+eval explain $q1;
+eval $q1;
+
+let $q2=
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+eval explain $q2;
+eval $q2;
+
+let $q3=
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+eval explain $q3;
+eval $q3;
+
+let $q4=
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+eval explain $q4;
+eval $q4;
+
+drop table t1;
+
+--echo #
--echo # End of 10.2 tests
--echo #
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index e933d2a..798b1f2 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -1852,6 +1852,9 @@ SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
next_key_part=arg.next_key_part;
max_part_no= arg.max_part_no;
use_count=1; elements=1;
+ next= 0;
+ if (next_key_part)
+ ++next_key_part->use_count;
}
@@ -9597,10 +9600,11 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
if (!tmp->next_key_part)
{
- if (key2->use_count)
+ SEL_ARG *key2_next= key2->next;
+ if (key2_shared)
{
SEL_ARG *key2_cpy= new SEL_ARG(*key2);
- if (key2_cpy)
+ if (!key2_cpy)
return 0;
key2= key2_cpy;
}
@@ -9621,7 +9625,7 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
Move on to next range in key2
*/
key2->increment_use_count(-1); // Free not used tree
- key2=key2->next;
+ key2=key2_next;
continue;
}
else
1
0
[Commits] 244f5bd: MDEV-23811: With large number of indexes optimizer chooses an inefficient plan
by IgorBabaev 06 Oct '20
by IgorBabaev 06 Oct '20
06 Oct '20
revision-id: 244f5bda2fe9cb5f793f5d0d086c323ac99bc342 (mariadb-10.2.31-478-g244f5bd)
parent(s): 01ffccd6a4ec2af645b81bcfef8a02f031a492e5
author: Igor Babaev
committer: Igor Babaev
timestamp: 2020-10-05 22:07:22 -0700
message:
MDEV-23811: With large number of indexes optimizer chooses an inefficient plan
This bug could manifest itself for a query with WHERE condition containing
top level OR formula such that each conjunct contained a single-range
condition supported by the same index. One of these range conditions must
be fully covered by another range condition that is used later in the OR
formula. Additionally at least one of these condition should be ANDed with
a sargable range condition supported by a different index.
There were several attempts to fix related problems for OR conditions after
the backport of range optimizer code from MySQL (commit
0e19f3e36f7842583feb6bead2c2600cd620bced). Unfortunately the first of these
fixes contained typo remained unnoticed until recently. This typo bug led
to rejection of valid range accesses. This patch fixed this typo bug.
The fix revealed another two bugs: one in a constructor for SEL_ARG,
the other in the function tree_or(). Both are fixed in this patch.
---
mysql-test/r/range.result | 74 ++++++++++++++++++++++++-
mysql-test/r/range_mrr_icp.result | 74 ++++++++++++++++++++++++-
mysql-test/r/range_vs_index_merge_innodb.result | 2 +-
mysql-test/t/range.test | 42 ++++++++++++++
sql/opt_range.cc | 15 +++--
5 files changed, 198 insertions(+), 9 deletions(-)
diff --git a/mysql-test/r/range.result b/mysql-test/r/range.result
index 26ea2c6..7299982 100644
--- a/mysql-test/r/range.result
+++ b/mysql-test/r/range.result
@@ -1274,7 +1274,7 @@ SELECT * FROM t1 WHERE
5 <= a AND b = 3 OR
3 <= a;
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE t1 range a a 5 NULL 4 Using where; Using index
+1 SIMPLE t1 range a a 5 NULL 3 Using where; Using index
SELECT * FROM t1 WHERE
3 <= a AND a <= 5 OR
5 <= a AND b = 3 OR
@@ -3054,5 +3054,77 @@ a b
set eq_range_index_dive_limit=default;
drop table t1;
#
+# MDEV-23811: Both disjunct of WHERE condition contain range conditions
+# for the same index such that the second range condition
+# fully covers the first one. Additionally one of the disjuncts
+# contains a range condition for the other index.
+#
+create table t1 (
+pk int primary key auto_increment, a int, b int,
+index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+(5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status OK
+explain select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+drop table t1;
+#
# End of 10.2 tests
#
diff --git a/mysql-test/r/range_mrr_icp.result b/mysql-test/r/range_mrr_icp.result
index fe4eb99..cdaaa5e 100644
--- a/mysql-test/r/range_mrr_icp.result
+++ b/mysql-test/r/range_mrr_icp.result
@@ -1276,7 +1276,7 @@ SELECT * FROM t1 WHERE
5 <= a AND b = 3 OR
3 <= a;
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE t1 range a a 5 NULL 4 Using where; Using index
+1 SIMPLE t1 range a a 5 NULL 3 Using where; Using index
SELECT * FROM t1 WHERE
3 <= a AND a <= 5 OR
5 <= a AND b = 3 OR
@@ -3066,6 +3066,78 @@ a b
set eq_range_index_dive_limit=default;
drop table t1;
#
+# MDEV-23811: Both disjunct of WHERE condition contain range conditions
+# for the same index such that the second range condition
+# fully covers the first one. Additionally one of the disjuncts
+# contains a range condition for the other index.
+#
+create table t1 (
+pk int primary key auto_increment, a int, b int,
+index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+(5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status OK
+explain select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+drop table t1;
+#
# End of 10.2 tests
#
set optimizer_switch=@mrr_icp_extra_tmp;
diff --git a/mysql-test/r/range_vs_index_merge_innodb.result b/mysql-test/r/range_vs_index_merge_innodb.result
index 581f512..916c30b 100644
--- a/mysql-test/r/range_vs_index_merge_innodb.result
+++ b/mysql-test/r/range_vs_index_merge_innodb.result
@@ -369,7 +369,7 @@ WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 200) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Population,PRIMARY 39,4,4 NULL 307 Using sort_union(Name,Population,PRIMARY); Using where
+1 SIMPLE City range PRIMARY,Population,Country,Name PRIMARY 4 NULL 200 Using where
SELECT * FROM City USE INDEX ()
WHERE ((ID < 10) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
diff --git a/mysql-test/t/range.test b/mysql-test/t/range.test
index 67d876d..0d2fbe2 100644
--- a/mysql-test/t/range.test
+++ b/mysql-test/t/range.test
@@ -2096,6 +2096,48 @@ set eq_range_index_dive_limit=default;
drop table t1;
--echo #
+--echo # MDEV-23811: Both disjunct of WHERE condition contain range conditions
+--echo # for the same index such that the second range condition
+--echo # fully covers the first one. Additionally one of the disjuncts
+--echo # contains a range condition for the other index.
+--echo #
+
+create table t1 (
+ pk int primary key auto_increment, a int, b int,
+ index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+ (5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+
+analyze table t1;
+
+let $q1=
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+eval explain $q1;
+eval $q1;
+
+let $q2=
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+eval explain $q2;
+eval $q2;
+
+let $q3=
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+eval explain $q3;
+eval $q3;
+
+let $q4=
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+eval explain $q4;
+eval $q4;
+
+drop table t1;
+
+--echo #
--echo # End of 10.2 tests
--echo #
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index e933d2a..9f1fb9a 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -1852,6 +1852,9 @@ SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
next_key_part=arg.next_key_part;
max_part_no= arg.max_part_no;
use_count=1; elements=1;
+ next= 0;
+ if (next_key_part)
+ ++next_key_part->use_count;
}
@@ -8899,11 +8902,10 @@ tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
{
SEL_ARG *key1= tree1->keys[key_no];
SEL_ARG *key2= tree2->keys[key_no];
- if (!must_be_ored)
- {
+ if (!must_be_ored || no_merges1)
key1->incr_refs();
+ if (!must_be_ored || no_merges2)
key2->incr_refs();
- }
if ((result->keys[key_no]= key_or(param, key1, key2)))
result->keys_map.set_bit(key_no);
}
@@ -9597,10 +9599,11 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
if (!tmp->next_key_part)
{
- if (key2->use_count)
+ SEL_ARG *key2_next= key2->next;
+ if (key2_shared)
{
SEL_ARG *key2_cpy= new SEL_ARG(*key2);
- if (key2_cpy)
+ if (!key2_cpy)
return 0;
key2= key2_cpy;
}
@@ -9621,7 +9624,7 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
Move on to next range in key2
*/
key2->increment_use_count(-1); // Free not used tree
- key2=key2->next;
+ key2=key2_next;
continue;
}
else
1
0
[Commits] 374d58a: MDEV-23811: With large number of indexes optimizer chooses an inefficient plan
by IgorBabaev 06 Oct '20
by IgorBabaev 06 Oct '20
06 Oct '20
revision-id: 374d58a35a1d888e10b3c44726d0275d48f036fe (mariadb-10.2.31-462-g374d58a)
parent(s): 7c5519c12d46ead947d341cbdcbb6fbbe4d4fe1b
author: Igor Babaev
committer: Igor Babaev
timestamp: 2020-10-05 21:31:05 -0700
message:
MDEV-23811: With large number of indexes optimizer chooses an inefficient plan
This bug could manifest itself for a query with WHERE condition containing
top level OR formula such that each conjunct contained a single-range
condition supported by the same index. One of these range conditions must
be fully covered by another range condition that is used later in the OR
formula. Additionally at least one of these condition should be ANDed with
a sargable range condition supported by a different index.
There were several attempts to fix related problems for OR conditions after
the backport of range optimizer code from MySQL (commit
0e19f3e36f7842583feb6bead2c2600cd620bced). Unfortunately the first of these
fixes contained typo remained unnoticed until recently. This typo bug led
to rejection of valid range accesses. This patch fixed this typo bug.
The fix revealed another two bugs: one in a constructor for SEL_ARG,
the other in the function tree_or(). Both are fixed in this patch.
---
mysql-test/r/range.result | 74 ++++++++++++++++++++++++-
mysql-test/r/range_mrr_icp.result | 74 ++++++++++++++++++++++++-
mysql-test/r/range_vs_index_merge_innodb.result | 2 +-
mysql-test/t/range.test | 42 ++++++++++++++
sql/opt_range.cc | 23 ++++----
5 files changed, 200 insertions(+), 15 deletions(-)
diff --git a/mysql-test/r/range.result b/mysql-test/r/range.result
index 26ea2c6..7299982 100644
--- a/mysql-test/r/range.result
+++ b/mysql-test/r/range.result
@@ -1274,7 +1274,7 @@ SELECT * FROM t1 WHERE
5 <= a AND b = 3 OR
3 <= a;
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE t1 range a a 5 NULL 4 Using where; Using index
+1 SIMPLE t1 range a a 5 NULL 3 Using where; Using index
SELECT * FROM t1 WHERE
3 <= a AND a <= 5 OR
5 <= a AND b = 3 OR
@@ -3054,5 +3054,77 @@ a b
set eq_range_index_dive_limit=default;
drop table t1;
#
+# MDEV-23811: Both disjunct of WHERE condition contain range conditions
+# for the same index such that the second range condition
+# fully covers the first one. Additionally one of the disjuncts
+# contains a range condition for the other index.
+#
+create table t1 (
+pk int primary key auto_increment, a int, b int,
+index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+(5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status OK
+explain select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+explain select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+pk a b
+7 2 20
+71 2 20
+3 3 30
+67 3 30
+6 4 40
+70 4 40
+1 5 50
+65 5 50
+drop table t1;
+#
# End of 10.2 tests
#
diff --git a/mysql-test/r/range_mrr_icp.result b/mysql-test/r/range_mrr_icp.result
index fe4eb99..cdaaa5e 100644
--- a/mysql-test/r/range_mrr_icp.result
+++ b/mysql-test/r/range_mrr_icp.result
@@ -1276,7 +1276,7 @@ SELECT * FROM t1 WHERE
5 <= a AND b = 3 OR
3 <= a;
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE t1 range a a 5 NULL 4 Using where; Using index
+1 SIMPLE t1 range a a 5 NULL 3 Using where; Using index
SELECT * FROM t1 WHERE
3 <= a AND a <= 5 OR
5 <= a AND b = 3 OR
@@ -3066,6 +3066,78 @@ a b
set eq_range_index_dive_limit=default;
drop table t1;
#
+# MDEV-23811: Both disjunct of WHERE condition contain range conditions
+# for the same index such that the second range condition
+# fully covers the first one. Additionally one of the disjuncts
+# contains a range condition for the other index.
+#
+create table t1 (
+pk int primary key auto_increment, a int, b int,
+index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+(5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+analyze table t1;
+Table Op Msg_type Msg_text
+test.t1 analyze status OK
+explain select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+explain select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 range idx1,idx2 idx1 5 NULL 11 Using index condition; Using where; Rowid-ordered scan
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+pk a b
+1 5 50
+3 3 30
+6 4 40
+7 2 20
+65 5 50
+67 3 30
+70 4 40
+71 2 20
+drop table t1;
+#
# End of 10.2 tests
#
set optimizer_switch=@mrr_icp_extra_tmp;
diff --git a/mysql-test/r/range_vs_index_merge_innodb.result b/mysql-test/r/range_vs_index_merge_innodb.result
index 581f512..916c30b 100644
--- a/mysql-test/r/range_vs_index_merge_innodb.result
+++ b/mysql-test/r/range_vs_index_merge_innodb.result
@@ -369,7 +369,7 @@ WHERE ((ID < 200) AND (Name LIKE 'Ha%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 200) AND
(Name LIKE 'Pa%' OR (Population > 103000 AND Population < 104000)));
id select_type table type possible_keys key key_len ref rows Extra
-1 SIMPLE City index_merge PRIMARY,Population,Country,Name Name,Population,PRIMARY 39,4,4 NULL 307 Using sort_union(Name,Population,PRIMARY); Using where
+1 SIMPLE City range PRIMARY,Population,Country,Name PRIMARY 4 NULL 200 Using where
SELECT * FROM City USE INDEX ()
WHERE ((ID < 10) AND (Name LIKE 'H%' OR (Country > 'A' AND Country < 'ARG')))
OR ((ID BETWEEN 100 AND 110) AND
diff --git a/mysql-test/t/range.test b/mysql-test/t/range.test
index 67d876d..0d2fbe2 100644
--- a/mysql-test/t/range.test
+++ b/mysql-test/t/range.test
@@ -2096,6 +2096,48 @@ set eq_range_index_dive_limit=default;
drop table t1;
--echo #
+--echo # MDEV-23811: Both disjunct of WHERE condition contain range conditions
+--echo # for the same index such that the second range condition
+--echo # fully covers the first one. Additionally one of the disjuncts
+--echo # contains a range condition for the other index.
+--echo #
+
+create table t1 (
+ pk int primary key auto_increment, a int, b int,
+ index idx1(a), index idx2(b)
+);
+insert into t1(a,b) values
+ (5,50), (1,10), (3,30), (7,70), (8,80), (4,40), (2,20), (6,60);
+insert into t1(a,b) select a+10, b+100 from t1;
+insert into t1(a,b) select a+20, b+200 from t1;
+insert into t1(a,b) select a+30, b+300 from t1;
+insert into t1(a,b) select a,b from t1;
+
+analyze table t1;
+
+let $q1=
+select * from t1 where ((a between 3 and 4) and b < 100) or (a between 2 and 5);
+eval explain $q1;
+eval $q1;
+
+let $q2=
+select * from t1 where (a between 2 and 5) or ((a between 3 and 4) and b < 100);
+eval explain $q2;
+eval $q2;
+
+let $q3=
+select * from t1 where (a between 3 and 4) or ((a between 2 and 5) and b < 100);
+eval explain $q3;
+eval $q3;
+
+let $q4=
+select * from t1 where ((a between 2 and 5) and b < 100) or (a between 3 and 4);
+eval explain $q4;
+eval $q4;
+
+drop table t1;
+
+--echo #
--echo # End of 10.2 tests
--echo #
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index e933d2a..99b6666 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -1852,6 +1852,9 @@ SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
next_key_part=arg.next_key_part;
max_part_no= arg.max_part_no;
use_count=1; elements=1;
+ next= 0;
+ if (next_key_part)
+ ++next_key_part->use_count;
}
@@ -8869,6 +8872,8 @@ tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
}
bool no_imerge_from_ranges= FALSE;
+ SEL_TREE *rt1= tree1;
+ SEL_TREE *rt2= tree2;
/* Build the range part of the tree for the formula (1) */
if (sel_trees_can_be_ored(param, tree1, tree2, &ored_keys))
{
@@ -8899,11 +8904,10 @@ tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
{
SEL_ARG *key1= tree1->keys[key_no];
SEL_ARG *key2= tree2->keys[key_no];
- if (!must_be_ored)
- {
+ if (!must_be_ored || no_merges1)
key1->incr_refs();
+ if (!must_be_ored || no_merges2)
key2->incr_refs();
- }
if ((result->keys[key_no]= key_or(param, key1, key2)))
result->keys_map.set_bit(key_no);
}
@@ -8929,12 +8933,6 @@ tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
else if (!no_ranges1 && !no_ranges2 && !no_imerge_from_ranges)
{
/* Build the imerge part of the tree for the formula (1) */
- SEL_TREE *rt1= tree1;
- SEL_TREE *rt2= tree2;
- if (no_merges1)
- rt1= new SEL_TREE(tree1, TRUE, param);
- if (no_merges2)
- rt2= new SEL_TREE(tree2, TRUE, param);
if (!rt1 || !rt2 ||
result->merges.push_back(imerge_from_ranges) ||
imerge_from_ranges->or_sel_tree(param, rt1) ||
@@ -9597,10 +9595,11 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
if (!tmp->next_key_part)
{
- if (key2->use_count)
+ SEL_ARG *key2_next= key2->next;
+ if (key2_shared)
{
SEL_ARG *key2_cpy= new SEL_ARG(*key2);
- if (key2_cpy)
+ if (!key2_cpy)
return 0;
key2= key2_cpy;
}
@@ -9621,7 +9620,7 @@ key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
Move on to next range in key2
*/
key2->increment_use_count(-1); // Free not used tree
- key2=key2->next;
+ key2=key2_next;
continue;
}
else
1
0
[Commits] 393ad02: MDEV-18511: CTE support for UPDATE and DELETE statements
by IgorBabaev 02 Oct '20
by IgorBabaev 02 Oct '20
02 Oct '20
revision-id: 393ad0215fa4f1833650c801133ae1806cc63ef4 (mariadb-10.5.2-410-g393ad02)
parent(s): d34cc6b3fd995ea7f10745d3b8e730053026dca2
author: Igor Babaev
committer: Igor Babaev
timestamp: 2020-10-01 15:47:26 -0700
message:
MDEV-18511: CTE support for UPDATE and DELETE statements
MDEV-23552: Merge mergeable derived tables used at the top level of UPDATE
statements
This is the first preliminary patch for the tasks.
The patch introduces the new test files derived_update.test,
cte_update.test, view_update.test, derived_update_multi.test,
cte_update_multi.test, view_update_multi.test. All these tests +
view.test, derived.test, derived_view.test, cte_nonrecursive and
cte recursive pass with the patch.
The code of the patch is far from being final.
---
mysql-test/main/cte_nonrecursive.result | 9 +-
mysql-test/main/cte_nonrecursive.test | 2 +-
mysql-test/main/cte_update.result | 432 +++++
mysql-test/main/cte_update.test | 221 +++
mysql-test/main/cte_update_multi.result | 2643 ++++++++++++++++++++++++++
mysql-test/main/cte_update_multi.test | 916 +++++++++
mysql-test/main/derived_update.result | 440 +++++
mysql-test/main/derived_update.test | 226 +++
mysql-test/main/derived_update_multi.result | 2529 +++++++++++++++++++++++++
mysql-test/main/derived_update_multi.test | 860 +++++++++
mysql-test/main/view.result | 24 +-
mysql-test/main/view.test | 21 +-
mysql-test/main/view_update.result | 412 ++++
mysql-test/main/view_update.test | 241 +++
mysql-test/main/view_update_multi.result | 2685 +++++++++++++++++++++++++++
mysql-test/main/view_update_multi.test | 970 ++++++++++
sql/item_subselect.cc | 1 -
sql/sp_head.cc | 3 +-
sql/sql_base.cc | 55 +-
sql/sql_class.h | 13 +-
sql/sql_cte.cc | 396 +++-
sql/sql_cte.h | 69 +-
sql/sql_delete.cc | 13 +-
sql/sql_derived.cc | 56 +-
sql/sql_insert.cc | 23 +-
sql/sql_lex.cc | 18 +-
sql/sql_lex.h | 32 +-
sql/sql_load.cc | 2 +-
sql/sql_parse.cc | 19 +-
sql/sql_prepare.cc | 7 +-
sql/sql_select.cc | 23 +-
sql/sql_show.cc | 2 +
sql/sql_tvc.cc | 4 +-
sql/sql_update.cc | 151 +-
sql/sql_view.cc | 13 +-
sql/sql_yacc.yy | 76 +-
sql/table.cc | 261 ++-
sql/table.h | 37 +-
38 files changed, 13472 insertions(+), 433 deletions(-)
diff --git a/mysql-test/main/cte_nonrecursive.result b/mysql-test/main/cte_nonrecursive.result
index 6ff33e6..b7bb143 100644
--- a/mysql-test/main/cte_nonrecursive.result
+++ b/mysql-test/main/cte_nonrecursive.result
@@ -618,7 +618,7 @@ with t(c) as (select a from t1 where b >= 'c')
select * from t r1, t r2 where r1.c=r2.c and r2.c=4;
show create view v4;
View Create View character_set_client collation_connection
-v4 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS with t(`c`) as (select `test`.`t1`.`a` AS `c` from `test`.`t1` where `test`.`t1`.`b` >= 'c')select `r1`.`c` AS `c`,`r2`.`c` AS `d` from (`t` `r1` join (select `test`.`t1`.`a` AS `c` from `test`.`t1` where `test`.`t1`.`b` >= 'c') `r2`) where `r1`.`c` = `r2`.`c` and `r2`.`c` = 4 latin1 latin1_swedish_ci
+v4 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS with t(`c`) as (select `t1`.`a` AS `c` from `t1` where `t1`.`b` >= 'c')select `r1`.`c` AS `c`,`r2`.`c` AS `d` from (`t` `r1` join (select `t1`.`a` AS `c` from `t1` where `t1`.`b` >= 'c') `r2`) where `r1`.`c` = `r2`.`c` and `r2`.`c` = 4 latin1 latin1_swedish_ci
select * from v4;
c d
4 4
@@ -636,7 +636,6 @@ create view v1(a) as
with t as (select a from t1 where b >= 'c')
select t.a from t2,t where t2.c=t.a;
update v1 set a=0 where a > 4;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
# prepare of a query containing a definition of a with table t
prepare stmt1 from "
@@ -1021,10 +1020,10 @@ select a from t1;
show table status;
show create view v1;
View Create View character_set_client collation_connection
-v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with t as (select `s`.`a` AS `a` from (select `test`.`t1`.`a` AS `a` from `test`.`t1`) `s`), r as (select `t`.`a` AS `a` from (`test`.`t2` join `t`) where `test`.`t2`.`b` = `t`.`a`)select `r`.`a` AS `a` from `r` latin1 latin1_swedish_ci
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with t as (select `s`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `s`), r as (select `t`.`a` AS `a` from (`t2` join `t`) where `t2`.`b` = `t`.`a`)select `r`.`a` AS `a` from `r` latin1 latin1_swedish_ci
show create view v2;
View Create View character_set_client collation_connection
-v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with t as (select `s`.`a` AS `a` from (select `test`.`t1`.`a` AS `a` from `test`.`t1`) `s`), r as (select `t`.`a` AS `a` from (`test`.`t2` join `t`) where `test`.`t2`.`b` = `t`.`a`)select `test`.`t1`.`a` AS `a` from `test`.`t1` latin1 latin1_swedish_ci
+v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with t as (select `s`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `s`), r as (select `t`.`a` AS `a` from (`t2` join `t`) where `t2`.`b` = `t`.`a`)select `t1`.`a` AS `a` from `t1` latin1 latin1_swedish_ci
select * from v1;
a
2
@@ -1126,7 +1125,7 @@ NULL UNION RESULT <union4,5> ALL NULL NULL NULL NULL NULL NULL
NULL UNION RESULT <union11,12> ALL NULL NULL NULL NULL NULL NULL
NULL UNION RESULT <union1,6> ALL NULL NULL NULL NULL NULL NULL
Warnings:
-Note 1003 with cte_e as (with cte_o as (with cte_i as (select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1 union select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1)select `cte_e1`.`a` AS `a` from `cte_e` `cte_e1` where `cte_e1`.`a` > 1 union select `cte_e2`.`a` AS `a` from (with cte_o as (with cte_i as (select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 union select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t
1`.`a` > 1 and `test`.`t1`.`a` < 7) `cte_e2`
+Note 1003 with cte_e as (with cte_o as (with cte_i as (/* select#2 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)/* select#3 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)/* select#4 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1 union /* select#5 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1)/* select#1 */ select `cte_e1`.`a` AS `a` from `cte_e` `cte_e1` where `cte_e1`.`a` > 1 union /* select#6 */ select `cte_e2`.`a` AS `a` from (with cte_o as (with cte_i as (/* select#9 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)/* select#10 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)/* select#11 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and
`test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 union /* select#12 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7) `cte_e2`
drop table t1;
#
# MDEV-13753: embedded CTE in a VIEW created in prepared statement
diff --git a/mysql-test/main/cte_nonrecursive.test b/mysql-test/main/cte_nonrecursive.test
index 807e2e0..4fd3cd9 100644
--- a/mysql-test/main/cte_nonrecursive.test
+++ b/mysql-test/main/cte_nonrecursive.test
@@ -345,7 +345,7 @@ drop view v1,v2,v3,v4;
create view v1(a) as
with t as (select a from t1 where b >= 'c')
select t.a from t2,t where t2.c=t.a;
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set a=0 where a > 4;
drop view v1;
diff --git a/mysql-test/main/cte_update.result b/mysql-test/main/cte_update.result
new file mode 100644
index 0000000..e675781
--- /dev/null
+++ b/mysql-test/main/cte_update.result
@@ -0,0 +1,432 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+# full update of mergeable cte1
+explain with cte1 as (select a from t1) update cte1 set a=10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+with cte1 as (select a from t1) update cte1 set a=10;
+select * from t1;
+a
+10
+10
+10
+10
+10
+explain with cte1 as (select * from t1) update cte1 set a=14;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+with cte1 as (select * from t1) update cte1 set a=14;
+select * from t1;
+a
+14
+14
+14
+14
+14
+# update with limit of mergeable cte1
+explain with cte1 as (select * from t1) update cte1 set cte1.a=18 limit 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+with cte1 as (select * from t1) update cte1 set cte1.a=18 limit 3;
+select * from t1;
+a
+18
+18
+18
+14
+14
+# partial update of mergeable cte1
+explain with cte1 as (select * from t1) update cte1 set cte1.a=2 where a<18;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from t1) update cte1 set cte1.a=2 where a<18;
+select * from t1;
+a
+18
+18
+18
+2
+2
+# partial update of mergeable cte1 defined as
+# full select from mergeable derived table dt1
+explain with cte1 as (select * from (select * from t1) dt1)
+update cte1 set cte1.a=22 where a>16;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from (select * from t1) dt1)
+update cte1 set cte1.a=22 where a>16;
+select * from t1;
+a
+22
+22
+22
+2
+2
+# full update of mergeable cte1 defined as
+# partial select from mergeable derived table dt1
+explain with cte1 as (select * from (select * from t1) dt1 where dt1.a > 20)
+update cte1 set cte1.a=24;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from (select * from t1) dt1 where dt1.a > 20)
+update cte1 set cte1.a=24;
+select * from t1;
+a
+24
+24
+24
+2
+2
+update t1 set a=26 limit 1;
+select * from t1;
+a
+26
+24
+24
+2
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable derived table dt1
+explain with cte1 as (select * from (select * from t1) dt1 where dt1.a < 25)
+update cte1 set cte1.a=15 where cte1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from (select * from t1) dt1 where dt1.a < 25)
+update cte1 set cte1.a=15 where cte1.a > 2;
+select * from t1;
+a
+26
+15
+15
+2
+2
+create view v1 as select * from t1;
+# partial update of mergeable cte1 defined as
+# full select from mergeable view v1
+explain with cte1 as (select * from v1)
+update cte1 set cte1.a=17 where cte1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from v1)
+update cte1 set cte1.a=17 where cte1.a between 10 and 20;
+select * from t1;
+a
+26
+17
+17
+2
+2
+# full update of mergeable cte1 defined as
+# partial select from mergeable view v1
+explain with cte1 as (select * from v1 where v1.a > 10)
+update cte1 set cte1.a=23;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from v1 where v1.a > 10)
+update cte1 set cte1.a=23;
+select * from t1;
+a
+23
+23
+23
+2
+2
+update t1 set a=28 limit 1;
+select * from t1;
+a
+28
+23
+23
+2
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable view v1
+explain with cte1 as (select * from v1 where v1.a < 27)
+update cte1 set cte1.a=19 where cte1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from v1 where v1.a < 27)
+update cte1 set cte1.a=19 where cte1.a > 2;
+select * from t1;
+a
+28
+19
+19
+2
+2
+# partial update of mergeable cte2 defined as
+# full select from mergeable embedded cte1
+explain with cte2 as (with cte1 as (select * from t1) select * from cte1)
+update cte2 set cte2.a=11 where cte2.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte2 as (with cte1 as (select * from t1) select * from cte1)
+update cte2 set cte2.a=11 where cte2.a between 10 and 20;
+select * from t1;
+a
+28
+11
+11
+2
+2
+# full update of mergeable cte2 defined as
+# partial select from mergeable embedded cte1
+explain with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10)
+update cte2 set cte2.a=21;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10)
+update cte2 set cte2.a=21;
+select * from t1;
+a
+21
+21
+21
+2
+2
+update t1 set a=29 limit 1;
+select * from t1;
+a
+29
+21
+21
+2
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable embedded cte1
+explain with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27)
+update cte2 set cte2.a=13 where cte2.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27)
+update cte2 set cte2.a=13 where cte2.a > 2;
+select * from t1;
+a
+29
+13
+13
+2
+2
+drop view v1;
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+# partial update of mergeable cte1 defined as
+# partial select from mergeable dt2 that uses mergeable dt1
+explain with cte1 as
+(select *
+from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 6;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as
+(select *
+from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 6;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+6
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable dt1 that uses mergeable cte1
+explain with cte2 as
+(select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 5 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as
+(select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 5 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+5
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+explain with cte1 as
+(select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as
+(select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 2 and 5;
+select * from t1;
+a
+4
+8
+6
+1
+5
+4
+6
+2
+drop view v1;
+# partial update of mergeable cte2 defined as
+# partial select from mergeable cte1 that uses mergeable dt1
+explain with cte2 as
+(with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as
+(with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 4 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+6
+2
+# partial update of mergeable cte3 defined as
+# partial select from mergeable cte2 that uses mergeable cte1
+explain with cte3 as
+(with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10)
+update cte3 set cte3.a=cte3.a+1 where cte3.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte3 as
+(with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10)
+update cte3 set cte3.a=cte3.a+1 where cte3.a between 4 and 7;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+7
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+explain with cte2 as
+(with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 4;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as
+(with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 4;
+select * from t1;
+a
+2
+8
+7
+1
+5
+2
+7
+1
+drop view v1;
+# partial update of mergeable cte1 defined as
+# partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+explain with cte1 as (select * from v1 where v1.a < 6)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as (select * from v1 where v1.a < 6)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 5;
+select * from t1;
+a
+2
+8
+7
+1
+6
+2
+7
+1
+drop view v1;
+# partial update of mergeable cte2 defined as
+# partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+explain with cte2 as (select * from v1 where v1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as (select * from v1 where v1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 5;
+select * from t1;
+a
+1
+8
+7
+1
+6
+1
+7
+1
+drop view v1;
+# partial update of mergeable cte1 defined as
+# partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+explain with cte1 as (select * from v2 where v2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 5 and 10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as (select * from v2 where v2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 5 and 10;
+select * from t1;
+a
+1
+8
+8
+1
+7
+1
+8
+1
+drop view v2;
+drop view v1;
+drop table t1;
diff --git a/mysql-test/main/cte_update.test b/mysql-test/main/cte_update.test
new file mode 100644
index 0000000..a6db27d
--- /dev/null
+++ b/mysql-test/main/cte_update.test
@@ -0,0 +1,221 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+--echo # full update of mergeable cte1
+let $q=
+with cte1 as (select a from t1) update cte1 set a=10;
+eval explain $q;
+eval $q;
+select * from t1;
+let $q=
+with cte1 as (select * from t1) update cte1 set a=14;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # update with limit of mergeable cte1
+let $q=
+with cte1 as (select * from t1) update cte1 set cte1.a=18 limit 3;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte1
+let $q=
+with cte1 as (select * from t1) update cte1 set cte1.a=2 where a<18;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # full select from mergeable derived table dt1
+let $q=
+with cte1 as (select * from (select * from t1) dt1)
+ update cte1 set cte1.a=22 where a>16;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable cte1 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+with cte1 as (select * from (select * from t1) dt1 where dt1.a > 20)
+ update cte1 set cte1.a=24;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=26 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+with cte1 as (select * from (select * from t1) dt1 where dt1.a < 25)
+ update cte1 set cte1.a=15 where cte1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+create view v1 as select * from t1;
+
+--echo # partial update of mergeable cte1 defined as
+--echo # full select from mergeable view v1
+let $q=
+with cte1 as (select * from v1)
+ update cte1 set cte1.a=17 where cte1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable cte1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+with cte1 as (select * from v1 where v1.a > 10)
+ update cte1 set cte1.a=23;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=28 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+with cte1 as (select * from v1 where v1.a < 27)
+ update cte1 set cte1.a=19 where cte1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # full select from mergeable embedded cte1
+let $q=
+with cte2 as (with cte1 as (select * from t1) select * from cte1)
+ update cte2 set cte2.a=11 where cte2.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable cte2 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+with cte2 as (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 10)
+ update cte2 set cte2.a=21;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=29 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+with cte2 as (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 27)
+ update cte2 set cte2.a=13 where cte2.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+drop view v1;
+
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable dt2 that uses mergeable dt1
+let $q=
+with cte1 as
+ (select *
+ from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+ where dt2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 6;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable cte1
+let $q=
+with cte2 as
+ (select *
+ from (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 8) dt1
+ where dt1.a > 1)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 5 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+let $q=
+with cte1 as
+ (select *
+ from (select * from v1 where v1.a < 8) dt1
+ where dt1.a > 2)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable dt1
+let $q=
+with cte2 as
+ (with cte1 as
+ (select * from (select * from t1) dt1 where dt1.a > 2)
+ select * from cte1 where cte1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte3 defined as
+--echo # partial select from mergeable cte2 that uses mergeable cte1
+let $q=
+with cte3 as
+ (with cte2 as
+ (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10)
+update cte3 set cte3.a=cte3.a+1 where cte3.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+let $q=
+with cte2 as
+ (with cte1 as
+ (select * from v1 where v1.a > 1)
+ select * from cte1 where cte1.a < 7)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 4;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+let $q=
+with cte1 as (select * from v1 where v1.a < 6)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+let $q=
+with cte2 as (select * from v1 where v1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+let $q=
+with cte1 as (select * from v2 where v2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 5 and 10;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+
+drop table t1;
diff --git a/mysql-test/main/cte_update_multi.result b/mysql-test/main/cte_update_multi.result
new file mode 100644
index 0000000..c7af6d3
--- /dev/null
+++ b/mysql-test/main/cte_update_multi.result
@@ -0,0 +1,2643 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and mergeable derived table dt1
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6) as dt1
+where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6) as dt1
+where cte1.a=dt1.b;
+a b
+5 5
+3 3
+3 3
+2 2
+2 2
+4 4
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where cte1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# multi-table update of mergeable cte1
+# and mergeable cte2
+explain with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+select * from cte1, cte2 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+select * from cte1, cte2 where cte1.a=cte2.b;
+a b
+6 6
+4 4
+4 4
+3 3
+3 3
+5 5
+6 6
+explain with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+update cte1, cte2
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+update cte1, cte2
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1 where cte1.a=cte2.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# multi-table update of mergeable cte1
+# and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+a b
+3 3
+3 3
+2 2
+2 2
+4 4
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2, v1.b=v1.b+2 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2, v1.b=v1.b+2 where cte1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+5
+9
+4
+4
+6
+5
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and non-mergeable derived table dt1
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6 group by b) as dt1
+where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6 group by b) as dt1
+where cte1.a=dt1.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where cte1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and non-mergeable cte2
+explain with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+update cte1, cte2
+set cte1.a=cte1.a-1 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+update cte1, cte2
+set cte1.a=cte1.a-1 where cte1.a=cte2.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2 where cte1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and mergeable derived table dt2
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# update of mergeable cte2 specified as join of
+# mergeable derived table dt1 and mergeable cte1
+explain with cte2 as
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1, cte2.b=cte2.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte2 as
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1, cte2.b=cte2.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable cte3 specified as join of
+# mergeable cte1 and mergeable cte2
+explain with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a-1, cte3.b=cte3.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a-1, cte3.b=cte3.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+4
+10
+3
+9
+2
+2
+4
+4
+1
+# update of mergeable cte2 specified as join of
+# mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a+1, cte2.b=cte2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a+1, cte2.b=cte2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable cte1 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+explain with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a-1, cte1.b=cte1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a-1, cte1.b=cte1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+5
+5
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and non-mergeable derived table dt2
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte2 specified as join of
+# mergeable derived table dt1 and non-mergeable cte1
+explain with cte2 as
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte2 specified as join of
+# mergeable cte1 and non-mergeable dt1
+explain with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte3 specified as join of
+# mergeable cte1 and non-mergeable cte2
+explain with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte2 specified as join of
+# mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+explain with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte1 specified as join of
+# mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte2 specified as join of
+# mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte2 as
+(with cte1 as (select b from t2 where b < 7 group by b)
+select * from v1, cte1 where v1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select b from t2 where b < 7 group by b)
+select * from v1, cte1 where v1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte1 specified as join of
+# mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+explain with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable derived table dt1 and mergeable derived table dt2
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable derived table dt1 and mergeable cte2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+set cte1.a=cte1.a-1, dt1.b=dt1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+set cte1.a=cte1.a-1, dt1.b=dt1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable cte2 and mergeable derived table dt1
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable cte2 and mergeable cte3
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5);
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, v1.b=v1.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, v1.b=v1.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable cte1,
+# mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10;
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, v1.b=v1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, v1.b=v1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable cte1,
+# mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable derived table dt1 and mergeable derived table dt2
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable derived table dt1 and mergeable cte2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# non-mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable cte2 and mergeable derived table dt1
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable cte2 and mergeable cte3
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# non-mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable cte1,
+# non-mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10 group by b;
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable cte1,
+# non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop table t1,t2,t3;
diff --git a/mysql-test/main/cte_update_multi.test b/mysql-test/main/cte_update_multi.test
new file mode 100644
index 0000000..77527bc
--- /dev/null
+++ b/mysql-test/main/cte_update_multi.test
@@ -0,0 +1,916 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and mergeable derived table dt1
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6) as dt1
+where cte1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where cte1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and mergeable cte2
+let $qs=
+with
+ cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9)
+select * from cte1, cte2 where cte1.a=cte2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9)
+update cte1, cte2
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1 where cte1.a=cte2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2, v1.b=v1.b+2 where cte1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and non-mergeable derived table dt1
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6 group by b) as dt1
+where cte1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where cte1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and non-mergeable cte2
+let $qs=
+with cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9 group by b)
+update cte1, cte2
+set cte1.a=cte1.a-1 where cte1.a=cte2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2 where cte1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+ where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable derived table dt1 and mergeable cte1
+let $qu=
+with cte2 as
+ (with cte1 as (select b from t2 where b < 9)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1, cte2.b=cte2.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte3 specified as join of
+--echo # mergeable cte1 and mergeable cte2
+let $qu=
+with cte3 as
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6)
+ select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a-1, cte3.b=cte3.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+with cte2 as
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a+1, cte2.b=cte2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+let $qu=
+with cte1 as
+ (select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a-1, cte1.b=cte1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable derived table dt2
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+ where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable cte1
+let $qu=
+with cte2 as
+ (with cte1 as (select b from t2 where b < 9 group by b)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable cte1 and non-mergeable dt1
+let $qu=
+with cte2 as
+ (with cte1 as (select a from t1 where a > 1)
+ select * from
+ cte1, (select b from t2 where b < 7 group by b) dt1
+ where cte1.a=dt1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte3 specified as join of
+--echo # mergeable cte1 and non-mergeable cte2
+let $qu=
+with cte3 as
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6 group by b)
+ select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+let $qu=
+with cte2 as
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+with cte1 as
+ (select * from
+ v1, (select b from t2 where b < 6 group by b) dt1
+ where v1.a=dt1.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+with cte2 as
+ (with cte1 as (select b from t2 where b < 7 group by b)
+ select * from v1, cte1 where v1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+let $qu=
+with cte1 as
+ (select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable derived table dt1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte2
+set cte1.a=cte1.a-1, dt1.b=dt1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ v1
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # mergeable cte2 and mergeable derived table dt1
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5)
+update
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable cte2 and mergeable cte3
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ cte2,
+ cte3
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5)
+update
+ cte1,
+ cte2,
+ v1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5);
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, v1.b=v1.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10;
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ v1,
+ cte2
+set cte1.a=cte1.a-1, v1.b=v1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ v2
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ v1,
+ v2
+set cte1.a=cte1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable derived table dt1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable derived table dt1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable cte2 and mergeable derived table dt1
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable cte2 and mergeable cte3
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10 group by b),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10 group by b),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ cte2,
+ cte3
+set cte1.a=cte1.a-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+ cte1,
+ cte2,
+ v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10 group by b;
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ v1,
+ cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ v2
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ v1,
+ v2
+set cte1.a=cte1.a+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+drop table t1,t2,t3;
diff --git a/mysql-test/main/derived_update.result b/mysql-test/main/derived_update.result
new file mode 100644
index 0000000..676cb7c
--- /dev/null
+++ b/mysql-test/main/derived_update.result
@@ -0,0 +1,440 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+# full update of mergeable derived table dt1
+explain update (select a from t1) as dt1 set a=10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update (select a from t1) as dt1 set a=10;
+select * from t1;
+a
+10
+10
+10
+10
+10
+explain update (select * from t1) as dt1 set a=14;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update (select * from t1) as dt1 set a=14;
+select * from t1;
+a
+14
+14
+14
+14
+14
+# update with limit of mergeable dt1
+explain update (select * from t1) as dt1 set dt1.a=18 limit 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update (select * from t1) as dt1 set dt1.a=18 limit 3;
+select * from t1;
+a
+18
+18
+18
+14
+14
+# partial update of mergeable dt1
+explain update (select * from t1) as dt1 set dt1.a=2 where a < 18;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from t1) as dt1 set dt1.a=2 where a < 18;
+select * from t1;
+a
+18
+18
+18
+2
+2
+# partial update of mergeable dt2 defined as
+# full select from mergeable derived table dt1
+explain update (select * from (select * from t1) as d1t) as dt2
+set dt2.a=22 where a > 16;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from (select * from t1) as d1t) as dt2
+set dt2.a=22 where a > 16;
+select * from t1;
+a
+22
+22
+22
+2
+2
+# full update of mergeable dt2 defined as
+# partial select from mergeable derived table dt1
+explain update (select * from (select * from t1) as dt1 where dt1.a > 20) as dt2
+set dt2.a=24;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from (select * from t1) as dt1 where dt1.a > 20) as dt2
+set dt2.a=24;
+select * from t1;
+a
+24
+24
+24
+2
+2
+update t1 set a=26 limit 1;
+select * from t1;
+a
+26
+24
+24
+2
+2
+# partial update of mergeable dt2 defined as
+# partial select from mergeable derived table dt1
+explain update (select * from (select * from t1) as dt1 where dt1.a < 25) as dt2
+set dt2.a=15 where dt2.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from (select * from t1) as dt1 where dt1.a < 25) as dt2
+set dt2.a=15 where dt2.a > 2;
+select * from t1;
+a
+26
+15
+15
+2
+2
+create view v1 as select * from t1;
+# partial update of mergeable dt1 defined as
+# full select from mergeable view v1
+explain update (select * from v1) dt1
+set dt1.a=17 where dt1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from v1) dt1
+set dt1.a=17 where dt1.a between 10 and 20;
+select * from t1;
+a
+26
+17
+17
+2
+2
+# full update of mergeable dt1 defined as
+# partial select from mergeable view v1
+explain update (select * from v1 where v1.a > 10) as dt1
+set dt1.a=23;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from v1 where v1.a > 10) as dt1
+set dt1.a=23;
+select * from t1;
+a
+23
+23
+23
+2
+2
+update t1 set a=28 limit 1;
+select * from t1;
+a
+28
+23
+23
+2
+2
+# partial update of mergeable dt1 defined as
+# partial select from mergeable view v1
+explain update (select * from v1 where v1.a < 27) as dt1
+set dt1.a=19 where dt1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from v1 where v1.a < 27) as dt1
+set dt1.a=19 where dt1.a > 2;
+select * from t1;
+a
+28
+19
+19
+2
+2
+# partial update of mergeable dt1 defined as
+# full select from mergeable embedded cte1
+explain update (with cte1 as (select * from t1) select * from cte1) dt1
+set dt1.a=11 where dt1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (with cte1 as (select * from t1) select * from cte1) dt1
+set dt1.a=11 where dt1.a between 10 and 20;
+select * from t1;
+a
+28
+11
+11
+2
+2
+# full update of mergeable dt1 defined as
+# partial select from mergeable embedded cte1
+explain update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10) dt1
+set dt1.a=21;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10) dt1
+set dt1.a=21;
+select * from t1;
+a
+21
+21
+21
+2
+2
+update t1 set a=29 limit 1;
+select * from t1;
+a
+29
+21
+21
+2
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable embedded cte1
+explain update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27) dt1
+set dt1.a=13 where dt1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27) dt1
+set dt1.a=13 where dt1.a > 2;
+select * from t1;
+a
+29
+13
+13
+2
+2
+drop view v1;
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+# partial update of mergeable dt3 defined as
+# partial select from mergeable dt2 that uses mergeable dt1
+explain update (select *
+from (select *
+from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8) dt3
+set dt3.a=dt3.a+1
+where dt3.a between 4 and 6;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select *
+from (select *
+from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8) dt3
+set dt3.a=dt3.a+1
+where dt3.a between 4 and 6;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+6
+2
+# partial update of mergeable dt2 defined as
+# partial select from mergeable dt1 that uses mergeable cte1
+explain update (select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 5 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 5 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+5
+2
+# partial update of mergeable dt2 defined as
+# partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+explain update (select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 2 and 5;
+select * from t1;
+a
+4
+8
+6
+1
+5
+4
+6
+2
+drop view v1;
+# partial update of mergeable dt2 defined as
+# partial select from mergeable cte1 that uses mergeable dt1
+explain update (with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 4 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+6
+2
+# partial update of mergeable dt1 defined as
+# partial select from mergeable cte2 that uses mergeable cte1
+explain update (with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 4 and 7;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+7
+2
+# partial update of mergeable dt1 defined as
+# partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+explain update (with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 4;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 4;
+select * from t1;
+a
+2
+8
+7
+1
+5
+2
+7
+1
+drop view v1;
+# partial update of mergeable dt2 defined as
+# partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+explain update (select * from v1 where v1.a < 6) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 4 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select * from v1 where v1.a < 6) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 4 and 5;
+select * from t1;
+a
+2
+8
+7
+1
+6
+2
+7
+1
+drop view v1;
+# partial update of mergeable dt1 defined as
+# partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+explain update (select * from v1 where v1.a < 6) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select * from v1 where v1.a < 6) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 5;
+select * from t1;
+a
+1
+8
+7
+1
+6
+1
+7
+1
+drop view v1;
+# partial update of mergeable dt1 defined as
+# partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+explain update (select * from v2 where v2.a < 8) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 5 and 10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select * from v2 where v2.a < 8) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 5 and 10;
+select * from t1;
+a
+1
+8
+8
+1
+7
+1
+8
+1
+drop view v2;
+drop view v1;
+drop table t1;
diff --git a/mysql-test/main/derived_update.test b/mysql-test/main/derived_update.test
new file mode 100644
index 0000000..58984d0
--- /dev/null
+++ b/mysql-test/main/derived_update.test
@@ -0,0 +1,226 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+
+--echo # full update of mergeable derived table dt1
+let $q=
+update (select a from t1) as dt1 set a=10;
+eval explain $q;
+eval $q;
+select * from t1;
+let $q=
+update (select * from t1) as dt1 set a=14;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # update with limit of mergeable dt1
+let $q=
+update (select * from t1) as dt1 set dt1.a=18 limit 3;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1
+let $q=
+update (select * from t1) as dt1 set dt1.a=2 where a < 18;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # full select from mergeable derived table dt1
+let $q=
+update (select * from (select * from t1) as d1t) as dt2
+ set dt2.a=22 where a > 16;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable dt2 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+update (select * from (select * from t1) as dt1 where dt1.a > 20) as dt2
+ set dt2.a=24;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=26 limit 1;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+update (select * from (select * from t1) as dt1 where dt1.a < 25) as dt2
+ set dt2.a=15 where dt2.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+create view v1 as select * from t1;
+
+--echo # partial update of mergeable dt1 defined as
+--echo # full select from mergeable view v1
+let $q=
+update (select * from v1) dt1
+ set dt1.a=17 where dt1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable dt1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+update (select * from v1 where v1.a > 10) as dt1
+ set dt1.a=23;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=28 limit 1;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+update (select * from v1 where v1.a < 27) as dt1
+ set dt1.a=19 where dt1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # full select from mergeable embedded cte1
+let $q=
+update (with cte1 as (select * from t1) select * from cte1) dt1
+ set dt1.a=11 where dt1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable dt1 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+update (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 10) dt1
+ set dt1.a=21;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=29 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+update (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 27) dt1
+ set dt1.a=13 where dt1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+drop view v1;
+
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+
+--echo # partial update of mergeable dt3 defined as
+--echo # partial select from mergeable dt2 that uses mergeable dt1
+let $q=
+update (select *
+ from (select *
+ from (select * from t1) dt1 where dt1.a > 1) dt2
+ where dt2.a < 8) dt3
+set dt3.a=dt3.a+1
+where dt3.a between 4 and 6;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable cte1
+let $q=
+update (select *
+ from (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 8) dt1
+ where dt1.a > 1) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 5 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+let $q=
+update (select *
+ from (select * from v1 where v1.a < 8) dt1
+ where dt1.a > 2) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable dt1
+let $q=
+update (with cte1 as
+ (select * from (select * from t1) dt1 where dt1.a > 2)
+ select * from cte1 where cte1.a < 6) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable cte2 that uses mergeable cte1
+let $q=
+update (with cte2 as
+ (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 3)
+ select * from cte2 where cte2.a < 10) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+let $q=
+update (with cte1 as
+ (select * from v1 where v1.a > 1)
+ select * from cte1 where cte1.a < 7) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 4;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+let $q=
+update (select * from v1 where v1.a < 6) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 4 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+let $q=
+update (select * from v1 where v1.a < 6) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+let $q=
+update (select * from v2 where v2.a < 8) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 5 and 10;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+
+drop table t1;
diff --git a/mysql-test/main/derived_update_multi.result b/mysql-test/main/derived_update_multi.result
new file mode 100644
index 0000000..3369854
--- /dev/null
+++ b/mysql-test/main/derived_update_multi.result
@@ -0,0 +1,2529 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and mergeable derived table dt2
+explain select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+a b
+5 5
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+set a=a+1, b=b+1 where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+set a=a+1, b=b+1 where dt1.a=dt2.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# multi-table update of mergeable derived table dt1
+# and mergeable cte1
+explain with cte1 as (select * from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select * from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+a b
+6 6
+4 4
+4 4
+3 3
+3 3
+5 5
+6 6
+explain with cte1 as (select * from t2 where b < 9)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1 where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select * from t2 where b < 9)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1 where dt1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# multi-table update of mergeable derived table dt1
+# and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+explain select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+a b
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2, v1.b=v1.b+2 where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2, v1.b=v1.b+2 where dt1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+5
+9
+4
+4
+6
+5
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and non-mergeable derived table dt2
+explain select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+set a=a+1 where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+set a=a+1 where dt1.a=dt2.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and non-mergeable cte1
+explain with cte1 as (select * from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select * from t2 where b < 9 group by b)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1 where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1 where dt1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+explain select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2 where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2 where dt1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable derived table dt3 specified as join of
+# mergeable derived table dt1 and mergeable derived table dt2
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1, dt3.b=dt3.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1, dt3.b=dt3.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and mergeable cte1
+explain update
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1, dt2.b=dt2.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1, dt2.b=dt2.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1, dt2.b=dt2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1, dt2.b=dt2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and mergeable cte2
+explain update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+4
+10
+3
+9
+2
+2
+4
+4
+1
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a+1, dt1.b=dt1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a+1, dt1.b=dt1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+explain update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+5
+5
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable derived table dt3 specified as join of
+# mergeable derived table dt1 and non-mergeable derived table dt2
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and non-mergeable cte1
+explain update
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt2 specified as join of
+# mergeable cte1 and non-mergeable derived table dt1
+explain update
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b) dt2
+set dt2.a=dt2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b) dt2
+set dt2.a=dt2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and non-mergeable cte2
+explain update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+explain update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt2 specified as join of
+# mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain update
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b) dt2
+set dt2.a=dt2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b) dt2
+set dt2.a=dt2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select b from t2 where b < 7 group by b)
+update
+(select * from v1, cte1 where v1.a=cte1.b) dt1
+set dt1.a=dt1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select b from t2 where b < 7 group by b)
+update
+(select * from v1, cte1 where v1.a=cte1.b) dt1
+set dt1.a=dt1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+explain update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable derived table dt2 and mergeable derived table dt3
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable derived table dt2 and mergeable cte1
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+set dt1.a=dt1.a-1, dt2.b=dt2.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+set dt1.a=dt1.a-1, dt2.b=dt2.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable cte1 and mergeable derived table dt2
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable cte1 and mergeable cte2
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10;
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, v1.b=v1.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, v1.b=v1.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable derived table dt2 and mergeable derived table dt3
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable derived table dt2 and mergeable cte1
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# non-mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable cte1 and mergeable derived table dt2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable cte1 and mergeable cte2
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# non-mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# non-mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10 group by b;
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop table t1,t2,t3;
diff --git a/mysql-test/main/derived_update_multi.test b/mysql-test/main/derived_update_multi.test
new file mode 100644
index 0000000..7db0848
--- /dev/null
+++ b/mysql-test/main/derived_update_multi.test
@@ -0,0 +1,860 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and mergeable derived table dt2
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+set a=a+1, b=b+1 where dt1.a=dt2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and mergeable cte1
+let $qs=
+with cte1 as (select * from t2 where b < 9)
+select * from
+ (select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1 where dt1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2, v1.b=v1.b+2 where dt1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and non-mergeable derived table dt2
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+set a=a+1 where dt1.a=dt2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and non-mergeable cte1
+let $qs=
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from
+ (select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9 group by b)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1 where dt1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2 where dt1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable derived table dt3 specified as join of
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+ where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1, dt3.b=dt3.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and mergeable cte1
+let $qu=
+update
+ (with cte1 as (select b from t2 where b < 9)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1, dt2.b=dt2.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1, dt2.b=dt2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and mergeable cte2
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6)
+ select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a+1, dt1.b=dt1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+let $qu=
+update
+ (select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable derived table dt3 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable derived table dt2
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+ where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable cte1
+let $qu=
+update
+ (with cte1 as (select b from t2 where b < 9 group by b)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable cte1 and non-mergeable derived table dt1
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1)
+ select * from
+ cte1, (select b from t2 where b < 7 group by b) dt1
+ where cte1.a=dt1.b) dt2
+set dt2.a=dt2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and non-mergeable cte2
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6 group by b)
+ select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+update
+ (select * from
+ v1, (select b from t2 where b < 6 group by b) dt1
+ where v1.a=dt1.b) dt2
+set dt2.a=dt2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+with cte1 as (select b from t2 where b < 7 group by b)
+update
+ (select * from v1, cte1 where v1.a=cte1.b) dt1
+set dt1.a=dt1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+let $qu=
+update
+ (select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable derived table dt2 and mergeable derived table dt3
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable derived table dt2 and mergeable cte1
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10) dt2,
+ cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10) dt2,
+ cte1
+set dt1.a=dt1.a-1, dt2.b=dt2.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ v1
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable cte1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable cte1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10;
+let $qs=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+set dt1.a=dt1.a-1, v1.b=v1.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable derived table dt2 and mergeable derived table dt3
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable derived table dt2 and mergeable cte1
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10 group by b) dt2,
+ cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10 group by b) dt2,
+ cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable cte1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable cte1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+set dt1.a=dt1.a-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10 group by b;
+let $qs=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+set dt1.a=dt1.a+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+drop table t1,t2,t3;
diff --git a/mysql-test/main/view.result b/mysql-test/main/view.result
index 55d95b2..b3a45bf 100644
--- a/mysql-test/main/view.result
+++ b/mysql-test/main/view.result
@@ -1114,13 +1114,13 @@ ERROR 44000: CHECK OPTION failed `test`.`v2`
insert into v3 values (0);
ERROR 44000: CHECK OPTION failed `test`.`v3`
insert into v2 values (2);
+ERROR 44000: CHECK OPTION failed `test`.`v2`
insert into v3 values (2);
ERROR 44000: CHECK OPTION failed `test`.`v3`
select * from t1;
a
1
1
-2
drop view v3,v2,v1;
drop table t1;
create table t1 (a int, primary key (a));
@@ -1955,7 +1955,6 @@ insert into t1 values (19,41,32);
create view v1 as select f59, f60 from t1 where f59 in
(select f59 from t1);
update v1 set f60=2345;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
drop table t1;
create table t1 (s1 int);
@@ -6490,56 +6489,47 @@ CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
insert into v1 values (-300);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s1=s1+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
CREATE VIEW v1 AS SELECT s1,s2 FROM (SELECT s1 as s2 FROM t1 WHERE s1 <
100) x, t1 WHERE t1.s1=x.s2;
select * from v1;
s1 s2
1 1
-2 2
3 3
+4 4
-200 -200
insert into v1 (s1) values (-300);
update v1 set s1=s1+1;
select * from v1;
s1 s2
2 2
-3 3
4 4
+5 5
-199 -199
-299 -299
select * from t1;
s1
2
-3
4
+5
200
-199
-299
insert into v1(s2) values (-300);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s2=s2+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM t1 WHERE s1
< 100) AS x;
insert into v1 values (-300);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s1=s1+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
< 100) as xx WHERE s1>1) AS x;
-insert into v1 values (-300);
-ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s1=s1+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
create view v2 as select * from v1;
-insert into v2 values (-300);
-ERROR HY000: The target table v2 of the INSERT is not insertable-into
update v2 set s1=s1+1;
-ERROR HY000: The target table v2 of the UPDATE is not updatable
drop view v1, v2;
drop table t1;
#
@@ -6554,7 +6544,7 @@ INSERT INTO t3 VALUES (1),(8);
CREATE VIEW v1 AS SELECT * FROM t1 LEFT JOIN ( SELECT t2.* FROM t2 INNER JOIN t3 ON ( k = j ) ) AS alias1 ON ( i = j );
show create view v1;
View Create View character_set_client collation_connection
-v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `test`.`t1`.`i` AS `i`,`alias1`.`j` AS `j` from (`test`.`t1` left join (select `test`.`t2`.`j` AS `j` from (`test`.`t2` join `test`.`t3` on(`test`.`t3`.`k` = `test`.`t2`.`j`))) `alias1` on(`test`.`t1`.`i` = `alias1`.`j`)) latin1 latin1_swedish_ci
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`i` AS `i`,`alias1`.`j` AS `j` from (`t1` left join (select `t2`.`j` AS `j` from (`t2` join `t3` on(`t3`.`k` = `t2`.`j`))) `alias1` on(`t1`.`i` = `alias1`.`j`)) latin1 latin1_swedish_ci
SELECT * FROM t1 LEFT JOIN ( SELECT t2.* FROM t2 INNER JOIN t3 ON ( k = j ) ) AS alias1 ON ( i = j );
i j
3 NULL
@@ -6637,7 +6627,7 @@ CREATE VIEW v AS SELECT * FROM ( SELECT * FROM t ) sq;
DROP TABLE IF EXISTS t;
SHOW CREATE VIEW v;
View Create View character_set_client collation_connection
-v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `sq`.`i` AS `i` from (select `test`.`t`.`i` AS `i` from `test`.`t`) `sq` latin1 latin1_swedish_ci
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `sq`.`i` AS `i` from (select `test`.`t`.`i` AS `i` from `t`) `sq` latin1 latin1_swedish_ci
Warnings:
Warning 1356 View 'test.v' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
DROP VIEW v;
@@ -6712,7 +6702,7 @@ EXECUTE stmt;
DEALLOCATE PREPARE stmt;
show create view v1;
View Create View character_set_client collation_connection
-v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `testalias`.`testcase` AS `testcase` from (select case when 1 in (select `test`.`t1`.`a` from `test`.`t1` where `test`.`t1`.`a` < 2) then 1 end AS `testcase`) `testalias` latin1 latin1_swedish_ci
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `testalias`.`testcase` AS `testcase` from (select case when 1 in (select `t1`.`a` from `t1` where `t1`.`a` < 2) then 1 end AS `testcase`) `testalias` latin1 latin1_swedish_ci
SELECT * FROM v1;
testcase
1
diff --git a/mysql-test/main/view.test b/mysql-test/main/view.test
index 1632e61..6df36ff 100644
--- a/mysql-test/main/view.test
+++ b/mysql-test/main/view.test
@@ -1020,6 +1020,7 @@ insert into v3 values (1);
insert into v2 values (0);
-- error ER_VIEW_CHECK_FAILED
insert into v3 values (0);
+-- error ER_VIEW_CHECK_FAILED
insert into v2 values (2);
-- error ER_VIEW_CHECK_FAILED
insert into v3 values (2);
@@ -1734,7 +1735,7 @@ create table t1 (f59 int, f60 int, f61 int);
insert into t1 values (19,41,32);
create view v1 as select f59, f60 from t1 where f59 in
(select f59 from t1);
--- error ER_NON_UPDATABLE_TABLE
+# -- error ER_NON_UPDATABLE_TABLE
update v1 set f60=2345;
drop view v1;
drop table t1;
@@ -6192,7 +6193,7 @@ CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
--error ER_NON_INSERTABLE_TABLE
insert into v1 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s1=s1+1;
drop view v1;
@@ -6207,7 +6208,7 @@ select * from v1;
select * from t1;
--error ER_NON_INSERTABLE_TABLE
insert into v1(s2) values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s2=s2+1;
drop view v1;
@@ -6217,7 +6218,7 @@ CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM t1 WHERE s1
--error ER_NON_INSERTABLE_TABLE
insert into v1 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s1=s1+1;
drop view v1;
@@ -6225,16 +6226,16 @@ drop view v1;
CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
< 100) as xx WHERE s1>1) AS x;
---error ER_NON_INSERTABLE_TABLE
-insert into v1 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_INSERTABLE_TABLE
+# insert into v1 values (-300);
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s1=s1+1;
create view v2 as select * from v1;
---error ER_NON_INSERTABLE_TABLE
-insert into v2 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_INSERTABLE_TABLE
+# insert into v2 values (-300);
+# --error ER_NON_UPDATABLE_TABLE
update v2 set s1=s1+1;
drop view v1, v2;
diff --git a/mysql-test/main/view_update.result b/mysql-test/main/view_update.result
new file mode 100644
index 0000000..fd3259f
--- /dev/null
+++ b/mysql-test/main/view_update.result
@@ -0,0 +1,412 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+# full update of mergeable view v1
+create view v1 as select * from t1;
+explain update v1 set a=10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update v1 set a=10;
+select * from t1;
+a
+10
+10
+10
+10
+10
+explain update v1 set a=14;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update v1 set a=14;
+select * from t1;
+a
+14
+14
+14
+14
+14
+# update with limit of mergeable view v1
+explain update v1 set v1.a=18 limit 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update v1 set v1.a=18 limit 3;
+select * from t1;
+a
+18
+18
+18
+14
+14
+# partial update of mergeable view v1
+explain update v1 set v1.a=2 where a<18;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=2 where a<18;
+select * from t1;
+a
+18
+18
+18
+2
+2
+drop view v1;
+# partial update of mergeable v1 defined as
+# full select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1;
+explain update v1 set v1.a=22 where a>16;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=22 where a>16;
+select * from t1;
+a
+22
+22
+22
+2
+2
+drop view v1;
+# full update of mergeable v1 defined as
+# partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 20;
+explain update v1 set v1.a=24;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=24;
+select * from t1;
+a
+24
+24
+24
+2
+2
+drop view v1;
+update t1 set a=26 limit 1;
+select * from t1;
+a
+26
+24
+24
+2
+2
+# partial update of mergeable v1 defined as
+# partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a < 25;
+explain update v1 set v1.a=15 where v1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=15 where v1.a > 2;
+select * from t1;
+a
+26
+15
+15
+2
+2
+drop view v1;
+create view v1 as select * from t1;
+# partial update of mergeable v2 defined as
+# full select from mergeable view v1
+create view v2 as select * from v1;
+explain update v2 set v2.a=17 where v2.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v2 set v2.a=17 where v2.a between 10 and 20;
+select * from t1;
+a
+26
+17
+17
+2
+2
+drop view v2;
+# full update of mergeable v2 defined as
+# partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a > 10;
+explain update v2 set v2.a=23;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v2 set v2.a=23;
+select * from t1;
+a
+23
+23
+23
+2
+2
+drop view v2;
+update t1 set a=28 limit 1;
+select * from t1;
+a
+28
+23
+23
+2
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a < 27;
+explain update v2 set v2.a=19 where v2.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v2 set v2.a=19 where v2.a > 2;
+select * from t1;
+a
+28
+19
+19
+2
+2
+drop view v2;
+drop view v1;
+# partial update of mergeable v1 defined as
+# full select from mergeable cte1
+create view v1 as with cte1 as (select * from t1) select * from cte1;
+explain update v1 set v1.a=11 where v1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=11 where v1.a between 10 and 20;
+select * from t1;
+a
+28
+11
+11
+2
+2
+drop view v1;
+# full update of mergeable v1 defined as
+# partial select from mergeable cte1
+create view v1 as
+with cte1 as (select * from t1)select * from cte1 where cte1.a > 10;
+explain update v1 set v1.a=21;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=21;
+select * from t1;
+a
+21
+21
+21
+2
+2
+drop view v1;
+update t1 set a=29 limit 1;
+select * from t1;
+a
+29
+21
+21
+2
+2
+# partial update of mergeable v1 defined as
+# partial select from mergeable embedded cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a < 27;
+explain update v1 set v1.a=13 where v1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=13 where v1.a > 2;
+select * from t1;
+a
+29
+13
+13
+2
+2
+drop view v1;
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+# partial update of mergeable v1 defined as
+# partial select from mergeable dt2 that uses mergeable dt1
+create view v1 as
+(select *
+from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8);
+explain update v1 set v1.a=v1.a+1 where v1.a between 4 and 6;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 6;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+6
+2
+drop view v1;
+# partial update of mergeable v1 defined as
+# partial select from mergeable dt1 that uses mergeable cte1
+create view v1 as
+(select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1);
+explain update v1 set v1.a=v1.a-1 where v1.a between 5 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a-1 where v1.a between 5 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+5
+2
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+create view v2 as
+select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2;
+explain update v2 set v2.a=v2.a+1 where v2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a+1 where v2.a between 2 and 5;
+select * from t1;
+a
+4
+8
+6
+1
+5
+4
+6
+2
+drop view v2;
+drop view v1;
+# partial update of mergeable v1 defined as
+# partial select from mergeable cte1 that uses mergeable dt1
+create view v1 as
+with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6;
+explain update v1 set v1.a=v1.a-1 where v1.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a-1 where v1.a between 4 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+6
+2
+drop view v1;
+# partial update of mergeable v1 defined as
+# partial select from mergeable cte2 that uses mergeable cte1
+create view v1 as
+with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10;
+explain update v1 set v1.a=v1.a+1 where v1.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 7;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+7
+2
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as
+with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7;
+explain update v2 set v2.a=v2.a-1 where v2.a between 2 and 4;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 4;
+select * from t1;
+a
+2
+8
+7
+1
+5
+2
+7
+1
+drop view v2;
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+create view v2 as select * from v1 where v1.a < 6;
+explain update v2 set v2.a=v2.a+1 where v2.a between 4 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a+1 where v2.a between 4 and 5;
+select * from t1;
+a
+2
+8
+7
+1
+6
+2
+7
+1
+drop view v2;
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+create view v2 as select * from v1 where v1.a < 6;
+explain update v2 set v2.a=v2.a-1 where v2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 5;
+select * from t1;
+a
+1
+8
+7
+1
+6
+1
+7
+1
+drop view v2;
+drop view v1;
+# partial update of mergeable v3 defined as
+# partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+create view v3 as select * from v2 where v2.a < 8;
+explain update v3 set v3.a=v3.a+1 where v3.a between 5 and 10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v3 set v3.a=v3.a+1 where v3.a between 5 and 10;
+select * from t1;
+a
+1
+8
+8
+1
+7
+1
+8
+1
+drop view v3;
+drop view v2;
+drop view v1;
+drop table t1;
diff --git a/mysql-test/main/view_update.test b/mysql-test/main/view_update.test
new file mode 100644
index 0000000..dc074fa
--- /dev/null
+++ b/mysql-test/main/view_update.test
@@ -0,0 +1,241 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+--echo # full update of mergeable view v1
+create view v1 as select * from t1;
+let $q=
+update v1 set a=10;
+eval explain $q;
+eval $q;
+select * from t1;
+let $q=
+update v1 set a=14;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # update with limit of mergeable view v1
+let $q=
+update v1 set v1.a=18 limit 3;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable view v1
+let $q=
+update v1 set v1.a=2 where a<18;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # full select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1;
+let $q=
+update v1 set v1.a=22 where a>16;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # full update of mergeable v1 defined as
+--echo # partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 20;
+let $q=
+update v1 set v1.a=24;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+update t1 set a=26 limit 1;
+select * from t1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a < 25;
+let $q=
+update v1 set v1.a=15 where v1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+
+create view v1 as select * from t1;
+
+--echo # partial update of mergeable v2 defined as
+--echo # full select from mergeable view v1
+create view v2 as select * from v1;
+let $q=
+update v2 set v2.a=17 where v2.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+--echo # full update of mergeable v2 defined as
+--echo # partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a > 10;
+let $q=
+update v2 set v2.a=23;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+
+update t1 set a=28 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a < 27;
+let $q=
+update v2 set v2.a=19 where v2.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # full select from mergeable cte1
+create view v1 as with cte1 as (select * from t1) select * from cte1;
+let $q=
+update v1 set v1.a=11 where v1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # full update of mergeable v1 defined as
+--echo # partial select from mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1)select * from cte1 where cte1.a > 10;
+let $q=
+update v1 set v1.a=21;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+update t1 set a=29 limit 1;
+select * from t1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable embedded cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a < 27;
+let $q=
+update v1 set v1.a=13 where v1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable dt2 that uses mergeable dt1
+create view v1 as
+ (select *
+ from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+ where dt2.a < 8);
+let $q=
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 6;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable dt1 that uses mergeable cte1
+create view v1 as
+ (select *
+ from (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 8) dt1
+ where dt1.a > 1);
+let $q=
+update v1 set v1.a=v1.a-1 where v1.a between 5 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+create view v2 as
+ select *
+ from (select * from v1 where v1.a < 8) dt1
+ where dt1.a > 2;
+let $q=
+update v2 set v2.a=v2.a+1 where v2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable cte1 that uses mergeable dt1
+create view v1 as
+ with cte1 as
+ (select * from (select * from t1) dt1 where dt1.a > 2)
+ select * from cte1 where cte1.a < 6;
+let $q=
+update v1 set v1.a=v1.a-1 where v1.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable cte2 that uses mergeable cte1
+create view v1 as
+ with cte2 as
+ (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 3)
+ select * from cte2 where cte2.a < 10;
+let $q=
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as
+ with cte1 as
+ (select * from v1 where v1.a > 1)
+ select * from cte1 where cte1.a < 7;
+let $q=
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 4;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+create view v2 as select * from v1 where v1.a < 6;
+let $q=
+update v2 set v2.a=v2.a+1 where v2.a between 4 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+create view v2 as select * from v1 where v1.a < 6;
+let $q=
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v3 defined as
+--echo # partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+create view v3 as select * from v2 where v2.a < 8;
+let $q=
+update v3 set v3.a=v3.a+1 where v3.a between 5 and 10;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v3;
+drop view v2;
+drop view v1;
+
+drop table t1;
diff --git a/mysql-test/main/view_update_multi.result b/mysql-test/main/view_update_multi.result
new file mode 100644
index 0000000..834b0e8
--- /dev/null
+++ b/mysql-test/main/view_update_multi.result
@@ -0,0 +1,2685 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable view v1
+# and mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain select * from v1, (select b from t2 where b < 6) as dt1
+where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from v1, (select b from t2 where b < 6) as dt1
+where v1.a=dt1.b;
+a b
+5 5
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update v1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where v1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select * from t2 where b < 9)
+select * from v1, cte1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select * from t2 where b < 9)
+select * from v1, cte1 where v1.a=cte1.b;
+a b
+6 6
+4 4
+4 4
+3 3
+3 3
+5 5
+6 6
+explain with cte1 as (select * from t2 where b < 9)
+update v1, cte1
+set v1.a=v1.a-1, cte1.b=cte1.b-1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select * from t2 where b < 9)
+update v1, cte1
+set v1.a=v1.a-1, cte1.b=cte1.b-1 where v1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5;
+explain select * from v1, v2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from v1, v2 where v1.a=v2.b;
+a b
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update v1, v2
+set v1.a=v1.a+2, v2.b=v2.b+2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1, v2
+set v1.a=v1.a+2, v2.b=v2.b+2 where v1.a=v2.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+5
+9
+4
+4
+6
+5
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable view v1;
+# and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+explain select * from v1, v2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, v2 where v1.a=v2.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+explain update v1
+set v1.a=v1.a+1, v1.b=v1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1
+set v1.a=v1.a+1, v1.b=v1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and mergeable cte1
+create view v1 as
+with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+explain update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+explain update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable cte1 and mergeable cte2
+create view v1 as
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b;
+explain update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+4
+10
+3
+9
+2
+2
+4
+4
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+explain update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v3 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+explain update v3
+set v3.a=v3.a-1, v3.b=v3.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v3
+set v3.a=v3.a-1, v3.b=v3.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+5
+5
+1
+drop view v3;
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable view v1;
+# and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+explain select * from v1, v2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, v2 where v1.a=v2.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and non-mergeable derived table dt2
+create view v1 as
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+explain update v1
+set v1.a=v1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and non-mergeable cte1
+create view v1 as
+with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+explain update v1
+set v1.a=v1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+create view v2 as
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+explain update v2
+set v2.a=v2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable cte1 and non-mergeable derived table dt1
+create view v1 as
+with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b;
+explain update v1
+set v1.a=v1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable cte1 and non-mergeable cte2
+create view v1 as
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+explain update v1
+set v1.a=v1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable cte1 and non mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+create view v2 as
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+explain update v2
+set v2.a=v2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b;
+explain update v2
+set v2.a=v2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+# update of mergeable view v2 specified as join of
+# mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+with cte1 as (select b from t2 where b < 7 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+explain update v2
+set v2.a=v2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+# update of mergeable view v3 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+explain update v3
+set v3.a=v3.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v3
+set v3.a=v3.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v3;
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+set v1.a=v1.a-1, dt1.b=dt1.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+set v1.a=v1.a-1, dt1.b=dt1.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable v1,
+# mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, cte1.b=cte1.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, cte1.b=cte1.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, cte1.b=cte1.b+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, cte1.b=cte1.b+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5);
+explain select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, v2.b=v2.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, v2.b=v2.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, v2.b=v2.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, v2.b=v2.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v2.b=v2.b+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v2.b=v2.b+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop view v3;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# non-mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable v1,
+# non-mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# non-mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5 group by b);
+explain select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# non-mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10 group by b;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# non-mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5 group by b;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop view v3;
+drop table t1,t2,t3;
diff --git a/mysql-test/main/view_update_multi.test b/mysql-test/main/view_update_multi.test
new file mode 100644
index 0000000..70c97ac
--- /dev/null
+++ b/mysql-test/main/view_update_multi.test
@@ -0,0 +1,970 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable view v1
+--echo # and mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+select * from v1, (select b from t2 where b < 6) as dt1
+where v1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where v1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+with cte1 as (select * from t2 where b < 9)
+select * from v1, cte1 where v1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9)
+update v1, cte1
+set v1.a=v1.a-1, cte1.b=cte1.b-1 where v1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5;
+let $qs=
+select * from v1, v2 where v1.a=v2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, v2
+set v1.a=v1.a+2, v2.b=v2.b+2 where v1.a=v2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable view v1;
+--echo # and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+let $qs=
+select * from v1, v2 where v1.a=v2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as
+ select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+ where dt1.a=dt2.b;
+let $qu=
+update v1
+set v1.a=v1.a+1, v1.b=v1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and mergeable cte1
+create view v1 as
+ with cte1 as (select b from t2 where b < 9)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b;
+let $qu=
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+ select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable cte1 and mergeable cte2
+create view v1 as
+ with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6)
+ select * from cte1, cte2 where cte1.a=cte2.b;
+let $qu=
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+ with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v3 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+let $qu=
+update v3
+set v3.a=v3.a-1, v3.b=v3.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable view v1;
+--echo # and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+let $qs=
+select * from v1, v2 where v1.a=v2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable derived table dt2
+create view v1 as
+ select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+ where dt1.a=dt2.b;
+let $qu=
+update v1
+set v1.a=v1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable cte1
+create view v1 as
+ with cte1 as (select b from t2 where b < 9 group by b)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b;
+let $qu=
+update v1
+set v1.a=v1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+create view v2 as
+ select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable cte1 and non-mergeable derived table dt1
+create view v1 as
+ with cte1 as (select a from t1 where a > 1)
+ select * from
+ cte1, (select b from t2 where b < 7 group by b) dt1
+ where cte1.a=dt1.b;
+let $qu=
+update v1
+set v1.a=v1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable cte1 and non-mergeable cte2
+create view v1 as
+ with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6 group by b)
+ select * from cte1, cte2 where cte1.a=cte2.b;
+let $qu=
+update v1
+set v1.a=v1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable cte1 and non mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+create view v2 as
+ with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+ select * from
+ v1, (select b from t2 where b < 6 group by b) dt1
+ where v1.a=dt1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+ with cte1 as (select b from t2 where b < 7 group by b)
+ select * from v1, cte1 where v1.a=cte1.b;
+let $qu=
+update v2
+set v2.a=v2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+--echo # update of mergeable view v3 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+let $qu=
+update v3
+set v3.a=v3.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable view v1,
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte1
+set v1.a=v1.a-1, dt1.b=dt1.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ v2
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ v2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable v1,
+--echo # mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, cte1.b=cte1.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ cte1,
+ cte2
+set v1.a=v1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ v2
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ v1,
+ cte1,
+ v2
+set v1.a=v1.a+1, cte1.b=cte1.b+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable view v1,
+--echo # mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, v2.b=v2.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ cte1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ v2,
+ cte1
+set v1.a=v1.a-1, v2.b=v2.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ v3
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ v3
+set v1.a=v1.a+1, v2.b=v2.b+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+drop view v3;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v2
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable v1,
+--echo # non-mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ cte1,
+ cte2
+set v1.a=v1.a-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ v2
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ v1,
+ cte1,
+ v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5 group by b);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10 group by b;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ cte1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ v2,
+ cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5 group by b;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ v3
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ v3
+set v1.a=v1.a+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+drop view v3;
+
+drop table t1,t2,t3;
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 23ff906..1cbf837 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -1782,7 +1782,6 @@ longlong Item_in_subselect::val_int()
As far as Item_in_subselect called only from Item_in_optimizer this
method should not be used
*/
- DBUG_ASSERT(0);
DBUG_ASSERT(fixed == 1);
if (forced_const)
return value;
diff --git a/sql/sp_head.cc b/sql/sp_head.cc
index 49c8229..f177966 100644
--- a/sql/sp_head.cc
+++ b/sql/sp_head.cc
@@ -3484,8 +3484,7 @@ sp_lex_keeper::reset_lex_and_exec_core(THD *thd, uint *nextp,
Json_writer_object trace_command(thd);
Json_writer_array trace_command_steps(thd, "steps");
if (open_tables)
- res= check_dependencies_in_with_clauses(m_lex->with_clauses_list) ||
- instr->exec_open_and_lock_tables(thd, m_lex->query_tables);
+ res= instr->exec_open_and_lock_tables(thd, m_lex->query_tables);
if (likely(!res))
{
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index 090ced5..71b3fa5 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -1183,7 +1183,7 @@ unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
{
TABLE_LIST *dup;
- table= table->find_table_for_update();
+ table= table->find_table_for_update(thd);
if (table->table &&
table->table->file->ha_table_flags() & HA_CAN_MULTISTEP_MERGE)
@@ -3568,7 +3568,18 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags,
if (tables->derived)
{
if (!tables->view)
+ {
+ if (!tables->is_derived())
+ tables->set_derived();
+ st_select_lex *sl= tables->derived->first_select();
+ if (sl->is_mergeable())
+ {
+ tables->merge_underlying_list= sl->table_list.first;
+ tables->propagate_properties_for_mergeable_derived();
+ tables->where= sl->where;
+ }
goto end;
+ }
/*
We restore view's name and database wiped out by derived tables
processing and fall back to standard open process in order to
@@ -3578,33 +3589,15 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags,
tables->db= tables->view_db;
tables->table_name= tables->view_name;
}
- else if (tables->select_lex)
+ else if (tables->select_lex)
{
- /*
- Check whether 'tables' refers to a table defined in a with clause.
- If so set the reference to the definition in tables->with.
- */
- if (!tables->with)
- tables->with= tables->select_lex->find_table_def_in_with_clauses(tables);
- /*
- If 'tables' is defined in a with clause set the pointer to the
- specification from its definition in tables->derived.
- */
if (tables->with)
{
- if (tables->is_recursive_with_table() &&
- !tables->is_with_table_recursive_reference())
- {
- tables->with->rec_outer_references++;
- With_element *with_elem= tables->with;
- while ((with_elem= with_elem->get_next_mutually_recursive()) !=
- tables->with)
- with_elem->rec_outer_references++;
- }
- if (tables->set_as_with_table(thd, tables->with))
+ if (!(tables->derived= tables->with->clone_parsed_spec(thd->lex, tables)))
DBUG_RETURN(1);
- else
- goto end;
+ tables->derived->first_select()->set_linkage(DERIVED_TABLE_TYPE);
+ tables->select_lex->add_statistics(tables->derived);
+ goto end;
}
}
@@ -5168,13 +5161,13 @@ bool open_and_lock_tables(THD *thd, const DDL_options_st &options,
/* Don't read statistics tables when opening internal tables */
if (!(flags & MYSQL_OPEN_IGNORE_LOGGING_FORMAT))
(void) read_statistics_for_tables_if_needed(thd, tables);
-
+
if (derived)
{
if (mysql_handle_derived(thd->lex, DT_INIT))
goto err;
if (thd->prepare_derived_at_open &&
- (mysql_handle_derived(thd->lex, DT_PREPARE)))
+ (mysql_handle_derived(thd->lex, DT_PREPARE)))
goto err;
}
@@ -6287,8 +6280,7 @@ find_field_in_tables(THD *thd, Item_ident *item,
when table_ref->field_translation != NULL.
*/
if (table_ref->table && !table_ref->view &&
- (!table_ref->is_merged_derived() ||
- (!table_ref->is_multitable() && table_ref->merged_for_insert)))
+ !table_ref->is_merged_derived())
{
found= find_field_in_table(thd, table_ref->table, name, length,
@@ -8017,7 +8009,7 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
(table->grant.privilege & SELECT_ACL)) ||
((!tables->is_non_derived() &&
(tables->grant.privilege & SELECT_ACL)))) &&
- !any_privileges)
+ !any_privileges)
{
field_iterator.set(tables);
if (check_grant_all_columns(thd, SELECT_ACL, &field_iterator))
@@ -8082,7 +8074,8 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
temporary table. Thus in this case we can be sure that 'item' is an
Item_field.
*/
- if (any_privileges && !tables->is_with_table() && !tables->is_derived())
+ if (any_privileges && !tables->is_with_table() && !tables->is_derived() &&
+ !thd->lex->can_use_merged())
{
DBUG_ASSERT((tables->field_translation == NULL && table) ||
tables->is_natural_join);
@@ -8304,7 +8297,7 @@ int setup_conds(THD *thd, TABLE_LIST *tables, List<TABLE_LIST> &leaves,
for (table= tables; table; table= table->next_local)
{
- if (select_lex == thd->lex->first_select_lex() &&
+ if (select_lex != thd->lex->first_select_lex() &&
select_lex->first_cond_optimization &&
table->merged_for_insert &&
table->prepare_where(thd, conds, FALSE))
diff --git a/sql/sql_class.h b/sql/sql_class.h
index ed4ebbb..ea0764f 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -4357,14 +4357,11 @@ class THD: public THD_count, /* this must be first */
to resolve all CTE names as we don't need this message to be thrown
for any CTE references.
*/
- if (!lex->with_clauses_list)
+ if (!lex->with_cte_resolution)
{
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
return TRUE;
}
- /* This will allow to throw an error later for non-CTE references */
- to->str= NULL;
- to->length= 0;
return FALSE;
}
@@ -5384,7 +5381,7 @@ class select_result :public select_result_sink
virtual void update_used_tables() {}
/* this method is called just before the first row of the table can be read */
- virtual void prepare_to_read_rows() {}
+ virtual void prepare_to_read_rows(THD *thd) {}
void remove_offset_limit()
{
@@ -6595,7 +6592,7 @@ class multi_delete :public select_result_interceptor
bool send_eof();
inline ha_rows num_deleted() const { return deleted; }
virtual void abort_result_set();
- void prepare_to_read_rows();
+ void prepare_to_read_rows(THD *thd);
};
@@ -6603,6 +6600,7 @@ class multi_update :public select_result_interceptor
{
TABLE_LIST *all_tables; /* query/update command tables */
List<TABLE_LIST> *leaves; /* list of leves of join table tree */
+ List<TABLE_LIST> updated_leaves;
TABLE_LIST *update_tables;
TABLE **tmp_tables, *main_table, *table_to_update;
TMP_TABLE_PARAM *tmp_table_param;
@@ -6640,6 +6638,7 @@ class multi_update :public select_result_interceptor
List<Item> *fields, List<Item> *values,
enum_duplicates handle_duplicates, bool ignore);
~multi_update();
+ bool init(THD *thd);
int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
int send_data(List<Item> &items);
bool initialize_tables (JOIN *join);
@@ -6650,7 +6649,7 @@ class multi_update :public select_result_interceptor
inline ha_rows num_updated() const { return updated; }
virtual void abort_result_set();
void update_used_tables();
- void prepare_to_read_rows();
+ void prepare_to_read_rows(THD *thd);
};
class my_var_sp;
diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc
index 5bf9930..1eb4327 100644
--- a/sql/sql_cte.cc
+++ b/sql/sql_cte.cc
@@ -56,11 +56,43 @@ bool With_clause::add_with_element(With_element *elem)
}
-void st_select_lex_unit::set_with_clause(With_clause *with_cl)
+void With_clause::move_tables_to_end(LEX *lex)
+{
+ if (tables_start_pos == tables_end_pos ||
+ tables_end_pos == lex->query_tables_last)
+ return;
+ TABLE_LIST *first_tbl= *tables_start_pos;
+ DBUG_ASSERT (first_tbl);
+ TABLE_LIST *next_tbl= *tables_end_pos;
+ DBUG_ASSERT(next_tbl);
+ /* Exclude the tables of the with clause from global list */
+ *(next_tbl->prev_global= tables_start_pos)= next_tbl;
+ /* Attach the tables of the with_clause to the very end of global list */
+ *(first_tbl->prev_global= lex->query_tables_last)= first_tbl;
+ *(lex->query_tables_last= tables_end_pos)= 0;
+ for (With_element *with_elem= with_list.first;
+ with_elem; with_elem= with_elem->next)
+ {
+ if (with_elem->head->tables_pos.start_pos != tables_start_pos)
+ break;
+ with_elem->head->tables_pos.set_start_pos(first_tbl->prev_global);
+ if (with_elem->head->tables_pos.end_pos != tables_start_pos)
+ break;
+ with_elem->head->tables_pos.set_end_pos(first_tbl->prev_global);
+ }
+ tables_start_pos= first_tbl->prev_global;
+ tables_end_pos= lex->query_tables_last;
+}
+
+
+void st_select_lex_unit::set_with_clause(LEX *lex, With_clause *with_cl)
{
with_clause= with_cl;
if (with_clause)
+ {
with_clause->set_owner(this);
+ with_clause->move_tables_to_end(lex);
+ }
}
@@ -84,7 +116,8 @@ void st_select_lex_unit::set_with_clause(With_clause *with_cl)
true on failure
*/
-bool check_dependencies_in_with_clauses(With_clause *with_clauses_list)
+
+bool LEX::check_dependencies_in_with_clauses()
{
for (With_clause *with_clause= with_clauses_list;
with_clause;
@@ -100,6 +133,216 @@ bool check_dependencies_in_with_clauses(With_clause *with_clauses_list)
}
+bool
+LEX::resolve_references_to_cte_in_hanging_cte(TABLE_LIST **start_ptr)
+{
+ for (With_clause *with_clause= with_clauses_list;
+ with_clause; with_clause= with_clause->next_with_clause)
+ {
+ for (With_element *with_elem= with_clause->with_list.first;
+ with_elem; with_elem= with_elem->next)
+ {
+ if (!with_elem->is_referenced())
+ {
+ TABLE_LIST *cte_last_ordered= 0;
+ TABLE_LIST *first_tbl=
+ with_elem->spec->first_select()->table_list.first;
+ TABLE_LIST **with_elem_end_pos= with_elem->head->tables_pos.end_pos;
+ if (resolve_and_order_references_to_cte(first_tbl,
+ with_elem_end_pos,
+ &cte_last_ordered))
+ return true;
+ reorder_table_list(first_tbl, start_ptr);
+ start_ptr= &cte_last_ordered->next_global;
+ }
+ }
+ }
+ return false;
+}
+
+
+bool LEX::resolve_and_order_references_to_cte(TABLE_LIST *tables,
+ TABLE_LIST **tables_last,
+ TABLE_LIST **last_ordered)
+{
+ TABLE_LIST *with_elem_first_tbl= 0;
+ TABLE_LIST **with_elem_end_pos= 0;
+ With_element *with_elem= 0;
+ bool rc= false;
+ Query_arena backup;
+ Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup);
+
+ for (TABLE_LIST *tbl= tables;
+ tbl != *tables_last;
+ tbl= tbl->next_global)
+ {
+ if (tbl->next_ordered || tbl == *last_ordered)
+ continue;
+ if (!*last_ordered)
+ *last_ordered= tbl;
+ else
+ *last_ordered= (*last_ordered)->next_ordered= tbl;
+ if (!tbl->db.str && !tbl->with)
+ tbl->with= tbl->select_lex->find_table_def_in_with_clauses(tbl);
+ if (!tbl->with)
+ {
+ if (!tbl->db.str)
+ {
+ if (!thd->db.str)
+ {
+ my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
+ rc= true;
+ goto err;
+ }
+ if (copy_db_to(&tbl->db))
+ {
+ rc= true;
+ goto err;
+ }
+ if (!(tbl->table_options & TL_OPTION_ALIAS))
+ MDL_REQUEST_INIT(&tbl->mdl_request, MDL_key::TABLE, tbl->db.str,
+ tbl->table_name.str, tbl->mdl_type, MDL_TRANSACTION);
+ if (tbl->is_mdl_request_type_to_be_set)
+ {
+ tbl->mdl_request.set_type((tbl->lock_type >= TL_WRITE_ALLOW_WRITE) ?
+ MDL_SHARED_WRITE : MDL_SHARED_READ);
+ tbl->is_mdl_request_type_to_be_set= false;
+ }
+ }
+ continue;
+ }
+ with_elem= tbl->with;
+ if (tbl->is_recursive_with_table() &&
+ !tbl->is_with_table_recursive_reference())
+ {
+ tbl->with->rec_outer_references++;
+ while ((with_elem= with_elem->get_next_mutually_recursive()) !=
+ tbl->with)
+ with_elem->rec_outer_references++;
+ }
+ if (!with_elem->is_used_in_query || with_elem->is_recursive)
+ {
+ tbl->derived= with_elem->spec;
+ if (tbl->derived != tbl->select_lex->master_unit() &&
+ !with_elem->is_recursive &&
+ !tbl->is_with_table_recursive_reference())
+ {
+ tbl->derived->move_as_slave(tbl->select_lex);
+ }
+ with_elem->is_used_in_query= true;
+ }
+ else
+ tbl->derived= 0;
+ tbl->db.str= empty_c_string;
+ tbl->db.length= 0;
+ tbl->schema_table= 0;
+ MDL_REQUEST_INIT(&tbl->mdl_request, MDL_key::TABLE, tbl->db.str,
+ tbl->table_name.str, tbl->mdl_type, MDL_TRANSACTION);
+ if (tbl->is_mdl_request_type_to_be_set)
+ {
+ tbl->mdl_request.set_type((tbl->lock_type >= TL_WRITE_ALLOW_WRITE) ?
+ MDL_SHARED_WRITE : MDL_SHARED_READ);
+ tbl->is_mdl_request_type_to_be_set= false;
+ }
+ if (tbl->derived)
+ {
+ tbl->derived->first_select()->set_linkage(DERIVED_TABLE_TYPE);
+ tbl->select_lex->add_statistics(tbl->derived);
+ }
+ if (tbl->with->is_recursive && tbl->is_with_table_recursive_reference())
+ continue;
+ with_elem->inc_references();
+ if (tbl->derived)
+ {
+ with_elem_first_tbl= tbl->derived->first_select()->table_list.first;
+ with_elem_end_pos= with_elem->head->tables_pos.end_pos;
+ if (with_elem_first_tbl &&
+ resolve_and_order_references_to_cte(with_elem_first_tbl,
+ with_elem_end_pos,
+ last_ordered))
+ {
+ rc= true;
+ goto err;
+ }
+ }
+ }
+err:
+ if (arena)
+ thd->restore_active_arena(arena, &backup);
+ return rc;
+}
+
+
+void LEX::reorder_table_list(TABLE_LIST *tables, TABLE_LIST **start_ptr)
+{
+ TABLE_LIST *next_ordered= 0;
+ TABLE_LIST **last_chunk_next_ptr= start_ptr;
+ bool is_first_chunk= true;
+ for (TABLE_LIST *tbl= tables; tbl; tbl= next_ordered)
+ {
+ TABLE_LIST *chunk_first= tbl;
+ for (next_ordered= tbl->next_ordered;
+ next_ordered && next_ordered == tbl->next_global;
+ next_ordered= tbl->next_ordered)
+ {
+ tbl= next_ordered;
+ }
+ TABLE_LIST *chunk_last= tbl;
+ if (is_first_chunk && *last_chunk_next_ptr == chunk_first)
+ {
+ /* The first chunk is in place */
+ }
+ else
+ {
+ TABLE_LIST *next_tbl= tbl->next_global;
+
+ /* Exclude chunk_first..chunk_last from global list */
+ if (next_tbl)
+ *(next_tbl->prev_global= chunk_first->prev_global)= next_tbl;
+ else
+ *(query_tables_last= chunk_first->prev_global)= 0;
+
+ next_tbl= *last_chunk_next_ptr;
+ /*
+ Include chunk_first..chunk_last before next_tbl if it's not 0 and
+ at the end of the list otherwise
+ */
+ if (next_tbl)
+ *(next_tbl->prev_global= &chunk_last->next_global)= next_tbl;
+ else
+ *(query_tables_last= &chunk_last->next_global)= 0;
+ *(chunk_first->prev_global= last_chunk_next_ptr)= chunk_first;
+ }
+ last_chunk_next_ptr= &chunk_last->next_global;
+ is_first_chunk= false;
+ }
+}
+
+
+bool
+LEX::check_cte_dependencies_and_resolve_references()
+{
+ if (check_dependencies_in_with_clauses())
+ return true;
+ if (!with_cte_resolution)
+ return false;
+ TABLE_LIST *last_ordered= 0;
+ TABLE_LIST *first_tbl= query_tables;
+ if (resolve_and_order_references_to_cte(first_tbl,
+ query_tables_last,
+ &last_ordered))
+ return true;
+ reorder_table_list(first_tbl, &query_tables);
+ TABLE_LIST **start_ptr= last_ordered ? &last_ordered->next_global :
+ &query_tables;
+ if (*start_ptr &&
+ resolve_references_to_cte_in_hanging_cte(start_ptr))
+ return true;
+ all_cte_resolved= true;
+ return false;
+}
+
+
/**
@brief
Check dependencies between tables defined in this with clause
@@ -138,10 +381,11 @@ bool With_clause::check_dependencies()
elem != with_elem;
elem= elem->next)
{
- if (lex_string_cmp(system_charset_info, with_elem->query_name,
- elem->query_name) == 0)
+ if (lex_string_cmp(system_charset_info, with_elem->get_name(),
+ elem->get_name()) == 0)
{
- my_error(ER_DUP_QUERY_NAME, MYF(0), with_elem->query_name->str);
+ my_error(ER_DUP_QUERY_NAME, MYF(0),
+ with_elem->get_name_str());
return true;
}
}
@@ -248,11 +492,12 @@ With_element *With_clause::find_table_def(TABLE_LIST *table,
with_elem != barrier;
with_elem= with_elem->next)
{
- if (my_strcasecmp(system_charset_info, with_elem->query_name->str,
+ if (my_strcasecmp(system_charset_info, with_elem->get_name_str(),
table->table_name.str) == 0 &&
!table->is_fqtn)
{
table->set_derived();
+ with_elem->referenced= true;
return with_elem;
}
}
@@ -609,7 +854,7 @@ bool With_clause::check_anchors()
if (elem == with_elem)
{
my_error(ER_RECURSIVE_WITHOUT_ANCHORS, MYF(0),
- with_elem->query_name->str);
+ with_elem->get_name_str());
return true;
}
}
@@ -642,7 +887,7 @@ bool With_clause::check_anchors()
if (elem->work_dep_map & elem->get_elem_map())
{
my_error(ER_UNACCEPTABLE_MUTUAL_RECURSION, MYF(0),
- with_elem->query_name->str);
+ with_elem->get_name_str());
return true;
}
}
@@ -827,14 +1072,17 @@ bool With_element::set_unparsed_spec(THD *thd,
NULL - otherwise
*/
-st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
+st_select_lex_unit *With_element::clone_parsed_spec(LEX *old_lex,
TABLE_LIST *with_table)
{
+ THD *thd= old_lex->thd;
LEX *lex;
st_select_lex_unit *res= NULL;
Query_arena backup;
Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup);
bool has_tmp_tables;
+ uint i= 0;
+
if (!(lex= (LEX*) new(thd->mem_root) st_lex_local))
{
@@ -842,14 +1090,12 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
thd->restore_active_arena(arena, &backup);
return res;
}
- LEX *old_lex= thd->lex;
thd->lex= lex;
bool parse_status= false;
Parser_state parser_state;
- TABLE_LIST *spec_tables;
- TABLE_LIST *spec_tables_tail;
st_select_lex *with_select;
+ st_select_lex *end_sl;
char save_end= unparsed_spec.str[unparsed_spec.length];
((char*) &unparsed_spec.str[unparsed_spec.length])[0]= '\0';
@@ -866,6 +1112,7 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
lex->spname= old_lex->spname;
lex->spcont= old_lex->spcont;
lex->sp_chistics= old_lex->sp_chistics;
+ lex->with_cte_resolution= true;
lex->stmt_lex= old_lex;
parse_status= parse_sql(thd, &parser_state, 0);
@@ -875,48 +1122,63 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
if (parse_status)
goto err;
- if (check_dependencies_in_with_clauses(lex->with_clauses_list))
- goto err;
-
- spec_tables= lex->query_tables;
- spec_tables_tail= 0;
- has_tmp_tables= thd->has_temporary_tables();
- for (TABLE_LIST *tbl= spec_tables;
- tbl;
- tbl= tbl->next_global)
+ if (lex->query_tables)
{
- if (has_tmp_tables && !tbl->derived && !tbl->schema_table &&
- thd->open_temporary_table(tbl))
- goto err;
- spec_tables_tail= tbl;
- }
- if (check_table_access(thd, SELECT_ACL, spec_tables, FALSE, UINT_MAX, FALSE))
- goto err;
- if (spec_tables)
- {
- if (with_table->next_global)
+ head->tables_pos.set_start_pos(&with_table->next_global);
+ head->tables_pos.set_end_pos(lex->query_tables_last);
+ TABLE_LIST *next_tbl= with_table->next_global;
+ if (next_tbl)
{
- spec_tables_tail->next_global= with_table->next_global;
- with_table->next_global->prev_global= &spec_tables_tail->next_global;
+ *(lex->query_tables->prev_global= next_tbl->prev_global)=
+ lex->query_tables;
+ *(next_tbl->prev_global= lex->query_tables_last)= next_tbl;
}
else
{
- old_lex->query_tables_last= &spec_tables_tail->next_global;
+ *(lex->query_tables->prev_global= old_lex->query_tables_last)=
+ lex->query_tables;
+ old_lex->query_tables_last= lex->query_tables_last;
}
- spec_tables->prev_global= &with_table->next_global;
- with_table->next_global= spec_tables;
}
+
res= &lex->unit;
lex->unit.include_down(with_table->select_lex);
- lex->unit.set_slave(with_select);
+ lex->unit.set_slave(with_select);
+ end_sl= lex->all_selects_list;
+ while (end_sl->next_select_in_list())
+ end_sl= end_sl->next_select_in_list();
old_lex->all_selects_list=
(st_select_lex*) (lex->all_selects_list->
insert_chain_before(
(st_select_lex_node **) &(old_lex->all_selects_list),
- with_select));
- if (check_dependencies_in_with_clauses(lex->with_clauses_list))
+ end_sl));
+ if (lex->check_cte_dependencies_and_resolve_references())
+ {
res= NULL;
+ goto err;
+ }
+
+ has_tmp_tables= thd->has_temporary_tables();
+ for (TABLE_LIST *tbl= *head->tables_pos.start_pos;
+ tbl != *head->tables_pos.end_pos;
+ tbl= tbl->next_global, i++)
+ {
+ if (has_tmp_tables && !tbl->derived && !tbl->schema_table &&
+ thd->open_temporary_table(tbl))
+ {
+ res= NULL;
+ goto err;
+ }
+ }
+
+ if (i && check_table_access(thd, SELECT_ACL, *head->tables_pos.start_pos,
+ FALSE, i, FALSE))
+ {
+ res= NULL;
+ goto err;
+ }
+
lex->sphead= NULL; // in order not to delete lex->sphead
lex_end(lex);
err:
@@ -1128,57 +1390,6 @@ With_element *st_select_lex::find_table_def_in_with_clauses(TABLE_LIST *table)
}
-/**
- @brief
- Set the specifying unit in this reference to a with table
-
- @details
- The method assumes that the given element with_elem defines the table T
- this table reference refers to.
- If this is the first reference to T the method just sets its specification
- in the field 'derived' as the unit that yields T. Otherwise the method
- first creates a clone specification and sets rather this clone in this field.
-
- @retval
- false on success
- true on failure
-*/
-
-bool TABLE_LIST::set_as_with_table(THD *thd, With_element *with_elem)
-{
- if (table)
- {
- /*
- This table was prematurely identified as a temporary table.
- We correct it here, but it's not a nice solution in the case
- when the temporary table with this name is not used anywhere
- else in the query.
- */
- thd->mark_tmp_table_as_free_for_reuse(table);
- table= 0;
- }
- with= with_elem;
- schema_table= NULL;
- if (!with_elem->is_referenced() || with_elem->is_recursive)
- {
- derived= with_elem->spec;
- if (derived != select_lex->master_unit() &&
- !with_elem->is_recursive &&
- !is_with_table_recursive_reference())
- {
- derived->move_as_slave(select_lex);
- }
- }
- else
- {
- if(!(derived= with_elem->clone_parsed_spec(thd, this)))
- return true;
- }
- derived->first_select()->set_linkage(DERIVED_TABLE_TYPE);
- select_lex->add_statistics(derived);
- with_elem->inc_references();
- return false;
-}
bool TABLE_LIST::is_recursive_with_table()
@@ -1280,7 +1491,7 @@ bool st_select_lex::check_unrestricted_recursive(bool only_standard_compliant)
if (only_standard_compliant && with_elem->is_unrestricted())
{
my_error(ER_NOT_STANDARD_COMPLIANT_RECURSIVE,
- MYF(0), with_elem->query_name->str);
+ MYF(0), with_elem->get_name_str());
return true;
}
@@ -1497,7 +1708,8 @@ static void list_strlex_print(THD *thd, String *str, List<Lex_ident_sys> *list)
void With_element::print(THD *thd, String *str, enum_query_type query_type)
{
- str->append(query_name);
+ str->append(get_name());
+
if (column_list.elements)
{
List_iterator_fast<Lex_ident_sys> li(column_list);
diff --git a/sql/sql_cte.h b/sql/sql_cte.h
index 4c42dd2..0c5ac72 100644
--- a/sql/sql_cte.h
+++ b/sql/sql_cte.h
@@ -23,6 +23,20 @@
class select_unit;
struct st_unit_ctxt_elem;
+class With_element_head : public Sql_alloc
+{
+ LEX_CSTRING *query_name;
+public:
+ TABLE_CHAIN tables_pos;
+ With_element_head(LEX_CSTRING *name)
+ : query_name(name)
+ {
+ tables_pos.set_start_pos(0);
+ tables_pos.set_end_pos(0);
+ }
+ friend class With_element;
+};
+
/**
@class With_element
@@ -85,6 +99,10 @@ class With_element : public Sql_alloc
subqueries and specifications of other with elements).
*/
uint references;
+ bool referenced;
+
+ bool is_used_in_query;
+
/*
Unparsed specification of the query that specifies this element.
It used to build clones of the specification if they are needed.
@@ -98,13 +116,10 @@ class With_element : public Sql_alloc
/* Return the map where 1 is set only in the position for this element */
table_map get_elem_map() { return (table_map) 1 << number; }
-
+
public:
- /*
- The name of the table introduced by this with elememt. The name
- can be used in FROM lists of the queries in the scope of the element.
- */
- LEX_CSTRING *query_name;
+ With_element_head *head;
+
/*
Optional list of column names to name the columns of the table introduced
by this with element. It is used in the case when the names are not
@@ -163,18 +178,27 @@ class With_element : public Sql_alloc
/* List of derived tables containing recursive references to this CTE */
SQL_I_List<TABLE_LIST> derived_with_rec_ref;
- With_element(LEX_CSTRING *name,
+ With_element(With_element_head *h,
List <Lex_ident_sys> list,
st_select_lex_unit *unit)
: next(NULL), base_dep_map(0), derived_dep_map(0),
sq_dep_map(0), work_dep_map(0), mutually_recursive(0),
top_level_dep_map(0), sq_rec_ref(NULL),
next_mutually_recursive(NULL), references(0),
- query_name(name), column_list(list), cycle_list(0), spec(unit),
+ referenced(false), is_used_in_query(false),
+ head(h), column_list(list), cycle_list(0), spec(unit),
is_recursive(false), rec_outer_references(0), with_anchor(false),
level(0), rec_result(NULL)
{ unit->with_element= this; }
+ LEX_CSTRING *get_name() { return head->query_name; }
+ const char *get_name_str() { return get_name()->str; }
+
+ void set_tables_start_pos(TABLE_LIST **pos)
+ { head->tables_pos.set_start_pos(pos); }
+ void set_tables_end_pos(TABLE_LIST **pos)
+ { head->tables_pos.set_end_pos(pos); }
+
bool check_dependencies_in_spec();
void check_dependencies_in_select(st_select_lex *sl, st_unit_ctxt_elem *ctxt,
@@ -196,14 +220,16 @@ class With_element : public Sql_alloc
bool check_dependency_on(With_element *with_elem)
{ return base_dep_map & with_elem->get_elem_map(); }
+
+
TABLE_LIST *find_first_sq_rec_ref_in_select(st_select_lex *sel);
bool set_unparsed_spec(THD *thd, const char *spec_start, const char *spec_end,
my_ptrdiff_t spec_offset);
- st_select_lex_unit *clone_parsed_spec(THD *thd, TABLE_LIST *with_table);
+ st_select_lex_unit *clone_parsed_spec(LEX *old_lex, TABLE_LIST *with_table);
- bool is_referenced() { return references != 0; }
+ bool is_referenced() { return referenced; }
void inc_references() { references++; }
@@ -263,6 +289,13 @@ class With_element : public Sql_alloc
void set_cycle_list(List<Lex_ident_sys> *cycle_list_arg);
friend class With_clause;
+
+ friend
+ bool LEX::resolve_and_order_references_to_cte(TABLE_LIST *tables,
+ TABLE_LIST **tables_last,
+ TABLE_LIST **last_ordered);
+ friend
+ bool LEX::resolve_references_to_cte_in_hanging_cte(TABLE_LIST **start_ptr);
};
const uint max_number_of_elements_in_with_clause= sizeof(table_map)*8;
@@ -294,6 +327,10 @@ class With_clause : public Sql_alloc
in the current statement
*/
With_clause *next_with_clause;
+
+ TABLE_LIST **tables_start_pos;
+ TABLE_LIST **tables_end_pos;
+
/* Set to true if dependencies between with elements have been checked */
bool dependencies_are_checked;
/*
@@ -341,6 +378,13 @@ class With_clause : public Sql_alloc
void attach_to(st_select_lex *select_lex);
With_clause *pop() { return embedding_with_clause; }
+
+ void set_tables_start_pos(TABLE_LIST **table_pos)
+ { tables_start_pos= table_pos; }
+ void set_tables_end_pos(TABLE_LIST **table_pos)
+ { tables_end_pos= table_pos; }
+
+ void move_tables_to_end(LEX *lex);
bool check_dependencies();
@@ -360,9 +404,8 @@ class With_clause : public Sql_alloc
friend class With_element;
- friend
- bool
- check_dependencies_in_with_clauses(With_clause *with_clauses_list);
+ friend bool LEX::check_dependencies_in_with_clauses();
+ friend bool LEX::resolve_references_to_cte_in_hanging_cte(TABLE_LIST**);
};
inline
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 5942efc..359ef46 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -339,11 +339,12 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
if (thd->lex->handle_list_of_derived(table_list, DT_PREPARE))
DBUG_RETURN(TRUE);
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE");
DBUG_RETURN(TRUE);
}
+
if (!(table= table_list->table) || !table->is_created())
{
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
@@ -1022,7 +1023,7 @@ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds,
setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
setup_ftfuncs(select_lex))
DBUG_RETURN(TRUE);
- if (!table_list->single_table_updatable() ||
+ if (!table_list->single_table_updatable(thd) ||
check_key_in_view(thd, table_list))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE");
@@ -1121,7 +1122,7 @@ int mysql_multi_delete_prepare(THD *thd)
DBUG_RETURN(TRUE);
}
- if (!target_tbl->correspondent_table->single_table_updatable() ||
+ if (!target_tbl->correspondent_table->single_table_updatable(thd) ||
check_key_in_view(thd, target_tbl->correspondent_table))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
@@ -1181,12 +1182,12 @@ multi_delete::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
DBUG_RETURN(0);
}
-void multi_delete::prepare_to_read_rows()
+void multi_delete::prepare_to_read_rows(THD *thd)
{
/* see multi_update::prepare_to_read_rows() */
for (TABLE_LIST *walk= delete_tables; walk; walk= walk->next_local)
{
- TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update();
+ TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update(thd);
tbl->table->mark_columns_needed_for_delete();
}
}
@@ -1206,7 +1207,7 @@ multi_delete::initialize_tables(JOIN *join)
delete_while_scanning= true;
for (walk= delete_tables; walk; walk= walk->next_local)
{
- TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update();
+ TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update(thd);
tables_to_delete_from|= tbl->table->map;
if (delete_while_scanning &&
unique_table(thd, tbl, join->tables_list, 0))
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index 428e7b1..596902e 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -382,10 +382,6 @@ bool mysql_derived_merge(THD *thd, LEX *lex, TABLE_LIST *derived)
DBUG_RETURN(FALSE);
}
- if (thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
- thd->lex->sql_command == SQLCOM_DELETE_MULTI)
- thd->save_prep_leaf_list= TRUE;
-
arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test
if (!derived->merged_for_insert ||
@@ -531,7 +527,7 @@ bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived)
derived->merged_for_insert,
derived->is_materialized_derived(),
derived->is_multitable(),
- derived->single_table_updatable(),
+ derived->single_table_updatable(thd),
derived->merge_underlying_list != 0));
if (derived->merged_for_insert)
DBUG_RETURN(FALSE);
@@ -544,20 +540,35 @@ bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived)
DBUG_RETURN(FALSE);
if (!derived->is_multitable())
{
- if (!derived->single_table_updatable())
+ if (!derived->is_view() ||
+ !derived->single_table_updatable(thd) ||
+ derived->merge_underlying_list->is_derived())
DBUG_RETURN(derived->create_field_translation(thd));
if (derived->merge_underlying_list)
{
derived->table= derived->merge_underlying_list->table;
derived->schema_table= derived->merge_underlying_list->schema_table;
derived->merged_for_insert= TRUE;
- DBUG_ASSERT(derived->table);
}
}
DBUG_RETURN(FALSE);
}
+void TABLE_LIST::propagate_properties_for_mergeable_derived()
+{
+ for (TABLE_LIST *tbl= derived->first_select()->table_list.first;
+ tbl;
+ tbl= tbl->next_local)
+ {
+ tbl->lock_type= lock_type;
+ if (!tbl->mdl_request.ticket)
+ tbl->mdl_request.set_type(mdl_request.type);
+ tbl->updating= updating;
+ }
+}
+
+
/*
Initialize a derived table/view
@@ -590,8 +601,6 @@ bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived)
bool res= derived->init_derived(thd, TRUE);
- derived->updatable= derived->updatable && derived->is_view();
-
DBUG_RETURN(res);
}
@@ -754,6 +763,12 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
break;
}
}
+ if (derived->is_recursive_with_table() &&
+ derived->is_with_table_recursive_reference())
+ {
+ derived->table->grant.privilege= SELECT_ACL;
+ derived->grant.privilege= SELECT_ACL;
+ }
DBUG_RETURN(FALSE);
}
@@ -827,8 +842,14 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
Depending on the result field translation will or will not
be created.
*/
- if (derived->init_derived(thd, FALSE))
- goto exit;
+ if (derived->is_merged_derived())
+ {
+ if ((derived->is_view() ||
+ (derived->get_unit()->prepared &&
+ !(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW))) &&
+ derived->create_field_translation(thd))
+ goto exit;
+ }
/*
Temp table is created so that it hounours if UNION without ALL is to be
@@ -920,8 +941,17 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
{
DBUG_ASSERT(derived->is_derived());
DBUG_ASSERT(derived->is_anonymous_derived_table());
- table->grant.privilege= SELECT_ACL;
- derived->grant.privilege= SELECT_ACL;
+ if (derived->is_materialized_derived())
+ {
+ table->grant.privilege= SELECT_ACL;
+ derived->grant.privilege= SELECT_ACL;
+ }
+ else
+ {
+ if (table->grant.privilege == NO_ACL)
+ table->grant.privilege= VIEW_ANY_ACL;
+ table->grant= derived->grant;
+ }
}
#endif
/* Add new temporary table to list of open derived tables */
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index a934427..228d5e9 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -121,7 +121,8 @@ static int binlog_show_create_table(THD *thd, TABLE *table,
@returns false if success.
*/
-static bool check_view_single_update(List<Item> &fields, List<Item> *values,
+static bool check_view_single_update(THD *thd,
+ List<Item> &fields, List<Item> *values,
TABLE_LIST *view, table_map *map,
bool insert)
{
@@ -171,7 +172,7 @@ static bool check_view_single_update(List<Item> &fields, List<Item> *values,
*/
tbl->table->insert_values= view->table->insert_values;
view->table= tbl->table;
- if (!tbl->single_table_updatable())
+ if (!tbl->single_table_updatable(thd))
{
if (insert)
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), view->alias.str, "INSERT");
@@ -179,6 +180,10 @@ static bool check_view_single_update(List<Item> &fields, List<Item> *values,
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), view->alias.str, "UPDATE");
return TRUE;
}
+ else if (tbl->is_derived() && insert)
+ {
+ my_error(ER_NON_INSERTABLE_TABLE, MYF(0), view->alias.str, "INSERT");
+ }
*map= tables;
return FALSE;
@@ -214,7 +219,7 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
TABLE *table= table_list->table;
DBUG_ENTER("check_insert_fields");
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT");
DBUG_RETURN(-1);
@@ -286,7 +291,7 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
if (table_list->is_view() && table_list->is_merged_derived())
{
- if (check_view_single_update(fields,
+ if (check_view_single_update(thd, fields,
fields_and_values_from_different_maps ?
(List<Item>*) 0 : &values,
table_list, map, true))
@@ -392,7 +397,7 @@ static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
if (insert_table_list->is_view() &&
insert_table_list->is_merged_derived() &&
- check_view_single_update(update_fields,
+ check_view_single_update(thd, update_fields,
fields_and_values_from_different_maps ?
(List<Item>*) 0 : &update_values,
insert_table_list, map, false))
@@ -1462,7 +1467,7 @@ static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list,
bool insert_into_view= (table_list->view != 0);
DBUG_ENTER("mysql_prepare_insert_check_table");
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd) || table_list->is_derived())
{
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT");
DBUG_RETURN(TRUE);
@@ -1576,11 +1581,11 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
DBUG_ASSERT (!select_insert || !values);
if (mysql_handle_derived(thd->lex, DT_INIT))
- DBUG_RETURN(TRUE);
+ DBUG_RETURN(TRUE);
if (table_list->handle_derived(thd->lex, DT_MERGE_FOR_INSERT))
- DBUG_RETURN(TRUE);
+ DBUG_RETURN(TRUE);
if (thd->lex->handle_list_of_derived(table_list, DT_PREPARE))
- DBUG_RETURN(TRUE);
+ DBUG_RETURN(TRUE);
if (duplic == DUP_UPDATE)
{
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 671948f..3d98a17 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -1227,6 +1227,7 @@ void LEX::start(THD *thd_arg)
curr_with_clause= 0;
with_clauses_list= 0;
with_clauses_list_last_next= &with_clauses_list;
+ all_cte_resolved= 0;
clone_spec_offset= 0;
create_view= NULL;
field_list.empty();
@@ -1249,6 +1250,7 @@ void LEX::start(THD *thd_arg)
explain_json= false;
context_analysis_only= 0;
derived_tables= 0;
+ with_cte_resolution= false;
safe_to_cache_query= 1;
parsing_options.reset();
empty_field_list_on_rset= 0;
@@ -1267,6 +1269,7 @@ void LEX::start(THD *thd_arg)
selects_allow_into= FALSE;
selects_allow_procedure= FALSE;
use_only_table_context= FALSE;
+ skip_access_check= false;
parse_vcol_expr= FALSE;
check_exists= FALSE;
create_info.lex_start();
@@ -8859,6 +8862,8 @@ bool LEX::check_main_unit_semantics()
if (unit.set_nest_level(0) ||
unit.check_parameters(first_select_lex()))
return TRUE;
+ if (check_cte_dependencies_and_resolve_references())
+ return TRUE;
return FALSE;
}
@@ -9551,8 +9556,13 @@ void st_select_lex::add_statistics(SELECT_LEX_UNIT *unit)
bool LEX::main_select_push()
{
DBUG_ENTER("LEX::main_select_push");
+#if 0
current_select_number= 1;
builtin_select.select_number= 1;
+#else
+ current_select_number= ++thd->lex->stmt_lex->current_select_number;
+ builtin_select.select_number= current_select_number;
+#endif
if (push_select(&builtin_select))
DBUG_RETURN(TRUE);
DBUG_RETURN(FALSE);
@@ -9648,7 +9658,7 @@ bool LEX::insert_select_hack(SELECT_LEX *sel)
builtin_select.link_prev= NULL; // indicator of removal
}
- if (set_main_unit(sel->master_unit()))
+ if (set_main_unit(this, sel->master_unit()))
return true;
DBUG_ASSERT(builtin_select.table_list.elements == 1);
@@ -10046,7 +10056,7 @@ TABLE_LIST *LEX::parsed_derived_table(SELECT_LEX_UNIT *unit,
bool LEX::parsed_create_view(SELECT_LEX_UNIT *unit, int check)
{
SQL_I_List<TABLE_LIST> *save= &first_select_lex()->table_list;
- if (set_main_unit(unit))
+ if (set_main_unit(this, unit))
return true;
if (check_main_unit_semantics())
return true;
@@ -10070,7 +10080,7 @@ bool LEX::select_finalize(st_select_lex_unit *expr)
sql_command= SQLCOM_SELECT;
selects_allow_into= TRUE;
selects_allow_procedure= TRUE;
- if (set_main_unit(expr))
+ if (set_main_unit(this, expr))
return true;
return check_main_unit_semantics();
}
@@ -10377,7 +10387,7 @@ void st_select_lex::pushdown_cond_into_where_clause(THD *thd, Item *cond,
above are marked with NO_EXTRACTION_FL.
@note
- This method is called for pushdown into materialized
+ This mesthod is called for pushdown into materialized
derived tables/views/IN subqueries optimization.
*/
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index adfa0c0..81621c9 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -969,7 +969,7 @@ class st_select_lex_unit: public st_select_lex_node {
{
return reinterpret_cast<st_select_lex*>(slave);
}
- void set_with_clause(With_clause *with_cl);
+ void set_with_clause(LEX *lex, With_clause *with_cl);
st_select_lex_unit* next_unit()
{
return reinterpret_cast<st_select_lex_unit*>(next);
@@ -1762,6 +1762,16 @@ class Query_tables_list
*/
void add_to_query_tables(TABLE_LIST *table)
{
+ if (table->derived)
+ {
+ TABLE_LIST *first_local= table->derived->first_select()->table_list.first;
+ if (first_local)
+ {
+ *(table->prev_global= first_local->prev_global)= table;
+ *(first_local->prev_global= &table->next_global)= first_local;
+ return;
+ }
+ }
*(table->prev_global= query_tables_last)= table;
query_tables_last= &table->next_global;
}
@@ -3132,6 +3142,9 @@ class Lex_grant_privilege: public Grant_privilege, public Sql_alloc
};
+class Query_name;
+
+
struct LEX: public Query_tables_list
{
SELECT_LEX_UNIT unit; /* most upper unit */
@@ -3154,6 +3167,7 @@ struct LEX: public Query_tables_list
with clause in the current statement
*/
With_clause **with_clauses_list_last_next;
+ bool all_cte_resolved;
/*
When a copy of a with element is parsed this is set to the offset of
the with element in the input string, otherwise it's set to 0
@@ -3372,6 +3386,7 @@ struct LEX: public Query_tables_list
*/
uint8 derived_tables;
uint8 context_analysis_only;
+ bool with_cte_resolution;
bool local_file;
bool check_exists;
bool autocommit;
@@ -3471,6 +3486,8 @@ struct LEX: public Query_tables_list
*/
bool use_only_table_context;
+ bool skip_access_check;
+
/*
Reference to a struct that contains information in various commands
to add/create/drop/change table spaces.
@@ -4524,7 +4541,7 @@ struct LEX: public Query_tables_list
bool insert_select_hack(SELECT_LEX *sel);
SELECT_LEX *create_priority_nest(SELECT_LEX *first_in_nest);
- bool set_main_unit(st_select_lex_unit *u)
+ bool set_main_unit(LEX *lex, st_select_lex_unit *u)
{
unit.options= u->options;
unit.uncacheable= u->uncacheable;
@@ -4532,7 +4549,7 @@ struct LEX: public Query_tables_list
unit.first_select()->options|= builtin_select.options;
unit.fake_select_lex= u->fake_select_lex;
unit.union_distinct= u->union_distinct;
- unit.set_with_clause(u->with_clause);
+ unit.set_with_clause(lex, u->with_clause);
builtin_select.exclude_from_global();
return false;
}
@@ -4675,6 +4692,15 @@ struct LEX: public Query_tables_list
const LEX_CSTRING *constraint_name,
Table_ident *ref_table_name,
DDL_options ddl_options);
+
+ bool check_dependencies_in_with_clauses();
+ bool resolve_references_to_cte_in_hanging_cte(TABLE_LIST **start_ptr);
+ bool check_cte_dependencies_and_resolve_references();
+ bool resolve_and_order_references_to_cte(TABLE_LIST *tables,
+ TABLE_LIST **tables_last,
+ TABLE_LIST **last_ordered);
+ void reorder_table_list(TABLE_LIST *tables, TABLE_LIST **start_ptr);
+
};
diff --git a/sql/sql_load.cc b/sql/sql_load.cc
index c3e301c..0637a68 100644
--- a/sql/sql_load.cc
+++ b/sql/sql_load.cc
@@ -403,7 +403,7 @@ int mysql_load(THD *thd, const sql_exchange *ex, TABLE_LIST *table_list,
INSERT_ACL | UPDATE_ACL, FALSE))
DBUG_RETURN(-1);
if (!table_list->table || // do not suport join view
- !table_list->single_table_updatable() || // and derived tables
+ !table_list->single_table_updatable(thd) || // and derived tables
check_key_in_view(thd, table_list))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "LOAD");
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 86f81dd..bf362da 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -3366,6 +3366,7 @@ mysql_execute_command(THD *thd)
already.
*/
DBUG_ASSERT(! thd->transaction_rollback_request || thd->in_sub_stmt);
+
/*
In many cases first table of main SELECT_LEX have special meaning =>
check that it is first table in global list and relink it first in
@@ -3411,9 +3412,6 @@ mysql_execute_command(THD *thd)
thd->get_stmt_da()->opt_clear_warning_info(thd->query_id);
}
- if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list))
- DBUG_RETURN(1);
-
#ifdef HAVE_REPLICATION
if (unlikely(thd->slave_thread))
{
@@ -6903,7 +6901,7 @@ bool check_one_table_access(THD *thd, privilege_t privilege,
UINT_MAX, FALSE)))
return 1;
}
- return 0;
+ return 0;
}
@@ -8183,7 +8181,7 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
ptr->is_fqtn= TRUE;
ptr->db= table->db;
}
- else if (lex->copy_db_to(&ptr->db))
+ else if (!lex->with_cte_resolution && lex->copy_db_to(&ptr->db))
DBUG_RETURN(0);
else
ptr->is_fqtn= FALSE;
@@ -8200,7 +8198,9 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
}
ptr->table_name= table->table;
- ptr->lock_type= lock_type;
+ ptr->lock_type= lock_type;
+ ptr->mdl_type= mdl_type;
+ ptr->table_options= table_options;
ptr->updating= MY_TEST(table_options & TL_OPTION_UPDATING);
/* TODO: remove TL_OPTION_FORCE_INDEX as it looks like it's not used */
ptr->force_index= MY_TEST(table_options & TL_OPTION_FORCE_INDEX);
@@ -8871,8 +8871,11 @@ void st_select_lex::set_lock_for_tables(thr_lock_type lock_type, bool for_update
{
tables->lock_type= lock_type;
tables->updating= for_update;
- tables->mdl_request.set_type((lock_type >= TL_WRITE_ALLOW_WRITE) ?
- MDL_SHARED_WRITE : MDL_SHARED_READ);
+ if (!tables->db.str)
+ tables->is_mdl_request_type_to_be_set= true;
+ else
+ tables->mdl_request.set_type((lock_type >= TL_WRITE_ALLOW_WRITE) ?
+ MDL_SHARED_WRITE : MDL_SHARED_READ);
}
DBUG_VOID_RETURN;
}
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 6dbdfe7..54288f2 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -1392,7 +1392,7 @@ static int mysql_test_update(Prepared_statement *stmt,
if (table_list->handle_derived(thd->lex, DT_PREPARE))
goto error;
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE");
goto error;
@@ -1470,7 +1470,7 @@ static bool mysql_test_delete(Prepared_statement *stmt,
if (mysql_handle_derived(thd->lex, DT_PREPARE))
goto error;
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE");
goto error;
@@ -2303,9 +2303,6 @@ static bool check_prepared_statement(Prepared_statement *stmt)
if (tables)
thd->get_stmt_da()->opt_clear_warning_info(thd->query_id);
- if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list))
- goto error;
-
if (sql_command_flags[sql_command] & CF_HA_CLOSE)
mysql_ha_rm_tables(thd, tables);
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 76cb96b..24f534d 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -1154,11 +1154,22 @@ JOIN::prepare(TABLE_LIST *tables_init, COND *conds_init, uint og_num,
Affects only materialized derived tables.
*/
/* Check that all tables, fields, conds and order are ok */
- if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
- setup_tables_and_check_access(thd, &select_lex->context, join_list,
- tables_list, select_lex->leaf_tables,
- FALSE, SELECT_ACL, SELECT_ACL, FALSE))
- DBUG_RETURN(-1);
+ if (!(select_options & OPTION_SETUP_TABLES_DONE))
+ {
+ if (thd->lex->skip_access_check)
+ {
+ if (setup_tables(thd, &select_lex->context, join_list, tables_list,
+ select_lex->leaf_tables, FALSE, FALSE))
+ DBUG_RETURN(-1);
+ }
+ else
+ {
+ if (setup_tables_and_check_access(thd, &select_lex->context, join_list,
+ tables_list, select_lex->leaf_tables,
+ FALSE, SELECT_ACL, SELECT_ACL, FALSE))
+ DBUG_RETURN(-1);
+ }
+ }
/*
Permanently remove redundant parts from the query if
@@ -2232,7 +2243,7 @@ JOIN::optimize_inner()
/* Calculate how to do the join */
THD_STAGE_INFO(thd, stage_statistics);
- result->prepare_to_read_rows();
+ result->prepare_to_read_rows(thd);
if (unlikely(make_join_statistics(this, select_lex->leaf_tables,
&keyuse)) ||
unlikely(thd->is_fatal_error))
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index 62c93f6..e72ed05 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -2581,6 +2581,8 @@ static int show_create_view(THD *thd, TABLE_LIST *table, String *buff)
tbl;
tbl= tbl->next_global)
{
+ if (!tbl->db.str || !tbl->db.str[0])
+ continue;
if (cmp(&table->view_db, tbl->view ? &tbl->view_db : &tbl->db))
{
table->compact_view_format= FALSE;
diff --git a/sql/sql_tvc.cc b/sql/sql_tvc.cc
index 37f0f80..06d74a2 100644
--- a/sql/sql_tvc.cc
+++ b/sql/sql_tvc.cc
@@ -730,7 +730,7 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
wrapper_sl->where= 0;
wrapper_sl->set_braces(false);
- derived_unit->set_with_clause(0);
+ derived_unit->set_with_clause(lex, 0);
if (arena)
thd->restore_active_arena(arena, &backup);
@@ -997,7 +997,7 @@ Item *Item_func_in::in_predicate_to_in_subs_transformer(THD *thd,
sq_select->where= 0;
sq_select->set_braces(false);
- derived_unit->set_with_clause(0);
+ derived_unit->set_with_clause(thd->lex, 0);
/* Create IN subquery predicate */
sq_select->parsing_place= parent_select->parsing_place;
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 8cd3b9f..dc2f8f4 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -401,9 +401,17 @@ int mysql_update(THD *thd,
if (open_tables(thd, &table_list, &table_count, 0))
DBUG_RETURN(1);
- /* Prepare views so they are handled correctly */
- if (mysql_handle_derived(thd->lex, DT_INIT))
+ for (TABLE_LIST *tbl= thd->lex->query_tables; tbl; tbl= tbl->next_global)
+ {
+ if (tbl->handle_derived(thd->lex, DT_INIT))
+ DBUG_RETURN(1);
+ }
+
+ if (!table_list->is_multitable() && !table_list->single_table_updatable(thd))
+ {
+ my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE");
DBUG_RETURN(1);
+ }
if (table_list->has_period() && table_list->is_view_or_derived())
{
@@ -411,9 +419,13 @@ int mysql_update(THD *thd,
DBUG_RETURN(TRUE);
}
- if (((update_source_table=unique_table(thd, table_list,
- table_list->next_global, 0)) ||
- table_list->is_multitable()))
+ table_list->single_table_update= table_list->single_table_updatable(thd);
+
+ if ((update_source_table= unique_table(thd, table_list,
+ table_list->next_global, 0)) ||
+ table_list->is_multitable() ||
+ (table_list->single_table_update &&
+ (update_source_table= table_list->find_table_for_update(thd))))
{
DBUG_ASSERT(update_source_table || table_list->view != 0);
DBUG_PRINT("info", ("Switch to multi-update"));
@@ -435,7 +447,7 @@ int mysql_update(THD *thd,
table= table_list->table;
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE");
DBUG_RETURN(1);
@@ -1402,7 +1414,7 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
We do not call DT_MERGE_FOR_INSERT because it has no sense for simple
(not multi-) update
*/
- if (mysql_handle_derived(thd->lex, DT_PREPARE))
+ if (table_list->handle_derived(thd->lex, DT_PREPARE))
DBUG_RETURN(TRUE);
if (setup_tables_and_check_access(thd, &select_lex->context,
@@ -1680,10 +1692,16 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
done= true;
- if (mysql_handle_derived(lex, DT_INIT) ||
- mysql_handle_derived(lex, DT_MERGE_FOR_INSERT) ||
- mysql_handle_derived(lex, DT_PREPARE))
+ if (!table_list->single_table_update &&
+ select_lex->handle_derived(lex, DT_INIT))
DBUG_RETURN(1);
+ lex->skip_access_check= true;
+ if (select_lex->handle_derived(lex, DT_PREPARE))
+ {
+ lex->skip_access_check= false;
+ DBUG_RETURN(1);
+ }
+ lex->skip_access_check= false;
/*
setup_tables() need for VIEWs. JOIN::prepare() will call setup_tables()
@@ -1691,12 +1709,8 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
call in setup_tables()).
*/
- if (setup_tables_and_check_access(thd, &select_lex->context,
- &select_lex->top_join_list, table_list, select_lex->leaf_tables,
- FALSE, UPDATE_ACL, SELECT_ACL, FALSE))
- DBUG_RETURN(1);
-
- if (select_lex->handle_derived(thd->lex, DT_MERGE))
+ if (setup_tables(thd, &select_lex->context, &select_lex->top_join_list,
+ table_list, select_lex->leaf_tables, FALSE, TRUE))
DBUG_RETURN(1);
List<Item> *fields= &lex->first_select_lex()->item_list;
@@ -1706,7 +1720,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
// Check if we have a view in the list ...
for (tl= table_list; tl ; tl= tl->next_local)
- if (tl->view)
+ if (tl->view || tl->is_merged_derived())
break;
// ... and pass this knowlage in check_fields call
if (check_fields(thd, table_list, *fields, tl != NULL ))
@@ -1732,7 +1746,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
/* if table will be updated then check that it is unique */
if (table->map & tables_for_update)
{
- if (!tl->single_table_updatable() || check_key_in_view(thd, tl))
+ if (!tl->single_table_updatable(thd) || check_key_in_view(thd, tl))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
tl->top_table()->alias.str, "UPDATE");
@@ -1790,6 +1804,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
DBUG_RETURN(TRUE);
}
+#if 0
/* check single table update for view compound from several tables */
for (tl= table_list; tl; tl= tl->next_local)
{
@@ -1803,6 +1818,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
DBUG_RETURN(1);
}
}
+#endif
DBUG_RETURN(0);
}
@@ -1887,9 +1903,6 @@ int mysql_multi_update_prepare(THD *thd)
*/
lex->first_select_lex()->exclude_from_table_unique_test= FALSE;
- if (lex->save_prep_leaf_tables())
- DBUG_RETURN(TRUE);
-
DBUG_RETURN (FALSE);
}
@@ -1914,12 +1927,20 @@ bool mysql_multi_update(THD *thd, TABLE_LIST *table_list, List<Item> *fields,
DBUG_RETURN(TRUE);
}
+ if ((*result)->init(thd))
+ DBUG_RETURN(1);
+
thd->abort_on_warning= !ignore && thd->is_strict_mode();
List<Item> total_list;
+ if (setup_tables(thd, &select_lex->context, &select_lex->top_join_list,
+ table_list, select_lex->leaf_tables, FALSE, FALSE))
+ DBUG_RETURN(1);
+
if (select_lex->vers_setup_conds(thd, table_list))
DBUG_RETURN(1);
+
res= mysql_select(thd,
table_list, total_list, conds,
select_lex->order_list.elements,
@@ -1957,6 +1978,20 @@ multi_update::multi_update(THD *thd_arg, TABLE_LIST *table_list,
{
}
+bool multi_update::init(THD *thd)
+{
+ table_map tables_to_update= get_table_map(fields);
+ List_iterator_fast<TABLE_LIST> li(*leaves);
+ TABLE_LIST *tbl;
+ while ((tbl =li++))
+ {
+ if (!(tbl->table->map & tables_to_update))
+ continue;
+ if (updated_leaves.push_back(tbl, thd->mem_root))
+ return true;
+ }
+ return false;
+}
/*
Connect fields with tables and create list of tables that are updated
@@ -1974,7 +2009,7 @@ int multi_update::prepare(List<Item> ¬_used_values,
List_iterator_fast<Item> value_it(*values);
uint i, max_fields;
uint leaf_table_count= 0;
- List_iterator<TABLE_LIST> ti(*leaves);
+ List_iterator<TABLE_LIST> ti(updated_leaves);
DBUG_ENTER("multi_update::prepare");
if (prepared)
@@ -2000,16 +2035,10 @@ int multi_update::prepare(List<Item> ¬_used_values,
*/
while ((table_ref= ti++))
{
- if (table_ref->is_jtbm())
- continue;
-
TABLE *table= table_ref->table;
- if (tables_to_update & table->map)
- {
- DBUG_ASSERT(table->read_set == &table->def_read_set);
- table->read_set= &table->tmp_set;
- bitmap_clear_all(table->read_set);
- }
+ DBUG_ASSERT(table->read_set == &table->def_read_set);
+ table->read_set= &table->value_set;
+ bitmap_clear_all(table->read_set);
}
/*
@@ -2023,16 +2052,10 @@ int multi_update::prepare(List<Item> ¬_used_values,
ti.rewind();
while ((table_ref= ti++))
{
- if (table_ref->is_jtbm())
- continue;
-
TABLE *table= table_ref->table;
- if (tables_to_update & table->map)
- {
- table->read_set= &table->def_read_set;
- bitmap_union(table->read_set, &table->tmp_set);
- table->file->prepare_for_insert(1);
- }
+ table->read_set= &table->def_read_set;
+ bitmap_union(table->read_set, &table->value_set);
+ table->file->prepare_for_insert(1);
}
if (unlikely(error))
DBUG_RETURN(1);
@@ -2047,25 +2070,26 @@ int multi_update::prepare(List<Item> ¬_used_values,
ti.rewind();
while ((table_ref= ti++))
{
- /* TODO: add support of view of join support */
+ TABLE *table=table_ref->table;
+ TABLE_LIST *tl= (TABLE_LIST*) thd->memdup(table_ref,
+ sizeof(*tl));
+ if (!tl)
+ DBUG_RETURN(1);
+ update.link_in_list(tl, &tl->next_local);
+ tl->shared= table_count++;
+ table->no_keyread=1;
+ table->covering_keys.clear_all();
+ table->pos_in_table_list= tl;
+ table->prepare_triggers_for_update_stmt_or_event();
+ table->reset_default_fields();
+ }
+
+ List_iterator_fast<TABLE_LIST> li(*leaves);
+ while ((table_ref= li++))
+ {
if (table_ref->is_jtbm())
continue;
- TABLE *table=table_ref->table;
leaf_table_count++;
- if (tables_to_update & table->map)
- {
- TABLE_LIST *tl= (TABLE_LIST*) thd->memdup(table_ref,
- sizeof(*tl));
- if (!tl)
- DBUG_RETURN(1);
- update.link_in_list(tl, &tl->next_local);
- tl->shared= table_count++;
- table->no_keyread=1;
- table->covering_keys.clear_all();
- table->pos_in_table_list= tl;
- table->prepare_triggers_for_update_stmt_or_event();
- table->reset_default_fields();
- }
}
table_count= update.elements;
@@ -2126,7 +2150,7 @@ void multi_update::update_used_tables()
}
}
-void multi_update::prepare_to_read_rows()
+void multi_update::prepare_to_read_rows(THD *thd)
{
/*
update column maps now. it cannot be done in ::prepare() before the
@@ -2196,7 +2220,7 @@ static bool safe_update_on_fly(THD *thd, JOIN_TAB *join_tab,
case JT_REF_OR_NULL:
return !is_key_used(table, join_tab->ref.key, table->write_set);
case JT_ALL:
- if (bitmap_is_overlapping(&table->tmp_set, table->write_set))
+ if (bitmap_is_overlapping(&table->value_set, table->write_set))
return FALSE;
/* If range search on index */
if (join_tab->quick)
@@ -2227,6 +2251,7 @@ bool
multi_update::initialize_tables(JOIN *join)
{
TABLE_LIST *table_ref;
+ table_map all_tables_to_update= get_table_map(fields);
DBUG_ENTER("initialize_tables");
if (unlikely((thd->variables.option_bits & OPTION_SAFE_UPDATES) &&
@@ -2249,6 +2274,8 @@ multi_update::initialize_tables(JOIN *join)
for (table_ref= update_tables; table_ref; table_ref= table_ref->next_local)
{
TABLE *table=table_ref->table;
+ if (!(table->map & all_tables_to_update))
+ continue;
uint cnt= table_ref->shared;
List<Item> temp_fields;
ORDER group;
@@ -2459,11 +2486,14 @@ multi_update::~multi_update()
int multi_update::send_data(List<Item> ¬_used_values)
{
TABLE_LIST *cur_table;
+ table_map all_tables_to_update= get_table_map(fields);
DBUG_ENTER("multi_update::send_data");
for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
{
TABLE *table= cur_table->table;
+ if (!(table->map & all_tables_to_update))
+ continue;
uint offset= cur_table->shared;
/*
Check if we are using outer join and we didn't find the row
@@ -2682,6 +2712,7 @@ void multi_update::abort_result_set()
int multi_update::do_updates()
{
TABLE_LIST *cur_table;
+ table_map all_tables_to_update= get_table_map(fields);
int local_error= 0;
ha_rows org_updated;
TABLE *table, *tmp_table, *err_table;
@@ -2710,8 +2741,12 @@ int multi_update::do_updates()
uint offset= cur_table->shared;
table = cur_table->table;
+ if (!(table->map & all_tables_to_update))
+ continue;
if (table == table_to_update)
+ {
continue; // Already updated
+ }
org_updated= updated;
tmp_table= tmp_tables[cur_table->shared];
tmp_table->file->extra(HA_EXTRA_CACHE); // Change to read cache
diff --git a/sql/sql_view.cc b/sql/sql_view.cc
index 6666f2e..0863beb 100644
--- a/sql/sql_view.cc
+++ b/sql/sql_view.cc
@@ -34,7 +34,7 @@
#include "sp_cache.h"
#include "datadict.h" // dd_frm_is_view()
#include "sql_derived.h"
-#include "sql_cte.h" // check_dependencies_in_with_clauses()
+#include "sql_cte.h" // check_cte_dependencies_and_resolve_references()
#include "opt_trace.h"
#include "wsrep_mysqld.h"
@@ -429,12 +429,6 @@ bool mysql_create_view(THD *thd, TABLE_LIST *views,
lex->link_first_table_back(view, link_to_local);
view->open_type= OT_BASE_ONLY;
- if (check_dependencies_in_with_clauses(lex->with_clauses_list))
- {
- res= TRUE;
- goto err_no_relink;
- }
-
WSREP_TO_ISOLATION_BEGIN(WSREP_MYSQL_DB, NULL, NULL);
/*
@@ -1104,6 +1098,7 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
goto err;
}
+#if 0
/*
Check that table of main select do not used in subqueries.
@@ -1123,6 +1118,7 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
{
view->updatable_view= 0;
}
+#endif
if (view->with_check != VIEW_CHECK_NONE &&
!view->updatable_view)
@@ -1417,9 +1413,6 @@ bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
TABLE_LIST *tbl;
Security_context *security_ctx= 0;
- if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list))
- goto err;
-
/*
Check rights to run commands which show underlying tables.
In the optimizer trace we would not like to show trace for
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index d8ad3ae..bcc68c4 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -268,6 +268,8 @@ void _CONCAT_UNDERSCORED(turn_parser_debug_on,yyparse)()
class sp_head *sphead;
class sp_name *spname;
class sp_variable *spvar;
+/* class Query_name *query_name; */
+ class With_element_head *with_element_head;
class With_clause *with_clause;
class Virtual_column_info *virtual_column;
@@ -1747,9 +1749,11 @@ End SQL_MODE_ORACLE_SPECIFIC */
'-' '+' '*' '/' '%' '(' ')'
',' '!' '{' '}' '&' '|'
-%type <with_clause> with_clause
+%type <with_clause> with_clause opt_with_clause
-%type <lex_str_ptr> query_name
+/* %type <query_name> query_name*/
+/* %type <lex_str_ptr> query_name */
+%type <with_element_head> with_element_head
%type <ident_sys_list>
comma_separated_ident_list
@@ -3741,6 +3745,8 @@ expr_lex:
$$= $<expr_lex>1;
$$->sp_lex_in_use= true;
$$->set_item($2);
+ if ($$->check_cte_dependencies_and_resolve_references())
+ MYSQL_YYABORT;
if ($$->sphead->restore_lex(thd))
MYSQL_YYABORT;
}
@@ -4628,7 +4634,7 @@ create_select_query_expression:
| LEFT_PAREN_WITH with_clause query_expression_no_with_clause ')'
{
SELECT_LEX *first_select= $3->first_select();
- $3->set_with_clause($2);
+ $3->set_with_clause(Lex, $2);
$2->attach_to(first_select);
if (Lex->parsed_insert_select(first_select))
MYSQL_YYABORT;
@@ -8524,7 +8530,7 @@ select:
opt_procedure_or_into
{
Lex->pop_select();
- $1->set_with_clause(NULL);
+ $1->set_with_clause(Lex, NULL);
if (Lex->select_finalize($1, $3))
MYSQL_YYABORT;
}
@@ -8538,7 +8544,7 @@ select:
opt_procedure_or_into
{
Lex->pop_select();
- $2->set_with_clause($1);
+ $2->set_with_clause(Lex, $1);
$1->attach_to($2->first_select());
if (Lex->select_finalize($2, $4))
MYSQL_YYABORT;
@@ -8574,7 +8580,7 @@ select_into:
MYSQL_YYABORT;
if ($4)
unit= Lex->add_tail_to_query_expression_body(unit, $4);
- unit->set_with_clause($1);
+ unit->set_with_clause(Lex, $1);
$1->attach_to($2);
if (Lex->select_finalize(unit))
MYSQL_YYABORT;
@@ -8698,13 +8704,13 @@ select_into_query_specification:
query_expression:
query_expression_no_with_clause
{
- $1->set_with_clause(NULL);
+ $1->set_with_clause(Lex, NULL);
$$= $1;
}
| with_clause
query_expression_no_with_clause
{
- $2->set_with_clause($1);
+ $2->set_with_clause(Lex, $1);
$1->attach_to($2->first_select());
$$= $2;
}
@@ -8883,7 +8889,7 @@ subquery:
}
| '(' with_clause query_expression_no_with_clause ')'
{
- $3->set_with_clause($2);
+ $3->set_with_clause(Lex, $2);
$2->attach_to($3->first_select());
if (!($$= Lex->parsed_subselect($3)))
YYABORT;
@@ -13094,7 +13100,7 @@ update_table_list:
/* Update rows in a table */
update:
- UPDATE_SYM
+ opt_with_clause UPDATE_SYM
{
LEX *lex= Lex;
if (Lex->main_select_push())
@@ -13109,24 +13115,23 @@ update:
SELECT_LEX *slex= Lex->first_select_lex();
if (slex->table_list.elements > 1)
Lex->sql_command= SQLCOM_UPDATE_MULTI;
- else if (slex->get_table_list()->derived)
- {
- /* it is single table update and it is update of derived table */
- my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
- slex->get_table_list()->alias.str, "UPDATE");
- MYSQL_YYABORT;
- }
/*
In case of multi-update setting write lock for all tables may
be too pessimistic. We will decrease lock level if possible in
mysql_multi_update().
*/
- slex->set_lock_for_tables($3, slex->table_list.elements == 1);
+ slex->set_lock_for_tables($4, slex->table_list.elements == 1);
+ if ($1)
+ {
+ st_select_lex_unit *unit= Lex->current_select->master_unit();
+ unit->set_with_clause(Lex, $1);
+ $1->attach_to(unit->first_select());
+ }
}
opt_where_clause opt_order_clause delete_limit_clause
{
- if ($10)
- Select->order_list= *($10);
+ if ($11)
+ Select->order_list= *($11);
} stmt_end {}
;
@@ -13167,7 +13172,7 @@ opt_low_priority:
/* Delete rows from a table */
delete:
- DELETE_SYM
+ opt_with_clause DELETE_SYM
{
LEX *lex= Lex;
lex->sql_command= SQLCOM_DELETE;
@@ -13178,6 +13183,12 @@ delete:
mysql_init_select(lex);
lex->ignore= 0;
lex->first_select_lex()->order_list.empty();
+ if ($1)
+ {
+ st_select_lex_unit *unit= Lex->current_select->master_unit();
+ unit->set_with_clause(Lex, $1);
+ $1->attach_to(unit->first_select());
+ }
}
delete_part2
{ }
@@ -13224,6 +13235,9 @@ delete_single_table_for_period:
if ($2)
Lex->last_table()->period_conditions= Lex->period_conditions;
}
+/*
+ | FROM table_primary_derived
+*/
;
single_multi:
@@ -14780,15 +14794,22 @@ temporal_literal:
}
;
+opt_with_clause:
+ /* empty */ { $$= 0; }
+ | with_clause { $$= $1; }
+ ;
+
with_clause:
WITH opt_recursive
{
LEX *lex= Lex;
With_clause *with_clause=
new With_clause($2, Lex->curr_with_clause);
+ with_clause->set_tables_start_pos(lex->query_tables_last);
if (unlikely(with_clause == NULL))
MYSQL_YYABORT;
lex->derived_tables|= DERIVED_WITH;
+ lex->with_cte_resolution= true;
lex->curr_with_clause= with_clause;
with_clause->add_to_list(Lex->with_clauses_list_last_next);
if (lex->current_select &&
@@ -14798,6 +14819,7 @@ with_clause:
with_list
{
$$= Lex->curr_with_clause;
+ Lex->curr_with_clause->set_tables_end_pos(Lex->query_tables_last);
Lex->curr_with_clause= Lex->curr_with_clause->pop();
}
;
@@ -14816,7 +14838,7 @@ with_list:
with_list_element:
- query_name
+ with_element_head
opt_with_column_list
AS '(' query_expression ')' opt_cycle
{
@@ -14830,6 +14852,7 @@ with_list_element:
if (elem->set_unparsed_spec(thd, spec_start, $6.pos(),
spec_start - query_start))
MYSQL_YYABORT;
+ elem->set_tables_end_pos(lex->query_tables_last);
if ($7)
{
elem->set_cycle_list($7);
@@ -14894,12 +14917,15 @@ comma_separated_ident_list:
;
-query_name:
+with_element_head:
ident
{
- $$= (LEX_CSTRING *) thd->memdup(&$1, sizeof(LEX_CSTRING));
- if (unlikely($$ == NULL))
+ LEX_CSTRING *name=
+ (LEX_CSTRING *) thd->memdup(&$1, sizeof(LEX_CSTRING));
+ $$= new (thd->mem_root) With_element_head(name);
+ if (unlikely(name == NULL || $$ == NULL))
MYSQL_YYABORT;
+ $$->tables_pos.set_start_pos(Lex->query_tables_last);
}
;
diff --git a/sql/table.cc b/sql/table.cc
index bec89db..8bc5fe7 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -4139,7 +4139,7 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
/* Allocate bitmaps */
bitmap_size= share->column_bitmap_size;
- bitmap_count= 7;
+ bitmap_count= 8;
if (share->virtual_fields)
bitmap_count++;
@@ -4166,6 +4166,9 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
my_bitmap_init(&outparam->cond_set,
(my_bitmap_map*) bitmaps, share->fields, FALSE);
bitmaps+= bitmap_size;
+ my_bitmap_init(&outparam->value_set,
+ (my_bitmap_map*) bitmaps, share->fields, FALSE);
+ bitmaps+= bitmap_size;
my_bitmap_init(&outparam->def_rpl_write_set,
(my_bitmap_map*) bitmaps, share->fields, FALSE);
outparam->default_column_bitmaps();
@@ -5609,7 +5612,7 @@ bool TABLE_LIST::setup_underlying(THD *thd)
{
DBUG_ENTER("TABLE_LIST::setup_underlying");
- if (!view || (!field_translation && merge_underlying_list))
+ if (!field_translation && merge_underlying_list)
{
SELECT_LEX *select= get_single_select();
@@ -5722,141 +5725,163 @@ bool TABLE_LIST::prep_where(THD *thd, Item **conds,
@return Result of the check.
*/
-bool TABLE_LIST::single_table_updatable()
+bool TABLE_LIST::single_table_updatable(THD *thd)
{
- if (!updatable)
+ if (!updatable ||
+ (is_derived() && !optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE)))
return false;
- if (view && view->first_select_lex()->table_list.elements == 1)
+ if (is_view_or_derived() &&
+ get_unit()->first_select()->table_list.elements == 1)
{
/*
We need to check deeply only single table views. Multi-table views
will be turned to multi-table updates and then checked by leaf tables
*/
- return (((TABLE_LIST *)view->first_select_lex()->table_list.first)->
- single_table_updatable());
+ TABLE_LIST *first= (TABLE_LIST *)get_unit()->first_select()->table_list.first;
+ bool res= first->single_table_updatable(thd);
+ return res;
}
return true;
}
-/*
- Merge ON expressions for a view
+/**
+ Return merged WHERE clause and join conditions for a view
- SYNOPSIS
- merge_on_conds()
- thd thread handle
- table table for the VIEW
- is_cascaded TRUE <=> merge ON expressions from underlying views
+ @param thd thread handle
+ @param table table for the VIEW
+ @param[out] pcond Pointer to the built condition (NULL if none)
- DESCRIPTION
- This function returns the result of ANDing the ON expressions
- of the given view and all underlying views. The ON expressions
- of the underlying views are added only if is_cascaded is TRUE.
+ This function returns the result of ANDing the WHERE clause and the
+ join conditions of the given view.
- RETURN
- Pointer to the built expression if there is any.
- Otherwise and in the case of a failure NULL is returned.
+ @returns false for success, true for error
*/
-static Item *
-merge_on_conds(THD *thd, TABLE_LIST *table, bool is_cascaded)
+static bool merge_join_conditions(THD *thd, TABLE_LIST *table, Item **pcond)
{
- DBUG_ENTER("merge_on_conds");
-
- Item *cond= NULL;
- DBUG_PRINT("info", ("alias: %s", table->alias.str));
- if (table->on_expr)
- cond= table->on_expr->copy_andor_structure(thd);
- if (!table->view)
- DBUG_RETURN(cond);
- for (TABLE_LIST *tbl=
- (TABLE_LIST*)table->view->first_select_lex()->table_list.first;
- tbl;
- tbl= tbl->next_local)
+ DBUG_ENTER("merge_join_conditions");
+ *pcond = NULL;
+ DBUG_PRINT("info", ("alias: %s", table->alias));
+ if (table->where)
+ {
+ if (!(*pcond = table->where->copy_andor_structure(thd)))
+ DBUG_RETURN(true); /* purecov: inspected */
+ }
+ else if (table->on_expr)
+ {
+ if (!(*pcond = table->on_expr->copy_andor_structure(thd)))
+ DBUG_RETURN(true); /* purecov: inspected */
+ }
+ if (!table->nested_join && !table->is_merged_derived())
+ DBUG_RETURN(false);
+ List<TABLE_LIST> *join_list= table->is_merged_derived() ?
+ table->get_unit()->first_select()->join_list :
+ &table->nested_join->join_list;
+ List_iterator<TABLE_LIST> li(*join_list);
+ while (TABLE_LIST *tbl = li++)
{
- if (tbl->view && !is_cascaded)
+ if (tbl->is_view())
continue;
- cond= and_conds(thd, cond, merge_on_conds(thd, tbl, is_cascaded));
+ Item *cond;
+ if (merge_join_conditions(thd, tbl, &cond))
+ DBUG_RETURN(true); /* purecov: inspected */
+ if (cond && !(*pcond = and_conds(thd, *pcond, cond)))
+ DBUG_RETURN(true); /* purecov: inspected */
}
- DBUG_RETURN(cond);
+ DBUG_RETURN(false);
}
-/*
+/**
Prepare check option expression of table
- SYNOPSIS
- TABLE_LIST::prep_check_option()
- thd - thread handler
- check_opt_type - WITH CHECK OPTION type (VIEW_CHECK_NONE,
- VIEW_CHECK_LOCAL, VIEW_CHECK_CASCADED)
- we use this parameter instead of direct check of
- effective_with_check to change type of underlying
- views to VIEW_CHECK_CASCADED if outer view have
- such option and prevent processing of underlying
- view check options if outer view have just
- VIEW_CHECK_LOCAL option.
+ @param thd thread handler
+ @param is_cascaded True if parent view requests that this view's
+ filtering condition be treated as WITH CASCADED CHECK OPTION; this is for
+ recursive calls; user code should omit this argument.
- NOTE
- This method builds check option condition to use it later on
- every call (usual execution or every SP/PS call).
- This method have to be called after WHERE preparation
- (TABLE_LIST::prep_where)
+ @details
- RETURN
- FALSE - OK
- TRUE - error
+ This function builds check option condition for use in regular execution or
+ subsequent SP/PS executions.
+
+ This function must be called after the WHERE clause and join condition
+ of this and all underlying derived tables/views have been resolved.
+
+ The function will always call itself recursively for all underlying views
+ and base tables.
+
+ On first invocation, the check option condition is built bottom-up in
+ statement mem_root, and check_option_processed is set true.
+
+ On subsequent executions, check_option_processed is true and no
+ expression building is necessary. However, the function needs to assure that
+ the expression is resolved by calling fix_fields() on it.
+
+ @returns false if success, true if error
*/
bool TABLE_LIST::prep_check_option(THD *thd, uint8 check_opt_type)
{
DBUG_ENTER("TABLE_LIST::prep_check_option");
- bool is_cascaded= check_opt_type == VIEW_CHECK_CASCADED;
- TABLE_LIST *merge_underlying_list= view->first_select_lex()->get_table_list();
- for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
+ DBUG_ASSERT(is_view_or_derived());
+
+ /*
+ True if conditions of underlying views should be treated as WITH CASCADED
+ CHECK OPTION
+ */
+ for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local)
{
- /* see comment of check_opt_type parameter */
- if (tbl->view && tbl->prep_check_option(thd, (is_cascaded ?
- VIEW_CHECK_CASCADED :
- VIEW_CHECK_NONE)))
- DBUG_RETURN(TRUE);
+ if (!tbl->is_view_or_derived())
+ continue;
+ uint8 next_check_opt_type= tbl->with_check;
+ if (tbl->is_derived() || check_opt_type == VIEW_CHECK_CASCADED)
+ next_check_opt_type= check_opt_type;
+ if (tbl->is_view() &&
+ tbl->prep_check_option(thd, next_check_opt_type))
+ DBUG_RETURN(true); /* purecov: inspected */
}
- if (check_opt_type && !check_option_processed)
+ if (check_opt_type != VIEW_CHECK_NONE && !check_option_processed)
{
Query_arena *arena= thd->stmt_arena, backup;
arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test
- if (where)
+ if (merge_join_conditions(thd, this, &check_option))
+ DBUG_RETURN(true);
+
+ for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local)
{
- check_option= where->copy_andor_structure(thd);
+ if (tbl->check_option &&
+ !(check_option = and_conds(thd, check_option, tbl->check_option)))
+ DBUG_RETURN(true); /* purecov: inspected */
}
- if (is_cascaded)
+
+ if (check_option)
{
- for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
+ Item *clone= check_option->build_clone(thd);
+ // DBUG_ASSERT(clone);
+ if (clone)
{
- if (tbl->check_option)
- check_option= and_conds(thd, check_option, tbl->check_option);
+ check_option= clone;
+ check_option->cleanup();
}
}
- check_option= and_conds(thd, check_option,
- merge_on_conds(thd, this, is_cascaded));
-
if (arena)
thd->restore_active_arena(arena, &backup);
- check_option_processed= TRUE;
-
+ check_option_processed= true;
}
- if (check_option)
- {
- const char *save_where= thd->where;
- thd->where= "check option";
+ if (check_option) {
+ const char *save_where = thd->where;
+ thd->where = "check option";
if (check_option->fix_fields_if_needed_for_bool(thd, &check_option))
- DBUG_RETURN(TRUE);
- thd->where= save_where;
+ DBUG_RETURN(true); /* purecov: inspected */
+ thd->where = save_where;
}
- DBUG_RETURN(FALSE);
+
+ DBUG_RETURN(false);
}
@@ -6470,6 +6495,8 @@ void TABLE_LIST::set_check_merged()
!derived->first_select()->exclude_from_table_unique_test ||
derived->outer_select()->
exclude_from_table_unique_test);
+ if (is_derived())
+ grant.privilege= ALL_KNOWN_ACL;
}
#endif
@@ -6654,7 +6681,7 @@ LEX_CSTRING *Field_iterator_table::name()
Item *Field_iterator_table::create_item(THD *thd)
{
- SELECT_LEX *select= thd->lex->current_select;
+ SELECT_LEX *select= table->select_lex;
Item_field *item= new (thd->mem_root) Item_field(thd, &select->context, *ptr);
DBUG_ASSERT(strlen(item->name.str) == item->name.length);
@@ -6715,9 +6742,14 @@ Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
{
DBUG_RETURN(field);
}
- Name_resolution_context *context= (view->view ?
- &view->view->first_select_lex()->context:
- &thd->lex->first_select_lex()->context);
+ Name_resolution_context *context;
+ if (view->is_view())
+ context= &view->view->first_select_lex()->context;
+ else if (view->is_derived())
+ context= &view->derived->first_select()->context;
+ else
+ context= &thd->lex->first_select_lex()->context;
+
Item *item= (new (thd->mem_root)
Item_direct_view_ref(thd, context, field_ref, view->alias,
*name, view));
@@ -6788,7 +6820,10 @@ void Field_iterator_table_ref::set_field_iterator()
table_ref->alias.str));
}
/* This is a merge view, so use field_translation. */
- else if (table_ref->field_translation)
+ else if (table_ref->field_translation &&
+ !(table_ref->is_view_or_derived() &&
+ table_ref->select_lex->master_unit()->
+ thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_PREPARE))
{
DBUG_ASSERT(table_ref->is_merged_derived());
field_it= &view_field_it;
@@ -6798,7 +6833,7 @@ void Field_iterator_table_ref::set_field_iterator()
/* This is a base table or stored view. */
else
{
- DBUG_ASSERT(table_ref->table || table_ref->view);
+ DBUG_ASSERT(table_ref->table || table_ref->view || table_ref->is_merged_derived());
field_it= &table_field_it;
DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_table",
table_ref->alias.str));
@@ -6841,7 +6876,11 @@ const char *Field_iterator_table_ref::get_table_name()
if (table_ref->view)
return table_ref->view_name.str;
if (table_ref->is_derived())
+ {
+ if (table_ref->is_merged_derived())
+ return table_ref->alias.str;
return table_ref->table->s->table_name.str;
+ }
else if (table_ref->is_natural_join)
return natural_join_it.column_ref()->safe_table_name();
@@ -6856,6 +6895,8 @@ const char *Field_iterator_table_ref::get_db_name()
{
if (table_ref->view)
return table_ref->view_db.str;
+ if (table_ref->is_merged_derived())
+ return 0;
else if (table_ref->is_natural_join)
return natural_join_it.column_ref()->safe_db_name();
@@ -9180,13 +9221,14 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
/* A subquery might be forced to be materialized due to a side-effect. */
if (!is_materialized_derived() && first_select->is_mergeable() &&
optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE) &&
- !thd->lex->can_not_use_merged() &&
- !(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
- thd->lex->sql_command == SQLCOM_DELETE_MULTI) &&
+ (!thd->lex->can_not_use_merged() || is_derived()) &&
!is_recursive_with_table())
set_merged_derived();
else
+ {
+ merge_underlying_list= 0;
set_materialized_derived();
+ }
}
/*
Derived tables/view are materialized prior to UPDATE, thus we can skip
@@ -9196,7 +9238,34 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
{
set_check_materialized();
}
+ else if ((is_derived()) &&
+ (updatable= unit->first_select()->is_mergeable()))
+ {
+ List_iterator<Item> it(first_select->item_list);
+ Item *item;
+ bool updatable_field= false;
+ while ((item= it++))
+ {
+ if (item->real_item()->type() == Item::FIELD_ITEM)
+ updatable_field= true;
+ }
+ bool underlying_updatable_table= false;
+ for (TABLE_LIST *tbl= unit->first_select()->table_list.first;
+ tbl; tbl= tbl->next_local)
+ {
+ if ((tbl->is_view() && !tbl->updatable_view) ||
+ (tbl->is_derived() && !tbl->updatable) ||
+ !tbl->updatable || tbl->schema_table)
+ continue;
+ underlying_updatable_table= true;
+ break;
+ }
+ updatable= underlying_updatable_table && updatable_field;
+ }
+ int rc= 0;
+ if (!thd->lex->can_use_merged())
+ merge_underlying_list= 0;
/*
Create field translation for mergeable derived tables/views.
For derived tables field translation can be created only after
@@ -9207,10 +9276,10 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
if (is_view() ||
(unit->prepared &&
!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW)))
- create_field_translation(thd);
+ rc= create_field_translation(thd);
}
- return FALSE;
+ return MY_TEST(rc);
}
diff --git a/sql/table.h b/sql/table.h
index f2fad6c..fc4ec1f 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -1306,6 +1306,7 @@ struct TABLE
MY_BITMAP def_rpl_write_set;
MY_BITMAP eq_join_set; /* used to mark equi-joined fields */
MY_BITMAP cond_set; /* used to mark fields from sargable conditions*/
+ MY_BITMAP value_set;
/* Active column sets */
MY_BITMAP *read_set, *write_set, *rpl_write_set;
/* On INSERT: fields that the user specified a value for */
@@ -1690,7 +1691,6 @@ struct TABLE
best_range_rowid_filter_for_partial_join(uint access_key_no,
double records,
double access_cost_factor);
-
/**
System Versioning support
*/
@@ -2100,6 +2100,20 @@ struct vers_select_conds_t
struct LEX;
class Index_hint;
+
+
+struct TABLE_CHAIN
+{
+ TABLE_CHAIN() {}
+
+ TABLE_LIST **start_pos;
+ TABLE_LIST ** end_pos;
+
+ void set_start_pos(TABLE_LIST **pos) { start_pos= pos; }
+ void set_end_pos(TABLE_LIST **pos) { end_pos= pos; }
+};
+
+
struct TABLE_LIST
{
TABLE_LIST() {} /* Remove gcc warning */
@@ -2187,6 +2201,7 @@ struct TABLE_LIST
TABLE_LIST *next_local;
/* link in a global list of all queries tables */
TABLE_LIST *next_global, **prev_global;
+ TABLE_LIST *next_ordered;
LEX_CSTRING db;
LEX_CSTRING table_name;
LEX_CSTRING schema_table_name;
@@ -2434,9 +2449,13 @@ struct TABLE_LIST
/* call back function for asking handler about caching in query cache */
qc_engine_callback callback_func;
thr_lock_type lock_type;
+ enum_mdl_type mdl_type;
+ ulong table_options;
+ bool is_mdl_request_type_to_be_set;
uint outer_join; /* Which join type */
uint shared; /* Used in multi-upd */
bool updatable; /* VIEW/TABLE can be updated now */
+ bool single_table_update;
bool straight; /* optimize with prev table */
bool updating; /* for replicate-do/ignore table */
bool force_index; /* prefer index over table scan */
@@ -2612,10 +2631,10 @@ struct TABLE_LIST
This is used for single-table UPDATE/DELETE when they are modifying a
single-table VIEW.
*/
- TABLE_LIST *find_table_for_update()
+ TABLE_LIST *find_table_for_update(THD *thd)
{
TABLE_LIST *tbl= this;
- while(!tbl->is_multitable() && tbl->single_table_updatable() &&
+ while(!tbl->is_multitable() && tbl->single_table_updatable(thd) &&
tbl->merge_underlying_list)
{
tbl= tbl->merge_underlying_list;
@@ -2737,6 +2756,7 @@ struct TABLE_LIST
(derived ? DTYPE_MASK : DTYPE_VIEW)) |
DTYPE_TABLE | DTYPE_MATERIALIZE);
set_check_materialized();
+ field_translation= 0;
DBUG_VOID_RETURN;
}
bool is_multitable() const { return (derived_type & DTYPE_MULTITABLE); }
@@ -2744,9 +2764,9 @@ struct TABLE_LIST
{
derived_type|= DTYPE_MULTITABLE;
}
- bool set_as_with_table(THD *thd, With_element *with_elem);
void reset_const_table();
bool handle_derived(LEX *lex, uint phases);
+ void propagate_properties_for_mergeable_derived();
/**
@brief True if this TABLE_LIST represents an anonymous derived table,
@@ -2776,7 +2796,7 @@ struct TABLE_LIST
int fetch_number_of_rows();
bool change_refs_to_fields();
- bool single_table_updatable();
+ bool single_table_updatable(THD *thd);
bool is_inner_table_of_outer_join()
{
@@ -2826,7 +2846,7 @@ class Item;
class Field_iterator: public Sql_alloc
{
public:
- Field_iterator() {} /* Remove gcc warning */
+ Field_iterator() {} /* Remove gcc warning */
virtual ~Field_iterator() {}
virtual void set(TABLE_LIST *)= 0;
virtual void next()= 0;
@@ -2845,9 +2865,10 @@ class Field_iterator: public Sql_alloc
class Field_iterator_table: public Field_iterator
{
Field **ptr;
+ TABLE_LIST *table;
public:
- Field_iterator_table() :ptr(0) {}
- void set(TABLE_LIST *table) { ptr= table->table->field; }
+ Field_iterator_table() :ptr(0), table(0) {}
+ void set(TABLE_LIST *tl) { table= tl; ptr= tl->table->field; }
void set_table(TABLE *table) { ptr= table->field; }
void next() { ptr++; }
bool end_of_fields() { return *ptr == 0; }
1
0
[Commits] cd57505: MDEV-18511: CTE support for UPDATE and DELETE statements
by IgorBabaev 01 Oct '20
by IgorBabaev 01 Oct '20
01 Oct '20
revision-id: cd57505442f262273f5a9a50bb7795ab75d452a4 (mariadb-10.5.2-410-gcd57505)
parent(s): d34cc6b3fd995ea7f10745d3b8e730053026dca2
author: Igor Babaev
committer: Igor Babaev
timestamp: 2020-10-01 11:54:54 -0700
message:
MDEV-18511: CTE support for UPDATE and DELETE statements
MDEV-23552: Merge mergeable derived tables used at the top level of UPDATE
statements
This is the first preliminary patch for the tasks.
The patch introduces the new test files derived_update.test,
cte_update.test, view_update.test, derived_update_multi.test,
cte_update_multi.test, view_update_multi.test. All these tests +
view.test, derived.test, derived_view.test, cte_nonrecursive and
cte recursive pass with the patch.
The code of the patch is far from being final.
---
mysql-test/main/cte_nonrecursive.result | 9 +-
mysql-test/main/cte_nonrecursive.test | 2 +-
mysql-test/main/cte_update.result | 1002 ++++++++++
mysql-test/main/cte_update.test | 450 +++++
mysql-test/main/cte_update_multi.result | 2643 ++++++++++++++++++++++++++
mysql-test/main/cte_update_multi.test | 918 +++++++++
mysql-test/main/derived_update.result | 576 ++++++
mysql-test/main/derived_update.test | 270 +++
mysql-test/main/derived_update_multi.result | 2529 +++++++++++++++++++++++++
mysql-test/main/derived_update_multi.test | 860 +++++++++
mysql-test/main/view.result | 24 +-
mysql-test/main/view.test | 21 +-
mysql-test/main/view_update.result | 412 ++++
mysql-test/main/view_update.test | 244 +++
mysql-test/main/view_update_multi.result | 2685 +++++++++++++++++++++++++++
mysql-test/main/view_update_multi.test | 970 ++++++++++
sql/item_subselect.cc | 1 -
sql/sp_head.cc | 3 +-
sql/sql_base.cc | 55 +-
sql/sql_class.h | 13 +-
sql/sql_cte.cc | 396 +++-
sql/sql_cte.h | 69 +-
sql/sql_delete.cc | 13 +-
sql/sql_derived.cc | 56 +-
sql/sql_insert.cc | 23 +-
sql/sql_lex.cc | 18 +-
sql/sql_lex.h | 32 +-
sql/sql_load.cc | 2 +-
sql/sql_parse.cc | 19 +-
sql/sql_prepare.cc | 7 +-
sql/sql_select.cc | 23 +-
sql/sql_show.cc | 2 +
sql/sql_tvc.cc | 4 +-
sql/sql_update.cc | 151 +-
sql/sql_view.cc | 13 +-
sql/sql_yacc.yy | 76 +-
sql/table.cc | 261 ++-
sql/table.h | 37 +-
38 files changed, 14456 insertions(+), 433 deletions(-)
diff --git a/mysql-test/main/cte_nonrecursive.result b/mysql-test/main/cte_nonrecursive.result
index 6ff33e6..b7bb143 100644
--- a/mysql-test/main/cte_nonrecursive.result
+++ b/mysql-test/main/cte_nonrecursive.result
@@ -618,7 +618,7 @@ with t(c) as (select a from t1 where b >= 'c')
select * from t r1, t r2 where r1.c=r2.c and r2.c=4;
show create view v4;
View Create View character_set_client collation_connection
-v4 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS with t(`c`) as (select `test`.`t1`.`a` AS `c` from `test`.`t1` where `test`.`t1`.`b` >= 'c')select `r1`.`c` AS `c`,`r2`.`c` AS `d` from (`t` `r1` join (select `test`.`t1`.`a` AS `c` from `test`.`t1` where `test`.`t1`.`b` >= 'c') `r2`) where `r1`.`c` = `r2`.`c` and `r2`.`c` = 4 latin1 latin1_swedish_ci
+v4 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS with t(`c`) as (select `t1`.`a` AS `c` from `t1` where `t1`.`b` >= 'c')select `r1`.`c` AS `c`,`r2`.`c` AS `d` from (`t` `r1` join (select `t1`.`a` AS `c` from `t1` where `t1`.`b` >= 'c') `r2`) where `r1`.`c` = `r2`.`c` and `r2`.`c` = 4 latin1 latin1_swedish_ci
select * from v4;
c d
4 4
@@ -636,7 +636,6 @@ create view v1(a) as
with t as (select a from t1 where b >= 'c')
select t.a from t2,t where t2.c=t.a;
update v1 set a=0 where a > 4;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
# prepare of a query containing a definition of a with table t
prepare stmt1 from "
@@ -1021,10 +1020,10 @@ select a from t1;
show table status;
show create view v1;
View Create View character_set_client collation_connection
-v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with t as (select `s`.`a` AS `a` from (select `test`.`t1`.`a` AS `a` from `test`.`t1`) `s`), r as (select `t`.`a` AS `a` from (`test`.`t2` join `t`) where `test`.`t2`.`b` = `t`.`a`)select `r`.`a` AS `a` from `r` latin1 latin1_swedish_ci
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with t as (select `s`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `s`), r as (select `t`.`a` AS `a` from (`t2` join `t`) where `t2`.`b` = `t`.`a`)select `r`.`a` AS `a` from `r` latin1 latin1_swedish_ci
show create view v2;
View Create View character_set_client collation_connection
-v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with t as (select `s`.`a` AS `a` from (select `test`.`t1`.`a` AS `a` from `test`.`t1`) `s`), r as (select `t`.`a` AS `a` from (`test`.`t2` join `t`) where `test`.`t2`.`b` = `t`.`a`)select `test`.`t1`.`a` AS `a` from `test`.`t1` latin1 latin1_swedish_ci
+v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with t as (select `s`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `s`), r as (select `t`.`a` AS `a` from (`t2` join `t`) where `t2`.`b` = `t`.`a`)select `t1`.`a` AS `a` from `t1` latin1 latin1_swedish_ci
select * from v1;
a
2
@@ -1126,7 +1125,7 @@ NULL UNION RESULT <union4,5> ALL NULL NULL NULL NULL NULL NULL
NULL UNION RESULT <union11,12> ALL NULL NULL NULL NULL NULL NULL
NULL UNION RESULT <union1,6> ALL NULL NULL NULL NULL NULL NULL
Warnings:
-Note 1003 with cte_e as (with cte_o as (with cte_i as (select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1 union select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1)select `cte_e1`.`a` AS `a` from `cte_e` `cte_e1` where `cte_e1`.`a` > 1 union select `cte_e2`.`a` AS `a` from (with cte_o as (with cte_i as (select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 union select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t
1`.`a` > 1 and `test`.`t1`.`a` < 7) `cte_e2`
+Note 1003 with cte_e as (with cte_o as (with cte_i as (/* select#2 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)/* select#3 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)/* select#4 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1 union /* select#5 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 and `test`.`t1`.`a` > 1)/* select#1 */ select `cte_e1`.`a` AS `a` from `cte_e` `cte_e1` where `cte_e1`.`a` > 1 union /* select#6 */ select `cte_e2`.`a` AS `a` from (with cte_o as (with cte_i as (/* select#9 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 7)/* select#10 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1)/* select#11 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` < 3 and
`test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7 union /* select#12 */ select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 4 and `test`.`t1`.`a` > 1 and `test`.`t1`.`a` < 7) `cte_e2`
drop table t1;
#
# MDEV-13753: embedded CTE in a VIEW created in prepared statement
diff --git a/mysql-test/main/cte_nonrecursive.test b/mysql-test/main/cte_nonrecursive.test
index 807e2e0..4fd3cd9 100644
--- a/mysql-test/main/cte_nonrecursive.test
+++ b/mysql-test/main/cte_nonrecursive.test
@@ -345,7 +345,7 @@ drop view v1,v2,v3,v4;
create view v1(a) as
with t as (select a from t1 where b >= 'c')
select t.a from t2,t where t2.c=t.a;
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set a=0 where a > 4;
drop view v1;
diff --git a/mysql-test/main/cte_update.result b/mysql-test/main/cte_update.result
new file mode 100644
index 0000000..537cb54
--- /dev/null
+++ b/mysql-test/main/cte_update.result
@@ -0,0 +1,1002 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+# full update of mergeable cte1
+explain with cte1 as (select a from t1) update cte1 set a=10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+with cte1 as (select a from t1) update cte1 set a=10;
+select * from t1;
+a
+10
+10
+10
+10
+10
+explain with cte1 as (select * from t1) update cte1 set a=14;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+with cte1 as (select * from t1) update cte1 set a=14;
+select * from t1;
+a
+14
+14
+14
+14
+14
+# update with limit of mergeable cte1
+explain with cte1 as (select * from t1) update cte1 set cte1.a=18 limit 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+with cte1 as (select * from t1) update cte1 set cte1.a=18 limit 3;
+select * from t1;
+a
+18
+18
+18
+14
+14
+# partial update of mergeable cte1
+explain with cte1 as (select * from t1) update cte1 set cte1.a=2 where a<18;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from t1) update cte1 set cte1.a=2 where a<18;
+select * from t1;
+a
+18
+18
+18
+2
+2
+# partial update of mergeable cte1 defined as
+# full select from mergeable derived table dt1
+explain with cte1 as (select * from (select * from t1) dt1)
+update cte1 set cte1.a=22 where a>16;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from (select * from t1) dt1)
+update cte1 set cte1.a=22 where a>16;
+select * from t1;
+a
+22
+22
+22
+2
+2
+# full update of mergeable cte1 defined as
+# partial select from mergeable derived table dt1
+explain with cte1 as (select * from (select * from t1) dt1 where dt1.a > 20)
+update cte1 set cte1.a=24;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from (select * from t1) dt1 where dt1.a > 20)
+update cte1 set cte1.a=24;
+select * from t1;
+a
+24
+24
+24
+2
+2
+update t1 set a=26 limit 1;
+select * from t1;
+a
+26
+24
+24
+2
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable derived table dt1
+explain with cte1 as (select * from (select * from t1) dt1 where dt1.a < 25)
+update cte1 set cte1.a=15 where cte1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from (select * from t1) dt1 where dt1.a < 25)
+update cte1 set cte1.a=15 where cte1.a > 2;
+select * from t1;
+a
+26
+15
+15
+2
+2
+create view v1 as select * from t1;
+# partial update of mergeable cte1 defined as
+# full select from mergeable view v1
+explain with cte1 as (select * from v1)
+update cte1 set cte1.a=17 where cte1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from v1)
+update cte1 set cte1.a=17 where cte1.a between 10 and 20;
+select * from t1;
+a
+26
+17
+17
+2
+2
+# full update of mergeable cte1 defined as
+# partial select from mergeable view v1
+explain with cte1 as (select * from v1 where v1.a > 10)
+update cte1 set cte1.a=23;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from v1 where v1.a > 10)
+update cte1 set cte1.a=23;
+select * from t1;
+a
+23
+23
+23
+2
+2
+update t1 set a=28 limit 1;
+select * from t1;
+a
+28
+23
+23
+2
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable view v1
+explain with cte1 as (select * from v1 where v1.a < 27)
+update cte1 set cte1.a=19 where cte1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte1 as (select * from v1 where v1.a < 27)
+update cte1 set cte1.a=19 where cte1.a > 2;
+select * from t1;
+a
+28
+19
+19
+2
+2
+# partial update of mergeable cte2 defined as
+# full select from mergeable embedded cte1
+explain with cte2 as (with cte1 as (select * from t1) select * from cte1)
+update cte2 set cte2.a=11 where cte2.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte2 as (with cte1 as (select * from t1) select * from cte1)
+update cte2 set cte2.a=11 where cte2.a between 10 and 20;
+select * from t1;
+a
+28
+11
+11
+2
+2
+# full update of mergeable cte2 defined as
+# partial select from mergeable embedded cte1
+explain with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10)
+update cte2 set cte2.a=21;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10)
+update cte2 set cte2.a=21;
+select * from t1;
+a
+21
+21
+21
+2
+2
+update t1 set a=29 limit 1;
+select * from t1;
+a
+29
+21
+21
+2
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable embedded cte1
+explain with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27)
+update cte2 set cte2.a=13 where cte2.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+with cte2 as (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27)
+update cte2 set cte2.a=13 where cte2.a > 2;
+select * from t1;
+a
+29
+13
+13
+2
+2
+drop view v1;
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+# partial update of mergeable cte1 defined as
+# partial select from mergeable dt2 that uses mergeable dt1
+explain with cte1 as
+(select *
+from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 6;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as
+(select *
+from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 6;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+6
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable dt1 that uses mergeable cte1
+explain with cte2 as
+(select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 5 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as
+(select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 5 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+5
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+explain with cte1 as
+(select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as
+(select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 2 and 5;
+select * from t1;
+a
+4
+8
+6
+1
+5
+4
+6
+2
+drop view v1;
+# partial update of mergeable cte2 defined as
+# partial select from mergeable cte1 that uses mergeable dt1
+explain with cte2 as
+(with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as
+(with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 4 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+6
+2
+# partial update of mergeable cte3 defined as
+# partial select from mergeable cte2 that uses mergeable cte1
+explain with cte3 as
+(with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10)
+update cte3 set cte3.a=cte3.a+1 where cte3.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte3 as
+(with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10)
+update cte3 set cte3.a=cte3.a+1 where cte3.a between 4 and 7;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+7
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+explain with cte2 as
+(with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 4;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as
+(with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 4;
+select * from t1;
+a
+2
+8
+7
+1
+5
+2
+7
+1
+drop view v1;
+# partial update of mergeable cte1 defined as
+# partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+explain with cte1 as (select * from v1 where v1.a < 6)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as (select * from v1 where v1.a < 6)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 5;
+select * from t1;
+a
+2
+8
+7
+1
+6
+2
+7
+1
+drop view v1;
+# partial update of mergeable cte2 defined as
+# partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+explain with cte2 as (select * from v1 where v1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte2 as (select * from v1 where v1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 5;
+select * from t1;
+a
+1
+8
+7
+1
+6
+1
+7
+1
+drop view v1;
+# partial update of mergeable cte1 defined as
+# partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+explain with cte1 as (select * from v2 where v2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 5 and 10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+with cte1 as (select * from v2 where v2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 5 and 10;
+select * from t1;
+a
+1
+8
+8
+1
+7
+1
+8
+1
+drop view v2;
+drop view v1;
+create view v1 as select * from t1 where t1.a < 10;
+create view v2 as select * from v1;
+update v2 set a=6;
+select * from t1;
+a
+6
+6
+6
+6
+6
+6
+6
+6
+with cte1 as (select * from (select * from t1 where t1.a > 2) dt1 where dt1.a < 24)
+update cte1 set cte1.a=4 limit 1;
+select * from t1;
+a
+4
+6
+6
+6
+6
+6
+6
+6
+drop view v1, v2;
+drop table t1;
+create table t1 (a int);
+insert into t1 values (1),(2),(3),(4),(7),(10);
+create table t2 (b int);
+insert into t2 values (4),(7),(1),(3),(11);
+select * from t1;
+a
+1
+2
+3
+4
+7
+10
+select * from t2;
+b
+4
+7
+1
+3
+11
+# CTEs with DTs
+with cte1 as (select * from t1 where a <2) update cte1 set a=0;
+select * from t1;
+a
+0
+2
+3
+4
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from t1 where a>2) update cte1 set a=0 where a<10;
+select * from t1;
+a
+1
+2
+0
+0
+0
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1) update cte1 set a=1 where a>2;
+select * from t1;
+a
+1
+2
+1
+1
+1
+1
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1 where a>1)
+update cte1 set a=9 where a>2;
+select * from t1;
+a
+1
+2
+9
+9
+9
+9
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from (select * from t1) dt1) dt2)
+update cte1 set a=2 where a>7;
+select * from t1;
+a
+1
+2
+3
+4
+7
+2
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from (select * from t1) dt1) dt2 where a<8)
+update cte1 set a=2 where a>3;
+select * from t1;
+a
+1
+2
+3
+2
+2
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from (select * from t1) dt1 where a>1) dt2 where a<11)
+update cte1 set a=2 where a>6;
+select * from t1;
+a
+1
+2
+3
+4
+2
+2
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from t1),
+cte2 as (select * from cte1) update cte2 set a=9 where a>5;
+select * from t1;
+a
+1
+2
+3
+4
+9
+9
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from t1 where a>2),
+cte2 as (select * from cte1 where a>3) update cte2 set a=9 where a<9;
+select * from t1;
+a
+1
+2
+3
+9
+9
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1),
+cte2 as (select * from cte1) update cte2 set a=9 where a>3;
+select * from t1;
+a
+1
+2
+3
+9
+9
+9
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1 where a>2),
+cte2 as (select * from cte1 where a>3) update cte2 set a=9 where a<9;
+select * from t1;
+a
+1
+2
+3
+9
+9
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+create view v1 as (select * from t1);
+select * from v1;
+a
+1
+2
+3
+4
+7
+10
+# Simple view with CTEs
+with cte1 as (select * from v1) update cte1 set a=20 where a>5;
+select * from t1;
+a
+1
+2
+3
+4
+20
+20
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1 where a>3) update cte1 set a=0 where a<9;
+select * from t1;
+a
+1
+2
+3
+0
+0
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v1) dt1) update cte1 set a=0 where a<7;
+select * from t1;
+a
+0
+0
+0
+0
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v1) dt1 where a>2) update cte1 set a=0 where a<7;
+select * from t1;
+a
+1
+2
+0
+0
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1), cte2 as (select * from cte1)
+update cte2 set a=11 where a>5;
+select * from t1;
+a
+1
+2
+3
+4
+11
+11
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1 where a<7), cte2 as (select * from cte1)
+update cte2 set a=11 where a>2;
+select * from t1;
+a
+1
+2
+11
+11
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1 where a<7), cte2 as (select * from cte1 where a>3)
+update cte2 set a=11 where a>2;
+select * from t1;
+a
+1
+2
+3
+11
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+create view v2 as (select * from (select * from t2) dt1 where b < 10);
+select * from v2;
+b
+4
+7
+1
+3
+# View having Embedded DT with CTEs
+with cte1 as (select * from v2) update cte1 set b=20 where b>5;
+select * from t2;
+b
+4
+20
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2 where b>3) update cte1 set b=0 where b<9;
+select * from t2;
+b
+0
+0
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v2) dt1) update cte1 set b=0 where b<7;
+select * from t2;
+b
+0
+7
+0
+0
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v2) dt1 where b>2) update cte1 set b=0 where b<7;
+select * from t2;
+b
+0
+7
+1
+0
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v2 where b>1) dt1 where b>2) update cte1 set b=0 where b<7;
+select * from t2;
+b
+0
+7
+1
+0
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+b
+4
+11
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2 where b<15), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+b
+4
+11
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2 where b<15), cte2 as (select * from cte1 where b>3)
+update cte2 set b=11 where b>2;
+select * from t2;
+b
+11
+11
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+create view v3 as (select * from v1 where a<7);
+select * from v3;
+a
+1
+2
+3
+4
+# View having Embedded another simple view with CTEs
+with cte1 as (select * from v3) update cte1 set a=20 where a>1;
+select * from t1;
+a
+1
+20
+20
+20
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3 where a>3) update cte1 set a=0 where a<9;
+select * from t1;
+a
+1
+2
+3
+0
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v3) dt1) update cte1 set a=0 where a<4;
+select * from t1;
+a
+0
+0
+0
+4
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v3) dt1 where a>2) update cte1 set a=0 where a<4;
+select * from t1;
+a
+1
+2
+0
+4
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3), cte2 as (select * from cte1)
+update cte2 set a=11 where a<5;
+select * from t1;
+a
+11
+11
+11
+11
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3), cte2 as (select * from cte1 where a>1)
+update cte2 set a=11 where a<5;
+select * from t1;
+a
+1
+11
+11
+11
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3 where a>0), cte2 as (select * from cte1 where a>1)
+update cte2 set a=11 where a<5;
+select * from t1;
+a
+1
+11
+11
+11
+7
+10
+delete from t1;
+insert into t1 values (1),(2),(3),(4),(7),(10);
+create view v4 as (select * from v2 where b>3);
+select * from v4;
+b
+4
+7
+# View having view inside which is DT with CTEs
+with cte1 as (select * from v4) update cte1 set b=20 where b>5;
+select * from t2;
+b
+4
+20
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4 where b>3) update cte1 set b=0 where b<9;
+select * from t2;
+b
+0
+0
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v4) dt1) update cte1 set b=0 where b<7;
+select * from t2;
+b
+0
+7
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v4) dt1 where b>2) update cte1 set b=0 where b<7;
+select * from t2;
+b
+0
+7
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+b
+4
+11
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4 where b<15), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+b
+4
+11
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4 where b<15), cte2 as (select * from cte1 where b>3)
+update cte2 set b=11 where b>2;
+select * from t2;
+b
+11
+11
+1
+3
+11
+delete from t2;
+insert into t2 values (4),(7),(1),(3),(11);
+# Non-updatable CTEs with base tables only
+with cte1 as (select * from t1 union select * from t2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from t1 group by a) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from t1 group by a having count(*) >=1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select DISTINCT * from t1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from t1 limit 1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from t1 union select * from t2 limit 2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select DISTINCT * from t1 union select * from t2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+# Non-updatable CTEs with views containing DT and base table
+with cte1 as (select * from v1 union select * from v2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v1 group by a) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v1 group by a having count(*) >=1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select DISTINCT * from v1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v1 limit 1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v1 union select * from v2 limit 2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select DISTINCT * from v1 union select * from v2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+# Non-updatable CTEs with Embedded views and Embedded DT views
+with cte1 as (select * from v3 union select * from v4) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v3 group by a) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v3 group by a having count(*) >=1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select DISTINCT * from v3) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v3 limit 1) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select * from v3 union select * from v4 limit 2) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+with cte1 as (select DISTINCT * from v3 union select * from v4) update cte1 set a=0;
+ERROR HY000: The target table cte1 of the UPDATE is not updatable
+drop table t1, t2;
+drop view v1, v2, v3, v4;
diff --git a/mysql-test/main/cte_update.test b/mysql-test/main/cte_update.test
new file mode 100644
index 0000000..3f1fdd1
--- /dev/null
+++ b/mysql-test/main/cte_update.test
@@ -0,0 +1,450 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+--echo # full update of mergeable cte1
+let $q=
+with cte1 as (select a from t1) update cte1 set a=10;
+eval explain $q;
+eval $q;
+select * from t1;
+let $q=
+with cte1 as (select * from t1) update cte1 set a=14;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # update with limit of mergeable cte1
+let $q=
+with cte1 as (select * from t1) update cte1 set cte1.a=18 limit 3;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte1
+let $q=
+with cte1 as (select * from t1) update cte1 set cte1.a=2 where a<18;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # full select from mergeable derived table dt1
+let $q=
+with cte1 as (select * from (select * from t1) dt1)
+ update cte1 set cte1.a=22 where a>16;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable cte1 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+with cte1 as (select * from (select * from t1) dt1 where dt1.a > 20)
+ update cte1 set cte1.a=24;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=26 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+with cte1 as (select * from (select * from t1) dt1 where dt1.a < 25)
+ update cte1 set cte1.a=15 where cte1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+create view v1 as select * from t1;
+
+--echo # partial update of mergeable cte1 defined as
+--echo # full select from mergeable view v1
+let $q=
+with cte1 as (select * from v1)
+ update cte1 set cte1.a=17 where cte1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable cte1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+with cte1 as (select * from v1 where v1.a > 10)
+ update cte1 set cte1.a=23;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=28 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+with cte1 as (select * from v1 where v1.a < 27)
+ update cte1 set cte1.a=19 where cte1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # full select from mergeable embedded cte1
+let $q=
+with cte2 as (with cte1 as (select * from t1) select * from cte1)
+ update cte2 set cte2.a=11 where cte2.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable cte2 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+with cte2 as (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 10)
+ update cte2 set cte2.a=21;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=29 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+with cte2 as (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 27)
+ update cte2 set cte2.a=13 where cte2.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+drop view v1;
+
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable dt2 that uses mergeable dt1
+let $q=
+with cte1 as
+ (select *
+ from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+ where dt2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 6;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable cte1
+let $q=
+with cte2 as
+ (select *
+ from (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 8) dt1
+ where dt1.a > 1)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 5 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+let $q=
+with cte1 as
+ (select *
+ from (select * from v1 where v1.a < 8) dt1
+ where dt1.a > 2)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable dt1
+let $q=
+with cte2 as
+ (with cte1 as
+ (select * from (select * from t1) dt1 where dt1.a > 2)
+ select * from cte1 where cte1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte3 defined as
+--echo # partial select from mergeable cte2 that uses mergeable cte1
+let $q=
+with cte3 as
+ (with cte2 as
+ (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10)
+update cte3 set cte3.a=cte3.a+1 where cte3.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+let $q=
+with cte2 as
+ (with cte1 as
+ (select * from v1 where v1.a > 1)
+ select * from cte1 where cte1.a < 7)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 4;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+let $q=
+with cte1 as (select * from v1 where v1.a < 6)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 4 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+let $q=
+with cte2 as (select * from v1 where v1.a < 6)
+update cte2 set cte2.a=cte2.a-1 where cte2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+let $q=
+with cte1 as (select * from v2 where v2.a < 8)
+update cte1 set cte1.a=cte1.a+1 where cte1.a between 5 and 10;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+
+
+create view v1 as select * from t1 where t1.a < 10;
+create view v2 as select * from v1;
+update v2 set a=6;
+select * from t1;
+with cte1 as (select * from (select * from t1 where t1.a > 2) dt1 where dt1.a < 24)
+update cte1 set cte1.a=4 limit 1;
+select * from t1;
+drop view v1, v2;
+drop table t1;
+
+create table t1 (a int);
+insert into t1 values (1),(2),(3),(4),(7),(10);
+create table t2 (b int);
+insert into t2 values (4),(7),(1),(3),(11);
+select * from t1;
+select * from t2;
+
+--echo # CTEs with DTs
+with cte1 as (select * from t1 where a <2) update cte1 set a=0;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from t1 where a>2) update cte1 set a=0 where a<10;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1) update cte1 set a=1 where a>2;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1 where a>1)
+update cte1 set a=9 where a>2;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from (select * from t1) dt1) dt2)
+update cte1 set a=2 where a>7;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from (select * from t1) dt1) dt2 where a<8)
+update cte1 set a=2 where a>3;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from (select * from t1) dt1 where a>1) dt2 where a<11)
+update cte1 set a=2 where a>6;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from t1),
+cte2 as (select * from cte1) update cte2 set a=9 where a>5;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from t1 where a>2),
+cte2 as (select * from cte1 where a>3) update cte2 set a=9 where a<9;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1),
+cte2 as (select * from cte1) update cte2 set a=9 where a>3;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from t1) dt1 where a>2),
+cte2 as (select * from cte1 where a>3) update cte2 set a=9 where a<9;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+
+create view v1 as (select * from t1);
+select * from v1;
+
+--echo # Simple view with CTEs
+with cte1 as (select * from v1) update cte1 set a=20 where a>5;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1 where a>3) update cte1 set a=0 where a<9;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v1) dt1) update cte1 set a=0 where a<7;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v1) dt1 where a>2) update cte1 set a=0 where a<7;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1), cte2 as (select * from cte1)
+update cte2 set a=11 where a>5;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1 where a<7), cte2 as (select * from cte1)
+update cte2 set a=11 where a>2;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v1 where a<7), cte2 as (select * from cte1 where a>3)
+update cte2 set a=11 where a>2;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+
+create view v2 as (select * from (select * from t2) dt1 where b < 10);
+select * from v2;
+
+--echo # View having Embedded DT with CTEs
+with cte1 as (select * from v2) update cte1 set b=20 where b>5;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2 where b>3) update cte1 set b=0 where b<9;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v2) dt1) update cte1 set b=0 where b<7;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v2) dt1 where b>2) update cte1 set b=0 where b<7;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v2 where b>1) dt1 where b>2) update cte1 set b=0 where b<7;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2 where b<15), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v2 where b<15), cte2 as (select * from cte1 where b>3)
+update cte2 set b=11 where b>2;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+
+create view v3 as (select * from v1 where a<7);
+select * from v3;
+
+--echo # View having Embedded another simple view with CTEs
+with cte1 as (select * from v3) update cte1 set a=20 where a>1;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3 where a>3) update cte1 set a=0 where a<9;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v3) dt1) update cte1 set a=0 where a<4;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from (select * from v3) dt1 where a>2) update cte1 set a=0 where a<4;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3), cte2 as (select * from cte1)
+update cte2 set a=11 where a<5;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3), cte2 as (select * from cte1 where a>1)
+update cte2 set a=11 where a<5;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+with cte1 as (select * from v3 where a>0), cte2 as (select * from cte1 where a>1)
+update cte2 set a=11 where a<5;
+select * from t1;
+delete from t1; insert into t1 values (1),(2),(3),(4),(7),(10);
+
+create view v4 as (select * from v2 where b>3);
+select * from v4;
+
+--echo # View having view inside which is DT with CTEs
+with cte1 as (select * from v4) update cte1 set b=20 where b>5;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4 where b>3) update cte1 set b=0 where b<9;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v4) dt1) update cte1 set b=0 where b<7;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from (select * from v4) dt1 where b>2) update cte1 set b=0 where b<7;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4 where b<15), cte2 as (select * from cte1)
+update cte2 set b=11 where b>5;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+with cte1 as (select * from v4 where b<15), cte2 as (select * from cte1 where b>3)
+update cte2 set b=11 where b>2;
+select * from t2;
+delete from t2; insert into t2 values (4),(7),(1),(3),(11);
+
+--echo # Non-updatable CTEs with base tables only
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from t1 union select * from t2) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from t1 group by a) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from t1 group by a having count(*) >=1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select DISTINCT * from t1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from t1 limit 1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from t1 union select * from t2 limit 2) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select DISTINCT * from t1 union select * from t2) update cte1 set a=0;
+
+--echo # Non-updatable CTEs with views containing DT and base table
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v1 union select * from v2) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v1 group by a) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v1 group by a having count(*) >=1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select DISTINCT * from v1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v1 limit 1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v1 union select * from v2 limit 2) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select DISTINCT * from v1 union select * from v2) update cte1 set a=0;
+
+--echo # Non-updatable CTEs with Embedded views and Embedded DT views
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v3 union select * from v4) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v3 group by a) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v3 group by a having count(*) >=1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select DISTINCT * from v3) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v3 limit 1) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select * from v3 union select * from v4 limit 2) update cte1 set a=0;
+--error ER_NON_UPDATABLE_TABLE
+with cte1 as (select DISTINCT * from v3 union select * from v4) update cte1 set a=0;
+
+drop table t1, t2;
+drop view v1, v2, v3, v4;
diff --git a/mysql-test/main/cte_update_multi.result b/mysql-test/main/cte_update_multi.result
new file mode 100644
index 0000000..29d137a
--- /dev/null
+++ b/mysql-test/main/cte_update_multi.result
@@ -0,0 +1,2643 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and mergeable derived table dt1
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6) as dt1
+where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6) as dt1
+where cte1.a=dt1.b;
+a b
+5 5
+3 3
+3 3
+2 2
+2 2
+4 4
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where cte1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# multi-table update of mergeable cte1
+# and mergeable cte2
+explain with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+select * from cte1, cte2 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+select * from cte1, cte2 where cte1.a=cte2.b;
+a b
+6 6
+4 4
+4 4
+3 3
+3 3
+5 5
+6 6
+explain with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+update cte1, cte2
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with
+cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9)
+update cte1, cte2
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1 where cte1.a=cte2.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# multi-table update of mergeable cte1
+# and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+a b
+3 3
+3 3
+2 2
+2 2
+4 4
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2, v1.b=v1.b+2 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2, v1.b=v1.b+2 where cte1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+5
+9
+4
+4
+6
+5
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and non-mergeable derived table dt1
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6 group by b) as dt1
+where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6 group by b) as dt1
+where cte1.a=dt1.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where cte1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where cte1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and non-mergeable cte2
+explain with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+update cte1, cte2
+set cte1.a=cte1.a-1 where cte1.a=cte2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select * from t2 where b < 9 group by b)
+update cte1, cte2
+set cte1.a=cte1.a-1 where cte1.a=cte2.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable cte1
+# and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+explain with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2 where cte1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2 where cte1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and mergeable derived table dt2
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# update of mergeable cte2 specified as join of
+# mergeable derived table dt1 and mergeable cte1
+explain with cte2 as
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1, cte2.b=cte2.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte2 as
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1, cte2.b=cte2.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable cte3 specified as join of
+# mergeable cte1 and mergeable cte2
+explain with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a-1, cte3.b=cte3.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a-1, cte3.b=cte3.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+4
+10
+3
+9
+2
+2
+4
+4
+1
+# update of mergeable cte2 specified as join of
+# mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a+1, cte2.b=cte2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a+1, cte2.b=cte2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable cte1 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+explain with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a-1, cte1.b=cte1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a-1, cte1.b=cte1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+5
+5
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and non-mergeable derived table dt2
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte2 specified as join of
+# mergeable derived table dt1 and non-mergeable cte1
+explain with cte2 as
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte1 specified as join of
+# mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+explain with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte2 specified as join of
+# mergeable cte1 and non-mergeable dt1
+explain with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte3 specified as join of
+# mergeable cte1 and non-mergeable cte2
+explain with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte3 as
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable cte2 specified as join of
+# mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+explain with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte1 specified as join of
+# mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte2 specified as join of
+# mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte2 as
+(with cte1 as (select b from t2 where b < 7 group by b)
+select * from v1, cte1 where v1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte2 as
+(with cte1 as (select b from t2 where b < 7 group by b)
+select * from v1, cte1 where v1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable cte1 specified as join of
+# mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+explain with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as
+(select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable derived table dt1 and mergeable derived table dt2
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable derived table dt1 and mergeable cte2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+set cte1.a=cte1.a-1, dt1.b=dt1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10) dt1,
+cte2
+set cte1.a=cte1.a-1, dt1.b=dt1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5) dt1,
+v1
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable cte2 and mergeable derived table dt1
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable cte2 and mergeable cte3
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5);
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, v1.b=v1.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, v1.b=v1.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable cte1,
+# mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10;
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, v1.b=v1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, v1.b=v1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable cte1,
+# mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable derived table dt1 and mergeable derived table dt2
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable derived table dt1 and mergeable cte2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# non-mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable cte2 and mergeable derived table dt1
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 2),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable cte2 and mergeable cte3
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+cte2,
+cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select * from t2 where t2.b < 10 group by b),
+cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+cte2,
+cte3
+set cte1.a=cte1.a-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable cte1,
+# non-mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+cte1,
+cte2,
+v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a < 3),
+cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+cte1,
+cte2,
+v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable cte1,
+# non-mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+explain with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a > 2)
+update
+cte1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable cte1,
+# non-mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10 group by b;
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t1 where t1.a > 5),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+cte1,
+v1,
+cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable cte1,
+# non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+cte1,
+v1,
+v2
+where cte1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+update
+cte1,
+v1,
+v2
+set cte1.a=cte1.a+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop table t1,t2,t3;
diff --git a/mysql-test/main/cte_update_multi.test b/mysql-test/main/cte_update_multi.test
new file mode 100644
index 0000000..529d736
--- /dev/null
+++ b/mysql-test/main/cte_update_multi.test
@@ -0,0 +1,918 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and mergeable derived table dt1
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6) as dt1
+where cte1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where cte1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and mergeable cte2
+let $qs=
+with
+ cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9)
+select * from cte1, cte2 where cte1.a=cte2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9)
+update cte1, cte2
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1 where cte1.a=cte2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2, v1.b=v1.b+2 where cte1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and non-mergeable derived table dt1
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, (select b from t2 where b < 6 group by b) as dt1
+where cte1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where cte1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and non-mergeable cte2
+let $qs=
+with cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1),
+ cte2 as (select * from t2 where b < 9 group by b)
+update cte1, cte2
+set cte1.a=cte1.a-1 where cte1.a=cte2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable cte1
+--echo # and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+let $qs=
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select a from t1 where a > 1)
+update cte1, v1
+set cte1.a=cte1.a+2 where cte1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+ where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable derived table dt1 and mergeable cte1
+let $qu=
+with cte2 as
+ (with cte1 as (select b from t2 where b < 9)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1, cte2.b=cte2.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1, cte1.b=cte1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte3 specified as join of
+--echo # mergeable cte1 and mergeable cte2
+let $qu=
+with cte3 as
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6)
+ select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a-1, cte3.b=cte3.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+with cte2 as
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a+1, cte2.b=cte2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+let $qu=
+with cte1 as
+ (select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a-1, cte1.b=cte1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable derived table dt2
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+ where dt1.a=dt2.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable cte1
+let $qu=
+with cte2 as
+ (with cte1 as (select b from t2 where b < 9 group by b)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+let $qu=
+with cte1 as
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable cte1 and non-mergeable dt1
+let $qu=
+with cte2 as
+ (with cte1 as (select a from t1 where a > 1)
+ select * from
+ cte1, (select b from t2 where b < 7 group by b) dt1
+ where cte1.a=dt1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte3 specified as join of
+--echo # mergeable cte1 and non-mergeable cte2
+let $qu=
+with cte3 as
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6 group by b)
+ select * from cte1, cte2 where cte1.a=cte2.b)
+update cte3
+set cte3.a=cte3.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+let $qu=
+with cte2 as
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+with cte1 as
+ (select * from
+ v1, (select b from t2 where b < 6 group by b) dt1
+ where v1.a=dt1.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte2 specified as join of
+--echo # mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+with cte2 as
+ (with cte1 as (select b from t2 where b < 7 group by b)
+ select * from v1, cte1 where v1.a=cte1.b)
+update cte2
+set cte2.a=cte2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable cte1 specified as join of
+--echo # mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+let $qu=
+with cte1 as
+ (select * from v1, v2 where v1.a=v2.b)
+update cte1
+set cte1.a=cte1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable derived table dt1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte2
+set cte1.a=cte1.a-1, dt1.b=dt1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 5) dt1,
+ v1
+set cte1.a=cte1.a+1, dt1.b=dt1.b+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # mergeable cte2 and mergeable derived table dt1
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5)
+update
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable cte2 and mergeable cte3
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ cte2,
+ cte3
+set cte1.a=cte1.a-1, cte2.b=cte2.b-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5)
+update
+ cte1,
+ cte2,
+ v1
+set cte1.a=cte1.a+1, cte2.b=cte2.b+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5);
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, v1.b=v1.b+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10;
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ v1,
+ cte2
+set cte1.a=cte1.a-1, v1.b=v1.b-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ v2
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ v1,
+ v2
+set cte1.a=cte1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable derived table dt1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set cte1.a=cte1.a+1, dt2.c=dt2.c-dt2.d1
+where cte1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable derived table dt1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte2
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=dt1.b and dt1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable derived table dt1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=dt1.b and dt1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable cte2 and mergeable derived table dt1
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 2),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+ cte1,
+ cte2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=cte2.b and cte2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable cte2 and mergeable cte3
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10 group by b),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ cte3
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select * from t2 where t2.b < 10 group by b),
+ cte3 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ cte2,
+ cte3
+set cte1.a=cte1.a-1, cte3.c=cte3.c+cte3.d1
+where cte1.a=cte2.b and cte2.b=cte3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable cte2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ cte1,
+ cte2,
+ v1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a < 3),
+ cte2 as (select * from t2 where t2.b < 5 group by b)
+update
+ cte1,
+ cte2,
+ v1
+set cte1.a=cte1.a+1, v1.c=v1.c+v1.d1
+where cte1.a=cte2.b and cte2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable view v1 and mergeable derived table dt1
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+let $qs=
+with cte1 as (select * from t1 where t1.a > 2)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a > 2)
+update
+ cte1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set cte1.a=cte1.a+1, dt1.c=dt1.c-dt1.d1
+where cte1.a=v1.b and v1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable view v1 and mergeable cte2
+create view v1 as select * from t2 where t2.b < 10 group by b;
+let $qs=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ cte2
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t1 where t1.a > 5),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ cte1,
+ v1,
+ cte2
+set cte1.a=cte1.a-1, cte2.c=cte2.c+cte2.d1
+where cte1.a=v1.b and v1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable cte1,
+--echo # non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ cte1,
+ v1,
+ v2
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t1 where t1.a < 3)
+update
+ cte1,
+ v1,
+ v2
+set cte1.a=cte1.a+1, v2.c=v2.c+v2.d1
+where cte1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+drop table t1,t2,t3;
+
+
diff --git a/mysql-test/main/derived_update.result b/mysql-test/main/derived_update.result
new file mode 100644
index 0000000..6dce3fd
--- /dev/null
+++ b/mysql-test/main/derived_update.result
@@ -0,0 +1,576 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+# full update of mergeable derived table dt1
+explain update (select a from t1) as dt1 set a=10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update (select a from t1) as dt1 set a=10;
+select * from t1;
+a
+10
+10
+10
+10
+10
+explain update (select * from t1) as dt1 set a=14;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update (select * from t1) as dt1 set a=14;
+select * from t1;
+a
+14
+14
+14
+14
+14
+# update with limit of mergeable dt1
+explain update (select * from t1) as dt1 set dt1.a=18 limit 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update (select * from t1) as dt1 set dt1.a=18 limit 3;
+select * from t1;
+a
+18
+18
+18
+14
+14
+# partial update of mergeable dt1
+explain update (select * from t1) as dt1 set dt1.a=2 where a < 18;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from t1) as dt1 set dt1.a=2 where a < 18;
+select * from t1;
+a
+18
+18
+18
+2
+2
+# partial update of mergeable dt2 defined as
+# full select from mergeable derived table dt1
+explain update (select * from (select * from t1) as d1t) as dt2
+set dt2.a=22 where a > 16;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from (select * from t1) as d1t) as dt2
+set dt2.a=22 where a > 16;
+select * from t1;
+a
+22
+22
+22
+2
+2
+# full update of mergeable dt2 defined as
+# partial select from mergeable derived table dt1
+explain update (select * from (select * from t1) as dt1 where dt1.a > 20) as dt2
+set dt2.a=24;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from (select * from t1) as dt1 where dt1.a > 20) as dt2
+set dt2.a=24;
+select * from t1;
+a
+24
+24
+24
+2
+2
+update t1 set a=26 limit 1;
+select * from t1;
+a
+26
+24
+24
+2
+2
+# partial update of mergeable dt2 defined as
+# partial select from mergeable derived table dt1
+explain update (select * from (select * from t1) as dt1 where dt1.a < 25) as dt2
+set dt2.a=15 where dt2.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from (select * from t1) as dt1 where dt1.a < 25) as dt2
+set dt2.a=15 where dt2.a > 2;
+select * from t1;
+a
+26
+15
+15
+2
+2
+create view v1 as select * from t1;
+# partial update of mergeable dt1 defined as
+# full select from mergeable view v1
+explain update (select * from v1) dt1
+set dt1.a=17 where dt1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from v1) dt1
+set dt1.a=17 where dt1.a between 10 and 20;
+select * from t1;
+a
+26
+17
+17
+2
+2
+# full update of mergeable dt1 defined as
+# partial select from mergeable view v1
+explain update (select * from v1 where v1.a > 10) as dt1
+set dt1.a=23;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from v1 where v1.a > 10) as dt1
+set dt1.a=23;
+select * from t1;
+a
+23
+23
+23
+2
+2
+update t1 set a=28 limit 1;
+select * from t1;
+a
+28
+23
+23
+2
+2
+# partial update of mergeable dt1 defined as
+# partial select from mergeable view v1
+explain update (select * from v1 where v1.a < 27) as dt1
+set dt1.a=19 where dt1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (select * from v1 where v1.a < 27) as dt1
+set dt1.a=19 where dt1.a > 2;
+select * from t1;
+a
+28
+19
+19
+2
+2
+# partial update of mergeable dt1 defined as
+# full select from mergeable embedded cte1
+explain update (with cte1 as (select * from t1) select * from cte1) dt1
+set dt1.a=11 where dt1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (with cte1 as (select * from t1) select * from cte1) dt1
+set dt1.a=11 where dt1.a between 10 and 20;
+select * from t1;
+a
+28
+11
+11
+2
+2
+# full update of mergeable dt1 defined as
+# partial select from mergeable embedded cte1
+explain update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10) dt1
+set dt1.a=21;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 10) dt1
+set dt1.a=21;
+select * from t1;
+a
+21
+21
+21
+2
+2
+update t1 set a=29 limit 1;
+select * from t1;
+a
+29
+21
+21
+2
+2
+# partial update of mergeable cte2 defined as
+# partial select from mergeable embedded cte1
+explain update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27) dt1
+set dt1.a=13 where dt1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 27) dt1
+set dt1.a=13 where dt1.a > 2;
+select * from t1;
+a
+29
+13
+13
+2
+2
+drop view v1;
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+# partial update of mergeable dt3 defined as
+# partial select from mergeable dt2 that uses mergeable dt1
+explain update (select *
+from (select *
+from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8) dt3
+set dt3.a=dt3.a+1
+where dt3.a between 4 and 6;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select *
+from (select *
+from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8) dt3
+set dt3.a=dt3.a+1
+where dt3.a between 4 and 6;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+6
+2
+# partial update of mergeable dt2 defined as
+# partial select from mergeable dt1 that uses mergeable cte1
+explain update (select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 5 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 5 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+5
+2
+# partial update of mergeable dt2 defined as
+# partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+explain update (select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 2 and 5;
+select * from t1;
+a
+4
+8
+6
+1
+5
+4
+6
+2
+drop view v1;
+# partial update of mergeable dt2 defined as
+# partial select from mergeable cte1 that uses mergeable dt1
+explain update (with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 4 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+6
+2
+# partial update of mergeable dt1 defined as
+# partial select from mergeable cte2 that uses mergeable cte1
+explain update (with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 4 and 7;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+7
+2
+# partial update of mergeable dt1 defined as
+# partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+explain update (with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 4;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 4;
+select * from t1;
+a
+2
+8
+7
+1
+5
+2
+7
+1
+drop view v1;
+# partial update of mergeable dt2 defined as
+# partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+explain update (select * from v1 where v1.a < 6) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 4 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select * from v1 where v1.a < 6) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 4 and 5;
+select * from t1;
+a
+2
+8
+7
+1
+6
+2
+7
+1
+drop view v1;
+# partial update of mergeable dt1 defined as
+# partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+explain update (select * from v1 where v1.a < 6) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select * from v1 where v1.a < 6) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 5;
+select * from t1;
+a
+1
+8
+7
+1
+6
+1
+7
+1
+drop view v1;
+# partial update of mergeable dt1 defined as
+# partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+explain update (select * from v2 where v2.a < 8) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 5 and 10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update (select * from v2 where v2.a < 8) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 5 and 10;
+select * from t1;
+a
+1
+8
+8
+1
+7
+1
+8
+1
+drop view v2;
+drop view v1;
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a < 10;
+create view v3 as select * from v2;
+update t1 set a=10 limit 1;
+select * from t1;
+a
+10
+8
+8
+1
+7
+1
+8
+1
+update t1 set a=7 where a > 10;
+select * from t1;
+a
+10
+8
+8
+1
+7
+1
+8
+1
+update v3 set a=6;
+select * from t1;
+a
+10
+6
+6
+6
+6
+6
+6
+6
+update (select * from (select * from t1) as t where t.a < 10) as tt set tt.a=2;
+select * from t1;
+a
+10
+2
+2
+2
+2
+2
+2
+2
+update t1 set a=5 where a < 10 limit 1;
+select * from t1;
+a
+10
+5
+2
+2
+2
+2
+2
+2
+update (select * from (select * from t1 where t1.a > 2) as t where t.a < 10) as tt set tt.a=4;
+select * from t1;
+a
+10
+4
+2
+2
+2
+2
+2
+2
+create view v4 as select * from (select * from t1) t where t.a < 10;
+show create view v4;
+View Create View character_set_client collation_connection
+v4 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS select `t`.`a` AS `a` from (select `t1`.`a` AS `a` from `t1`) `t` where `t`.`a` < 10 latin1 latin1_swedish_ci
+select * from v4;
+a
+4
+2
+2
+2
+2
+2
+2
+create view v5 as select a as b from v1 where v1.a < 10;
+show create view v5;
+View Create View character_set_client collation_connection
+v5 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v5` AS select `v1`.`a` AS `b` from `v1` where `v1`.`a` < 10 latin1 latin1_swedish_ci
+drop view v4;
+create view v4 as select * from (select a as b from t1) t where t.b < 10;
+show create view v4;
+View Create View character_set_client collation_connection
+v4 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v4` AS select `t`.`b` AS `b` from (select `t1`.`a` AS `b` from `t1`) `t` where `t`.`b` < 10 latin1 latin1_swedish_ci
+select * from v4;
+b
+4
+2
+2
+2
+2
+2
+2
+delete from t1;
+insert into t1 values (3),(7),(1);
+update v4 set v4.b=12 where v4.b > 5;
+select * from t1;
+a
+3
+12
+1
+update v5 set v5.b=12 where v5.b < 3;
+select * from t1;
+a
+3
+12
+12
+create view v6 as select * from (select a as b from t1) t where t.b < 10 with check option;
+show create view v6;
+View Create View character_set_client collation_connection
+v6 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v6` AS select `t`.`b` AS `b` from (select `t1`.`a` AS `b` from `t1`) `t` where `t`.`b` < 10 WITH CASCADED CHECK OPTION latin1 latin1_swedish_ci
+create view v7 as select a as b from v1 where v1.a < 10 with check option;
+show create view v7;
+View Create View character_set_client collation_connection
+v7 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v7` AS select `v1`.`a` AS `b` from `v1` where `v1`.`a` < 10 WITH CASCADED CHECK OPTION latin1 latin1_swedish_ci
+show create view v7;
+View Create View character_set_client collation_connection
+v7 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v7` AS select `v1`.`a` AS `b` from `v1` where `v1`.`a` < 10 WITH CASCADED CHECK OPTION latin1 latin1_swedish_ci
+update v7 set v7.b=20 where v7.b < 5;
+ERROR 44000: CHECK OPTION failed `test`.`v7`
+update v6 set v6.b=15 where v6.b < 5;
+ERROR 44000: CHECK OPTION failed `test`.`v6`
+update (select * from v6 where v6.b < 5) t set t.b=7;
+select * from t1;
+a
+7
+12
+12
+drop view v1,v2,v3,v4,v5,v6,v7;
+drop table t1;
diff --git a/mysql-test/main/derived_update.test b/mysql-test/main/derived_update.test
new file mode 100644
index 0000000..797c966
--- /dev/null
+++ b/mysql-test/main/derived_update.test
@@ -0,0 +1,270 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+
+--echo # full update of mergeable derived table dt1
+let $q=
+update (select a from t1) as dt1 set a=10;
+eval explain $q;
+eval $q;
+select * from t1;
+let $q=
+update (select * from t1) as dt1 set a=14;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # update with limit of mergeable dt1
+let $q=
+update (select * from t1) as dt1 set dt1.a=18 limit 3;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1
+let $q=
+update (select * from t1) as dt1 set dt1.a=2 where a < 18;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # full select from mergeable derived table dt1
+let $q=
+update (select * from (select * from t1) as d1t) as dt2
+ set dt2.a=22 where a > 16;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable dt2 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+update (select * from (select * from t1) as dt1 where dt1.a > 20) as dt2
+ set dt2.a=24;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=26 limit 1;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable derived table dt1
+let $q=
+update (select * from (select * from t1) as dt1 where dt1.a < 25) as dt2
+ set dt2.a=15 where dt2.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+create view v1 as select * from t1;
+
+--echo # partial update of mergeable dt1 defined as
+--echo # full select from mergeable view v1
+let $q=
+update (select * from v1) dt1
+ set dt1.a=17 where dt1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable dt1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+update (select * from v1 where v1.a > 10) as dt1
+ set dt1.a=23;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=28 limit 1;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable view v1
+let $q=
+update (select * from v1 where v1.a < 27) as dt1
+ set dt1.a=19 where dt1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # full select from mergeable embedded cte1
+let $q=
+update (with cte1 as (select * from t1) select * from cte1) dt1
+ set dt1.a=11 where dt1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # full update of mergeable dt1 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+update (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 10) dt1
+ set dt1.a=21;
+eval explain $q;
+eval $q;
+select * from t1;
+update t1 set a=29 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte2 defined as
+--echo # partial select from mergeable embedded cte1
+let $q=
+update (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 27) dt1
+ set dt1.a=13 where dt1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+
+drop view v1;
+
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+
+--echo # partial update of mergeable dt3 defined as
+--echo # partial select from mergeable dt2 that uses mergeable dt1
+let $q=
+update (select *
+ from (select *
+ from (select * from t1) dt1 where dt1.a > 1) dt2
+ where dt2.a < 8) dt3
+set dt3.a=dt3.a+1
+where dt3.a between 4 and 6;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable cte1
+let $q=
+update (select *
+ from (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 8) dt1
+ where dt1.a > 1) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 5 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+let $q=
+update (select *
+ from (select * from v1 where v1.a < 8) dt1
+ where dt1.a > 2) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable dt1
+let $q=
+update (with cte1 as
+ (select * from (select * from t1) dt1 where dt1.a > 2)
+ select * from cte1 where cte1.a < 6) dt2
+set dt2.a=dt2.a-1
+where dt2.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable cte2 that uses mergeable cte1
+let $q=
+update (with cte2 as
+ (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 3)
+ select * from cte2 where cte2.a < 10) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+let $q=
+update (with cte1 as
+ (select * from v1 where v1.a > 1)
+ select * from cte1 where cte1.a < 7) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 4;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt2 defined as
+--echo # partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+let $q=
+update (select * from v1 where v1.a < 6) dt2
+set dt2.a=dt2.a+1
+where dt2.a between 4 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+let $q=
+update (select * from v1 where v1.a < 6) dt1
+set dt1.a=dt1.a-1
+where dt1.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable dt1 defined as
+--echo # partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+let $q=
+update (select * from v2 where v2.a < 8) dt1
+set dt1.a=dt1.a+1
+where dt1.a between 5 and 10;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a < 10;
+create view v3 as select * from v2;
+update t1 set a=10 limit 1;
+select * from t1;
+update t1 set a=7 where a > 10;
+select * from t1;
+update v3 set a=6;
+select * from t1;
+update (select * from (select * from t1) as t where t.a < 10) as tt set tt.a=2;
+select * from t1;
+update t1 set a=5 where a < 10 limit 1;
+select * from t1;
+update (select * from (select * from t1 where t1.a > 2) as t where t.a < 10) as tt set tt.a=4;
+select * from t1;
+create view v4 as select * from (select * from t1) t where t.a < 10;
+show create view v4;
+select * from v4;
+create view v5 as select a as b from v1 where v1.a < 10;
+show create view v5;
+drop view v4;
+create view v4 as select * from (select a as b from t1) t where t.b < 10;
+show create view v4;
+select * from v4;
+delete from t1;
+insert into t1 values (3),(7),(1);
+update v4 set v4.b=12 where v4.b > 5;
+select * from t1;
+update v5 set v5.b=12 where v5.b < 3;
+select * from t1;
+create view v6 as select * from (select a as b from t1) t where t.b < 10 with check option;
+show create view v6;
+create view v7 as select a as b from v1 where v1.a < 10 with check option;
+show create view v7;
+show create view v7;
+-- error ER_VIEW_CHECK_FAILED
+update v7 set v7.b=20 where v7.b < 5;
+-- error ER_VIEW_CHECK_FAILED
+update v6 set v6.b=15 where v6.b < 5;
+update (select * from v6 where v6.b < 5) t set t.b=7;
+select * from t1;
+
+drop view v1,v2,v3,v4,v5,v6,v7;
+drop table t1;
+
diff --git a/mysql-test/main/derived_update_multi.result b/mysql-test/main/derived_update_multi.result
new file mode 100644
index 0000000..b8e3fc9
--- /dev/null
+++ b/mysql-test/main/derived_update_multi.result
@@ -0,0 +1,2529 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and mergeable derived table dt2
+explain select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+a b
+5 5
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+set a=a+1, b=b+1 where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+set a=a+1, b=b+1 where dt1.a=dt2.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# multi-table update of mergeable derived table dt1
+# and mergeable cte1
+explain with cte1 as (select * from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select * from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+a b
+6 6
+4 4
+4 4
+3 3
+3 3
+5 5
+6 6
+explain with cte1 as (select * from t2 where b < 9)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1 where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select * from t2 where b < 9)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1 where dt1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# multi-table update of mergeable derived table dt1
+# and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+explain select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+a b
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2, v1.b=v1.b+2 where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2, v1.b=v1.b+2 where dt1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+5
+9
+4
+4
+6
+5
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and non-mergeable derived table dt2
+explain select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+set a=a+1 where dt1.a=dt2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+set a=a+1 where dt1.a=dt2.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and non-mergeable cte1
+explain with cte1 as (select * from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select * from t2 where b < 9 group by b)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1 where dt1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1 where dt1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable derived table dt1
+# and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+explain select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2 where dt1.a=v1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2 where dt1.a=v1.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable derived table dt3 specified as join of
+# mergeable derived table dt1 and mergeable derived table dt2
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1, dt3.b=dt3.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1, dt3.b=dt3.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and mergeable cte1
+explain update
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1, dt2.b=dt2.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1, dt2.b=dt2.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1, dt2.b=dt2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1, dt2.b=dt2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and mergeable cte2
+explain update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+4
+10
+3
+9
+2
+2
+4
+4
+1
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+explain update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a+1, dt1.b=dt1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a+1, dt1.b=dt1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+explain update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+5
+5
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable derived table dt3 specified as join of
+# mergeable derived table dt1 and non-mergeable derived table dt2
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and non-mergeable cte1
+explain update
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt2 specified as join of
+# mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+explain update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt2 specified as join of
+# mergeable cte1 and non-mergeable derived table dt1
+explain update
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b) dt2
+set dt2.a=dt2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b) dt2
+set dt2.a=dt2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and non-mergeable cte2
+explain update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# update of mergeable derived table dt1 specified as join of
+# mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+explain update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt2 specified as join of
+# mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain update
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b) dt2
+set dt2.a=dt2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b) dt2
+set dt2.a=dt2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select b from t2 where b < 7 group by b)
+update
+(select * from v1, cte1 where v1.a=cte1.b) dt1
+set dt1.a=dt1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select b from t2 where b < 7 group by b)
+update
+(select * from v1, cte1 where v1.a=cte1.b) dt1
+set dt1.a=dt1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable derived table dt1 specified as join of
+# mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+explain update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable derived table dt2 and mergeable derived table dt3
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable derived table dt2 and mergeable cte1
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+set dt1.a=dt1.a-1, dt2.b=dt2.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10) dt2,
+cte1
+set dt1.a=dt1.a-1, dt2.b=dt2.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5) dt2,
+v1
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable cte1 and mergeable derived table dt2
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable cte1 and mergeable cte2
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10;
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, v1.b=v1.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, v1.b=v1.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable derived table dt2 and mergeable derived table dt3
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a > 2) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable derived table dt2 and mergeable cte1
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+(select * from t2 where t2.b < 10 group by b) dt2,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# non-mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a < 3) dt1,
+(select * from t2 where t2.b < 5 group by b) dt2,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable cte1 and mergeable derived table dt2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a > 2) dt1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable cte1 and mergeable cte2
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+cte1,
+cte2
+set dt1.a=dt1.a-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+# update of join of mergeable derived table dt1,
+# non-mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+(select * from t1 where t1.a < 3) dt1,
+cte1,
+v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable derived table dt1,
+# non-mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a > 2) dt1,
+v1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# non-mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10 group by b;
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+(select * from t1 where t1.a > 5) dt1,
+v1,
+cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable derived table dt1,
+# non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+where dt1.a=v1.b and v1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+(select * from t1 where t1.a < 3) dt1,
+v1,
+v2
+set dt1.a=dt1.a+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop table t1,t2,t3;
diff --git a/mysql-test/main/derived_update_multi.test b/mysql-test/main/derived_update_multi.test
new file mode 100644
index 0000000..853b760
--- /dev/null
+++ b/mysql-test/main/derived_update_multi.test
@@ -0,0 +1,860 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and mergeable derived table dt2
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+set a=a+1, b=b+1 where dt1.a=dt2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and mergeable cte1
+let $qs=
+with cte1 as (select * from t2 where b < 9)
+select * from
+ (select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1 where dt1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and mergeable view v1
+create view v1 as select * from t2 where b < 5;
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2, v1.b=v1.b+2 where dt1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and non-mergeable derived table dt2
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+set a=a+1 where dt1.a=dt2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and non-mergeable cte1
+let $qs=
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from
+ (select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9 group by b)
+update
+(select a from t1 where a > 1) as dt1, cte1
+set dt1.a=dt1.a-1 where dt1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable derived table dt1
+--echo # and non-mergeable view v1
+create view v1 as select * from t2 where b < 5 group by b;
+let $qs=
+select * from
+ (select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+(select a from t1 where a > 1) as dt1, v1
+set dt1.a=dt1.a+2 where dt1.a=v1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable derived table dt3 specified as join of
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+ where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1, dt3.b=dt3.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and mergeable cte1
+let $qu=
+update
+ (with cte1 as (select b from t2 where b < 9)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1, dt2.b=dt2.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1, dt2.b=dt2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and mergeable cte2
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6)
+ select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a+1, dt1.b=dt1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+let $qu=
+update
+ (select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a-1, dt1.b=dt1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable derived table dt3 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable derived table dt2
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+ where dt1.a=dt2.b) dt3
+set dt3.a=dt3.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable cte1
+let $qu=
+update
+ (with cte1 as (select b from t2 where b < 9 group by b)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b) dt2
+set dt2.a=dt2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+let $qu=
+update
+ (select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b) dt2
+set dt2.a=dt2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable cte1 and non-mergeable derived table dt1
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1)
+ select * from
+ cte1, (select b from t2 where b < 7 group by b) dt1
+ where cte1.a=dt1.b) dt2
+set dt2.a=dt2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and non-mergeable cte2
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6 group by b)
+ select * from cte1, cte2 where cte1.a=cte2.b) dt1
+set dt1.a=dt1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable cte1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+let $qu=
+update
+ (with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b) dt1
+set dt1.a=dt1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt2 specified as join of
+--echo # mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+update
+ (select * from
+ v1, (select b from t2 where b < 6 group by b) dt1
+ where v1.a=dt1.b) dt2
+set dt2.a=dt2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qu=
+with cte1 as (select b from t2 where b < 7 group by b)
+update
+ (select * from v1, cte1 where v1.a=cte1.b) dt1
+set dt1.a=dt1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable derived table dt1 specified as join of
+--echo # mergeable view v1 and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+let $qu=
+update
+ (select * from v1, v2 where v1.a=v2.b) dt1
+set dt1.a=dt1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable derived table dt2 and mergeable derived table dt3
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable derived table dt2 and mergeable cte1
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10) dt2,
+ cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10) dt2,
+ cte1
+set dt1.a=dt1.a-1, dt2.b=dt2.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5) dt2,
+ v1
+set dt1.a=dt1.a+1, dt2.b=dt2.b+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable cte1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable cte1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+set dt1.a=dt1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+set dt1.a=dt1.a+1, cte1.b=cte1.b+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10;
+let $qs=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+set dt1.a=dt1.a-1, v1.b=v1.b-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+set dt1.a=dt1.a+1, v1.b=v1.b+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable derived table dt2 and mergeable derived table dt3
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt3
+set dt1.a=dt1.a+1, dt3.c=dt3.c-dt3.d1
+where dt1.a=dt2.b and dt2.b=dt3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable derived table dt2 and mergeable cte1
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10 group by b) dt2,
+ cte1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ (select * from t2 where t2.b < 10 group by b) dt2,
+ cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=dt2.b and dt2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable derived table dt2 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ v1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ (select * from t2 where t2.b < 5 group by b) dt2,
+ v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=dt2.b and dt2.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable cte1 and mergeable derived table dt2
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ (select * from t1 where t1.a > 2) dt1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=cte1.b and cte1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable cte1 and mergeable cte2
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ cte1,
+ cte2
+set dt1.a=dt1.a-1, cte2.c=cte2.c+cte2.d1
+where dt1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable cte1 and mergeable view v1
+create view v1 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ (select * from t1 where t1.a < 3) dt1,
+ cte1,
+ v1
+set dt1.a=dt1.a+1, v1.c=v1.c+v1.d1
+where dt1.a=cte1.b and cte1.b=v1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable view v1 and mergeable derived table dt2
+create view v1 as (select * from t2 where t2.b < 5 group by b);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a > 2) dt1,
+ v1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set dt1.a=dt1.a+1, dt2.c=dt2.c-dt2.d1
+where dt1.a=v1.b and v1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable view v1 and mergeable cte1
+create view v1 as select * from t2 where t2.b < 10 group by b;
+let $qs=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ (select * from t1 where t1.a > 5) dt1,
+ v1,
+ cte1
+set dt1.a=dt1.a-1, cte1.c=cte1.c+cte1.d1
+where dt1.a=v1.b and v1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable derived table dt1,
+--echo # non-mergeable view v1 and mergeable view v2
+create view v1 as select * from t2 where t2.b < 5 group by b;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ (select * from t1 where t1.a < 3) dt1,
+ v1,
+ v2
+set dt1.a=dt1.a+1, v2.c=v2.c+v2.d1
+where dt1.a=v1.b and v1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+drop table t1,t2,t3;
diff --git a/mysql-test/main/view.result b/mysql-test/main/view.result
index 55d95b2..b3a45bf 100644
--- a/mysql-test/main/view.result
+++ b/mysql-test/main/view.result
@@ -1114,13 +1114,13 @@ ERROR 44000: CHECK OPTION failed `test`.`v2`
insert into v3 values (0);
ERROR 44000: CHECK OPTION failed `test`.`v3`
insert into v2 values (2);
+ERROR 44000: CHECK OPTION failed `test`.`v2`
insert into v3 values (2);
ERROR 44000: CHECK OPTION failed `test`.`v3`
select * from t1;
a
1
1
-2
drop view v3,v2,v1;
drop table t1;
create table t1 (a int, primary key (a));
@@ -1955,7 +1955,6 @@ insert into t1 values (19,41,32);
create view v1 as select f59, f60 from t1 where f59 in
(select f59 from t1);
update v1 set f60=2345;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
drop table t1;
create table t1 (s1 int);
@@ -6490,56 +6489,47 @@ CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
insert into v1 values (-300);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s1=s1+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
CREATE VIEW v1 AS SELECT s1,s2 FROM (SELECT s1 as s2 FROM t1 WHERE s1 <
100) x, t1 WHERE t1.s1=x.s2;
select * from v1;
s1 s2
1 1
-2 2
3 3
+4 4
-200 -200
insert into v1 (s1) values (-300);
update v1 set s1=s1+1;
select * from v1;
s1 s2
2 2
-3 3
4 4
+5 5
-199 -199
-299 -299
select * from t1;
s1
2
-3
4
+5
200
-199
-299
insert into v1(s2) values (-300);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s2=s2+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM t1 WHERE s1
< 100) AS x;
insert into v1 values (-300);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s1=s1+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
drop view v1;
CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
< 100) as xx WHERE s1>1) AS x;
-insert into v1 values (-300);
-ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set s1=s1+1;
-ERROR HY000: The target table v1 of the UPDATE is not updatable
create view v2 as select * from v1;
-insert into v2 values (-300);
-ERROR HY000: The target table v2 of the INSERT is not insertable-into
update v2 set s1=s1+1;
-ERROR HY000: The target table v2 of the UPDATE is not updatable
drop view v1, v2;
drop table t1;
#
@@ -6554,7 +6544,7 @@ INSERT INTO t3 VALUES (1),(8);
CREATE VIEW v1 AS SELECT * FROM t1 LEFT JOIN ( SELECT t2.* FROM t2 INNER JOIN t3 ON ( k = j ) ) AS alias1 ON ( i = j );
show create view v1;
View Create View character_set_client collation_connection
-v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `test`.`t1`.`i` AS `i`,`alias1`.`j` AS `j` from (`test`.`t1` left join (select `test`.`t2`.`j` AS `j` from (`test`.`t2` join `test`.`t3` on(`test`.`t3`.`k` = `test`.`t2`.`j`))) `alias1` on(`test`.`t1`.`i` = `alias1`.`j`)) latin1 latin1_swedish_ci
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`i` AS `i`,`alias1`.`j` AS `j` from (`t1` left join (select `t2`.`j` AS `j` from (`t2` join `t3` on(`t3`.`k` = `t2`.`j`))) `alias1` on(`t1`.`i` = `alias1`.`j`)) latin1 latin1_swedish_ci
SELECT * FROM t1 LEFT JOIN ( SELECT t2.* FROM t2 INNER JOIN t3 ON ( k = j ) ) AS alias1 ON ( i = j );
i j
3 NULL
@@ -6637,7 +6627,7 @@ CREATE VIEW v AS SELECT * FROM ( SELECT * FROM t ) sq;
DROP TABLE IF EXISTS t;
SHOW CREATE VIEW v;
View Create View character_set_client collation_connection
-v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `sq`.`i` AS `i` from (select `test`.`t`.`i` AS `i` from `test`.`t`) `sq` latin1 latin1_swedish_ci
+v CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select `sq`.`i` AS `i` from (select `test`.`t`.`i` AS `i` from `t`) `sq` latin1 latin1_swedish_ci
Warnings:
Warning 1356 View 'test.v' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
DROP VIEW v;
@@ -6712,7 +6702,7 @@ EXECUTE stmt;
DEALLOCATE PREPARE stmt;
show create view v1;
View Create View character_set_client collation_connection
-v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `testalias`.`testcase` AS `testcase` from (select case when 1 in (select `test`.`t1`.`a` from `test`.`t1` where `test`.`t1`.`a` < 2) then 1 end AS `testcase`) `testalias` latin1 latin1_swedish_ci
+v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `testalias`.`testcase` AS `testcase` from (select case when 1 in (select `t1`.`a` from `t1` where `t1`.`a` < 2) then 1 end AS `testcase`) `testalias` latin1 latin1_swedish_ci
SELECT * FROM v1;
testcase
1
diff --git a/mysql-test/main/view.test b/mysql-test/main/view.test
index 1632e61..6df36ff 100644
--- a/mysql-test/main/view.test
+++ b/mysql-test/main/view.test
@@ -1020,6 +1020,7 @@ insert into v3 values (1);
insert into v2 values (0);
-- error ER_VIEW_CHECK_FAILED
insert into v3 values (0);
+-- error ER_VIEW_CHECK_FAILED
insert into v2 values (2);
-- error ER_VIEW_CHECK_FAILED
insert into v3 values (2);
@@ -1734,7 +1735,7 @@ create table t1 (f59 int, f60 int, f61 int);
insert into t1 values (19,41,32);
create view v1 as select f59, f60 from t1 where f59 in
(select f59 from t1);
--- error ER_NON_UPDATABLE_TABLE
+# -- error ER_NON_UPDATABLE_TABLE
update v1 set f60=2345;
drop view v1;
drop table t1;
@@ -6192,7 +6193,7 @@ CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
--error ER_NON_INSERTABLE_TABLE
insert into v1 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s1=s1+1;
drop view v1;
@@ -6207,7 +6208,7 @@ select * from v1;
select * from t1;
--error ER_NON_INSERTABLE_TABLE
insert into v1(s2) values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s2=s2+1;
drop view v1;
@@ -6217,7 +6218,7 @@ CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM t1 WHERE s1
--error ER_NON_INSERTABLE_TABLE
insert into v1 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s1=s1+1;
drop view v1;
@@ -6225,16 +6226,16 @@ drop view v1;
CREATE VIEW v1 AS SELECT * FROM (SELECT s1 FROM (SELECT s1 FROM t1 WHERE s1
< 100) as xx WHERE s1>1) AS x;
---error ER_NON_INSERTABLE_TABLE
-insert into v1 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_INSERTABLE_TABLE
+# insert into v1 values (-300);
+# --error ER_NON_UPDATABLE_TABLE
update v1 set s1=s1+1;
create view v2 as select * from v1;
---error ER_NON_INSERTABLE_TABLE
-insert into v2 values (-300);
---error ER_NON_UPDATABLE_TABLE
+# --error ER_NON_INSERTABLE_TABLE
+# insert into v2 values (-300);
+# --error ER_NON_UPDATABLE_TABLE
update v2 set s1=s1+1;
drop view v1, v2;
diff --git a/mysql-test/main/view_update.result b/mysql-test/main/view_update.result
new file mode 100644
index 0000000..0ec43fd
--- /dev/null
+++ b/mysql-test/main/view_update.result
@@ -0,0 +1,412 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+# full update of mergeable view v1
+create view v1 as select * from t1;
+explain update v1 set a=10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update v1 set a=10;
+select * from t1;
+a
+10
+10
+10
+10
+10
+explain update v1 set a=14;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update v1 set a=14;
+select * from t1;
+a
+14
+14
+14
+14
+14
+# update with limit of mergeable view v1
+explain update v1 set v1.a=18 limit 3;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5
+update v1 set v1.a=18 limit 3;
+select * from t1;
+a
+18
+18
+18
+14
+14
+# partial update of mergeable view v1
+explain update v1 set v1.a=2 where a<18;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=2 where a<18;
+select * from t1;
+a
+18
+18
+18
+2
+2
+drop view v1;
+# partial update of mergeable v1 defined as
+# full select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1;
+explain update v1 set v1.a=22 where a>16;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=22 where a>16;
+select * from t1;
+a
+22
+22
+22
+2
+2
+drop view v1;
+# full update of mergeable v1 defined as
+# partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 20;
+explain update v1 set v1.a=24;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=24;
+select * from t1;
+a
+24
+24
+24
+2
+2
+drop view v1;
+update t1 set a=26 limit 1;
+select * from t1;
+a
+26
+24
+24
+2
+2
+# partial update of mergeable v1 defined as
+# partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a < 25;
+explain update v1 set v1.a=15 where v1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=15 where v1.a > 2;
+select * from t1;
+a
+26
+15
+15
+2
+2
+drop view v1;
+create view v1 as select * from t1;
+# partial update of mergeable v2 defined as
+# full select from mergeable view v1
+create view v2 as select * from v1;
+explain update v2 set v2.a=17 where v2.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v2 set v2.a=17 where v2.a between 10 and 20;
+select * from t1;
+a
+26
+17
+17
+2
+2
+drop view v2;
+# full update of mergeable v2 defined as
+# partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a > 10;
+explain update v2 set v2.a=23;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v2 set v2.a=23;
+select * from t1;
+a
+23
+23
+23
+2
+2
+drop view v2;
+update t1 set a=28 limit 1;
+select * from t1;
+a
+28
+23
+23
+2
+2
+# partial update of mergeable cte1 defined as
+# partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a < 27;
+explain update v2 set v2.a=19 where v2.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v2 set v2.a=19 where v2.a > 2;
+select * from t1;
+a
+28
+19
+19
+2
+2
+drop view v2;
+drop view v1;
+# partial update of mergeable v1 defined as
+# full select from mergeable cte1
+create view v1 as with cte1 as (select * from t1) select * from cte1;
+explain update v1 set v1.a=11 where v1.a between 10 and 20;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=11 where v1.a between 10 and 20;
+select * from t1;
+a
+28
+11
+11
+2
+2
+drop view v1;
+# full update of mergeable v1 defined as
+# partial select from mergeable cte1
+create view v1 as
+with cte1 as (select * from t1)select * from cte1 where cte1.a > 10;
+explain update v1 set v1.a=21;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=21;
+select * from t1;
+a
+21
+21
+21
+2
+2
+drop view v1;
+update t1 set a=29 limit 1;
+select * from t1;
+a
+29
+21
+21
+2
+2
+# partial update of mergeable v1 defined as
+# partial select from mergeable embedded cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a < 27;
+explain update v1 set v1.a=13 where v1.a > 2;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where
+update v1 set v1.a=13 where v1.a > 2;
+select * from t1;
+a
+29
+13
+13
+2
+2
+drop view v1;
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+# partial update of mergeable v1 defined as
+# partial select from mergeable dt2 that uses mergeable dt1
+create view v1 as
+(select *
+from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+where dt2.a < 8);
+explain update v1 set v1.a=v1.a+1 where v1.a between 4 and 6;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 6;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+6
+2
+drop view v1;
+# partial update of mergeable v1 defined as
+# partial select from mergeable dt1 that uses mergeable cte1
+create view v1 as
+(select *
+from (with cte1 as (select * from t1)
+select * from cte1 where cte1.a < 8) dt1
+where dt1.a > 1);
+explain update v1 set v1.a=v1.a-1 where v1.a between 5 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a-1 where v1.a between 5 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+5
+2
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+create view v2 as
+select *
+from (select * from v1 where v1.a < 8) dt1
+where dt1.a > 2;
+explain update v2 set v2.a=v2.a+1 where v2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a+1 where v2.a between 2 and 5;
+select * from t1;
+a
+4
+8
+6
+1
+5
+4
+6
+2
+drop view v2;
+drop view v1;
+# partial update of mergeable v1 defined as
+# partial select from mergeable cte1 that uses mergeable dt1
+create view v1 as
+with cte1 as
+(select * from (select * from t1) dt1 where dt1.a > 2)
+select * from cte1 where cte1.a < 6;
+explain update v1 set v1.a=v1.a-1 where v1.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a-1 where v1.a between 4 and 7;
+select * from t1;
+a
+3
+8
+6
+1
+4
+3
+6
+2
+drop view v1;
+# partial update of mergeable v1 defined as
+# partial select from mergeable cte2 that uses mergeable cte1
+create view v1 as
+with cte2 as
+(with cte1 as (select * from t1)
+select * from cte1 where cte1.a > 3)
+select * from cte2 where cte2.a < 10;
+explain update v1 set v1.a=v1.a+1 where v1.a between 4 and 7;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 7;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+7
+2
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as
+with cte1 as
+(select * from v1 where v1.a > 1)
+select * from cte1 where cte1.a < 7;
+explain update v2 set v2.a=v2.a-1 where v2.a between 2 and 4;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 4;
+select * from t1;
+a
+2
+8
+7
+1
+5
+2
+7
+1
+drop view v2;
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+create view v2 as select * from v1 where v1.a < 6;
+explain update v2 set v2.a=v2.a+1 where v2.a between 4 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a+1 where v2.a between 4 and 5;
+select * from t1;
+a
+2
+8
+7
+1
+6
+2
+7
+1
+drop view v2;
+drop view v1;
+# partial update of mergeable v2 defined as
+# partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+create view v2 as select * from v1 where v1.a < 6;
+explain update v2 set v2.a=v2.a-1 where v2.a between 2 and 5;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 5;
+select * from t1;
+a
+1
+8
+7
+1
+6
+1
+7
+1
+drop view v2;
+drop view v1;
+# partial update of mergeable v3 defined as
+# partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+create view v3 as select * from v2 where v2.a < 8;
+explain update v3 set v3.a=v3.a+1 where v3.a between 5 and 10;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+update v3 set v3.a=v3.a+1 where v3.a between 5 and 10;
+select * from t1;
+a
+1
+8
+8
+1
+7
+1
+8
+1
+drop view v3;
+drop view v2;
+drop view v1;
+drop table t1;
diff --git a/mysql-test/main/view_update.test b/mysql-test/main/view_update.test
new file mode 100644
index 0000000..1acf926
--- /dev/null
+++ b/mysql-test/main/view_update.test
@@ -0,0 +1,244 @@
+create table t1 (a int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4);
+--echo # full update of mergeable view v1
+create view v1 as select * from t1;
+let $q=
+update v1 set a=10;
+eval explain $q;
+eval $q;
+select * from t1;
+let $q=
+update v1 set a=14;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # update with limit of mergeable view v1
+let $q=
+update v1 set v1.a=18 limit 3;
+eval explain $q;
+eval $q;
+select * from t1;
+--echo # partial update of mergeable view v1
+let $q=
+update v1 set v1.a=2 where a<18;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # full select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1;
+let $q=
+update v1 set v1.a=22 where a>16;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # full update of mergeable v1 defined as
+--echo # partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 20;
+let $q=
+update v1 set v1.a=24;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+update t1 set a=26 limit 1;
+select * from t1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable derived table dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a < 25;
+let $q=
+update v1 set v1.a=15 where v1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+
+create view v1 as select * from t1;
+
+--echo # partial update of mergeable v2 defined as
+--echo # full select from mergeable view v1
+create view v2 as select * from v1;
+let $q=
+update v2 set v2.a=17 where v2.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+--echo # full update of mergeable v2 defined as
+--echo # partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a > 10;
+let $q=
+update v2 set v2.a=23;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+
+update t1 set a=28 limit 1;
+select * from t1;
+--echo # partial update of mergeable cte1 defined as
+--echo # partial select from mergeable view v1
+create view v2 as select * from v1 where v1.a < 27;
+let $q=
+update v2 set v2.a=19 where v2.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # full select from mergeable cte1
+create view v1 as with cte1 as (select * from t1) select * from cte1;
+let $q=
+update v1 set v1.a=11 where v1.a between 10 and 20;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # full update of mergeable v1 defined as
+--echo # partial select from mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1)select * from cte1 where cte1.a > 10;
+let $q=
+update v1 set v1.a=21;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+update t1 set a=29 limit 1;
+select * from t1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable embedded cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a < 27;
+let $q=
+update v1 set v1.a=13 where v1.a > 2;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+
+delete from t1;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable dt2 that uses mergeable dt1
+create view v1 as
+ (select *
+ from (select * from (select * from t1) dt1 where dt1.a > 1) dt2
+ where dt2.a < 8);
+let $q=
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 6;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable dt1 that uses mergeable cte1
+create view v1 as
+ (select *
+ from (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a < 8) dt1
+ where dt1.a > 1);
+let $q=
+update v1 set v1.a=v1.a-1 where v1.a between 5 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable dt1 that uses mergeable view v1
+create view v1 as select * from t1;
+create view v2 as
+ select *
+ from (select * from v1 where v1.a < 8) dt1
+ where dt1.a > 2;
+let $q=
+update v2 set v2.a=v2.a+1 where v2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable cte1 that uses mergeable dt1
+create view v1 as
+ with cte1 as
+ (select * from (select * from t1) dt1 where dt1.a > 2)
+ select * from cte1 where cte1.a < 6;
+let $q=
+update v1 set v1.a=v1.a-1 where v1.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v1 defined as
+--echo # partial select from mergeable cte2 that uses mergeable cte1
+create view v1 as
+ with cte2 as
+ (with cte1 as (select * from t1)
+ select * from cte1 where cte1.a > 3)
+ select * from cte2 where cte2.a < 10;
+let $q=
+update v1 set v1.a=v1.a+1 where v1.a between 4 and 7;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable cte1 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as
+ with cte1 as
+ (select * from v1 where v1.a > 1)
+ select * from cte1 where cte1.a < 7;
+let $q=
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 4;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable v1 that uses mergeable dt1
+create view v1 as select * from (select * from t1) dt1 where dt1.a > 2;
+create view v2 as select * from v1 where v1.a < 6;
+let $q=
+update v2 set v2.a=v2.a+1 where v2.a between 4 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v2 defined as
+--echo # partial select from mergeable v1 that uses mergeable cte1
+create view v1 as
+ with cte1 as (select * from t1) select * from cte1 where cte1.a > 1;
+create view v2 as select * from v1 where v1.a < 6;
+let $q=
+update v2 set v2.a=v2.a-1 where v2.a between 2 and 5;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v2;
+drop view v1;
+--echo # partial update of mergeable v3 defined as
+--echo # partial select from mergeable v2 that uses mergeable v1
+create view v1 as select a from t1;
+create view v2 as select * from v1 where v1.a > 3;
+create view v3 as select * from v2 where v2.a < 8;
+let $q=
+update v3 set v3.a=v3.a+1 where v3.a between 5 and 10;
+eval explain $q;
+eval $q;
+select * from t1;
+drop view v3;
+drop view v2;
+drop view v1;
+
+
+drop table t1;
+
+
diff --git a/mysql-test/main/view_update_multi.result b/mysql-test/main/view_update_multi.result
new file mode 100644
index 0000000..3b6b49f
--- /dev/null
+++ b/mysql-test/main/view_update_multi.result
@@ -0,0 +1,2685 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable view v1
+# and mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain select * from v1, (select b from t2 where b < 6) as dt1
+where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from v1, (select b from t2 where b < 6) as dt1
+where v1.a=dt1.b;
+a b
+5 5
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update v1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where v1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select * from t2 where b < 9)
+select * from v1, cte1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+with cte1 as (select * from t2 where b < 9)
+select * from v1, cte1 where v1.a=cte1.b;
+a b
+6 6
+4 4
+4 4
+3 3
+3 3
+5 5
+6 6
+explain with cte1 as (select * from t2 where b < 9)
+update v1, cte1
+set v1.a=v1.a-1, cte1.b=cte1.b-1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+with cte1 as (select * from t2 where b < 9)
+update v1, cte1
+set v1.a=v1.a-1, cte1.b=cte1.b-1 where v1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5;
+explain select * from v1, v2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+select * from v1, v2 where v1.a=v2.b;
+a b
+3 3
+3 3
+2 2
+2 2
+4 4
+explain update v1, v2
+set v1.a=v1.a+2, v2.b=v2.b+2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1, v2
+set v1.a=v1.a+2, v2.b=v2.b+2 where v1.a=v2.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+5
+9
+4
+4
+6
+5
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable view v1;
+# and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+explain select * from v1, v2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, v2 where v1.a=v2.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6) as dt2
+where dt1.a=dt2.b;
+explain update v1
+set v1.a=v1.a+1, v1.b=v1.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1
+set v1.a=v1.a+1, v1.b=v1.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+6
+10
+4
+9
+3
+3
+5
+6
+1
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and mergeable cte1
+create view v1 as
+with cte1 as (select b from t2 where b < 9)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+explain update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+5
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+explain update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable cte1 and mergeable cte2
+create view v1 as
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6)
+select * from cte1, cte2 where cte1.a=cte2.b;
+explain update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+4
+10
+3
+9
+2
+2
+4
+4
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+explain update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+4
+9
+3
+3
+5
+5
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v3 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+explain update v3
+set v3.a=v3.a-1, v3.b=v3.b-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+update v3
+set v3.a=v3.a-1, v3.b=v3.b-1;
+select * from t1;
+a
+3
+8
+7
+1
+5
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+5
+5
+1
+drop view v3;
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+# multi-table update of mergeable view v1;
+# and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+explain select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+a b
+3 3
+4 4
+3 3
+5 5
+2 2
+explain update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+explain with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+a b
+4 4
+5 5
+4 4
+6 6
+3 3
+explain with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# multi-table update of mergeable view v1
+# and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+explain select * from v1, v2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select * from v1, v2 where v1.a=v2.b;
+a b
+3 3
+4 4
+3 3
+2 2
+explain update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+select * from t1;
+a
+5
+8
+7
+1
+6
+5
+5
+4
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and non-mergeable derived table dt2
+create view v1 as
+select * from
+(select a from t1 where a > 1) as dt1,
+(select b from t2 where b < 6 group by b) as dt2
+where dt1.a=dt2.b;
+explain update v1
+set v1.a=v1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+6
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable derived table dt1 and non-mergeable cte1
+create view v1 as
+with cte1 as (select b from t2 where b < 9 group by b)
+select * from
+(select a from t1 where a > 1) as dt1, cte1
+where dt1.a=cte1.b;
+explain update v1
+set v1.a=v1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+create view v2 as
+select * from
+(select a from t1 where a > 1) as dt1, v1
+where dt1.a=v1.b;
+explain update v2
+set v2.a=v2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable cte1 and non-mergeable derived table dt1
+create view v1 as
+with cte1 as (select a from t1 where a > 1)
+select * from
+cte1, (select b from t2 where b < 7 group by b) dt1
+where cte1.a=dt1.b;
+explain update v1
+set v1.a=v1.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v1 specified as join of
+# mergeable cte1 and non-mergeable cte2
+create view v1 as
+with cte1 as (select a from t1 where a > 1),
+cte2 as (select b from t2 where b < 6 group by b)
+select * from cte1, cte2 where cte1.a=cte2.b;
+explain update v1
+set v1.a=v1.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v1
+set v1.a=v1.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable cte1 and non mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+create view v2 as
+with cte1 as (select a from t1 where a > 1)
+select * from cte1, v1 where cte1.a=v1.b;
+explain update v2
+set v2.a=v2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v2;
+drop view v1;
+# update of mergeable view v2 specified as join of
+# mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+select * from
+v1, (select b from t2 where b < 6 group by b) dt1
+where v1.a=dt1.b;
+explain update v2
+set v2.a=v2.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+# update of mergeable view v2 specified as join of
+# mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+with cte1 as (select b from t2 where b < 7 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+explain update v2
+set v2.a=v2.a-1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v2
+set v2.a=v2.a-1;
+select * from t1;
+a
+3
+8
+7
+1
+4
+3
+4
+2
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v1;
+drop view v2;
+# update of mergeable view v3 specified as join of
+# mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+explain update v3
+set v3.a=v3.a+1;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update v3
+set v3.a=v3.a+1;
+select * from t1;
+a
+4
+8
+7
+1
+5
+4
+5
+3
+select * from t2;
+b
+5
+10
+3
+9
+2
+2
+4
+6
+1
+drop view v3;
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+set v1.a=v1.a-1, dt1.b=dt1.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10) dt1,
+cte1
+set v1.a=v1.a-1, dt1.b=dt1.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+(select * from t2 where t2.b < 5) dt1,
+v2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable v1,
+# mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, cte1.b=cte1.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, cte1.b=cte1.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with
+cte1 as (select * from t2 where t2.b < 10),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, cte1.b=cte1.b+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select * from t2 where t2.b < 5)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, cte1.b=cte1.b+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5);
+explain select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, v2.b=v2.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, v2.b=v2.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+4
+9
+2
+2
+5
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, v2.b=v2.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, v2.b=v2.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+6
+4
+9
+2
+2
+5
+5
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY t2 ALL NULL NULL NULL NULL 9 Using where; Using join buffer (flat, BNL join)
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (incremental, BNL join)
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+a b c d1
+2 2 2 2
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+2 2 2 2
+explain update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v2.b=v2.b+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 8 Using where
+1 SIMPLE t2 ALL NULL NULL NULL NULL 9 Using where
+1 SIMPLE t3 ALL NULL NULL NULL NULL 10 Using where
+update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v2.b=v2.b+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+6
+4
+9
+3
+3
+5
+5
+2
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop view v3;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# non-mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+(select * from t2 where t2.b < 10 group by b) dt1,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+where v1.a=dt1.b and dt1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+(select * from t2 where t2.b < 5 group by b) dt1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable v1,
+# non-mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+cte1,
+cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with
+cte1 as (select * from t2 where t2.b < 10 group by b),
+cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+cte1,
+cte2
+set v1.a=v1.a-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+# update of join of mergeable view v1,
+# non-mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+v1,
+cte1,
+v2
+where v1.a=cte1.b and cte1.b=v2.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+2 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+v1,
+cte1,
+v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+a
+3
+6
+7
+1
+4
+3
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+3 1
+2 3
+7 1
+1 1
+4 1
+5 2
+6 1
+# update of join of mergeable view v1,
+# non-mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5 group by b);
+explain select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+select a,b,c,d1 from
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+a b c d1
+3 3 3 2
+3 3 3 2
+4 4 4 2
+explain update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+v2,
+(select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+select * from t1;
+a
+4
+6
+7
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+7 1
+1 1
+2 1
+5 2
+6 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# non-mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10 group by b;
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+v1,
+v2,
+cte1
+where v1.a=v2.b and v2.b=cte1.c;
+a b c d1
+7 7 7 2
+6 6 6 2
+explain with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+v1,
+v2,
+cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+select * from t1;
+a
+4
+5
+6
+1
+5
+4
+5
+2
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+2 1
+1 1
+2 3
+9 1
+1 1
+2 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+# update of join of mergeable view v1,
+# non-mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5 group by b;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+explain with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived4> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where; Using join buffer (flat, BNL join)
+4 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+v1,
+v2,
+v3
+where v1.a=v2.b and v2.b=v3.c;
+a b c d1
+2 2 2 2
+1 1 1 2
+1 1 1 2
+2 2 2 2
+explain update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t1 ALL NULL NULL NULL NULL 8 Using where
+1 PRIMARY <derived3> ref key0 key0 5 test.t1.a 2
+1 PRIMARY t3 ALL NULL NULL NULL NULL 10 Using where
+3 DERIVED t2 ALL NULL NULL NULL NULL 9 Using where; Using temporary; Using filesort
+update
+v1,
+v2,
+v3
+set v1.a=v1.a+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+select * from t1;
+a
+4
+5
+6
+2
+5
+4
+5
+3
+select * from t2;
+b
+5
+7
+3
+9
+2
+2
+4
+6
+1
+select * from t3;
+c d
+4 2
+9 3
+4 1
+3 1
+2 3
+9 1
+3 1
+4 1
+5 2
+8 1
+drop view v1;
+drop view v2;
+drop view v3;
+drop table t1,t2,t3;
diff --git a/mysql-test/main/view_update_multi.test b/mysql-test/main/view_update_multi.test
new file mode 100644
index 0000000..c8c7403
--- /dev/null
+++ b/mysql-test/main/view_update_multi.test
@@ -0,0 +1,970 @@
+create table t1 (a int) engine=myisam;
+create table t2 (b int) engine=myisam;
+create table t3 (c int, d int) engine=myisam;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable view v1
+--echo # and mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+select * from v1, (select b from t2 where b < 6) as dt1
+where v1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, (select b from t2 where b < 6) as dt1
+set a=a+1, b=b+1 where v1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+with cte1 as (select * from t2 where b < 9)
+select * from v1, cte1 where v1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9)
+update v1, cte1
+set v1.a=v1.a-1, cte1.b=cte1.b-1 where v1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5;
+let $qs=
+select * from v1, v2 where v1.a=v2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, v2
+set v1.a=v1.a+2, v2.b=v2.b+2 where v1.a=v2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable view v1;
+--echo # and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+let $qs=
+select * from v1, v2 where v1.a=v2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as
+ select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6) as dt2
+ where dt1.a=dt2.b;
+let $qu=
+update v1
+set v1.a=v1.a+1, v1.b=v1.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and mergeable cte1
+create view v1 as
+ with cte1 as (select b from t2 where b < 9)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b;
+let $qu=
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable derived table dt1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+ select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable cte1 and mergeable cte2
+create view v1 as
+ with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6)
+ select * from cte1, cte2 where cte1.a=cte2.b;
+let $qu=
+update v1
+set v1.a=v1.a-1, v1.b=v1.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable cte1 and mergeable view v1
+create view v1 as select b from t2 where b < 5;
+create view v2 as
+ with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1, v2.b=v2.b+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v3 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+let $qu=
+update v3
+set v3.a=v3.a-1, v3.b=v3.b-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+select * from t1;
+select * from t2;
+--echo # multi-table update of mergeable view v1;
+--echo # and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+select * from v1, (select b from t2 where b < 6 group by b) as dt1
+where v1.a=dt1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, (select b from t2 where b < 6 group by b) as dt1
+set a=a+1 where v1.a=dt1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+let $qs=
+with cte1 as (select * from t2 where b < 9 group by b)
+select * from v1, cte1 where v1.a=cte1.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where b < 9 group by b)
+update v1, cte1
+set v1.a=v1.a-1 where v1.a=cte1.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # multi-table update of mergeable view v1
+--echo # and non-mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select * from t2 where b < 5 group by b;
+let $qs=
+select * from v1, v2 where v1.a=v2.b;
+eval explain $qs;
+eval $qs;
+let $qu=
+update v1, v2
+set v1.a=v1.a+2 where v1.a=v2.b;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+insert into t1 values (3), (8), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (10), (3), (9), (2), (2), (4), (6), (1);
+
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable derived table dt2
+create view v1 as
+ select * from
+ (select a from t1 where a > 1) as dt1,
+ (select b from t2 where b < 6 group by b) as dt2
+ where dt1.a=dt2.b;
+let $qu=
+update v1
+set v1.a=v1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable cte1
+create view v1 as
+ with cte1 as (select b from t2 where b < 9 group by b)
+ select * from
+ (select a from t1 where a > 1) as dt1, cte1
+ where dt1.a=cte1.b;
+let $qu=
+update v1
+set v1.a=v1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable derived table dt1 and non-mergeable view v1
+create view v1 as select b from t2 where b < 5 group by b;
+create view v2 as
+ select * from
+ (select a from t1 where a > 1) as dt1, v1
+ where dt1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable cte1 and non-mergeable derived table dt1
+create view v1 as
+ with cte1 as (select a from t1 where a > 1)
+ select * from
+ cte1, (select b from t2 where b < 7 group by b) dt1
+ where cte1.a=dt1.b;
+let $qu=
+update v1
+set v1.a=v1.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v1 specified as join of
+--echo # mergeable cte1 and non-mergeable cte2
+create view v1 as
+ with cte1 as (select a from t1 where a > 1),
+ cte2 as (select b from t2 where b < 6 group by b)
+ select * from cte1, cte2 where cte1.a=cte2.b;
+let $qu=
+update v1
+set v1.a=v1.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable cte1 and non mergeable view v1
+create view v1 as select b from t2 where b < 7 group by b;
+create view v2 as
+ with cte1 as (select a from t1 where a > 1)
+ select * from cte1, v1 where cte1.a=v1.b;
+let $qu=
+update v2
+set v2.a=v2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v2;
+drop view v1;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable view v1 and non-mergeable derived table dt1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+ select * from
+ v1, (select b from t2 where b < 6 group by b) dt1
+ where v1.a=dt1.b;
+let $qu=
+update v2
+set v2.a=v2.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+--echo # update of mergeable view v2 specified as join of
+--echo # mergeable view v1 and non-mergeable cte1
+create view v1 as select a from t1 where a > 1;
+create view v2 as
+ with cte1 as (select b from t2 where b < 7 group by b)
+ select * from v1, cte1 where v1.a=cte1.b;
+let $qu=
+update v2
+set v2.a=v2.a-1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v1;
+drop view v2;
+--echo # update of mergeable view v3 specified as join of
+--echo # mergeable view v1 and mergeable view v2
+create view v1 as select a from t1 where a > 1;
+create view v2 as select b from t2 where b < 5 group by b;
+create view v3 as select * from v1, v2 where v1.a=v2.b;
+let $qu=
+update v3
+set v3.a=v3.a+1;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+drop view v3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable view v1,
+--echo # mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ (select * from t2 where t2.b < 10) dt1,
+ cte1
+set v1.a=v1.a-1, dt1.b=dt1.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ v2
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5) dt1,
+ v2
+set v1.a=v1.a+1, dt1.b=dt1.b+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable v1,
+--echo # mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, cte1.b=cte1.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ cte1,
+ cte2
+set v1.a=v1.a-1, cte1.b=cte1.b-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ v2
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5)
+update
+ v1,
+ cte1,
+ v2
+set v1.a=v1.a+1, cte1.b=cte1.b+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable view v1,
+--echo # mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, v2.b=v2.b+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ cte1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ v2,
+ cte1
+set v1.a=v1.a-1, v2.b=v2.b-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ v3
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ v3
+set v1.a=v1.a+1, v2.b=v2.b+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+drop view v3;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable derived table dt1 and mergeable derived table dt2
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt2
+set v1.a=v1.a+1, dt2.c=dt2.c-dt2.d1
+where v1.a=dt1.b and dt1.b=dt2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable derived table dt1 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ (select * from t2 where t2.b < 10 group by b) dt1,
+ cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=dt1.b and dt1.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable derived table dt1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v2
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ (select * from t2 where t2.b < 5 group by b) dt1,
+ v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=dt1.b and dt1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable v1,
+--echo # non-mergeable cte1 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ v1,
+ cte1,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=cte1.b and cte1.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable cte1 and mergeable cte2
+create view v1 as select * from t1 where t1.a > 5;
+let $qs=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ cte2
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with
+ cte1 as (select * from t2 where t2.b < 10 group by b),
+ cte2 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ cte1,
+ cte2
+set v1.a=v1.a-1, cte2.c=cte2.c+cte2.d1
+where v1.a=cte1.b and cte1.b=cte2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable cte1 and mergeable view v2
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+select a,b,c,d1 from
+ v1,
+ cte1,
+ v2
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select * from t2 where t2.b < 5 group by b)
+update
+ v1,
+ cte1,
+ v2
+set v1.a=v1.a+1, v2.c=v2.c+v2.d1
+where v1.a=cte1.b and cte1.b=v2.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+
+delete from t1;
+delete from t2;
+delete from t3;
+insert into t1 values (3), (6), (7), (1), (4), (3), (5), (2);
+insert into t2 values (5), (7), (3), (9), (2), (2), (4), (6), (1);
+insert into t3 values
+(4,2), (9,3), (2,1), (3,1), (2,3), (7,1), (1,1), (4,1), (5,2), (6,1);
+select * from t1;
+select * from t2;
+select * from t3;
+
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable view v2 and mergeable derived table dt1
+create view v1 as select * from t1 where t1.a > 2;
+create view v2 as (select * from t2 where t2.b < 5 group by b);
+let $qs=
+select a,b,c,d1 from
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ (select c, d+1 as d1 from t3 where t3.d = 1) dt1
+set v1.a=v1.a+1, dt1.c=dt1.c-dt1.d1
+where v1.a=v2.b and v2.b=dt1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable view v2 and mergeable cte1
+create view v1 as select * from t1 where t1.a > 5;
+create view v2 as select * from t2 where t2.b < 10 group by b;
+let $qs=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ cte1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+with cte1 as (select c, d+1 as d1 from t3 where t3.d < 3)
+update
+ v1,
+ v2,
+ cte1
+set v1.a=v1.a-1, cte1.c=cte1.c+cte1.d1
+where v1.a=v2.b and v2.b=cte1.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+--echo # update of join of mergeable view v1,
+--echo # non-mergeable view v2 and mergeable view v3
+create view v1 as select * from t1 where t1.a < 3;
+create view v2 as select * from t2 where t2.b < 5 group by b;
+create view v3 as select c, d+1 as d1 from t3 where t3.d in (1,2);
+let $qs=
+with cte1 as (select * from t1 where t1.a < 3)
+select a,b,c,d1 from
+ v1,
+ v2,
+ v3
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qs;
+eval $qs;
+let $qu=
+update
+ v1,
+ v2,
+ v3
+set v1.a=v1.a+1, v3.c=v3.c+v3.d1
+where v1.a=v2.b and v2.b=v3.c;
+eval explain $qu;
+eval $qu;
+select * from t1;
+select * from t2;
+select * from t3;
+drop view v1;
+drop view v2;
+drop view v3;
+
+drop table t1,t2,t3;
diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc
index 23ff906..1cbf837 100644
--- a/sql/item_subselect.cc
+++ b/sql/item_subselect.cc
@@ -1782,7 +1782,6 @@ longlong Item_in_subselect::val_int()
As far as Item_in_subselect called only from Item_in_optimizer this
method should not be used
*/
- DBUG_ASSERT(0);
DBUG_ASSERT(fixed == 1);
if (forced_const)
return value;
diff --git a/sql/sp_head.cc b/sql/sp_head.cc
index 49c8229..f177966 100644
--- a/sql/sp_head.cc
+++ b/sql/sp_head.cc
@@ -3484,8 +3484,7 @@ sp_lex_keeper::reset_lex_and_exec_core(THD *thd, uint *nextp,
Json_writer_object trace_command(thd);
Json_writer_array trace_command_steps(thd, "steps");
if (open_tables)
- res= check_dependencies_in_with_clauses(m_lex->with_clauses_list) ||
- instr->exec_open_and_lock_tables(thd, m_lex->query_tables);
+ res= instr->exec_open_and_lock_tables(thd, m_lex->query_tables);
if (likely(!res))
{
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index 090ced5..71b3fa5 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -1183,7 +1183,7 @@ unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
{
TABLE_LIST *dup;
- table= table->find_table_for_update();
+ table= table->find_table_for_update(thd);
if (table->table &&
table->table->file->ha_table_flags() & HA_CAN_MULTISTEP_MERGE)
@@ -3568,7 +3568,18 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags,
if (tables->derived)
{
if (!tables->view)
+ {
+ if (!tables->is_derived())
+ tables->set_derived();
+ st_select_lex *sl= tables->derived->first_select();
+ if (sl->is_mergeable())
+ {
+ tables->merge_underlying_list= sl->table_list.first;
+ tables->propagate_properties_for_mergeable_derived();
+ tables->where= sl->where;
+ }
goto end;
+ }
/*
We restore view's name and database wiped out by derived tables
processing and fall back to standard open process in order to
@@ -3578,33 +3589,15 @@ open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags,
tables->db= tables->view_db;
tables->table_name= tables->view_name;
}
- else if (tables->select_lex)
+ else if (tables->select_lex)
{
- /*
- Check whether 'tables' refers to a table defined in a with clause.
- If so set the reference to the definition in tables->with.
- */
- if (!tables->with)
- tables->with= tables->select_lex->find_table_def_in_with_clauses(tables);
- /*
- If 'tables' is defined in a with clause set the pointer to the
- specification from its definition in tables->derived.
- */
if (tables->with)
{
- if (tables->is_recursive_with_table() &&
- !tables->is_with_table_recursive_reference())
- {
- tables->with->rec_outer_references++;
- With_element *with_elem= tables->with;
- while ((with_elem= with_elem->get_next_mutually_recursive()) !=
- tables->with)
- with_elem->rec_outer_references++;
- }
- if (tables->set_as_with_table(thd, tables->with))
+ if (!(tables->derived= tables->with->clone_parsed_spec(thd->lex, tables)))
DBUG_RETURN(1);
- else
- goto end;
+ tables->derived->first_select()->set_linkage(DERIVED_TABLE_TYPE);
+ tables->select_lex->add_statistics(tables->derived);
+ goto end;
}
}
@@ -5168,13 +5161,13 @@ bool open_and_lock_tables(THD *thd, const DDL_options_st &options,
/* Don't read statistics tables when opening internal tables */
if (!(flags & MYSQL_OPEN_IGNORE_LOGGING_FORMAT))
(void) read_statistics_for_tables_if_needed(thd, tables);
-
+
if (derived)
{
if (mysql_handle_derived(thd->lex, DT_INIT))
goto err;
if (thd->prepare_derived_at_open &&
- (mysql_handle_derived(thd->lex, DT_PREPARE)))
+ (mysql_handle_derived(thd->lex, DT_PREPARE)))
goto err;
}
@@ -6287,8 +6280,7 @@ find_field_in_tables(THD *thd, Item_ident *item,
when table_ref->field_translation != NULL.
*/
if (table_ref->table && !table_ref->view &&
- (!table_ref->is_merged_derived() ||
- (!table_ref->is_multitable() && table_ref->merged_for_insert)))
+ !table_ref->is_merged_derived())
{
found= find_field_in_table(thd, table_ref->table, name, length,
@@ -8017,7 +8009,7 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
(table->grant.privilege & SELECT_ACL)) ||
((!tables->is_non_derived() &&
(tables->grant.privilege & SELECT_ACL)))) &&
- !any_privileges)
+ !any_privileges)
{
field_iterator.set(tables);
if (check_grant_all_columns(thd, SELECT_ACL, &field_iterator))
@@ -8082,7 +8074,8 @@ insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
temporary table. Thus in this case we can be sure that 'item' is an
Item_field.
*/
- if (any_privileges && !tables->is_with_table() && !tables->is_derived())
+ if (any_privileges && !tables->is_with_table() && !tables->is_derived() &&
+ !thd->lex->can_use_merged())
{
DBUG_ASSERT((tables->field_translation == NULL && table) ||
tables->is_natural_join);
@@ -8304,7 +8297,7 @@ int setup_conds(THD *thd, TABLE_LIST *tables, List<TABLE_LIST> &leaves,
for (table= tables; table; table= table->next_local)
{
- if (select_lex == thd->lex->first_select_lex() &&
+ if (select_lex != thd->lex->first_select_lex() &&
select_lex->first_cond_optimization &&
table->merged_for_insert &&
table->prepare_where(thd, conds, FALSE))
diff --git a/sql/sql_class.h b/sql/sql_class.h
index ed4ebbb..ea0764f 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -4357,14 +4357,11 @@ class THD: public THD_count, /* this must be first */
to resolve all CTE names as we don't need this message to be thrown
for any CTE references.
*/
- if (!lex->with_clauses_list)
+ if (!lex->with_cte_resolution)
{
my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
return TRUE;
}
- /* This will allow to throw an error later for non-CTE references */
- to->str= NULL;
- to->length= 0;
return FALSE;
}
@@ -5384,7 +5381,7 @@ class select_result :public select_result_sink
virtual void update_used_tables() {}
/* this method is called just before the first row of the table can be read */
- virtual void prepare_to_read_rows() {}
+ virtual void prepare_to_read_rows(THD *thd) {}
void remove_offset_limit()
{
@@ -6595,7 +6592,7 @@ class multi_delete :public select_result_interceptor
bool send_eof();
inline ha_rows num_deleted() const { return deleted; }
virtual void abort_result_set();
- void prepare_to_read_rows();
+ void prepare_to_read_rows(THD *thd);
};
@@ -6603,6 +6600,7 @@ class multi_update :public select_result_interceptor
{
TABLE_LIST *all_tables; /* query/update command tables */
List<TABLE_LIST> *leaves; /* list of leves of join table tree */
+ List<TABLE_LIST> updated_leaves;
TABLE_LIST *update_tables;
TABLE **tmp_tables, *main_table, *table_to_update;
TMP_TABLE_PARAM *tmp_table_param;
@@ -6640,6 +6638,7 @@ class multi_update :public select_result_interceptor
List<Item> *fields, List<Item> *values,
enum_duplicates handle_duplicates, bool ignore);
~multi_update();
+ bool init(THD *thd);
int prepare(List<Item> &list, SELECT_LEX_UNIT *u);
int send_data(List<Item> &items);
bool initialize_tables (JOIN *join);
@@ -6650,7 +6649,7 @@ class multi_update :public select_result_interceptor
inline ha_rows num_updated() const { return updated; }
virtual void abort_result_set();
void update_used_tables();
- void prepare_to_read_rows();
+ void prepare_to_read_rows(THD *thd);
};
class my_var_sp;
diff --git a/sql/sql_cte.cc b/sql/sql_cte.cc
index 5bf9930..1eb4327 100644
--- a/sql/sql_cte.cc
+++ b/sql/sql_cte.cc
@@ -56,11 +56,43 @@ bool With_clause::add_with_element(With_element *elem)
}
-void st_select_lex_unit::set_with_clause(With_clause *with_cl)
+void With_clause::move_tables_to_end(LEX *lex)
+{
+ if (tables_start_pos == tables_end_pos ||
+ tables_end_pos == lex->query_tables_last)
+ return;
+ TABLE_LIST *first_tbl= *tables_start_pos;
+ DBUG_ASSERT (first_tbl);
+ TABLE_LIST *next_tbl= *tables_end_pos;
+ DBUG_ASSERT(next_tbl);
+ /* Exclude the tables of the with clause from global list */
+ *(next_tbl->prev_global= tables_start_pos)= next_tbl;
+ /* Attach the tables of the with_clause to the very end of global list */
+ *(first_tbl->prev_global= lex->query_tables_last)= first_tbl;
+ *(lex->query_tables_last= tables_end_pos)= 0;
+ for (With_element *with_elem= with_list.first;
+ with_elem; with_elem= with_elem->next)
+ {
+ if (with_elem->head->tables_pos.start_pos != tables_start_pos)
+ break;
+ with_elem->head->tables_pos.set_start_pos(first_tbl->prev_global);
+ if (with_elem->head->tables_pos.end_pos != tables_start_pos)
+ break;
+ with_elem->head->tables_pos.set_end_pos(first_tbl->prev_global);
+ }
+ tables_start_pos= first_tbl->prev_global;
+ tables_end_pos= lex->query_tables_last;
+}
+
+
+void st_select_lex_unit::set_with_clause(LEX *lex, With_clause *with_cl)
{
with_clause= with_cl;
if (with_clause)
+ {
with_clause->set_owner(this);
+ with_clause->move_tables_to_end(lex);
+ }
}
@@ -84,7 +116,8 @@ void st_select_lex_unit::set_with_clause(With_clause *with_cl)
true on failure
*/
-bool check_dependencies_in_with_clauses(With_clause *with_clauses_list)
+
+bool LEX::check_dependencies_in_with_clauses()
{
for (With_clause *with_clause= with_clauses_list;
with_clause;
@@ -100,6 +133,216 @@ bool check_dependencies_in_with_clauses(With_clause *with_clauses_list)
}
+bool
+LEX::resolve_references_to_cte_in_hanging_cte(TABLE_LIST **start_ptr)
+{
+ for (With_clause *with_clause= with_clauses_list;
+ with_clause; with_clause= with_clause->next_with_clause)
+ {
+ for (With_element *with_elem= with_clause->with_list.first;
+ with_elem; with_elem= with_elem->next)
+ {
+ if (!with_elem->is_referenced())
+ {
+ TABLE_LIST *cte_last_ordered= 0;
+ TABLE_LIST *first_tbl=
+ with_elem->spec->first_select()->table_list.first;
+ TABLE_LIST **with_elem_end_pos= with_elem->head->tables_pos.end_pos;
+ if (resolve_and_order_references_to_cte(first_tbl,
+ with_elem_end_pos,
+ &cte_last_ordered))
+ return true;
+ reorder_table_list(first_tbl, start_ptr);
+ start_ptr= &cte_last_ordered->next_global;
+ }
+ }
+ }
+ return false;
+}
+
+
+bool LEX::resolve_and_order_references_to_cte(TABLE_LIST *tables,
+ TABLE_LIST **tables_last,
+ TABLE_LIST **last_ordered)
+{
+ TABLE_LIST *with_elem_first_tbl= 0;
+ TABLE_LIST **with_elem_end_pos= 0;
+ With_element *with_elem= 0;
+ bool rc= false;
+ Query_arena backup;
+ Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup);
+
+ for (TABLE_LIST *tbl= tables;
+ tbl != *tables_last;
+ tbl= tbl->next_global)
+ {
+ if (tbl->next_ordered || tbl == *last_ordered)
+ continue;
+ if (!*last_ordered)
+ *last_ordered= tbl;
+ else
+ *last_ordered= (*last_ordered)->next_ordered= tbl;
+ if (!tbl->db.str && !tbl->with)
+ tbl->with= tbl->select_lex->find_table_def_in_with_clauses(tbl);
+ if (!tbl->with)
+ {
+ if (!tbl->db.str)
+ {
+ if (!thd->db.str)
+ {
+ my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
+ rc= true;
+ goto err;
+ }
+ if (copy_db_to(&tbl->db))
+ {
+ rc= true;
+ goto err;
+ }
+ if (!(tbl->table_options & TL_OPTION_ALIAS))
+ MDL_REQUEST_INIT(&tbl->mdl_request, MDL_key::TABLE, tbl->db.str,
+ tbl->table_name.str, tbl->mdl_type, MDL_TRANSACTION);
+ if (tbl->is_mdl_request_type_to_be_set)
+ {
+ tbl->mdl_request.set_type((tbl->lock_type >= TL_WRITE_ALLOW_WRITE) ?
+ MDL_SHARED_WRITE : MDL_SHARED_READ);
+ tbl->is_mdl_request_type_to_be_set= false;
+ }
+ }
+ continue;
+ }
+ with_elem= tbl->with;
+ if (tbl->is_recursive_with_table() &&
+ !tbl->is_with_table_recursive_reference())
+ {
+ tbl->with->rec_outer_references++;
+ while ((with_elem= with_elem->get_next_mutually_recursive()) !=
+ tbl->with)
+ with_elem->rec_outer_references++;
+ }
+ if (!with_elem->is_used_in_query || with_elem->is_recursive)
+ {
+ tbl->derived= with_elem->spec;
+ if (tbl->derived != tbl->select_lex->master_unit() &&
+ !with_elem->is_recursive &&
+ !tbl->is_with_table_recursive_reference())
+ {
+ tbl->derived->move_as_slave(tbl->select_lex);
+ }
+ with_elem->is_used_in_query= true;
+ }
+ else
+ tbl->derived= 0;
+ tbl->db.str= empty_c_string;
+ tbl->db.length= 0;
+ tbl->schema_table= 0;
+ MDL_REQUEST_INIT(&tbl->mdl_request, MDL_key::TABLE, tbl->db.str,
+ tbl->table_name.str, tbl->mdl_type, MDL_TRANSACTION);
+ if (tbl->is_mdl_request_type_to_be_set)
+ {
+ tbl->mdl_request.set_type((tbl->lock_type >= TL_WRITE_ALLOW_WRITE) ?
+ MDL_SHARED_WRITE : MDL_SHARED_READ);
+ tbl->is_mdl_request_type_to_be_set= false;
+ }
+ if (tbl->derived)
+ {
+ tbl->derived->first_select()->set_linkage(DERIVED_TABLE_TYPE);
+ tbl->select_lex->add_statistics(tbl->derived);
+ }
+ if (tbl->with->is_recursive && tbl->is_with_table_recursive_reference())
+ continue;
+ with_elem->inc_references();
+ if (tbl->derived)
+ {
+ with_elem_first_tbl= tbl->derived->first_select()->table_list.first;
+ with_elem_end_pos= with_elem->head->tables_pos.end_pos;
+ if (with_elem_first_tbl &&
+ resolve_and_order_references_to_cte(with_elem_first_tbl,
+ with_elem_end_pos,
+ last_ordered))
+ {
+ rc= true;
+ goto err;
+ }
+ }
+ }
+err:
+ if (arena)
+ thd->restore_active_arena(arena, &backup);
+ return rc;
+}
+
+
+void LEX::reorder_table_list(TABLE_LIST *tables, TABLE_LIST **start_ptr)
+{
+ TABLE_LIST *next_ordered= 0;
+ TABLE_LIST **last_chunk_next_ptr= start_ptr;
+ bool is_first_chunk= true;
+ for (TABLE_LIST *tbl= tables; tbl; tbl= next_ordered)
+ {
+ TABLE_LIST *chunk_first= tbl;
+ for (next_ordered= tbl->next_ordered;
+ next_ordered && next_ordered == tbl->next_global;
+ next_ordered= tbl->next_ordered)
+ {
+ tbl= next_ordered;
+ }
+ TABLE_LIST *chunk_last= tbl;
+ if (is_first_chunk && *last_chunk_next_ptr == chunk_first)
+ {
+ /* The first chunk is in place */
+ }
+ else
+ {
+ TABLE_LIST *next_tbl= tbl->next_global;
+
+ /* Exclude chunk_first..chunk_last from global list */
+ if (next_tbl)
+ *(next_tbl->prev_global= chunk_first->prev_global)= next_tbl;
+ else
+ *(query_tables_last= chunk_first->prev_global)= 0;
+
+ next_tbl= *last_chunk_next_ptr;
+ /*
+ Include chunk_first..chunk_last before next_tbl if it's not 0 and
+ at the end of the list otherwise
+ */
+ if (next_tbl)
+ *(next_tbl->prev_global= &chunk_last->next_global)= next_tbl;
+ else
+ *(query_tables_last= &chunk_last->next_global)= 0;
+ *(chunk_first->prev_global= last_chunk_next_ptr)= chunk_first;
+ }
+ last_chunk_next_ptr= &chunk_last->next_global;
+ is_first_chunk= false;
+ }
+}
+
+
+bool
+LEX::check_cte_dependencies_and_resolve_references()
+{
+ if (check_dependencies_in_with_clauses())
+ return true;
+ if (!with_cte_resolution)
+ return false;
+ TABLE_LIST *last_ordered= 0;
+ TABLE_LIST *first_tbl= query_tables;
+ if (resolve_and_order_references_to_cte(first_tbl,
+ query_tables_last,
+ &last_ordered))
+ return true;
+ reorder_table_list(first_tbl, &query_tables);
+ TABLE_LIST **start_ptr= last_ordered ? &last_ordered->next_global :
+ &query_tables;
+ if (*start_ptr &&
+ resolve_references_to_cte_in_hanging_cte(start_ptr))
+ return true;
+ all_cte_resolved= true;
+ return false;
+}
+
+
/**
@brief
Check dependencies between tables defined in this with clause
@@ -138,10 +381,11 @@ bool With_clause::check_dependencies()
elem != with_elem;
elem= elem->next)
{
- if (lex_string_cmp(system_charset_info, with_elem->query_name,
- elem->query_name) == 0)
+ if (lex_string_cmp(system_charset_info, with_elem->get_name(),
+ elem->get_name()) == 0)
{
- my_error(ER_DUP_QUERY_NAME, MYF(0), with_elem->query_name->str);
+ my_error(ER_DUP_QUERY_NAME, MYF(0),
+ with_elem->get_name_str());
return true;
}
}
@@ -248,11 +492,12 @@ With_element *With_clause::find_table_def(TABLE_LIST *table,
with_elem != barrier;
with_elem= with_elem->next)
{
- if (my_strcasecmp(system_charset_info, with_elem->query_name->str,
+ if (my_strcasecmp(system_charset_info, with_elem->get_name_str(),
table->table_name.str) == 0 &&
!table->is_fqtn)
{
table->set_derived();
+ with_elem->referenced= true;
return with_elem;
}
}
@@ -609,7 +854,7 @@ bool With_clause::check_anchors()
if (elem == with_elem)
{
my_error(ER_RECURSIVE_WITHOUT_ANCHORS, MYF(0),
- with_elem->query_name->str);
+ with_elem->get_name_str());
return true;
}
}
@@ -642,7 +887,7 @@ bool With_clause::check_anchors()
if (elem->work_dep_map & elem->get_elem_map())
{
my_error(ER_UNACCEPTABLE_MUTUAL_RECURSION, MYF(0),
- with_elem->query_name->str);
+ with_elem->get_name_str());
return true;
}
}
@@ -827,14 +1072,17 @@ bool With_element::set_unparsed_spec(THD *thd,
NULL - otherwise
*/
-st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
+st_select_lex_unit *With_element::clone_parsed_spec(LEX *old_lex,
TABLE_LIST *with_table)
{
+ THD *thd= old_lex->thd;
LEX *lex;
st_select_lex_unit *res= NULL;
Query_arena backup;
Query_arena *arena= thd->activate_stmt_arena_if_needed(&backup);
bool has_tmp_tables;
+ uint i= 0;
+
if (!(lex= (LEX*) new(thd->mem_root) st_lex_local))
{
@@ -842,14 +1090,12 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
thd->restore_active_arena(arena, &backup);
return res;
}
- LEX *old_lex= thd->lex;
thd->lex= lex;
bool parse_status= false;
Parser_state parser_state;
- TABLE_LIST *spec_tables;
- TABLE_LIST *spec_tables_tail;
st_select_lex *with_select;
+ st_select_lex *end_sl;
char save_end= unparsed_spec.str[unparsed_spec.length];
((char*) &unparsed_spec.str[unparsed_spec.length])[0]= '\0';
@@ -866,6 +1112,7 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
lex->spname= old_lex->spname;
lex->spcont= old_lex->spcont;
lex->sp_chistics= old_lex->sp_chistics;
+ lex->with_cte_resolution= true;
lex->stmt_lex= old_lex;
parse_status= parse_sql(thd, &parser_state, 0);
@@ -875,48 +1122,63 @@ st_select_lex_unit *With_element::clone_parsed_spec(THD *thd,
if (parse_status)
goto err;
- if (check_dependencies_in_with_clauses(lex->with_clauses_list))
- goto err;
-
- spec_tables= lex->query_tables;
- spec_tables_tail= 0;
- has_tmp_tables= thd->has_temporary_tables();
- for (TABLE_LIST *tbl= spec_tables;
- tbl;
- tbl= tbl->next_global)
+ if (lex->query_tables)
{
- if (has_tmp_tables && !tbl->derived && !tbl->schema_table &&
- thd->open_temporary_table(tbl))
- goto err;
- spec_tables_tail= tbl;
- }
- if (check_table_access(thd, SELECT_ACL, spec_tables, FALSE, UINT_MAX, FALSE))
- goto err;
- if (spec_tables)
- {
- if (with_table->next_global)
+ head->tables_pos.set_start_pos(&with_table->next_global);
+ head->tables_pos.set_end_pos(lex->query_tables_last);
+ TABLE_LIST *next_tbl= with_table->next_global;
+ if (next_tbl)
{
- spec_tables_tail->next_global= with_table->next_global;
- with_table->next_global->prev_global= &spec_tables_tail->next_global;
+ *(lex->query_tables->prev_global= next_tbl->prev_global)=
+ lex->query_tables;
+ *(next_tbl->prev_global= lex->query_tables_last)= next_tbl;
}
else
{
- old_lex->query_tables_last= &spec_tables_tail->next_global;
+ *(lex->query_tables->prev_global= old_lex->query_tables_last)=
+ lex->query_tables;
+ old_lex->query_tables_last= lex->query_tables_last;
}
- spec_tables->prev_global= &with_table->next_global;
- with_table->next_global= spec_tables;
}
+
res= &lex->unit;
lex->unit.include_down(with_table->select_lex);
- lex->unit.set_slave(with_select);
+ lex->unit.set_slave(with_select);
+ end_sl= lex->all_selects_list;
+ while (end_sl->next_select_in_list())
+ end_sl= end_sl->next_select_in_list();
old_lex->all_selects_list=
(st_select_lex*) (lex->all_selects_list->
insert_chain_before(
(st_select_lex_node **) &(old_lex->all_selects_list),
- with_select));
- if (check_dependencies_in_with_clauses(lex->with_clauses_list))
+ end_sl));
+ if (lex->check_cte_dependencies_and_resolve_references())
+ {
res= NULL;
+ goto err;
+ }
+
+ has_tmp_tables= thd->has_temporary_tables();
+ for (TABLE_LIST *tbl= *head->tables_pos.start_pos;
+ tbl != *head->tables_pos.end_pos;
+ tbl= tbl->next_global, i++)
+ {
+ if (has_tmp_tables && !tbl->derived && !tbl->schema_table &&
+ thd->open_temporary_table(tbl))
+ {
+ res= NULL;
+ goto err;
+ }
+ }
+
+ if (i && check_table_access(thd, SELECT_ACL, *head->tables_pos.start_pos,
+ FALSE, i, FALSE))
+ {
+ res= NULL;
+ goto err;
+ }
+
lex->sphead= NULL; // in order not to delete lex->sphead
lex_end(lex);
err:
@@ -1128,57 +1390,6 @@ With_element *st_select_lex::find_table_def_in_with_clauses(TABLE_LIST *table)
}
-/**
- @brief
- Set the specifying unit in this reference to a with table
-
- @details
- The method assumes that the given element with_elem defines the table T
- this table reference refers to.
- If this is the first reference to T the method just sets its specification
- in the field 'derived' as the unit that yields T. Otherwise the method
- first creates a clone specification and sets rather this clone in this field.
-
- @retval
- false on success
- true on failure
-*/
-
-bool TABLE_LIST::set_as_with_table(THD *thd, With_element *with_elem)
-{
- if (table)
- {
- /*
- This table was prematurely identified as a temporary table.
- We correct it here, but it's not a nice solution in the case
- when the temporary table with this name is not used anywhere
- else in the query.
- */
- thd->mark_tmp_table_as_free_for_reuse(table);
- table= 0;
- }
- with= with_elem;
- schema_table= NULL;
- if (!with_elem->is_referenced() || with_elem->is_recursive)
- {
- derived= with_elem->spec;
- if (derived != select_lex->master_unit() &&
- !with_elem->is_recursive &&
- !is_with_table_recursive_reference())
- {
- derived->move_as_slave(select_lex);
- }
- }
- else
- {
- if(!(derived= with_elem->clone_parsed_spec(thd, this)))
- return true;
- }
- derived->first_select()->set_linkage(DERIVED_TABLE_TYPE);
- select_lex->add_statistics(derived);
- with_elem->inc_references();
- return false;
-}
bool TABLE_LIST::is_recursive_with_table()
@@ -1280,7 +1491,7 @@ bool st_select_lex::check_unrestricted_recursive(bool only_standard_compliant)
if (only_standard_compliant && with_elem->is_unrestricted())
{
my_error(ER_NOT_STANDARD_COMPLIANT_RECURSIVE,
- MYF(0), with_elem->query_name->str);
+ MYF(0), with_elem->get_name_str());
return true;
}
@@ -1497,7 +1708,8 @@ static void list_strlex_print(THD *thd, String *str, List<Lex_ident_sys> *list)
void With_element::print(THD *thd, String *str, enum_query_type query_type)
{
- str->append(query_name);
+ str->append(get_name());
+
if (column_list.elements)
{
List_iterator_fast<Lex_ident_sys> li(column_list);
diff --git a/sql/sql_cte.h b/sql/sql_cte.h
index 4c42dd2..0c5ac72 100644
--- a/sql/sql_cte.h
+++ b/sql/sql_cte.h
@@ -23,6 +23,20 @@
class select_unit;
struct st_unit_ctxt_elem;
+class With_element_head : public Sql_alloc
+{
+ LEX_CSTRING *query_name;
+public:
+ TABLE_CHAIN tables_pos;
+ With_element_head(LEX_CSTRING *name)
+ : query_name(name)
+ {
+ tables_pos.set_start_pos(0);
+ tables_pos.set_end_pos(0);
+ }
+ friend class With_element;
+};
+
/**
@class With_element
@@ -85,6 +99,10 @@ class With_element : public Sql_alloc
subqueries and specifications of other with elements).
*/
uint references;
+ bool referenced;
+
+ bool is_used_in_query;
+
/*
Unparsed specification of the query that specifies this element.
It used to build clones of the specification if they are needed.
@@ -98,13 +116,10 @@ class With_element : public Sql_alloc
/* Return the map where 1 is set only in the position for this element */
table_map get_elem_map() { return (table_map) 1 << number; }
-
+
public:
- /*
- The name of the table introduced by this with elememt. The name
- can be used in FROM lists of the queries in the scope of the element.
- */
- LEX_CSTRING *query_name;
+ With_element_head *head;
+
/*
Optional list of column names to name the columns of the table introduced
by this with element. It is used in the case when the names are not
@@ -163,18 +178,27 @@ class With_element : public Sql_alloc
/* List of derived tables containing recursive references to this CTE */
SQL_I_List<TABLE_LIST> derived_with_rec_ref;
- With_element(LEX_CSTRING *name,
+ With_element(With_element_head *h,
List <Lex_ident_sys> list,
st_select_lex_unit *unit)
: next(NULL), base_dep_map(0), derived_dep_map(0),
sq_dep_map(0), work_dep_map(0), mutually_recursive(0),
top_level_dep_map(0), sq_rec_ref(NULL),
next_mutually_recursive(NULL), references(0),
- query_name(name), column_list(list), cycle_list(0), spec(unit),
+ referenced(false), is_used_in_query(false),
+ head(h), column_list(list), cycle_list(0), spec(unit),
is_recursive(false), rec_outer_references(0), with_anchor(false),
level(0), rec_result(NULL)
{ unit->with_element= this; }
+ LEX_CSTRING *get_name() { return head->query_name; }
+ const char *get_name_str() { return get_name()->str; }
+
+ void set_tables_start_pos(TABLE_LIST **pos)
+ { head->tables_pos.set_start_pos(pos); }
+ void set_tables_end_pos(TABLE_LIST **pos)
+ { head->tables_pos.set_end_pos(pos); }
+
bool check_dependencies_in_spec();
void check_dependencies_in_select(st_select_lex *sl, st_unit_ctxt_elem *ctxt,
@@ -196,14 +220,16 @@ class With_element : public Sql_alloc
bool check_dependency_on(With_element *with_elem)
{ return base_dep_map & with_elem->get_elem_map(); }
+
+
TABLE_LIST *find_first_sq_rec_ref_in_select(st_select_lex *sel);
bool set_unparsed_spec(THD *thd, const char *spec_start, const char *spec_end,
my_ptrdiff_t spec_offset);
- st_select_lex_unit *clone_parsed_spec(THD *thd, TABLE_LIST *with_table);
+ st_select_lex_unit *clone_parsed_spec(LEX *old_lex, TABLE_LIST *with_table);
- bool is_referenced() { return references != 0; }
+ bool is_referenced() { return referenced; }
void inc_references() { references++; }
@@ -263,6 +289,13 @@ class With_element : public Sql_alloc
void set_cycle_list(List<Lex_ident_sys> *cycle_list_arg);
friend class With_clause;
+
+ friend
+ bool LEX::resolve_and_order_references_to_cte(TABLE_LIST *tables,
+ TABLE_LIST **tables_last,
+ TABLE_LIST **last_ordered);
+ friend
+ bool LEX::resolve_references_to_cte_in_hanging_cte(TABLE_LIST **start_ptr);
};
const uint max_number_of_elements_in_with_clause= sizeof(table_map)*8;
@@ -294,6 +327,10 @@ class With_clause : public Sql_alloc
in the current statement
*/
With_clause *next_with_clause;
+
+ TABLE_LIST **tables_start_pos;
+ TABLE_LIST **tables_end_pos;
+
/* Set to true if dependencies between with elements have been checked */
bool dependencies_are_checked;
/*
@@ -341,6 +378,13 @@ class With_clause : public Sql_alloc
void attach_to(st_select_lex *select_lex);
With_clause *pop() { return embedding_with_clause; }
+
+ void set_tables_start_pos(TABLE_LIST **table_pos)
+ { tables_start_pos= table_pos; }
+ void set_tables_end_pos(TABLE_LIST **table_pos)
+ { tables_end_pos= table_pos; }
+
+ void move_tables_to_end(LEX *lex);
bool check_dependencies();
@@ -360,9 +404,8 @@ class With_clause : public Sql_alloc
friend class With_element;
- friend
- bool
- check_dependencies_in_with_clauses(With_clause *with_clauses_list);
+ friend bool LEX::check_dependencies_in_with_clauses();
+ friend bool LEX::resolve_references_to_cte_in_hanging_cte(TABLE_LIST**);
};
inline
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 5942efc..359ef46 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -339,11 +339,12 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
if (thd->lex->handle_list_of_derived(table_list, DT_PREPARE))
DBUG_RETURN(TRUE);
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE");
DBUG_RETURN(TRUE);
}
+
if (!(table= table_list->table) || !table->is_created())
{
my_error(ER_VIEW_DELETE_MERGE_VIEW, MYF(0),
@@ -1022,7 +1023,7 @@ int mysql_prepare_delete(THD *thd, TABLE_LIST *table_list, Item **conds,
setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
setup_ftfuncs(select_lex))
DBUG_RETURN(TRUE);
- if (!table_list->single_table_updatable() ||
+ if (!table_list->single_table_updatable(thd) ||
check_key_in_view(thd, table_list))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE");
@@ -1121,7 +1122,7 @@ int mysql_multi_delete_prepare(THD *thd)
DBUG_RETURN(TRUE);
}
- if (!target_tbl->correspondent_table->single_table_updatable() ||
+ if (!target_tbl->correspondent_table->single_table_updatable(thd) ||
check_key_in_view(thd, target_tbl->correspondent_table))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
@@ -1181,12 +1182,12 @@ multi_delete::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
DBUG_RETURN(0);
}
-void multi_delete::prepare_to_read_rows()
+void multi_delete::prepare_to_read_rows(THD *thd)
{
/* see multi_update::prepare_to_read_rows() */
for (TABLE_LIST *walk= delete_tables; walk; walk= walk->next_local)
{
- TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update();
+ TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update(thd);
tbl->table->mark_columns_needed_for_delete();
}
}
@@ -1206,7 +1207,7 @@ multi_delete::initialize_tables(JOIN *join)
delete_while_scanning= true;
for (walk= delete_tables; walk; walk= walk->next_local)
{
- TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update();
+ TABLE_LIST *tbl= walk->correspondent_table->find_table_for_update(thd);
tables_to_delete_from|= tbl->table->map;
if (delete_while_scanning &&
unique_table(thd, tbl, join->tables_list, 0))
diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc
index 428e7b1..596902e 100644
--- a/sql/sql_derived.cc
+++ b/sql/sql_derived.cc
@@ -382,10 +382,6 @@ bool mysql_derived_merge(THD *thd, LEX *lex, TABLE_LIST *derived)
DBUG_RETURN(FALSE);
}
- if (thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
- thd->lex->sql_command == SQLCOM_DELETE_MULTI)
- thd->save_prep_leaf_list= TRUE;
-
arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test
if (!derived->merged_for_insert ||
@@ -531,7 +527,7 @@ bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived)
derived->merged_for_insert,
derived->is_materialized_derived(),
derived->is_multitable(),
- derived->single_table_updatable(),
+ derived->single_table_updatable(thd),
derived->merge_underlying_list != 0));
if (derived->merged_for_insert)
DBUG_RETURN(FALSE);
@@ -544,20 +540,35 @@ bool mysql_derived_merge_for_insert(THD *thd, LEX *lex, TABLE_LIST *derived)
DBUG_RETURN(FALSE);
if (!derived->is_multitable())
{
- if (!derived->single_table_updatable())
+ if (!derived->is_view() ||
+ !derived->single_table_updatable(thd) ||
+ derived->merge_underlying_list->is_derived())
DBUG_RETURN(derived->create_field_translation(thd));
if (derived->merge_underlying_list)
{
derived->table= derived->merge_underlying_list->table;
derived->schema_table= derived->merge_underlying_list->schema_table;
derived->merged_for_insert= TRUE;
- DBUG_ASSERT(derived->table);
}
}
DBUG_RETURN(FALSE);
}
+void TABLE_LIST::propagate_properties_for_mergeable_derived()
+{
+ for (TABLE_LIST *tbl= derived->first_select()->table_list.first;
+ tbl;
+ tbl= tbl->next_local)
+ {
+ tbl->lock_type= lock_type;
+ if (!tbl->mdl_request.ticket)
+ tbl->mdl_request.set_type(mdl_request.type);
+ tbl->updating= updating;
+ }
+}
+
+
/*
Initialize a derived table/view
@@ -590,8 +601,6 @@ bool mysql_derived_init(THD *thd, LEX *lex, TABLE_LIST *derived)
bool res= derived->init_derived(thd, TRUE);
- derived->updatable= derived->updatable && derived->is_view();
-
DBUG_RETURN(res);
}
@@ -754,6 +763,12 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
break;
}
}
+ if (derived->is_recursive_with_table() &&
+ derived->is_with_table_recursive_reference())
+ {
+ derived->table->grant.privilege= SELECT_ACL;
+ derived->grant.privilege= SELECT_ACL;
+ }
DBUG_RETURN(FALSE);
}
@@ -827,8 +842,14 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
Depending on the result field translation will or will not
be created.
*/
- if (derived->init_derived(thd, FALSE))
- goto exit;
+ if (derived->is_merged_derived())
+ {
+ if ((derived->is_view() ||
+ (derived->get_unit()->prepared &&
+ !(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW))) &&
+ derived->create_field_translation(thd))
+ goto exit;
+ }
/*
Temp table is created so that it hounours if UNION without ALL is to be
@@ -920,8 +941,17 @@ bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *derived)
{
DBUG_ASSERT(derived->is_derived());
DBUG_ASSERT(derived->is_anonymous_derived_table());
- table->grant.privilege= SELECT_ACL;
- derived->grant.privilege= SELECT_ACL;
+ if (derived->is_materialized_derived())
+ {
+ table->grant.privilege= SELECT_ACL;
+ derived->grant.privilege= SELECT_ACL;
+ }
+ else
+ {
+ if (table->grant.privilege == NO_ACL)
+ table->grant.privilege= VIEW_ANY_ACL;
+ table->grant= derived->grant;
+ }
}
#endif
/* Add new temporary table to list of open derived tables */
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index a934427..228d5e9 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -121,7 +121,8 @@ static int binlog_show_create_table(THD *thd, TABLE *table,
@returns false if success.
*/
-static bool check_view_single_update(List<Item> &fields, List<Item> *values,
+static bool check_view_single_update(THD *thd,
+ List<Item> &fields, List<Item> *values,
TABLE_LIST *view, table_map *map,
bool insert)
{
@@ -171,7 +172,7 @@ static bool check_view_single_update(List<Item> &fields, List<Item> *values,
*/
tbl->table->insert_values= view->table->insert_values;
view->table= tbl->table;
- if (!tbl->single_table_updatable())
+ if (!tbl->single_table_updatable(thd))
{
if (insert)
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), view->alias.str, "INSERT");
@@ -179,6 +180,10 @@ static bool check_view_single_update(List<Item> &fields, List<Item> *values,
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), view->alias.str, "UPDATE");
return TRUE;
}
+ else if (tbl->is_derived() && insert)
+ {
+ my_error(ER_NON_INSERTABLE_TABLE, MYF(0), view->alias.str, "INSERT");
+ }
*map= tables;
return FALSE;
@@ -214,7 +219,7 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
TABLE *table= table_list->table;
DBUG_ENTER("check_insert_fields");
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT");
DBUG_RETURN(-1);
@@ -286,7 +291,7 @@ static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
if (table_list->is_view() && table_list->is_merged_derived())
{
- if (check_view_single_update(fields,
+ if (check_view_single_update(thd, fields,
fields_and_values_from_different_maps ?
(List<Item>*) 0 : &values,
table_list, map, true))
@@ -392,7 +397,7 @@ static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
if (insert_table_list->is_view() &&
insert_table_list->is_merged_derived() &&
- check_view_single_update(update_fields,
+ check_view_single_update(thd, update_fields,
fields_and_values_from_different_maps ?
(List<Item>*) 0 : &update_values,
insert_table_list, map, false))
@@ -1462,7 +1467,7 @@ static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list,
bool insert_into_view= (table_list->view != 0);
DBUG_ENTER("mysql_prepare_insert_check_table");
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd) || table_list->is_derived())
{
my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias.str, "INSERT");
DBUG_RETURN(TRUE);
@@ -1576,11 +1581,11 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
DBUG_ASSERT (!select_insert || !values);
if (mysql_handle_derived(thd->lex, DT_INIT))
- DBUG_RETURN(TRUE);
+ DBUG_RETURN(TRUE);
if (table_list->handle_derived(thd->lex, DT_MERGE_FOR_INSERT))
- DBUG_RETURN(TRUE);
+ DBUG_RETURN(TRUE);
if (thd->lex->handle_list_of_derived(table_list, DT_PREPARE))
- DBUG_RETURN(TRUE);
+ DBUG_RETURN(TRUE);
if (duplic == DUP_UPDATE)
{
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index 671948f..3d98a17 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -1227,6 +1227,7 @@ void LEX::start(THD *thd_arg)
curr_with_clause= 0;
with_clauses_list= 0;
with_clauses_list_last_next= &with_clauses_list;
+ all_cte_resolved= 0;
clone_spec_offset= 0;
create_view= NULL;
field_list.empty();
@@ -1249,6 +1250,7 @@ void LEX::start(THD *thd_arg)
explain_json= false;
context_analysis_only= 0;
derived_tables= 0;
+ with_cte_resolution= false;
safe_to_cache_query= 1;
parsing_options.reset();
empty_field_list_on_rset= 0;
@@ -1267,6 +1269,7 @@ void LEX::start(THD *thd_arg)
selects_allow_into= FALSE;
selects_allow_procedure= FALSE;
use_only_table_context= FALSE;
+ skip_access_check= false;
parse_vcol_expr= FALSE;
check_exists= FALSE;
create_info.lex_start();
@@ -8859,6 +8862,8 @@ bool LEX::check_main_unit_semantics()
if (unit.set_nest_level(0) ||
unit.check_parameters(first_select_lex()))
return TRUE;
+ if (check_cte_dependencies_and_resolve_references())
+ return TRUE;
return FALSE;
}
@@ -9551,8 +9556,13 @@ void st_select_lex::add_statistics(SELECT_LEX_UNIT *unit)
bool LEX::main_select_push()
{
DBUG_ENTER("LEX::main_select_push");
+#if 0
current_select_number= 1;
builtin_select.select_number= 1;
+#else
+ current_select_number= ++thd->lex->stmt_lex->current_select_number;
+ builtin_select.select_number= current_select_number;
+#endif
if (push_select(&builtin_select))
DBUG_RETURN(TRUE);
DBUG_RETURN(FALSE);
@@ -9648,7 +9658,7 @@ bool LEX::insert_select_hack(SELECT_LEX *sel)
builtin_select.link_prev= NULL; // indicator of removal
}
- if (set_main_unit(sel->master_unit()))
+ if (set_main_unit(this, sel->master_unit()))
return true;
DBUG_ASSERT(builtin_select.table_list.elements == 1);
@@ -10046,7 +10056,7 @@ TABLE_LIST *LEX::parsed_derived_table(SELECT_LEX_UNIT *unit,
bool LEX::parsed_create_view(SELECT_LEX_UNIT *unit, int check)
{
SQL_I_List<TABLE_LIST> *save= &first_select_lex()->table_list;
- if (set_main_unit(unit))
+ if (set_main_unit(this, unit))
return true;
if (check_main_unit_semantics())
return true;
@@ -10070,7 +10080,7 @@ bool LEX::select_finalize(st_select_lex_unit *expr)
sql_command= SQLCOM_SELECT;
selects_allow_into= TRUE;
selects_allow_procedure= TRUE;
- if (set_main_unit(expr))
+ if (set_main_unit(this, expr))
return true;
return check_main_unit_semantics();
}
@@ -10377,7 +10387,7 @@ void st_select_lex::pushdown_cond_into_where_clause(THD *thd, Item *cond,
above are marked with NO_EXTRACTION_FL.
@note
- This method is called for pushdown into materialized
+ This mesthod is called for pushdown into materialized
derived tables/views/IN subqueries optimization.
*/
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index adfa0c0..81621c9 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -969,7 +969,7 @@ class st_select_lex_unit: public st_select_lex_node {
{
return reinterpret_cast<st_select_lex*>(slave);
}
- void set_with_clause(With_clause *with_cl);
+ void set_with_clause(LEX *lex, With_clause *with_cl);
st_select_lex_unit* next_unit()
{
return reinterpret_cast<st_select_lex_unit*>(next);
@@ -1762,6 +1762,16 @@ class Query_tables_list
*/
void add_to_query_tables(TABLE_LIST *table)
{
+ if (table->derived)
+ {
+ TABLE_LIST *first_local= table->derived->first_select()->table_list.first;
+ if (first_local)
+ {
+ *(table->prev_global= first_local->prev_global)= table;
+ *(first_local->prev_global= &table->next_global)= first_local;
+ return;
+ }
+ }
*(table->prev_global= query_tables_last)= table;
query_tables_last= &table->next_global;
}
@@ -3132,6 +3142,9 @@ class Lex_grant_privilege: public Grant_privilege, public Sql_alloc
};
+class Query_name;
+
+
struct LEX: public Query_tables_list
{
SELECT_LEX_UNIT unit; /* most upper unit */
@@ -3154,6 +3167,7 @@ struct LEX: public Query_tables_list
with clause in the current statement
*/
With_clause **with_clauses_list_last_next;
+ bool all_cte_resolved;
/*
When a copy of a with element is parsed this is set to the offset of
the with element in the input string, otherwise it's set to 0
@@ -3372,6 +3386,7 @@ struct LEX: public Query_tables_list
*/
uint8 derived_tables;
uint8 context_analysis_only;
+ bool with_cte_resolution;
bool local_file;
bool check_exists;
bool autocommit;
@@ -3471,6 +3486,8 @@ struct LEX: public Query_tables_list
*/
bool use_only_table_context;
+ bool skip_access_check;
+
/*
Reference to a struct that contains information in various commands
to add/create/drop/change table spaces.
@@ -4524,7 +4541,7 @@ struct LEX: public Query_tables_list
bool insert_select_hack(SELECT_LEX *sel);
SELECT_LEX *create_priority_nest(SELECT_LEX *first_in_nest);
- bool set_main_unit(st_select_lex_unit *u)
+ bool set_main_unit(LEX *lex, st_select_lex_unit *u)
{
unit.options= u->options;
unit.uncacheable= u->uncacheable;
@@ -4532,7 +4549,7 @@ struct LEX: public Query_tables_list
unit.first_select()->options|= builtin_select.options;
unit.fake_select_lex= u->fake_select_lex;
unit.union_distinct= u->union_distinct;
- unit.set_with_clause(u->with_clause);
+ unit.set_with_clause(lex, u->with_clause);
builtin_select.exclude_from_global();
return false;
}
@@ -4675,6 +4692,15 @@ struct LEX: public Query_tables_list
const LEX_CSTRING *constraint_name,
Table_ident *ref_table_name,
DDL_options ddl_options);
+
+ bool check_dependencies_in_with_clauses();
+ bool resolve_references_to_cte_in_hanging_cte(TABLE_LIST **start_ptr);
+ bool check_cte_dependencies_and_resolve_references();
+ bool resolve_and_order_references_to_cte(TABLE_LIST *tables,
+ TABLE_LIST **tables_last,
+ TABLE_LIST **last_ordered);
+ void reorder_table_list(TABLE_LIST *tables, TABLE_LIST **start_ptr);
+
};
diff --git a/sql/sql_load.cc b/sql/sql_load.cc
index c3e301c..0637a68 100644
--- a/sql/sql_load.cc
+++ b/sql/sql_load.cc
@@ -403,7 +403,7 @@ int mysql_load(THD *thd, const sql_exchange *ex, TABLE_LIST *table_list,
INSERT_ACL | UPDATE_ACL, FALSE))
DBUG_RETURN(-1);
if (!table_list->table || // do not suport join view
- !table_list->single_table_updatable() || // and derived tables
+ !table_list->single_table_updatable(thd) || // and derived tables
check_key_in_view(thd, table_list))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "LOAD");
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 86f81dd..bf362da 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -3366,6 +3366,7 @@ mysql_execute_command(THD *thd)
already.
*/
DBUG_ASSERT(! thd->transaction_rollback_request || thd->in_sub_stmt);
+
/*
In many cases first table of main SELECT_LEX have special meaning =>
check that it is first table in global list and relink it first in
@@ -3411,9 +3412,6 @@ mysql_execute_command(THD *thd)
thd->get_stmt_da()->opt_clear_warning_info(thd->query_id);
}
- if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list))
- DBUG_RETURN(1);
-
#ifdef HAVE_REPLICATION
if (unlikely(thd->slave_thread))
{
@@ -6903,7 +6901,7 @@ bool check_one_table_access(THD *thd, privilege_t privilege,
UINT_MAX, FALSE)))
return 1;
}
- return 0;
+ return 0;
}
@@ -8183,7 +8181,7 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
ptr->is_fqtn= TRUE;
ptr->db= table->db;
}
- else if (lex->copy_db_to(&ptr->db))
+ else if (!lex->with_cte_resolution && lex->copy_db_to(&ptr->db))
DBUG_RETURN(0);
else
ptr->is_fqtn= FALSE;
@@ -8200,7 +8198,9 @@ TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
}
ptr->table_name= table->table;
- ptr->lock_type= lock_type;
+ ptr->lock_type= lock_type;
+ ptr->mdl_type= mdl_type;
+ ptr->table_options= table_options;
ptr->updating= MY_TEST(table_options & TL_OPTION_UPDATING);
/* TODO: remove TL_OPTION_FORCE_INDEX as it looks like it's not used */
ptr->force_index= MY_TEST(table_options & TL_OPTION_FORCE_INDEX);
@@ -8871,8 +8871,11 @@ void st_select_lex::set_lock_for_tables(thr_lock_type lock_type, bool for_update
{
tables->lock_type= lock_type;
tables->updating= for_update;
- tables->mdl_request.set_type((lock_type >= TL_WRITE_ALLOW_WRITE) ?
- MDL_SHARED_WRITE : MDL_SHARED_READ);
+ if (!tables->db.str)
+ tables->is_mdl_request_type_to_be_set= true;
+ else
+ tables->mdl_request.set_type((lock_type >= TL_WRITE_ALLOW_WRITE) ?
+ MDL_SHARED_WRITE : MDL_SHARED_READ);
}
DBUG_VOID_RETURN;
}
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 6dbdfe7..54288f2 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -1392,7 +1392,7 @@ static int mysql_test_update(Prepared_statement *stmt,
if (table_list->handle_derived(thd->lex, DT_PREPARE))
goto error;
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE");
goto error;
@@ -1470,7 +1470,7 @@ static bool mysql_test_delete(Prepared_statement *stmt,
if (mysql_handle_derived(thd->lex, DT_PREPARE))
goto error;
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "DELETE");
goto error;
@@ -2303,9 +2303,6 @@ static bool check_prepared_statement(Prepared_statement *stmt)
if (tables)
thd->get_stmt_da()->opt_clear_warning_info(thd->query_id);
- if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list))
- goto error;
-
if (sql_command_flags[sql_command] & CF_HA_CLOSE)
mysql_ha_rm_tables(thd, tables);
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 76cb96b..24f534d 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -1154,11 +1154,22 @@ JOIN::prepare(TABLE_LIST *tables_init, COND *conds_init, uint og_num,
Affects only materialized derived tables.
*/
/* Check that all tables, fields, conds and order are ok */
- if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
- setup_tables_and_check_access(thd, &select_lex->context, join_list,
- tables_list, select_lex->leaf_tables,
- FALSE, SELECT_ACL, SELECT_ACL, FALSE))
- DBUG_RETURN(-1);
+ if (!(select_options & OPTION_SETUP_TABLES_DONE))
+ {
+ if (thd->lex->skip_access_check)
+ {
+ if (setup_tables(thd, &select_lex->context, join_list, tables_list,
+ select_lex->leaf_tables, FALSE, FALSE))
+ DBUG_RETURN(-1);
+ }
+ else
+ {
+ if (setup_tables_and_check_access(thd, &select_lex->context, join_list,
+ tables_list, select_lex->leaf_tables,
+ FALSE, SELECT_ACL, SELECT_ACL, FALSE))
+ DBUG_RETURN(-1);
+ }
+ }
/*
Permanently remove redundant parts from the query if
@@ -2232,7 +2243,7 @@ JOIN::optimize_inner()
/* Calculate how to do the join */
THD_STAGE_INFO(thd, stage_statistics);
- result->prepare_to_read_rows();
+ result->prepare_to_read_rows(thd);
if (unlikely(make_join_statistics(this, select_lex->leaf_tables,
&keyuse)) ||
unlikely(thd->is_fatal_error))
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index 62c93f6..e72ed05 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -2581,6 +2581,8 @@ static int show_create_view(THD *thd, TABLE_LIST *table, String *buff)
tbl;
tbl= tbl->next_global)
{
+ if (!tbl->db.str || !tbl->db.str[0])
+ continue;
if (cmp(&table->view_db, tbl->view ? &tbl->view_db : &tbl->db))
{
table->compact_view_format= FALSE;
diff --git a/sql/sql_tvc.cc b/sql/sql_tvc.cc
index 37f0f80..06d74a2 100644
--- a/sql/sql_tvc.cc
+++ b/sql/sql_tvc.cc
@@ -730,7 +730,7 @@ st_select_lex *wrap_tvc(THD *thd, st_select_lex *tvc_sl,
wrapper_sl->where= 0;
wrapper_sl->set_braces(false);
- derived_unit->set_with_clause(0);
+ derived_unit->set_with_clause(lex, 0);
if (arena)
thd->restore_active_arena(arena, &backup);
@@ -997,7 +997,7 @@ Item *Item_func_in::in_predicate_to_in_subs_transformer(THD *thd,
sq_select->where= 0;
sq_select->set_braces(false);
- derived_unit->set_with_clause(0);
+ derived_unit->set_with_clause(thd->lex, 0);
/* Create IN subquery predicate */
sq_select->parsing_place= parent_select->parsing_place;
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index 8cd3b9f..dc2f8f4 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -401,9 +401,17 @@ int mysql_update(THD *thd,
if (open_tables(thd, &table_list, &table_count, 0))
DBUG_RETURN(1);
- /* Prepare views so they are handled correctly */
- if (mysql_handle_derived(thd->lex, DT_INIT))
+ for (TABLE_LIST *tbl= thd->lex->query_tables; tbl; tbl= tbl->next_global)
+ {
+ if (tbl->handle_derived(thd->lex, DT_INIT))
+ DBUG_RETURN(1);
+ }
+
+ if (!table_list->is_multitable() && !table_list->single_table_updatable(thd))
+ {
+ my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE");
DBUG_RETURN(1);
+ }
if (table_list->has_period() && table_list->is_view_or_derived())
{
@@ -411,9 +419,13 @@ int mysql_update(THD *thd,
DBUG_RETURN(TRUE);
}
- if (((update_source_table=unique_table(thd, table_list,
- table_list->next_global, 0)) ||
- table_list->is_multitable()))
+ table_list->single_table_update= table_list->single_table_updatable(thd);
+
+ if ((update_source_table= unique_table(thd, table_list,
+ table_list->next_global, 0)) ||
+ table_list->is_multitable() ||
+ (table_list->single_table_update &&
+ (update_source_table= table_list->find_table_for_update(thd))))
{
DBUG_ASSERT(update_source_table || table_list->view != 0);
DBUG_PRINT("info", ("Switch to multi-update"));
@@ -435,7 +447,7 @@ int mysql_update(THD *thd,
table= table_list->table;
- if (!table_list->single_table_updatable())
+ if (!table_list->single_table_updatable(thd))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias.str, "UPDATE");
DBUG_RETURN(1);
@@ -1402,7 +1414,7 @@ bool mysql_prepare_update(THD *thd, TABLE_LIST *table_list,
We do not call DT_MERGE_FOR_INSERT because it has no sense for simple
(not multi-) update
*/
- if (mysql_handle_derived(thd->lex, DT_PREPARE))
+ if (table_list->handle_derived(thd->lex, DT_PREPARE))
DBUG_RETURN(TRUE);
if (setup_tables_and_check_access(thd, &select_lex->context,
@@ -1680,10 +1692,16 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
done= true;
- if (mysql_handle_derived(lex, DT_INIT) ||
- mysql_handle_derived(lex, DT_MERGE_FOR_INSERT) ||
- mysql_handle_derived(lex, DT_PREPARE))
+ if (!table_list->single_table_update &&
+ select_lex->handle_derived(lex, DT_INIT))
DBUG_RETURN(1);
+ lex->skip_access_check= true;
+ if (select_lex->handle_derived(lex, DT_PREPARE))
+ {
+ lex->skip_access_check= false;
+ DBUG_RETURN(1);
+ }
+ lex->skip_access_check= false;
/*
setup_tables() need for VIEWs. JOIN::prepare() will call setup_tables()
@@ -1691,12 +1709,8 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
call in setup_tables()).
*/
- if (setup_tables_and_check_access(thd, &select_lex->context,
- &select_lex->top_join_list, table_list, select_lex->leaf_tables,
- FALSE, UPDATE_ACL, SELECT_ACL, FALSE))
- DBUG_RETURN(1);
-
- if (select_lex->handle_derived(thd->lex, DT_MERGE))
+ if (setup_tables(thd, &select_lex->context, &select_lex->top_join_list,
+ table_list, select_lex->leaf_tables, FALSE, TRUE))
DBUG_RETURN(1);
List<Item> *fields= &lex->first_select_lex()->item_list;
@@ -1706,7 +1720,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
// Check if we have a view in the list ...
for (tl= table_list; tl ; tl= tl->next_local)
- if (tl->view)
+ if (tl->view || tl->is_merged_derived())
break;
// ... and pass this knowlage in check_fields call
if (check_fields(thd, table_list, *fields, tl != NULL ))
@@ -1732,7 +1746,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
/* if table will be updated then check that it is unique */
if (table->map & tables_for_update)
{
- if (!tl->single_table_updatable() || check_key_in_view(thd, tl))
+ if (!tl->single_table_updatable(thd) || check_key_in_view(thd, tl))
{
my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
tl->top_table()->alias.str, "UPDATE");
@@ -1790,6 +1804,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
DBUG_RETURN(TRUE);
}
+#if 0
/* check single table update for view compound from several tables */
for (tl= table_list; tl; tl= tl->next_local)
{
@@ -1803,6 +1818,7 @@ bool Multiupdate_prelocking_strategy::handle_end(THD *thd)
DBUG_RETURN(1);
}
}
+#endif
DBUG_RETURN(0);
}
@@ -1887,9 +1903,6 @@ int mysql_multi_update_prepare(THD *thd)
*/
lex->first_select_lex()->exclude_from_table_unique_test= FALSE;
- if (lex->save_prep_leaf_tables())
- DBUG_RETURN(TRUE);
-
DBUG_RETURN (FALSE);
}
@@ -1914,12 +1927,20 @@ bool mysql_multi_update(THD *thd, TABLE_LIST *table_list, List<Item> *fields,
DBUG_RETURN(TRUE);
}
+ if ((*result)->init(thd))
+ DBUG_RETURN(1);
+
thd->abort_on_warning= !ignore && thd->is_strict_mode();
List<Item> total_list;
+ if (setup_tables(thd, &select_lex->context, &select_lex->top_join_list,
+ table_list, select_lex->leaf_tables, FALSE, FALSE))
+ DBUG_RETURN(1);
+
if (select_lex->vers_setup_conds(thd, table_list))
DBUG_RETURN(1);
+
res= mysql_select(thd,
table_list, total_list, conds,
select_lex->order_list.elements,
@@ -1957,6 +1978,20 @@ multi_update::multi_update(THD *thd_arg, TABLE_LIST *table_list,
{
}
+bool multi_update::init(THD *thd)
+{
+ table_map tables_to_update= get_table_map(fields);
+ List_iterator_fast<TABLE_LIST> li(*leaves);
+ TABLE_LIST *tbl;
+ while ((tbl =li++))
+ {
+ if (!(tbl->table->map & tables_to_update))
+ continue;
+ if (updated_leaves.push_back(tbl, thd->mem_root))
+ return true;
+ }
+ return false;
+}
/*
Connect fields with tables and create list of tables that are updated
@@ -1974,7 +2009,7 @@ int multi_update::prepare(List<Item> ¬_used_values,
List_iterator_fast<Item> value_it(*values);
uint i, max_fields;
uint leaf_table_count= 0;
- List_iterator<TABLE_LIST> ti(*leaves);
+ List_iterator<TABLE_LIST> ti(updated_leaves);
DBUG_ENTER("multi_update::prepare");
if (prepared)
@@ -2000,16 +2035,10 @@ int multi_update::prepare(List<Item> ¬_used_values,
*/
while ((table_ref= ti++))
{
- if (table_ref->is_jtbm())
- continue;
-
TABLE *table= table_ref->table;
- if (tables_to_update & table->map)
- {
- DBUG_ASSERT(table->read_set == &table->def_read_set);
- table->read_set= &table->tmp_set;
- bitmap_clear_all(table->read_set);
- }
+ DBUG_ASSERT(table->read_set == &table->def_read_set);
+ table->read_set= &table->value_set;
+ bitmap_clear_all(table->read_set);
}
/*
@@ -2023,16 +2052,10 @@ int multi_update::prepare(List<Item> ¬_used_values,
ti.rewind();
while ((table_ref= ti++))
{
- if (table_ref->is_jtbm())
- continue;
-
TABLE *table= table_ref->table;
- if (tables_to_update & table->map)
- {
- table->read_set= &table->def_read_set;
- bitmap_union(table->read_set, &table->tmp_set);
- table->file->prepare_for_insert(1);
- }
+ table->read_set= &table->def_read_set;
+ bitmap_union(table->read_set, &table->value_set);
+ table->file->prepare_for_insert(1);
}
if (unlikely(error))
DBUG_RETURN(1);
@@ -2047,25 +2070,26 @@ int multi_update::prepare(List<Item> ¬_used_values,
ti.rewind();
while ((table_ref= ti++))
{
- /* TODO: add support of view of join support */
+ TABLE *table=table_ref->table;
+ TABLE_LIST *tl= (TABLE_LIST*) thd->memdup(table_ref,
+ sizeof(*tl));
+ if (!tl)
+ DBUG_RETURN(1);
+ update.link_in_list(tl, &tl->next_local);
+ tl->shared= table_count++;
+ table->no_keyread=1;
+ table->covering_keys.clear_all();
+ table->pos_in_table_list= tl;
+ table->prepare_triggers_for_update_stmt_or_event();
+ table->reset_default_fields();
+ }
+
+ List_iterator_fast<TABLE_LIST> li(*leaves);
+ while ((table_ref= li++))
+ {
if (table_ref->is_jtbm())
continue;
- TABLE *table=table_ref->table;
leaf_table_count++;
- if (tables_to_update & table->map)
- {
- TABLE_LIST *tl= (TABLE_LIST*) thd->memdup(table_ref,
- sizeof(*tl));
- if (!tl)
- DBUG_RETURN(1);
- update.link_in_list(tl, &tl->next_local);
- tl->shared= table_count++;
- table->no_keyread=1;
- table->covering_keys.clear_all();
- table->pos_in_table_list= tl;
- table->prepare_triggers_for_update_stmt_or_event();
- table->reset_default_fields();
- }
}
table_count= update.elements;
@@ -2126,7 +2150,7 @@ void multi_update::update_used_tables()
}
}
-void multi_update::prepare_to_read_rows()
+void multi_update::prepare_to_read_rows(THD *thd)
{
/*
update column maps now. it cannot be done in ::prepare() before the
@@ -2196,7 +2220,7 @@ static bool safe_update_on_fly(THD *thd, JOIN_TAB *join_tab,
case JT_REF_OR_NULL:
return !is_key_used(table, join_tab->ref.key, table->write_set);
case JT_ALL:
- if (bitmap_is_overlapping(&table->tmp_set, table->write_set))
+ if (bitmap_is_overlapping(&table->value_set, table->write_set))
return FALSE;
/* If range search on index */
if (join_tab->quick)
@@ -2227,6 +2251,7 @@ bool
multi_update::initialize_tables(JOIN *join)
{
TABLE_LIST *table_ref;
+ table_map all_tables_to_update= get_table_map(fields);
DBUG_ENTER("initialize_tables");
if (unlikely((thd->variables.option_bits & OPTION_SAFE_UPDATES) &&
@@ -2249,6 +2274,8 @@ multi_update::initialize_tables(JOIN *join)
for (table_ref= update_tables; table_ref; table_ref= table_ref->next_local)
{
TABLE *table=table_ref->table;
+ if (!(table->map & all_tables_to_update))
+ continue;
uint cnt= table_ref->shared;
List<Item> temp_fields;
ORDER group;
@@ -2459,11 +2486,14 @@ multi_update::~multi_update()
int multi_update::send_data(List<Item> ¬_used_values)
{
TABLE_LIST *cur_table;
+ table_map all_tables_to_update= get_table_map(fields);
DBUG_ENTER("multi_update::send_data");
for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
{
TABLE *table= cur_table->table;
+ if (!(table->map & all_tables_to_update))
+ continue;
uint offset= cur_table->shared;
/*
Check if we are using outer join and we didn't find the row
@@ -2682,6 +2712,7 @@ void multi_update::abort_result_set()
int multi_update::do_updates()
{
TABLE_LIST *cur_table;
+ table_map all_tables_to_update= get_table_map(fields);
int local_error= 0;
ha_rows org_updated;
TABLE *table, *tmp_table, *err_table;
@@ -2710,8 +2741,12 @@ int multi_update::do_updates()
uint offset= cur_table->shared;
table = cur_table->table;
+ if (!(table->map & all_tables_to_update))
+ continue;
if (table == table_to_update)
+ {
continue; // Already updated
+ }
org_updated= updated;
tmp_table= tmp_tables[cur_table->shared];
tmp_table->file->extra(HA_EXTRA_CACHE); // Change to read cache
diff --git a/sql/sql_view.cc b/sql/sql_view.cc
index 6666f2e..0863beb 100644
--- a/sql/sql_view.cc
+++ b/sql/sql_view.cc
@@ -34,7 +34,7 @@
#include "sp_cache.h"
#include "datadict.h" // dd_frm_is_view()
#include "sql_derived.h"
-#include "sql_cte.h" // check_dependencies_in_with_clauses()
+#include "sql_cte.h" // check_cte_dependencies_and_resolve_references()
#include "opt_trace.h"
#include "wsrep_mysqld.h"
@@ -429,12 +429,6 @@ bool mysql_create_view(THD *thd, TABLE_LIST *views,
lex->link_first_table_back(view, link_to_local);
view->open_type= OT_BASE_ONLY;
- if (check_dependencies_in_with_clauses(lex->with_clauses_list))
- {
- res= TRUE;
- goto err_no_relink;
- }
-
WSREP_TO_ISOLATION_BEGIN(WSREP_MYSQL_DB, NULL, NULL);
/*
@@ -1104,6 +1098,7 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
goto err;
}
+#if 0
/*
Check that table of main select do not used in subqueries.
@@ -1123,6 +1118,7 @@ static int mysql_register_view(THD *thd, TABLE_LIST *view,
{
view->updatable_view= 0;
}
+#endif
if (view->with_check != VIEW_CHECK_NONE &&
!view->updatable_view)
@@ -1417,9 +1413,6 @@ bool mysql_make_view(THD *thd, TABLE_SHARE *share, TABLE_LIST *table,
TABLE_LIST *tbl;
Security_context *security_ctx= 0;
- if (check_dependencies_in_with_clauses(thd->lex->with_clauses_list))
- goto err;
-
/*
Check rights to run commands which show underlying tables.
In the optimizer trace we would not like to show trace for
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index d8ad3ae..bcc68c4 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -268,6 +268,8 @@ void _CONCAT_UNDERSCORED(turn_parser_debug_on,yyparse)()
class sp_head *sphead;
class sp_name *spname;
class sp_variable *spvar;
+/* class Query_name *query_name; */
+ class With_element_head *with_element_head;
class With_clause *with_clause;
class Virtual_column_info *virtual_column;
@@ -1747,9 +1749,11 @@ End SQL_MODE_ORACLE_SPECIFIC */
'-' '+' '*' '/' '%' '(' ')'
',' '!' '{' '}' '&' '|'
-%type <with_clause> with_clause
+%type <with_clause> with_clause opt_with_clause
-%type <lex_str_ptr> query_name
+/* %type <query_name> query_name*/
+/* %type <lex_str_ptr> query_name */
+%type <with_element_head> with_element_head
%type <ident_sys_list>
comma_separated_ident_list
@@ -3741,6 +3745,8 @@ expr_lex:
$$= $<expr_lex>1;
$$->sp_lex_in_use= true;
$$->set_item($2);
+ if ($$->check_cte_dependencies_and_resolve_references())
+ MYSQL_YYABORT;
if ($$->sphead->restore_lex(thd))
MYSQL_YYABORT;
}
@@ -4628,7 +4634,7 @@ create_select_query_expression:
| LEFT_PAREN_WITH with_clause query_expression_no_with_clause ')'
{
SELECT_LEX *first_select= $3->first_select();
- $3->set_with_clause($2);
+ $3->set_with_clause(Lex, $2);
$2->attach_to(first_select);
if (Lex->parsed_insert_select(first_select))
MYSQL_YYABORT;
@@ -8524,7 +8530,7 @@ select:
opt_procedure_or_into
{
Lex->pop_select();
- $1->set_with_clause(NULL);
+ $1->set_with_clause(Lex, NULL);
if (Lex->select_finalize($1, $3))
MYSQL_YYABORT;
}
@@ -8538,7 +8544,7 @@ select:
opt_procedure_or_into
{
Lex->pop_select();
- $2->set_with_clause($1);
+ $2->set_with_clause(Lex, $1);
$1->attach_to($2->first_select());
if (Lex->select_finalize($2, $4))
MYSQL_YYABORT;
@@ -8574,7 +8580,7 @@ select_into:
MYSQL_YYABORT;
if ($4)
unit= Lex->add_tail_to_query_expression_body(unit, $4);
- unit->set_with_clause($1);
+ unit->set_with_clause(Lex, $1);
$1->attach_to($2);
if (Lex->select_finalize(unit))
MYSQL_YYABORT;
@@ -8698,13 +8704,13 @@ select_into_query_specification:
query_expression:
query_expression_no_with_clause
{
- $1->set_with_clause(NULL);
+ $1->set_with_clause(Lex, NULL);
$$= $1;
}
| with_clause
query_expression_no_with_clause
{
- $2->set_with_clause($1);
+ $2->set_with_clause(Lex, $1);
$1->attach_to($2->first_select());
$$= $2;
}
@@ -8883,7 +8889,7 @@ subquery:
}
| '(' with_clause query_expression_no_with_clause ')'
{
- $3->set_with_clause($2);
+ $3->set_with_clause(Lex, $2);
$2->attach_to($3->first_select());
if (!($$= Lex->parsed_subselect($3)))
YYABORT;
@@ -13094,7 +13100,7 @@ update_table_list:
/* Update rows in a table */
update:
- UPDATE_SYM
+ opt_with_clause UPDATE_SYM
{
LEX *lex= Lex;
if (Lex->main_select_push())
@@ -13109,24 +13115,23 @@ update:
SELECT_LEX *slex= Lex->first_select_lex();
if (slex->table_list.elements > 1)
Lex->sql_command= SQLCOM_UPDATE_MULTI;
- else if (slex->get_table_list()->derived)
- {
- /* it is single table update and it is update of derived table */
- my_error(ER_NON_UPDATABLE_TABLE, MYF(0),
- slex->get_table_list()->alias.str, "UPDATE");
- MYSQL_YYABORT;
- }
/*
In case of multi-update setting write lock for all tables may
be too pessimistic. We will decrease lock level if possible in
mysql_multi_update().
*/
- slex->set_lock_for_tables($3, slex->table_list.elements == 1);
+ slex->set_lock_for_tables($4, slex->table_list.elements == 1);
+ if ($1)
+ {
+ st_select_lex_unit *unit= Lex->current_select->master_unit();
+ unit->set_with_clause(Lex, $1);
+ $1->attach_to(unit->first_select());
+ }
}
opt_where_clause opt_order_clause delete_limit_clause
{
- if ($10)
- Select->order_list= *($10);
+ if ($11)
+ Select->order_list= *($11);
} stmt_end {}
;
@@ -13167,7 +13172,7 @@ opt_low_priority:
/* Delete rows from a table */
delete:
- DELETE_SYM
+ opt_with_clause DELETE_SYM
{
LEX *lex= Lex;
lex->sql_command= SQLCOM_DELETE;
@@ -13178,6 +13183,12 @@ delete:
mysql_init_select(lex);
lex->ignore= 0;
lex->first_select_lex()->order_list.empty();
+ if ($1)
+ {
+ st_select_lex_unit *unit= Lex->current_select->master_unit();
+ unit->set_with_clause(Lex, $1);
+ $1->attach_to(unit->first_select());
+ }
}
delete_part2
{ }
@@ -13224,6 +13235,9 @@ delete_single_table_for_period:
if ($2)
Lex->last_table()->period_conditions= Lex->period_conditions;
}
+/*
+ | FROM table_primary_derived
+*/
;
single_multi:
@@ -14780,15 +14794,22 @@ temporal_literal:
}
;
+opt_with_clause:
+ /* empty */ { $$= 0; }
+ | with_clause { $$= $1; }
+ ;
+
with_clause:
WITH opt_recursive
{
LEX *lex= Lex;
With_clause *with_clause=
new With_clause($2, Lex->curr_with_clause);
+ with_clause->set_tables_start_pos(lex->query_tables_last);
if (unlikely(with_clause == NULL))
MYSQL_YYABORT;
lex->derived_tables|= DERIVED_WITH;
+ lex->with_cte_resolution= true;
lex->curr_with_clause= with_clause;
with_clause->add_to_list(Lex->with_clauses_list_last_next);
if (lex->current_select &&
@@ -14798,6 +14819,7 @@ with_clause:
with_list
{
$$= Lex->curr_with_clause;
+ Lex->curr_with_clause->set_tables_end_pos(Lex->query_tables_last);
Lex->curr_with_clause= Lex->curr_with_clause->pop();
}
;
@@ -14816,7 +14838,7 @@ with_list:
with_list_element:
- query_name
+ with_element_head
opt_with_column_list
AS '(' query_expression ')' opt_cycle
{
@@ -14830,6 +14852,7 @@ with_list_element:
if (elem->set_unparsed_spec(thd, spec_start, $6.pos(),
spec_start - query_start))
MYSQL_YYABORT;
+ elem->set_tables_end_pos(lex->query_tables_last);
if ($7)
{
elem->set_cycle_list($7);
@@ -14894,12 +14917,15 @@ comma_separated_ident_list:
;
-query_name:
+with_element_head:
ident
{
- $$= (LEX_CSTRING *) thd->memdup(&$1, sizeof(LEX_CSTRING));
- if (unlikely($$ == NULL))
+ LEX_CSTRING *name=
+ (LEX_CSTRING *) thd->memdup(&$1, sizeof(LEX_CSTRING));
+ $$= new (thd->mem_root) With_element_head(name);
+ if (unlikely(name == NULL || $$ == NULL))
MYSQL_YYABORT;
+ $$->tables_pos.set_start_pos(Lex->query_tables_last);
}
;
diff --git a/sql/table.cc b/sql/table.cc
index bec89db..8bc5fe7 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -4139,7 +4139,7 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
/* Allocate bitmaps */
bitmap_size= share->column_bitmap_size;
- bitmap_count= 7;
+ bitmap_count= 8;
if (share->virtual_fields)
bitmap_count++;
@@ -4166,6 +4166,9 @@ enum open_frm_error open_table_from_share(THD *thd, TABLE_SHARE *share,
my_bitmap_init(&outparam->cond_set,
(my_bitmap_map*) bitmaps, share->fields, FALSE);
bitmaps+= bitmap_size;
+ my_bitmap_init(&outparam->value_set,
+ (my_bitmap_map*) bitmaps, share->fields, FALSE);
+ bitmaps+= bitmap_size;
my_bitmap_init(&outparam->def_rpl_write_set,
(my_bitmap_map*) bitmaps, share->fields, FALSE);
outparam->default_column_bitmaps();
@@ -5609,7 +5612,7 @@ bool TABLE_LIST::setup_underlying(THD *thd)
{
DBUG_ENTER("TABLE_LIST::setup_underlying");
- if (!view || (!field_translation && merge_underlying_list))
+ if (!field_translation && merge_underlying_list)
{
SELECT_LEX *select= get_single_select();
@@ -5722,141 +5725,163 @@ bool TABLE_LIST::prep_where(THD *thd, Item **conds,
@return Result of the check.
*/
-bool TABLE_LIST::single_table_updatable()
+bool TABLE_LIST::single_table_updatable(THD *thd)
{
- if (!updatable)
+ if (!updatable ||
+ (is_derived() && !optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE)))
return false;
- if (view && view->first_select_lex()->table_list.elements == 1)
+ if (is_view_or_derived() &&
+ get_unit()->first_select()->table_list.elements == 1)
{
/*
We need to check deeply only single table views. Multi-table views
will be turned to multi-table updates and then checked by leaf tables
*/
- return (((TABLE_LIST *)view->first_select_lex()->table_list.first)->
- single_table_updatable());
+ TABLE_LIST *first= (TABLE_LIST *)get_unit()->first_select()->table_list.first;
+ bool res= first->single_table_updatable(thd);
+ return res;
}
return true;
}
-/*
- Merge ON expressions for a view
+/**
+ Return merged WHERE clause and join conditions for a view
- SYNOPSIS
- merge_on_conds()
- thd thread handle
- table table for the VIEW
- is_cascaded TRUE <=> merge ON expressions from underlying views
+ @param thd thread handle
+ @param table table for the VIEW
+ @param[out] pcond Pointer to the built condition (NULL if none)
- DESCRIPTION
- This function returns the result of ANDing the ON expressions
- of the given view and all underlying views. The ON expressions
- of the underlying views are added only if is_cascaded is TRUE.
+ This function returns the result of ANDing the WHERE clause and the
+ join conditions of the given view.
- RETURN
- Pointer to the built expression if there is any.
- Otherwise and in the case of a failure NULL is returned.
+ @returns false for success, true for error
*/
-static Item *
-merge_on_conds(THD *thd, TABLE_LIST *table, bool is_cascaded)
+static bool merge_join_conditions(THD *thd, TABLE_LIST *table, Item **pcond)
{
- DBUG_ENTER("merge_on_conds");
-
- Item *cond= NULL;
- DBUG_PRINT("info", ("alias: %s", table->alias.str));
- if (table->on_expr)
- cond= table->on_expr->copy_andor_structure(thd);
- if (!table->view)
- DBUG_RETURN(cond);
- for (TABLE_LIST *tbl=
- (TABLE_LIST*)table->view->first_select_lex()->table_list.first;
- tbl;
- tbl= tbl->next_local)
+ DBUG_ENTER("merge_join_conditions");
+ *pcond = NULL;
+ DBUG_PRINT("info", ("alias: %s", table->alias));
+ if (table->where)
+ {
+ if (!(*pcond = table->where->copy_andor_structure(thd)))
+ DBUG_RETURN(true); /* purecov: inspected */
+ }
+ else if (table->on_expr)
+ {
+ if (!(*pcond = table->on_expr->copy_andor_structure(thd)))
+ DBUG_RETURN(true); /* purecov: inspected */
+ }
+ if (!table->nested_join && !table->is_merged_derived())
+ DBUG_RETURN(false);
+ List<TABLE_LIST> *join_list= table->is_merged_derived() ?
+ table->get_unit()->first_select()->join_list :
+ &table->nested_join->join_list;
+ List_iterator<TABLE_LIST> li(*join_list);
+ while (TABLE_LIST *tbl = li++)
{
- if (tbl->view && !is_cascaded)
+ if (tbl->is_view())
continue;
- cond= and_conds(thd, cond, merge_on_conds(thd, tbl, is_cascaded));
+ Item *cond;
+ if (merge_join_conditions(thd, tbl, &cond))
+ DBUG_RETURN(true); /* purecov: inspected */
+ if (cond && !(*pcond = and_conds(thd, *pcond, cond)))
+ DBUG_RETURN(true); /* purecov: inspected */
}
- DBUG_RETURN(cond);
+ DBUG_RETURN(false);
}
-/*
+/**
Prepare check option expression of table
- SYNOPSIS
- TABLE_LIST::prep_check_option()
- thd - thread handler
- check_opt_type - WITH CHECK OPTION type (VIEW_CHECK_NONE,
- VIEW_CHECK_LOCAL, VIEW_CHECK_CASCADED)
- we use this parameter instead of direct check of
- effective_with_check to change type of underlying
- views to VIEW_CHECK_CASCADED if outer view have
- such option and prevent processing of underlying
- view check options if outer view have just
- VIEW_CHECK_LOCAL option.
+ @param thd thread handler
+ @param is_cascaded True if parent view requests that this view's
+ filtering condition be treated as WITH CASCADED CHECK OPTION; this is for
+ recursive calls; user code should omit this argument.
- NOTE
- This method builds check option condition to use it later on
- every call (usual execution or every SP/PS call).
- This method have to be called after WHERE preparation
- (TABLE_LIST::prep_where)
+ @details
- RETURN
- FALSE - OK
- TRUE - error
+ This function builds check option condition for use in regular execution or
+ subsequent SP/PS executions.
+
+ This function must be called after the WHERE clause and join condition
+ of this and all underlying derived tables/views have been resolved.
+
+ The function will always call itself recursively for all underlying views
+ and base tables.
+
+ On first invocation, the check option condition is built bottom-up in
+ statement mem_root, and check_option_processed is set true.
+
+ On subsequent executions, check_option_processed is true and no
+ expression building is necessary. However, the function needs to assure that
+ the expression is resolved by calling fix_fields() on it.
+
+ @returns false if success, true if error
*/
bool TABLE_LIST::prep_check_option(THD *thd, uint8 check_opt_type)
{
DBUG_ENTER("TABLE_LIST::prep_check_option");
- bool is_cascaded= check_opt_type == VIEW_CHECK_CASCADED;
- TABLE_LIST *merge_underlying_list= view->first_select_lex()->get_table_list();
- for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
+ DBUG_ASSERT(is_view_or_derived());
+
+ /*
+ True if conditions of underlying views should be treated as WITH CASCADED
+ CHECK OPTION
+ */
+ for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local)
{
- /* see comment of check_opt_type parameter */
- if (tbl->view && tbl->prep_check_option(thd, (is_cascaded ?
- VIEW_CHECK_CASCADED :
- VIEW_CHECK_NONE)))
- DBUG_RETURN(TRUE);
+ if (!tbl->is_view_or_derived())
+ continue;
+ uint8 next_check_opt_type= tbl->with_check;
+ if (tbl->is_derived() || check_opt_type == VIEW_CHECK_CASCADED)
+ next_check_opt_type= check_opt_type;
+ if (tbl->is_view() &&
+ tbl->prep_check_option(thd, next_check_opt_type))
+ DBUG_RETURN(true); /* purecov: inspected */
}
- if (check_opt_type && !check_option_processed)
+ if (check_opt_type != VIEW_CHECK_NONE && !check_option_processed)
{
Query_arena *arena= thd->stmt_arena, backup;
arena= thd->activate_stmt_arena_if_needed(&backup); // For easier test
- if (where)
+ if (merge_join_conditions(thd, this, &check_option))
+ DBUG_RETURN(true);
+
+ for (TABLE_LIST *tbl = merge_underlying_list; tbl; tbl = tbl->next_local)
{
- check_option= where->copy_andor_structure(thd);
+ if (tbl->check_option &&
+ !(check_option = and_conds(thd, check_option, tbl->check_option)))
+ DBUG_RETURN(true); /* purecov: inspected */
}
- if (is_cascaded)
+
+ if (check_option)
{
- for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
+ Item *clone= check_option->build_clone(thd);
+ // DBUG_ASSERT(clone);
+ if (clone)
{
- if (tbl->check_option)
- check_option= and_conds(thd, check_option, tbl->check_option);
+ check_option= clone;
+ check_option->cleanup();
}
}
- check_option= and_conds(thd, check_option,
- merge_on_conds(thd, this, is_cascaded));
-
if (arena)
thd->restore_active_arena(arena, &backup);
- check_option_processed= TRUE;
-
+ check_option_processed= true;
}
- if (check_option)
- {
- const char *save_where= thd->where;
- thd->where= "check option";
+ if (check_option) {
+ const char *save_where = thd->where;
+ thd->where = "check option";
if (check_option->fix_fields_if_needed_for_bool(thd, &check_option))
- DBUG_RETURN(TRUE);
- thd->where= save_where;
+ DBUG_RETURN(true); /* purecov: inspected */
+ thd->where = save_where;
}
- DBUG_RETURN(FALSE);
+
+ DBUG_RETURN(false);
}
@@ -6470,6 +6495,8 @@ void TABLE_LIST::set_check_merged()
!derived->first_select()->exclude_from_table_unique_test ||
derived->outer_select()->
exclude_from_table_unique_test);
+ if (is_derived())
+ grant.privilege= ALL_KNOWN_ACL;
}
#endif
@@ -6654,7 +6681,7 @@ LEX_CSTRING *Field_iterator_table::name()
Item *Field_iterator_table::create_item(THD *thd)
{
- SELECT_LEX *select= thd->lex->current_select;
+ SELECT_LEX *select= table->select_lex;
Item_field *item= new (thd->mem_root) Item_field(thd, &select->context, *ptr);
DBUG_ASSERT(strlen(item->name.str) == item->name.length);
@@ -6715,9 +6742,14 @@ Item *create_view_field(THD *thd, TABLE_LIST *view, Item **field_ref,
{
DBUG_RETURN(field);
}
- Name_resolution_context *context= (view->view ?
- &view->view->first_select_lex()->context:
- &thd->lex->first_select_lex()->context);
+ Name_resolution_context *context;
+ if (view->is_view())
+ context= &view->view->first_select_lex()->context;
+ else if (view->is_derived())
+ context= &view->derived->first_select()->context;
+ else
+ context= &thd->lex->first_select_lex()->context;
+
Item *item= (new (thd->mem_root)
Item_direct_view_ref(thd, context, field_ref, view->alias,
*name, view));
@@ -6788,7 +6820,10 @@ void Field_iterator_table_ref::set_field_iterator()
table_ref->alias.str));
}
/* This is a merge view, so use field_translation. */
- else if (table_ref->field_translation)
+ else if (table_ref->field_translation &&
+ !(table_ref->is_view_or_derived() &&
+ table_ref->select_lex->master_unit()->
+ thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_PREPARE))
{
DBUG_ASSERT(table_ref->is_merged_derived());
field_it= &view_field_it;
@@ -6798,7 +6833,7 @@ void Field_iterator_table_ref::set_field_iterator()
/* This is a base table or stored view. */
else
{
- DBUG_ASSERT(table_ref->table || table_ref->view);
+ DBUG_ASSERT(table_ref->table || table_ref->view || table_ref->is_merged_derived());
field_it= &table_field_it;
DBUG_PRINT("info", ("field_it for '%s' is Field_iterator_table",
table_ref->alias.str));
@@ -6841,7 +6876,11 @@ const char *Field_iterator_table_ref::get_table_name()
if (table_ref->view)
return table_ref->view_name.str;
if (table_ref->is_derived())
+ {
+ if (table_ref->is_merged_derived())
+ return table_ref->alias.str;
return table_ref->table->s->table_name.str;
+ }
else if (table_ref->is_natural_join)
return natural_join_it.column_ref()->safe_table_name();
@@ -6856,6 +6895,8 @@ const char *Field_iterator_table_ref::get_db_name()
{
if (table_ref->view)
return table_ref->view_db.str;
+ if (table_ref->is_merged_derived())
+ return 0;
else if (table_ref->is_natural_join)
return natural_join_it.column_ref()->safe_db_name();
@@ -9180,13 +9221,14 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
/* A subquery might be forced to be materialized due to a side-effect. */
if (!is_materialized_derived() && first_select->is_mergeable() &&
optimizer_flag(thd, OPTIMIZER_SWITCH_DERIVED_MERGE) &&
- !thd->lex->can_not_use_merged() &&
- !(thd->lex->sql_command == SQLCOM_UPDATE_MULTI ||
- thd->lex->sql_command == SQLCOM_DELETE_MULTI) &&
+ (!thd->lex->can_not_use_merged() || is_derived()) &&
!is_recursive_with_table())
set_merged_derived();
else
+ {
+ merge_underlying_list= 0;
set_materialized_derived();
+ }
}
/*
Derived tables/view are materialized prior to UPDATE, thus we can skip
@@ -9196,7 +9238,34 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
{
set_check_materialized();
}
+ else if ((is_derived()) &&
+ (updatable= unit->first_select()->is_mergeable()))
+ {
+ List_iterator<Item> it(first_select->item_list);
+ Item *item;
+ bool updatable_field= false;
+ while ((item= it++))
+ {
+ if (item->real_item()->type() == Item::FIELD_ITEM)
+ updatable_field= true;
+ }
+ bool underlying_updatable_table= false;
+ for (TABLE_LIST *tbl= unit->first_select()->table_list.first;
+ tbl; tbl= tbl->next_local)
+ {
+ if ((tbl->is_view() && !tbl->updatable_view) ||
+ (tbl->is_derived() && !tbl->updatable) ||
+ !tbl->updatable || tbl->schema_table)
+ continue;
+ underlying_updatable_table= true;
+ break;
+ }
+ updatable= underlying_updatable_table && updatable_field;
+ }
+ int rc= 0;
+ if (!thd->lex->can_use_merged())
+ merge_underlying_list= 0;
/*
Create field translation for mergeable derived tables/views.
For derived tables field translation can be created only after
@@ -9207,10 +9276,10 @@ bool TABLE_LIST::init_derived(THD *thd, bool init_view)
if (is_view() ||
(unit->prepared &&
!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW)))
- create_field_translation(thd);
+ rc= create_field_translation(thd);
}
- return FALSE;
+ return MY_TEST(rc);
}
diff --git a/sql/table.h b/sql/table.h
index f2fad6c..fc4ec1f 100644
--- a/sql/table.h
+++ b/sql/table.h
@@ -1306,6 +1306,7 @@ struct TABLE
MY_BITMAP def_rpl_write_set;
MY_BITMAP eq_join_set; /* used to mark equi-joined fields */
MY_BITMAP cond_set; /* used to mark fields from sargable conditions*/
+ MY_BITMAP value_set;
/* Active column sets */
MY_BITMAP *read_set, *write_set, *rpl_write_set;
/* On INSERT: fields that the user specified a value for */
@@ -1690,7 +1691,6 @@ struct TABLE
best_range_rowid_filter_for_partial_join(uint access_key_no,
double records,
double access_cost_factor);
-
/**
System Versioning support
*/
@@ -2100,6 +2100,20 @@ struct vers_select_conds_t
struct LEX;
class Index_hint;
+
+
+struct TABLE_CHAIN
+{
+ TABLE_CHAIN() {}
+
+ TABLE_LIST **start_pos;
+ TABLE_LIST ** end_pos;
+
+ void set_start_pos(TABLE_LIST **pos) { start_pos= pos; }
+ void set_end_pos(TABLE_LIST **pos) { end_pos= pos; }
+};
+
+
struct TABLE_LIST
{
TABLE_LIST() {} /* Remove gcc warning */
@@ -2187,6 +2201,7 @@ struct TABLE_LIST
TABLE_LIST *next_local;
/* link in a global list of all queries tables */
TABLE_LIST *next_global, **prev_global;
+ TABLE_LIST *next_ordered;
LEX_CSTRING db;
LEX_CSTRING table_name;
LEX_CSTRING schema_table_name;
@@ -2434,9 +2449,13 @@ struct TABLE_LIST
/* call back function for asking handler about caching in query cache */
qc_engine_callback callback_func;
thr_lock_type lock_type;
+ enum_mdl_type mdl_type;
+ ulong table_options;
+ bool is_mdl_request_type_to_be_set;
uint outer_join; /* Which join type */
uint shared; /* Used in multi-upd */
bool updatable; /* VIEW/TABLE can be updated now */
+ bool single_table_update;
bool straight; /* optimize with prev table */
bool updating; /* for replicate-do/ignore table */
bool force_index; /* prefer index over table scan */
@@ -2612,10 +2631,10 @@ struct TABLE_LIST
This is used for single-table UPDATE/DELETE when they are modifying a
single-table VIEW.
*/
- TABLE_LIST *find_table_for_update()
+ TABLE_LIST *find_table_for_update(THD *thd)
{
TABLE_LIST *tbl= this;
- while(!tbl->is_multitable() && tbl->single_table_updatable() &&
+ while(!tbl->is_multitable() && tbl->single_table_updatable(thd) &&
tbl->merge_underlying_list)
{
tbl= tbl->merge_underlying_list;
@@ -2737,6 +2756,7 @@ struct TABLE_LIST
(derived ? DTYPE_MASK : DTYPE_VIEW)) |
DTYPE_TABLE | DTYPE_MATERIALIZE);
set_check_materialized();
+ field_translation= 0;
DBUG_VOID_RETURN;
}
bool is_multitable() const { return (derived_type & DTYPE_MULTITABLE); }
@@ -2744,9 +2764,9 @@ struct TABLE_LIST
{
derived_type|= DTYPE_MULTITABLE;
}
- bool set_as_with_table(THD *thd, With_element *with_elem);
void reset_const_table();
bool handle_derived(LEX *lex, uint phases);
+ void propagate_properties_for_mergeable_derived();
/**
@brief True if this TABLE_LIST represents an anonymous derived table,
@@ -2776,7 +2796,7 @@ struct TABLE_LIST
int fetch_number_of_rows();
bool change_refs_to_fields();
- bool single_table_updatable();
+ bool single_table_updatable(THD *thd);
bool is_inner_table_of_outer_join()
{
@@ -2826,7 +2846,7 @@ class Item;
class Field_iterator: public Sql_alloc
{
public:
- Field_iterator() {} /* Remove gcc warning */
+ Field_iterator() {} /* Remove gcc warning */
virtual ~Field_iterator() {}
virtual void set(TABLE_LIST *)= 0;
virtual void next()= 0;
@@ -2845,9 +2865,10 @@ class Field_iterator: public Sql_alloc
class Field_iterator_table: public Field_iterator
{
Field **ptr;
+ TABLE_LIST *table;
public:
- Field_iterator_table() :ptr(0) {}
- void set(TABLE_LIST *table) { ptr= table->table->field; }
+ Field_iterator_table() :ptr(0), table(0) {}
+ void set(TABLE_LIST *tl) { table= tl; ptr= tl->table->field; }
void set_table(TABLE *table) { ptr= table->field; }
void next() { ptr++; }
bool end_of_fields() { return *ptr == 0; }
1
0
[Commits] de5aeee3dae: MDEV-20129: Equality propagation for ORDER BY items do not work with expressions
by varun 30 Sep '20
by varun 30 Sep '20
30 Sep '20
revision-id: de5aeee3daef05f39889b5df073261cb78a04e6e (mariadb-10.5.2-272-gde5aeee3dae)
parent(s): 3b72b35a776b473c15df5afa5846b859797d9473
author: Varun Gupta
committer: Varun Gupta
timestamp: 2020-09-30 20:13:28 +0530
message:
MDEV-20129: Equality propagation for ORDER BY items do not work with expressions
Equality propagation for ORDER BY clause should be allowed for
expression. This would allow the expression in the ORDER BY clause
to be substitute with the best field.
Also moved the function make_sortorder from execution to optimization
stage.
---
mysql-test/main/order_by.result | 103 ++++++++++++++++++++++++++++++++++++++++
mysql-test/main/order_by.test | 52 ++++++++++++++++++++
sql/filesort.cc | 2 +-
sql/filesort.h | 1 +
sql/item.cc | 4 ++
sql/sql_delete.cc | 1 +
sql/sql_select.cc | 24 +++++-----
sql/sql_table.cc | 2 +
sql/sql_update.cc | 1 +
sql/sql_window.cc | 5 ++
10 files changed, 183 insertions(+), 12 deletions(-)
diff --git a/mysql-test/main/order_by.result b/mysql-test/main/order_by.result
index 29a31a9c28c..3b9dc758817 100644
--- a/mysql-test/main/order_by.result
+++ b/mysql-test/main/order_by.result
@@ -4260,4 +4260,107 @@ a group_concat(t1.b)
96 2
58 1
DROP TABLE t1, t2;
+#
+# MDEV-20129: Equality propagation for ORDER BY items do not work with expressions
+#
+create table ten(a int);
+insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int, b int);
+insert into t1 select A.a + B.a* 10, A.a + B.a* 10 from ten A, ten B;
+create table t2(a int, b int, key(a));
+insert into t2 select A.a + B.a* 10, A.a+B.a*10 from ten A, ten B;
+should have Using Filesort only
+explain select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where; Using filesort
+1 SIMPLE t2 ref a a 5 test.t1.a 1
+select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a;
+a b a b
+96 96 96 96
+97 97 97 97
+98 98 98 98
+99 99 99 99
+should have Using Filesort only with expressions too
+explain select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a+t1.b;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 100 Using where; Using filesort
+1 SIMPLE t2 ref a a 5 test.t1.a 1
+select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a+t1.b;
+a b a b
+96 96 96 96
+97 97 97 97
+98 98 98 98
+99 99 99 99
+drop table t1,t2,ten;
+CREATE TABLE t1(a int, b int);
+CREATE TABLE t2(a int, b int, key(a));
+INSERT INTO t1 VALUES (1,1), (2,2);
+INSERT INTO t2 VALUES (1,4), (1,5), (2,4), (2,5);
+INSERT INTO t2 VALUES (1,6), (1,7), (2,6), (2,7);
+INSERT INTO t2 VALUES (1,9), (1,8), (2,8), (2,9);
+CREATE VIEW v1 AS
+SELECT t1.a as c, t2.b as d FROM t1,t2 WHERE t1.a=t2.a;
+# should have Using Filesort only
+explain SELECT c,d FROM v1 ORDER BY c DESC;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using filesort
+1 SIMPLE t2 ref a a 5 test.t1.a 2
+SELECT c,d FROM v1 ORDER BY c DESC;
+c d
+2 4
+2 5
+2 6
+2 7
+2 8
+2 9
+1 4
+1 5
+1 6
+1 7
+1 9
+1 8
+CREATE VIEW v2 AS
+SELECT t1.b+t2.a as c, abs(t2.a) as d, t2.b as e FROM t1,t2 WHERE t1.a=t2.a;
+views use Item_direct_view_ref that are expressions
+should have Using Filesort only
+explain SELECT c,e FROM v2 ORDER BY c DESC;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using filesort
+1 SIMPLE t2 ref a a 5 test.t1.a 2
+SELECT c,e FROM v2 ORDER BY c DESC;
+c e
+4 4
+4 5
+4 6
+4 7
+4 8
+4 9
+2 4
+2 5
+2 6
+2 7
+2 9
+2 8
+views use Item_direct_view_ref that are expressions
+should have Using Filesort only
+explain SELECT d,e FROM v2 ORDER BY d DESC;
+id select_type table type possible_keys key key_len ref rows Extra
+1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using filesort
+1 SIMPLE t2 ref a a 5 test.t1.a 2
+SELECT d,e FROM v2 ORDER BY d DESC;
+d e
+2 4
+2 5
+2 6
+2 7
+2 8
+2 9
+1 4
+1 5
+1 6
+1 7
+1 9
+1 8
+DROP TABLE t1,t2;
+DROP VIEW v1,v2;
# End of 10.6 tests
diff --git a/mysql-test/main/order_by.test b/mysql-test/main/order_by.test
index fd87f54cc3d..4c4c22fbf66 100644
--- a/mysql-test/main/order_by.test
+++ b/mysql-test/main/order_by.test
@@ -2591,4 +2591,56 @@ eval EXPLAIN FORMAT=JSON $query;
eval $query;
DROP TABLE t1, t2;
+--echo #
+--echo # MDEV-20129: Equality propagation for ORDER BY items do not work with expressions
+--echo #
+
+create table ten(a int);
+insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
+create table t1(a int, b int);
+insert into t1 select A.a + B.a* 10, A.a + B.a* 10 from ten A, ten B;
+create table t2(a int, b int, key(a));
+insert into t2 select A.a + B.a* 10, A.a+B.a*10 from ten A, ten B;
+
+--echo should have Using Filesort only
+explain select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a;
+select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a;
+
+--echo should have Using Filesort only with expressions too
+explain select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a+t1.b;
+select * from t1,t2 where t1.a > 95 and t1.a=t2.a order by t2.a+t1.b;
+drop table t1,t2,ten;
+
+CREATE TABLE t1(a int, b int);
+CREATE TABLE t2(a int, b int, key(a));
+INSERT INTO t1 VALUES (1,1), (2,2);
+INSERT INTO t2 VALUES (1,4), (1,5), (2,4), (2,5);
+INSERT INTO t2 VALUES (1,6), (1,7), (2,6), (2,7);
+INSERT INTO t2 VALUES (1,9), (1,8), (2,8), (2,9);
+CREATE VIEW v1 AS
+SELECT t1.a as c, t2.b as d FROM t1,t2 WHERE t1.a=t2.a;
+
+--echo # should have Using Filesort only
+let $query= SELECT c,d FROM v1 ORDER BY c DESC;
+eval explain $query;
+eval $query;
+
+CREATE VIEW v2 AS
+SELECT t1.b+t2.a as c, abs(t2.a) as d, t2.b as e FROM t1,t2 WHERE t1.a=t2.a;
+--echo views use Item_direct_view_ref that are expressions
+--echo should have Using Filesort only
+let $query= SELECT c,e FROM v2 ORDER BY c DESC;
+eval explain $query;
+eval $query;
+
+
+--echo views use Item_direct_view_ref that are expressions
+--echo should have Using Filesort only
+let $query= SELECT d,e FROM v2 ORDER BY d DESC;
+eval explain $query;
+eval $query;
+
+DROP TABLE t1,t2;
+DROP VIEW v1,v2;
+
--echo # End of 10.6 tests
diff --git a/sql/filesort.cc b/sql/filesort.cc
index 2a713ecf97b..180a69baa79 100644
--- a/sql/filesort.cc
+++ b/sql/filesort.cc
@@ -208,7 +208,7 @@ SORT_INFO *filesort(THD *thd, TABLE *table, Filesort *filesort,
DBUG_ENTER("filesort");
- if (!(sort_keys= filesort->make_sortorder(thd, join, first_table_bit)))
+ if (!(sort_keys= filesort->get_sort_keys()))
DBUG_RETURN(NULL); /* purecov: inspected */
s_length= static_cast<uint>(sort_keys->size());
diff --git a/sql/filesort.h b/sql/filesort.h
index 29ae5e20cc6..af3af3ca4a5 100644
--- a/sql/filesort.h
+++ b/sql/filesort.h
@@ -90,6 +90,7 @@ class Filesort: public Sql_alloc
Sort_keys* make_sortorder(THD *thd, JOIN *join, table_map first_table_bit);
/* Unpack temp table columns to base table columns*/
void (*unpack)(TABLE *);
+ Sort_keys* get_sort_keys() { return sort_keys; }
private:
void cleanup();
diff --git a/sql/item.cc b/sql/item.cc
index 52274380cd1..f3c673492ae 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -9138,7 +9138,11 @@ Item *Item_direct_view_ref::propagate_equal_fields(THD *thd,
{
Item *field_item= real_item();
if (field_item->type() != FIELD_ITEM)
+ {
+ Item *res= field_item->propagate_equal_fields(thd, ctx, cond);
+ DBUG_ASSERT(res == field_item);
return this;
+ }
Item *item= field_item->propagate_equal_fields(thd, ctx, cond);
set_item_equal(field_item->get_item_equal());
field_item->set_item_equal(NULL);
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index 7280236e43f..0c679205db6 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -630,6 +630,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
{
{
Filesort fsort(order, HA_POS_ERROR, true, select);
+ fsort.make_sortorder(thd, NULL, table->map);
DBUG_ASSERT(query_plan.index == MAX_KEY);
Filesort_tracker *fs_tracker=
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 420a64ba827..db79e4cbd05 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -3864,6 +3864,8 @@ JOIN::add_sorting_to_table(JOIN_TAB *tab, ORDER *order)
if (!tab->filesort)
return true;
+ tab->filesort->make_sortorder(thd, this, tab->table->map);
+
TABLE *table= tab->table;
if ((tab == join_tab + const_tables) &&
table->pos_in_table_list &&
@@ -14253,7 +14255,6 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond,
bool can_subst_to_first_table= false;
if (optimizer_flag(join->thd, OPTIMIZER_SWITCH_ORDERBY_EQ_PROP) &&
first_is_base_table &&
- order->item[0]->real_item()->type() == Item::FIELD_ITEM &&
join->cond_equal)
{
table_map first_table_bit=
@@ -14273,16 +14274,17 @@ remove_const(JOIN *join,ORDER *first_order, COND *cond,
Value_source::
Context_identity(),
join->cond_equal);
- Item_equal *item_eq;
- if ((item_eq= res->get_item_equal()))
- {
- Item *first= item_eq->get_first(NO_PARTICULAR_TAB, NULL);
- if (first->const_item() || first->used_tables() ==
- first_table_bit)
- {
- can_subst_to_first_table= true;
- }
- }
+
+ res=
+ substitute_for_best_equal_field(join->thd, NO_PARTICULAR_TAB,
+ res,
+ join->cond_equal,join->map2table,
+ true);
+ res->update_used_tables();
+
+ if (!(res->used_tables() & ~ first_table_bit))
+ can_subst_to_first_table= true;
+
}
if (!can_subst_to_first_table)
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 15d190c3139..396d27eb652 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -11224,6 +11224,8 @@ copy_data_between_tables(THD *thd, TABLE *from, TABLE *to,
&tables, fields, all_fields, order))
goto err;
+ fsort.make_sortorder(thd, NULL, from->map);
+
if (!(file_sort= filesort(thd, from, &fsort, &dummy_tracker)))
goto err;
}
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index fccc2a426c4..9b997a5401f 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -769,6 +769,7 @@ int mysql_update(THD *thd,
NOTE: filesort will call table->prepare_for_position()
*/
Filesort fsort(order, limit, true, select);
+ fsort.make_sortorder(thd, NULL, table->map);
Filesort_tracker *fs_tracker=
thd->lex->explain->get_upd_del_plan()->filesort_tracker;
diff --git a/sql/sql_window.cc b/sql/sql_window.cc
index af6a73006a8..60b186c1679 100644
--- a/sql/sql_window.cc
+++ b/sql/sql_window.cc
@@ -3076,6 +3076,11 @@ bool Window_funcs_sort::setup(THD *thd, SQL_SELECT *sel,
}
filesort= new (thd->mem_root) Filesort(sort_order, HA_POS_ERROR, true, NULL);
+ if (!filesort)
+ return true;
+
+ filesort->make_sortorder(thd, join_tab->join, join_tab->table->map);
+
/* Apply the same condition that the subsequent sort has. */
filesort->select= sel;
1
0
[Commits] fbfc1470ee4: MDEV-21829: Packed Sort Keys in Unique: Address review input
by Sergei Petrunia 29 Sep '20
by Sergei Petrunia 29 Sep '20
29 Sep '20
revision-id: fbfc1470ee49bdccad51f0f3a5a0b1fcc88bcef8 (mariadb-10.5.2-494-gfbfc1470ee4)
parent(s): af34fcbe4f5a1e20fef1aef4b404678a52e0157f
author: Sergei Petrunia
committer: Sergei Petrunia
timestamp: 2020-09-29 15:33:02 +0300
message:
MDEV-21829: Packed Sort Keys in Unique: Address review input
Make the Unique class an interface, and move the implementation
into class Unique_impl.
The idea is to allow seamless change of Unique class's implementation/
to something else.
The construction of Unique object still exposes the implementation:
the code uses "new Unique_impl(...)". It will be hidden in a factory
function.
---
sql/item_sum.cc | 4 ++--
sql/opt_range.cc | 20 +++++++++----------
sql/sql_delete.cc | 8 ++++----
sql/sql_statistics.cc | 4 ++--
sql/uniques.cc | 32 +++++++++++++++----------------
sql/uniques.h | 53 +++++++++++++++++++++++++++++++++++++++------------
6 files changed, 75 insertions(+), 46 deletions(-)
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index d1023ba65db..0f544142bff 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -1178,7 +1178,7 @@ void Aggregator_distinct::endup()
{
DBUG_ASSERT(item_sum->fixed == 1);
Item_sum_count *sum= (Item_sum_count *)item_sum;
- if (tree && tree->elements == 0)
+ if (tree && tree->get_n_elements() == 0)
{
/* everything fits in memory */
sum->count= (longlong) tree->elements_in_tree();
@@ -4948,7 +4948,7 @@ Unique* Item_sum::get_unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
if (allow_packing)
return new Unique_packed(comp_func, comp_func_fixed_arg, size_arg,
max_in_memory_size_arg, min_dupl_count_arg);
- return new Unique(comp_func, comp_func_fixed_arg, size_arg,
+ return new Unique_impl(comp_func, comp_func_fixed_arg, size_arg,
max_in_memory_size_arg, min_dupl_count_arg);
}
diff --git a/sql/opt_range.cc b/sql/opt_range.cc
index 7d65c85129b..8720e0fa7c4 100644
--- a/sql/opt_range.cc
+++ b/sql/opt_range.cc
@@ -5209,7 +5209,7 @@ TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
/* Add Unique operations cost */
unique_calc_buff_size=
- Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
+ Unique_impl::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
param->table->file->ref_length,
(size_t)param->thd->variables.sortbuff_size);
if (param->imerge_cost_buff_size < unique_calc_buff_size)
@@ -5221,7 +5221,7 @@ TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
}
{
- const double dup_removal_cost= Unique::get_use_cost(
+ const double dup_removal_cost= Unique_impl::get_use_cost(
param->imerge_cost_buff, (uint)non_cpk_scan_records,
param->table->file->ref_length,
(size_t)param->thd->variables.sortbuff_size,
@@ -5831,7 +5831,7 @@ bool prepare_search_best_index_intersect(PARAM *param,
return TRUE;
size_t calc_cost_buff_size=
- Unique::get_cost_calc_buff_size((size_t)records_in_scans,
+ Unique_impl::get_cost_calc_buff_size((size_t)records_in_scans,
common->key_size,
common->max_memory_size);
if (!(common->buff_elems= (uint *) alloc_root(param->mem_root,
@@ -6180,7 +6180,7 @@ bool check_index_intersect_extension(PARTIAL_INDEX_INTERSECT_INFO *curr,
*/
ha_rows elems_in_tree= common_info->search_scans[0]->records-
common_info->search_scans[0]->filtered_out ;
- next->in_memory_cost+= Unique::get_search_cost(elems_in_tree,
+ next->in_memory_cost+= Unique_impl::get_search_cost(elems_in_tree,
common_info->compare_factor)*
ext_index_scan_records;
cost= next->in_memory_cost;
@@ -6193,7 +6193,7 @@ bool check_index_intersect_extension(PARTIAL_INDEX_INTERSECT_INFO *curr,
size_t max_memory_size= common_info->max_memory_size;
records_sent_to_unique+= ext_index_scan_records;
- cost= Unique::get_use_cost(buff_elems, (size_t) records_sent_to_unique, key_size,
+ cost= Unique_impl::get_use_cost(buff_elems, (size_t) records_sent_to_unique, key_size,
max_memory_size, compare_factor, TRUE,
&next->in_memory);
if (records_filtered_out_by_cpk)
@@ -6203,7 +6203,7 @@ bool check_index_intersect_extension(PARTIAL_INDEX_INTERSECT_INFO *curr,
double cost2;
bool in_memory2;
ha_rows records2= records_sent_to_unique-records_filtered_out_by_cpk;
- cost2= Unique::get_use_cost(buff_elems, (size_t) records2, key_size,
+ cost2= Unique_impl::get_use_cost(buff_elems, (size_t) records2, key_size,
max_memory_size, compare_factor, TRUE,
&in_memory2);
cost2+= get_cpk_filter_cost(ext_index_scan_records, common_info->cpk_scan,
@@ -11758,7 +11758,7 @@ int read_keys_and_merge_scans(THD *thd,
DBUG_EXECUTE_IF("only_one_Unique_may_be_created",
DBUG_SET("+d,index_merge_may_not_create_a_Unique"); );
- unique= new Unique(refpos_order_cmp, (void *)file,
+ unique= new Unique_impl(refpos_order_cmp, (void *)file,
file->ref_length,
(size_t)thd->variables.sortbuff_size,
intersection ? quick_selects.elements : 0);
@@ -11830,7 +11830,7 @@ int read_keys_and_merge_scans(THD *thd,
*/
head->file->ha_end_keyread();
if (init_read_record(read_record, thd, head, (SQL_SELECT*) 0,
- &unique->sort, 1 , 1, TRUE))
+ unique->get_sort(), 1 , 1, TRUE))
result= 1;
DBUG_RETURN(result);
@@ -11873,7 +11873,7 @@ int QUICK_INDEX_MERGE_SELECT::get_next()
result= HA_ERR_END_OF_FILE;
end_read_record(&read_record);
// Free things used by sort early. Shouldn't be strictly necessary
- unique->sort.reset();
+ unique->get_sort()->reset();
/* All rows from Unique have been retrieved, do a clustered PK scan */
if (pk_quick_select)
{
@@ -11908,7 +11908,7 @@ int QUICK_INDEX_INTERSECT_SELECT::get_next()
{
result= HA_ERR_END_OF_FILE;
end_read_record(&read_record);
- unique->sort.reset(); // Free things early
+ unique->get_sort()->reset(); // Free things early
}
DBUG_RETURN(result);
diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc
index db5b5b777a8..64cb025cce0 100644
--- a/sql/sql_delete.cc
+++ b/sql/sql_delete.cc
@@ -702,7 +702,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
clause. Instead of deleting the rows, first mark them deleted.
*/
ha_rows tmplimit=limit;
- deltempfile= new (thd->mem_root) Unique (refpos_order_cmp, table->file,
+ deltempfile= new (thd->mem_root) Unique_impl (refpos_order_cmp, table->file,
table->file->ref_length,
MEM_STRIP_BUF_SIZE);
@@ -727,7 +727,7 @@ bool mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds,
end_read_record(&info);
if (unlikely(deltempfile->get(table)) ||
unlikely(table->file->ha_index_or_rnd_end()) ||
- unlikely(init_read_record(&info, thd, table, 0, &deltempfile->sort, 0,
+ unlikely(init_read_record(&info, thd, table, 0, deltempfile->get_sort(), 0,
1, false)))
{
error= 1;
@@ -1269,7 +1269,7 @@ multi_delete::initialize_tables(JOIN *join)
for (;walk ;walk= walk->next_local)
{
TABLE *table=walk->table;
- *tempfiles_ptr++= new (thd->mem_root) Unique (refpos_order_cmp, table->file,
+ *tempfiles_ptr++= new (thd->mem_root) Unique_impl (refpos_order_cmp, table->file,
table->file->ref_length,
MEM_STRIP_BUF_SIZE);
}
@@ -1452,7 +1452,7 @@ int multi_delete::do_deletes()
if (unlikely(tempfiles[counter]->get(table)))
DBUG_RETURN(1);
- local_error= do_table_deletes(table, &tempfiles[counter]->sort,
+ local_error= do_table_deletes(table, tempfiles[counter]->get_sort(),
thd->lex->ignore);
if (unlikely(thd->killed) && likely(!local_error))
diff --git a/sql/sql_statistics.cc b/sql/sql_statistics.cc
index 60e5b17fa81..2cc132dfe2b 100644
--- a/sql/sql_statistics.cc
+++ b/sql/sql_statistics.cc
@@ -1732,7 +1732,7 @@ class Count_distinct_field: public Sql_alloc
}
tree_key_length= table_field->pack_length();
- tree= new Unique((qsort_cmp2) simple_str_key_cmp, (void*) table_field,
+ tree= new Unique_impl((qsort_cmp2) simple_str_key_cmp, (void*) table_field,
tree_key_length, max_heap_table_size, 1);
return tree == NULL;
@@ -1858,7 +1858,7 @@ class Count_distinct_field_bit: public Count_distinct_field
bool setup(THD *thd, size_t max_heap_table_size)
{
tree_key_length= sizeof(ulonglong);
- tree= new Unique((qsort_cmp2) simple_ulonglong_key_cmp,
+ tree= new Unique_impl((qsort_cmp2) simple_ulonglong_key_cmp,
(void*) &tree_key_length,
tree_key_length, max_heap_table_size, 1);
return tree == NULL;
diff --git a/sql/uniques.cc b/sql/uniques.cc
index 4ec032276fe..615387ddfce 100644
--- a/sql/uniques.cc
+++ b/sql/uniques.cc
@@ -40,25 +40,25 @@
#include "uniques.h" // Unique
#include "sql_sort.h"
-int unique_write_to_file(uchar* key, element_count count, Unique *unique)
+int unique_write_to_file(uchar* key, element_count count, Unique_impl *unique)
{
return unique->write_record_to_file(key) ? 1 : 0;
}
-int unique_write_to_file_with_count(uchar* key, element_count count, Unique *unique)
+int unique_write_to_file_with_count(uchar* key, element_count count, Unique_impl *unique)
{
return unique_write_to_file(key, count, unique) ||
my_b_write(&unique->file, (uchar*)&count, sizeof(element_count)) ? 1 : 0;
}
-int unique_write_to_ptrs(uchar* key, element_count count, Unique *unique)
+int unique_write_to_ptrs(uchar* key, element_count count, Unique_impl *unique)
{
memcpy(unique->sort.record_pointers, key, unique->size);
unique->sort.record_pointers+=unique->size;
return 0;
}
-int unique_intersect_write_to_ptrs(uchar* key, element_count count, Unique *unique)
+int unique_intersect_write_to_ptrs(uchar* key, element_count count, Unique_impl *unique)
{
if (count >= unique->min_dupl_count)
{
@@ -71,7 +71,7 @@ int unique_intersect_write_to_ptrs(uchar* key, element_count count, Unique *uniq
}
-Unique::Unique(qsort_cmp2 comp_func, void * comp_func_fixed_arg,
+Unique_impl::Unique_impl(qsort_cmp2 comp_func, void * comp_func_fixed_arg,
uint size_arg, size_t max_in_memory_size_arg,
uint min_dupl_count_arg)
:max_in_memory_size(max_in_memory_size_arg),
@@ -306,7 +306,7 @@ static double get_merge_many_buffs_cost(uint *buffer,
these will be random seeks.
*/
-double Unique::get_use_cost(uint *buffer, size_t nkeys, uint key_size,
+double Unique_impl::get_use_cost(uint *buffer, size_t nkeys, uint key_size,
size_t max_in_memory_size,
double compare_factor,
bool intersect_fl, bool *in_memory)
@@ -368,7 +368,7 @@ double Unique::get_use_cost(uint *buffer, size_t nkeys, uint key_size,
return result;
}
-Unique::~Unique()
+Unique_impl::~Unique_impl()
{
close_cached_file(&file);
delete_tree(&tree, 0);
@@ -377,7 +377,7 @@ Unique::~Unique()
/* Write tree to disk; clear tree */
-bool Unique::flush()
+bool Unique_impl::flush()
{
Merge_chunk file_ptr;
elements+= tree.elements_in_tree;
@@ -406,7 +406,7 @@ bool Unique::flush()
*/
void
-Unique::reset()
+Unique_impl::reset()
{
reset_tree(&tree);
/*
@@ -663,7 +663,7 @@ static bool merge_walk(uchar *merge_buffer, size_t merge_buffer_size,
<> 0 error
*/
-bool Unique::walk(TABLE *table, tree_walk_action action, void *walk_action_arg)
+bool Unique_impl::walk(TABLE *table, tree_walk_action action, void *walk_action_arg)
{
int res= 0;
uchar *merge_buffer;
@@ -712,7 +712,7 @@ bool Unique::walk(TABLE *table, tree_walk_action action, void *walk_action_arg)
TRUE.
SYNOPSIS
- Unique:merge()
+ Unique_impl::merge()
All params are 'IN':
table the parameter to access sort context
buff merge buffer
@@ -723,7 +723,7 @@ bool Unique::walk(TABLE *table, tree_walk_action action, void *walk_action_arg)
<> 0 error
*/
-bool Unique::merge(TABLE *table, uchar *buff, size_t buff_size,
+bool Unique_impl::merge(TABLE *table, uchar *buff, size_t buff_size,
bool without_last_merge)
{
IO_CACHE *outfile= &sort.io_cache;
@@ -804,12 +804,12 @@ bool Unique::merge(TABLE *table, uchar *buff, size_t buff_size,
rows will be read in priority order.
*/
-bool Unique::get(TABLE *table)
+bool Unique_impl::get(TABLE *table)
{
bool rc= 1;
uchar *sort_buffer= NULL;
sort.return_rows= elements+tree.elements_in_tree;
- DBUG_ENTER("Unique::get");
+ DBUG_ENTER("Unique_impl::get");
DBUG_ASSERT(is_packed() == FALSE);
@@ -860,7 +860,7 @@ bool Unique::get(TABLE *table)
Unique_packed::Unique_packed(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
uint size_arg, size_t max_in_memory_size_arg,
uint min_dupl_count_arg):
- Unique(comp_func, comp_func_fixed_arg, size_arg,
+ Unique_impl(comp_func, comp_func_fixed_arg, size_arg,
max_in_memory_size_arg, min_dupl_count_arg)
{
}
@@ -877,7 +877,7 @@ Unique_packed::Unique_packed(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
=0 Record successfully written
*/
-int Unique::write_record_to_file(uchar *key)
+int Unique_impl::write_record_to_file(uchar *key)
{
return my_b_write(get_file(), key, size);
}
diff --git a/sql/uniques.h b/sql/uniques.h
index 3d90039b597..64d22920adc 100644
--- a/sql/uniques.h
+++ b/sql/uniques.h
@@ -18,6 +18,33 @@
#include "filesort.h"
+class Unique : public Sql_alloc {
+public:
+
+ virtual void reset() = 0;
+ virtual bool unique_add(void *ptr, uint size_arg) = 0;
+ virtual ~Unique() {};
+
+ virtual void close_for_expansion() = 0;
+
+ virtual bool get(TABLE *table) = 0;
+ virtual bool walk(TABLE *table, tree_walk_action action, void *walk_action_arg)= 0;
+
+ virtual SORT_INFO *get_sort() = 0;
+
+ virtual ulong get_n_elements() = 0;
+ virtual size_t get_max_in_memory_size() const = 0;
+ virtual bool is_in_memory() = 0;
+
+ // This will be renamed:
+ virtual ulong elements_in_tree() = 0;
+
+ // These will be removed:
+ virtual uint get_size() const = 0;
+ virtual bool is_packed() = 0;
+ virtual uint get_full_size() const = 0;
+};
+
/*
Unique -- class for unique (removing of duplicates).
Puts all values to the TREE. If the tree becomes too big,
@@ -26,8 +53,7 @@
memory simultaneously with iteration, so it should be ~2-3x faster.
*/
-class Unique :public Sql_alloc
-{
+class Unique_impl : public Unique {
DYNAMIC_ARRAY file_ptrs;
ulong max_elements; /* Total number of elements that will be stored in-memory */
size_t max_in_memory_size;
@@ -69,11 +95,14 @@ class Unique :public Sql_alloc
public:
ulong elements;
+ ulong get_n_elements() override { return elements; }
SORT_INFO sort;
- Unique(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
+ SORT_INFO *get_sort() override { return &sort; }
+
+ Unique_impl(qsort_cmp2 comp_func, void *comp_func_fixed_arg,
uint size_arg, size_t max_in_memory_size_arg,
uint min_dupl_count_arg= 0);
- virtual ~Unique();
+ virtual ~Unique_impl();
ulong elements_in_tree() { return tree.elements_in_tree; }
/*
@@ -84,7 +113,7 @@ class Unique :public Sql_alloc
size length of the key
*/
- inline bool unique_add(void *ptr, uint size_arg)
+ bool unique_add(void *ptr, uint size_arg) override
{
DBUG_ENTER("unique_add");
DBUG_PRINT("info", ("tree %u - %lu", tree.elements_in_tree, max_elements));
@@ -132,10 +161,10 @@ class Unique :public Sql_alloc
return (int) (sizeof(uint)*(1 + nkeys/max_elems_in_tree));
}
- void reset();
+ void reset() override;
bool walk(TABLE *table, tree_walk_action action, void *walk_action_arg);
- uint get_size() const { return size; }
+ uint get_size() const override { return size; }
uint get_full_size() const { return full_size; }
size_t get_max_in_memory_size() const { return max_in_memory_size; }
bool is_count_stored() { return with_counters; }
@@ -145,13 +174,13 @@ class Unique :public Sql_alloc
// returns TRUE if the unique tree stores packed values
virtual bool is_packed() { return false; }
- friend int unique_write_to_file(uchar* key, element_count count, Unique *unique);
- friend int unique_write_to_ptrs(uchar* key, element_count count, Unique *unique);
+ friend int unique_write_to_file(uchar* key, element_count count, Unique_impl *unique);
+ friend int unique_write_to_ptrs(uchar* key, element_count count, Unique_impl *unique);
friend int unique_write_to_file_with_count(uchar* key, element_count count,
- Unique *unique);
+ Unique_impl *unique);
friend int unique_intersect_write_to_ptrs(uchar* key, element_count count,
- Unique *unique);
+ Unique_impl *unique);
};
@@ -162,7 +191,7 @@ class Unique :public Sql_alloc
inside the tree.
*/
-class Unique_packed : public Unique
+class Unique_packed : public Unique_impl
{
protected:
public:
1
0
[Commits] 7a1e338fbe6: MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view
by varun 28 Sep '20
by varun 28 Sep '20
28 Sep '20
revision-id: 7a1e338fbe69dcaf3f1e000c1948422426852886 (mariadb-10.2.31-462-g7a1e338fbe6)
parent(s): 7c5519c12d46ead947d341cbdcbb6fbbe4d4fe1b
author: Varun Gupta
committer: Varun Gupta
timestamp: 2020-09-28 13:17:39 +0530
message:
MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view
Introduced val_time_packed and val_datetime_packed functions for Item_direct_view_ref
to make sure to get the value from the item it is referring to.
The issue for incorrect result was that the item was getting its value
from the temporary table rather than from the view.
---
mysql-test/r/group_by.result | 44 ++++++++++++++++++++++++++++++++++++++++++++
mysql-test/t/group_by.test | 33 +++++++++++++++++++++++++++++++++
sql/item.cc | 17 +++++++++++++++++
sql/item.h | 2 ++
4 files changed, 96 insertions(+)
diff --git a/mysql-test/r/group_by.result b/mysql-test/r/group_by.result
index c996627486c..55e7703a377 100644
--- a/mysql-test/r/group_by.result
+++ b/mysql-test/r/group_by.result
@@ -2875,3 +2875,47 @@ WHERE c.table_schema=(SELECT COUNT(*) FROM INFORMATION_SCHEMA.columns GROUP BY c
GROUP BY t.table_name;
ERROR HY001: Out of sort memory, consider increasing server sort buffer size
SET max_sort_length= @save_max_sort_length;
+#
+# MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view
+#
+CREATE TABLE t1
+(
+id INT PRIMARY KEY AUTO_INCREMENT,
+dt datetime,
+INDEX(dt),
+foo int
+);
+INSERT INTO t1 VALUES (1,'2020-09-26 12:00:00',1);
+INSERT INTO t1 VALUES (2,'2020-09-26 13:00:00',1);
+INSERT INTO t1 VALUES (3,'2020-09-27 13:00:00',1);
+INSERT INTO t1 VALUES (4,'2020-09-27 12:00:00',1);
+INSERT INTO t1 VALUES (5,'2020-09-28 12:00:00',1);
+INSERT INTO t1 VALUES (6,'2020-09-28 13:00:00',1);
+INSERT INTO t1 VALUES (7,'2020-09-25 12:00:00',1);
+INSERT INTO t1 VALUES (8,'2020-09-25 13:00:00',1);
+INSERT INTO t1 VALUES (9,'2020-09-26 13:00:00',1);
+CREATE VIEW v1 AS SELECT * FROM t1;
+CREATE VIEW v2 AS SELECT * FROM t1 ORDER BY dt;
+SELECT dt, sum(foo) AS foo FROM v1 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
+dt foo
+2020-09-25 12:00:00 1
+2020-09-25 13:00:00 1
+2020-09-26 12:00:00 1
+2020-09-26 13:00:00 2
+2020-09-27 12:00:00 1
+2020-09-27 13:00:00 1
+2020-09-28 12:00:00 1
+2020-09-28 13:00:00 1
+SELECT dt, sum(foo) AS foo FROM v2 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
+dt foo
+2020-09-25 12:00:00 1
+2020-09-25 13:00:00 1
+2020-09-26 12:00:00 1
+2020-09-26 13:00:00 2
+2020-09-27 12:00:00 1
+2020-09-27 13:00:00 1
+2020-09-28 12:00:00 1
+2020-09-28 13:00:00 1
+DROP TABLE t1;
+DROP VIEW v1,v2;
+# End of 10.2 tests
diff --git a/mysql-test/t/group_by.test b/mysql-test/t/group_by.test
index 324b41ce23c..2791f672dc1 100644
--- a/mysql-test/t/group_by.test
+++ b/mysql-test/t/group_by.test
@@ -1996,3 +1996,36 @@ ON t.table_schema=c.table_schema
WHERE c.table_schema=(SELECT COUNT(*) FROM INFORMATION_SCHEMA.columns GROUP BY column_type)
GROUP BY t.table_name;
SET max_sort_length= @save_max_sort_length;
+
+--echo #
+--echo # MDEV-23826: ORDER BY in view definition leads to wrong result with GROUP BY on query using view
+--echo #
+
+CREATE TABLE t1
+(
+ id INT PRIMARY KEY AUTO_INCREMENT,
+ dt datetime,
+ INDEX(dt),
+ foo int
+);
+
+INSERT INTO t1 VALUES (1,'2020-09-26 12:00:00',1);
+INSERT INTO t1 VALUES (2,'2020-09-26 13:00:00',1);
+INSERT INTO t1 VALUES (3,'2020-09-27 13:00:00',1);
+INSERT INTO t1 VALUES (4,'2020-09-27 12:00:00',1);
+INSERT INTO t1 VALUES (5,'2020-09-28 12:00:00',1);
+INSERT INTO t1 VALUES (6,'2020-09-28 13:00:00',1);
+INSERT INTO t1 VALUES (7,'2020-09-25 12:00:00',1);
+INSERT INTO t1 VALUES (8,'2020-09-25 13:00:00',1);
+INSERT INTO t1 VALUES (9,'2020-09-26 13:00:00',1);
+
+CREATE VIEW v1 AS SELECT * FROM t1;
+CREATE VIEW v2 AS SELECT * FROM t1 ORDER BY dt;
+SELECT dt, sum(foo) AS foo FROM v1 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
+SELECT dt, sum(foo) AS foo FROM v2 WHERE dt>DATE_SUB('2020-09-27 00:00:00', INTERVAL 3 DAY) GROUP BY dt;
+
+DROP TABLE t1;
+
+DROP VIEW v1,v2;
+
+--echo # End of 10.2 tests
diff --git a/sql/item.cc b/sql/item.cc
index 994d45a9dc3..16db9bb3ebf 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -8896,6 +8896,23 @@ bool Item_direct_view_ref::val_bool_result()
}
+longlong Item_direct_view_ref::val_time_packed()
+{
+ DBUG_ASSERT(fixed);
+ longlong tmp = (*ref)->val_time_packed();
+ null_value= (*ref)->null_value;
+ return tmp;
+}
+
+longlong Item_direct_view_ref::val_datetime_packed()
+{
+ DBUG_ASSERT(fixed);
+ longlong tmp = (*ref)->val_datetime_packed();
+ null_value= (*ref)->null_value;
+ return tmp;
+}
+
+
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
{
return item->type() == DEFAULT_VALUE_ITEM &&
diff --git a/sql/item.h b/sql/item.h
index 4a761bfd70a..a51df45365b 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -5012,6 +5012,8 @@ class Item_direct_view_ref :public Item_direct_ref
String *str_result(String* tmp);
my_decimal *val_decimal_result(my_decimal *val);
bool val_bool_result();
+ longlong val_time_packed();
+ longlong val_datetime_packed();
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_direct_view_ref>(thd, mem_root, this); }
1
0
[Commits] 038b234: MDEV-23778 Derived table handler looses data on conversion from HEAP to Aria
by IgorBabaev 26 Sep '20
by IgorBabaev 26 Sep '20
26 Sep '20
revision-id: 038b2347f003e53fd6a72b213b206835729c5ed2 (mariadb-10.4.11-396-g038b234)
parent(s): 61df98f9642c1616f872d61808ce6d010fe35b44
author: Igor Babaev
committer: Igor Babaev
timestamp: 2020-09-26 10:07:28 -0700
message:
MDEV-23778 Derived table handler looses data on conversion from HEAP to Aria
This bug happened when the HEAP temporary table used for the derived table
created for a derived handler of a remote engine of the federated type
became full and was converted to an Area table. For this conversion
the tmp_table_param parameter must be always taken from the select_unit
object created for the result of the derived table.
---
.../federated/federatedx_create_handlers.result | 44 +++++++++++++++++
.../federated/federatedx_create_handlers.test | 57 ++++++++++++++++++++++
sql/derived_handler.cc | 4 +-
3 files changed, 102 insertions(+), 3 deletions(-)
diff --git a/mysql-test/suite/federated/federatedx_create_handlers.result b/mysql-test/suite/federated/federatedx_create_handlers.result
index 63e56bf..b115cc7 100644
--- a/mysql-test/suite/federated/federatedx_create_handlers.result
+++ b/mysql-test/suite/federated/federatedx_create_handlers.result
@@ -376,6 +376,50 @@ DROP TABLE federated.t1, federated.t2, federated.t3, federated.t4;
connection slave;
DROP TABLE federated.t1, federated.t2;
connection default;
+#
+# MDEV-23778: Derived handler used for big derived tables
+#
+connection slave;
+CREATE TABLE federated.t1 (
+a varchar(100) NOT NULL default '123'
+)
+DEFAULT CHARSET=latin1;
+CREATE TABLE federated.t2 LIKE federated.t1;
+BEGIN NOT ATOMIC
+DECLARE i INT DEFAULT 0;
+START TRANSACTION;
+WHILE i < 70000 DO
+INSERT INTO federated.t1 VALUES (i);
+SET i = i + 1;
+END WHILE;
+COMMIT;
+END
+$$
+connection master;
+CREATE TABLE federated.t1 (
+a varchar(100) NOT NULL default '123'
+)
+ENGINE="FEDERATED" DEFAULT CHARSET=latin1
+CONNECTION='mysql://root@127.0.0.1:SLAVE_PORT/federated/t1';
+CREATE TABLE federated.t2 (
+a varchar(100) NOT NULL default '123'
+)
+ENGINE="FEDERATED" DEFAULT CHARSET=latin1
+CONNECTION='mysql://root@127.0.0.1:SLAVE_PORT/federated/t2';
+SELECT COUNT(DISTINCT a) FROM federated.t1;
+COUNT(DISTINCT a)
+70000
+INSERT INTO federated.t2
+SELECT * FROM (SELECT * FROM federated.t1 LIMIT 100) dt;
+SELECT COUNT(DISTINCT a) FROM federated.t2;
+COUNT(DISTINCT a)
+100
+TRUNCATE TABLE federated.t2;
+INSERT INTO federated.t2
+SELECT * FROM (SELECT * FROM federated.t1 LIMIT 70000) dt;
+SELECT COUNT(DISTINCT a) FROM federated.t2;
+COUNT(DISTINCT a)
+70000
set global federated_pushdown=0;
connection master;
DROP TABLE IF EXISTS federated.t1;
diff --git a/mysql-test/suite/federated/federatedx_create_handlers.test b/mysql-test/suite/federated/federatedx_create_handlers.test
index d765588..4b5789e 100644
--- a/mysql-test/suite/federated/federatedx_create_handlers.test
+++ b/mysql-test/suite/federated/federatedx_create_handlers.test
@@ -208,6 +208,63 @@ DROP TABLE federated.t1, federated.t2;
connection default;
+--echo #
+--echo # MDEV-23778: Derived handler used for big derived tables
+--echo #
+
+connection slave;
+
+CREATE TABLE federated.t1 (
+ a varchar(100) NOT NULL default '123'
+)
+DEFAULT CHARSET=latin1;
+
+CREATE TABLE federated.t2 LIKE federated.t1;
+
+DELIMITER $$;
+BEGIN NOT ATOMIC
+ DECLARE i INT DEFAULT 0;
+ START TRANSACTION;
+ WHILE i < 70000 DO
+ INSERT INTO federated.t1 VALUES (i);
+ SET i = i + 1;
+ END WHILE;
+ COMMIT;
+END
+$$
+
+DELIMITER ;$$
+
+connection master;
+
+--replace_result $SLAVE_MYPORT SLAVE_PORT
+eval
+CREATE TABLE federated.t1 (
+ a varchar(100) NOT NULL default '123'
+)
+ENGINE="FEDERATED" DEFAULT CHARSET=latin1
+CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/federated/t1';
+
+
+--replace_result $SLAVE_MYPORT SLAVE_PORT
+eval
+CREATE TABLE federated.t2 (
+ a varchar(100) NOT NULL default '123'
+)
+ENGINE="FEDERATED" DEFAULT CHARSET=latin1
+CONNECTION='mysql://root@127.0.0.1:$SLAVE_MYPORT/federated/t2';
+
+SELECT COUNT(DISTINCT a) FROM federated.t1;
+
+INSERT INTO federated.t2
+ SELECT * FROM (SELECT * FROM federated.t1 LIMIT 100) dt;
+SELECT COUNT(DISTINCT a) FROM federated.t2;
+
+TRUNCATE TABLE federated.t2;
+INSERT INTO federated.t2
+ SELECT * FROM (SELECT * FROM federated.t1 LIMIT 70000) dt;
+SELECT COUNT(DISTINCT a) FROM federated.t2;
+
set global federated_pushdown=0;
source include/federated_cleanup.inc;
diff --git a/sql/derived_handler.cc b/sql/derived_handler.cc
index 76fd736..f48b95c 100644
--- a/sql/derived_handler.cc
+++ b/sql/derived_handler.cc
@@ -120,8 +120,6 @@ void derived_handler::set_derived(TABLE_LIST *tbl)
table= tbl->table;
unit= tbl->derived;
select= unit->first_select();
- tmp_table_param= select->next_select() ?
- ((select_unit *)(unit->result))->get_tmp_table_param() :
- &select->join->tmp_table_param;
+ tmp_table_param= ((select_unit *)(unit->result))->get_tmp_table_param();
}
1
0