1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
|
import pickle
import re
from unittest import mock
from sqlalchemy import and_
from sqlalchemy import bindparam
from sqlalchemy import case
from sqlalchemy import Column
from sqlalchemy import exc
from sqlalchemy import extract
from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy import Integer
from sqlalchemy import join
from sqlalchemy import literal
from sqlalchemy import literal_column
from sqlalchemy import MetaData
from sqlalchemy import null
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy import true
from sqlalchemy import tuple_
from sqlalchemy import union
from sqlalchemy.sql import ClauseElement
from sqlalchemy.sql import column
from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL
from sqlalchemy.sql import operators
from sqlalchemy.sql import table
from sqlalchemy.sql import util as sql_util
from sqlalchemy.sql import visitors
from sqlalchemy.sql.elements import _clone
from sqlalchemy.sql.expression import _from_objects
from sqlalchemy.sql.util import _deep_annotate
from sqlalchemy.sql.visitors import ClauseVisitor
from sqlalchemy.sql.visitors import cloned_traverse
from sqlalchemy.sql.visitors import CloningVisitor
from sqlalchemy.sql.visitors import ReplacingCloningVisitor
from sqlalchemy.testing import assert_raises
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import AssertsExecutionResults
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing import is_not
from sqlalchemy.testing.schema import eq_clause_element
A = B = t1 = t2 = t3 = table1 = table2 = table3 = table4 = None
class TraversalTest(
fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL
):
"""test ClauseVisitor's traversal, particularly its
ability to copy and modify a ClauseElement in place."""
@classmethod
def setup_test_class(cls):
global A, B
# establish two fictitious ClauseElements.
# define deep equality semantics as well as deep
# identity semantics.
class A(ClauseElement):
__visit_name__ = "a"
_traverse_internals = []
def __init__(self, expr):
self.expr = expr
def is_other(self, other):
return other is self
__hash__ = ClauseElement.__hash__
def __eq__(self, other):
return other.expr == self.expr
def __ne__(self, other):
return other.expr != self.expr
def __str__(self):
return "A(%s)" % repr(self.expr)
class B(ClauseElement):
__visit_name__ = "b"
def __init__(self, *items):
self.items = items
def is_other(self, other):
if other is not self:
return False
for i1, i2 in zip(self.items, other.items):
if i1 is not i2:
return False
return True
__hash__ = ClauseElement.__hash__
def __eq__(self, other):
for i1, i2 in zip(self.items, other.items):
if i1 != i2:
return False
return True
def __ne__(self, other):
for i1, i2 in zip(self.items, other.items):
if i1 != i2:
return True
return False
def _copy_internals(self, clone=_clone, **kw):
self.items = [clone(i, **kw) for i in self.items]
def get_children(self, **kwargs):
return self.items
def __str__(self):
return "B(%s)" % repr([str(i) for i in self.items])
def test_test_classes(self):
a1 = A("expr1")
struct = B(a1, A("expr2"), B(A("expr1b"), A("expr2b")), A("expr3"))
struct2 = B(a1, A("expr2"), B(A("expr1b"), A("expr2b")), A("expr3"))
struct3 = B(
a1, A("expr2"), B(A("expr1b"), A("expr2bmodified")), A("expr3")
)
assert a1.is_other(a1)
assert struct.is_other(struct)
assert struct == struct2
assert struct != struct3
assert not struct.is_other(struct2)
assert not struct.is_other(struct3)
def test_clone(self):
struct = B(
A("expr1"), A("expr2"), B(A("expr1b"), A("expr2b")), A("expr3")
)
class Vis(CloningVisitor):
def visit_a(self, a):
pass
def visit_b(self, b):
pass
vis = Vis()
s2 = vis.traverse(struct)
assert struct == s2
assert not struct.is_other(s2)
def test_no_clone(self):
struct = B(
A("expr1"), A("expr2"), B(A("expr1b"), A("expr2b")), A("expr3")
)
class Vis(ClauseVisitor):
def visit_a(self, a):
pass
def visit_b(self, b):
pass
vis = Vis()
s2 = vis.traverse(struct)
assert struct == s2
assert struct.is_other(s2)
def test_clone_anon_label(self):
from sqlalchemy.sql.elements import Grouping
c1 = Grouping(literal_column("q"))
s1 = select(c1)
class Vis(CloningVisitor):
def visit_grouping(self, elem):
pass
vis = Vis()
s2 = vis.traverse(s1)
eq_(list(s2.selected_columns)[0]._anon_name_label, c1._anon_name_label)
@testing.combinations(
("clone",), ("pickle",), ("conv_to_unique"), ("none"), argnames="meth"
)
@testing.combinations(
("name with space",),
("name with [brackets]",),
("name with~~tildes~~",),
argnames="name",
)
@testing.combinations(True, False, argnames="positional")
def test_bindparam_key_proc_for_copies(self, meth, name, positional):
r"""test :ticket:`6249`.
Revised for :ticket:`8056`.
The key of the bindparam needs spaces and other characters
escaped out for the POSTCOMPILE regex to work correctly.
Currently, the bind key reg is::
re.sub(r"[%\(\) \$\[\]]", "_", name)
and the compiler postcompile reg is::
re.sub(r"\__[POSTCOMPILE_(\S+)\]", process_expanding, self.string)
Interestingly, brackets in the name seems to work out.
"""
expr = column(name).in_([1, 2, 3])
if meth == "clone":
expr = visitors.cloned_traverse(expr, {}, {})
elif meth == "pickle":
expr = pickle.loads(pickle.dumps(expr))
elif meth == "conv_to_unique":
expr.right.unique = False
expr.right._convert_to_unique()
token = re.sub(r"[%\(\) \$\[\]]", "_", name)
if positional:
self.assert_compile(
expr,
'"%(name)s" IN (?, ?, ?)' % {"name": name},
checkpositional=(1, 2, 3),
render_postcompile=True,
dialect="default_qmark",
)
else:
tokens = ["%s_1_%s" % (token, i) for i in range(1, 4)]
self.assert_compile(
expr,
'"%(name)s" IN (:%(token)s_1_1, '
":%(token)s_1_2, :%(token)s_1_3)"
% {"name": name, "token": token},
checkparams=dict(zip(tokens, [1, 2, 3])),
render_postcompile=True,
dialect="default",
)
def test_expanding_in_bindparam_safe_to_clone(self):
expr = column("x").in_([1, 2, 3])
expr2 = expr._clone()
# shallow copy, bind is used twice
is_(expr.right, expr2.right)
stmt = and_(expr, expr2)
self.assert_compile(
stmt, "x IN (__[POSTCOMPILE_x_1]) AND x IN (__[POSTCOMPILE_x_1])"
)
self.assert_compile(
stmt, "x IN (1, 2, 3) AND x IN (1, 2, 3)", literal_binds=True
)
def test_traversal_size(self):
"""Test :ticket:`6304`.
Testing that _iterate_from_elements returns only unique FROM
clauses; overall traversal should be short and all items unique.
"""
t = table("t", *[column(x) for x in "pqrxyz"])
s1 = select(t.c.p, t.c.q, t.c.r, t.c.x, t.c.y, t.c.z).subquery()
s2 = (
select(s1.c.p, s1.c.q, s1.c.r, s1.c.x, s1.c.y, s1.c.z)
.select_from(s1)
.subquery()
)
s3 = (
select(s2.c.p, s2.c.q, s2.c.r, s2.c.x, s2.c.y, s2.c.z)
.select_from(s2)
.subquery()
)
tt = list(s3.element._iterate_from_elements())
eq_(tt, [s2])
total = list(visitors.iterate(s3))
# before the bug was fixed, this was 750
eq_(len(total), 25)
seen = set()
for elem in visitors.iterate(s3):
assert elem not in seen
seen.add(elem)
eq_(len(seen), 25)
def test_change_in_place(self):
struct = B(
A("expr1"), A("expr2"), B(A("expr1b"), A("expr2b")), A("expr3")
)
struct2 = B(
A("expr1"),
A("expr2modified"),
B(A("expr1b"), A("expr2b")),
A("expr3"),
)
struct3 = B(
A("expr1"),
A("expr2"),
B(A("expr1b"), A("expr2bmodified")),
A("expr3"),
)
class Vis(CloningVisitor):
def visit_a(self, a):
if a.expr == "expr2":
a.expr = "expr2modified"
def visit_b(self, b):
pass
vis = Vis()
s2 = vis.traverse(struct)
assert struct != s2
assert not struct.is_other(s2)
assert struct2 == s2
class Vis2(CloningVisitor):
def visit_a(self, a):
if a.expr == "expr2b":
a.expr = "expr2bmodified"
def visit_b(self, b):
pass
vis2 = Vis2()
s3 = vis2.traverse(struct)
assert struct != s3
assert struct3 == s3
def test_visit_name(self):
# override fns in testlib/schema.py
from sqlalchemy import Column
class CustomObj(Column):
pass
assert CustomObj.__visit_name__ == Column.__visit_name__ == "column"
foo, bar = CustomObj("foo", String), CustomObj("bar", String)
bin_ = foo == bar
set(ClauseVisitor().iterate(bin_))
assert set(ClauseVisitor().iterate(bin_)) == {foo, bar, bin_}
class BinaryEndpointTraversalTest(fixtures.TestBase):
"""test the special binary product visit"""
def _assert_traversal(self, expr, expected):
canary = []
def visit(binary, l, r):
canary.append((binary.operator, l, r))
print(binary.operator, l, r)
sql_util.visit_binary_product(visit, expr)
eq_(canary, expected)
def test_basic(self):
a, b = column("a"), column("b")
self._assert_traversal(a == b, [(operators.eq, a, b)])
def test_with_tuples(self):
a, b, c, d, b1, b1a, b1b, e, f = (
column("a"),
column("b"),
column("c"),
column("d"),
column("b1"),
column("b1a"),
column("b1b"),
column("e"),
column("f"),
)
expr = tuple_(a, b, b1 == tuple_(b1a, b1b == d), c) > tuple_(
func.go(e + f)
)
self._assert_traversal(
expr,
[
(operators.gt, a, e),
(operators.gt, a, f),
(operators.gt, b, e),
(operators.gt, b, f),
(operators.eq, b1, b1a),
(operators.eq, b1b, d),
(operators.gt, c, e),
(operators.gt, c, f),
],
)
def test_composed(self):
a, b, e, f, q, j, r = (
column("a"),
column("b"),
column("e"),
column("f"),
column("q"),
column("j"),
column("r"),
)
expr = and_((a + b) == q + func.sum(e + f), and_(j == r, f == q))
self._assert_traversal(
expr,
[
(operators.eq, a, q),
(operators.eq, a, e),
(operators.eq, a, f),
(operators.eq, b, q),
(operators.eq, b, e),
(operators.eq, b, f),
(operators.eq, j, r),
(operators.eq, f, q),
],
)
def test_subquery(self):
a, b, c = column("a"), column("b"), column("c")
subq = select(c).where(c == a).scalar_subquery()
expr = and_(a == b, b == subq)
self._assert_traversal(
expr, [(operators.eq, a, b), (operators.eq, b, subq)]
)
class ClauseTest(fixtures.TestBase, AssertsCompiledSQL):
"""test copy-in-place behavior of various ClauseElements."""
__dialect__ = "default"
@classmethod
def setup_test_class(cls):
global t1, t2, t3
t1 = table("table1", column("col1"), column("col2"), column("col3"))
t2 = table("table2", column("col1"), column("col2"), column("col3"))
t3 = Table(
"table3",
MetaData(),
Column("col1", Integer),
Column("col2", Integer),
)
def test_binary(self):
clause = t1.c.col2 == t2.c.col2
eq_(str(clause), str(CloningVisitor().traverse(clause)))
def test_binary_anon_label_quirk(self):
t = table("t1", column("col1"))
f = t.c.col1 * 5
self.assert_compile(
select(f), "SELECT t1.col1 * :col1_1 AS anon_1 FROM t1"
)
f._anon_name_label
a = t.alias()
f = sql_util.ClauseAdapter(a).traverse(f)
self.assert_compile(
select(f), "SELECT t1_1.col1 * :col1_1 AS anon_1 FROM t1 AS t1_1"
)
@testing.combinations(
(lambda t1: t1.c.col1, "t1_1.col1"),
(lambda t1: t1.c.col1 == "foo", "t1_1.col1 = :col1_1"),
(
lambda t1: case((t1.c.col1 == "foo", "bar"), else_=t1.c.col1),
"CASE WHEN (t1_1.col1 = :col1_1) THEN :param_1 ELSE t1_1.col1 END",
),
argnames="case, expected",
)
@testing.combinations(False, True, argnames="label_")
@testing.combinations(False, True, argnames="annotate")
def test_annotated_label_cases(self, case, expected, label_, annotate):
"""test #6550"""
t1 = table("t1", column("col1"))
a1 = t1.alias()
expr = case(t1=t1)
if label_:
expr = expr.label(None)
if annotate:
expr = expr._annotate({"foo": "bar"})
adapted = sql_util.ClauseAdapter(a1).traverse(expr)
self.assert_compile(adapted, expected)
@testing.variation("annotate", [True, False])
def test_bindparam_render_literal_execute(self, annotate):
"""test #9526"""
bp = bindparam("some_value")
if annotate:
bp = bp._annotate({"foo": "bar"})
bp = bp.render_literal_execute()
self.assert_compile(
column("q") == bp, "q = __[POSTCOMPILE_some_value]"
)
@testing.variation("limit_type", ["limit", "fetch"])
@testing.variation("dialect", ["default", "oracle"])
def test_annotated_fetch(self, limit_type: testing.Variation, dialect):
"""test #9526"""
if limit_type.limit:
stmt = select(column("q")).limit(1)
elif limit_type.fetch:
stmt = select(column("q")).fetch(1)
else:
limit_type.fail()
stmt = _deep_annotate(stmt, {"foo": "bar"})
if limit_type.limit:
if dialect.default:
self.assert_compile(
stmt,
"SELECT q LIMIT :param_1",
use_literal_execute_for_simple_int=True,
dialect=dialect.name,
)
elif dialect.oracle:
self.assert_compile(
stmt,
"SELECT q FROM DUAL FETCH FIRST "
"__[POSTCOMPILE_param_1] ROWS ONLY",
dialect=dialect.name,
)
else:
dialect.fail()
elif limit_type.fetch:
if dialect.default:
self.assert_compile(
stmt,
"SELECT q FETCH FIRST __[POSTCOMPILE_param_1] ROWS ONLY",
use_literal_execute_for_simple_int=True,
dialect=dialect.name,
)
elif dialect.oracle:
self.assert_compile(
stmt,
"SELECT q FROM DUAL FETCH FIRST "
"__[POSTCOMPILE_param_1] ROWS ONLY",
dialect=dialect.name,
)
else:
dialect.fail()
else:
limit_type.fail()
@testing.combinations((null(),), (true(),))
def test_dont_adapt_singleton_elements(self, elem):
"""test :ticket:`6259`"""
t1 = table("t1", column("c1"))
stmt = select(t1.c.c1, elem)
wherecond = t1.c.c1.is_(elem)
subq = stmt.subquery()
adapted_wherecond = sql_util.ClauseAdapter(subq).traverse(wherecond)
stmt = select(subq).where(adapted_wherecond)
self.assert_compile(
stmt,
"SELECT anon_1.c1, anon_1.anon_2 FROM (SELECT t1.c1 AS c1, "
"%s AS anon_2 FROM t1) AS anon_1 WHERE anon_1.c1 IS %s"
% (str(elem), str(elem)),
dialect="default_enhanced",
)
def test_adapt_funcs_etc_on_identity_one(self):
"""Adapting to a function etc. will adapt if its on identity"""
t1 = table("t1", column("c1"))
elem = func.foobar()
stmt = select(t1.c.c1, elem)
wherecond = t1.c.c1 == elem
subq = stmt.subquery()
adapted_wherecond = sql_util.ClauseAdapter(subq).traverse(wherecond)
stmt = select(subq).where(adapted_wherecond)
self.assert_compile(
stmt,
"SELECT anon_1.c1, anon_1.foobar_1 FROM (SELECT t1.c1 AS c1, "
"foobar() AS foobar_1 FROM t1) AS anon_1 "
"WHERE anon_1.c1 = anon_1.foobar_1",
dialect="default_enhanced",
)
def test_adapt_funcs_etc_on_identity_two(self):
"""Adapting to a function etc. will not adapt if they are different"""
t1 = table("t1", column("c1"))
elem = func.foobar()
elem2 = func.foobar()
stmt = select(t1.c.c1, elem)
wherecond = t1.c.c1 == elem2
subq = stmt.subquery()
adapted_wherecond = sql_util.ClauseAdapter(subq).traverse(wherecond)
stmt = select(subq).where(adapted_wherecond)
self.assert_compile(
stmt,
"SELECT anon_1.c1, anon_1.foobar_1 FROM (SELECT t1.c1 AS c1, "
"foobar() AS foobar_1 FROM t1) AS anon_1 "
"WHERE anon_1.c1 = foobar()",
dialect="default_enhanced",
)
def test_join(self):
clause = t1.join(t2, t1.c.col2 == t2.c.col2)
c1 = str(clause)
assert str(clause) == str(CloningVisitor().traverse(clause))
class Vis(CloningVisitor):
def visit_binary(self, binary):
binary.right = t2.c.col3
clause2 = Vis().traverse(clause)
assert c1 == str(clause)
assert str(clause2) == str(t1.join(t2, t1.c.col2 == t2.c.col3))
def test_aliased_column_adapt(self):
t1.select()
aliased = t1.select().alias()
aliased2 = t1.alias()
adapter = sql_util.ColumnAdapter(aliased)
f = select(*[adapter.columns[c] for c in aliased2.c]).select_from(
aliased
)
s = select(aliased2).select_from(aliased)
eq_(str(s), str(f))
f = select(adapter.columns[func.count(aliased2.c.col1)]).select_from(
aliased
)
eq_(
str(select(func.count(aliased2.c.col1)).select_from(aliased)),
str(f),
)
def test_aliased_cloned_column_adapt_inner(self):
clause = select(t1.c.col1, func.foo(t1.c.col2).label("foo"))
c_sub = clause.subquery()
aliased1 = select(c_sub.c.col1, c_sub.c.foo).subquery()
aliased2 = clause
aliased2.selected_columns.col1, aliased2.selected_columns.foo
aliased3 = cloned_traverse(aliased2, {}, {})
# fixed by [ticket:2419]. the inside columns
# on aliased3 have _is_clone_of pointers to those of
# aliased2. corresponding_column checks these
# now.
adapter = sql_util.ColumnAdapter(aliased1)
f1 = select(*[adapter.columns[c] for c in aliased2._raw_columns])
f2 = select(*[adapter.columns[c] for c in aliased3._raw_columns])
eq_(str(f1), str(f2))
def test_aliased_cloned_column_adapt_exported(self):
clause = select(t1.c.col1, func.foo(t1.c.col2).label("foo")).subquery()
aliased1 = select(clause.c.col1, clause.c.foo).subquery()
aliased2 = clause
aliased2.c.col1, aliased2.c.foo
aliased3 = cloned_traverse(aliased2, {}, {})
# also fixed by [ticket:2419]. When we look at the
# *outside* columns of aliased3, they previously did not
# have an _is_clone_of pointer. But we now modified _make_proxy
# to assign this.
adapter = sql_util.ColumnAdapter(aliased1)
f1 = select(*[adapter.columns[c] for c in aliased2.c])
f2 = select(*[adapter.columns[c] for c in aliased3.c])
eq_(str(f1), str(f2))
def test_aliased_cloned_schema_column_adapt_exported(self):
clause = select(t3.c.col1, func.foo(t3.c.col2).label("foo")).subquery()
aliased1 = select(clause.c.col1, clause.c.foo).subquery()
aliased2 = clause
aliased2.c.col1, aliased2.c.foo
aliased3 = cloned_traverse(aliased2, {}, {})
# also fixed by [ticket:2419]. When we look at the
# *outside* columns of aliased3, they previously did not
# have an _is_clone_of pointer. But we now modified _make_proxy
# to assign this.
adapter = sql_util.ColumnAdapter(aliased1)
f1 = select(*[adapter.columns[c] for c in aliased2.c])
f2 = select(*[adapter.columns[c] for c in aliased3.c])
eq_(str(f1), str(f2))
def test_labeled_expression_adapt(self):
lbl_x = (t3.c.col1 == 1).label("x")
t3_alias = t3.alias()
adapter = sql_util.ColumnAdapter(t3_alias)
lblx_adapted = adapter.traverse(lbl_x)
is_not(lblx_adapted._element, lbl_x._element)
lblx_adapted = adapter.traverse(lbl_x)
self.assert_compile(
select(lblx_adapted.self_group()),
"SELECT (table3_1.col1 = :col1_1) AS x FROM table3 AS table3_1",
)
self.assert_compile(
select(lblx_adapted.is_(True)),
"SELECT (table3_1.col1 = :col1_1) IS 1 AS anon_1 "
"FROM table3 AS table3_1",
)
def test_cte_w_union(self):
t = select(func.values(1).label("n")).cte("t", recursive=True)
t = t.union_all(select(t.c.n + 1).where(t.c.n < 100))
s = select(func.sum(t.c.n))
from sqlalchemy.sql.visitors import cloned_traverse
cloned = cloned_traverse(s, {}, {})
self.assert_compile(
cloned,
"WITH RECURSIVE t(n) AS "
"(SELECT values(:values_1) AS n "
"UNION ALL SELECT t.n + :n_1 AS anon_1 "
"FROM t "
"WHERE t.n < :n_2) "
"SELECT sum(t.n) AS sum_1 FROM t",
)
def test_aliased_cte_w_union(self):
t = (
select(func.values(1).label("n"))
.cte("t", recursive=True)
.alias("foo")
)
t = t.union_all(select(t.c.n + 1).where(t.c.n < 100))
s = select(func.sum(t.c.n))
from sqlalchemy.sql.visitors import cloned_traverse
cloned = cloned_traverse(s, {}, {})
self.assert_compile(
cloned,
"WITH RECURSIVE foo(n) AS (SELECT values(:values_1) AS n "
"UNION ALL SELECT foo.n + :n_1 AS anon_1 FROM foo "
"WHERE foo.n < :n_2) SELECT sum(foo.n) AS sum_1 FROM foo",
)
def test_text(self):
clause = text("select * from table where foo=:bar").bindparams(
bindparam("bar")
)
c1 = str(clause)
class Vis(CloningVisitor):
def visit_textclause(self, text):
text.text = text.text + " SOME MODIFIER=:lala"
text._bindparams["lala"] = bindparam("lala")
clause2 = Vis().traverse(clause)
assert c1 == str(clause)
assert str(clause2) == c1 + " SOME MODIFIER=:lala"
assert list(clause._bindparams.keys()) == ["bar"]
assert set(clause2._bindparams.keys()) == {"bar", "lala"}
def test_select(self):
s2 = select(t1)
s2_assert = str(s2)
s3_assert = str(select(t1).where(t1.c.col2 == 7))
class Vis(CloningVisitor):
def visit_select(self, select):
select.where.non_generative(select, t1.c.col2 == 7)
s3 = Vis().traverse(s2)
assert str(s3) == s3_assert
assert str(s2) == s2_assert
print(str(s2))
print(str(s3))
class Vis(ClauseVisitor):
def visit_select(self, select):
select.where.non_generative(select, t1.c.col2 == 7)
Vis().traverse(s2)
assert str(s2) == s3_assert
s4_assert = str(select(t1).where(and_(t1.c.col2 == 7, t1.c.col3 == 9)))
class Vis(CloningVisitor):
def visit_select(self, select):
select.where.non_generative(select, t1.c.col3 == 9)
s4 = Vis().traverse(s3)
print(str(s3))
print(str(s4))
assert str(s4) == s4_assert
assert str(s3) == s3_assert
s5_assert = str(select(t1).where(and_(t1.c.col2 == 7, t1.c.col1 == 9)))
class Vis(CloningVisitor):
def visit_binary(self, binary):
if binary.left is t1.c.col3:
binary.left = t1.c.col1
binary.right = bindparam("col1", unique=True)
s5 = Vis().traverse(s4)
print(str(s4))
print(str(s5))
assert str(s5) == s5_assert
assert str(s4) == s4_assert
def test_union(self):
u = union(t1.select(), t2.select())
u2 = CloningVisitor().traverse(u)
eq_(str(u), str(u2))
eq_(
[str(c) for c in u2.selected_columns],
[str(c) for c in u.selected_columns],
)
u = union(t1.select(), t2.select())
cols = [str(c) for c in u.selected_columns]
u2 = CloningVisitor().traverse(u)
eq_(str(u), str(u2))
eq_([str(c) for c in u2.selected_columns], cols)
s1 = select(t1).where(t1.c.col1 == bindparam("id_param"))
s2 = select(t2)
u = union(s1, s2)
u2 = u.params(id_param=7)
u3 = u.params(id_param=10)
eq_(str(u), str(u2))
eq_(str(u2), str(u3))
eq_(u2.compile().params, {"id_param": 7})
eq_(u3.compile().params, {"id_param": 10})
def test_params_elements_in_setup_joins(self):
"""test #7055"""
meta = MetaData()
X = Table("x", meta, Column("a", Integer), Column("b", Integer))
Y = Table("y", meta, Column("a", Integer), Column("b", Integer))
s1 = select(X.c.a).where(X.c.b == bindparam("xb")).alias("s1")
jj = (
select(Y)
.join(s1, Y.c.a == s1.c.a)
.where(Y.c.b == bindparam("yb"))
.alias("s2")
)
params = {"xb": 42, "yb": 33}
sel = select(Y).select_from(jj).params(params)
eq_(
[
eq_clause_element(bindparam("yb", value=33)),
eq_clause_element(bindparam("xb", value=42)),
],
sel._generate_cache_key()[1],
)
def test_dont_traverse_immutables(self):
meta = MetaData()
b = Table("b", meta, Column("id", Integer), Column("data", String))
subq = select(b.c.id).where(b.c.data == "some data").subquery()
check = mock.Mock()
class Vis(dict):
def get(self, key, default=None):
return getattr(check, key)
def __missing__(self, key):
return getattr(check, key)
visitors.cloned_traverse(subq, {}, Vis())
eq_(
check.mock_calls,
[
mock.call.bindparam(mock.ANY),
mock.call.binary(mock.ANY),
mock.call.select(mock.ANY),
mock.call.subquery(mock.ANY),
],
)
def test_params_on_expr_against_subquery(self):
"""test #7489"""
meta = MetaData()
b = Table("b", meta, Column("id", Integer), Column("data", String))
subq = select(b.c.id).where(b.c.data == "some data").subquery()
criteria = b.c.id == subq.c.id
stmt = select(b).where(criteria)
param_key = stmt._generate_cache_key()[1][0].key
self.assert_compile(
stmt,
"SELECT b.id, b.data FROM b, (SELECT b.id AS id "
"FROM b WHERE b.data = :data_1) AS anon_1 WHERE b.id = anon_1.id",
checkparams={"data_1": "some data"},
)
eq_(
[
eq_clause_element(bindparam(param_key, value="some data")),
],
stmt._generate_cache_key()[1],
)
stmt = select(b).where(criteria.params({param_key: "some other data"}))
self.assert_compile(
stmt,
"SELECT b.id, b.data FROM b, (SELECT b.id AS id "
"FROM b WHERE b.data = :data_1) AS anon_1 WHERE b.id = anon_1.id",
checkparams={"data_1": "some other data"},
)
eq_(
[
eq_clause_element(
bindparam(param_key, value="some other data")
),
],
stmt._generate_cache_key()[1],
)
def test_params_subqueries_in_joins_one(self):
"""test #7055"""
meta = MetaData()
Pe = Table(
"pe",
meta,
Column("c", Integer),
Column("p", Integer),
Column("pid", Integer),
)
S = Table(
"s",
meta,
Column("c", Integer),
Column("p", Integer),
Column("sid", Integer),
)
Ps = Table("ps", meta, Column("c", Integer), Column("p", Integer))
params = {"pid": 42, "sid": 33}
pe_s = select(Pe).where(Pe.c.pid == bindparam("pid")).alias("pe_s")
s_s = select(S).where(S.c.sid == bindparam("sid")).alias("s_s")
jj = join(
Ps,
join(pe_s, s_s, and_(pe_s.c.c == s_s.c.c, pe_s.c.p == s_s.c.p)),
and_(Ps.c.c == pe_s.c.c, Ps.c.p == Ps.c.p),
).params(params)
eq_(
[
eq_clause_element(bindparam("pid", value=42)),
eq_clause_element(bindparam("sid", value=33)),
],
jj._generate_cache_key()[1],
)
def test_params_subqueries_in_joins_two(self):
"""test #7055"""
meta = MetaData()
Pe = Table(
"pe",
meta,
Column("c", Integer),
Column("p", Integer),
Column("pid", Integer),
)
S = Table(
"s",
meta,
Column("c", Integer),
Column("p", Integer),
Column("sid", Integer),
)
Ps = Table("ps", meta, Column("c", Integer), Column("p", Integer))
params = {"pid": 42, "sid": 33}
pe_s = select(Pe).where(Pe.c.pid == bindparam("pid")).alias("pe_s")
s_s = select(S).where(S.c.sid == bindparam("sid")).alias("s_s")
jj = (
join(Ps, pe_s, and_(Ps.c.c == pe_s.c.c, Ps.c.p == Ps.c.p))
.join(s_s, and_(Ps.c.c == s_s.c.c, Ps.c.p == s_s.c.p))
.params(params)
)
eq_(
[
eq_clause_element(bindparam("pid", value=42)),
eq_clause_element(bindparam("sid", value=33)),
],
jj._generate_cache_key()[1],
)
def test_in(self):
expr = t1.c.col1.in_(["foo", "bar"])
expr2 = CloningVisitor().traverse(expr)
assert str(expr) == str(expr2)
def test_over(self):
expr = func.row_number().over(order_by=t1.c.col1)
expr2 = CloningVisitor().traverse(expr)
assert str(expr) == str(expr2)
assert expr in visitors.iterate(expr, {})
def test_within_group(self):
expr = func.row_number().within_group(t1.c.col1)
expr2 = CloningVisitor().traverse(expr)
assert str(expr) == str(expr2)
assert expr in visitors.iterate(expr, {})
def test_funcfilter(self):
expr = func.count(1).filter(t1.c.col1 > 1)
expr2 = CloningVisitor().traverse(expr)
assert str(expr) == str(expr2)
def test_adapt_union(self):
u = union(
t1.select().where(t1.c.col1 == 4),
t1.select().where(t1.c.col1 == 5),
).alias()
assert sql_util.ClauseAdapter(u).traverse(t1) is u
def test_bindparams(self):
"""test that unique bindparams change their name upon clone()
to prevent conflicts"""
s = select(t1).where(t1.c.col1 == bindparam(None, unique=True)).alias()
s2 = CloningVisitor().traverse(s).alias()
s3 = select(s).where(s.c.col2 == s2.c.col2)
self.assert_compile(
s3,
"SELECT anon_1.col1, anon_1.col2, anon_1.col3 FROM "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 WHERE table1.col1 = :param_1) "
"AS anon_1, "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, table1.col3 "
"AS col3 FROM table1 WHERE table1.col1 = :param_2) AS anon_2 "
"WHERE anon_1.col2 = anon_2.col2",
)
s = select(t1).where(t1.c.col1 == 4).alias()
s2 = CloningVisitor().traverse(s).alias()
s3 = select(s).where(s.c.col2 == s2.c.col2)
self.assert_compile(
s3,
"SELECT anon_1.col1, anon_1.col2, anon_1.col3 FROM "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 WHERE table1.col1 = :col1_1) "
"AS anon_1, "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, table1.col3 "
"AS col3 FROM table1 WHERE table1.col1 = :col1_2) AS anon_2 "
"WHERE anon_1.col2 = anon_2.col2",
)
def test_extract(self):
s = select(extract("foo", t1.c.col1).label("col1"))
self.assert_compile(
s, "SELECT EXTRACT(foo FROM table1.col1) AS col1 FROM table1"
)
s2 = CloningVisitor().traverse(s).alias()
s3 = select(s2.c.col1)
self.assert_compile(
s, "SELECT EXTRACT(foo FROM table1.col1) AS col1 FROM table1"
)
self.assert_compile(
s3,
"SELECT anon_1.col1 FROM (SELECT EXTRACT(foo FROM "
"table1.col1) AS col1 FROM table1) AS anon_1",
)
@testing.emits_warning(".*replaced by another column with the same key")
def test_alias(self):
subq = t2.select().alias("subq")
s = select(t1.c.col1, subq.c.col1).select_from(
t1, subq, t1.join(subq, t1.c.col1 == subq.c.col2)
)
orig = str(s)
s2 = CloningVisitor().traverse(s)
eq_(orig, str(s))
eq_(str(s), str(s2))
s4 = CloningVisitor().traverse(s2)
eq_(orig, str(s))
eq_(str(s), str(s2))
eq_(str(s), str(s4))
s3 = sql_util.ClauseAdapter(table("foo")).traverse(s)
eq_(orig, str(s))
eq_(str(s), str(s3))
s4 = sql_util.ClauseAdapter(table("foo")).traverse(s3)
eq_(orig, str(s))
eq_(str(s), str(s3))
eq_(str(s), str(s4))
subq = subq.alias("subq")
s = select(t1.c.col1, subq.c.col1).select_from(
t1,
subq,
t1.join(subq, t1.c.col1 == subq.c.col2),
)
s5 = CloningVisitor().traverse(s)
eq_(str(s), str(s5))
def test_correlated_select(self):
s = (
select(literal_column("*"))
.where(t1.c.col1 == t2.c.col1)
.select_from(t1, t2)
.correlate(t2)
)
class Vis(CloningVisitor):
def visit_select(self, select):
select.where.non_generative(select, t1.c.col2 == 7)
self.assert_compile(
select(t2).where(t2.c.col1 == Vis().traverse(s).scalar_subquery()),
"SELECT table2.col1, table2.col2, table2.col3 "
"FROM table2 WHERE table2.col1 = "
"(SELECT * FROM table1 WHERE table1.col1 = table2.col1 "
"AND table1.col2 = :col2_1)",
)
def test_this_thing(self):
s = select(t1).where(t1.c.col1 == "foo").alias()
s2 = select(s.c.col1)
self.assert_compile(
s2,
"SELECT anon_1.col1 FROM (SELECT "
"table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 WHERE "
"table1.col1 = :col1_1) AS anon_1",
)
t1a = t1.alias()
s2 = sql_util.ClauseAdapter(t1a).traverse(s2)
self.assert_compile(
s2,
"SELECT anon_1.col1 FROM (SELECT "
"table1_1.col1 AS col1, table1_1.col2 AS "
"col2, table1_1.col3 AS col3 FROM table1 "
"AS table1_1 WHERE table1_1.col1 = "
":col1_1) AS anon_1",
)
def test_this_thing_using_setup_joins_one(self):
s = select(t1).join_from(t1, t2, t1.c.col1 == t2.c.col2).subquery()
s2 = select(s.c.col1).join_from(t3, s, t3.c.col2 == s.c.col1)
self.assert_compile(
s2,
"SELECT anon_1.col1 FROM table3 JOIN (SELECT table1.col1 AS "
"col1, table1.col2 AS col2, table1.col3 AS col3 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2) AS anon_1 "
"ON table3.col2 = anon_1.col1",
)
t1a = t1.alias()
s2 = sql_util.ClauseAdapter(t1a).traverse(s2)
self.assert_compile(
s2,
"SELECT anon_1.col1 FROM table3 JOIN (SELECT table1_1.col1 AS "
"col1, table1_1.col2 AS col2, table1_1.col3 AS col3 "
"FROM table1 AS table1_1 JOIN table2 ON table1_1.col1 = "
"table2.col2) AS anon_1 ON table3.col2 = anon_1.col1",
)
def test_this_thing_using_setup_joins_two(self):
s = select(t1.c.col1).join(t2, t1.c.col1 == t2.c.col2).subquery()
s2 = select(s.c.col1)
self.assert_compile(
s2,
"SELECT anon_1.col1 FROM (SELECT table1.col1 AS col1 "
"FROM table1 JOIN table2 ON table1.col1 = table2.col2) AS anon_1",
)
t1alias = t1.alias("t1alias")
j = t1.join(t1alias, t1.c.col1 == t1alias.c.col2)
vis = sql_util.ClauseAdapter(j)
s2 = vis.traverse(s2)
self.assert_compile(
s2,
"SELECT anon_1.col1 FROM (SELECT table1.col1 AS col1 "
"FROM table1 JOIN table1 AS t1alias "
"ON table1.col1 = t1alias.col2 "
"JOIN table2 ON table1.col1 = table2.col2) AS anon_1",
)
def test_this_thing_using_setup_joins_three(self):
j = t1.join(t2, t1.c.col1 == t2.c.col2)
s1 = select(j)
s2 = s1.join(t3, t1.c.col1 == t3.c.col1)
self.assert_compile(
s2,
"SELECT table1.col1, table1.col2, table1.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2 JOIN table3 "
"ON table3.col1 = table1.col1",
)
vis = sql_util.ClauseAdapter(j)
s3 = vis.traverse(s1)
s4 = s3.join(t3, t1.c.col1 == t3.c.col1)
self.assert_compile(
s4,
"SELECT table1.col1, table1.col2, table1.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2 JOIN table3 "
"ON table3.col1 = table1.col1",
)
s5 = vis.traverse(s3)
s6 = s5.join(t3, t1.c.col1 == t3.c.col1)
self.assert_compile(
s6,
"SELECT table1.col1, table1.col2, table1.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2 JOIN table3 "
"ON table3.col1 = table1.col1",
)
def test_this_thing_using_setup_joins_four(self):
j = t1.join(t2, t1.c.col1 == t2.c.col2)
s1 = select(j)
assert not s1._from_obj
s2 = s1.join(t3, t1.c.col1 == t3.c.col1)
self.assert_compile(
s2,
"SELECT table1.col1, table1.col2, table1.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2 JOIN table3 "
"ON table3.col1 = table1.col1",
)
s3 = visitors.replacement_traverse(s1, {}, lambda elem: None)
s4 = s3.join(t3, t1.c.col1 == t3.c.col1)
self.assert_compile(
s4,
"SELECT table1.col1, table1.col2, table1.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2 JOIN table3 "
"ON table3.col1 = table1.col1",
)
s5 = visitors.replacement_traverse(s3, {}, lambda elem: None)
s6 = s5.join(t3, t1.c.col1 == t3.c.col1)
self.assert_compile(
s6,
"SELECT table1.col1, table1.col2, table1.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col2 JOIN table3 "
"ON table3.col1 = table1.col1",
)
def test_select_fromtwice_one(self):
t1a = t1.alias()
s = (
select(1)
.where(t1.c.col1 == t1a.c.col1)
.select_from(t1a)
.correlate(t1a)
)
s = select(t1).where(t1.c.col1 == s.scalar_subquery())
self.assert_compile(
s,
"SELECT table1.col1, table1.col2, table1.col3 FROM table1 "
"WHERE table1.col1 = "
"(SELECT 1 FROM table1 AS table1_1, table1 "
"WHERE table1.col1 = table1_1.col1)",
)
s = CloningVisitor().traverse(s)
self.assert_compile(
s,
"SELECT table1.col1, table1.col2, table1.col3 FROM table1 "
"WHERE table1.col1 = "
"(SELECT 1 FROM table1 AS table1_1, table1 "
"WHERE table1.col1 = table1_1.col1)",
)
def test_select_fromtwice_two(self):
s = select(t1).where(t1.c.col1 == "foo").alias()
s2 = (
select(1).where(t1.c.col1 == s.c.col1).select_from(s).correlate(t1)
)
s3 = select(t1).where(t1.c.col1 == s2.scalar_subquery())
self.assert_compile(
s3,
"SELECT table1.col1, table1.col2, table1.col3 "
"FROM table1 WHERE table1.col1 = "
"(SELECT 1 FROM "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 "
"WHERE table1.col1 = :col1_1) "
"AS anon_1 WHERE table1.col1 = anon_1.col1)",
)
s4 = ReplacingCloningVisitor().traverse(s3)
self.assert_compile(
s4,
"SELECT table1.col1, table1.col2, table1.col3 "
"FROM table1 WHERE table1.col1 = "
"(SELECT 1 FROM "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 "
"WHERE table1.col1 = :col1_1) "
"AS anon_1 WHERE table1.col1 = anon_1.col1)",
)
def test_select_setup_joins_adapt_element_one(self):
s = select(t1).join(t2, t1.c.col1 == t2.c.col2)
t1a = t1.alias()
s2 = sql_util.ClauseAdapter(t1a).traverse(s)
self.assert_compile(
s,
"SELECT table1.col1, table1.col2, table1.col3 "
"FROM table1 JOIN table2 ON table1.col1 = table2.col2",
)
self.assert_compile(
s2,
"SELECT table1_1.col1, table1_1.col2, table1_1.col3 "
"FROM table1 AS table1_1 JOIN table2 "
"ON table1_1.col1 = table2.col2",
)
def test_select_setup_joins_adapt_element_two(self):
s = select(literal_column("1")).join_from(
t1, t2, t1.c.col1 == t2.c.col2
)
t1a = t1.alias()
s2 = sql_util.ClauseAdapter(t1a).traverse(s)
self.assert_compile(
s, "SELECT 1 FROM table1 JOIN table2 ON table1.col1 = table2.col2"
)
self.assert_compile(
s2,
"SELECT 1 FROM table1 AS table1_1 "
"JOIN table2 ON table1_1.col1 = table2.col2",
)
def test_select_setup_joins_adapt_element_three(self):
s = select(literal_column("1")).join_from(
t1, t2, t1.c.col1 == t2.c.col2
)
t2a = t2.alias()
s2 = sql_util.ClauseAdapter(t2a).traverse(s)
self.assert_compile(
s, "SELECT 1 FROM table1 JOIN table2 ON table1.col1 = table2.col2"
)
self.assert_compile(
s2,
"SELECT 1 FROM table1 "
"JOIN table2 AS table2_1 ON table1.col1 = table2_1.col2",
)
def test_select_setup_joins_straight_clone(self):
s = select(t1).join(t2, t1.c.col1 == t2.c.col2)
s2 = CloningVisitor().traverse(s)
self.assert_compile(
s,
"SELECT table1.col1, table1.col2, table1.col3 "
"FROM table1 JOIN table2 ON table1.col1 = table2.col2",
)
self.assert_compile(
s2,
"SELECT table1.col1, table1.col2, table1.col3 "
"FROM table1 JOIN table2 ON table1.col1 = table2.col2",
)
class ColumnAdapterTest(fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = "default"
@classmethod
def setup_test_class(cls):
global t1, t2
t1 = table(
"table1",
column("col1"),
column("col2"),
column("col3"),
column("col4"),
)
t2 = table("table2", column("col1"), column("col2"), column("col3"))
def test_traverse_memoizes_w_columns(self):
t1a = t1.alias()
adapter = sql_util.ColumnAdapter(t1a, anonymize_labels=True)
expr = select(t1a.c.col1).label("x")
expr_adapted = adapter.traverse(expr)
is_not(expr, expr_adapted)
is_(adapter.columns[expr], expr_adapted)
def test_traverse_memoizes_w_itself(self):
t1a = t1.alias()
adapter = sql_util.ColumnAdapter(t1a, anonymize_labels=True)
expr = select(t1a.c.col1).label("x")
expr_adapted = adapter.traverse(expr)
is_not(expr, expr_adapted)
is_(adapter.traverse(expr), expr_adapted)
def test_columns_memoizes_w_itself(self):
t1a = t1.alias()
adapter = sql_util.ColumnAdapter(t1a, anonymize_labels=True)
expr = select(t1a.c.col1).label("x")
expr_adapted = adapter.columns[expr]
is_not(expr, expr_adapted)
is_(adapter.columns[expr], expr_adapted)
def test_wrapping_fallthrough(self):
t1a = t1.alias(name="t1a")
t2a = t2.alias(name="t2a")
a1 = sql_util.ColumnAdapter(t1a)
s1 = (
select(t1a.c.col1, t2a.c.col1)
.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
.alias()
)
a2 = sql_util.ColumnAdapter(s1)
a3 = a2.wrap(a1)
a4 = a1.wrap(a2)
a5 = a1.chain(a2)
# t1.c.col1 -> s1.c.t1a_col1
# adapted by a2
is_(a3.columns[t1.c.col1], s1.c.t1a_col1)
is_(a4.columns[t1.c.col1], s1.c.t1a_col1)
# chaining can't fall through because a1 grabs it
# first
is_(a5.columns[t1.c.col1], t1a.c.col1)
# t2.c.col1 -> s1.c.t2a_col1
# adapted by a2
is_(a3.columns[t2.c.col1], s1.c.t2a_col1)
is_(a4.columns[t2.c.col1], s1.c.t2a_col1)
# chaining, t2 hits s1
is_(a5.columns[t2.c.col1], s1.c.t2a_col1)
# t1.c.col2 -> t1a.c.col2
# fallthrough to a1
is_(a3.columns[t1.c.col2], t1a.c.col2)
is_(a4.columns[t1.c.col2], t1a.c.col2)
# chaining hits a1
is_(a5.columns[t1.c.col2], t1a.c.col2)
# t2.c.col2 -> t2.c.col2
# fallthrough to no adaption
is_(a3.columns[t2.c.col2], t2.c.col2)
is_(a4.columns[t2.c.col2], t2.c.col2)
def test_wrapping_ordering(self):
"""illustrate an example where order of wrappers matters.
This test illustrates both the ordering being significant
as well as a scenario where multiple translations are needed
(e.g. wrapping vs. chaining).
"""
stmt = (
select(t1.c.col1, t2.c.col1)
.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
.subquery()
)
sa = stmt.alias()
stmt2 = select(t2, sa).subquery()
a1 = sql_util.ColumnAdapter(stmt)
a2 = sql_util.ColumnAdapter(stmt2)
a2_to_a1 = a2.wrap(a1)
a1_to_a2 = a1.wrap(a2)
# when stmt2 and stmt represent the same column
# in different contexts, order of wrapping matters
# t2.c.col1 via a2 is stmt2.c.col1; then ignored by a1
is_(a2_to_a1.columns[t2.c.col1], stmt2.c.col1)
# t2.c.col1 via a1 is stmt.c.table2_col1; a2 then
# sends this to stmt2.c.table2_col1
is_(a1_to_a2.columns[t2.c.col1], stmt2.c.table2_col1)
# check that these aren't the same column
is_not(stmt2.c.col1, stmt2.c.table2_col1)
# for mutually exclusive columns, order doesn't matter
is_(a2_to_a1.columns[t1.c.col1], stmt2.c.table1_col1)
is_(a1_to_a2.columns[t1.c.col1], stmt2.c.table1_col1)
is_(a2_to_a1.columns[t2.c.col2], stmt2.c.col2)
def test_wrapping_multiple(self):
"""illustrate that wrapping runs both adapters"""
t1a = t1.alias(name="t1a")
t2a = t2.alias(name="t2a")
a1 = sql_util.ColumnAdapter(t1a)
a2 = sql_util.ColumnAdapter(t2a)
a3 = a2.wrap(a1)
stmt = select(t1.c.col1, t2.c.col2)
self.assert_compile(
a3.traverse(stmt),
"SELECT t1a.col1, t2a.col2 FROM table1 AS t1a, table2 AS t2a",
)
# chaining does too because these adapters don't share any
# columns
a4 = a2.chain(a1)
self.assert_compile(
a4.traverse(stmt),
"SELECT t1a.col1, t2a.col2 FROM table1 AS t1a, table2 AS t2a",
)
def test_wrapping_inclusions(self):
"""test wrapping and inclusion rules together,
taking into account multiple objects with equivalent hash identity."""
t1a = t1.alias(name="t1a")
t2a = t2.alias(name="t2a")
a1 = sql_util.ColumnAdapter(
t1a, include_fn=lambda col: "a1" in col._annotations
)
s1 = (
select(t1a, t2a)
.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
.alias()
)
a2 = sql_util.ColumnAdapter(
s1, include_fn=lambda col: "a2" in col._annotations
)
a3 = a2.wrap(a1)
c1a1 = t1.c.col1._annotate(dict(a1=True))
c1a2 = t1.c.col1._annotate(dict(a2=True))
c1aa = t1.c.col1._annotate(dict(a1=True, a2=True))
c2a1 = t2.c.col1._annotate(dict(a1=True))
c2a2 = t2.c.col1._annotate(dict(a2=True))
c2aa = t2.c.col1._annotate(dict(a1=True, a2=True))
is_(a3.columns[c1a1], t1a.c.col1)
is_(a3.columns[c1a2], s1.c.t1a_col1)
is_(a3.columns[c1aa], s1.c.t1a_col1)
# not covered by a1, accepted by a2
is_(a3.columns[c2aa], s1.c.t2a_col1)
# not covered by a1, accepted by a2
is_(a3.columns[c2a2], s1.c.t2a_col1)
# not covered by a1, rejected by a2
is_(a3.columns[c2a1], c2a1)
@testing.combinations(True, False, argnames="colpresent")
@testing.combinations(True, False, argnames="adapt_on_names")
@testing.combinations(True, False, argnames="use_label")
def test_adapt_binary_col(self, colpresent, use_label, adapt_on_names):
"""test #9273"""
if use_label:
stmt = select(t1.c.col1, (t1.c.col2 > 18).label("foo"))
else:
stmt = select(t1.c.col1, (t1.c.col2 > 18))
sq = stmt.subquery()
if colpresent:
s2 = select(sq.c[0], sq.c[1])
else:
s2 = select(sq.c[0])
a1 = sql_util.ColumnAdapter(s2, adapt_on_names=adapt_on_names)
is_(a1.columns[stmt.selected_columns[0]], s2.selected_columns[0])
if colpresent:
is_(a1.columns[stmt.selected_columns[1]], s2.selected_columns[1])
else:
is_(
a1.columns[stmt.selected_columns[1]],
a1.columns[stmt.selected_columns[1]],
)
class ClauseAdapterTest(fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = "default"
@classmethod
def setup_test_class(cls):
global t1, t2
t1 = table("table1", column("col1"), column("col2"), column("col3"))
t2 = table("table2", column("col1"), column("col2"), column("col3"))
def test_correlation_on_clone(self):
t1alias = t1.alias("t1alias")
t2alias = t2.alias("t2alias")
vis = sql_util.ClauseAdapter(t1alias)
s = (
select(literal_column("*"))
.select_from(t1alias, t2alias)
.scalar_subquery()
)
froms = list(s._iterate_from_elements())
assert t2alias in froms
assert t1alias in froms
self.assert_compile(
select(literal_column("*")).where(t2alias.c.col1 == s),
"SELECT * FROM table2 AS t2alias WHERE "
"t2alias.col1 = (SELECT * FROM table1 AS "
"t1alias)",
)
s = vis.traverse(s)
froms = list(s._iterate_from_elements())
assert t2alias in froms # present because it was not cloned
assert t1alias in froms # present because the adapter placed
# it there and was also not cloned
# correlate list on "s" needs to take into account the full
# _cloned_set for each element in _froms when correlating
self.assert_compile(
select(literal_column("*")).where(t2alias.c.col1 == s),
"SELECT * FROM table2 AS t2alias WHERE "
"t2alias.col1 = (SELECT * FROM table1 AS "
"t1alias)",
)
s = (
select(literal_column("*"))
.select_from(t1alias, t2alias)
.correlate(t2alias)
.scalar_subquery()
)
self.assert_compile(
select(literal_column("*")).where(t2alias.c.col1 == s),
"SELECT * FROM table2 AS t2alias WHERE "
"t2alias.col1 = (SELECT * FROM table1 AS "
"t1alias)",
)
s = vis.traverse(s)
self.assert_compile(
select(literal_column("*")).where(t2alias.c.col1 == s),
"SELECT * FROM table2 AS t2alias WHERE "
"t2alias.col1 = (SELECT * FROM table1 AS "
"t1alias)",
)
s = CloningVisitor().traverse(s)
self.assert_compile(
select(literal_column("*")).where(t2alias.c.col1 == s),
"SELECT * FROM table2 AS t2alias WHERE "
"t2alias.col1 = (SELECT * FROM table1 AS "
"t1alias)",
)
s = (
select(literal_column("*"))
.where(t1.c.col1 == t2.c.col1)
.scalar_subquery()
)
self.assert_compile(
select(t1.c.col1, s),
"SELECT table1.col1, (SELECT * FROM table2 "
"WHERE table1.col1 = table2.col1) AS "
"anon_1 FROM table1",
)
vis = sql_util.ClauseAdapter(t1alias)
s = vis.traverse(s)
self.assert_compile(
select(t1alias.c.col1, s),
"SELECT t1alias.col1, (SELECT * FROM "
"table2 WHERE t1alias.col1 = table2.col1) "
"AS anon_1 FROM table1 AS t1alias",
)
s = CloningVisitor().traverse(s)
self.assert_compile(
select(t1alias.c.col1, s),
"SELECT t1alias.col1, (SELECT * FROM "
"table2 WHERE t1alias.col1 = table2.col1) "
"AS anon_1 FROM table1 AS t1alias",
)
s = (
select(literal_column("*"))
.where(t1.c.col1 == t2.c.col1)
.correlate(t1)
.scalar_subquery()
)
self.assert_compile(
select(t1.c.col1, s),
"SELECT table1.col1, (SELECT * FROM table2 "
"WHERE table1.col1 = table2.col1) AS "
"anon_1 FROM table1",
)
vis = sql_util.ClauseAdapter(t1alias)
s = vis.traverse(s)
self.assert_compile(
select(t1alias.c.col1, s),
"SELECT t1alias.col1, (SELECT * FROM "
"table2 WHERE t1alias.col1 = table2.col1) "
"AS anon_1 FROM table1 AS t1alias",
)
s = CloningVisitor().traverse(s)
self.assert_compile(
select(t1alias.c.col1, s),
"SELECT t1alias.col1, (SELECT * FROM "
"table2 WHERE t1alias.col1 = table2.col1) "
"AS anon_1 FROM table1 AS t1alias",
)
def test_adapt_select_w_unlabeled_fn(self):
expr = func.count(t1.c.col1)
stmt = select(t1, expr)
self.assert_compile(
stmt,
"SELECT table1.col1, table1.col2, table1.col3, "
"count(table1.col1) AS count_1 FROM table1",
)
stmt2 = select(stmt.subquery())
self.assert_compile(
stmt2,
"SELECT anon_1.col1, anon_1.col2, anon_1.col3, anon_1.count_1 "
"FROM (SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3, count(table1.col1) AS count_1 "
"FROM table1) AS anon_1",
)
is_(
stmt2.selected_columns[3],
stmt2.selected_columns.corresponding_column(expr),
)
is_(
sql_util.ClauseAdapter(stmt2).replace(expr),
stmt2.selected_columns[3],
)
column_adapter = sql_util.ColumnAdapter(stmt2)
is_(column_adapter.columns[expr], stmt2.selected_columns[3])
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_correlate_except_on_clone(self, use_adapt_from):
# test [ticket:4537]'s issue
t1alias = t1.alias("t1alias")
j = t1.join(t1alias, t1.c.col1 == t1alias.c.col2)
if use_adapt_from:
vis = sql_util.ClauseAdapter(j, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(j)
# "control" subquery - uses correlate which has worked w/ adaption
# for a long time
control_s = (
select(t2.c.col1)
.where(t2.c.col1 == t1.c.col1)
.correlate(t2)
.scalar_subquery()
)
# test subquery - given only t1 and t2 in the enclosing selectable,
# will do the same thing as the "control" query since the correlation
# works out the same
s = (
select(t2.c.col1)
.where(t2.c.col1 == t1.c.col1)
.correlate_except(t1)
.scalar_subquery()
)
# use both subqueries in statements
control_stmt = select(control_s, t1.c.col1, t2.c.col1).select_from(
t1.join(t2, t1.c.col1 == t2.c.col1)
)
stmt = select(s, t1.c.col1, t2.c.col1).select_from(
t1.join(t2, t1.c.col1 == t2.c.col1)
)
# they are the same
self.assert_compile(
control_stmt,
"SELECT "
"(SELECT table2.col1 FROM table1 "
"WHERE table2.col1 = table1.col1) AS anon_1, "
"table1.col1, table2.col1 AS col1_1 "
"FROM table1 "
"JOIN table2 ON table1.col1 = table2.col1",
)
self.assert_compile(
stmt,
"SELECT "
"(SELECT table2.col1 FROM table1 "
"WHERE table2.col1 = table1.col1) AS anon_1, "
"table1.col1, table2.col1 AS col1_1 "
"FROM table1 "
"JOIN table2 ON table1.col1 = table2.col1",
)
# now test against the adaption of "t1" into "t1 JOIN t1alias".
# note in the control case, we aren't actually testing that
# Select is processing the "correlate" list during the adaption
# since we aren't adapting the "correlate"
self.assert_compile(
vis.traverse(control_stmt),
"SELECT "
"(SELECT table2.col1 FROM "
"table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 "
"WHERE table2.col1 = table1.col1) AS anon_1, "
"table1.col1, table2.col1 AS col1_1 "
"FROM table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 "
"JOIN table2 ON table1.col1 = table2.col1",
)
# but here, correlate_except() does have the thing we're adapting
# so whatever is in there has to be expanded out to include
# the adaptation target, in this case "t1 JOIN t1alias".
self.assert_compile(
vis.traverse(stmt),
"SELECT "
"(SELECT table2.col1 FROM "
"table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 "
"WHERE table2.col1 = table1.col1) AS anon_1, "
"table1.col1, table2.col1 AS col1_1 "
"FROM table1 JOIN table1 AS t1alias ON table1.col1 = t1alias.col2 "
"JOIN table2 ON table1.col1 = table2.col1",
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_correlate_except_with_mixed_tables(self, use_adapt_from):
# test [ticket:6060]'s issue
stmt = select(
t1.c.col1,
select(func.count(t2.c.col1))
.where(t2.c.col1 == t1.c.col1)
.correlate_except(t2)
.scalar_subquery(),
)
self.assert_compile(
stmt,
"SELECT table1.col1, "
"(SELECT count(table2.col1) AS count_1 FROM table2 "
"WHERE table2.col1 = table1.col1) AS anon_1 "
"FROM table1",
)
subq = (
select(t1)
.join(t2, t1.c.col1 == t2.c.col1)
.where(t2.c.col2 == "x")
.subquery()
)
if use_adapt_from:
vis = sql_util.ClauseAdapter(subq, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(subq)
if use_adapt_from:
self.assert_compile(
vis.traverse(stmt),
"SELECT anon_1.col1, "
"(SELECT count(table2.col1) AS count_1 FROM table2 WHERE "
"table2.col1 = anon_1.col1) AS anon_2 "
"FROM (SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 JOIN table2 ON table1.col1 = "
"table2.col1 WHERE table2.col2 = :col2_1) AS anon_1",
)
else:
# here's the buggy version. table2 gets yanked out of the
# correlated subquery also. AliasedClass now uses
# adapt_from_selectables in all cases
self.assert_compile(
vis.traverse(stmt),
"SELECT anon_1.col1, "
"(SELECT count(table2.col1) AS count_1 FROM table2, "
"(SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 JOIN table2 ON "
"table1.col1 = table2.col1 WHERE table2.col2 = :col2_1) AS "
"anon_1 WHERE table2.col1 = anon_1.col1) AS anon_2 "
"FROM (SELECT table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1 JOIN table2 "
"ON table1.col1 = table2.col1 "
"WHERE table2.col2 = :col2_1) AS anon_1",
)
@testing.fails_on_everything_except()
def test_joins_dont_adapt(self):
# adapting to a join, i.e. ClauseAdapter(t1.join(t2)), doesn't
# make much sense. ClauseAdapter doesn't make any changes if
# it's against a straight join.
users = table("users", column("id"))
addresses = table("addresses", column("id"), column("user_id"))
ualias = users.alias()
s = (
select(func.count(addresses.c.id))
.where(users.c.id == addresses.c.user_id)
.correlate(users)
)
s = sql_util.ClauseAdapter(ualias).traverse(s)
j1 = addresses.join(ualias, addresses.c.user_id == ualias.c.id)
self.assert_compile(
sql_util.ClauseAdapter(j1).traverse(s),
"SELECT count(addresses.id) AS count_1 "
"FROM addresses WHERE users_1.id = "
"addresses.user_id",
)
def test_prev_entities_adapt(self):
"""test #6503"""
m = MetaData()
users = Table("users", m, Column("id", Integer, primary_key=True))
addresses = Table(
"addresses",
m,
Column("id", Integer, primary_key=True),
Column("user_id", ForeignKey("users.id")),
)
ualias = users.alias()
s = select(users).join(addresses).with_only_columns(addresses.c.id)
s = sql_util.ClauseAdapter(ualias).traverse(s)
self.assert_compile(
s,
"SELECT addresses.id FROM users AS users_1 "
"JOIN addresses ON users_1.id = addresses.user_id",
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_table_to_alias_1(self, use_adapt_from):
t1alias = t1.alias("t1alias")
if use_adapt_from:
vis = sql_util.ClauseAdapter(t1alias, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(t1alias)
ff = vis.traverse(func.count(t1.c.col1).label("foo"))
assert list(_from_objects(ff)) == [t1alias]
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_table_to_alias_2(self, use_adapt_from):
t1alias = t1.alias("t1alias")
if use_adapt_from:
vis = sql_util.ClauseAdapter(t1alias, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
vis.traverse(select(literal_column("*")).select_from(t1)),
"SELECT * FROM table1 AS t1alias",
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_table_to_alias_3(self, use_adapt_from):
t1alias = t1.alias("t1alias")
if use_adapt_from:
vis = sql_util.ClauseAdapter(t1alias, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
vis.traverse(
select(literal_column("*")).where(t1.c.col1 == t2.c.col2)
),
"SELECT * FROM table1 AS t1alias, table2 "
"WHERE t1alias.col1 = table2.col2",
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_table_to_alias_4(self, use_adapt_from):
t1alias = t1.alias("t1alias")
if use_adapt_from:
vis = sql_util.ClauseAdapter(t1alias, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
vis.traverse(
select(literal_column("*"))
.where(t1.c.col1 == t2.c.col2)
.select_from(t1, t2)
),
"SELECT * FROM table1 AS t1alias, table2 "
"WHERE t1alias.col1 = table2.col2",
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_table_to_alias_5(self, use_adapt_from):
t1alias = t1.alias("t1alias")
if use_adapt_from:
vis = sql_util.ClauseAdapter(t1alias, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
select(t1alias, t2).where(
t1alias.c.col1
== vis.traverse(
select(literal_column("*"))
.where(t1.c.col1 == t2.c.col2)
.select_from(t1, t2)
.correlate(t1)
.scalar_subquery()
)
),
"SELECT t1alias.col1, t1alias.col2, t1alias.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 "
"FROM table1 AS t1alias, table2 WHERE t1alias.col1 = "
"(SELECT * FROM table2 WHERE t1alias.col1 = table2.col2)",
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_table_to_alias_6(self, use_adapt_from):
t1alias = t1.alias("t1alias")
if use_adapt_from:
vis = sql_util.ClauseAdapter(t1alias, adapt_from_selectables=[t1])
else:
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
select(t1alias, t2).where(
t1alias.c.col1
== vis.traverse(
select(literal_column("*"))
.where(t1.c.col1 == t2.c.col2)
.select_from(t1, t2)
.correlate(t2)
.scalar_subquery()
)
),
"SELECT t1alias.col1, t1alias.col2, t1alias.col3, "
"table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 "
"FROM table1 AS t1alias, table2 "
"WHERE t1alias.col1 = "
"(SELECT * FROM table1 AS t1alias "
"WHERE t1alias.col1 = table2.col2)",
)
def test_table_to_alias_7(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
vis.traverse(case((t1.c.col1 == 5, t1.c.col2), else_=t1.c.col1)),
"CASE WHEN (t1alias.col1 = :col1_1) THEN "
"t1alias.col2 ELSE t1alias.col1 END",
)
def test_table_to_alias_8(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
vis.traverse(
case((5, t1.c.col2), value=t1.c.col1, else_=t1.c.col1)
),
"CASE t1alias.col1 WHEN :param_1 THEN "
"t1alias.col2 ELSE t1alias.col1 END",
)
def test_table_to_alias_9(self):
s = select(literal_column("*")).select_from(t1).alias("foo")
self.assert_compile(
s.select(), "SELECT foo.* FROM (SELECT * FROM table1) " "AS foo"
)
def test_table_to_alias_10(self):
s = select(literal_column("*")).select_from(t1).alias("foo")
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
self.assert_compile(
vis.traverse(s.select()),
"SELECT foo.* FROM (SELECT * FROM table1 " "AS t1alias) AS foo",
)
def test_table_to_alias_11(self):
s = select(literal_column("*")).select_from(t1).alias("foo")
self.assert_compile(
s.select(), "SELECT foo.* FROM (SELECT * FROM table1) " "AS foo"
)
def test_table_to_alias_12(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
ff = vis.traverse(func.count(t1.c.col1).label("foo"))
self.assert_compile(
select(ff),
"SELECT count(t1alias.col1) AS foo FROM " "table1 AS t1alias",
)
assert list(_from_objects(ff)) == [t1alias]
# def test_table_to_alias_2(self):
# TODO: self.assert_compile(vis.traverse(select(func.count(t1.c
# .col1).l abel('foo')), clone=True), "SELECT
# count(t1alias.col1) AS foo FROM table1 AS t1alias")
def test_table_to_alias_13(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
t2alias = t2.alias("t2alias")
vis.chain(sql_util.ClauseAdapter(t2alias))
self.assert_compile(
vis.traverse(
select(literal_column("*")).where(t1.c.col1 == t2.c.col2)
),
"SELECT * FROM table1 AS t1alias, table2 "
"AS t2alias WHERE t1alias.col1 = "
"t2alias.col2",
)
def test_table_to_alias_14(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
t2alias = t2.alias("t2alias")
vis.chain(sql_util.ClauseAdapter(t2alias))
self.assert_compile(
vis.traverse(
select("*").where(t1.c.col1 == t2.c.col2).select_from(t1, t2)
),
"SELECT * FROM table1 AS t1alias, table2 "
"AS t2alias WHERE t1alias.col1 = "
"t2alias.col2",
)
def test_table_to_alias_15(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
t2alias = t2.alias("t2alias")
vis.chain(sql_util.ClauseAdapter(t2alias))
self.assert_compile(
select(t1alias, t2alias).where(
t1alias.c.col1
== vis.traverse(
select("*")
.where(t1.c.col1 == t2.c.col2)
.select_from(t1, t2)
.correlate(t1)
.scalar_subquery()
)
),
"SELECT t1alias.col1, t1alias.col2, t1alias.col3, "
"t2alias.col1 AS col1_1, t2alias.col2 AS col2_1, "
"t2alias.col3 AS col3_1 "
"FROM table1 AS t1alias, table2 AS t2alias "
"WHERE t1alias.col1 = "
"(SELECT * FROM table2 AS t2alias "
"WHERE t1alias.col1 = t2alias.col2)",
)
def test_table_to_alias_16(self):
t1alias = t1.alias("t1alias")
vis = sql_util.ClauseAdapter(t1alias)
t2alias = t2.alias("t2alias")
vis.chain(sql_util.ClauseAdapter(t2alias))
self.assert_compile(
t2alias.select().where(
t2alias.c.col2
== vis.traverse(
select("*")
.where(t1.c.col1 == t2.c.col2)
.select_from(t1, t2)
.correlate(t2)
.scalar_subquery()
)
),
"SELECT t2alias.col1, t2alias.col2, t2alias.col3 "
"FROM table2 AS t2alias WHERE t2alias.col2 = "
"(SELECT * FROM table1 AS t1alias WHERE "
"t1alias.col1 = t2alias.col2)",
)
def test_include_exclude(self):
m = MetaData()
a = Table(
"a",
m,
Column("id", Integer, primary_key=True),
Column(
"xxx_id",
Integer,
ForeignKey("a.id", name="adf", use_alter=True),
),
)
e = a.c.id == a.c.xxx_id
assert str(e) == "a.id = a.xxx_id"
b = a.alias()
e = sql_util.ClauseAdapter(
b,
include_fn=lambda x: x in {a.c.id},
equivalents={a.c.id: {a.c.id}},
).traverse(e)
assert str(e) == "a_1.id = a.xxx_id"
def test_recursive_equivalents(self):
m = MetaData()
a = Table("a", m, Column("x", Integer), Column("y", Integer))
b = Table("b", m, Column("x", Integer), Column("y", Integer))
c = Table("c", m, Column("x", Integer), Column("y", Integer))
# force a recursion overflow, by linking a.c.x<->c.c.x, and
# asking for a nonexistent col. corresponding_column should prevent
# endless depth.
adapt = sql_util.ClauseAdapter(
b, equivalents={a.c.x: {c.c.x}, c.c.x: {a.c.x}}
)
assert adapt._corresponding_column(a.c.x, False) is None
def test_multilevel_equivalents(self):
m = MetaData()
a = Table("a", m, Column("x", Integer), Column("y", Integer))
b = Table("b", m, Column("x", Integer), Column("y", Integer))
c = Table("c", m, Column("x", Integer), Column("y", Integer))
alias = select(a).select_from(a.join(b, a.c.x == b.c.x)).alias()
# two levels of indirection from c.x->b.x->a.x, requires recursive
# corresponding_column call
adapt = sql_util.ClauseAdapter(
alias, equivalents={b.c.x: {a.c.x}, c.c.x: {b.c.x}}
)
assert adapt._corresponding_column(a.c.x, False) is alias.c.x
assert adapt._corresponding_column(c.c.x, False) is alias.c.x
def test_join_to_alias(self):
metadata = MetaData()
a = Table("a", metadata, Column("id", Integer, primary_key=True))
b = Table(
"b",
metadata,
Column("id", Integer, primary_key=True),
Column("aid", Integer, ForeignKey("a.id")),
)
c = Table(
"c",
metadata,
Column("id", Integer, primary_key=True),
Column("bid", Integer, ForeignKey("b.id")),
)
d = Table(
"d",
metadata,
Column("id", Integer, primary_key=True),
Column("aid", Integer, ForeignKey("a.id")),
)
j1 = a.outerjoin(b)
j2 = (
select(j1)
.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
.subquery()
)
j3 = c.join(j2, j2.c.b_id == c.c.bid)
j4 = j3.outerjoin(d)
self.assert_compile(
j4,
"c JOIN (SELECT a.id AS a_id, b.id AS "
"b_id, b.aid AS b_aid FROM a LEFT OUTER "
"JOIN b ON a.id = b.aid) AS anon_1 ON anon_1.b_id = c.bid "
"LEFT OUTER JOIN d ON anon_1.a_id = d.aid",
)
j5 = (
j3.select()
.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
.subquery("foo")
)
j6 = sql_util.ClauseAdapter(j5).copy_and_process([j4])[0]
# this statement takes c join(a join b), wraps it inside an
# aliased "select * from c join(a join b) AS foo". the outermost
# right side "left outer join d" stays the same, except "d"
# joins against foo.a_id instead of plain "a_id"
self.assert_compile(
j6,
"(SELECT c.id AS c_id, c.bid AS c_bid, "
"anon_1.a_id AS anon_1_a_id, anon_1.b_id AS anon_1_b_id, "
"anon_1.b_aid AS "
"anon_1_b_aid FROM c JOIN (SELECT a.id AS a_id, "
"b.id AS b_id, b.aid AS b_aid FROM a LEFT "
"OUTER JOIN b ON a.id = b.aid) AS anon_1 ON anon_1.b_id = "
"c.bid) AS foo LEFT OUTER JOIN d ON "
"foo.anon_1_a_id = d.aid",
)
def test_derived_from(self):
assert select(t1).is_derived_from(t1)
assert not select(t2).is_derived_from(t1)
assert not t1.is_derived_from(select(t1))
assert t1.alias().is_derived_from(t1)
s1 = select(t1, t2).alias("foo")
s2 = select(s1).limit(5).offset(10).alias()
assert s2.is_derived_from(s1)
s2 = s2._clone()
assert s2.is_derived_from(s1)
def test_aliasedselect_to_aliasedselect_straight(self):
# original issue from ticket #904
s1 = select(t1).alias("foo")
s2 = select(s1).limit(5).offset(10).alias()
self.assert_compile(
sql_util.ClauseAdapter(s2).traverse(s1),
"SELECT foo.col1, foo.col2, foo.col3 FROM "
"(SELECT table1.col1 AS col1, table1.col2 "
"AS col2, table1.col3 AS col3 FROM table1) "
"AS foo LIMIT :param_1 OFFSET :param_2",
{"param_1": 5, "param_2": 10},
)
def test_aliasedselect_to_aliasedselect_join(self):
s1 = select(t1).alias("foo")
s2 = select(s1).limit(5).offset(10).alias()
j = s1.outerjoin(t2, s1.c.col1 == t2.c.col1)
self.assert_compile(
sql_util.ClauseAdapter(s2).traverse(j).select(),
"SELECT anon_1.col1, anon_1.col2, "
"anon_1.col3, table2.col1 AS col1_1, table2.col2 AS col2_1, "
"table2.col3 AS col3_1 FROM (SELECT foo.col1 AS "
"col1, foo.col2 AS col2, foo.col3 AS col3 "
"FROM (SELECT table1.col1 AS col1, "
"table1.col2 AS col2, table1.col3 AS col3 "
"FROM table1) AS foo LIMIT :param_1 OFFSET "
":param_2) AS anon_1 LEFT OUTER JOIN "
"table2 ON anon_1.col1 = table2.col1",
{"param_1": 5, "param_2": 10},
)
@testing.combinations((True,), (False,), argnames="use_adapt_from")
def test_aliasedselect_to_aliasedselect_join_nested_table(
self, use_adapt_from
):
"""test the logic in clauseadapter regarding not traversing aliases.
adapt_from_selectables case added to test #6762, which is a regression
from #6060
"""
s1 = select(t1).alias("foo")
s2 = select(s1).limit(5).offset(10).alias()
talias = t1.alias("bar")
# here is the problem. s2 is derived from s1 which is derived
# from t1
assert s2.is_derived_from(t1)
# however, s2 is not derived from talias, which *is* derived from t1
assert not s2.is_derived_from(talias)
# therefore, talias gets its table replaced, except for a rule
# we added to ClauseAdapter to stop traversal if the selectable is
# not derived from an alias of a table. This rule was previously
# in Alias._copy_internals().
j = s1.outerjoin(talias, s1.c.col1 == talias.c.col1)
if use_adapt_from:
vis = sql_util.ClauseAdapter(s2, adapt_from_selectables=[s1])
else:
vis = sql_util.ClauseAdapter(s2)
self.assert_compile(
vis.traverse(j).select(),
"SELECT anon_1.col1, anon_1.col2, "
"anon_1.col3, bar.col1 AS col1_1, bar.col2 AS col2_1, "
"bar.col3 AS col3_1 "
"FROM (SELECT foo.col1 AS col1, foo.col2 "
"AS col2, foo.col3 AS col3 FROM (SELECT "
"table1.col1 AS col1, table1.col2 AS col2, "
"table1.col3 AS col3 FROM table1) AS foo "
"LIMIT :param_1 OFFSET :param_2) AS anon_1 "
"LEFT OUTER JOIN table1 AS bar ON "
"anon_1.col1 = bar.col1",
{"param_1": 5, "param_2": 10},
)
def test_functions(self):
self.assert_compile(
sql_util.ClauseAdapter(t1.alias()).traverse(func.count(t1.c.col1)),
"count(table1_1.col1)",
)
s = select(func.count(t1.c.col1))
self.assert_compile(
sql_util.ClauseAdapter(t1.alias()).traverse(s),
"SELECT count(table1_1.col1) AS count_1 "
"FROM table1 AS table1_1",
)
def test_table_valued_column(self):
"""test #6775"""
stmt = select(func.some_json_func(t1.table_valued()))
self.assert_compile(
stmt,
"SELECT some_json_func(table1) AS some_json_func_1 FROM table1",
)
self.assert_compile(
sql_util.ClauseAdapter(t1.alias()).traverse(stmt),
"SELECT some_json_func(table1_1) AS some_json_func_1 "
"FROM table1 AS table1_1",
)
def test_recursive(self):
metadata = MetaData()
a = Table("a", metadata, Column("id", Integer, primary_key=True))
b = Table(
"b",
metadata,
Column("id", Integer, primary_key=True),
Column("aid", Integer, ForeignKey("a.id")),
)
c = Table(
"c",
metadata,
Column("id", Integer, primary_key=True),
Column("bid", Integer, ForeignKey("b.id")),
)
d = Table(
"d",
metadata,
Column("id", Integer, primary_key=True),
Column("aid", Integer, ForeignKey("a.id")),
)
u = union(
a.join(b).select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL),
a.join(d).select().set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL),
).alias()
self.assert_compile(
sql_util.ClauseAdapter(u).traverse(
select(c.c.bid).where(c.c.bid == u.c.b_aid)
),
"SELECT c.bid "
"FROM c, (SELECT a.id AS a_id, b.id AS b_id, b.aid AS b_aid "
"FROM a JOIN b ON a.id = b.aid UNION SELECT a.id AS a_id, d.id "
"AS d_id, d.aid AS d_aid "
"FROM a JOIN d ON a.id = d.aid) AS anon_1 "
"WHERE c.bid = anon_1.b_aid",
)
def test_label_anonymize_one(self):
t1a = t1.alias()
adapter = sql_util.ClauseAdapter(t1a, anonymize_labels=True)
expr = select(t1.c.col2).where(t1.c.col3 == 5).label("expr")
expr_adapted = adapter.traverse(expr)
stmt = select(expr, expr_adapted).order_by(expr, expr_adapted)
self.assert_compile(
stmt,
"SELECT "
"(SELECT table1.col2 FROM table1 WHERE table1.col3 = :col3_1) "
"AS expr, "
"(SELECT table1_1.col2 FROM table1 AS table1_1 "
"WHERE table1_1.col3 = :col3_2) AS anon_1 "
"ORDER BY expr, anon_1",
)
def test_label_anonymize_two(self):
t1a = t1.alias()
adapter = sql_util.ClauseAdapter(t1a, anonymize_labels=True)
expr = select(t1.c.col2).where(t1.c.col3 == 5).label(None)
expr_adapted = adapter.traverse(expr)
stmt = select(expr, expr_adapted).order_by(expr, expr_adapted)
self.assert_compile(
stmt,
"SELECT "
"(SELECT table1.col2 FROM table1 WHERE table1.col3 = :col3_1) "
"AS anon_1, "
"(SELECT table1_1.col2 FROM table1 AS table1_1 "
"WHERE table1_1.col3 = :col3_2) AS anon_2 "
"ORDER BY anon_1, anon_2",
)
def test_label_anonymize_three(self):
t1a = t1.alias()
adapter = sql_util.ColumnAdapter(
t1a, anonymize_labels=True, allow_label_resolve=False
)
expr = select(t1.c.col2).where(t1.c.col3 == 5).label(None)
l1 = expr
is_(l1._order_by_label_element, l1)
eq_(l1._allow_label_resolve, True)
expr_adapted = adapter.traverse(expr)
l2 = expr_adapted
is_(l2._order_by_label_element, l2)
eq_(l2._allow_label_resolve, False)
l3 = adapter.traverse(expr)
is_(l3._order_by_label_element, l3)
eq_(l3._allow_label_resolve, False)
class SpliceJoinsTest(fixtures.TestBase, AssertsCompiledSQL):
__dialect__ = "default"
@classmethod
def setup_test_class(cls):
global table1, table2, table3, table4
def _table(name):
return table(name, column("col1"), column("col2"), column("col3"))
table1, table2, table3, table4 = (
_table(name) for name in ("table1", "table2", "table3", "table4")
)
def test_splice(self):
t1, t2, t3, t4 = table1, table2, table1.alias(), table2.alias()
j = (
t1.join(t2, t1.c.col1 == t2.c.col1)
.join(t3, t2.c.col1 == t3.c.col1)
.join(t4, t4.c.col1 == t1.c.col1)
)
s = select(t1).where(t1.c.col2 < 5).alias()
self.assert_compile(
sql_util.splice_joins(s, j),
"(SELECT table1.col1 AS col1, table1.col2 "
"AS col2, table1.col3 AS col3 FROM table1 "
"WHERE table1.col2 < :col2_1) AS anon_1 "
"JOIN table2 ON anon_1.col1 = table2.col1 "
"JOIN table1 AS table1_1 ON table2.col1 = "
"table1_1.col1 JOIN table2 AS table2_1 ON "
"table2_1.col1 = anon_1.col1",
)
def test_stop_on(self):
t1, t2, t3 = table1, table2, table3
j1 = t1.join(t2, t1.c.col1 == t2.c.col1)
j2 = j1.join(t3, t2.c.col1 == t3.c.col1)
s = select(t1).select_from(j1).alias()
self.assert_compile(
sql_util.splice_joins(s, j2),
"(SELECT table1.col1 AS col1, table1.col2 "
"AS col2, table1.col3 AS col3 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col1) "
"AS anon_1 JOIN table2 ON anon_1.col1 = "
"table2.col1 JOIN table3 ON table2.col1 = "
"table3.col1",
)
self.assert_compile(
sql_util.splice_joins(s, j2, j1),
"(SELECT table1.col1 AS col1, table1.col2 "
"AS col2, table1.col3 AS col3 FROM table1 "
"JOIN table2 ON table1.col1 = table2.col1) "
"AS anon_1 JOIN table3 ON table2.col1 = "
"table3.col1",
)
def test_splice_2(self):
t2a = table2.alias()
t3a = table3.alias()
j1 = table1.join(t2a, table1.c.col1 == t2a.c.col1).join(
t3a, t2a.c.col2 == t3a.c.col2
)
t2b = table4.alias()
j2 = table1.join(t2b, table1.c.col3 == t2b.c.col3)
self.assert_compile(
sql_util.splice_joins(table1, j1),
"table1 JOIN table2 AS table2_1 ON "
"table1.col1 = table2_1.col1 JOIN table3 "
"AS table3_1 ON table2_1.col2 = "
"table3_1.col2",
)
self.assert_compile(
sql_util.splice_joins(table1, j2),
"table1 JOIN table4 AS table4_1 ON " "table1.col3 = table4_1.col3",
)
self.assert_compile(
sql_util.splice_joins(sql_util.splice_joins(table1, j1), j2),
"table1 JOIN table2 AS table2_1 ON "
"table1.col1 = table2_1.col1 JOIN table3 "
"AS table3_1 ON table2_1.col2 = "
"table3_1.col2 JOIN table4 AS table4_1 ON "
"table1.col3 = table4_1.col3",
)
class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
"""tests the generative capability of Select"""
__dialect__ = "default"
@classmethod
def setup_test_class(cls):
global t1, t2
t1 = table("table1", column("col1"), column("col2"), column("col3"))
t2 = table("table2", column("col1"), column("col2"), column("col3"))
def test_columns(self):
s = t1.select()
self.assert_compile(
s, "SELECT table1.col1, table1.col2, " "table1.col3 FROM table1"
)
select_copy = s.add_columns(column("yyy"))
self.assert_compile(
select_copy,
"SELECT table1.col1, table1.col2, " "table1.col3, yyy FROM table1",
)
is_not(s.selected_columns, select_copy.selected_columns)
is_not(s._raw_columns, select_copy._raw_columns)
self.assert_compile(
s, "SELECT table1.col1, table1.col2, " "table1.col3 FROM table1"
)
def test_froms(self):
s = t1.select()
self.assert_compile(
s, "SELECT table1.col1, table1.col2, " "table1.col3 FROM table1"
)
select_copy = s.select_from(t2)
self.assert_compile(
select_copy,
"SELECT table1.col1, table1.col2, "
"table1.col3 FROM table2, table1",
)
self.assert_compile(
s, "SELECT table1.col1, table1.col2, " "table1.col3 FROM table1"
)
def test_prefixes(self):
s = t1.select()
self.assert_compile(
s, "SELECT table1.col1, table1.col2, " "table1.col3 FROM table1"
)
select_copy = s.prefix_with("FOOBER")
self.assert_compile(
select_copy,
"SELECT FOOBER table1.col1, table1.col2, "
"table1.col3 FROM table1",
)
self.assert_compile(
s, "SELECT table1.col1, table1.col2, " "table1.col3 FROM table1"
)
def test_execution_options(self):
s = select().execution_options(foo="bar")
s2 = s.execution_options(bar="baz")
s3 = s.execution_options(foo="not bar")
# The original select should not be modified.
eq_(s.get_execution_options(), dict(foo="bar"))
# s2 should have its execution_options based on s, though.
eq_(s2.get_execution_options(), dict(foo="bar", bar="baz"))
eq_(s3.get_execution_options(), dict(foo="not bar"))
def test_invalid_options(self):
assert_raises(
exc.ArgumentError, select().execution_options, compiled_cache={}
)
assert_raises(
exc.ArgumentError,
select().execution_options,
isolation_level="READ_COMMITTED",
)
# this feature not available yet
def _NOTYET_test_execution_options_in_kwargs(self):
s = select(execution_options=dict(foo="bar"))
s2 = s.execution_options(bar="baz")
# The original select should not be modified.
assert s._execution_options == dict(foo="bar")
# s2 should have its execution_options based on s, though.
assert s2._execution_options == dict(foo="bar", bar="baz")
# this feature not available yet
def _NOTYET_test_execution_options_in_text(self):
s = text("select 42", execution_options=dict(foo="bar"))
assert s._execution_options == dict(foo="bar")
class ValuesBaseTest(fixtures.TestBase, AssertsCompiledSQL):
"""Tests the generative capability of Insert, Update"""
__dialect__ = "default"
# fixme: consolidate converage from elsewhere here and expand
@classmethod
def setup_test_class(cls):
global t1, t2
t1 = table("table1", column("col1"), column("col2"), column("col3"))
t2 = table("table2", column("col1"), column("col2"), column("col3"))
def test_prefixes(self):
i = t1.insert()
self.assert_compile(
i,
"INSERT INTO table1 (col1, col2, col3) "
"VALUES (:col1, :col2, :col3)",
)
gen = i.prefix_with("foober")
self.assert_compile(
gen,
"INSERT foober INTO table1 (col1, col2, col3) "
"VALUES (:col1, :col2, :col3)",
)
self.assert_compile(
i,
"INSERT INTO table1 (col1, col2, col3) "
"VALUES (:col1, :col2, :col3)",
)
i2 = t1.insert().prefix_with("squiznart")
self.assert_compile(
i2,
"INSERT squiznart INTO table1 (col1, col2, col3) "
"VALUES (:col1, :col2, :col3)",
)
gen2 = i2.prefix_with("quux")
self.assert_compile(
gen2,
"INSERT squiznart quux INTO "
"table1 (col1, col2, col3) "
"VALUES (:col1, :col2, :col3)",
)
def test_add_kwarg(self):
i = t1.insert()
compile_state = i._compile_state_factory(i, None)
eq_(compile_state._dict_parameters, None)
i = i.values(col1=5)
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(compile_state._dict_parameters, {"col1": 5})
i = i.values(col2=7)
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(
compile_state._dict_parameters, {"col1": 5, "col2": 7}
)
def test_via_tuple_single(self):
i = t1.insert()
compile_state = i._compile_state_factory(i, None)
eq_(compile_state._dict_parameters, None)
i = i.values((5, 6, 7))
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(
compile_state._dict_parameters,
{"col1": 5, "col2": 6, "col3": 7},
)
def test_kw_and_dict_simultaneously_single(self):
i = t1.insert()
assert_raises_message(
exc.ArgumentError,
r"Can't pass positional and kwargs to values\(\) simultaneously",
i.values,
{"col1": 5},
col2=7,
)
def test_via_tuple_multi(self):
i = t1.insert()
compile_state = i._compile_state_factory(i, None)
eq_(compile_state._dict_parameters, None)
i = i.values([(5, 6, 7), (8, 9, 10)])
compile_state = i._compile_state_factory(i, None)
eq_(
compile_state._dict_parameters,
{"col1": 5, "col2": 6, "col3": 7},
)
eq_(compile_state._has_multi_parameters, True)
eq_(
compile_state._multi_parameters,
[
{"col1": 5, "col2": 6, "col3": 7},
{"col1": 8, "col2": 9, "col3": 10},
],
)
def test_inline_values_single(self):
i = t1.insert().values({"col1": 5})
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(compile_state._dict_parameters, {"col1": 5})
is_(compile_state._has_multi_parameters, False)
def test_inline_values_multi(self):
i = t1.insert().values([{"col1": 5}, {"col1": 6}])
compile_state = i._compile_state_factory(i, None)
# multiparams are not converted to bound parameters
eq_(compile_state._dict_parameters, {"col1": 5})
# multiparams are not converted to bound parameters
eq_(compile_state._multi_parameters, [{"col1": 5}, {"col1": 6}])
is_(compile_state._has_multi_parameters, True)
def _compare_param_dict(self, a, b):
if list(a) != list(b):
return False
from sqlalchemy.types import NullType
for a_k, a_i in a.items():
b_i = b[a_k]
# compare BindParameter on the left to
# literal value on the right
assert a_i.compare(literal(b_i, type_=NullType()))
def test_add_dictionary(self):
i = t1.insert()
compile_state = i._compile_state_factory(i, None)
eq_(compile_state._dict_parameters, None)
i = i.values({"col1": 5})
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(compile_state._dict_parameters, {"col1": 5})
is_(compile_state._has_multi_parameters, False)
i = i.values({"col1": 6})
# note replaces
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(compile_state._dict_parameters, {"col1": 6})
is_(compile_state._has_multi_parameters, False)
i = i.values({"col2": 7})
compile_state = i._compile_state_factory(i, None)
self._compare_param_dict(
compile_state._dict_parameters, {"col1": 6, "col2": 7}
)
is_(compile_state._has_multi_parameters, False)
def test_add_kwarg_disallowed_multi(self):
i = t1.insert()
i = i.values([{"col1": 5}, {"col1": 7}])
i = i.values(col2=7)
assert_raises_message(
exc.InvalidRequestError,
"Can't mix single and multiple VALUES formats",
i.compile,
)
def test_cant_mix_single_multi_formats_dict_to_list(self):
i = t1.insert().values(col1=5)
i = i.values([{"col1": 6}])
assert_raises_message(
exc.InvalidRequestError,
"Can't mix single and multiple VALUES "
"formats in one INSERT statement",
i.compile,
)
def test_cant_mix_single_multi_formats_list_to_dict(self):
i = t1.insert().values([{"col1": 6}])
i = i.values({"col1": 5})
assert_raises_message(
exc.InvalidRequestError,
"Can't mix single and multiple VALUES "
"formats in one INSERT statement",
i.compile,
)
def test_erroneous_multi_args_dicts(self):
i = t1.insert()
assert_raises_message(
exc.ArgumentError,
"Only a single dictionary/tuple or list of "
"dictionaries/tuples is accepted positionally.",
i.values,
{"col1": 5},
{"col1": 7},
)
def test_erroneous_multi_args_tuples(self):
i = t1.insert()
assert_raises_message(
exc.ArgumentError,
"Only a single dictionary/tuple or list of "
"dictionaries/tuples is accepted positionally.",
i.values,
(5, 6, 7),
(8, 9, 10),
)
def test_erroneous_multi_args_plus_kw(self):
i = t1.insert()
assert_raises_message(
exc.ArgumentError,
r"Can't pass positional and kwargs to values\(\) simultaneously",
i.values,
[{"col1": 5}],
col2=7,
)
def test_update_no_support_multi_values(self):
u = t1.update()
u = u.values([{"col1": 5}, {"col1": 7}])
assert_raises_message(
exc.InvalidRequestError,
"UPDATE construct does not support multiple parameter sets.",
u.compile,
)
def test_update_no_support_multi_constructor(self):
stmt = t1.update().values([{"col1": 5}, {"col1": 7}])
assert_raises_message(
exc.InvalidRequestError,
"UPDATE construct does not support multiple parameter sets.",
stmt.compile,
)
@testing.variation("stmt_type", ["update", "delete"])
def test_whereclause_returning_adapted(self, stmt_type):
"""test #9033"""
if stmt_type.update:
stmt = (
t1.update()
.where(t1.c.col1 == 10)
.values(col1=15)
.returning(t1.c.col1)
)
elif stmt_type.delete:
stmt = t1.delete().where(t1.c.col1 == 10).returning(t1.c.col1)
else:
stmt_type.fail()
stmt = visitors.replacement_traverse(stmt, {}, lambda elem: None)
assert isinstance(stmt._where_criteria, tuple)
assert isinstance(stmt._returning, tuple)
stmt = stmt.where(t1.c.col2 == 5).returning(t1.c.col2)
if stmt_type.update:
self.assert_compile(
stmt,
"UPDATE table1 SET col1=:col1 WHERE table1.col1 = :col1_1 "
"AND table1.col2 = :col2_1 RETURNING table1.col1, table1.col2",
)
elif stmt_type.delete:
self.assert_compile(
stmt,
"DELETE FROM table1 WHERE table1.col1 = :col1_1 "
"AND table1.col2 = :col2_1 RETURNING table1.col1, table1.col2",
)
else:
stmt_type.fail()
|