@@ -2337,4 +2337,278 @@ set sql_mode="oracle";
2337
2337
with data as (select 1 as id)
2338
2338
select id into @myid from data;
2339
2339
set sql_mode= @save_sql_mode;
2340
+ #
2341
+ # MDEV-31995 Bogus error executing PS for query using CTE with renaming of columns
2342
+ #
2343
+ create table t1 (a int, b int);
2344
+ insert into t1 values (1,1),(1,2),(1,3),(2,1),(2,2);
2345
+ create table t2 (a int, b int);
2346
+ insert into t2 values (3,1),(3,2),(3,3),(4,1),(4,2);
2347
+ with cte (c1,c2) as
2348
+ (select a as col1, sum(b) as col2 from t1 group by col1)
2349
+ select * from cte;
2350
+ c1 c2
2351
+ 1 6
2352
+ 2 3
2353
+ prepare st from "with cte (c1,c2) as
2354
+ (select a as col1, sum(b) as col2 from t1 group by col1)
2355
+ select * from cte";
2356
+ execute st;
2357
+ c1 c2
2358
+ 1 6
2359
+ 2 3
2360
+ execute st;
2361
+ c1 c2
2362
+ 1 6
2363
+ 2 3
2364
+ drop prepare st;
2365
+ create procedure sp() with cte (c1,c2) as
2366
+ (select a as col1, sum(b) as col2 from t1 group by col1)
2367
+ select * from cte;
2368
+ call sp();
2369
+ c1 c2
2370
+ 1 6
2371
+ 2 3
2372
+ call sp();
2373
+ c1 c2
2374
+ 1 6
2375
+ 2 3
2376
+ drop procedure sp;
2377
+ with cte (c1,c2) as
2378
+ (select a as col1, sum(b) as col2 from t1 order by col1)
2379
+ select * from cte;
2380
+ c1 c2
2381
+ 1 9
2382
+ prepare st from "with cte (c1,c2) as
2383
+ (select a as col1, sum(b) as col2 from t1 order by col1)
2384
+ select * from cte";
2385
+ execute st;
2386
+ c1 c2
2387
+ 1 9
2388
+ execute st;
2389
+ c1 c2
2390
+ 1 9
2391
+ drop prepare st;
2392
+ create procedure sp() with cte (c1,c2) as
2393
+ (select a as col1, sum(b) as col2 from t1 order by col1)
2394
+ select * from cte;
2395
+ call sp();
2396
+ c1 c2
2397
+ 1 9
2398
+ call sp();
2399
+ c1 c2
2400
+ 1 9
2401
+ drop procedure sp;
2402
+ with cte (c1,c2) as
2403
+ (select a as col1, sum(b) as col2 from t1 where a > 1 group by col1
2404
+ union select a as col3, sum(b) as col4 from t2 where b > 2 group by col3),
2405
+ cte2 (c3, c4) as
2406
+ (select a as col5, sum(b) as col6 from t1 where a <= 1 group by col5
2407
+ union select a as col7, sum(b) as col8 from t2 where b <= 2 group by col7)
2408
+ select * from cte where c1=1 union select * from cte2 where c3=3;
2409
+ c1 c2
2410
+ 3 3
2411
+ prepare st from "with cte (c1,c2) as
2412
+ (select a as col1, sum(b) as col2 from t1 where a > 1 group by col1
2413
+ union select a as col3, sum(b) as col4 from t2 where b > 2 group by col3),
2414
+ cte2 (c3, c4) as
2415
+ (select a as col5, sum(b) as col6 from t1 where a <= 1 group by col5
2416
+ union select a as col7, sum(b) as col8 from t2 where b <= 2 group by col7)
2417
+ select * from cte where c1=1 union select * from cte2 where c3=3";
2418
+ execute st;
2419
+ c1 c2
2420
+ 3 3
2421
+ execute st;
2422
+ c1 c2
2423
+ 3 3
2424
+ drop prepare st;
2425
+ create procedure sp() with cte (c1,c2) as
2426
+ (select a as col1, sum(b) as col2 from t1 where a > 1 group by col1
2427
+ union select a as col3, sum(b) as col4 from t2 where b > 2 group by col3),
2428
+ cte2 (c3, c4) as
2429
+ (select a as col5, sum(b) as col6 from t1 where a <= 1 group by col5
2430
+ union select a as col7, sum(b) as col8 from t2 where b <= 2 group by col7)
2431
+ select * from cte where c1=1 union select * from cte2 where c3=3;
2432
+ call sp();
2433
+ c1 c2
2434
+ 3 3
2435
+ call sp();
2436
+ c1 c2
2437
+ 3 3
2438
+ drop procedure sp;
2439
+ with cte (c1,c2) as (select * from t1)
2440
+ select cte.c1+1 as col1 , cte.c2 as col2 from cte where cte.c1 > 1
2441
+ union
2442
+ select cte.c1 as col3, cte.c2+1 as col4 from cte where cte.c1 < 0;
2443
+ col1 col2
2444
+ 3 1
2445
+ 3 2
2446
+ prepare st from "with cte (c1,c2) as (select * from t1)
2447
+ select cte.c1+1 as col1 , cte.c2 as col2 from cte where cte.c1 > 1
2448
+ union
2449
+ select cte.c1 as col3, cte.c2+1 as col4 from cte where cte.c1 < 0";
2450
+ execute st;
2451
+ col1 col2
2452
+ 3 1
2453
+ 3 2
2454
+ execute st;
2455
+ col1 col2
2456
+ 3 1
2457
+ 3 2
2458
+ save this to the end to test errors >drop prepare st;
2459
+ create procedure sp() with cte (c1,c2) as (select * from t1)
2460
+ select cte.c1+1 as col1 , cte.c2 as col2 from cte where cte.c1 > 1
2461
+ union
2462
+ select cte.c1 as col3, cte.c2+1 as col4 from cte where cte.c1 < 0;
2463
+ call sp();
2464
+ col1 col2
2465
+ 3 1
2466
+ 3 2
2467
+ call sp();
2468
+ col1 col2
2469
+ 3 1
2470
+ 3 2
2471
+ drop procedure sp;
2472
+ insert into t1 select * from t2;
2473
+ with cte (c1, c2)
2474
+ as (select a, sum(b) from t1 where b > 1 group by a having sum(b) < 5)
2475
+ select * from cte where c1 < 4 and c2 > 1;
2476
+ c1 c2
2477
+ 2 2
2478
+ # Check pushdown conditions in JSON output
2479
+ explain format=json with cte (c1, c2)
2480
+ as (select a, sum(b) from t1 where b > 1 group by a having sum(b) < 5)
2481
+ select * from cte where c1 < 4 and c2 > 1;
2482
+ EXPLAIN
2483
+ {
2484
+ "query_block": {
2485
+ "select_id": 1,
2486
+ "cost": "REPLACED",
2487
+ "nested_loop": [
2488
+ {
2489
+ "table": {
2490
+ "table_name": "<derived2>",
2491
+ "access_type": "ALL",
2492
+ "loops": 1,
2493
+ "rows": 10,
2494
+ "cost": "REPLACED",
2495
+ "filtered": 100,
2496
+ "attached_condition": "cte.c1 < 4 and cte.c2 > 1",
2497
+ "materialized": {
2498
+ "query_block": {
2499
+ "select_id": 2,
2500
+ "cost": "REPLACED",
2501
+ "having_condition": "sum(t1.b) < 5 and c2 > 1",
2502
+ "filesort": {
2503
+ "sort_key": "t1.a",
2504
+ "temporary_table": {
2505
+ "nested_loop": [
2506
+ {
2507
+ "table": {
2508
+ "table_name": "t1",
2509
+ "access_type": "ALL",
2510
+ "loops": 1,
2511
+ "rows": 10,
2512
+ "cost": "REPLACED",
2513
+ "filtered": 100,
2514
+ "attached_condition": "t1.b > 1 and t1.a < 4"
2515
+ }
2516
+ }
2517
+ ]
2518
+ }
2519
+ }
2520
+ }
2521
+ }
2522
+ }
2523
+ }
2524
+ ]
2525
+ }
2526
+ }
2527
+ alter table t1 add column c int;
2528
+ execute st;
2529
+ ERROR HY000: WITH column list and SELECT field list have different column counts
2530
+ drop prepare st;
2531
+ drop table t1,t2;
2532
+ Test out recursive CTEs
2533
+ create table distances (src char(1), dest char(1), distance int);
2534
+ create table city_population (city char(1), population int);
2535
+ INSERT INTO `distances` VALUES ('A','A',0),('B','A',593),('C','A',800),
2536
+ ('D','A',221),('E','A',707),('F','A',869),('G','A',225),('H','A',519),
2537
+ ('A','B',919),('B','B',0),('C','B',440),('D','B',79),('E','B',79),
2538
+ ('F','B',154),('G','B',537),('H','B',220),('A','C',491),('B','C',794),
2539
+ ('C','C',0),('D','C',100),('E','C',350),('F','C',748),('G','C',712),
2540
+ ('H','C',315),('A','D',440),('B','D',256),('C','D',958),('D','D',0),
2541
+ ('E','D',255),('F','D',161),('G','D',63),('H','D',831),('A','E',968),
2542
+ ('B','E',345),('C','E',823),('D','E',81),('E','E',0),('F','E',436),
2543
+ ('G','E',373),('H','E',558),('A','F',670),('B','F',677),('C','F',375),
2544
+ ('D','F',843),('E','F',90),('F','F',0),('G','F',328),('H','F',881),
2545
+ ('A','G',422),('B','G',467),('C','G',67),('D','G',936),('E','G',480),
2546
+ ('F','G',592),('G','G',0),('H','G',819),('A','H',537),('B','H',229),
2547
+ ('C','H',534),('D','H',984),('E','H',319),('F','H',643),('G','H',257),
2548
+ ('H','H',0);
2549
+ insert into city_population values ('A', 5000), ('B', 6000), ('C', 100000),
2550
+ ('D', 80000), ('E', 7000), ('F', 1000), ('G', 100), ('H', -80000);
2551
+ #find the biggest city within 300 kellikams of 'E'
2552
+ with recursive travel (src, path, dest, distance, population) as (
2553
+ select city, cast('' as varchar(10)), city,
2554
+ 0, population
2555
+ from city_population where city='E'
2556
+ union all
2557
+ select src.src, concat(src.path, dst.dest), dst.dest,
2558
+ src.distance + dst.distance, dstc.population
2559
+ from travel src
2560
+ join distances dst on src.dest != dst.dest
2561
+ join city_population dstc on dst.dest = dstc.city
2562
+ where dst.src = src.dest and src.distance + dst.distance < 300
2563
+ and length(path) < 10
2564
+ )
2565
+ select * from travel where dest != 'E' order by population desc, distance
2566
+ limit 1;
2567
+ src path dest distance population
2568
+ E FD D 251 80000
2569
+ prepare st from "with recursive travel (src, path, dest, distance, population) as (
2570
+ select city, cast('' as varchar(10)), city,
2571
+ 0, population
2572
+ from city_population where city='E'
2573
+ union all
2574
+ select src.src, concat(src.path, dst.dest), dst.dest,
2575
+ src.distance + dst.distance, dstc.population
2576
+ from travel src
2577
+ join distances dst on src.dest != dst.dest
2578
+ join city_population dstc on dst.dest = dstc.city
2579
+ where dst.src = src.dest and src.distance + dst.distance < 300
2580
+ and length(path) < 10
2581
+ )
2582
+ select * from travel where dest != 'E' order by population desc, distance
2583
+ limit 1";
2584
+ execute st;
2585
+ src path dest distance population
2586
+ E FD D 251 80000
2587
+ execute st;
2588
+ src path dest distance population
2589
+ E FD D 251 80000
2590
+ drop prepare st;
2591
+ create procedure sp() with recursive travel (src, path, dest, distance, population) as (
2592
+ select city, cast('' as varchar(10)), city,
2593
+ 0, population
2594
+ from city_population where city='E'
2595
+ union all
2596
+ select src.src, concat(src.path, dst.dest), dst.dest,
2597
+ src.distance + dst.distance, dstc.population
2598
+ from travel src
2599
+ join distances dst on src.dest != dst.dest
2600
+ join city_population dstc on dst.dest = dstc.city
2601
+ where dst.src = src.dest and src.distance + dst.distance < 300
2602
+ and length(path) < 10
2603
+ )
2604
+ select * from travel where dest != 'E' order by population desc, distance
2605
+ limit 1;
2606
+ call sp();
2607
+ src path dest distance population
2608
+ E FD D 251 80000
2609
+ call sp();
2610
+ src path dest distance population
2611
+ E FD D 251 80000
2612
+ drop procedure sp;
2613
+ drop table distances, city_population;
2340
2614
# End of 10.4 tests
0 commit comments