File: sparql2sql.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (6903 lines) | stat: -rw-r--r-- 282,414 bytes parent folder | download | duplicates (2)
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
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
/*
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "sparql2sql.h"
#include "sqlparext.h"
#include "arith.h"
#include "sqlcmps.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "sparql_p.h"
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include "xmlparser.h"
#include "xmlparser_impl.h"
#ifdef __cplusplus
}
#endif
#include "xml_ecm.h"
#include "rdf_core.h"

/* PART 1. EXPRESSION TERM REWRITING */

/* Composing list of retvals instead of '*'.
\c trav_env_this is not used.
\c common_env points to dk_set_t of collected distinct variable names. */
int
sparp_gp_trav_list_expn_retvals (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  caddr_t varname;
  if (SPAR_VARIABLE != curr->type)
    return 0;
  varname = curr->_.var.vname;
  if (SPART_VARNAME_IS_GLOB(varname)) /* Query run-time env or external query param ? -- not in result-set */
    return SPAR_GPT_NODOWN;
  DO_SET (caddr_t, listed, (dk_set_t *)(common_env))
    {
      if (!strcmp (listed, varname))
        return SPAR_GPT_NODOWN;
    }
  END_DO_SET()
  t_set_push ((dk_set_t *)(common_env), varname);
  return SPAR_GPT_NODOWN;
}

int
sparp_gp_trav_list_subquery_retvals (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int ctr;
  SPART **options = curr->_.gp.options;
  if (SPAR_GP != curr->type)
    return 0;
  if (SELECT_L != curr->_.gp.subtype)
    return 0;
  DO_BOX_FAST (SPART *, retval, ctr, curr->_.gp.subquery->_.req_top.retvals)
    {
      caddr_t name;
      switch (SPART_TYPE (retval))
        {
          case SPAR_VARIABLE: name = retval->_.var.vname; break;
          case SPAR_ALIAS: name = retval->_.alias.aname; break;
          default: name = NULL;
        }
      if ((NULL != name) && !SPART_VARNAME_IS_GLOB(name) && (0 > dk_set_position_of_string (((dk_set_t *)(common_env))[0], name)))
        t_set_push ((dk_set_t *)(common_env), name);
    }
  END_DO_BOX_FAST;
  for (ctr = BOX_ELEMENTS_0 (options); 1 < ctr; ctr -= 2)
    {
      ptrlong key = ((ptrlong)(options[ctr-2]));
      SPART *val = options[ctr-1];
      caddr_t name = NULL;
      switch (key)
        {
        case OFFBAND_L: case SCORE_L: name = val->_.var.vname; break;
        case T_STEP_L: name = val->_.alias.aname; break;
        }
      if ((NULL != name) && !SPART_VARNAME_IS_GLOB(name) && (0 > dk_set_position_of_string (((dk_set_t *)(common_env))[0], name)))
        t_set_push ((dk_set_t *)(common_env), name);
    }
  return 0;
}

typedef struct list_nonaggregate_retvals_s {
  dk_set_t names;
  ptrlong agg_found;
  } list_nonaggregate_retvals_t;

int
sparp_gp_trav_list_nonaggregate_retvals (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  switch (curr->type)
    {
    case SPAR_VARIABLE:
      {
        caddr_t varname;
        varname = curr->_.var.vname;
        if (SPART_VARNAME_IS_GLOB(varname)) /* Query run-time env or external query param ? -- not in result-set */
          return SPAR_GPT_NODOWN;
        DO_SET (caddr_t, listed, (dk_set_t *)(common_env))
          {
            if (!strcmp (listed, varname))
              return SPAR_GPT_NODOWN;
          }
        END_DO_SET()
        t_set_push (&(((list_nonaggregate_retvals_t *)(common_env))->names), varname);
        return SPAR_GPT_NODOWN;
      }
    case SPAR_FUNCALL:
      if (curr->_.funcall.agg_mode)
        {
          ((list_nonaggregate_retvals_t *)(common_env))->agg_found = 1;
          return SPAR_GPT_NODOWN;
        }
      break;
    case SPAR_GP:
      return SPAR_GPT_NODOWN;
    }
  return SPAR_GPT_ENV_PUSH; /* To preserve sts_this->sts_curr_array and sts_this->sts_ofs_of_curr_in_array for wrapper of vars into fake MAX() */
}

static void
sparp_preprocess_obys (sparp_t *sparp, SPART *root)
{
  int oby_ctr;
  SPART **retvals = root->_.req_top.retvals;
  int rv_count = (IS_BOX_POINTER (retvals) ? BOX_ELEMENTS (retvals) : -1);
  DO_BOX_FAST (SPART *, oby, oby_ctr, root->_.req_top.order)
    {
      SPART *oby_expn = oby->_.oby.expn;
      if (IS_BOX_POINTER (oby_expn))
        {
          if (SPAR_VARIABLE == SPART_TYPE (oby_expn))
            {
              int rv_ctr;
              caddr_t vname = oby_expn->_.var.vname;
              for (rv_ctr = 0; rv_ctr < rv_count; rv_ctr++)
                {
                  SPART *rv = root->_.req_top.retvals[rv_ctr];
                  if ((SPAR_ALIAS == SPART_TYPE (rv)) && !strcmp (vname, rv->_.alias.aname))
                    {
                      oby->_.oby.expn = (SPART *)((ptrlong)(rv_ctr+1));
                      break;
                    }
                }
            }
        }
      else
        {
          long i = (ptrlong)(oby_expn);
          if (0 >= rv_count)
            spar_error (sparp, "SELECT query should contain explicit list of returned columns if ORDER BY refers to indexes of that columns");
          if ((0 >= i) || (rv_count < i))
            spar_error (sparp, "ORDER BY refers to resulting column index %ld, should be in range 1 to %d", i, rv_count );
        }
    }
  END_DO_BOX_FAST;
}

void
sparp_expand_top_retvals (sparp_t *sparp, SPART *query, int safely_copy_all_vars)
{
  sparp_env_t *env = sparp->sparp_env;
  caddr_t retselid = query->_.req_top.retselid;
  list_nonaggregate_retvals_t lnar;
  dk_set_t new_vars = NULL;
  SPART **retvals = query->_.req_top.retvals;
  sparp_preprocess_obys (sparp, query);
  lnar.agg_found = 0;
  lnar.names = NULL;
  if (IS_BOX_POINTER (retvals))
    {
      if (safely_copy_all_vars)
        query->_.req_top.orig_retvals = sparp_treelist_full_copy (sparp, retvals, query->_.req_top.pattern); /* No cloning equivs here but no equivs at this moment at all */
      else
        query->_.req_top.orig_retvals = (SPART **) t_box_copy ((box_t) retvals);
      if (0 == sparp->sparp_query_uses_aggregates)
        return;
      sparp_gp_localtrav_treelist (sparp, retvals,
        NULL, &lnar,
        NULL, NULL,
        sparp_gp_trav_list_nonaggregate_retvals, NULL, sparp_gp_trav_list_nonaggregate_retvals,
        NULL );
      if (NULL != query->_.req_top.having)
        {
          sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
          memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
          stss[0].sts_env = NULL;
          stss[1].sts_curr_array = NULL;
          stss[1].sts_ofs_of_curr_in_array = 0;
          sparp_gp_trav_int (sparp, query->_.req_top.having, stss+1, &lnar,
          NULL, NULL,
          sparp_gp_trav_list_nonaggregate_retvals, NULL, sparp_gp_trav_list_nonaggregate_retvals,
          NULL );
        }
      sparp_gp_localtrav_treelist (sparp, query->_.req_top.order,
        NULL, &lnar,
        NULL, NULL,
        sparp_gp_trav_list_nonaggregate_retvals, NULL, sparp_gp_trav_list_nonaggregate_retvals,
        NULL );
      if (0 == lnar.agg_found)
        return;
      if (NULL != query->_.req_top.groupings)
        {
          dk_set_t names_in_groupings = NULL;
          sparp_gp_localtrav_treelist (sparp, query->_.req_top.groupings,
            NULL, &names_in_groupings,
            sparp_gp_trav_list_subquery_retvals, NULL,
            sparp_gp_trav_list_expn_retvals, NULL, NULL,
            NULL );
          while (NULL != lnar.names)
            {
              caddr_t varname = (caddr_t)t_set_pop (&(lnar.names));
              if (0 > dk_set_position_of_string (names_in_groupings, varname))
                spar_error (sparp, "Variable ?%.200s is used in the result set outside aggregate and not mentioned in GROUP BY clause", varname);
            }
          return;
        }
      t_set_push (&(env->spare_selids), retselid);
      while (NULL != lnar.names)
        {
          caddr_t varname = (caddr_t)t_set_pop (&(lnar.names));
          SPART *var = spar_make_variable (sparp, varname);
          t_set_push (&new_vars, var);
        }
      t_set_pop (&(env->spare_selids));
      query->_.req_top.groupings = (SPART **)t_revlist_to_array_or_null (new_vars);
      return;
    }
  {
    sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
    memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
    sparp_gp_trav_int (sparp, query->_.req_top.pattern, stss+1, &(lnar.names),
      sparp_gp_trav_list_subquery_retvals, NULL,
      sparp_gp_trav_list_expn_retvals, NULL, NULL,
      NULL );
  }
  t_set_push (&(env->spare_selids), retselid);
  while (NULL != lnar.names)
    {
      caddr_t varname = (caddr_t)t_set_pop (&(lnar.names));
      SPART *var = spar_make_variable (sparp, varname);
      t_set_push (&new_vars, var);
    }
  t_set_pop (&(env->spare_selids));
  if ((SPART **)_STAR == retvals)
    {
      if (NULL == new_vars)
        {
          if (env->spare_signal_void_variables)
            spar_error (sparp, "The list of return values contains '*' but the pattern does not contain variables");
          else
            t_set_push (&new_vars, spartlist (sparp, 4, SPAR_ALIAS,
              t_box_num (1), t_box_dv_short_string ("_star_fake"), SSG_VALMODE_AUTO ) );
        }
      query->_.req_top.retvals = retvals = (SPART **)t_list_to_array (new_vars);
      if (safely_copy_all_vars)
        query->_.req_top.orig_retvals = sparp_treelist_full_copy (sparp, retvals, query->_.req_top.pattern); /* No cloning equivs here but no equivs at this moment at all */
      else
        query->_.req_top.orig_retvals = (SPART **) t_box_copy ((box_t) retvals);
    }
/*  else if ((SPART **)COUNT_L == old_retvals)
    {
      SPART *countagg;
      t_set_push (&new_vars, (caddr_t)((ptrlong)1));
      countagg = spar_make_funcall (sparp, 1, "SPECIAL::bif:COUNT", (SPART **)t_list_to_array (new_vars));
      sparp->sparp_expr->_.req_top.retvals = (SPART **)t_list (1, countagg);
    }*/
  else
    spar_internal_error (sparp, "sparp_" "expand_top_retvals () failed to process special result-set");
}

int sparp_gp_trav_wrap_vars_in_max (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  caddr_t varname;
  ssg_valmode_t native;
  if ((SPAR_FUNCALL == curr->type) && curr->_.funcall.agg_mode)
    return SPAR_GPT_NODOWN;
  if ((SPAR_ALIAS == curr->type) && !memcmp (curr->_.alias.aname, "ctor-", 5))
    return SPAR_GPT_NODOWN;
  if (SPAR_VARIABLE != curr->type) /* Not a variable ? -- nothing to do */
    return SPAR_GPT_ENV_PUSH; /* To preserve sts_this->sts_curr_array and sts_this->sts_ofs_of_curr_in_array for wrapper of vars into fake MAX() */
  varname = curr->_.var.vname;
  if (SPART_VARNAME_IS_GLOB(varname)) /* Query run-time env or external query param ? -- not in result-set */
    return SPAR_GPT_NODOWN;
  native = sparp_expn_native_valmode (sparp, curr);
  if (IS_BOX_POINTER (native) && native->qmfIsBijection)
    return SPAR_GPT_NODOWN;
  if (curr->_.var.rvr.rvrRestrictions & SPART_VARR_FIXED)
    return SPAR_GPT_NODOWN;
  sts_this->sts_curr_array[sts_this->sts_ofs_of_curr_in_array] =
    spartlist (sparp, 4, SPAR_ALIAS,
      spar_make_funcall (sparp, 1, t_box_dv_uname_string ("SPECIAL::bif:_LONG_MAX"), (SPART **)t_list (1, curr)),
      varname, SSG_VALMODE_AUTO );
  return SPAR_GPT_NODOWN;
}

void
sparp_wpar_retvars_in_max (sparp_t *sparp, SPART *query)
{
  SPART **retvals = query->_.req_top.retvals;
  const char *formatter, *agg_formatter, *agg_meta;
  caddr_t retvalmode_name, formatmode_name;
  if ((0 == sparp->sparp_query_uses_aggregates) || (0 != BOX_ELEMENTS_0 (query->_.req_top.groupings)))
    return;
  retvalmode_name = query->_.req_top.retvalmode_name;
  formatmode_name = query->_.req_top.formatmode_name;
  ssg_find_formatter_by_name_and_subtype (formatmode_name, query->_.req_top.subtype, &formatter, &agg_formatter, &agg_meta);
  if (((SELECT_L == query->_.req_top.subtype) ||
    (DISTINCT_L == query->_.req_top.subtype) ) &&
    (NULL == formatter) && (NULL == agg_formatter) &&
    ((NULL == retvalmode_name) ||
    (SSG_VALMODE_SQLVAL == ssg_find_valmode_by_name (retvalmode_name)) ) )
  return;
  sparp_gp_localtrav_treelist (sparp, retvals,
    NULL, NULL,
    NULL, NULL,
    sparp_gp_trav_wrap_vars_in_max, NULL, NULL,
    NULL );
}

int
sparp_gp_trav_preopt_in_gp (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      if (SELECT_L == curr->_.gp.subtype)
        {
          sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
          sparp_rewrite_retvals (sub_sparp, 1);
          sparp_up_from_sub (sparp, curr, sub_sparp);
        }
      return SPAR_GPT_ENV_PUSH;
    }
  return 0;
}

int
sparp_gp_trav_preopt_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_rewrite_retvals (sub_sparp, 1);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

void
sparp_rewrite_retvals (sparp_t *sparp, int safely_copy_retvals)
{
  rdf_grab_config_t *rgc = &(sparp->sparp_env->spare_src.ssrc_grab);
  SPART *root = sparp->sparp_expr;
  if (rgc->rgc_all)
    spar_add_rgc_vars_and_consts_from_retvals (sparp, root->_.req_top.retvals);
  if (safely_copy_retvals)
    root->_.req_top.expanded_orig_retvals = sparp_treelist_full_copy (sparp, root->_.req_top.retvals, root->_.req_top.pattern);
  else
    root->_.req_top.expanded_orig_retvals = (SPART **)t_box_copy ((caddr_t)(root->_.req_top.retvals));
/* Unlike spar_retvals_of_construct() that can be called during parsing,
spar_retvals_of_describe() should wait for obtaining all variables and then
sparp_expand_top_retvals () to process 'DESCRIBE * ...'. */
  if (DESCRIBE_L == root->_.req_top.subtype)
    {
      root->_.req_top.retvals =
        spar_retvals_of_describe (sparp,
          root->_.req_top.retvals,
          root->_.req_top.limit,
          root->_.req_top.offset );
    }
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_preopt_in_gp, NULL,
    NULL, NULL, sparp_gp_trav_preopt_expn_subq,
    NULL );
}

/* Composing counters of usages.
\c trav_env_this points to the innermost graph pattern.
\c common_env is not used. */

int sparp_gp_trav_cu_in_triples (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env);
int sparp_gp_trav_cu_out_triples_1 (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env);
int sparp_gp_trav_cu_out_triples_2 (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env);
int sparp_gp_trav_cu_in_expns (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env);
int sparp_gp_trav_cu_in_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env);
int sparp_gp_trav_cu_in_retvals (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env);

void
sparp_gp_trav_cu_in_options (sparp_t *sparp, SPART *gp, SPART *curr, SPART **options, void *common_env)
{
  int ctr;
  for (ctr = BOX_ELEMENTS (options); 1 < ctr; ctr -= 2)
    {
      ptrlong key = ((ptrlong)(options[ctr-2]));
      SPART *val = options[ctr-1];
      switch (key)
        {
        case OFFBAND_L: case SCORE_L:
          { if (SPART_VARR_GLOBAL & val->_.var.rvr.rvrRestrictions) spar_error (sparp, "Only plain variables can be used in OFFBAND_L or SCORE_L options, not parameters like ?%.50s", val->_.var.vname);
            sparp_equiv_get (sparp, gp, val, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_INS_VARIABLE | SPARP_EQUIV_ADD_GSPO_USE);
            break;
          }
        case T_STEP_L:
          {
            caddr_t name = val->_.alias.aname;
            sparp_equiv_get (sparp, gp, (SPART *)name, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_ADD_SUBQUERY_USE);
            break;
          }
        case SCORE_LIMIT_L: case T_MIN_L: case T_MAX_L:
          {
            sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
            memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
            stss[1].sts_ancestor_gp = gp;
            sparp_gp_trav_int (sparp, val, stss+1, common_env,
              sparp_gp_trav_cu_in_triples, sparp_gp_trav_cu_out_triples_1,
              sparp_gp_trav_cu_in_expns, NULL, sparp_gp_trav_cu_in_subq, NULL );
            break;
          }
        case T_IN_L: case T_OUT_L:
          {
            int v_ctr;
            DO_BOX_FAST (SPART *, v, v_ctr, val->_.list.items)
              {
                sparp_equiv_t *eq = sparp_equiv_get (sparp, gp, (SPART *)(v->_.var.vname), SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_GET_ASSERT);
                ptrlong *pos1_ptr = ((T_IN_L == key) ? &(eq->e_pos1_t_in) : &(eq->e_pos1_t_out));
                if ((0 != pos1_ptr[0]) && ((1+v_ctr) != pos1_ptr[0]))
                  spar_error (sparp, "Variable ?%.100s is used twice in %s option (directly or via equality with other variable)",
                    v->_.var.vname, ((T_IN_L == key) ? "T_IN" : "T_OUT") );
                pos1_ptr[0] = 1+v_ctr;
              }
            END_DO_BOX_FAST;
            break;
          }
        case SAME_AS_L: case SAME_AS_O_L: case SAME_AS_P_L: case SAME_AS_S_L:  case SAME_AS_S_O_L:
          {
            sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
            if (!IS_BOX_POINTER (val))
              break;
            memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
            stss[1].sts_ancestor_gp = gp;
            sparp_gp_trav_int (sparp, val, stss+1, common_env,
              sparp_gp_trav_cu_in_triples, sparp_gp_trav_cu_out_triples_1,
              sparp_gp_trav_cu_in_expns, NULL, sparp_gp_trav_cu_in_subq, NULL );
            break;
          }
        }
    }
}

int
sparp_gp_trav_cu_in_triples (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int fctr;
  SPART *gp = sts_this->sts_ancestor_gp;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      if ((SELECT_L == curr->_.gp.subtype) && (0 == curr->_.gp.equiv_count))
        {
          int ctr;
          sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
          sparp_rewrite_all (sub_sparp, 1);
          sparp_up_from_sub (sparp, curr, sub_sparp);
          DO_BOX_FAST (SPART *, retval, ctr, curr->_.gp.subquery->_.req_top.orig_retvals)
            {
              caddr_t name;
              switch (SPART_TYPE (retval))
                {
                  case SPAR_VARIABLE:
                    name = retval->_.var.vname;
                    curr->_.gp.subquery->_.req_top.orig_retvals[ctr] =
                      retval = spartlist (sparp, 4, SPAR_ALIAS, retval, name, SSG_VALMODE_AUTO);
                    break;
                  case SPAR_ALIAS:
                    name = retval->_.alias.aname; break;
                  default: name = NULL;
                }
              if (NULL == name)
                goto ignore_retval_name; /* see below */
              if (SPART_VARNAME_IS_GLOB(name))
                goto ignore_retval_name; /* see below */
              sparp_equiv_get (sparp, curr, (SPART *)name, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_ADD_SUBQUERY_USE);
ignore_retval_name: ;
            }
          END_DO_BOX_FAST;
          if (NULL != curr->_.gp.options)
            sparp_gp_trav_cu_in_options (sparp, curr, curr, curr->_.gp.options, common_env);
        }
      return SPAR_GPT_ENV_PUSH;
    case SPAR_TRIPLE: break;
    default: return 0;
    }
  if (UNION_L != gp->_.gp.subtype)
    {
      for (fctr = 0; fctr < SPART_TRIPLE_FIELDS_COUNT; fctr++)
        {
          SPART *fld = curr->_.triple.tr_fields[fctr];
          sparp_equiv_t *eq;
          switch (SPART_TYPE(fld))
            {
              case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL: break;
              default: continue;
            }
          if (OPTIONAL_L == curr->_.triple.subtype)
            continue;
          eq = sparp_equiv_get (sparp, gp, fld, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_INS_VARIABLE | SPARP_EQUIV_ADD_GSPO_USE);
          sparp_equiv_tighten (sparp, eq, &(fld->_.var.rvr), ~0);
        }
      if (NULL != curr->_.triple.options)
        sparp_gp_trav_cu_in_options (sparp, gp, curr, curr->_.triple.options, common_env);
    }
  if (UNION_L == gp->_.gp.subtype)
    {
      int eq_ctr;
      SPARP_FOREACH_GP_EQUIV (sparp, gp, eq_ctr, eq)
        {
          if ((0 < eq->e_gspo_uses) && (0 == eq->e_rvr.rvrRestrictions))
            {
	      int varctr;
              rdf_val_range_t acc;
              memset (&acc, 0, sizeof (rdf_val_range_t));
              acc.rvrRestrictions = SPART_VARR_CONFLICT;
              for (varctr = eq->e_var_count; varctr--; /*no step*/)
                {
                  SPART *var = eq->e_vars[varctr];
                  sparp_rvr_loose (sparp, &acc, &(var->_.var.rvr), ~SPART_VARR_NOT_NULL);
                  eq->e_rvr.rvrRestrictions |= (var->_.var.rvr.rvrRestrictions & (SPART_VARR_EXPORTED | SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL));
                }
              sparp_equiv_tighten (sparp, eq, &acc, ~0);
            }
        }
      END_SPARP_FOREACH_GP_EQUIV;
    }
  return SPAR_GPT_NODOWN;
}

int
sparp_gp_trav_cu_out_triples_1 (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_GP == SPART_TYPE(curr))
    {
      int eq_ctr;
      SPARP_FOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
        {
          int sub_ctr;
          eq->e_nested_bindings = 0;
          DO_BOX_FAST_REV (ptrlong, sub_idx, sub_ctr, eq->e_subvalue_idxs)
            {
              sparp_equiv_t *sub_eq = SPARP_EQUIV(sparp,sub_idx);
              if (SPARP_EQ_IS_ASSIGNED_LOCALLY(sub_eq))
                eq->e_nested_bindings += 1;
            }
          END_DO_BOX_FAST;
        }
      END_SPARP_FOREACH_GP_EQUIV;
    }
  return 0;
}

int
sparp_gp_trav_cu_out_triples_1_merge_recvs (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eq_ctr;
  sparp_gp_trav_cu_out_triples_1 (sparp, curr, sts_this, common_env);
  if (SPAR_GP != SPART_TYPE(curr))
    return 0;
  if (OPTIONAL_L == curr->_.gp.subtype)
    return 0;
  SPARP_FOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
    {
      int ctr_r1;
      DO_BOX_FAST_REV (ptrlong, recv1_idx, ctr_r1, eq->e_receiver_idxs)
        {
          int ctr_r2;
          for (ctr_r2 = BOX_ELEMENTS (eq->e_receiver_idxs); --ctr_r2 > ctr_r1; /* no step */)
            {
              int recv2_idx = eq->e_receiver_idxs[ctr_r2];
              SPART *recv_gp;
              sparp_equiv_t *recv1_eq = SPARP_EQUIV(sparp,recv1_idx);
              sparp_equiv_t *recv2_eq = SPARP_EQUIV(sparp,recv2_idx);
              if (recv1_eq == recv2_eq)
                spar_internal_error (sparp, "sparp_" "gp_trav_cu_out_triples_1_merge_recvs (): duplicate receiver");
              recv_gp = recv1_eq->e_gp;
              if (recv_gp != recv2_eq->e_gp)
                spar_internal_error (sparp, "sparp_" "gp_trav_cu_out_triples_1_merge_recvs (): receivers in different gps");
              if (UNION_L == recv_gp->_.gp.subtype)
                return 0;
              sparp_equiv_merge (sparp, recv1_eq, recv2_eq);
            }
        }
      END_DO_BOX_FAST;
    }
  END_SPARP_FOREACH_GP_EQUIV;
  return 0;
}


int
sparp_gp_trav_cu_out_triples_2 (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_GP == SPART_TYPE(curr))
    {
      int eq_ctr;
      SPARP_FOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
        {
          int varnamectr;
          DO_BOX_FAST (caddr_t, varname, varnamectr, eq->e_varnames)
            {
              t_set_push_new_string ((dk_set_t *)common_env, varname);
            }
          END_DO_BOX_FAST;
        }
      END_SPARP_FOREACH_GP_EQUIV;
    }
  return 0;
}

int
sparp_gp_trav_cu_in_expns (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART *gp = sts_this->sts_ancestor_gp;
  sparp_equiv_t *eq;
  switch (SPART_TYPE(curr))
    {
    case SPAR_VARIABLE: break;
    case SPAR_BLANK_NODE_LABEL: break;
    default: return 0;
    }
  eq = sparp_equiv_get (sparp, gp, curr,
    SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_INS_VARIABLE |
    ((NULL == curr->_.var.tabid) ? SPARP_EQUIV_ADD_CONST_READ : SPARP_EQUIV_ADD_GSPO_USE) );
  eq->e_rvr.rvrRestrictions |= (curr->_.var.rvr.rvrRestrictions & (SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL)); /* sparp_equiv_tighten (sparp, eq, &(curr->_.var.rvr), ~0); A variable in an expression can not bring knowledge by itself */
  return 0;
}

int
sparp_gp_trav_cu_in_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  dk_set_t all_sub_vars = NULL;
  sparp_t *sub_sparp;
  sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_count_usages (sub_sparp, &all_sub_vars);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  DO_SET (caddr_t, varname, &all_sub_vars)
    {
      SPART *rel_gp;
      sparp_equiv_get (sparp, curr, (SPART *)varname, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_ADD_OPTIONAL_READ);
      if (NULL != sts_this->sts_ancestor_gp)
        rel_gp = sts_this->sts_ancestor_gp;
      else
        rel_gp = sparp->sparp_expr->_.req_top.pattern;
      sparp_equiv_get (sparp, rel_gp, (SPART *)varname, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_ADD_OPTIONAL_READ);
    }
  END_DO_SET ()
  sparp_gp_trav_cu_out_triples_1 (sparp, curr, sts_this, common_env);
  return 0;
}

int
sparp_gp_trav_cu_in_retvals (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART *top_gp = sparp->sparp_expr->_.req_top.pattern;
  sparp_equiv_t *eq;
  switch (SPART_TYPE(curr))
    {
    case SPAR_VARIABLE: break;
    case SPAR_BLANK_NODE_LABEL: break;
    default: return 0;
    }
  curr->_.var.selid = top_gp->_.gp.selid;
  curr->_.var.tabid = NULL;
  eq = sparp_equiv_get (sparp, top_gp, curr, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_INS_VARIABLE | SPARP_EQUIV_ADD_CONST_READ);
  curr->_.var.equiv_idx = eq->e_own_idx;
  curr->_.var.rvr.rvrRestrictions |= SPART_VARR_EXPORTED /* This is redundand: if these bits are set, why set them again: | (curr->_.var.rvr.rvrRestrictions & (SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL))*/ ;
  sparp_equiv_tighten (sparp, eq, &(curr->_.var.rvr), ~0);
  return 0;
}

void
sparp_count_usages (sparp_t *sparp, dk_set_t *optvars_ret)
{
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, optvars_ret,
    sparp_gp_trav_cu_in_triples, sparp_gp_trav_cu_out_triples_1,
    sparp_gp_trav_cu_in_expns, NULL, sparp_gp_trav_cu_in_subq,
    NULL );
  sparp_trav_out_clauses (sparp, sparp->sparp_expr, optvars_ret,
        NULL, NULL,
        sparp_gp_trav_cu_in_retvals, NULL, sparp_gp_trav_cu_in_subq,
        NULL );
  if (NULL != optvars_ret)
    sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, optvars_ret,
      NULL, sparp_gp_trav_cu_out_triples_2,
      NULL, NULL, sparp_gp_trav_cu_in_subq,
      NULL );
}

int
sparp_tree_returns_ref (sparp_t *sparp, SPART *tree)
{
  switch (SPART_TYPE (tree))
    {
    case SPAR_QNAME: return 1;
    case SPAR_BUILT_IN_CALL:
      if (IRI_L == tree->_.builtin.btype)
        return 1;
      break;
    }
  return 0;
}

static int
sparp_expn_rside_rank (SPART *tree)
{
  switch (SPART_TYPE (tree))
    {
    case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
      {
        ptrlong restr = tree->_.var.rvr.rvrRestrictions;
        if (SPARP_FIXED_AND_NOT_NULL (restr))
          return 0x40;
        if (SPART_VARR_FIXED & restr)
          return 0x20;
        if ((SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL) & restr)
          return 0x10;
        if (SPART_VARR_IS_IRI & restr)
          return 0x8;
        if (SPART_VARR_IS_REF & restr)
          return 0x4;
        if (SPART_VARR_IS_LIT & restr)
          return 0x2;
        return 0x0;
      }
    case SPAR_LIT: return 0x400;
    case SPAR_QNAME: return 0x1000;
    case SPAR_BUILT_IN_CALL: return 0x4000;
    case SPAR_FUNCALL: return 0x10000;
    }
  return 0x20000;
}

void
sparp_rotate_comparisons_by_rank (SPART *filt)
{
  switch (SPART_TYPE (filt))
    {
    case SPAR_BOP_EQ: /* no break */
    case BOP_EQ:
    case BOP_NEQ:
    case BOP_LT:
    case BOP_LTE:
    case BOP_GT:
    case BOP_GTE:
      {
        SPART *l = filt->_.bin_exp.left;
        SPART *r = filt->_.bin_exp.right;
        int lrrank = sparp_expn_rside_rank (l);
        int rrrank = sparp_expn_rside_rank (r);
        if (lrrank > rrrank)
          {
            filt->_.bin_exp.right = l;
            filt->_.bin_exp.left = r;
            switch (SPART_TYPE (filt))
              {
              case BOP_LT:	filt->type = BOP_GT; break;
              case BOP_LTE:	filt->type = BOP_GTE; break;
              case BOP_GT:	filt->type = BOP_LT; break;
              case BOP_GTE:	filt->type = BOP_LTE; break;
              }
          }
        break;
      }
    case SPAR_BUILT_IN_CALL:
      if (SPAR_BIF_SAMETERM == filt->_.builtin.btype)
        {
          SPART *l = filt->_.builtin.args[0];
          SPART *r = filt->_.builtin.args[1];
          int lrrank = sparp_expn_rside_rank (l);
          int rrrank = sparp_expn_rside_rank (r);
          if (lrrank > rrrank)
            {
              filt->_.builtin.args[0] = r;
              filt->_.builtin.args[1] = l;
            }
        }
      break;
    default:
      break;
    }
}

typedef struct so_BOP_OR_filter_ctx_s
{
  sparp_t *bofc_sparp;			/*!< parser/compiler context, to not pass an extra argument */
  SPART *bofc_var_sample;		/*!< Common optimizable variable in question */
  dk_set_t bofc_strings;		/*!< Collected string values, they may be convert into sprintf format strings to tighten equiv of the common variable */
  ptrlong bofc_not_optimizable;		/*!< Teh filter is of complicated form or the variable is not common or global */
  ptrlong bofc_can_be_iri;		/*!< Flag if there's at least equality to a IRI */
  ptrlong bofc_can_be_string;		/*!< Flag if there's at least equality to a literal string */
  ptrlong bofc_can_be_nonstringlit;	/*!< Flag if there's at least equality to a non-string literal */
} so_BOP_OR_filter_ctx_t;

int
sparp_optimize_BOP_OR_filter_walk_lvar (SPART *lvar, so_BOP_OR_filter_ctx_t *ctx)
{
  if (SPAR_VARIABLE != SPART_TYPE (lvar))
    { /* for optimization, there should be variable at left */
      ctx->bofc_not_optimizable = 1;
      return 1;
    }
  if (NULL == ctx->bofc_var_sample)
    ctx->bofc_var_sample = lvar;
  else if (strcmp (ctx->bofc_var_sample->_.var.vname, lvar->_.var.vname))
    { /* for optimization, there should be _same_ variable at left */
      ctx->bofc_not_optimizable = 1;
      return 1;
    }
  return 0;
}

int
sparp_optimize_BOP_OR_filter_walk_rexpn (SPART *rexpn, so_BOP_OR_filter_ctx_t *ctx)
{
  caddr_t lit_val;
  switch (SPART_TYPE (rexpn))
    {
    case SPAR_QNAME:
      ctx->bofc_can_be_iri++;
      dk_set_push (&(ctx->bofc_strings), rexpn->_.lit.val);
      return 0;
    case SPAR_LIT:
      lit_val = rexpn->_.lit.val;
      if (!IS_STRING_DTP (DV_TYPE_OF (lit_val)))
        ctx->bofc_can_be_nonstringlit++;
      else
        {
          ctx->bofc_can_be_string++;
          dk_set_push (&(ctx->bofc_strings), lit_val);
        }
      return 0;
/* !!! TBD support for constant expressions here */
    }
  ctx->bofc_not_optimizable = 1;
  return 1;
}

int
sparp_merge_BOP_OR_of_INs_prep (SPART *tree, so_BOP_OR_filter_ctx_t *ctx, SPART **var_ret, SPART ***vals_ret, int *val_count_ret)
{
  switch (SPART_TYPE (tree))
    {
    case BOP_EQ:
      var_ret[0] = tree->_.bin_exp.left;
      vals_ret[0] = &(tree->_.bin_exp.right);
      val_count_ret[0] = 1;
      break;
    case SPAR_BUILT_IN_CALL:
      if (IN_L != tree->_.builtin.btype)
        return 1;
      var_ret[0] = tree->_.builtin.args[0];
      vals_ret[0] = tree->_.builtin.args+1;
      val_count_ret[0] = BOX_ELEMENTS (tree->_.builtin.args) - 1;
      break;
    default:
      return 1;
    }
  if (SPAR_VARIABLE != SPART_TYPE (var_ret[0]))
    return 1;
  return 0;
}

SPART *
sparp_merge_BOP_OR_of_INs (SPART *first, SPART *second, so_BOP_OR_filter_ctx_t *ctx)
{
  sparp_t *sparp = ctx->bofc_sparp;
  SPART *first_var, *second_var;
  SPART **first_vals, **second_vals;
  SPART **res_IN_args;
  int first_val_count, second_val_count;
  if (sparp_merge_BOP_OR_of_INs_prep (first, ctx, &first_var, &first_vals, &first_val_count))
    return NULL;
  if (sparp_merge_BOP_OR_of_INs_prep (second, ctx, &second_var, &second_vals, &second_val_count))
    return NULL;
  if (strcmp (first_var->_.var.vname, second_var->_.var.vname))
    return NULL;
  res_IN_args = (SPART **)t_alloc_box ((1 + first_val_count + second_val_count) * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
  res_IN_args[0] = first_var;
  memcpy (res_IN_args + 1, first_vals, first_val_count * sizeof (caddr_t));
  memcpy (res_IN_args + 1 + first_val_count, second_vals, second_val_count * sizeof (caddr_t));
  sparp_equiv_remove_var (sparp, SPARP_EQUIV (sparp, second_var->_.var.equiv_idx), second_var);
  return sparp_make_builtin_call (sparp, IN_L, res_IN_args);
}

SPART *
sparp_optimize_BOP_OR_filter_walk (SPART *filt, so_BOP_OR_filter_ctx_t *ctx)
{
  ptrlong filt_type = SPART_TYPE (filt);
  if (THR_IS_STACK_OVERFLOW (THREAD_CURRENT_THREAD, &filt_type, 8000))
    spar_error (ctx->bofc_sparp, "Stack overflow");
  switch (filt_type)
    {
    case BOP_OR:
      {
        SPART *new_l = sparp_optimize_BOP_OR_filter_walk (filt->_.bin_exp.left, ctx);
        SPART *new_r = sparp_optimize_BOP_OR_filter_walk (filt->_.bin_exp.right, ctx);
        SPART *new_merged;
        if (BOP_OR != SPART_TYPE (new_r))
          {
            if (BOP_OR != SPART_TYPE (new_l))
              {
                new_merged = sparp_merge_BOP_OR_of_INs (new_l, new_r, ctx);
                if (NULL != new_merged)
                  return new_merged;
              }
            else
              {
                new_merged = sparp_merge_BOP_OR_of_INs (new_l->_.bin_exp.left, new_r, ctx);
                if (NULL != new_merged)
                  {
                    new_l->_.bin_exp.left = new_merged;
                    return new_l;
                  }
                new_merged = sparp_merge_BOP_OR_of_INs (new_l->_.bin_exp.right, new_r, ctx);
                if (NULL != new_merged)
                  {
                    new_l->_.bin_exp.right = new_merged;
                    return new_l;
                  }
              }
          }
        filt->_.bin_exp.left = new_l;
        filt->_.bin_exp.right = new_r;
        return filt;
      }
    case SPAR_BUILT_IN_CALL:
      if (IN_L == filt->_.builtin.btype)
        {
          int argctr;
          if (sparp_optimize_BOP_OR_filter_walk_lvar (filt->_.builtin.args[0], ctx))
            goto cannot_optimize; /* see below */
          for (argctr = BOX_ELEMENTS (filt->_.builtin.args); 0 < --argctr; /* no step */)
            {
              if (sparp_optimize_BOP_OR_filter_walk_rexpn (filt->_.builtin.args[argctr], ctx))
                goto cannot_optimize; /* see below */
            }
          return filt;
        }
      if (SPAR_BIF_SAMETERM != filt->_.builtin.btype)
        goto cannot_optimize; /* see below */
      /* no break, try get optimization hints like it is BOP_EQ */
    case BOP_EQ: /* No case for SPAR_BOP_EQ ! */
      sparp_rotate_comparisons_by_rank (filt);
      if (sparp_optimize_BOP_OR_filter_walk_lvar (filt->_.bin_exp.left, ctx))
        goto cannot_optimize; /* see below */
      if (sparp_optimize_BOP_OR_filter_walk_rexpn (filt->_.bin_exp.right, ctx))
        goto cannot_optimize; /* see below */
      return filt;
    default: ;
    }
cannot_optimize:
/* The very natural default is to say 'cannot optimize' and escape */
  ctx->bofc_not_optimizable = 1;
  return filt;
}

/*! Processes of simple filters inside BOP_OR (or top-level IN_L) that introduce restrictions on variables. */
SPART *
sparp_optimize_BOP_OR_filter (sparp_t *sparp, SPART *curr, SPART *filt)
{
  sparp_equiv_t *eq_l;
  rdf_val_range_t new_rvr;
  so_BOP_OR_filter_ctx_t ctx;
  int sff_ctr;
  SPART *new_filt;
  memset (&ctx, 0, sizeof (so_BOP_OR_filter_ctx_t));
  ctx.bofc_sparp = sparp;
  new_filt = sparp_optimize_BOP_OR_filter_walk (filt, &ctx);
  if (ctx.bofc_not_optimizable)
    {
      while (NULL != ctx.bofc_strings) dk_set_pop (&(ctx.bofc_strings));
      return new_filt;
    }
  eq_l = sparp_equiv_get (sparp, curr, ctx.bofc_var_sample, 0);
  memset (&new_rvr, 0, sizeof (rdf_val_range_t));
  new_rvr.rvrRestrictions |= SPART_VARR_NOT_NULL;
  if (0 == ctx.bofc_can_be_iri)
    new_rvr.rvrRestrictions |= SPART_VARR_IS_LIT;
  if ((0 == ctx.bofc_can_be_string) && (0 == ctx.bofc_can_be_nonstringlit))
    new_rvr.rvrRestrictions |= SPART_VARR_IS_REF | SPART_VARR_IS_IRI;
  if (0 == ctx.bofc_can_be_nonstringlit)
    {
      new_rvr.rvrRestrictions |= SPART_VARR_SPRINTFF;
      new_rvr.rvrSprintffCount = dk_set_length (ctx.bofc_strings);
      new_rvr.rvrSprintffs = (ccaddr_t *)t_alloc_box (new_rvr.rvrSprintffCount * sizeof(caddr_t), DV_ARRAY_OF_POINTER);
      for (sff_ctr = new_rvr.rvrSprintffCount; sff_ctr--; /* no step */)
        new_rvr.rvrSprintffs[sff_ctr] = sprintff_from_strg (dk_set_pop (&(ctx.bofc_strings)), 1);
    }
  else
    {
      while (NULL != ctx.bofc_strings) dk_set_pop (&(ctx.bofc_strings));
    }
  sparp_equiv_tighten (sparp, eq_l, &new_rvr, ~0);
/* TBD: it is possible to remove branches of OR that contradicts with known restrictions of \c eq_l */
  return new_filt;
}

int
sparp_equiv_contains_t_io (sparp_t *sparp, sparp_equiv_t *eq)
{
  int sub_ctr;
  if ((0 != eq->e_pos1_t_in) || (0 != eq->e_pos1_t_in))
    return 1;
  DO_BOX_FAST_REV (ptrlong, sub_idx, sub_ctr, eq->e_subvalue_idxs)
    {
      sparp_equiv_t *sub = SPARP_EQUIV (sparp, sub_idx);
      if (sparp_equiv_contains_t_io (sparp, sub))
        return 1;
    }
  END_DO_BOX_FAST_REV;
  return 0;
}

/*! For an equality in group \c curr between member of \c eq_l and expression \c r,
the function restricts \c eq_l or even merges it with variable of other equiv.
\returns SPART_VARR_XXX bits that can be added to eq_l->e_replaces_filter if equality is no longer needed due to merge, 0 otherwise */
int
spar_var_eq_to_equiv (sparp_t *sparp, SPART *curr, sparp_equiv_t *eq_l, SPART *r)
{
  int ret = 0;
  int flags = 0;
  ptrlong tree_restr_bits = sparp_restr_bits_of_expn (sparp, r);
  eq_l->e_rvr.rvrRestrictions |= SPART_VARR_NOT_NULL | (tree_restr_bits & (
    SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_IS_BLANK |
    SPART_VARR_IS_LIT | SPART_VARR_LONG_EQ_SQL |
    SPART_VARR_NOT_NULL | SPART_VARR_ALWAYS_NULL ) );
  switch (SPART_TYPE (r))
    {
    case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
      {
        sparp_equiv_t *eq_r = sparp_equiv_get (sparp, curr, r, 0);
        eq_l->e_rvr.rvrRestrictions |= SPART_VARR_NOT_NULL;
        ret = sparp_equiv_merge (sparp, eq_l, eq_r);
        if (
          (SPARP_EQUIV_MERGE_OK != ret) &&
          (SPARP_EQUIV_MERGE_CONFLICT != ret) &&
          (SPARP_EQUIV_MERGE_DUPE != ret) )
          return 0;
        if (sparp_equiv_contains_t_io (sparp, eq_r))
          return 0;
        flags = SPART_VARR_EQ_VAR;
        break;
      }
    case SPAR_LIT: case SPAR_QNAME:
      {
        int old_rvr = eq_l->e_rvr.rvrRestrictions;
        ret = sparp_equiv_restrict_by_constant (sparp, eq_l, NULL, r);
        if (
          (SPARP_EQUIV_MERGE_OK != ret) &&
          (SPARP_EQUIV_MERGE_CONFLICT != ret) &&
          (SPARP_EQUIV_MERGE_DUPE != ret) )
          return 0;
        flags = SPART_VARR_FIXED | (eq_l->e_rvr.rvrRestrictions & ~old_rvr);
        break;
      }
    case SPAR_BUILT_IN_CALL:
      {
        switch (r->_.builtin.btype)
          {
          case SPAR_BIF_STR: eq_l->e_rvr.rvrRestrictions |= SPART_VARR_IS_LIT | SPART_VARR_NOT_NULL; break;
          case IRI_L: eq_l->e_rvr.rvrRestrictions |= SPART_VARR_IS_REF | SPART_VARR_NOT_NULL; break;
          default:
            {
              const sparp_bif_desc_t *bif_desc = sparp_bif_descs + r->_.builtin.desc_ofs;
              if ((SSG_VALMODE_NUM == bif_desc->sbd_ret_valmode) || (SSG_VALMODE_BOOL == bif_desc->sbd_ret_valmode))
                eq_l->e_rvr.rvrRestrictions |= SPART_VARR_LONG_EQ_SQL;
            }
          }
        return 0;
      }
     default: return 0;
    }
  if (sparp_equiv_contains_t_io (sparp, eq_l))
    return 0;
  return flags | SPART_VARR_NOT_NULL;
}

/* For an != in group \c curr between member of \c eq_l and expression \c r,
the function may sometimes set conflict to \c eq_l or find that the filter is useless (say, inequality between literal and IRI)
\returns SPART_VARR_CONFLICT if != is no longer needed due to added conflict, -1 if proven useless, 0 otherwise */
int
spar_var_bangeq_to_equiv (sparp_t *sparp, SPART *curr, sparp_equiv_t *eq_l, SPART *r)
{
  ptrlong tree_restr_bits = sparp_restr_bits_of_expn (sparp, r);
  if ((eq_l->e_rvr.rvrRestrictions & SPART_VARR_IS_REF) && (tree_restr_bits & SPART_VARR_IS_LIT))
    return -1;
  if ((eq_l->e_rvr.rvrRestrictions & SPART_VARR_IS_LIT) && (tree_restr_bits & SPART_VARR_IS_REF))
    return -1;
  if ((eq_l->e_rvr.rvrRestrictions & SPART_VARR_IS_IRI) && (tree_restr_bits & SPART_VARR_IS_BLANK))
    return -1;
  if ((eq_l->e_rvr.rvrRestrictions & SPART_VARR_IS_BLANK) && (tree_restr_bits & SPART_VARR_IS_IRI))
    return -1;
  if (!(eq_l->e_rvr.rvrRestrictions & SPART_VARR_FIXED))
    return 0;
  switch (SPART_TYPE (r))
    {
    case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
      {
        sparp_equiv_t *eq_r = sparp_equiv_get (sparp, curr, r, 0);
        if (!(eq_r->e_rvr.rvrRestrictions & SPART_VARR_FIXED))
          return 0;
        if (sparp_equivs_have_same_fixedvalue (sparp, eq_l, eq_r))
          {
            eq_l->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
            eq_r->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
            return SPART_VARR_CONFLICT;
          }
        break;
      }
    case SPAR_LIT: case SPAR_QNAME:
      {
        if (sparp_fixedvalues_equal (sparp, (SPART *)(eq_l->e_rvr.rvrFixedValue), r))
          {
            eq_l->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
            return SPART_VARR_CONFLICT;
          }
        break;
      }
    }
  return 0;
}

/* Processing of simple filters that introduce restrictions on variables
\c trav_env_this is not used.
\c common_env is not used. */
int
sparp_filter_to_equiv (sparp_t *sparp, SPART *curr, SPART *filt)
{
  int flags;
/* We rotate comparisons before anything else */
  sparp_rotate_comparisons_by_rank (filt);
/* Now filters can be processed */
  switch (SPART_TYPE (filt))
    {
    case BOP_EQ: /* No case for SPAR_BOP_EQ ! Indeed, this is the main reason for introducing SPAR_BOP_EQ at all */
      {
        SPART *l = filt->_.bin_exp.left;
        SPART *r = filt->_.bin_exp.right;
        switch (SPART_TYPE (l))
          {
          case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
            {
              sparp_equiv_t *eq_l = sparp_equiv_get (sparp, curr, l, 0);
              flags = spar_var_eq_to_equiv (sparp, curr, eq_l, r);
              if (flags)
                {
                  eq_l->e_replaces_filter |= flags;
                  return 1;
                }
              return 0;
            }
          case SPAR_QNAME:
            {
              caddr_t lval = l->_.lit.val;
              if ((SPAR_BUILT_IN_CALL == SPART_TYPE (r)) &&
                (DATATYPE_L == r->_.builtin.btype) )
                {
                  SPART *rarg1 = r->_.builtin.args[0];
	          if (SPAR_IS_BLANK_OR_VAR (rarg1))
                    {
                      sparp_equiv_t *rarg1_eq = sparp_equiv_get (sparp, curr, rarg1, 0);
                      flags = SPART_VARR_NOT_NULL;
                      if (
                        (lval == uname_xmlschema_ns_uri_hash_boolean) ||
                        (lval == uname_xmlschema_ns_uri_hash_string) )
                        {
                          flags |= SPART_VARR_IS_LIT;
                        }
                      else if (
                        (lval == uname_xmlschema_ns_uri_hash_date) ||
                        (lval == uname_xmlschema_ns_uri_hash_dateTime) ||
                        (lval == uname_xmlschema_ns_uri_hash_decimal) ||
                        (lval == uname_xmlschema_ns_uri_hash_double) ||
                        (lval == uname_xmlschema_ns_uri_hash_float) ||
                        (lval == uname_xmlschema_ns_uri_hash_integer) ||
                        (lval == uname_xmlschema_ns_uri_hash_time) )
                        {
                          flags |= SPART_VARR_IS_LIT | SPART_VARR_LONG_EQ_SQL;
                        }
                      rarg1_eq->e_rvr.rvrRestrictions |= flags;
                      rarg1_eq->e_replaces_filter |= flags;
                    }
                  return 0;
                }
            }
          case SPAR_LIT:
            {
              caddr_t str_lval = NULL;
              switch (DV_TYPE_OF (l))
                {
                case DV_ARRAY_OF_POINTER:
                  str_lval = l->_.lit.val;
                  if (DV_STRING != DV_TYPE_OF (str_lval))
                    str_lval = NULL;
                  break;
                case DV_STRING:
                  str_lval = (caddr_t)l;
                  break;
                }
              if ((NULL != str_lval) && (SPAR_BUILT_IN_CALL == SPART_TYPE (r)) && (SPAR_BIF_STR == r->_.builtin.btype))
                {
                  SPART *rarg1 = r->_.builtin.args[0];
                  if (SPAR_IS_BLANK_OR_VAR (rarg1))
                    {
                      sparp_equiv_t *rarg1_eq = sparp_equiv_get (sparp, curr, rarg1, 0);
                      flags = SPART_VARR_NOT_NULL;
                      rarg1_eq->e_rvr.rvrRestrictions |= flags;
                      rarg1_eq->e_replaces_filter |= flags;
                      if (SPART_VARR_IS_REF & rarg1_eq->e_rvr.rvrRestrictions)
                        {
                          int old_rvr = rarg1_eq->e_rvr.rvrRestrictions;
                          int restr_ret;
                          SPART *lval_tmp_qname = spartlist (sparp, 2, SPAR_QNAME, t_box_dv_uname_nchars (str_lval, box_length (str_lval)-1));
                          restr_ret = sparp_equiv_restrict_by_constant (sparp, rarg1_eq, NULL, lval_tmp_qname);
                          if (
                            (SPARP_EQUIV_MERGE_OK != restr_ret) &&
                            (SPARP_EQUIV_MERGE_CONFLICT != restr_ret) &&
                            (SPARP_EQUIV_MERGE_DUPE != restr_ret) )
                            return 0;
                          flags = SPART_VARR_FIXED | (rarg1_eq->e_rvr.rvrRestrictions & ~old_rvr);
/* no need in <code>if (sparp_equiv_contains_t_io (sparp, eq_l)) return 0;</code> as it is written in spar_var_eq_to_equiv(),
because const=str(var) is never recognized as a special condition on t_in or t_out variables. */
/* no need in <code>rarg1_eq->e_rvr.rvrRestrictions |= flags;</code>, because it is set in sparp_equiv_restrict_by_constant() above */
                          rarg1_eq->e_replaces_filter |= flags;
                          return 1;
                        }
                    }
                  return 0;
                }
            }
          }
        break;
      }
    case BOP_NEQ:
      {
        SPART *l = filt->_.bin_exp.left;
        SPART *r = filt->_.bin_exp.right;
        switch (SPART_TYPE (l))
          {
          case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
            {
              sparp_equiv_t *eq_l = sparp_equiv_get (sparp, curr, l, 0);
              flags = spar_var_bangeq_to_equiv (sparp, curr, eq_l, r);
              if (flags)
                {
                  if (-1 != flags)
                    eq_l->e_replaces_filter |= flags;
                  return 1;
                }
              return 0;
            }
          }
        break;
      }
    case BOP_NOT: break;
    case SPAR_BUILT_IN_CALL:
      {
        SPART *arg1 = filt->_.builtin.args[0];
        sparp_equiv_t *arg1_eq;
        if (SPAR_IS_BLANK_OR_VAR (arg1))
          arg1_eq = sparp_equiv_get (sparp, curr, arg1, 0);
        else
          break;
        switch (filt->_.builtin.btype)
          {
          case SPAR_BIF_ISIRI:
          case SPAR_BIF_ISURI:
            flags = SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_NOT_NULL;
            arg1_eq->e_rvr.rvrRestrictions |= flags;
            arg1_eq->e_replaces_filter |= flags;
            return 1;
          case SPAR_BIF_ISBLANK:
            flags = SPART_VARR_IS_REF | SPART_VARR_IS_BLANK | SPART_VARR_NOT_NULL;
            arg1_eq->e_rvr.rvrRestrictions |= flags;
            arg1_eq->e_replaces_filter |= flags;
            return 1;
          case SPAR_BIF_ISREF:
            flags = SPART_VARR_IS_REF | SPART_VARR_NOT_NULL;
            arg1_eq->e_rvr.rvrRestrictions |= flags;
            arg1_eq->e_replaces_filter |= flags;
            return 1;
          case SPAR_BIF_ISLITERAL:
            flags = SPART_VARR_IS_LIT | SPART_VARR_NOT_NULL;
            arg1_eq->e_rvr.rvrRestrictions |= flags;
            arg1_eq->e_replaces_filter |= flags;
            return 1;
          case SPAR_BIF_ISNUMERIC:
            flags = SPART_VARR_IS_LIT | SPART_VARR_LONG_EQ_SQL | SPART_VARR_NOT_NULL;
            arg1_eq->e_rvr.rvrRestrictions |= flags;
            arg1_eq->e_replaces_filter |= flags;
            break;
          case BOUND_L:
            flags = SPART_VARR_NOT_NULL;
            arg1_eq->e_rvr.rvrRestrictions |= flags;
            arg1_eq->e_replaces_filter |= flags;
            return 1;
          case SPAR_BIF_SAMETERM:
            {
              SPART *arg2 = filt->_.builtin.args[1];
              spar_var_eq_to_equiv (sparp, curr, arg1_eq, arg2); /* No return because sameTerm is more strict than merge of equivs */
              break;
            }
        }
      break;
      }
    default: break;
    }
  return 0;
}

int
sparp_gp_trav_restrict_by_simple_filters_gp_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int fctr;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      break;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN;
    default: return 0;
    }
/* Note that glued filters do not participate in this optimization, otherwise
select * where { graph <g1> { ?s1 ?p1 ?o1 } optional { graph <g2> { ?s2 ?p2 ?o2 } filter (?o1 = <const>) }}
become an equivalent of
select * where { graph <g1> { ?s1 ?p1 ?o1 . filter (?o1 = <const>) } optional { graph <g2> { ?s2 ?p2 ?o2 } filter (?o1 = <const>) }}
*/
  for (fctr = BOX_ELEMENTS (curr->_.gp.filters) - curr->_.gp.glued_filters_count; fctr--; /* no step */)
    { /* The descending order of fctr values is important -- note possible sparp_gp_detach_filter () */
      SPART *filt = curr->_.gp.filters[fctr];
      int ret;
      if (BOP_OR == SPART_TYPE (filt))
        {
          SPART *new_filt = sparp_optimize_BOP_OR_filter (sparp, curr, filt);
          if (NULL == new_filt)
            sparp_gp_detach_filter (sparp, curr, fctr, NULL);
          else
            curr->_.gp.filters[fctr] = new_filt;
          continue;
        }
      ret = sparp_filter_to_equiv (sparp, curr, filt);
      if (0 != ret)
        sparp_gp_detach_filter (sparp, curr, fctr, NULL);
    }
  return 0;
}

int
sparp_gp_trav_restrict_by_simple_filters_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_continue_gp_trav_in_sub (sparp, curr, common_env);
  return 0;
}


void
sparp_restrict_by_simple_filters (sparp_t *sparp)
{
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_restrict_by_simple_filters_gp_in, NULL,
    NULL, NULL, sparp_gp_trav_restrict_by_simple_filters_expn_subq,
    NULL );
}


/* Composing equivs for common names without local variables.
\c trav_env_this points to dk_set_t of names.
\c common_env is not used. */

int
sparp_gp_trav_make_common_eqs_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      /*000 if (UNION_L == curr->_.gp.subtype)
        return 0;*/
      if (sts_this == sparp->sparp_stss+1)
        {
          sts_this[0].sts_env = ((dk_set_t *)common_env)[0];
          ((dk_set_t *)common_env)[0] = NULL;
        }
      else
        {
          if (NULL != ((dk_set_t *)common_env)[0])
            GPF_T;
          sts_this[0].sts_env = NULL;
        }
      return SPAR_GPT_ENV_PUSH;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN;
    default: return 0;
    }
}

/*! For each duplicate name in \c local_vars the procedure adds new eq to \c curr.
\returns variables that can me propagated */
dk_set_t
sparp_create_eqs_for_dupe_locals (sparp_t *sparp, SPART *curr, dk_set_t *local_vars)
{
  int eq_ctr;
  caddr_t var_name;
  dk_set_t vars_to_propagate = NULL;
  while (NULL != local_vars[0])
    {
      var_name = (caddr_t)dk_set_pop (local_vars);
      if (-1 != dk_set_position_of_string (local_vars[0], var_name))
        {
          sparp_equiv_get (sparp, curr, (SPART *)var_name, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_GET_NAMESAKES);
        }
      else
        dk_set_push (&vars_to_propagate, var_name);
    }
  SPARP_FOREACH_GP_EQUIV(sparp,curr,eq_ctr,eq)
    {
      int varname_ctr;
      DO_BOX_FAST (caddr_t, vname, varname_ctr, eq->e_varnames)
        {
          if (SPART_VARNAME_IS_GLOB (vname))
            continue;
          if (-1 == dk_set_position_of_string (vars_to_propagate, vname))
	    dk_set_push (&vars_to_propagate, vname);
        }
      END_DO_BOX_FAST;
    }
  END_SPARP_FOREACH_GP_EQUIV;
  return vars_to_propagate;
}

int
sparp_gp_trav_make_common_eqs_out (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  dk_set_t *local_vars;
  dk_set_t vars_to_propagate;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP: break;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN;
    default: return 0;
    }
  local_vars = (dk_set_t *)(&(sts_this[0].sts_env));
  vars_to_propagate = sparp_create_eqs_for_dupe_locals (sparp, curr, local_vars);
  if (sts_this == sparp->sparp_stss+1)
    {
      if (NULL != ((dk_set_t *)common_env)[0])
        GPF_T;
      ((dk_set_t *)common_env)[0] = vars_to_propagate;
    }
  else
    {
      dk_set_t *parent_vars = (dk_set_t *)(&(sts_this[-1].sts_env));
      while (NULL != vars_to_propagate)
        dk_set_push (parent_vars, dk_set_pop (&vars_to_propagate));
    }
  return 0;
}

int
sparp_gp_trav_make_common_eqs_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  dk_set_t *parent_vars;
  dk_set_t vars_to_propagate = NULL;
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_make_common_eqs (sub_sparp);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  if (sts_this == sparp->sparp_stss+1)
    parent_vars = ((dk_set_t *)common_env);
  else
    parent_vars = (dk_set_t *)(&(sts_this[-1].sts_env));
  vars_to_propagate = sparp_create_eqs_for_dupe_locals (sparp, curr, (dk_set_t *)common_env);
  while (NULL != vars_to_propagate)
    dk_set_push (parent_vars, dk_set_pop (&vars_to_propagate));
  return 0;
}

void
sparp_make_common_eqs (sparp_t *sparp)
{
  dk_set_t top_vars_to_propagate = NULL;
  sparp_trav_out_clauses (sparp, sparp->sparp_expr, &top_vars_to_propagate,
    sparp_gp_trav_make_common_eqs_in, sparp_gp_trav_make_common_eqs_out,
    NULL, NULL, sparp_gp_trav_make_common_eqs_expn_subq,
    NULL );
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, &top_vars_to_propagate,
    sparp_gp_trav_make_common_eqs_in, sparp_gp_trav_make_common_eqs_out,
    NULL, NULL, sparp_gp_trav_make_common_eqs_expn_subq,
    NULL );
  while (NULL != top_vars_to_propagate)
   dk_set_pop (&top_vars_to_propagate);
}

/* Composing aliases.
\c trav_env_this points to the innermost graph pattern.
\c common_env is used in sparp_gp_trav_make_retval_aliases to pass vector of query return variables. */

void
sparp_gp_add_chain_aliases (sparp_t *sparp, SPART *inner_var, sparp_equiv_t *inner_eq, sparp_trav_state_t *sts_this, SPART *top_gp)
{
  sparp_trav_state_t *sts_iter = sts_this;
  sparp_equiv_t *curr_eq = inner_eq;
#ifdef DEBUG
  if (NULL == curr_eq)
    spar_internal_error (sparp, "sparp_" "gp_add_chain_aliases () has NULL eq for inner_var");
  if (curr_eq->e_gp != sts_iter[0].sts_env)
    spar_internal_error (sparp, "sparp_" "gp_add_chain_aliases () has eq for inner_var not equal to sts_iter[0].sts_env");
#endif
  for (;;)
    {
      sparp_equiv_t *parent_eq;
      SPART *parent_gp = (SPART *)(sts_iter[-1].sts_env);
      if (NULL == parent_gp)
        break;
      parent_eq = sparp_equiv_get (sparp, parent_gp, inner_var, SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_INS_CLASS);
      sparp_equiv_connect (sparp, parent_eq, curr_eq, 1);
      if (parent_gp == top_gp)
        break;
      curr_eq = parent_eq;
      sts_iter--;
    }
}

int
sparp_gp_trav_make_retval_aliases (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART **retvars = (SPART **)common_env;
  int retvar_ctr;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP: break;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN;
    default: return 0;
    }
  sts_this[0].sts_env = curr;
  for (retvar_ctr = BOX_ELEMENTS (retvars); retvar_ctr--; /* no step */)
    {
      SPART *curr_retvar = retvars[retvar_ctr];
      sparp_equiv_t *curr_eq;
      caddr_t curr_varname = spar_var_name_of_ret_column (curr_retvar);
      if (NULL == curr_varname)
        continue;
      curr_eq = sparp_equiv_get (sparp, curr, (SPART *)curr_varname, SPARP_EQUIV_GET_NAMESAKES);
      if (NULL != curr_eq)
        {
          if (SPAR_ALIAS == SPART_TYPE(curr_retvar))
            curr_retvar = curr_retvar->_.alias.arg;
          sparp_gp_add_chain_aliases (sparp, curr_retvar, curr_eq, sts_this, NULL);
        }
    }
  return SPAR_GPT_ENV_PUSH;
}

int
sparp_gp_trav_make_common_aliases_gp_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eq_ctr;
  sparp_trav_state_t *outer_gp_sts;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP: break;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN;
    default: return 0;
    }
  sts_this[0].sts_env = curr;
  SPARP_FOREACH_GP_EQUIV(sparp,curr,eq_ctr,eq)
    {
#if 0
      int var_ctr;
      for (var_ctr = eq->e_var_count; var_ctr--; /* no step */)
        {
	  SPART *var = eq->e_vars[var_ctr];
          for (outer_gp_sts = sparp->sparp_stss+1; outer_gp_sts < sts_this; outer_gp_sts++)
            {
              SPART *outer_gp = outer_gp_sts->sts_env;
	      sparp_equiv_t *topmost_eq = sparp_equiv_get (sparp, outer_gp, var, SPARP_EQUIV_GET_NAMESAKES);
	      if (NULL != topmost_eq)
		{
		  sparp_gp_add_chain_aliases (sparp, var, eq, sts_this, outer_gp);
		  break;
		}
            }
        }
#else
      int varname_ctr;
      DO_BOX_FAST (caddr_t, vname, varname_ctr, eq->e_varnames)
        {
          for (outer_gp_sts = sparp->sparp_stss+1; outer_gp_sts < sts_this; outer_gp_sts++)
            {
              SPART *outer_gp = (SPART *)(outer_gp_sts->sts_env);
	      sparp_equiv_t *topmost_eq = sparp_equiv_get (sparp, outer_gp, (SPART *)vname, SPARP_EQUIV_GET_NAMESAKES);
	      if (NULL != topmost_eq)
		{
		  sparp_gp_add_chain_aliases (sparp, (SPART *) vname, eq, sts_this, outer_gp);
		  break;
		}
            }
        }
      END_DO_BOX_FAST;
#endif
    } END_SPARP_FOREACH_GP_EQUIV;
  return SPAR_GPT_ENV_PUSH;
}

int
sparp_gp_trav_make_aliases_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_make_aliases (sub_sparp);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

int
sparp_gp_trav_remove_unused_aliases (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eq_ctr;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP: break;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN;
    default: return 0;
    }
  sts_this[0].sts_env = curr;
  SPARP_REVFOREACH_GP_EQUIV(sparp,curr,eq_ctr,eq)
    {
      int sub_ctr;
      if (
        (0 != eq->e_const_reads) ||
        (0 != eq->e_gspo_uses) ||
        (0 != eq->e_subquery_uses) ||
        (0 != BOX_ELEMENTS_INT_0 (eq->e_receiver_idxs)) ||
        (1 < (eq->e_nested_bindings + eq->e_optional_reads)) ||
        (0 != eq->e_var_count) ||
        (SPART_VARR_EXPORTED & eq->e_rvr.rvrRestrictions) ||
        eq->e_replaces_filter )
	continue; /* Do not remove if in use */
      for (sub_ctr = BOX_ELEMENTS_INT_0 (eq->e_subvalue_idxs); sub_ctr--; /* no step */)
        {
          sparp_equiv_t *sub_eq = SPARP_EQUIV(sparp, eq->e_subvalue_idxs[sub_ctr]);
          sparp_equiv_disconnect (sparp, eq, sub_eq);
        }
      sparp_equiv_remove (sparp, eq);
    }
  END_SPARP_REVFOREACH_GP_EQUIV;
  return SPAR_GPT_ENV_PUSH;
}

void
sparp_make_aliases (sparp_t *sparp)
{
  SPART **retvars = sparp->sparp_expr->_.req_top.retvals;
  SPART *top = sparp->sparp_expr;
  SPART *top_pattern = top->_.req_top.pattern;
  /*int retvar_ctr;*/
  sparp_gp_trav (sparp, top_pattern, retvars,
    sparp_gp_trav_make_retval_aliases, sparp_gp_trav_cu_out_triples_1,
    NULL, NULL, NULL,
    NULL );
  sparp_gp_trav (sparp, top_pattern, NULL,
    sparp_gp_trav_make_common_aliases_gp_in, sparp_gp_trav_cu_out_triples_1_merge_recvs,
    NULL, NULL, sparp_gp_trav_make_aliases_expn_subq,
    NULL );
  sparp_gp_trav (sparp, top_pattern, NULL,
    sparp_gp_trav_remove_unused_aliases, sparp_gp_trav_cu_out_triples_1,
    NULL, NULL, NULL,
    NULL );
  sparp_trav_out_clauses (sparp, top, NULL,
    NULL, NULL /* was sparp_gp_trav_cu_out_triples_1 */,
    NULL, NULL, sparp_gp_trav_make_aliases_expn_subq,
    NULL );
}

sparp_equiv_t *
sparp_find_external_namesake_eq_of_varname (sparp_t *sparp, caddr_t varname, dk_set_t parent_gps)
{
#if 0 /* code for old draft of SPARQL 1.1 */
  if (NULL != sparp->sparp_env->spare_bindings_vars)
    {
      int ctr;
      DO_BOX_FAST_REV (SPART *, bvar, ctr, sparp->sparp_env->spare_bindings_vars)
        {
          if (strcmp (varname, bvar->_.var.vname))
            continue;
          return SPARP_EQUIV (sparp, bvar->_.var.equiv_idx);
        }
      END_DO_BOX_FAST_REV;
    }
#endif
  DO_SET (SPART *, parent, &parent_gps)
    {
      sparp_equiv_t *parent_eq = sparp_equiv_get (sparp, parent, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES);
      if ((NULL != parent_eq) &&
        ((0 < parent_eq->e_gspo_uses) ||
          (0 < parent_eq->e_subquery_uses) ||
          (0 < parent_eq->e_nested_bindings) ) )
        {
      /*
          if ((SPART_BAD_EQUIV_IDX != eq->e_external_src_idx) &&
            (parent_eq->e_own_idx != eq->e_external_src_idx) &&
            !(parent_eq->e_rvr.rvrRestrictions & SPART_VARR_EXTERNAL) )
            spar_internal_error (sparp, "sparp_" "gp_trav_label_external_vars_gp_in (): mismatch in origin of external");
      */
          return parent_eq;
        }
    }
  END_DO_SET ()
  return NULL;
}

int
sparp_gp_trav_label_external_vars_gp_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eqctr;
  sparp_label_external_vars_env_t *sleve;
  if (SPAR_GP != SPART_TYPE (curr))
    return 0;
  sleve = (sparp_label_external_vars_env_t *)common_env;
  if ((NULL == sleve->parent_gps_for_table_subq) /* && (NULL == sparp->sparp_env->spare_bindings_vars) --- code for old draft of SPARQL 1.1 */)
    return SPAR_GPT_ENV_PUSH;
  if (SELECT_L == curr->_.gp.subtype)
    {
      sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
      sparp_label_external_vars_env_t tmp_sleve;
      tmp_sleve.parent_gps_for_var_search = tmp_sleve.parent_gps_for_table_subq = sleve->parent_gps_for_table_subq;
      sparp_label_external_vars (sub_sparp, &tmp_sleve);
      sparp_up_from_sub (sparp, curr, sub_sparp);
    }
  SPARP_FOREACH_GP_EQUIV (sparp, curr, eqctr, eq)
    {
      int varnamectr;
      DO_BOX_FAST_REV (caddr_t, varname, varnamectr, eq->e_varnames)
        {
          sparp_equiv_t *external_namesake_eq = sparp_find_external_namesake_eq_of_varname (sparp, varname, sleve->parent_gps_for_var_search);
          if (NULL == external_namesake_eq)
            continue;
          eq->e_external_src_idx = external_namesake_eq->e_own_idx;
          eq->e_rvr.rvrRestrictions |= SPART_VARR_EXTERNAL;
          break;
        }
      END_DO_BOX_FAST_REV;
    }
  END_SPARP_FOREACH_GP_EQUIV;
  return SPAR_GPT_ENV_PUSH;
}

/* This makes same labeling as \c sparp_gp_trav_label_external_vars_gp_in() but should be used for retvals.
Note that it label retval vals whereas sparp_gp_trav_label_external_vars_gp_in() does not. */
int
sparp_gp_trav_label_external_vars_expn_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_IS_BLANK_OR_VAR (curr))
    {
      sparp_label_external_vars_env_t *sleve = (sparp_label_external_vars_env_t *)common_env;
      sparp_equiv_t *external_namesake_eq = sparp_find_external_namesake_eq_of_varname (sparp, curr->_.var.vname, sleve->parent_gps_for_var_search);
      if (NULL != external_namesake_eq)
        {
          sparp_equiv_t *eq = SPARP_EQUIV (sparp, curr->_.var.equiv_idx);
          eq->e_external_src_idx = external_namesake_eq->e_own_idx;
          eq->e_rvr.rvrRestrictions |= SPART_VARR_EXTERNAL;
          curr->_.var.rvr.rvrRestrictions |= SPART_VARR_EXTERNAL;
        }
    }
  return 0;
}

int
sparp_gp_trav_label_external_vars_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_label_external_vars_env_t *sleve = (sparp_label_external_vars_env_t *)common_env;
  SPART *anc_gp = sts_this->sts_ancestor_gp;
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  s_node_t tmp_stack;
  sparp_label_external_vars_env_t tmp_sleve;
  tmp_stack.data = anc_gp;
  tmp_stack.next = sleve->parent_gps_for_var_search;
  tmp_sleve.parent_gps_for_var_search = tmp_sleve.parent_gps_for_table_subq = &tmp_stack;
  sparp_label_external_vars (sub_sparp, &tmp_sleve);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

void
sparp_label_external_vars (sparp_t *sparp, sparp_label_external_vars_env_t *sleve)
{
  SPART *top = sparp->sparp_expr;
  SPART *top_pattern = top->_.req_top.pattern;
  sparp_label_external_vars_env_t tmp_sleve;
  if (NULL == sleve)
    {
      tmp_sleve.parent_gps_for_var_search = tmp_sleve.parent_gps_for_table_subq = NULL;
      sleve = &tmp_sleve;
    }
  sparp_trav_out_clauses (sparp, top, sleve,
    NULL, NULL,
    (((NULL != sleve->parent_gps_for_var_search) /*|| (NULL != sparp->sparp_env->spare_bindings_vars) --- code for old draft of SPARQL 1.1 */) ?
      sparp_gp_trav_label_external_vars_expn_in : NULL ),
    NULL,
    sparp_gp_trav_label_external_vars_expn_subq,
    NULL );
  sparp_gp_trav (sparp, top_pattern, sleve,
    sparp_gp_trav_label_external_vars_gp_in, NULL,
    NULL, NULL, sparp_gp_trav_label_external_vars_expn_subq,
    NULL );
}

void
sparp_remove_totally_useless_equivs (sparp_t *sparp)
{
  int equiv_first_idx, equiv_ctr, recv_ctr, dirty;

again:
  dirty = 0;
  equiv_first_idx = sparp->sparp_first_equiv_idx;
  for (equiv_ctr = sparp->sparp_sg->sg_equiv_count; equiv_first_idx < equiv_ctr--; /*no step*/)
    {
      sparp_equiv_t *eq = SPARP_EQUIV (sparp, equiv_ctr);
      if (NULL == eq)
        continue;
      if (SPARP_EQ_IS_ASSIGNED_LOCALLY(eq) || (0 != eq->e_const_reads) || (0 != eq->e_optional_reads) || eq->e_replaces_filter || (0 != BOX_ELEMENTS_0 (eq->e_subvalue_idxs)))
        continue;
      DO_BOX_FAST_REV (ptrlong, recv_idx, recv_ctr, eq->e_receiver_idxs)
        {
          sparp_equiv_disconnect (sparp, SPARP_EQUIV (sparp, recv_idx), eq);
        }
      END_DO_BOX_FAST_REV;
      if (WHERE_L != eq->e_gp->_.gp.subtype)
        eq->e_rvr.rvrRestrictions &= ~SPART_VARR_EXPORTED;
      else if (eq->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED)
        continue;
      sparp_equiv_remove (sparp, eq);
      dirty = 1;
    }
  if (dirty)
    goto again; /* see above */
}

int
sparp_gp_trav_remove_redundant_connections (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_remove_redundant_connections (sub_sparp, (ptrlong)common_env);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

void
sparp_remove_redundant_connections (sparp_t *sparp, ptrlong flags)
{
  SPART *top = sparp->sparp_expr;
  SPART *top_pattern = top->_.req_top.pattern;
  sparp_equiv_t **equivs = sparp->sparp_sg->sg_equivs;
  int eq_first_idx, eq_ctr;
  sparp_gp_trav (sparp, top_pattern, NULL,
    sparp_gp_trav_remove_unused_aliases, sparp_gp_trav_cu_out_triples_1,
    NULL, NULL, sparp_gp_trav_remove_redundant_connections,
    NULL );
  sparp_trav_out_clauses (sparp, top, NULL,
    NULL, NULL /* was sparp_gp_trav_cu_out_triples_1*/,
    NULL, NULL, sparp_gp_trav_remove_redundant_connections,
    NULL );
  if (!(SPARP_UNLINK_IF_ASSIGNED_EXTERNALLY & flags))
    goto skip_ext_ext_unlinks; /* see below */
  eq_first_idx = sparp->sparp_first_equiv_idx;
  for (eq_ctr = sparp->sparp_sg->sg_equiv_count; eq_first_idx < eq_ctr--; /*no step*/)
    {
      sparp_equiv_t *eq = equivs[eq_ctr];
      int sub_ctr;
      if (NULL == eq)
        continue;
      if (!SPARP_EQ_IS_ASSIGNED_EXTERNALLY (eq))
        continue;
      for (sub_ctr = BOX_ELEMENTS_INT_0 (eq->e_subvalue_idxs); sub_ctr--; /*no step*/)
        {
          int can_unlink = 0;
          sparp_equiv_t *sub_eq = equivs[eq->e_subvalue_idxs[sub_ctr]];
          if ((OPTIONAL_L == sub_eq->e_gp->_.gp.subtype) && !SPARP_EQ_IS_ASSIGNED_LOCALLY(sub_eq))
            continue; /* To avoid disconnect of loj filter in SELECT * { ?x <p> ?v . OPTIONAL { ?y <q> ?w . FILTER (?v=2) }} */
          if (!SPARP_EQ_IS_ASSIGNED_EXTERNALLY(sub_eq))
            continue;
          if (
            SPARP_EQ_IS_ASSIGNED_BY_CONTEXT(eq) &&
            SPARP_EQ_IS_ASSIGNED_BY_CONTEXT(sub_eq) &&
            (1 == BOX_ELEMENTS (eq->e_varnames)) &&
            (1 == BOX_ELEMENTS (sub_eq->e_varnames)) &&
            !strcmp (eq->e_varnames[0], sub_eq->e_varnames[0]) )
            can_unlink = 1;
          else if (sparp_equivs_have_same_fixedvalue (sparp, eq, sub_eq) && (0 != eq->e_gspo_uses))
            can_unlink = 1;
          if (can_unlink)
            sparp_equiv_disconnect (sparp, eq, sub_eq);
        }
    }

skip_ext_ext_unlinks:
  sparp_gp_trav (sparp, top_pattern, NULL,
    sparp_gp_trav_remove_unused_aliases, NULL,
    NULL, NULL, NULL,
    NULL );
}

/* Copying restrictions from equivalences to variables */

void
sparp_restr_of_select_eq_from_connected_subvalues (sparp_t *sparp, sparp_equiv_t *eq)
{
  SPART *gp = eq->e_gp;
  caddr_t vname = eq->e_varnames[0];
  SPART *sub_expn = sparp_find_subexpn_in_retlist (sparp, vname, gp->_.gp.subquery->_.req_top.orig_retvals, 0);
  if (NULL != sub_expn)
    {
      if (SPAR_IS_BLANK_OR_VAR(sub_expn))
        {
          sparp_equiv_t *eq_sub = sparp_equiv_get (sparp, gp->_.gp.subquery->_.req_top.pattern, sub_expn, 0);
          sparp_equiv_tighten (sparp, eq, &(eq_sub->e_rvr), ~0);
        }
      else
        {
          ptrlong restr_bits = sparp_restr_bits_of_expn (sparp, sub_expn);
          eq->e_rvr.rvrRestrictions |= restr_bits & (
            SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_IS_BLANK |
            SPART_VARR_IS_LIT | SPART_VARR_LONG_EQ_SQL |
            SPART_VARR_NOT_NULL | SPART_VARR_ALWAYS_NULL );
        }
    }
}

void
sparp_restr_of_union_eq_from_connected_subvalues (sparp_t *sparp, sparp_equiv_t *eq)
{
  sparp_equiv_t **equivs = sparp->sparp_sg->sg_equivs;
  int sub_ctr, varname_ctr;
  int nice_sub_count = 0;
  ptrlong nice_sub_chksum = 0;
  int memb_count = BOX_ELEMENTS (eq->e_gp->_.gp.members);
  dk_set_t common_vars = NULL;
  rdf_val_range_t acc;
  memset (&acc, 0, sizeof (rdf_val_range_t));
  acc.rvrRestrictions = SPART_VARR_CONFLICT;
  DO_BOX_FAST (ptrlong, sub_eq_idx, sub_ctr, eq->e_subvalue_idxs)
    {
      sparp_equiv_t *sub_eq = equivs[sub_eq_idx];
      SPART *sub_gp = sub_eq->e_gp;
      DO_BOX_FAST (caddr_t, varname, varname_ctr, sub_eq->e_varnames)
        {
          if (!SPART_VARNAME_IS_GLOB (varname))
            continue;
          if (0 > dk_set_position_of_string (common_vars, varname))
            continue;
          t_set_push (&common_vars, varname);
        }
      END_DO_BOX_FAST;
      if (sub_eq->e_rvr.rvrRestrictions & SPART_VARR_CONFLICT)
        {
          nice_sub_chksum ^= (ptrlong)(sub_gp);
          nice_sub_count++; /* Conflict is nice because it will be removed soon */
          continue;
        }
      if (!SPARP_EQ_IS_ASSIGNED_LOCALLY (sub_eq))
        continue;
      sparp_rvr_loose (sparp, &acc, &(sub_eq->e_rvr), ~0);
      if ((0 == sub_gp->_.gp.subtype) & (sub_eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL))
        {
          nice_sub_chksum ^= (ptrlong)(sub_gp);
          nice_sub_count++;
        }
    }
  END_DO_BOX_FAST;
  if (nice_sub_count == memb_count)
    {
      int memb_ctr;
      for (memb_ctr = memb_count; memb_ctr--; /* no step */)
        nice_sub_chksum ^= (ptrlong)(eq->e_gp->_.gp.members[memb_ctr]);
    }
  else
    nice_sub_chksum = -1;
  if ((0 != nice_sub_chksum) || (0 == nice_sub_count))
    acc.rvrRestrictions &= ~SPART_VARR_NOT_NULL;
#ifdef DEBUG
  if ((acc.rvrRestrictions & SPART_VARR_NOT_NULL) && (!(eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL)))
    dbg_printf (("sparp_" "restr_of_union_eq_from_connected_subvalues(): strong optimization on ?%s (was 0x%x) by 0x%x acc", eq->e_varnames[0],
      (unsigned)(eq->e_rvr.rvrRestrictions), (unsigned)(acc.rvrRestrictions) ) );
#endif
  sparp_equiv_tighten (sparp, eq, &acc, ~0);
  if (NULL == common_vars)
    eq->e_rvr.rvrRestrictions &= ~(SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL);
  else
    {
      DO_BOX_FAST (caddr_t, varname, varname_ctr, eq->e_varnames)
        {
          if (0 > dk_set_position_of_string (common_vars, varname))
            t_set_push (&common_vars, varname);
        }
      END_DO_BOX_FAST;
      eq->e_varnames = t_list_to_array (common_vars);
    }
}

void
sparp_restr_of_join_eq_from_connected_subvalue (sparp_t *sparp, sparp_equiv_t *eq, sparp_equiv_t *sub_eq)
{
  SPART *sub_gp = sub_eq->e_gp;
  if (OPTIONAL_L == sub_gp->_.gp.subtype)
    {
      if ((1 == eq->e_nested_bindings) && (0 == eq->e_gspo_uses) && (0 == eq->e_subquery_uses) &&
        (0 == eq->e_replaces_filter) && SPARP_EQ_IS_ASSIGNED_LOCALLY (sub_eq) &&
        !(sub_eq->e_rvr.rvrRestrictions & (SPART_VARR_CONFLICT | SPART_VARR_ALWAYS_NULL)) )
        sparp_equiv_tighten (sparp, eq, &(sub_eq->e_rvr), ~SPART_VARR_NOT_NULL);
    }
  else
    if (SPARP_EQ_IS_ASSIGNED_LOCALLY (sub_eq))
      sparp_equiv_tighten (sparp, eq, &(sub_eq->e_rvr), ~0);
}

void
sparp_restr_of_join_eq_from_connected_subvalues (sparp_t *sparp, sparp_equiv_t *eq)
{
  sparp_equiv_t **equivs = sparp->sparp_sg->sg_equivs;
  int sub_ctr;
  DO_BOX_FAST (ptrlong, sub_eq_idx, sub_ctr, eq->e_subvalue_idxs)
    {
      sparp_equiv_t *sub_eq = equivs[sub_eq_idx];
      sparp_restr_of_join_eq_from_connected_subvalue (sparp, eq, sub_eq);
    }
  END_DO_BOX_FAST;
}

void
sparp_find_best_join_eq_for_optional (sparp_t *sparp, SPART *parent, int pos_of_curr_memb, sparp_equiv_t *eq, sparp_equiv_t **ret_parent_eq, SPART **ret_tree_in_parent, SPART **ret_source_in_parent)
{
  SPART *curr = NULL, *prev = NULL;
  int varname_ctr, ctr;
  int pos_of_prev_memb;
  int good_is_gp = 0;
  caddr_t good_varname = NULL;
  SPART *good_prev = NULL;
  SPART *good_prev_var = NULL;
  sparp_equiv_t *good_parent_eq = NULL;
  sparp_equiv_t *good_prev_eq = NULL;
#ifndef NDEBUG
  if ((SPAR_GP != SPART_TYPE (parent)) || (BOX_ELEMENTS (parent->_.gp.members) <= pos_of_curr_memb))
    spar_internal_error (sparp, "sparp_" "find_best_join_eq_for_optional(): bad call");
#endif
  ret_parent_eq[0] = NULL;
  ret_tree_in_parent[0] = NULL;
  ret_source_in_parent[0] = NULL;
  if (0 == pos_of_curr_memb)
    return;
  curr = parent->_.gp.members[pos_of_curr_memb];
  for (pos_of_prev_memb = 0; pos_of_prev_memb < pos_of_curr_memb; pos_of_prev_memb++)
    {
      prev = parent->_.gp.members[pos_of_prev_memb];
      if (SPAR_GP == prev->type)
        {
          DO_BOX_FAST (caddr_t, varname, varname_ctr, eq->e_varnames)
            {
              sparp_equiv_t *parent_eq, *prev_eq;
              parent_eq = sparp_equiv_get_ro (sparp->sparp_sg->sg_equivs, sparp->sparp_sg->sg_equiv_count, parent, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES);
              if (NULL == parent_eq)
                continue;
              prev_eq = sparp_equiv_get_ro (sparp->sparp_sg->sg_equivs, sparp->sparp_sg->sg_equiv_count, prev, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES);
              if (NULL == prev_eq)
                continue;
              good_prev = prev;
              good_varname = varname;
              good_parent_eq = parent_eq;
              good_prev_var = NULL;
              good_prev_eq = prev_eq;
              good_is_gp = 1;
              if (SPART_VARR_NOT_NULL & prev_eq->e_rvr.rvrRestrictions)
                goto good_found; /* see below */
            }
          END_DO_BOX_FAST;
        }
      else
        {
          DO_BOX_FAST (caddr_t, varname, varname_ctr, eq->e_varnames)
            {
              sparp_equiv_t *parent_eq;
              parent_eq = sparp_equiv_get_ro (sparp->sparp_sg->sg_equivs, sparp->sparp_sg->sg_equiv_count, parent, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES);
              if (NULL == parent_eq)
                continue;
              for (ctr = parent_eq->e_var_count; ctr--; /* no step */)
                {
                  SPART *prev_var = parent_eq->e_vars[ctr];
                  if (NULL == prev_var->_.var.tabid)
                    continue;
                  if (strcmp (prev_var->_.var.tabid, prev->_.triple.tabid))
                    continue;
                  good_prev = prev;
                  good_varname = NULL;
                  good_parent_eq = parent_eq;
                  good_prev_var = prev_var;
                  good_is_gp = 0;
                  if (SPART_VARR_NOT_NULL & prev_var->_.var.rvr.rvrRestrictions)
                    goto good_found; /* see below */
                }
            }
          END_DO_BOX_FAST;
        }
    }
good_found:
  ret_parent_eq[0] = good_parent_eq;
  ret_source_in_parent[0] = good_prev;
  if (good_is_gp)
    {
      SPART *var_rv = (SPART *)t_alloc_box (sizeof (SPART), DV_ARRAY_OF_POINTER);
      memset (var_rv, 0, sizeof (SPART));
      var_rv->type = SPAR_RETVAL;
      var_rv->_.retval.equiv_idx = good_prev_eq->e_own_idx;
      var_rv->_.retval.gp = prev;
      memcpy (&(var_rv->_.retval.rvr), &(good_prev_eq->e_rvr), sizeof (rdf_val_range_t));
      var_rv->_.retval.selid = prev->_.gp.selid;
      var_rv->_.retval.vname = good_varname;
      ret_tree_in_parent[0] = var_rv;
    }
  else
    {
      ret_tree_in_parent[0] = good_prev_var;
    }
}

int
sparp_gp_trav_eq_restr_from_connected_subvalues_gp_out (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eq_ctr;
  if (SPAR_GP != SPART_TYPE(curr))
    return 0;
  SPARP_FOREACH_GP_EQUIV(sparp,curr,eq_ctr,eq)
    {
      switch (curr->_.gp.subtype)
        {
        case SELECT_L: sparp_restr_of_select_eq_from_connected_subvalues (sparp, eq); break;
        case UNION_L: sparp_restr_of_union_eq_from_connected_subvalues (sparp, eq); break;
        default: sparp_restr_of_join_eq_from_connected_subvalues (sparp, eq); break;
        }
      if (SPARP_EQ_IS_ASSIGNED_LOCALLY(eq))
        continue;
      if (SPARP_EQ_IS_ASSIGNED_BY_CONTEXT(eq))
        continue;
      if (OPTIONAL_L == curr->_.gp.subtype)
        {
          int recv_ctr;
          sparp_equiv_t *good_loj_for_filter_var = 0;
          DO_BOX_FAST_REV (ptrlong, recv_idx, recv_ctr, eq->e_receiver_idxs)
            {
              sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, recv_idx);
              if ((SPARP_EQ_IS_ASSIGNED_LOCALLY(recv_eq) || SPARP_EQ_IS_ASSIGNED_BY_CONTEXT(recv_eq)) &&
                !(recv_eq->e_rvr.rvrRestrictions & SPART_VARR_ALWAYS_NULL) )
                {
                  good_loj_for_filter_var = recv_eq;
                  break;
                }
            }
          END_DO_BOX_FAST_REV;
          if (NULL != good_loj_for_filter_var)
            continue;
        }
/*      if ((eq->e_replaces_filter) && (OPTIONAL_L == curr->_.gp.subtype))
        continue;*/
      if (eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL)
        eq->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
      else
        eq->e_rvr.rvrRestrictions |= SPART_VARR_ALWAYS_NULL;
    } END_SPARP_FOREACH_GP_EQUIV;
  return 0;
}

int
sparp_gp_trav_eq_restr_from_connected_subvalues_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_continue_gp_trav_in_sub (sparp, curr, common_env);
  return 0;
}

int
sparp_gp_trav_eq_restr_from_connected_receivers_gp_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_equiv_t **equivs = sparp->sparp_sg->sg_equivs;
  int eq_ctr;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      break;
    case SPAR_TRIPLE:
      return SPAR_GPT_NODOWN;
    default: return 0;
    }
  SPARP_FOREACH_GP_EQUIV(sparp,curr,eq_ctr,eq)
    {
      int var_ctr;
      for (var_ctr = eq->e_var_count; var_ctr--; /*no step*/)
       {
         SPART *var = eq->e_vars[var_ctr];
         sparp_trav_state_t *sts_iter;
         int changeable =
           ( SPART_VARR_CONFLICT | SPART_VARR_NOT_NULL |
             SPART_VARR_IS_BLANK | SPART_VARR_IS_IRI |
             SPART_VARR_IS_LIT | SPART_VARR_IS_REF |
             SPART_VARR_TYPED | SPART_VARR_FIXED |
             SPART_VARR_SPRINTFF | SPART_VARR_LONG_EQ_SQL );
         if (NULL == var->_.var.tabid)
           continue;
         sparp_rvr_tighten (sparp, &(var->_.var.rvr), &(eq->e_rvr), changeable);
         for (sts_iter = sts_this;
           (NULL != sts_iter->sts_parent) &&
           (SPAR_GP == SPART_TYPE (sts_iter->sts_parent)) &&
           (SELECT_L != sts_iter->sts_parent->_.gp.subtype);
           sts_iter-- )
           {
             sparp_equiv_t *outer_eq = sparp_equiv_get_ro (equivs, sparp->sparp_sg->sg_equiv_count, sts_iter->sts_parent, (SPART *)(var->_.var.vname), SPARP_EQUIV_GET_NAMESAKES);
             if (NULL != outer_eq)
               sparp_rvr_tighten (sparp, &(var->_.var.rvr), &(outer_eq->e_rvr), changeable);
           }
       }
    } END_SPARP_FOREACH_GP_EQUIV;
  return SPAR_GPT_ENV_PUSH;
}

int
sparp_gp_trav_eq_restr_from_connected_receivers_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_continue_gp_trav_in_sub (sparp, curr, common_env);
  return 0;
}

void
sparp_eq_restr_from_connected (sparp_t *sparp)
{
  /*sparp_env_t *env = sparp->sparp_env;*/
  SPART *binv = sparp->sparp_expr->_.req_top.binv;
  if (NULL != binv)
    {
      int varctr, varcount = BOX_ELEMENTS (binv->_.binv.vars);
      SPART **retlist = sparp->sparp_expr->_.req_top.retvals;
      for (varctr = varcount; varctr--; /* no step */)
        {
          SPART *var, *ret_expn = NULL;
          int retctr, ret_expn_type;
          sparp_equiv_t *var_eq;
          if (binv->_.binv.counters_of_unbound[varctr])
            continue;
          var = binv->_.binv.vars[varctr];
          var_eq = SPARP_EQUIV (sparp, var->_.var.equiv_idx);
          for (retctr = BOX_ELEMENTS (retlist); retctr--; /* no step */)
            {
              SPART *candidate = retlist[retctr];
              ret_expn_type = SPART_TYPE (candidate);
              if (SPAR_VARIABLE == ret_expn_type)
                {
                  if (strcmp (candidate->_.var.vname, var->_.var.vname))
                    continue;
                  ret_expn = candidate;
                  break;
                }
              if (SPAR_ALIAS == ret_expn_type)
                {
                  if (strcmp (candidate->_.alias.aname, var->_.var.vname))
                    continue;
                  ret_expn = candidate->_.alias.arg;
                  ret_expn_type = SPART_TYPE (ret_expn);
                  break;
                }
            }
          if (NULL == ret_expn)
            {
              if (sparp->sparp_env->spare_signal_void_variables)
                spar_error (sparp, "Variable name '%.100s' is used in then BINDINGS clause but not in the query result set", var->_.var.vname);
              var_eq->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
            }
          else
            {
              sparp_equiv_tighten (sparp, var_eq, &(var->_.var.rvr), ~0);
              if (SPAR_VARIABLE == ret_expn_type)
                {
                  sparp_equiv_t *ret_orig_eq = SPARP_EQUIV (sparp, ret_expn->_.var.equiv_idx);
                  sparp_equiv_tighten (sparp, var_eq, &(ret_orig_eq->e_rvr), ~(SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL));
                  if (!(sparp_req_top_has_limofs (sparp->sparp_expr) && (NULL != sparp->sparp_expr->_.req_top.order)))
                    sparp_equiv_tighten (sparp, ret_orig_eq, &(var_eq->e_rvr), ~0);
                }
            }
        }
      spar_shorten_binv_dataset (sparp, binv);
      spar_refresh_binv_var_rvrs (sparp, binv);
    }
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    NULL, sparp_gp_trav_eq_restr_from_connected_subvalues_gp_out,
    NULL, NULL, sparp_gp_trav_eq_restr_from_connected_subvalues_expn_subq,
    NULL );
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_eq_restr_from_connected_receivers_gp_in, NULL,
    NULL, NULL, sparp_gp_trav_eq_restr_from_connected_receivers_expn_subq,
    NULL );
}

/* Copying restrictions from equivalences to variables */

int
sparp_gp_trav_eq_restr_to_vars_gp_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eq_ctr, var_ctr;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      break;
    case SPAR_TRIPLE:
      return SPAR_GPT_NODOWN;
    default: return 0;
    }
  SPARP_FOREACH_GP_EQUIV(sparp,curr,eq_ctr,eq)
    {
      for (var_ctr = eq->e_var_count; var_ctr--; /* no step */)
	{
	  SPART *var = eq->e_vars[var_ctr];
#ifdef DEBUG
	  if ((SPAR_VARIABLE != SPART_TYPE(var)) && (SPAR_BLANK_NODE_LABEL != SPART_TYPE(var)))
	    spar_internal_error (sparp, "Not a variable in equiv in sparp_gp_trav_eq_restr_to_vars()");
#endif
	  sparp_rvr_tighten (sparp, &(var->_.var.rvr), &(eq->e_rvr), ~0 /* not (SPART_VARR_EXTERNAL | SPART_VARR_GLOBAL)*/);
	  var->_.var.equiv_idx = eq->e_own_idx;
	}
    } END_SPARP_FOREACH_GP_EQUIV;
  return 0;
}

int
sparp_gp_trav_eq_restr_to_vars_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_continue_gp_trav_in_sub (sparp, curr, common_env);
  return 0;
}

void
sparp_eq_restr_to_vars (sparp_t *sparp)
{
  /*sparp_env_t *env = sparp->sparp_env;*/
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_eq_restr_to_vars_gp_in, NULL,
    NULL, NULL, sparp_gp_trav_eq_restr_to_vars_expn_subq,
    NULL );
}

#ifdef DEBUG

static int
sparp_gp_trav_equiv_audit_inner_vars (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART *gp = sts_this->sts_ancestor_gp;
  int eq_ctr;
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      SPARP_FOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
        {
          int recv_ctr;
          if (NULL == eq)
            {
              spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): gp with deleted eq in use");
              continue; /* spar_audit_error can continue without a signal if runs inside sparp_internal_error in DEBUG mode. */
            }
          DO_BOX_FAST (ptrlong, recv_idx, recv_ctr, eq->e_receiver_idxs)
            {
              sparp_equiv_t *recv = SPARP_EQUIV (sparp, recv_idx);
              if (recv->e_gp != gp)
                spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): gp of recv eq is not parent of gp of curr eq, gp %s eq %# for %s", gp->_.gp.selid, eq->e_own_idx, eq->e_varnames[0]);
            }
          END_DO_BOX_FAST;
        }
      END_SPARP_FOREACH_GP_EQUIV;
      if (NULL != curr->_.gp.subquery)
        {
          sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
          memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
          stss[0].sts_ofs_of_curr_in_array = -1;
          stss[1].sts_ancestor_gp = curr->_.gp.subquery->_.req_top.pattern;
          sparp_gp_trav_int (sparp, curr->_.gp.subquery->_.req_top.pattern, stss+1, common_env,
            sparp_gp_trav_equiv_audit_inner_vars, NULL,
            sparp_gp_trav_equiv_audit_inner_vars, NULL, sparp_gp_trav_equiv_audit_inner_vars,
            NULL );
          sparp_equiv_audit_retvals (sparp, curr->_.gp.subquery);
          return SPAR_GPT_NODOWN;
        }
      return SPAR_GPT_ENV_PUSH;
    case SPAR_VARIABLE: break;
    case SPAR_BLANK_NODE_LABEL: break;
    default: return 0;
    }
  if (SPART_BAD_EQUIV_IDX != curr->_.var.equiv_idx)
    {
      sparp_equiv_t *eq_by_id, *eq;
      eq_by_id = SPARP_EQUIV (sparp, curr->_.var.equiv_idx);
      if (NULL == eq_by_id)
        {
          spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): curr with deleted eq_by_id in use");
          return 0;
        }
      if (eq_by_id->e_gp != gp)
        spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): e_gp of eq_by_id does not match gp, gp %s var %s/%s/%s", gp->_.gp.selid, curr->_.var.selid, curr->_.var.tabid, curr->_.var.vname);
      eq = sparp_equiv_get (sparp, gp, curr, 0);
      if (NULL == eq)
        {
          spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): eq not found, gp %s var %s/%s/%s", gp->_.gp.selid, curr->_.var.selid, curr->_.var.tabid, curr->_.var.vname);
          return 0;
        }
      if (eq->e_own_idx != curr->_.var.equiv_idx)
        spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): eq idx mismatch, gp %s var %s/%s/%s", gp->_.gp.selid, curr->_.var.selid, curr->_.var.tabid, curr->_.var.vname);
      if (strcmp (gp->_.gp.selid, curr->_.var.selid))
        spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): var selid differs from gp selid, gp %s var %s/%s/%s", gp->_.gp.selid, curr->_.var.selid, curr->_.var.tabid, curr->_.var.vname);
    }
  else if (SPARP_EQUIV_AUDIT_NOBAD & ((ptrlong)common_env))
    spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_inner_vars(): var with SPART_BAD_EQUIV_IDX, var %s/%s/%s", curr->_.var.selid, curr->_.var.tabid, curr->_.var.vname);
  return 0;
}

static int
sparp_gp_trav_equiv_audit_retvals (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART *top_gp = (SPART *)(common_env);
  sparp_equiv_t *eq;
  switch (SPART_TYPE(curr))
    {
    case SPAR_VARIABLE: break;
    case SPAR_BLANK_NODE_LABEL: break;
    default: return 0;
    }
  eq = sparp_equiv_get (sparp, top_gp, curr, SPARP_EQUIV_GET_ASSERT | SPARP_EQUIV_GET_NAMESAKES);
  if (eq->e_own_idx != curr->_.var.equiv_idx)
    spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_retvals(): eq idx mismatch");
  if (!(eq->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED))
    spar_audit_error (sparp, "sparp_" "gp_trav_equiv_audit_retvals(): lost SPART_VARR_EXPORTED");
  return 0;
}

void
sparp_equiv_audit_retvals (sparp_t *sparp, SPART *top)
{
  int ctr;
  sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
  DO_BOX_FAST (SPART *, expn, ctr, top->_.req_top.retvals)
    {
      memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
      stss[0].sts_ofs_of_curr_in_array = -1;
      sparp_gp_trav_int (sparp, expn, stss+1, top->_.req_top.pattern,
        NULL, NULL,
        sparp_gp_trav_equiv_audit_retvals, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
  DO_BOX_FAST (SPART *, grouping, ctr, top->_.req_top.groupings)
    {
      memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
      stss[0].sts_ofs_of_curr_in_array = -1;
      sparp_gp_trav_int (sparp, grouping, stss+1, top->_.req_top.pattern,
        NULL, NULL,
        sparp_gp_trav_equiv_audit_retvals, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
  if (NULL != top->_.req_top.having)
    {
      memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
      stss[0].sts_ofs_of_curr_in_array = -1;
      sparp_gp_trav_int (sparp, top->_.req_top.having, stss+1, top->_.req_top.pattern,
        NULL, NULL,
        sparp_gp_trav_equiv_audit_retvals, NULL, NULL,
        NULL );
    }
  DO_BOX_FAST (SPART *, oby, ctr, top->_.req_top.order)
    {
      memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
      stss[0].sts_ofs_of_curr_in_array = -1;
      sparp_gp_trav_int (sparp, oby->_.oby.expn, stss+1, top->_.req_top.pattern,
        NULL, NULL,
        sparp_gp_trav_equiv_audit_retvals, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
}

void
sparp_equiv_audit_gp (sparp_t *sparp, SPART *gp, int is_deprecated, sparp_equiv_t *chk_eq)
{
  int gp_eq_ctr;
  SPARP_FOREACH_GP_EQUIV (sparp, gp, gp_eq_ctr, gp_eq)
    {
      if (gp_eq->e_gp != gp)
        spar_audit_error (sparp, "sparp_" "equiv_audit_gp(): gp_eq->e_gp != gp, gp %s eq #%d for %s", gp->_.gp.selid, gp_eq->e_own_idx, gp_eq->e_varnames[0]);
      if (chk_eq == gp_eq)
        chk_eq = NULL;
      if (gp_eq->e_deprecated && !is_deprecated)
        spar_audit_error (sparp, "sparp_" "equiv_audit_gp(): eq is deprecated, gp %s eq #%d for %s", gp->_.gp.selid, gp_eq->e_own_idx, gp_eq->e_varnames[0]);
      if (is_deprecated && !(gp_eq->e_deprecated))
        spar_audit_error (sparp, "sparp_" "equiv_audit_gp(): eq is expected to be deprecated, gp %s eq #%d for %s", gp->_.gp.selid, gp_eq->e_own_idx, gp_eq->e_varnames[0]);
    }
  END_SPARP_FOREACH_GP_EQUIV;
  if (NULL != chk_eq)
    spar_audit_error (sparp, "sparp_" "equiv_audit_gp(): no reference to chk_eq in gp, gp %s chk_eq #%d for %s", gp->_.gp.selid, chk_eq->e_own_idx, chk_eq->e_varnames[0]);
}

void
sparp_equiv_audit_all (sparp_t *sparp, int flags)
{
  sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
  int eq_ctr, eq_count, var_ctr, recv_ctr, subv_ctr;
  if (NULL == sparp->sparp_expr)
    return; /* Internal error during parsing phase, there's no complete query to validate equivalence classes in it. */
  memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
  stss[0].sts_ofs_of_curr_in_array = -1;
  sparp_gp_trav_int (sparp, sparp->sparp_expr->_.req_top.pattern, stss+1, (void *)((ptrlong)flags),
    sparp_gp_trav_equiv_audit_inner_vars, NULL,
    sparp_gp_trav_equiv_audit_inner_vars, NULL, sparp_gp_trav_equiv_audit_inner_vars,
    NULL );
  sparp_equiv_audit_retvals (sparp, sparp->sparp_expr);
  eq_count = sparp->sparp_sg->sg_equiv_count;
  for (eq_ctr = sparp->sparp_first_equiv_idx; eq_ctr < eq_count; eq_ctr++)
    {
      sparp_equiv_t *eq = SPARP_EQUIV (sparp, eq_ctr);
      SPART *gp;
      if (NULL == eq)
        continue;
      if (eq->e_own_idx != eq_ctr)
        spar_audit_error (sparp, "sparp_" "equiv_audot_all(): wrong own index, eq #%d for %s has e_own_idx %d", eq_ctr, eq->e_varnames[0], eq->e_own_idx);
      for (var_ctr = eq->e_var_count; var_ctr--; /*no step*/)
        {
          SPART *var = eq->e_vars [var_ctr];
          if (var->_.var.equiv_idx != eq_ctr)
            spar_audit_error (sparp, "sparp_" "equiv_audit_all(): var->_.var.equiv_idx != eq_ctr: eq #%d for %s, gp %s, var %s/%s/%s with equiv_idx %d", eq_ctr, eq->e_varnames[0], var->_.var.selid, var->_.var.tabid, var->_.var.vname, var->_.var.equiv_idx);
        }
      gp = eq->e_gp;
      if (SPAR_GP != gp->type)
        continue;
      /*if (eq->e_nested_bindings != BOX_ELEMENTS_0 (eq->e_subvalue_idxs))
        printf ("sparp_" "equiv_audit_all(): warning: strange: equiv %d (?%s): e_nested_bindings = %d, %d subvalues in list, e_optional_reads = %d\n",
            (int)(eq->e_own_idx), eq->e_varnames[0],
            (int)(eq->e_nested_bindings), BOX_ELEMENTS_INT_0 (eq->e_subvalue_idxs), (int)(eq->e_optional_reads) );*/
      sparp_equiv_audit_gp (sparp, gp, ((SPART_BAD_GP_SUBTYPE == gp->_.gp.subtype) ? 1 : 0), eq);
      for (var_ctr = eq->e_var_count; var_ctr--; /*no step*/)
        {
          SPART *var = eq->e_vars [var_ctr];
          if (var->_.var.equiv_idx != eq_ctr)
            spar_audit_error (sparp, "sparp_" "equiv_audit_all(): var->_.var.equiv_idx != eq_ctr: eq #%d for %s, gp %s, var %s/%s/%s with equiv_idx %d", eq_ctr, eq->e_varnames[0], var->_.var.selid, var->_.var.tabid, var->_.var.vname, var->_.var.equiv_idx);
          if (strcmp (var->_.var.selid, gp->_.gp.selid))
            spar_audit_error (sparp, "sparp_" "equiv_audit_all(): selid of var of eq differs from selid of gp of eq, gp %s, var %s/%s/%s with equiv_idx %d", gp->_.gp.selid, var->_.var.selid, var->_.var.tabid, var->_.var.vname, var->_.var.equiv_idx);
          if (NULL != var->_.var.tabid)
            {
              int var_tr_idx = var->_.var.tr_idx;
              int triple_idx;
              for (triple_idx = BOX_ELEMENTS (gp->_.gp.members); triple_idx--; /* no step */)
                {
                  SPART *triple = gp->_.gp.members[triple_idx];
                  if (SPAR_TRIPLE != triple->type)
                    continue;
                  if (var_tr_idx < SPART_TRIPLE_FIELDS_COUNT)
                    {
                      if (triple->_.triple.tr_fields[var_tr_idx] == var)
                        break;
                    }
                  else
                    {
                      if (sparp_get_option (sparp, triple->_.triple.options, var_tr_idx) == var)
                        break;
                    }
                }
              if (0 > triple_idx)
                {
                  if (var_tr_idx < SPART_TRIPLE_FIELDS_COUNT)
                    spar_audit_error (sparp, "sparp_" "equiv_audit_all(): var is in equiv but not in any triple of the group, var %s/%s#%d/%s", var->_.var.selid, var->_.var.tabid, var->_.var.tr_idx, var->_.var.vname);
#if 0
                  else
                    spar_audit_error (sparp, "sparp_" "equiv_audit_all(): var is in equiv but not in any triple of the group, var %s/%s#%d/%s", var->_.var.selid, var->_.var.tabid, var->_.var.tr_idx, var->_.var.vname);
#endif
                }
            }
        }
      recv_ctr = BOX_ELEMENTS_0 (eq->e_receiver_idxs);
      if (0 != recv_ctr)
        {
          SPART *recv_gp_0 = (SPARP_EQUIV (sparp, eq->e_receiver_idxs[0]))->e_gp;
          while (recv_ctr-- > 1)
            {
              SPART *recv_gp_N = (SPARP_EQUIV (sparp, eq->e_receiver_idxs[recv_ctr]))->e_gp;
              if (recv_gp_N != recv_gp_0)
                spar_audit_error (sparp, "sparp_" "equiv_audit_all(): gps of different recvs differ");
            }
          DO_BOX_FAST_REV (ptrlong, recv_idx, recv_ctr, eq->e_receiver_idxs)
            {
              sparp_equiv_t *recv = SPARP_EQUIV (sparp, recv_idx);
              DO_BOX_FAST_REV (ptrlong, subv_idx, subv_ctr, recv->e_subvalue_idxs)
                {
                  if (subv_idx == eq_ctr)
                    goto subv_of_recv_ok;
                }
              END_DO_BOX_FAST_REV;
              spar_audit_error (sparp, "sparp_" "equiv_audit_all(): no matching subv for recv");
subv_of_recv_ok: ;
            }
          END_DO_BOX_FAST_REV;
          DO_BOX_FAST_REV (ptrlong, subv_idx, subv_ctr, eq->e_subvalue_idxs)
            {
              sparp_equiv_t *subv = SPARP_EQUIV (sparp, subv_idx);
              DO_BOX_FAST_REV (ptrlong, recv_idx, recv_ctr, subv->e_receiver_idxs)
                {
                  if (recv_idx == eq_ctr)
                    goto recv_of_subv_ok;
                }
              END_DO_BOX_FAST_REV;
              spar_audit_error (sparp, "sparp_" "equiv_audit_all(): no matching recv for subv");
recv_of_subv_ok: ;
            }
          END_DO_BOX_FAST_REV;
        }
#if 0
      if (!SPARP_EQ_IS_ASSIGNED_LOCALLY(eq) &&
        !SPARP_EQ_IS_ASSIGNED_EXTERNALLY (eq) &&
        ((0 != eq->e_const_reads) || /* no check for (0 != eq->e_optional_reads) */
          (0 != BOX_ELEMENTS_0 (eq->e_receiver_idxs)) /*||
          (eq->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED)*/ ) )
      spar_error (sparp, "Variable '%.100s' is used but not assigned", eq->e_varnames[0]);
#endif
    }
}

void
sparp_audit_mem (sparp_t *sparp)
{
  t_check_tree (sparp->sparp_expr);
}

#endif

int sparp_gp_trav_check_if_local (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_VARIABLE == curr->type)
    {
      caddr_t varname = curr->_.var.vname;
      if (!SPART_VARNAME_IS_GLOB(varname))
        return SPAR_GPT_COMPLETED;
      return SPAR_GPT_NODOWN;
    }
  if (SPAR_BLANK_NODE_LABEL == curr->type)
    return SPAR_GPT_COMPLETED;
  return 0;
}

int
sparp_tree_is_global_expn (sparp_t *sparp, SPART *tree)
{
  int res = sparp_gp_trav (sparp, tree, NULL,
    NULL, NULL,
    sparp_gp_trav_check_if_local, NULL, NULL /*!!!TBD add*/,
    NULL );
  return !(SPAR_GPT_COMPLETED & res);
}

int sparp_gp_trav_expn_reads_equiv (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if ((SPAR_VARIABLE == curr->type) || (SPAR_BLANK_NODE_LABEL == curr->type))
    {
      if (curr->_.var.equiv_idx == ((ptrlong)(common_env)))
        return SPAR_GPT_COMPLETED;
    }
  return 0;
}

int
sparp_expn_reads_equiv (sparp_t *sparp, SPART *expn, sparp_equiv_t *eq)
{
  ptrlong eq_idx = eq->e_own_idx;
  int res = sparp_gp_trav (sparp, expn, (void *)eq_idx,
    NULL, NULL,
    sparp_gp_trav_expn_reads_equiv, NULL, NULL /*!!!TBD add*/,
    NULL );
  return (SPAR_GPT_COMPLETED & res);
}

/* Main rewriting functions */

void
sparp_rewrite_basic (sparp_t *sparp)
{
  sparp_audit_mem (sparp);
  sparp_count_usages (sparp, NULL);
  sparp_audit_mem (sparp);
  sparp_restrict_by_simple_filters (sparp);
  sparp_audit_mem (sparp);
  sparp_make_common_eqs (sparp);
  sparp_make_aliases (sparp);
  sparp_eq_restr_from_connected (sparp);
  sparp_eq_restr_to_vars (sparp);
  sparp_label_external_vars (sparp, NULL);
  sparp_remove_totally_useless_equivs (sparp);
  sparp_remove_redundant_connections (sparp, 0);
  sparp_audit_mem (sparp);
}

/* PART 2. GRAPH PATTERN TERM REWRITING */

void
spar_invalidate_binv_dataset_row (sparp_t *sparp, SPART *binv, int rowno, int reason_col)
{
  int varctr;
  unsigned mask_byte;
  if ('/' != binv->_.binv.data_rows_mask[rowno])
    spar_internal_error (sparp, "double invalidation of a binding");
  mask_byte = (unsigned)'0' + (unsigned)reason_col;
  if (0x7f < mask_byte)
    mask_byte = 0x7f;
  binv->_.binv.data_rows_mask[rowno] = mask_byte;
  DO_BOX_FAST (ptrlong, counter_of_unbound, varctr, binv->_.binv.counters_of_unbound)
    {
      if (NULL != binv->_.binv.data_rows[rowno][varctr])
        continue;
      binv->_.binv.counters_of_unbound[varctr] = counter_of_unbound - 1;
    }
  END_DO_BOX_FAST;
  binv->_.binv.rows_in_use--;
}

void
spar_shorten_binv_dataset (sparp_t *sparp, SPART *binv)
{
  int varcount, rowcount, varctr, rowctr;
  int *fmt_use_counters = NULL;
  int max_sff_count = -1;
  varcount = BOX_ELEMENTS (binv->_.binv.vars);
  if (0 == binv->_.binv.rows_in_use)
    return;
  varcount = BOX_ELEMENTS (binv->_.binv.vars);
  rowcount = BOX_ELEMENTS (binv->_.binv.data_rows);
/* All loops by rows here must be in reverse order. */
  for (varctr = varcount; varctr--; /* no step */)
    {
      SPART *var = binv->_.binv.vars[varctr];
      sparp_equiv_t *eq = SPARP_EQUIV (sparp, var->_.var.equiv_idx);
      if (var->_.var.rvr.rvrRestrictions & (SPART_VARR_CONFLICT | SPART_VARR_ALWAYS_NULL))
        continue;
      if (eq->e_rvr.rvrRestrictions & (SPART_VARR_CONFLICT | SPART_VARR_ALWAYS_NULL))
        {
          for (rowctr = rowcount; rowctr--; /* no step */)
            {
              if ('/' != binv->_.binv.data_rows_mask[rowctr])
                continue;
              if (NULL == binv->_.binv.data_rows[rowctr][varctr])
                continue;
              spar_invalidate_binv_dataset_row (sparp, binv, rowctr, varctr);
            }
          continue;
        }
      if ((eq->e_rvr.rvrRestrictions & SPART_VARR_SPRINTFF) && (eq->e_rvr.rvrSprintffCount > max_sff_count))
        max_sff_count = eq->e_rvr.rvrSprintffCount;
    }
  if (0 <= max_sff_count)
    fmt_use_counters = (int *)t_alloc (max_sff_count * sizeof (int));
  for (varctr = varcount; varctr--; /* no step */)
    {
      SPART *var = binv->_.binv.vars[varctr];
      sparp_equiv_t *eq = SPARP_EQUIV (sparp, var->_.var.equiv_idx);
      int eq_has_sffs_bit = (eq->e_rvr.rvrRestrictions & SPART_VARR_SPRINTFF);
      int eq_sff_count = eq->e_rvr.rvrSprintffCount;
      if (!rvr_can_be_tightned (sparp, &(var->_.var.rvr), &(eq->e_rvr), 1))
        continue;
      if (eq->e_rvr.rvrSprintffCount)
        memset (fmt_use_counters, 0, eq->e_rvr.rvrSprintffCount * sizeof (int));
      for (rowctr = rowcount; rowctr--; /* no step */)
        {
          SPART *datum;
          rdf_val_range_t tmp;
          if ('/' != binv->_.binv.data_rows_mask[rowctr])
            continue;
          datum = binv->_.binv.data_rows[rowctr][varctr];
          if (NULL == datum)
            continue;
          sparp_rvr_set_by_constant (sparp, &tmp, NULL, datum);
          eq->e_rvr.rvrRestrictions &= ~SPART_VARR_SPRINTFF;
          eq->e_rvr.rvrSprintffCount = 0;
          sparp_rvr_tighten (sparp, &tmp, &(eq->e_rvr), ~0);
          eq->e_rvr.rvrRestrictions |= eq_has_sffs_bit;
          eq->e_rvr.rvrSprintffCount = eq_sff_count;
          if (tmp.rvrRestrictions & SPART_VARR_CONFLICT)
            {
              spar_invalidate_binv_dataset_row (sparp, binv, rowctr, varctr);
              continue;
            }
          if (eq_has_sffs_bit)
            {
              int sff_ctr;
              int hit = 0;
              caddr_t datum_val = SPAR_LIT_OR_QNAME_VAL (datum);
              dtp_t datum_val_dtp = DV_TYPE_OF (datum_val);
              if ((DV_STRING == datum_val_dtp) || (DV_UNAME == datum_val_dtp))
                {
                  for (sff_ctr = eq->e_rvr.rvrSprintffCount; sff_ctr--; /* no step */)
                    {
                      if (!sprintff_like (datum_val, eq->e_rvr.rvrSprintffs[sff_ctr]))
                        continue;
                      fmt_use_counters[sff_ctr] += 1;
                      hit = 1;
                    }
                }
              if (!hit)
                {
                  spar_invalidate_binv_dataset_row (sparp, binv, rowctr, varctr);
                  continue;
                }
            }
        }
      if (eq_has_sffs_bit && (0 == binv->_.binv.counters_of_unbound[varctr]))
        {
          int sff_ctr;
          for (sff_ctr = eq->e_rvr.rvrSprintffCount; sff_ctr--; /* no step */)
            {
              if (0 != fmt_use_counters[sff_ctr])
                continue;
              if (sff_ctr < (eq->e_rvr.rvrSprintffCount-1))
                eq->e_rvr.rvrSprintffs [sff_ctr] = eq->e_rvr.rvrSprintffs [eq->e_rvr.rvrSprintffCount-1];
              eq->e_rvr.rvrSprintffCount -= 1;
            }
          if (0 == eq->e_rvr.rvrSprintffCount)
            eq->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
          if (eq->e_rvr.rvrSprintffCount != var->_.var.rvr.rvrSprintffCount)
            {
              sparp_rvr_tighten (sparp, &(var->_.var.rvr), &(eq->e_rvr), ~0);
              var->_.var.rvr.rvrSprintffCount = eq->e_rvr.rvrSprintffCount;
              var->_.var.rvr.rvrSprintffs = (ccaddr_t *)t_box_copy ((caddr_t)(eq->e_rvr.rvrSprintffs));
            }
        }
    }
}

void
spar_refresh_binv_var_rvrs (sparp_t *sparp, SPART *binv)
{
  int varcount, rowcount, varctr, rowctr;
  if (binv->_.binv.rows_in_use == binv->_.binv.rows_last_rvr)
    return;
  binv->_.binv.rows_last_rvr = binv->_.binv.rows_in_use; 
  varcount = BOX_ELEMENTS (binv->_.binv.vars);
  if (0 == binv->_.binv.rows_in_use)
    {
      for (varctr = varcount; varctr--; /* no step */)
        binv->_.binv.vars[varctr]->_.var.rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
      return;
    }
  rowcount = BOX_ELEMENTS (binv->_.binv.data_rows);
  if (1 == binv->_.binv.rows_in_use)
    {
      rowctr = strchr (binv->_.binv.data_rows_mask, '/') - binv->_.binv.data_rows_mask;
      if (0 > rowctr)
        spar_internal_error (sparp, "No used rows but nonzero counter of them");
      for (varctr = varcount; varctr--; /* no step */)
        {
          SPART *var;
          SPART *datum;
          if (binv->_.binv.counters_of_unbound[varctr])
            continue;
          var = binv->_.binv.vars[varctr];
          datum = binv->_.binv.data_rows[rowctr][varctr];
          sparp_rvr_set_by_constant (sparp, &(var->_.var.rvr), NULL, datum);
        }
      return;
    }
  for (varctr = varcount; varctr--; /* no step */)
    {
      SPART *var;
      int restr_set = SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_IS_BLANK | SPART_VARR_IS_LIT | SPART_VARR_NOT_NULL;
      if (binv->_.binv.counters_of_unbound[varctr])
        continue;
      var = binv->_.binv.vars[varctr];
      for (rowctr = rowcount; rowctr--; /* no step */)
        {
          SPART *datum;
          if ('/' != binv->_.binv.data_rows_mask[rowctr])
            continue;
          datum = binv->_.binv.data_rows[rowctr][varctr];
          if (NULL == datum)
            spar_internal_error (sparp, "NULL datum in BINDINGS col without expected unbounds");
          switch (SPART_TYPE (datum))
            {
            case SPAR_QNAME:
              restr_set &= ~SPART_VARR_IS_LIT;
              /*                                 0123456789 */
              if (!strncmp (datum->_.qname.val, "nodeID://", 9))
                restr_set &= ~SPART_VARR_IS_IRI;
              else
                restr_set &= ~SPART_VARR_IS_BLANK;
              break;
            case SPAR_LIT:
              restr_set &= ~(SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_IS_BLANK);
              break;
            }
        }
      var->_.var.rvr.rvrRestrictions |= restr_set;
    }
}

int
sparp_check_field_mapping_of_cvalue (sparp_t *sparp, SPART *cvalue, rdf_val_range_t *qmv_or_fmt_rvr, rdf_val_range_t *rvr)
{
  if ((NULL != rvr) && (NULL != rvr->rvrFixedValue))
    {
      if (!sparp_fixedvalues_equal (sparp, cvalue, (SPART *)(rvr->rvrFixedValue)))
        return SSG_QM_NO_MATCH;
      return SSG_QM_PROVEN_MATCH;
    }
  if (NULL != qmv_or_fmt_rvr)
    {
      if (SPART_VARR_SPRINTFF & qmv_or_fmt_rvr->rvrRestrictions)
        {
          if (!rvr_sprintffs_like ((caddr_t) cvalue, qmv_or_fmt_rvr))
            return SSG_QM_NO_MATCH;
        }
      if (SPART_VARR_FIXED & qmv_or_fmt_rvr->rvrRestrictions)
        {
          if (!sparp_fixedvalues_equal (sparp, cvalue, (SPART *)(qmv_or_fmt_rvr->rvrFixedValue)))
            return SSG_QM_NO_MATCH;
          return SSG_QM_PROVEN_MATCH;
        }
    }
  return SSG_QM_APPROX_MATCH;
}

int
sparp_check_mapping_of_sources (sparp_t *sparp, tc_context_t *tcc,
  rdf_val_range_t *qmv_or_fmt_rvr, rdf_val_range_t *rvr, int invalidation_level )
{
  int min_match = 0xFFFF;
  int source_ctr;
#ifdef DEBUG
  if (!(tcc->tcc_check_source_graphs))
    GPF_T1("Bad invocation of sparp_check_mapping_of_sources()");
#endif
  DO_BOX_FAST (SPART *, source, source_ctr, tcc->tcc_sources)
    {
      int chk_res;
      if (SPART_GRAPH_MIN_NEGATION < source->_.graph.subtype)
        {
          if (NULL == source->_.graph.iri)
            continue;
          if ((NULL != rvr) && (NULL != rvr->rvrFixedValue) &&
            sparp_fixedvalues_equal (sparp, (SPART *)(source->_.graph.iri), (SPART *)(rvr->rvrFixedValue)) )
            return SSG_QM_NO_MATCH;
          if ((NULL != qmv_or_fmt_rvr) && (SPART_VARR_FIXED & qmv_or_fmt_rvr->rvrRestrictions) &&
            sparp_fixedvalues_equal (sparp, (SPART *)(source->_.graph.iri), (SPART *)(qmv_or_fmt_rvr->rvrFixedValue)))
            return SSG_QM_NO_MATCH;
          continue;
        }
      if (tcc->tcc_source_invalidation_masks[source_ctr])
        continue;
      if (NULL == source->_.graph.iri)
        chk_res = ((NULL != rvr->rvrFixedValue) ? SSG_QM_APPROX_MATCH : SSG_QM_PARTIAL_MATCH);
      else
        chk_res = sparp_check_field_mapping_of_cvalue (sparp, (SPART *)(source->_.graph.iri), qmv_or_fmt_rvr, rvr);
      if (SSG_QM_NO_MATCH != chk_res)
        {
          if (chk_res < min_match)
            min_match = chk_res;
        }
      else
        tcc->tcc_source_invalidation_masks[source_ctr] |= (1 << invalidation_level);
    }
  END_DO_BOX_FAST;
  if (0xFFFF == min_match)
    return SSG_QM_NO_MATCH;
  return min_match;
}

ptrdiff_t qm_field_map_offsets[4] = {
  JSO_FIELD_OFFSET(quad_map_t,qmGraphMap),
  JSO_FIELD_OFFSET(quad_map_t,qmSubjectMap),
  JSO_FIELD_OFFSET(quad_map_t,qmPredicateMap),
  JSO_FIELD_OFFSET(quad_map_t,qmObjectMap) };

ptrdiff_t qm_field_constants_offsets[4] = {
  JSO_FIELD_OFFSET(quad_map_t,qmGraphRange.rvrFixedValue),
  JSO_FIELD_OFFSET(quad_map_t,qmSubjectRange.rvrFixedValue),
  JSO_FIELD_OFFSET(quad_map_t,qmPredicateRange.rvrFixedValue),
  JSO_FIELD_OFFSET(quad_map_t,qmObjectRange.rvrFixedValue) };

int
sparp_check_field_mapping_g (sparp_t *sparp, tc_context_t *tcc, SPART *field,
  quad_map_t *qm, qm_value_t *qmv, rdf_val_range_t *rvr, int invalidation_level )
{
  int source_ctr;
  rdf_val_range_t *qmv_or_fmt_rvr = NULL;
  ptrlong field_type = SPART_TYPE (field);
  if (NULL != qmv)
    {
      if ((SPART_VARR_SPRINTFF | SPART_VARR_FIXED) & qmv->qmvRange.rvrRestrictions)
        qmv_or_fmt_rvr = &(qmv->qmvRange);
      else if (NULL != qmv->qmvFormat)
        {
          if ((SPART_VARR_SPRINTFF | SPART_VARR_FIXED) & qmv->qmvFormat->qmfValRange.rvrRestrictions)
            qmv_or_fmt_rvr = &(qmv->qmvFormat->qmfValRange);
        }
    }
  for (source_ctr = BOX_ELEMENTS_0 (tcc->tcc_sources); source_ctr--; /* no step */)
    tcc->tcc_source_invalidation_masks[source_ctr] &= (1 << invalidation_level) - 1;
  if (tcc->tcc_check_source_graphs)
    {
      int chk_res = sparp_check_mapping_of_sources (sparp, tcc, qmv_or_fmt_rvr, rvr, invalidation_level);
      if (SSG_QM_NO_MATCH == chk_res)
        return SSG_QM_NO_MATCH;
    }
  if (!tcc->tcc_check_source_graphs && (qm != tcc->tcc_qs->qsDefaultMap) &&
    (tcc->tcc_qs->qsMatchingFlags & SPART_QS_NO_IMPLICIT_USER_QM) &&
    (SPAR_BLANK_NODE_LABEL == SPART_TYPE (field)) )
    return SSG_QM_NO_MATCH;
  if ((SPAR_BLANK_NODE_LABEL == field_type) || (SPAR_VARIABLE == field_type))
    {
      if (tcc->tcc_check_source_graphs)
        {
          int chk_res = sparp_check_mapping_of_sources (sparp, tcc, &(field->_.var.rvr), NULL, invalidation_level);
          if (SSG_QM_NO_MATCH == chk_res)
            return SSG_QM_NO_MATCH;
        }
      if (SPART_VARR_FIXED & field->_.var.rvr.rvrRestrictions)
        {
          /*sparp_equiv_t *eq_g = sparp->sparp_sg->sg_equivs[field->_.var.equiv_idx];*/
          int chk_res;
          if (DV_UNAME != DV_TYPE_OF (field->_.var.rvr.rvrFixedValue))
            { /* This would be very strange failure */
#ifdef DEBUG
              GPF_T1 ("sparp_check_field_mapping_g(): non-UNAME fixed value of variable used as graph of a triple. Legal but strange");
#else
              return SSG_QM_NO_MATCH;
#endif
            }
          chk_res = sparp_check_field_mapping_of_cvalue (sparp, (SPART *)(field->_.var.rvr.rvrFixedValue), qmv_or_fmt_rvr, rvr);
          return chk_res;
        }
      if ((NULL != qmv_or_fmt_rvr) && (SPART_VARR_SPRINTFF & qmv_or_fmt_rvr->rvrRestrictions))
        {
          if (SPART_VARR_FIXED & field->_.var.rvr.rvrRestrictions)
            { /* Check if a fixed value of a field variable matches to one of sffs of the mapping value */
              caddr_t fv = rvr_string_fixedvalue (&(field->_.var.rvr));
              if (NULL != fv)
                {
                  int ctr;
                  for (ctr = qmv_or_fmt_rvr->rvrSprintffCount; ctr--; /* no step */)
                    if (sprintff_like (fv, qmv_or_fmt_rvr->rvrSprintffs[ctr]))
                      goto fixed_field_matches_qmv_sff; /* see below */
                  return SSG_QM_NO_MATCH; /* reached if no match found */

fixed_field_matches_qmv_sff: ;
                }
            }
          if (SPART_VARR_SPRINTFF & field->_.var.rvr.rvrRestrictions)
            { /* Check if either of formats of a field variable matches to one of sffs of the mapping value */
              int fld_ctr, qmv_ctr;
/* First pass is optimistic and tries to find an exact equality */
              for (fld_ctr = field->_.var.rvr.rvrSprintffCount; fld_ctr--; /* no step */)
                for (qmv_ctr = qmv_or_fmt_rvr->rvrSprintffCount; qmv_ctr--; /* no step */)
                   if (!strcmp (
                          field->_.var.rvr.rvrSprintffs [fld_ctr],
                          qmv_or_fmt_rvr->rvrSprintffs [qmv_ctr] ) )
                     goto field_sff_isects_qmv_sff;
/* Second pass checks everything, slowly */
              for (fld_ctr = field->_.var.rvr.rvrSprintffCount; fld_ctr--; /* no step */)
                for (qmv_ctr = qmv_or_fmt_rvr->rvrSprintffCount; qmv_ctr--; /* no step */)
                   if (NULL != sprintff_intersect (
                          field->_.var.rvr.rvrSprintffs [fld_ctr],
                          qmv_or_fmt_rvr->rvrSprintffs [qmv_ctr], 1 ) )
                     goto field_sff_isects_qmv_sff;
              return SSG_QM_NO_MATCH; /* reached if no match found */
field_sff_isects_qmv_sff: ;
            }
        }
      if (NULL == rvr->rvrFixedValue)
        return SSG_QM_APPROX_MATCH; /* This is not quite true, but this is the matching rule for EXCLUSIVE */
      return SSG_QM_PARTIAL_MATCH;
    }
  else if ((SPAR_LIT == field_type) || (SPAR_QNAME == field_type))
    {
      int chk_res;
      caddr_t eff_val = SPAR_LIT_OR_QNAME_VAL (field);
      if (DV_UNAME != DV_TYPE_OF (eff_val))
        { /* This would be very-very strange failure */
#ifdef DEBUG
          GPF_T1 ("sparp_check_field_mapping_g(): non-UNAME constant used as graph of a triple, legal but strange");
#else
          return SSG_QM_NO_MATCH;
#endif
        }
      if (tcc->tcc_check_source_graphs)
        {
          rdf_val_range_t fake_rvr;
          fake_rvr.rvrRestrictions = SPART_VARR_FIXED;
          fake_rvr.rvrFixedValue = eff_val;
          chk_res = sparp_check_mapping_of_sources (sparp, tcc, NULL, &fake_rvr, invalidation_level);
          if (SSG_QM_NO_MATCH == chk_res)
            return SSG_QM_NO_MATCH;
        }
      chk_res = sparp_check_field_mapping_of_cvalue (sparp, (SPART *) eff_val, qmv_or_fmt_rvr, rvr);
      return chk_res;
    }
  GPF_T1("ssg_check_field_mapping_g(): field is neither variable nor literal?");
  return SSG_QM_NO_MATCH;
}

int
sparp_check_field_mapping_spo (sparp_t *sparp, tc_context_t *tcc, SPART *field,
  quad_map_t *qm, qm_value_t *qmv, rdf_val_range_t *rvr)
{
  rdf_val_range_t *qmv_or_fmt_rvr = NULL;
  ptrlong field_type = SPART_TYPE (field);
  if (NULL != qmv)
    {
      if ((SPART_VARR_SPRINTFF | SPART_VARR_FIXED) & qmv->qmvRange.rvrRestrictions)
        qmv_or_fmt_rvr = &(qmv->qmvRange);
      else if (NULL != qmv->qmvFormat)
        {
          if ((SPART_VARR_SPRINTFF | SPART_VARR_FIXED | SPART_VARR_IS_LIT | SPART_VARR_IS_REF) & qmv->qmvFormat->qmfValRange.rvrRestrictions)
            qmv_or_fmt_rvr = &(qmv->qmvFormat->qmfValRange);
        }
    }
  if ((SPAR_BLANK_NODE_LABEL == field_type) || (SPAR_VARIABLE == field_type))
    {
      if ((NULL != rvr->rvrFixedValue) && (SPART_VARR_FIXED & field->_.var.rvr.rvrRestrictions))
        { /* Check if a fixed value of a field variable is equal to the constant field of the mapping */
          if (!sparp_fixedvalues_equal (sparp, (SPART *)(field->_.var.rvr.rvrFixedValue), (SPART *)(rvr->rvrFixedValue)))
            return SSG_QM_NO_MATCH;
          return SSG_QM_PROVEN_MATCH;
        }
      if ((NULL != qmv_or_fmt_rvr) && (SPART_VARR_SPRINTFF & qmv_or_fmt_rvr->rvrRestrictions))
        {
          if (SPART_VARR_FIXED & field->_.var.rvr.rvrRestrictions)
            { /* Check if a fixed value of a field variable matches to one of sffs of the mapping value */
              caddr_t fv = rvr_string_fixedvalue (&(field->_.var.rvr));
              if (NULL != fv)
                {
                  int ctr;
                  for (ctr = qmv_or_fmt_rvr->rvrSprintffCount; ctr--; /* no step */)
                    if (sprintff_like (fv, qmv_or_fmt_rvr->rvrSprintffs[ctr]))
                      goto fixed_field_matches_qmv_sff; /* see below */
                  return SSG_QM_NO_MATCH; /* reached if no match found */
fixed_field_matches_qmv_sff: ;
                }
            }
          if (SPART_VARR_SPRINTFF & field->_.var.rvr.rvrRestrictions)
            { /* Check if either of formats of a field variable matches to one of sffs of the mapping value */
              int fld_ctr, qmv_ctr;
/* First pass is optimistic and tries to find an exact equality */
              for (fld_ctr = field->_.var.rvr.rvrSprintffCount; fld_ctr--; /* no step */)
                for (qmv_ctr = qmv_or_fmt_rvr->rvrSprintffCount; qmv_ctr--; /* no step */)
                   if (!strcmp (
                          field->_.var.rvr.rvrSprintffs [fld_ctr],
                          qmv_or_fmt_rvr->rvrSprintffs [qmv_ctr] ) )
                     goto field_sff_isects_qmv_sff;
/* Second pass checks everything, slowly */
              for (fld_ctr = field->_.var.rvr.rvrSprintffCount; fld_ctr--; /* no step */)
                for (qmv_ctr = qmv_or_fmt_rvr->rvrSprintffCount; qmv_ctr--; /* no step */)
                   if (NULL != sprintff_intersect (
                          field->_.var.rvr.rvrSprintffs [fld_ctr],
                          qmv_or_fmt_rvr->rvrSprintffs [qmv_ctr], 1 ) )
                     goto field_sff_isects_qmv_sff;
              return SSG_QM_NO_MATCH; /* reached if no match found */
field_sff_isects_qmv_sff: ;
            }
        }
      if (NULL == rvr->rvrFixedValue)
        return SSG_QM_APPROX_MATCH; /* This is not quite true, but this is the matching rule for EXCLUSIVE */
      return SSG_QM_PARTIAL_MATCH;
    }
  else if ((SPAR_LIT == field_type) || (SPAR_QNAME == field_type))
    {
      rdf_val_range_t *some_map_rvr = ((NULL != qmv_or_fmt_rvr) ? qmv_or_fmt_rvr : rvr);
      if (SPAR_LIT == field_type)
        {
          if (SPART_VARR_IS_REF & some_map_rvr->rvrRestrictions)
            return SSG_QM_NO_MATCH;
        }
      else
        {
          if (SPART_VARR_IS_LIT & some_map_rvr->rvrRestrictions)
            return SSG_QM_NO_MATCH;
        }
      if (NULL != rvr->rvrFixedValue)
        {
          if (!sparp_fixedvalues_equal (sparp, field, (SPART *)(rvr->rvrFixedValue)))
            return SSG_QM_NO_MATCH;
          return SSG_QM_PROVEN_MATCH;
        }
      if ((NULL != qmv_or_fmt_rvr) && (SPART_VARR_SPRINTFF & qmv_or_fmt_rvr->rvrRestrictions))
        {
          caddr_t fv = SPAR_LIT_OR_QNAME_VAL (field);
          if ((NULL != fv) && IS_STRING_DTP (DV_TYPE_OF (fv)))
            {
              int ctr;
              for (ctr = qmv_or_fmt_rvr->rvrSprintffCount; ctr--; /* no step */)
                if (sprintff_like (fv, qmv_or_fmt_rvr->rvrSprintffs[ctr]))
                  return SSG_QM_PROVEN_MATCH;
              return SSG_QM_NO_MATCH; /* reached if no match found */
            }
        }
      return SSG_QM_PROVEN_MATCH;
    }
  GPF_T1("ssg_check_field_mapping_spo(): field is neither variable nor literal?");
  return SSG_QM_NO_MATCH;
}

int
sparp_check_triple_case (sparp_t *sparp, tc_context_t *tcc, quad_map_t *qm, int invalidation_level)
{
  SPART *src_g = tcc->tcc_triple->_.triple.tr_graph;
  SPART *src_s = tcc->tcc_triple->_.triple.tr_subject;
  SPART *src_p = tcc->tcc_triple->_.triple.tr_predicate;
  SPART *src_o = tcc->tcc_triple->_.triple.tr_object;
  int g_match = SSG_QM_UNSET, s_match = SSG_QM_UNSET,
    p_match = SSG_QM_UNSET, o_match = SSG_QM_UNSET;
  if (NULL == qm)
    return SSG_QM_NO_MATCH;
  /* First of all we perform check for cached matches, esp. for failures. */
  if ((NULL != qm->qmGraphMap) && (tcc->tcc_last_qmvs [SPART_TRIPLE_GRAPH_IDX] == qm->qmGraphMap))
    {
      g_match = tcc->tcc_last_qmv_results [SPART_TRIPLE_GRAPH_IDX];
      if (SSG_QM_NO_MATCH == g_match)
        return SSG_QM_NO_MATCH;
    }
  if ((NULL != qm->qmSubjectMap) && (tcc->tcc_last_qmvs [SPART_TRIPLE_SUBJECT_IDX] == qm->qmSubjectMap))
    {
      s_match = tcc->tcc_last_qmv_results [SPART_TRIPLE_SUBJECT_IDX];
      if (SSG_QM_NO_MATCH == s_match)
        return SSG_QM_NO_MATCH;
    }
  if ((NULL != qm->qmPredicateMap) && (tcc->tcc_last_qmvs [SPART_TRIPLE_PREDICATE_IDX] == qm->qmPredicateMap))
    {
      p_match = tcc->tcc_last_qmv_results [SPART_TRIPLE_PREDICATE_IDX];
      if (SSG_QM_NO_MATCH == p_match)
        return SSG_QM_NO_MATCH;
    }
  if ((NULL != qm->qmObjectMap) && (tcc->tcc_last_qmvs [SPART_TRIPLE_OBJECT_IDX] == qm->qmObjectMap))
    {
      o_match = tcc->tcc_last_qmv_results [SPART_TRIPLE_OBJECT_IDX];
      if (SSG_QM_NO_MATCH == o_match)
        return SSG_QM_NO_MATCH;
    }
  /* Check of predicate first, because it's more selective than other */
  if (SSG_QM_UNSET == p_match)
    {
      p_match = sparp_check_field_mapping_spo (sparp, tcc, src_p, qm, qm->qmPredicateMap, &(qm->qmPredicateRange));
      if (NULL != qm->qmPredicateMap)
        {
          tcc->tcc_last_qmvs [SPART_TRIPLE_PREDICATE_IDX] = qm->qmPredicateMap;
          tcc->tcc_last_qmv_results [SPART_TRIPLE_PREDICATE_IDX] = p_match;
        }
      if (SSG_QM_NO_MATCH == p_match)
        return SSG_QM_NO_MATCH;
    }
  /* Check of graph */
  if (SSG_QM_UNSET == g_match)
    {
      g_match = sparp_check_field_mapping_g (sparp, tcc, src_g, qm, qm->qmGraphMap, &(qm->qmGraphRange), invalidation_level);
      if (NULL != qm->qmGraphMap)
        {
          tcc->tcc_last_qmvs [SPART_TRIPLE_GRAPH_IDX] = qm->qmGraphMap;
          tcc->tcc_last_qmv_results [SPART_TRIPLE_GRAPH_IDX] = g_match;
        }
      if (SSG_QM_NO_MATCH == g_match)
        return SSG_QM_NO_MATCH;
    }
  /* Check of subject */
  if (SSG_QM_UNSET == s_match)
    {
      s_match = sparp_check_field_mapping_spo (sparp, tcc, src_s, qm, qm->qmSubjectMap, &(qm->qmSubjectRange));
      if (NULL != qm->qmSubjectMap)
        {
          tcc->tcc_last_qmvs [SPART_TRIPLE_SUBJECT_IDX] = qm->qmSubjectMap;
          tcc->tcc_last_qmv_results [SPART_TRIPLE_SUBJECT_IDX] = s_match;
        }
      if (SSG_QM_NO_MATCH == s_match)
        return SSG_QM_NO_MATCH;
    }
  /* Check of object */
  if (SSG_QM_UNSET == o_match)
    {
      o_match = sparp_check_field_mapping_spo (sparp, tcc, src_o, qm, qm->qmObjectMap, &(qm->qmObjectRange));
      if (NULL != qm->qmObjectMap)
        {
          tcc->tcc_last_qmvs [SPART_TRIPLE_OBJECT_IDX] = qm->qmObjectMap;
          tcc->tcc_last_qmv_results [SPART_TRIPLE_OBJECT_IDX] = o_match;
        }
      if (SSG_QM_NO_MATCH == o_match)
        return SSG_QM_NO_MATCH;
    }
  /* The conclusion */
  if ((SSG_QM_PROVEN_MATCH == g_match) && (SSG_QM_PROVEN_MATCH == s_match) &&
    (SSG_QM_PROVEN_MATCH == p_match) && (SSG_QM_PROVEN_MATCH == o_match) )
    return SSG_QM_PROVEN_MATCH;
  if ((SSG_QM_APPROX_MATCH <= g_match) && (SSG_QM_APPROX_MATCH <= s_match) &&
    (SSG_QM_APPROX_MATCH <= p_match) && (SSG_QM_APPROX_MATCH <= o_match) )
    return SSG_QM_APPROX_MATCH;
  return SSG_QM_PARTIAL_MATCH;
}

int
sparp_qm_find_triple_cases (sparp_t *sparp, tc_context_t *tcc, quad_map_t *qm, int inside_allowed_qm, int invalidation_level)
{
  int ctr, fld_ctr, single_fixed_fld = -1;
  int qm_is_a_good_case = 0;
  int common_status = sparp_check_triple_case (sparp, tcc, qm, invalidation_level);
  if (SSG_QM_NO_MATCH == common_status)
    return SSG_QM_NO_MATCH;
  if ((NULL == tcc->tcc_top_allowed_qm) || (qm == tcc->tcc_top_allowed_qm))
    inside_allowed_qm = 1;
  DO_BOX_FAST (quad_map_t *, sub_qm, ctr, qm->qmUserSubMaps)
    {
      int status = sparp_qm_find_triple_cases (sparp, tcc, sub_qm, inside_allowed_qm, invalidation_level+1);
      if (SSG_QM_MATCH_AND_CUT == status)
        return SSG_QM_MATCH_AND_CUT;
    }
  END_DO_BOX_FAST;
  for (;;)
    {
      int ft_t;
      if (SPART_QM_EMPTY & qm->qmMatchingFlags)
        break; /* not a good case */
      ft_t = tcc->tcc_triple->_.triple.ft_type;
      if (0 != ft_t)
        {
          qm_ftext_t *qmft;
          if (NULL == qm->qmObjectMap)
            break; /* not a good case for ft */
          qmft = ((SPAR_GEO_CONTAINS == ft_t) ? qm->qmObjectMap->qmvGeo : qm->qmObjectMap->qmvFText);
          if (NULL == qmft)
            break; /* not a good case for ft */
        }
      qm_is_a_good_case = 1;
      break;
    }
  if (qm_is_a_good_case)
    {
      triple_case_t *tc = (triple_case_t *)t_alloc (sizeof (triple_case_t));
      tc->tc_qm = qm;
      for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
        {
          if (NULL != tcc->tcc_cuts [fld_ctr])
            tc->tc_red_cuts [fld_ctr] = (ccaddr_t *)t_revlist_to_array (tcc->tcc_cuts [fld_ctr]);
          else
            tc->tc_red_cuts [fld_ctr] = NULL;
        }
      tcc->tcc_nonfiltered_cases_found += 1;
      if (inside_allowed_qm)
        dk_set_push (&(tcc->tcc_found_cases), tc);
    }
  if (SPART_QM_EXCLUSIVE & qm->qmMatchingFlags)
    {
      for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
        {
          caddr_t fld_const = SPARP_FIELD_CONST_OF_QM (qm, fld_ctr);
          qm_value_t *qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
          if (NULL != fld_const)
            {
              if (-1 == single_fixed_fld)
                {
                  single_fixed_fld = fld_ctr;
                  continue;
                }
              single_fixed_fld = -2;
              break;
            }
          if (NULL != qmv)
            {
              single_fixed_fld = -3;
              break;
            }
        }
      if (0 <= single_fixed_fld)
        {
          caddr_t single_fixed_val = SPARP_FIELD_CONST_OF_QM (qm, single_fixed_fld);
          dk_set_push (tcc->tcc_cuts + single_fixed_fld, single_fixed_val);
        }
    }
  if ((SSG_QM_PROVEN_MATCH == common_status) && (SPART_QM_SOFT_EXCLUSIVE & qm->qmMatchingFlags))
    return SSG_QM_MATCH_AND_CUT;
  if ((SSG_QM_APPROX_MATCH <= common_status) && (SPART_QM_EXCLUSIVE & qm->qmMatchingFlags))
    return SSG_QM_MATCH_AND_CUT;
  return common_status;
}

triple_case_t **
sparp_find_triple_cases (sparp_t *sparp, SPART *triple, SPART **sources, int required_source_type)
{
  caddr_t triple_qm_iri = triple->_.triple.qm_iri_or_pair;
  caddr_t triple_storage_iri;
  SPART *sinv = NULL;
  quad_storage_t *triple_storage;
  int ctr, fld_ctr, source_ctr;
  triple_case_t **res_list;
  tc_context_t tmp_tcc;
  if (DV_ARRAY_OF_POINTER == DV_TYPE_OF (triple_qm_iri))
    {
      sinv = SPARP_SINV (sparp, unbox (((caddr_t *)triple_qm_iri)[1]));
      triple_storage_iri = sinv->_.sinv.storage_uri;
      triple_qm_iri = ((caddr_t *)triple_qm_iri)[0];
      triple_storage = sparp_find_storage_by_name (triple_storage_iri);
      if (NULL == triple_storage)
        spar_internal_error (sparp, "quad storage metadata are lost");
      if ((SPART **)((ptrlong)(_STAR)) == sources)
        sources = sinv->_.sinv.sources;
    }
  else
    {
      triple_storage_iri = sparp->sparp_env->spare_storage_name;
      triple_storage = sparp->sparp_storage;
      if ((SPART **)((ptrlong)(_STAR)) == sources)
        sources = sparp->sparp_expr->_.req_top.sources;
    }
  if (NULL == triple_storage)
    {
      triple_case_t **res_list = (triple_case_t **)t_list (1, tc_default);
      mp_box_tag_modify (res_list, DV_ARRAY_OF_LONG);
      return res_list;
    }
  memset (&tmp_tcc, 0, sizeof (tc_context_t));
  tmp_tcc.tcc_triple = triple;
  tmp_tcc.tcc_qs = triple_storage;
  tmp_tcc.tcc_sources = sources;
  tmp_tcc.tcc_source_invalidation_masks = (uint32 *)t_alloc_box (sizeof (uint32) * BOX_ELEMENTS (sources), DV_BIN);
  DO_BOX_FAST (SPART *, source, source_ctr, tmp_tcc.tcc_sources)
    {
      if ((0 == required_source_type) || ((source->_.graph.subtype & ~SPART_GRAPH_GROUP_BIT) == required_source_type))
        {
          tmp_tcc.tcc_check_source_graphs++;
          tmp_tcc.tcc_source_invalidation_masks[source_ctr] = 0;
        }
      else if (!((SPART_GRAPH_NOT_FROM == source->_.graph.subtype) ||
          (SPART_GRAPH_NOT_NAMED == source->_.graph.subtype) ) )
        tmp_tcc.tcc_source_invalidation_masks[source_ctr] = 0x1;
    }
  END_DO_BOX_FAST;
  if (((caddr_t)DEFAULT_L) == triple_qm_iri)
    {
      if (NULL == triple_storage->qsDefaultMap)
        spar_error (sparp, "QUAD MAP DEFAULT group pattern is used in RDF storage '%.200s' that has no default quad map", triple_storage_iri);
      tmp_tcc.tcc_top_allowed_qm = triple_storage->qsDefaultMap;
    }
  else if (((caddr_t)_STAR) == triple_qm_iri)
    tmp_tcc.tcc_top_allowed_qm = NULL;
  else
    {
      quad_map_t *top_qm = sparp_find_quad_map_by_name (triple_qm_iri);
      if (NULL == top_qm)
        spar_error (sparp, "QUAD MAP '%.200s' group pattern refers to undefined quad map", triple_qm_iri);
      tmp_tcc.tcc_top_allowed_qm = top_qm;
    }
  DO_BOX_FAST (quad_map_t *, qm, ctr, triple_storage->qsMjvMaps)
    {
      int status;
      if (0 != BOX_ELEMENTS_0 (qm->qmUserSubMaps))
        spar_error (sparp, "RDF quad mapping metadata are corrupted: MJV has submaps; the quad storage '%.200s' used in the query should be configured again", triple_storage_iri);
      if (SPART_QM_EMPTY & qm->qmMatchingFlags)
        spar_error (sparp, "RDF quad mapping metadata are corrupted: MJV is declared as empty; the quad storage '%.200s' used in the query should be configured again", triple_storage_iri);
      status = sparp_qm_find_triple_cases (sparp, &tmp_tcc, qm, 0, 1);
      if (SSG_QM_MATCH_AND_CUT == status)
        goto full_exclusive_match_detected;
    }
  END_DO_BOX_FAST;
  DO_BOX_FAST (quad_map_t *, qm, ctr, triple_storage->qsUserMaps)
    {
      int status = sparp_qm_find_triple_cases (sparp, &tmp_tcc, qm, 0, 1);
      if (SSG_QM_MATCH_AND_CUT == status)
        goto full_exclusive_match_detected;
    }
  END_DO_BOX_FAST;
  if (NULL != triple_storage->qsDefaultMap)
    sparp_qm_find_triple_cases (sparp, &tmp_tcc, triple_storage->qsDefaultMap, 0, 1);

full_exclusive_match_detected:
#if 0
  if (NULL == tmp_tcc.tcc_found_cases)
    spar_internal_error (sparp, "Empty quad map list :(");
#else
#ifdef DEBUG
  if (NULL == tmp_tcc.tcc_found_cases)
    {
      printf ("Empty quad map list:");
      printf ("\nStorage : "); dbg_print_box (triple_storage_iri, stdout);
      printf ("\nGraph   : "); dbg_print_box ((caddr_t)(triple->_.triple.tr_graph), stdout);
      printf ("\nSubj    : "); dbg_print_box ((caddr_t)(triple->_.triple.tr_subject), stdout);
      printf ("\nPred    : "); dbg_print_box ((caddr_t)(triple->_.triple.tr_predicate), stdout);
      printf ("\nObj     : "); dbg_print_box ((caddr_t)(triple->_.triple.tr_object), stdout);
      printf ("\n");
    }
#endif
#endif
  res_list = (triple_case_t **)t_revlist_to_array (tmp_tcc.tcc_found_cases);
  mp_box_tag_modify (res_list, DV_ARRAY_OF_LONG);
  while (tmp_tcc.tcc_found_cases) dk_set_pop (&(tmp_tcc.tcc_found_cases));
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    {
      dk_set_t cuts = tmp_tcc.tcc_cuts[fld_ctr];
      while (cuts) dk_set_pop (&cuts);
    }
  return res_list;
}

void
sparp_refresh_triple_cases (sparp_t *sparp, SPART *triple)
{
  SPART **sources = sparp->sparp_expr->_.req_top.sources;
  /*ssg_valmode_t valmodes[SPART_TRIPLE_FIELDS_COUNT];*/
  triple_case_t **new_cases;
  int old_cases_count, new_cases_count, ctr;
  int field_ctr;
  SPART *graph;
  int graph_type;
  int required_source_type;
  old_cases_count = BOX_ELEMENTS_0 (triple->_.triple.tc_list);
  if ((0 < old_cases_count) && (4 > old_cases_count))
    return;
  graph = triple->_.triple.tr_graph;
  graph_type = SPART_TYPE(graph);
  required_source_type = ((SPAR_VARIABLE == graph_type) ? SPART_GRAPH_NAMED : ((SPAR_BLANK_NODE_LABEL == graph_type) ? SPART_GRAPH_FROM : 0));
  new_cases = sparp_find_triple_cases (sparp, triple, sources, required_source_type);
  new_cases_count = BOX_ELEMENTS (new_cases);
  if ((NULL == triple->_.triple.tc_list) &&
    (0 == new_cases_count) &&
    sparp->sparp_env->spare_signal_void_variables )
    spar_error (sparp, "No one quad map pattern is suitable for GRAPH %s { %s %s %s } triple at line %ld",
      spar_dbg_string_of_triple_field (sparp, graph),
      spar_dbg_string_of_triple_field (sparp, triple->_.triple.tr_subject),
      spar_dbg_string_of_triple_field (sparp, triple->_.triple.tr_predicate),
      spar_dbg_string_of_triple_field (sparp, triple->_.triple.tr_object),
      (long) unbox(triple->srcline));
  for (field_ctr = SPART_TRIPLE_FIELDS_COUNT; field_ctr--; /*no step*/)
    {
      ssg_valmode_t field_valmode = SSG_VALMODE_AUTO;
      SPART *field_expn = triple->_.triple.tr_fields[field_ctr];
      rdf_val_range_t acc_rvr;
      int all_cases_make_only_refs = 1;
      memset (&acc_rvr, 0, sizeof (rdf_val_range_t));
      acc_rvr.rvrRestrictions = SPART_VARR_CONFLICT;
      for (ctr = 0; ctr < new_cases_count; ctr++)
        {
          triple_case_t *tc = new_cases [ctr];
          qm_value_t *qmv = SPARP_FIELD_QMV_OF_QM (tc->tc_qm, field_ctr);
          caddr_t fld_const = SPARP_FIELD_CONST_OF_QM (tc->tc_qm, field_ctr);
          ccaddr_t *red_cuts = tc->tc_red_cuts [field_ctr];
          rdf_val_range_t qmv_rvr;
          if (NULL != qmv)
            {
              qm_format_t *qmv_fmt = qmv->qmvFormat;
              if (all_cases_make_only_refs && !(SPART_VARR_IS_REF & qmv_fmt->qmfValRange.rvrRestrictions))
                all_cases_make_only_refs = 0;
              field_valmode = ssg_smallest_union_valmode (field_valmode, qmv_fmt);
              sparp_rvr_copy (sparp, &qmv_rvr, &(qmv->qmvRange));
              if (SPART_VARR_SPRINTFF & qmv_fmt->qmfValRange.rvrRestrictions)
                {
                  qmv_rvr.rvrRestrictions |= SPART_VARR_SPRINTFF;
                  sparp_rvr_add_sprintffs (sparp, &qmv_rvr, qmv_fmt->qmfValRange.rvrSprintffs, qmv_fmt->qmfValRange.rvrSprintffCount);
                }
              if ((NULL != qmv->qmvIriClass) &&
                !(SPART_VARR_IRI_CALC & qmv_rvr.rvrRestrictions) &&
                (SPART_VARR_IS_REF & qmv->qmvFormat->qmfValRange.rvrRestrictions) )
                {
                  qmv_rvr.rvrRestrictions |= SPART_VARR_IRI_CALC;
                  sparp_rvr_add_iri_classes (sparp, &qmv_rvr, &(qmv->qmvIriClass), 1);
                }
              sparp_rvr_intersect_red_cuts (sparp, &qmv_rvr, red_cuts, BOX_ELEMENTS_0 (red_cuts));
            }
          else if (NULL != fld_const)
            {
              if (NULL != qmv)
                spar_internal_error (sparp, "Invalid quad map storage metadata: quad map has set both quad map value and a constant for same field.");
              memset (&qmv_rvr, 0, sizeof (rdf_val_range_t));
              /* qmv_rvr.rvrRestrictions |= SPART_VARR_FIXED | SPART_VARR_NOT_NULL; This is set below */
#ifdef DEBUG
              if ((SPART_TRIPLE_GRAPH_IDX == field_ctr) && (DV_UNAME != DV_TYPE_OF (fld_const)))
                GPF_T1("sparp_" "refresh_triple_cases(): const GRAPH field of qm is not a UNAME");
#endif
              qmv_rvr.rvrFixedValue = fld_const;
              if (all_cases_make_only_refs && (DV_UNAME != DV_TYPE_OF (fld_const)))
                all_cases_make_only_refs = 0;
            }
          else
            spar_internal_error (sparp, "Invalid quad map storage metadata: neither quad map value nor a constant is set for a field of a quad map.");
          if ((NULL != qmv_rvr.rvrFixedValue) && !(SPART_VARR_FIXED & qmv_rvr.rvrRestrictions))
            {
              if (DV_UNAME == DV_TYPE_OF (qmv_rvr.rvrFixedValue))
                qmv_rvr.rvrRestrictions |= (SPART_VARR_FIXED | SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_NOT_NULL);
              else
                qmv_rvr.rvrRestrictions |= (SPART_VARR_FIXED | SPART_VARR_IS_LIT | SPART_VARR_NOT_NULL);
            }
          if (NULL != qmv)
            sparp_rvr_tighten (sparp, &qmv_rvr, &(qmv->qmvFormat->qmfValRange), ~SPART_VARR_IRI_CALC);
          sparp_rvr_loose (sparp, &acc_rvr, &qmv_rvr, ~0);
        }
      if (all_cases_make_only_refs && (SSG_VALMODE_LONG == field_valmode))
        field_valmode = SSG_VALMODE_SQLVAL;
      sparp_jso_validate_format (sparp, field_valmode);
      triple->_.triple.native_formats[field_ctr] = field_valmode;
      if (SPAR_IS_BLANK_OR_VAR (field_expn))
        sparp_rvr_tighten (sparp, &(field_expn->_.var.rvr), &acc_rvr, ~(SPART_VARR_EXTERNAL | SPART_VARR_GLOBAL));
    }
  triple->_.triple.tc_list = new_cases;
}

int
sparp_detach_conflicts (sparp_t *sparp, SPART *parent_gp)
{
  int memb_ctr;
  DO_BOX_FAST_REV (SPART *, memb, memb_ctr, parent_gp->_.gp.members)
    { /* countdown direction of 'for' is important due to possible removals */
      int eq_ctr;
      if (SPAR_GP != memb->type)
        continue;
      switch (memb->_.gp.subtype)
        {
        case OPTIONAL_L:
          SPARP_FOREACH_GP_EQUIV (sparp, memb, eq_ctr, eq)
            {
              if ((SPART_VARR_CONFLICT & eq->e_rvr.rvrRestrictions) &&
                (SPART_VARR_NOT_NULL & eq->e_rvr.rvrRestrictions) )
                goto do_detach_memb; /* see below */
            }
          END_SPARP_FOREACH_GP_EQUIV;
          continue;
        case UNION_L:
          if (0 == BOX_ELEMENTS_0 (memb->_.gp.members))
            goto do_detach_memb; /* see below */
          continue;
        default:
          SPARP_FOREACH_GP_EQUIV (sparp, memb, eq_ctr, eq)
            {
              if (((SPART_VARR_CONFLICT | SPART_VARR_ALWAYS_NULL) & eq->e_rvr.rvrRestrictions) &&
                (SPART_VARR_NOT_NULL & eq->e_rvr.rvrRestrictions) )
                goto do_detach_or_zap; /* see below */
            }
          END_SPARP_FOREACH_GP_EQUIV;
          continue;
        }
do_detach_or_zap:
      if (UNION_L != parent_gp->_.gp.subtype)
        {
          sparp_gp_produce_nothing (sparp, parent_gp);
          return 1;
        }
do_detach_memb:
      sparp_gp_detach_member (sparp, parent_gp, memb_ctr, NULL);
      sparp_gp_deprecate (sparp, memb, 1);
    }
  END_DO_BOX_FAST_REV;
  return 0;
}

void
sparp_flatten_union (sparp_t *sparp, SPART *parent_gp)
{
  int memb_ctr;
#ifdef DEBUG
  if (SPAR_GP != SPART_TYPE (parent_gp))
    spar_internal_error (sparp, "sparp_" "flatten_union(): parent_gp is not a GP");
  if (UNION_L != parent_gp->_.gp.subtype)
    spar_internal_error (sparp, "sparp_" "flatten_union(): parent_gp is not a union");
#endif
  for (memb_ctr = BOX_ELEMENTS (parent_gp->_.gp.members); memb_ctr--; /*no step*/)
    {
      SPART *memb = parent_gp->_.gp.members [memb_ctr];
      if ((SPAR_GP == SPART_TYPE (memb)) &&
        (0 == BOX_ELEMENTS_0 (memb->_.gp.filters)) && /* This condition might be commented out if memb_filters and memb_filters_count below are uncommented */
        (NULL == memb->_.gp.options) &&
        ((UNION_L == memb->_.gp.subtype) ||
          ((0 == memb->_.gp.subtype) &&
            (1 == BOX_ELEMENTS (memb->_.gp.members)) &&
            (SPAR_GP == SPART_TYPE (memb->_.gp.members[0])) ) ) )
        {
          int sub_count = BOX_ELEMENTS (memb->_.gp.members);
          int sub_ctr;
          /* SPART **memb_filters = sparp_gp_detach_all_filters (sparp, memb, NULL); */
          /* int memb_filters_count = BOX_ELEMENTS_0 (memb_filters); */
          for (sub_ctr = sub_count; sub_ctr--; /* no step */)
            {
              SPART *sub_memb = sparp_gp_detach_member (sparp, memb, sub_ctr, NULL);
              sparp_gp_attach_member (sparp, parent_gp, sub_memb, memb_ctr, NULL);
              /* if (0 != memb_filters_count)
                sparp_gp_attach_many_filters (sparp, sub_memb, sparp_treelist_full_copy (sparp, memb_filters, NULL), 0, NULL); */
            }
          memb_ctr += sub_count;
          sparp_gp_detach_member (sparp, parent_gp, memb_ctr, NULL);
          sparp_gp_deprecate (sparp, memb, 0);
        }
    }
}


void
sparp_flatten_join (sparp_t *sparp, SPART *parent_gp)
{
  int memb_ctr;
#ifdef DEBUG
  if (SPAR_GP != SPART_TYPE (parent_gp))
    spar_internal_error (sparp, "sparp_" "flatten_join(): parent_gp is not a GP");
  if ((UNION_L == parent_gp->_.gp.subtype) || (SELECT_L == parent_gp->_.gp.subtype))
    spar_internal_error (sparp, "sparp_" "flatten_join(): parent_gp is not a join");
#endif
  for (memb_ctr = BOX_ELEMENTS (parent_gp->_.gp.members); memb_ctr--; /*no step*/)
    {
      SPART *memb = parent_gp->_.gp.members [memb_ctr];
      SPART **memb_filters;
      int sub_count;
      int sub_ctr;
      int memb_filters_count;
      if (SPAR_GP != SPART_TYPE (memb))
        continue; /* It's plain triple, it can't be simpler than that */
      if (OPTIONAL_L == memb->_.gp.subtype)
        break; /* No optimizations at left of LEFT OUTER JOIN. */
      if (NULL != memb->_.gp.options)
        continue; /* Members with options can not be optimized */
      sub_count = BOX_ELEMENTS (memb->_.gp.members);
      if ((UNION_L == memb->_.gp.subtype) && (1 == sub_count))
        goto just_remove_braces; /* see below */
      if (0 == memb->_.gp.subtype)
        {
          int memb_equiv_inx;
          int first_conflicting_predecessor_idx;
          int first_optional_sub_memb_pos;
          if (0 == sub_count)
            {
              sparp_gp_produce_nothing (sparp, parent_gp);
              return;
            }
          if (0 == memb_ctr)
            goto just_remove_braces; /* see below */
/* First member can always be flatten. For others, there is an exception.
If a member in question contains OPTIONAL then flattening will change the left member of left outer join.
This may change semantics if an OPTIONAL contains a variable that is nullable in the member but present in members before the current. */
          first_optional_sub_memb_pos = -1;
          for (sub_ctr = 0; sub_ctr < sub_count; sub_ctr++)
            {
              SPART *sub_memb = memb->_.gp.members [sub_ctr];
              if ((SPAR_GP != SPART_TYPE(sub_memb)) || (OPTIONAL_L != sub_memb->_.gp.subtype))
                continue;
              first_optional_sub_memb_pos = sub_ctr;
              break;
            }
          if (-1 == first_optional_sub_memb_pos)
            goto just_remove_braces; /* see below */
          first_conflicting_predecessor_idx = -1; /* No conflicts if there are no OPTIONALs in the member, btw. */
          SPARP_FOREACH_GP_EQUIV (sparp, memb, memb_equiv_inx, memb_eq)
            {
              int parent_conn_ctr;
              if (SPART_VARR_NOT_NULL & memb_eq->e_rvr.rvrRestrictions)
                continue;
              DO_BOX_FAST (ptrlong, parent_equiv_idx, parent_conn_ctr, memb_eq->e_receiver_idxs)
                {
                  sparp_equiv_t *parent_equiv = SPARP_EQUIV (sparp, parent_equiv_idx);
                  int preceding_memb_ctr;
                  for (preceding_memb_ctr = memb_ctr-1; preceding_memb_ctr > first_conflicting_predecessor_idx; preceding_memb_ctr--)
                    {
                      SPART *preceding_memb = parent_gp->_.gp.members [preceding_memb_ctr];
                      if (sparp_tree_uses_var_of_eq (sparp, preceding_memb, parent_equiv))
                        {
                          first_conflicting_predecessor_idx = preceding_memb_ctr;
                          break;
                        }
                    }
                }
              END_DO_BOX_FAST;
            }
          END_SPARP_FOREACH_GP_EQUIV;
          if (-1 == first_conflicting_predecessor_idx)
            goto just_remove_braces; /* see below */
#if 0
/* If there are things between first conflicting predecessor and the  */
          if ((memb_ctr-1) == first_conflicting_predecessor_idx)
            continue;
/*!!! TBD: moving members from first_conflicting_predecessor_idx+1 to memb_ctr-1 inclusive into left part of memb if appropriate */
#endif
        }
      continue;

just_remove_braces:
      if (0 != memb->_.gp.glued_filters_count)
        {
          int glued_last_idx = BOX_ELEMENTS (memb->_.gp.filters);
          int glued_first_idx = glued_last_idx - memb->_.gp.glued_filters_count;
          sparp_equiv_t *suspicious_filt_eq = NULL;
          int glued_idx, memb_equiv_inx;
          if (parent_gp->_.gp.glued_filters_count)
            continue; /* Don't know how to safely mix two lists of glued filters, one already in parent and one from member, hence the sabotage */
/* Consider a glued filter in memb that refers to ?x . ?x may present in memb or not, it may also present in parent_gp or not.
?x in memb	| ?x in parent	| Can filter be moved?
Yes & bound	| Yes & bound	| These two are equal due to join so filter can be moved
Yes & bound	| Yes & !bound	| Empty join, filter does not matter, so it can be moved
Yes & bound	| No		| Safe to move, the only occurence will define the value as it was
Yes & !bound	| Yes & bound	| Empty join, filter does not matter, so it can be moved
Yes & !bound	| Yes & !bound	| Empty join, filter does not matter, so it can be moved
Yes & !bound	| No		| Safe to move, the only occurence will define the value as it was
No		| Yes & bound	| !!! Can't move, not bound may become bound
No		| Yes & !bound	| Safe to move, unbound anyway
No		| No		| Safe to move, unbound anyway
So the only unsafe case is a fixed filter on a variable that is missing where the filter resides but present at the parent.
*/
          SPARP_FOREACH_GP_EQUIV (sparp, memb, memb_equiv_inx, memb_eq)
            {
              int parent_conn_ctr;
              if (SPART_VARR_NOT_NULL & memb_eq->e_rvr.rvrRestrictions)
                continue;
              DO_BOX_FAST (ptrlong, parent_equiv_idx, parent_conn_ctr, memb_eq->e_receiver_idxs)
                {
                  sparp_equiv_t *parent_equiv = SPARP_EQUIV (sparp, parent_equiv_idx);
                  int glued_idx;
                  for (glued_idx = glued_first_idx; glued_idx < glued_last_idx; glued_idx++)
                    {
                      SPART *glued_filt = memb->_.gp.filters[glued_idx];
                      if (sparp_tree_uses_var_of_eq (sparp, glued_filt, parent_equiv))
                        {
                          suspicious_filt_eq = memb_eq;
                          goto suspicious_filt_eq_found; /* see below */
                        }
                    }
                }
              END_DO_BOX_FAST;
            }
          END_SPARP_FOREACH_GP_EQUIV;
suspicious_filt_eq_found:
          if (NULL != suspicious_filt_eq)
            continue;
          for (glued_idx = glued_first_idx; glued_idx < glued_last_idx; glued_idx++)
            {
              SPART *filt = sparp_gp_detach_filter (sparp, memb, glued_first_idx, NULL);
              sparp_gp_attach_filter (sparp, parent_gp, filt, BOX_ELEMENTS (parent_gp->_.gp.filters), NULL);
            }
          parent_gp->_.gp.glued_filters_count += (glued_last_idx - glued_first_idx);
        }
      memb_filters = sparp_gp_detach_all_filters (sparp, memb, NULL);
      memb_filters_count = BOX_ELEMENTS_0 (memb_filters);
      for (sub_ctr = sub_count; sub_ctr--; /* no step */)
        {
          SPART *sub_memb = sparp_gp_detach_member (sparp, memb, sub_ctr, NULL);
          sparp_gp_attach_member (sparp, parent_gp, sub_memb, memb_ctr, NULL);
        }
      if (0 != memb_filters_count)
        sparp_gp_attach_many_filters (sparp, parent_gp, memb_filters /*!!! should it be sparp_treelist_full_copy (sparp, memb_filters, NULL) ? */, 0, NULL);
      memb_ctr += sub_count;
      sparp_gp_tighten_by_eq_replaced_filters (sparp, parent_gp, memb, 1);
      sparp_gp_detach_member (sparp, parent_gp, memb_ctr, NULL);
      sparp_gp_deprecate (sparp, memb, 0);
    }
}

void
sparp_set_options_selid_and_tabid (sparp_t *sparp, SPART **options, caddr_t new_selid, caddr_t new_tabid)
{
  int ctr;
  DO_BOX_FAST_REV (SPART *, opt_expn, ctr, options)
    {
      if (!SPAR_IS_BLANK_OR_VAR (opt_expn))
        continue;
      if (strcmp (opt_expn->_.var.selid, new_selid)) /* weird re-location */
        {
          if (SPART_BAD_EQUIV_IDX != opt_expn->_.var.equiv_idx)
            {
              sparp_equiv_t *eq = SPARP_EQUIV (sparp, opt_expn->_.var.equiv_idx);
              sparp_equiv_remove_var (sparp, eq, opt_expn);
            }
          opt_expn->_.var.selid = /*t_box_copy*/ (new_selid);
        }
      if (NULL != opt_expn->_.var.tabid)
        opt_expn->_.var.tabid = /*t_box_copy*/ (new_tabid);
    }
  END_DO_BOX_FAST_REV;
}

void
sparp_set_triple_selid_and_tabid (sparp_t *sparp, SPART *triple, caddr_t new_selid, caddr_t new_tabid)
{
  int field_ctr;
  if (
    !strcmp (triple->_.triple.tabid, new_tabid) &&
    !strcmp (triple->_.triple.selid, new_selid) )
    return;
  for (field_ctr = SPART_TRIPLE_FIELDS_COUNT; field_ctr--; /*no step*/)
    {
      SPART *fld_expn = triple->_.triple.tr_fields[field_ctr];
      if (!SPAR_IS_BLANK_OR_VAR (fld_expn))
        continue;
      if (strcmp (fld_expn->_.var.selid, new_selid)) /* weird re-location */
        {
          if (SPART_BAD_EQUIV_IDX != fld_expn->_.var.equiv_idx)
            {
              sparp_equiv_t *eq = SPARP_EQUIV (sparp, fld_expn->_.var.equiv_idx);
              sparp_equiv_remove_var (sparp, eq, fld_expn);
            }
          fld_expn->_.var.selid = /*t_box_copy*/ (new_selid);
        }
      fld_expn->_.var.tabid = /*t_box_copy*/ (new_tabid);
    }
  if (NULL != triple->_.triple.options)
    sparp_set_options_selid_and_tabid (sparp, triple->_.triple.options, new_selid, new_tabid);
  triple->_.triple.selid = /*t_box_copy*/ (new_selid);
  triple->_.triple.tabid = /*t_box_copy*/ (new_tabid);
}


#if 0
int
sparp_set_retval_selid_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_IS_BLANK_OR_VAR (curr))
    curr->_.var.selid = t_box_copy (common_env);
  return 0;
}

void
sparp_set_retval_and_order_selid (sparp_t *sparp)
{
  int ctr;
  sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
  caddr_t top_gp_selid = sparp->sparp_expr->_.req_top.pattern->_.gp.selid;
  DO_BOX_FAST (SPART *, filt, ctr, sparp->sparp_expr->_.req_top.retvals)
    {
      sparp_gp_trav_int (sparp, filt, stss + 1, top_gp_selid,
        NULL, NULL,
        sparp_set_retval_selid_cbk, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
  DO_BOX_FAST (SPART *, grouping, ctr, sparp->sparp_expr->_.req_top.groupings)
    {
      sparp_gp_trav_int (sparp, grouping, stss + 1, top_gp_selid,
        NULL, NULL,
        sparp_set_retval_selid_cbk, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
  if (NULL != sparp->sparp_expr->_.req_top.having)
    {
      sparp_gp_trav_int (sparp, sparp->sparp_expr->_.req_top.having, stss + 1, top_gp_selid,
        NULL, NULL,
        sparp_set_retval_selid_cbk, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
  DO_BOX_FAST (SPART *, oby, ctr, sparp->sparp_expr->_.req_top.order)
    {
      sparp_gp_trav_int (sparp, oby->_.oby.expn, stss + 1, top_gp_selid,
        NULL, NULL,
        sparp_set_retval_selid_cbk, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
}
#endif

int
sparp_expns_are_equal (sparp_t *sparp, SPART *one, SPART *two)
{
  ptrlong one_type = SPART_TYPE (one);
  if (one == two)
    return 1;
  if (SPART_TYPE (two) != one_type)
    return 0;
  switch (one_type)
    {
    case SPAR_BLANK_NODE_LABEL: case SPAR_VARIABLE:
      return !strcmp (one->_.var.vname, two->_.var.vname);
    case SPAR_QNAME: /* case SPAR_QNAME_NS: */
      return sparp_expns_are_equal (sparp, (SPART *)(one->_.lit.val), (SPART *)(two->_.lit.val));
    case SPAR_LIT:
      if (DV_TYPE_OF (one) != DV_TYPE_OF (two))
        return 0;
      if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (one))
        return (DVC_MATCH == cmp_boxes ((caddr_t)one, (caddr_t)two, NULL, NULL));
      else
        return (
          sparp_expns_are_equal (sparp, (SPART *)(one->_.lit.val), (SPART *)(two->_.lit.val)) &&
          sparp_expns_are_equal (sparp, (SPART *)(one->_.lit.datatype), (SPART *)(two->_.lit.datatype)) &&
          sparp_expns_are_equal (sparp, (SPART *)(one->_.lit.language), (SPART *)(two->_.lit.language)) );
    case SPAR_BUILT_IN_CALL:
      return (
        (one->_.builtin.btype == two->_.builtin.btype) &&
        sparp_expn_lists_are_equal (sparp, one->_.builtin.args, two->_.builtin.args) );
    case SPAR_FUNCALL:
      return (
        (one->_.funcall.agg_mode == two->_.funcall.agg_mode) &&
        !strcmp (one->_.funcall.qname, two->_.funcall.qname) &&
        sparp_expn_lists_are_equal (sparp, one->_.funcall.argtrees, two->_.funcall.argtrees) );
    case SPAR_GP:
      return !strcmp (one->_.gp.selid, two->_.gp.selid); /*!!!TBD: this check is good enough for TPC-D Q16. Do we need more accurate check? */
    case BOP_EQ: case SPAR_BOP_EQ: case BOP_NEQ:
    case BOP_AND: case BOP_OR:
    case BOP_SAME: case BOP_NSAME:
      return (
        ( sparp_expns_are_equal (sparp, one->_.bin_exp.left, two->_.bin_exp.left) &&
          sparp_expns_are_equal (sparp, one->_.bin_exp.right, two->_.bin_exp.right) ) ||
        ( sparp_expns_are_equal (sparp, one->_.bin_exp.left, two->_.bin_exp.right) &&
          sparp_expns_are_equal (sparp, one->_.bin_exp.right, two->_.bin_exp.left) ) );
    case BOP_LT: case BOP_LTE: case BOP_GT: case BOP_GTE:
    /*case BOP_LIKE: Like is built-in in SPARQL, not a BOP! */
    case BOP_PLUS: case BOP_MINUS: case BOP_TIMES: case BOP_DIV: case BOP_MOD:
      return (
        sparp_expns_are_equal (sparp, one->_.bin_exp.left, two->_.bin_exp.left) &&
        sparp_expns_are_equal (sparp, one->_.bin_exp.right, two->_.bin_exp.right) );
    case BOP_NOT:
      return sparp_expns_are_equal (sparp, one->_.bin_exp.left, two->_.bin_exp.left);
/* Add more cases right above this line when introducing more SPAR_nnn constants that may appear inside expression */
    default: spar_internal_error (sparp, "sparp_" "expns_are_equal () get expression of unsupported type");
    }
  GPF_T;
  return 0;
}

int
sparp_expn_lists_are_equal (sparp_t *sparp, SPART **one, SPART **two)
{
  int ctr, one_len = BOX_ELEMENTS_0 (one);
  if (BOX_ELEMENTS_0 (two) != one_len)
    return 0;
  for (ctr = 0; ctr < one_len; ctr++)
    {
      if (!sparp_expns_are_equal (sparp, one[ctr], two[ctr]))
        return 0;
    }
  return 1;
}

#if 0
int
sparp_set_special_order_selid_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART *new_gp = (SPART *)common_env;
  if (SPAR_IS_BLANK_OR_VAR (curr))
    {
      sparp_equiv_t *eq;
/* !!! TBD: replace with silent detach if needed.
      int idx = curr->_.var.equiv_idx;
      if (SPART_BAD_EQUIV_IDX != idx)
        spar_internal_error (sparp, "sparp_" "set_special_order_selid(): attempt to attach a filter with used variable"); */
      curr->_.var.selid = t_box_copy (new_gp->_.gp.selid);
      eq = sparp_equiv_get (sparp, new_gp, curr, SPARP_EQUIV_INS_VARIABLE/* !!!TBD: add | SPARP_EQUIV_ADD_CONST_READ*/);
      if (NULL == eq)
        spar_internal_error (sparp, "sparp_" "set_special_order_selid(): variable in order by comes from nowhere");
      curr->_.var.equiv_idx = eq->e_own_idx;
    }
  return 0;
}

void
sparp_set_special_order_selid (sparp_t *sparp, SPART *new_gp)
{
  int ctr;
  sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
  DO_BOX_FAST (SPART *, oby, ctr, sparp->sparp_expr->_.req_top.order)
    {
      sparp_gp_trav_int (sparp, oby->_.oby.expn, stss + 1, new_gp,
        NULL, NULL,
        sparp_set_special_order_selid_cbk, NULL, NULL,
        NULL );
    }
  END_DO_BOX_FAST;
}
#endif

SPART **
sparp_make_qm_cases (sparp_t *sparp, SPART *triple, SPART *parent_gp)
{
  triple_case_t **tc_list = triple->_.triple.tc_list;
  SPART *ft_cond_to_relocate = NULL;
  SPART **res;
  int tc_idx;
#ifdef DEBUG
  if (1 >= BOX_ELEMENTS (triple->_.triple.tc_list))
    spar_internal_error (sparp, "sparp_" "make_qm_cases(): redundant call");
#endif
  if (triple->_.triple.ft_type)
    {
      int filt_ctr;
      DO_BOX_FAST_REV (SPART *, filt, filt_ctr, parent_gp->_.gp.filters)
        {
          if (spar_filter_is_freetext (sparp, filt, triple))
            {
              ft_cond_to_relocate = sparp_gp_detach_filter (sparp, parent_gp, filt_ctr, NULL);
              break;
            }
        }
      END_DO_BOX_FAST_REV;
      if (NULL == ft_cond_to_relocate)
        spar_error (sparp, "optimizer can not process a combination of quad map patterns and free-text condition for variable ?%.200s",
          triple->_.triple.tr_object->_.var.vname );
    }
  res = (SPART **)t_alloc_box (box_length (tc_list), DV_ARRAY_OF_POINTER);
  DO_BOX_FAST (triple_case_t *, tc, tc_idx, tc_list)
    {
      quad_map_t * qm = tc->tc_qm;
      int field_ctr;
      SPART *qm_case_triple = (SPART *)t_box_copy ((caddr_t)triple);
      SPART *qm_case_gp = sparp_new_empty_gp (sparp, 0, unbox (triple->srcline));
      caddr_t qm_selid = qm_case_gp->_.gp.selid;
      caddr_t qm_tabid = t_box_sprintf (100, "%s-qm%d", triple->_.triple.tabid, tc_idx);
      triple_case_t **one_tc = (triple_case_t **)t_list (1, tc);
      mp_box_tag_modify (one_tc, DV_ARRAY_OF_LONG);
      qm_case_triple->_.triple.tc_list = one_tc;
      qm_case_triple->_.triple.selid = /*t_box_copy*/ (qm_selid);
      qm_case_triple->_.triple.tabid = qm_tabid;
      for (field_ctr = SPART_TRIPLE_FIELDS_COUNT; field_ctr--; /*no step*/)
        {
          SPART *fld_expn = triple->_.triple.tr_fields[field_ctr];
          qm_value_t *fld_qmv = SPARP_FIELD_QMV_OF_QM (qm,field_ctr);
          caddr_t fld_const = SPARP_FIELD_CONST_OF_QM (qm,field_ctr);
          ccaddr_t *fld_tc_cuts = tc->tc_red_cuts [field_ctr];
          SPART *new_fld_expn;
          qm_format_t *native_fmt;
          if (SPAR_IS_BLANK_OR_VAR (fld_expn))
            {
              sparp_equiv_t *eq;
              new_fld_expn = (SPART *)t_box_copy ((caddr_t)fld_expn);
              new_fld_expn->_.var.selid = /*t_box_copy*/ (qm_selid);
              new_fld_expn->_.var.tabid = /*t_box_copy*/ (qm_tabid);
              new_fld_expn->_.var.vname = /*t_box_copy*/ (fld_expn->_.var.vname);
              new_fld_expn->_.var.equiv_idx = SPART_BAD_EQUIV_IDX;
              sparp_rvr_copy (sparp, &(new_fld_expn->_.var.rvr), &(fld_expn->_.var.rvr));
              eq = sparp_equiv_get (sparp, qm_case_gp, new_fld_expn, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_INS_VARIABLE | SPARP_EQUIV_ADD_GSPO_USE);
              eq->e_rvr.rvrRestrictions |= (new_fld_expn->_.var.rvr.rvrRestrictions & (SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL));
              if (NULL == fld_qmv)
                sparp_equiv_restrict_by_constant (sparp, eq, NULL, (SPART *)fld_const);
              else
                {
                  sparp_equiv_tighten (sparp, eq, &(fld_qmv->qmvRange), ~SPART_VARR_IRI_CALC);
                  sparp_equiv_tighten (sparp, eq, &(fld_qmv->qmvFormat->qmfValRange), ~SPART_VARR_IRI_CALC);
                }
              if (NULL != fld_tc_cuts)
                sparp_rvr_add_red_cuts (sparp, &(eq->e_rvr), fld_tc_cuts, BOX_ELEMENTS (fld_tc_cuts));
              sparp_rvr_tighten (sparp, (&new_fld_expn->_.var.rvr), &(eq->e_rvr), ~0 /* not (SPART_VARR_EXTERNAL | SPART_VARR_GLOBAL)*/);
            }
          else
            new_fld_expn = sparp_tree_full_copy (sparp, fld_expn, NULL);
          qm_case_triple->_.triple.tr_fields[field_ctr] = new_fld_expn;
          native_fmt = ((NULL != fld_qmv) ? fld_qmv->qmvFormat : SSG_VALMODE_AUTO);
          sparp_jso_validate_format (sparp, native_fmt);
          qm_case_triple->_.triple.native_formats[field_ctr] = native_fmt;
        }
      if (NULL != triple->_.triple.options)
        {
          qm_case_triple->_.triple.options = sparp_treelist_full_copy (sparp, triple->_.triple.options, parent_gp);
          sparp_set_options_selid_and_tabid (sparp, qm_case_triple->_.triple.options, qm_selid, qm_tabid);
        }
      sparp_gp_attach_member (sparp, qm_case_gp, qm_case_triple, 0, NULL);
      if (NULL != ft_cond_to_relocate)
        sparp_gp_attach_filter (sparp, qm_case_gp, sparp_tree_full_copy (sparp, ft_cond_to_relocate, parent_gp), 0, NULL);
      res [tc_idx] = qm_case_gp;
    }
  END_DO_BOX_FAST;
  return res;
}

SPART *
sparp_new_empty_gp (sparp_t *sparp, ptrlong subtype, ptrlong srcline)
{
  SPART *res = spartlist (sparp, 10,
    SPAR_GP, subtype,
    t_list (0),
    t_list (0),
    NULL,
    spar_mkid (sparp, "s"),
    NULL, (ptrlong)(0), (ptrlong)(0), NULL );
  res->srcline = t_box_num (srcline);
  return res;
}

void
sparp_gp_produce_nothing (sparp_t *sparp, SPART *curr)
{
  int eq_ctr;
  SPARP_REVFOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
    {
      int recv_eq_ctr;
      if (SPART_VARR_NOT_NULL & eq->e_rvr.rvrRestrictions)
        eq->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
      else
        eq->e_rvr.rvrRestrictions |= SPART_VARR_ALWAYS_NULL;
      DO_BOX_FAST (ptrlong, recv_eq_idx, recv_eq_ctr, eq->e_receiver_idxs)
        {
          sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, recv_eq_idx);
          if ((UNION_L != recv_eq->e_gp->_.gp.subtype) && (OPTIONAL_L != curr->_.gp.subtype))
            recv_eq->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
          sparp_equiv_disconnect (sparp, recv_eq, eq);
        }
      END_DO_BOX_FAST;
      eq->e_replaces_filter = 0;
    }
  END_SPARP_REVFOREACH_GP_EQUIV;
  curr->_.gp.glued_filters_count = 0; /* The (now redundant) glue may prevent us from detaching some filters */
  sparp_gp_detach_all_filters (sparp, curr, NULL);
  while (0 < BOX_ELEMENTS (curr->_.gp.members))
    {
      SPART *memb = sparp_gp_detach_member (sparp, curr, 0, NULL);
      if (SPAR_GP == memb->type)
        sparp_gp_deprecate (sparp, memb, 1);
    }
}

int sparp_gp_trav_refresh_triple_cases (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  switch (curr->type)
    {
    case SPAR_TRIPLE:
      sparp_refresh_triple_cases (sparp, curr);
      return SPAR_GPT_NODOWN /*| SPAR_GPT_NOOUT */;
    case SPAR_GP:
      if (SELECT_L == curr->_.gp.subtype)
        {
          sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
          if (sparp_rewrite_qm_optloop (sub_sparp, (ptrlong)common_env))
            sparp->sparp_rewrite_dirty = 1;
          sparp_up_from_sub (sparp, curr, sub_sparp);
          return SPAR_GPT_NODOWN /*| SPAR_GPT_NOOUT */;
        }
      break;
    }
  return 0;
}


int sparp_gp_trav_multiqm_to_unions (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int memb_ctr;
  if (SPAR_GP != curr->type) /* Not a gp ? -- nothing to do */
    return 0;
  if (sparp_detach_conflicts (sparp, curr))
    return 0;
  DO_BOX_FAST_REV (SPART *, memb, memb_ctr, curr->_.gp.members)
    { /* countdown direction of 'for' is important due to possible insertions/removals */
      int tc_count;
      SPART **qm_cases;
      int case_ctr;
      if (SPAR_TRIPLE != memb->type)
        continue;
      tc_count = BOX_ELEMENTS (memb->_.triple.tc_list);
      if (1 == tc_count)
        continue;
      if (0 == tc_count)
        {
          if (UNION_L != curr->_.gp.subtype)
            {
              sparp_gp_produce_nothing (sparp, curr);
              return SPAR_GPT_NODOWN;
            }
          sparp_gp_detach_member (sparp, curr, memb_ctr, NULL);
          continue;
        }
      sparp_gp_detach_member (sparp, curr, memb_ctr, NULL);
      qm_cases = sparp_make_qm_cases (sparp, memb, curr);
      if (UNION_L == curr->_.gp.subtype)
        {
          DO_BOX_FAST_REV (SPART *, qm_case, case_ctr, qm_cases)
            {
              sparp_gp_attach_member (sparp, curr, qm_case, memb_ctr, NULL);
            }
          END_DO_BOX_FAST_REV;
        }
      else
        {
          SPART *case_union = sparp_new_empty_gp (sparp, UNION_L, unbox (memb->srcline));
          DO_BOX_FAST_REV (SPART *, qm_case, case_ctr, qm_cases)
            {
              sparp_gp_attach_member (sparp, case_union, qm_case, 0, NULL);
            }
          END_DO_BOX_FAST_REV;
          sparp_gp_attach_member (sparp, curr, case_union, memb_ctr, NULL);
        }
    }
  END_DO_BOX_FAST_REV;
  if (UNION_L == curr->_.gp.subtype)
    sparp_flatten_union (sparp, curr);
  else if (SELECT_L != curr->_.gp.subtype)
    sparp_flatten_join (sparp, curr);
  return 0;
}


int sparp_gp_trav_detach_conflicts_out (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_GP != curr->type) /* Not a gp ? -- nothing to do */
    return 0;
  if (sparp_detach_conflicts (sparp, curr))
    return 0;
  if (UNION_L == curr->_.gp.subtype)
    sparp_flatten_union (sparp, curr);
  else if (SELECT_L != curr->_.gp.subtype)
    sparp_flatten_join (sparp, curr);
  return 0;
}

int sparp_gp_trav_1var (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  SPART **single_var_ptr = (SPART **)common_env;
  if (!SPAR_IS_BLANK_OR_VAR (curr))
    return 0;
  if (SPART_VARNAME_IS_GLOB (curr->_.var.vname))
    return 0;
  if (NULL != single_var_ptr[0])
    {
      single_var_ptr[0] = (SPART *)(1L);
      return SPAR_GPT_COMPLETED;
    }
  single_var_ptr[0] = curr;
  return SPAR_GPT_NODOWN;
}

#if 0 /*!!!TBD: support of filter localization for services */
typedef struct sparp_filter_relocation_details_s {
  SPART **sfrd_var;		/*!< A non-global non-external variable used in filter */
  ptrlong sfrd_var_count;	/*!< Count of distinct non-global non-external variables found in filter */
  ptrlong sfrd_syntax;		/*!< Syntax features found in filter */
} sparp_filter_relocation_details_t;
#endif

int
sparp_gp_trav_localize_filters (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int filt_ctr, filt_count;
  if (SPAR_GP != curr->type) /* Not a gp ? -- nothing to do */
    return 0;
  filt_count = BOX_ELEMENTS_0 (curr->_.gp.filters) - curr->_.gp.glued_filters_count; /* Glued filters should not be localized, that's what they're glued for */
  for (filt_ctr = filt_count; filt_ctr--; /* no step */)
    {
      SPART *filt = curr->_.gp.filters[filt_ctr];
      SPART *single_var = NULL;
      sparp_equiv_t *sv_eq;
      int filt_is_detached = 0;
      int subval_ctr, subval_count, subval_unions_count, subval_in_unions_count, localize_in_unions, localizations_left;
      sparp_gp_trav_int (sparp, filt, sts_this, &(single_var),
        NULL, NULL,
        sparp_gp_trav_1var, NULL, NULL,
        NULL );
      if (!IS_BOX_POINTER (single_var))
        continue;
      sv_eq = SPARP_EQUIV(sparp, single_var->_.var.equiv_idx);
      if (0 != sv_eq->e_gspo_uses)
        continue; /* The filter can not be detached here so it may be localized on every loop, resulting in redundant localized filters */
      subval_count = BOX_ELEMENTS_0 (sv_eq->e_subvalue_idxs);
      subval_unions_count = 0;
      subval_in_unions_count = 0;
      localize_in_unions = 1;
      DO_BOX_FAST_REV (ptrlong, subval_eq_idx, subval_ctr, sv_eq->e_subvalue_idxs)
        {
          sparp_equiv_t *sub_eq = SPARP_EQUIV (sparp, subval_eq_idx);
          SPART *sub_gp = sub_eq->e_gp;
          switch (sub_gp->_.gp.subtype)
            {
            case UNION_L:
              if (!(SPART_VARR_NOT_NULL & sv_eq->e_rvr.rvrRestrictions))
                subval_count --; /* It's too hard to safely localize a filter on nullable variable in UNION, too many checks for too little effect */
              /* In case of union, we can't place filter right in UNION gp, because nobody expects it there.
              Instead, we place a copy of the filter in each branch of the union. This can be unsafe if there are too many branches */
              subval_unions_count++;
              subval_in_unions_count += BOX_ELEMENTS (sub_gp->_.gp.members);
              break;
            case SELECT_L: subval_count --; break;  /*!!!TBD now HAVING is supported so filter can be moved inside subselect if there's no LIMIT/OFFSET */
            case SERVICE_L: subval_count --; break;
            case OPTIONAL_L:
              {
                if (!(SPART_VARR_NOT_NULL & sv_eq->e_rvr.rvrRestrictions))
                  subval_count --; /* It's unsafe to localize a filter on nullable variable. Consider { ... optional {<s> <p> ?o} filter (!bound(?o))}} */
                break;
              }
            }
        }
      END_DO_BOX_FAST_REV;
      if (10 < subval_in_unions_count)
        {
          localize_in_unions = 0; /* Too many subvalues in unions -- unsafe to localize there, too many filter conditions may kill SQL compiler */
          subval_count -= subval_unions_count; /* Nevertheless we can try to localize in non-unions */
        }
      if (0 == subval_count)
        continue; /* No subvalues -- can't localize because this either have no effect or drop the filter */
      localizations_left = 5; /* With too many subvalues, only few can be used for localization, too many filter conditions may kill SQL compiler */
      /* Now it's safe to localize the filter in each place where a subvalue comes from */
      DO_BOX_FAST_REV (ptrlong, subval_eq_idx, subval_ctr, sv_eq->e_subvalue_idxs)
        {
          sparp_equiv_t *sub_eq = SPARP_EQUIV (sparp, subval_eq_idx);
          SPART *sub_gp = sub_eq->e_gp;
          SPART *filter_clone;
          switch (sub_gp->_.gp.subtype)
            {
            case UNION_L:
              if (!localize_in_unions)
                continue;
              if (!(SPART_VARR_NOT_NULL & sv_eq->e_rvr.rvrRestrictions))
                continue;
              break;
            case SELECT_L: continue; /*!!!TBD see comment above */
            case SERVICE_L: continue;
            case OPTIONAL_L:
              {
                if (!(SPART_VARR_NOT_NULL & sv_eq->e_rvr.rvrRestrictions))
                  continue;
                break;
              }
            }
          if (0 >= localizations_left--)
            break;
          if (UNION_L == sub_gp->_.gp.subtype)
            {
              int sub_memb_ctr, bad_subcase_found = 0;
              if (!filt_is_detached)
                {
/* If some branches are inappropriate for that trick then we don't detach the external filter in order to guarantee that results of all branches are filtered somewhere outside. */
                  DO_BOX_FAST_REV (SPART *, sub_memb, sub_memb_ctr, sub_gp->_.gp.members)
                    {
                      if ((SPAR_GP != SPART_TYPE (sub_memb)) || (0 != sub_memb->_.gp.subtype))
                        {
                          bad_subcase_found = 1;
                          break;
                        }
                    }
                  END_DO_BOX_FAST_REV;
                }
              DO_BOX_FAST_REV (SPART *, sub_memb, sub_memb_ctr, sub_gp->_.gp.members)
                {
                  if (!bad_subcase_found && !filt_is_detached)
                    {
                      sparp_gp_detach_filter (sparp, curr, filt_ctr, NULL);
                      filt_is_detached = 1;
                    }
                  filter_clone = sparp_tree_full_copy (sparp, filt, curr);
                  sparp_gp_attach_filter (sparp, sub_memb, filter_clone, 0, NULL);
                }
              END_DO_BOX_FAST_REV;
              continue;
            }
          if (!filt_is_detached)
            {
              sparp_gp_detach_filter (sparp, curr, filt_ctr, NULL);
              filt_is_detached = 1;
            }
          filter_clone = sparp_tree_full_copy (sparp, filt, curr);
          sparp_gp_attach_filter (sparp, sub_gp, filter_clone, 0, NULL);
        }
      END_DO_BOX_FAST_REV;
    }
  return 0;
}

int
sparp_calc_importance_of_eq (sparp_t *sparp, sparp_equiv_t *eq)
{
  int res = 16 * eq->e_subquery_uses +
    4 * eq->e_gspo_uses +
    4 * eq->e_nested_bindings +
    3 * eq->e_optional_reads +
    2 * eq->e_const_reads +
    2 * BOX_ELEMENTS_0 (eq->e_receiver_idxs);
  if (SPARP_EQ_IS_FIXED_AND_NOT_NULL (eq))
    res *= 5;
  else if (SPART_VARR_FIXED & eq->e_rvr.rvrRestrictions)
    res *= 4;
  if (SPART_VARR_TYPED & eq->e_rvr.rvrRestrictions)
    res *= 2;
  if ((SPART_VARR_IS_IRI | SPART_VARR_IS_BLANK | SPART_VARR_IS_LIT) & eq->e_rvr.rvrRestrictions)
    res = res * 3 / 2;
  return res;
}

int
sparp_calc_importance_of_member (sparp_t *sparp, SPART *memb)
{
  int ctr;
  int res = 0;
  switch (memb->type)
    {
    case SPAR_TRIPLE:
      for (ctr = SPART_TRIPLE_FIELDS_COUNT; ctr--; /* no step */)
        {
          SPART *field = memb->_.triple.tr_fields [ctr];
          if (SPAR_IS_BLANK_OR_VAR (field))
            {
              sparp_equiv_t *eq = SPARP_EQUIV (sparp, field->_.var.equiv_idx);
              res += sparp_calc_importance_of_eq (sparp, eq);
            }
          else
            res += 12;
        }
    case SPAR_GP:
      SPARP_REVFOREACH_GP_EQUIV (sparp, memb, ctr, eq)
        {
          res += sparp_calc_importance_of_eq (sparp, eq);
        }
      END_SPARP_REVFOREACH_GP_EQUIV;
      break;
    }
  return res;
}

int
sparp_find_index_of_most_important_union (sparp_t *sparp, SPART *parent_gp)
{
  int idx, best_idx = -1, best_importance = -1;
  DO_BOX_FAST_REV (SPART *, memb, idx, parent_gp->_.gp.members)
    {
      int importance;
      if ((SPAR_GP != memb->type) || (UNION_L != memb->_.gp.subtype))
        continue;
      importance = sparp_calc_importance_of_member (sparp, memb);
      if (importance >= best_importance) /* '>=', not '>' to give little preference to the leftmost union of a few */
        {
          best_idx = idx;
          best_importance = importance;
        }
    }
  END_DO_BOX_FAST_REV;
  return best_idx;
}

int
sparp_gp_trav_union_of_joins_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  switch (SPART_TYPE(curr))
    {
    case SPAR_GP:
      sts_this[0].sts_env = curr;
      return SPAR_GPT_ENV_PUSH;
    case SPAR_TRIPLE: return SPAR_GPT_NODOWN |  SPAR_GPT_NOOUT;
    default: return 0;
    }
}

int
sparp_gp_trav_union_of_joins_out (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_GP != curr->type)
    return 0;
  if ((0 == curr->_.gp.subtype) || (WHERE_L == curr->_.gp.subtype))
    {
      int union_idx;
      int case_ctr, case_count;
      int curr_had_one_member = ((1 >= BOX_ELEMENTS (curr->_.gp.members)) ? 1 : 0);
      int join_glued_filters_count, union_glued_filters_count;
      SPART *sub_union;
      SPART **detached_join_parts;
      SPART **detached_join_filters;
      SPART **detached_union_parts;
      SPART **detached_union_filters;
      SPART *new_union;
      SPART **new_union_joins;
      if ((curr_had_one_member) && (WHERE_L == curr->_.gp.subtype))
        return 0; /* top-level union can not be really optimized: we need a wrapper to give alias name to retval vars */
      union_idx = sparp_find_index_of_most_important_union (sparp, curr);
      if (0 > union_idx)
        return 0;
      sub_union = curr->_.gp.members [union_idx];
      case_count = BOX_ELEMENTS (sub_union->_.gp.members);
      if (WHERE_L == curr->_.gp.subtype)
        {
          if (curr_had_one_member)
            return 0;
        }
      else
        {
          int parent_len = 1;
          SPART *parent = (SPART *)(sts_this[-1].sts_env);
#ifdef DEBUG
          if (SPAR_GP != SPART_TYPE (parent))
            spar_internal_error (sparp, "sparp_" "gp_trav_union_of_joins_out (): parent is not a gp");
#endif
          if (UNION_L == parent->_.gp.subtype)
            parent_len = BOX_ELEMENTS (parent->_.gp.members);
          if (2000 < (parent_len + case_count))
            return 0; /* This restricts the size of the resulting SQL statement */
        }
      sparp_equiv_audit_all (sparp, 0);
      union_glued_filters_count = sub_union->_.gp.glued_filters_count;
      sub_union->_.gp.glued_filters_count = 0;
      detached_union_filters = sparp_gp_detach_all_filters (sparp, sub_union, NULL);
      detached_union_parts = sparp_gp_detach_all_members (sparp, sub_union, NULL);
      join_glued_filters_count = curr->_.gp.glued_filters_count;
      curr->_.gp.glued_filters_count = 0;
      detached_join_filters = sparp_gp_detach_all_filters (sparp, curr, NULL);
      detached_join_parts = sparp_gp_detach_all_members (sparp, curr, NULL);
      if (curr_had_one_member)
        {
          new_union_joins = detached_union_parts;
        }
      else
        {
          new_union_joins = (SPART **)t_alloc_box (case_count * sizeof (SPART *), DV_ARRAY_OF_POINTER);
          for (case_ctr = 0; case_ctr < case_count; case_ctr++)
            new_union_joins [case_ctr] = sparp_new_empty_gp (sparp, 0, unbox (detached_union_parts [case_ctr]->srcline));
        }
      if (WHERE_L != curr->_.gp.subtype)
        {
          curr->_.gp.subtype = UNION_L;
          new_union = curr;
        }
      else
        {
          new_union = sparp_new_empty_gp (sparp, UNION_L, unbox (curr->srcline));
          sparp_gp_attach_member (sparp, curr, new_union, 0, NULL);
        }
      sparp_gp_attach_many_members (sparp, new_union, new_union_joins, 0, NULL);
      detached_join_parts [union_idx] = NULL;
      for (case_ctr = 0; case_ctr < case_count; case_ctr++)
        {
          int last_case = (((case_count-1) == case_ctr) ? 1 : 0);
          SPART *new_join = new_union->_.gp.members [case_ctr]; /* equal to union_part if curr_had_one_member */
          SPART **new_filts_u = (last_case ? detached_union_filters : sparp_treelist_full_clone (sparp, detached_union_filters));
          SPART **new_filts_j = (last_case ? detached_join_filters : sparp_treelist_full_clone (sparp, detached_join_filters));
          if (!curr_had_one_member)
            {
              int join_part_ctr;
              SPART *union_part = detached_union_parts [case_ctr];
              SPART **new_join_parts = (SPART **)t_box_copy ((caddr_t)detached_join_parts);
              new_join_parts [union_idx] = union_part;
              DO_BOX_FAST (SPART *, join_part, join_part_ctr, new_join_parts)
                {
                  if (SPAR_GP == join_part->type)
                    {
                      SPART *cloned_join_part = (last_case ? join_part : sparp_gp_full_clone (sparp, join_part));
                      new_join_parts [join_part_ctr] = cloned_join_part;
                    }
                  else
                    new_join_parts [join_part_ctr] = (last_case ? join_part : sparp_tree_full_copy (sparp, join_part, curr));
                }
              END_DO_BOX_FAST;
              sparp_gp_attach_many_members (sparp, new_join, new_join_parts, 0, NULL);
              sparp_equiv_audit_all (sparp, 0);
            }
          sparp_gp_attach_many_filters (sparp, new_join, new_filts_u, BOX_ELEMENTS (new_join->_.gp.filters) - new_join->_.gp.glued_filters_count, NULL);
          new_join->_.gp.glued_filters_count += union_glued_filters_count;
          sparp_gp_attach_many_filters (sparp, new_join, new_filts_j, BOX_ELEMENTS (new_join->_.gp.filters) - new_join->_.gp.glued_filters_count, NULL);
          new_join->_.gp.glued_filters_count += join_glued_filters_count;
          sparp_gp_tighten_by_eq_replaced_filters (sparp, new_join, sub_union, 0);
          sparp_gp_tighten_by_eq_replaced_filters (sparp, new_join, curr, 0);
          sparp_equiv_audit_all (sparp, 0);
        }
      sparp->sparp_rewrite_dirty += 10;
      sparp_gp_tighten_by_eq_replaced_filters (sparp, NULL, sub_union, 1);
      sparp_gp_tighten_by_eq_replaced_filters (sparp, NULL, curr, 1);
      sparp_gp_deprecate (sparp, sub_union, 0);
      sparp_equiv_audit_all (sparp, 0);
    }
  return 0;
}

static void
sparp_collect_single_atable_use (sparp_t *sparp_or_null, ccaddr_t alias, ccaddr_t tablename, qm_atable_use_t *uses, ptrlong *use_count_ptr )
{
  ptrlong old_qmatu_idx = ecm_find_name (alias, uses, use_count_ptr[0], sizeof (qm_atable_use_t));
  if (ECM_MEM_NOT_FOUND == old_qmatu_idx)
    {
      ptrlong use_idx = ecm_add_name (alias, (void **)(&uses), use_count_ptr, sizeof (qm_atable_use_t));
      qm_atable_use_t *use = uses + use_idx;
      use->qmatu_alias = (char *) alias;
      use->qmatu_tablename = tablename;
      use->qmatu_more = NULL;
    }
  else
    {
      qm_atable_use_t *qmatu = uses + old_qmatu_idx;
      if (strcmp (tablename, qmatu->qmatu_tablename))
        {
          if (NULL == sparp_or_null)
            sqlr_new_error ("22023", "SR640", "internal error in processing table \"%.200s\" (alias \"%.200s\") in some RDF View", tablename, alias);
          else
            spar_internal_error (sparp_or_null, "sparp_" "collect_atable_uses(): probable corruption of some quad map");
        }
    }
}

static void
sparp_collect_atable_uses (sparp_t *sparp_or_null, ccaddr_t singletablename, qm_atable_array_t qmatables, qm_atable_use_t *uses, ptrlong *use_count_ptr )
{
  int ata_ctr;
  if ((NULL != singletablename) && ('\0' != singletablename[0]))
    sparp_collect_single_atable_use (sparp_or_null, uname___empty, singletablename, uses, use_count_ptr);
  DO_BOX_FAST (qm_atable_t *, ata, ata_ctr, qmatables)
    {
      sparp_collect_single_atable_use (sparp_or_null, ata->qmvaAlias, ata->qmvaTableName, uses, use_count_ptr);
    }
  END_DO_BOX_FAST;
}

void
sparp_collect_all_atable_uses (sparp_t *sparp_or_null, quad_map_t *qm)
{
  int fld_ctr, max_uses = 0, default_qm_table_used = 0;
  ptrlong use_count = 0;
  qm_atable_use_t *uses;
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    {
      qm_value_t *qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
      if (NULL != qmv)
        {
          if (NULL != qmv->qmvTableName)
            max_uses++;
          max_uses += BOX_ELEMENTS_0 (qmv->qmvATables);
        }
    }
  if (NULL != qm->qmTableName)
    max_uses++;
  max_uses += BOX_ELEMENTS_0 (qm->qmATables);
  uses = dk_alloc_box_zero (sizeof (qm_atable_use_t) * max_uses, DV_ARRAY_OF_LONG);
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    {
      int col_ctr, default_qm_val_table_used = 0;
      qm_value_t *qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
      if (NULL == qmv)
        continue;
      DO_BOX_FAST (qm_column_t *, col, col_ctr, qmv->qmvColumns)
        {
          if ((NULL == col->qmvcAlias) || ('\0' == col->qmvcAlias[0]))
            {
              if ((NULL != qmv->qmvTableName) && ('\0' != qmv->qmvTableName[0]))
                default_qm_val_table_used++;
              else
                default_qm_table_used++;
            }
        }
      END_DO_BOX_FAST;
      sparp_collect_atable_uses (sparp_or_null,
        (default_qm_val_table_used ? qmv->qmvTableName : NULL),
        qmv->qmvATables, uses, &use_count );
    }
  sparp_collect_atable_uses (sparp_or_null,
    (default_qm_table_used ? qm->qmTableName : NULL),
    qm->qmATables, uses, &use_count );
  qm->qmAllATableUses = (ptrlong *)uses;
  qm->qmAllATableUseCount = use_count;
}

#if 0
static int
sparp_atable_uses_match (
  qm_atable_array_t qmatables,
  qm_atable_use_t *uses, int use_count )
{
  int ata_ctr, use_ctr, found_ctr = 0;
  DO_BOX_FAST (qm_atable_t *, ata, ata_ctr, qmatables)
    {
      qm_atable_use_t *qmatu;
      for (use_ctr = use_count; use_ctr--; /* no step */)
        {
          qmatu = uses + use_ctr;
          if (strcmp (ata->qmvaAlias, qmatu->qmatu_alias))
            continue;
          if (strcmp (ata->qmvaTableName, qmatu->qmatu_ata->qmvaTableName))
            continue;
          if (NULL == qmatu->qmatu_more)
            {
              found_ctr++;
              qmatu->qmatu_more = ata;
            }
          goto qmatu_exists; /* see below */
        }
      return -1; /* No match because qmatables contains an item that is not in \c uses */
qmatu_exists: ;
    }
  END_DO_BOX_FAST;
  return found_ctr;
}
#endif

void
sparp_collect_all_conds (sparp_t *sparp_or_null, quad_map_t *qm)
{
  int fld_ctr, max_conds = 0;
  ptrlong cond_ctr, cond_count = 0;
  ccaddr_t *conds;
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    {
      qm_value_t *qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
      if (NULL != qmv)
        max_conds += BOX_ELEMENTS_0 (qmv->qmvConds);
    }
  max_conds += BOX_ELEMENTS_0 (qm->qmConds);
  conds = dk_alloc_box_zero (sizeof (ccaddr_t) * max_conds, DV_ARRAY_OF_LONG);
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    {
      qm_value_t *qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
      if (NULL == qmv)
        continue;
      DO_BOX_FAST (ccaddr_t, cond, cond_ctr, qmv->qmvConds)
        {
          ecm_map_name (cond, (void **)(&conds), &cond_count, sizeof (ccaddr_t));
        }
      END_DO_BOX_FAST;
    }
  DO_BOX_FAST (ccaddr_t, cond, cond_ctr, qm->qmConds)
    {
      ecm_map_name (cond, (void **)(&conds), &cond_count, sizeof (ccaddr_t));
    }
  END_DO_BOX_FAST;
  qm->qmAllConds = conds;
  qm->qmAllCondCount = cond_count;
}

int
sparp_quad_maps_eq_for_breakup (sparp_t *sparp, quad_map_t *qm_one, quad_map_t *qm_two)
{
  int use_ctr, use_count, cond_ctr, cond_count;
  qm_atable_use_t *uses_one, *uses_two;
  ccaddr_t *conds_one, *conds_two;
/* First of all we check if sets of atables are equal */
  if (NULL == qm_one->qmAllATableUses)
    sparp_collect_all_atable_uses (sparp, qm_one);
  if (NULL == qm_two->qmAllATableUses)
    sparp_collect_all_atable_uses (sparp, qm_two);
  uses_one = (qm_atable_use_t *)(qm_one->qmAllATableUses);
  uses_two = (qm_atable_use_t *)(qm_two->qmAllATableUses);
  use_count = qm_one->qmAllATableUseCount;
  if (use_count != qm_two->qmAllATableUseCount)
    return 0;
  for (use_ctr = use_count; use_ctr--; /* no step */)
    {
      if (strcmp (uses_one[use_ctr].qmatu_alias, uses_two[use_ctr].qmatu_alias))
        return 0;
      if (strcmp (uses_one[use_ctr].qmatu_tablename, uses_two[use_ctr].qmatu_tablename))
        return 0;
    }
  /* If sets of atables are equal then we compare conditions */
  if (NULL == qm_one->qmAllConds)
    sparp_collect_all_conds (sparp, qm_one);
  if (NULL == qm_two->qmAllConds)
    sparp_collect_all_conds (sparp, qm_two);
  conds_one = qm_one->qmAllConds;
  conds_two = qm_two->qmAllConds;
  cond_count = qm_one->qmAllCondCount;
  if (cond_count != qm_two->qmAllCondCount)
    return 0;
  for (cond_ctr = cond_count; cond_ctr--; /* no step */)
    {
      if (strcmp (conds_one[cond_ctr], conds_two[cond_ctr]))
        return 0;
    }
/*return 0;*/
  return 1;
}

caddr_t *
sparp_gp_may_reuse_tabids_in_union (sparp_t *sparp, SPART *gp, int expected_triples_count)
{
  dk_set_t res = NULL;
  int triple_ctr, triples_count;
  if ((SPAR_GP != gp->type) ||
    (0 != gp->_.gp.subtype) )
    return NULL;
  triples_count = BOX_ELEMENTS (gp->_.gp.members);
  if (((0 <= expected_triples_count) && (triples_count != expected_triples_count)) || (0 == triples_count))
    return NULL;
  for (triple_ctr = 0; triple_ctr < triples_count; triple_ctr++)
    {
      SPART * gp_triple = gp->_.gp.members[triple_ctr];
      if (SPAR_TRIPLE != gp_triple->type)
        return NULL;
      if (1 != BOX_ELEMENTS (gp_triple->_.triple.tc_list))
        return NULL;
      if (NULL != gp_triple->_.triple.options)
        return NULL;
      if (gp_triple->_.triple.ft_type)
        return NULL; /* TBD: support of free-text indexing in breakup */
    }
  for (triple_ctr = 0; triple_ctr < triples_count; triple_ctr++)
    {
      SPART * gp_triple = gp->_.gp.members[triple_ctr];
      t_set_push (&res, gp_triple->_.triple.tabid);
    }
  return t_revlist_to_array (res);
}

int
sparp_bitmask_fields_with_equations (sparp_t *sparp, SPART *triple)
{
  quad_map_t *qm = triple->_.triple.tc_list[0]->tc_qm;
  int fld_ctr;
  int res = 0;
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    {
       SPART *fld = triple->_.triple.tr_fields[fld_ctr];
       sparp_equiv_t *fld_eq;
       qm_value_t *fld_qmv;
       long restr;
       int fld_has_equations;
       if (!SPAR_IS_BLANK_OR_VAR (fld))
         {
           res |= (1 << fld_ctr);
           continue;
         }
       fld_eq = SPARP_EQUIV (sparp, fld->_.var.equiv_idx);
       fld_qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
       restr = fld_eq->e_rvr.rvrRestrictions;
       fld_has_equations = ((1 < fld_eq->e_var_count) ||
         (restr & (SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL)) ||
         ((restr & SPART_VARR_FIXED) && (NULL != fld_qmv)) );
       if (fld_has_equations)
         res |= (1 << fld_ctr);
    }
  return res;
}

int
sparp_try_reuse_tabid_in_union (sparp_t *sparp, SPART *curr, int base_idx)
{
  SPART *base = curr->_.gp.members[base_idx];
  /*SPART **base_filters = base->_.gp.filters; !!!TBD: check for filters that may restrict the search by idex */
  SPART **base_triples = base->_.gp.members;
  int bt_ctr, base_triples_count = BOX_ELEMENTS (base_triples);
  int dep_idx, memb_count, breakup_shift = 0, breakup_unictr = -1;
  int base_should_change_tabid = 0;
  memb_count = BOX_ELEMENTS_0 (curr->_.gp.members);
  for (dep_idx = base_idx + 1; /* breakup optimization is symmetrical so the case of two triples should be considered only once, not base_triple...dep then dep...base_triple */
    dep_idx < memb_count; dep_idx++)
    {
      SPART *dep = curr->_.gp.members[dep_idx];
      SPART **dep_triples;
      if (NULL == sparp_gp_may_reuse_tabids_in_union (sparp, dep, base_triples_count))
        continue;
      if (dep_idx == base_idx + 1)
        base_should_change_tabid = 1; /* There's a danger of tabid collision so printer will treat base and dep as breakup even if it is not true */
      dep_triples = dep->_.gp.members;
      if (BOX_ELEMENTS (dep_triples) != base_triples_count)
        goto next_dep; /* see below */
      for (bt_ctr = base_triples_count; bt_ctr--; /* no step */)
        {
          SPART *base_triple = base_triples[bt_ctr];
          SPART *dep_triple = dep_triples[bt_ctr];
          quad_map_t *base_qm, *dep_qm;
          int fld_ctr, base_bitmask_of_equations, dep_bitmask_of_equations;
          if (dep_triple->_.triple.src_serial != base_triple->_.triple.src_serial)
            goto next_dep; /* see below */
          base_qm = base_triple->_.triple.tc_list[0]->tc_qm;
          dep_qm = dep_triple->_.triple.tc_list[0]->tc_qm;
          base_bitmask_of_equations = sparp_bitmask_fields_with_equations (sparp, base_triple);
          if ((base_bitmask_of_equations & (1 << SPART_TRIPLE_OBJECT_IDX)) &&
             !(base_bitmask_of_equations & (1 << SPART_TRIPLE_SUBJECT_IDX)) ) /* If chances on good breakup are low but cost of wrong decision may be high... */
            goto next_dep; /* see below */
          dep_bitmask_of_equations = sparp_bitmask_fields_with_equations (sparp, dep_triple);
          if ((dep_bitmask_of_equations & (1 << SPART_TRIPLE_OBJECT_IDX)) &&
             !(dep_bitmask_of_equations & (1 << SPART_TRIPLE_SUBJECT_IDX)) ) /* If chances on good breakup are low but cost of wrong decision may be high... */
            goto next_dep; /* see below */
          if (!sparp_expn_lists_are_equal (sparp, base->_.gp.filters, dep->_.gp.filters))
            goto next_dep; /* see below */
          if (!sparp_quad_maps_eq_for_breakup (sparp, base_qm, dep_qm))
            goto next_dep; /* see below */
          for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
            { /* For each variable that makes equalities we check that it has identical qmv in base and in dep */
              SPART *base_fld = base_triple->_.triple.tr_fields[fld_ctr];
              SPART *dep_fld = dep_triple->_.triple.tr_fields[fld_ctr];
              sparp_equiv_t *base_fld_eq, *dep_fld_eq;
              qm_value_t *base_fld_qmv, *dep_fld_qmv;
              if (!SPAR_IS_BLANK_OR_VAR (base_fld))
                continue;
              if (!SPAR_IS_BLANK_OR_VAR (dep_fld))
                spar_internal_error (sparp, "sparp_" "try_reuse_tabid_in_union(): different field types in triples with same src_serial");
              base_fld_eq = SPARP_EQUIV (sparp, base_fld->_.var.equiv_idx);
              dep_fld_eq = SPARP_EQUIV (sparp, dep_fld->_.var.equiv_idx);
              base_fld_qmv = SPARP_FIELD_QMV_OF_QM (base_qm, fld_ctr);
              dep_fld_qmv = SPARP_FIELD_QMV_OF_QM (dep_qm, fld_ctr);
              if (base_fld_qmv != dep_fld_qmv)
                goto next_dep; /* see below */
              if (!(base_bitmask_of_equations & (1 << fld_ctr)))
                continue;
              if (!(dep_bitmask_of_equations & (1 << fld_ctr)))
                continue;
              if (NULL == base_fld_qmv)
                {
                  long base_restr = base_fld_eq->e_rvr.rvrRestrictions;
                  long dep_restr = dep_fld_eq->e_rvr.rvrRestrictions;
                  caddr_t base_fld_const = SPARP_FIELD_CONST_OF_QM (base_qm, fld_ctr);
                  caddr_t dep_fld_const = SPARP_FIELD_CONST_OF_QM (dep_qm, fld_ctr);
                  if (((base_restr & SPART_VARR_IS_REF) && (dep_restr & SPART_VARR_IS_LIT)) ||
                    ((base_restr & SPART_VARR_IS_LIT) && (dep_restr & SPART_VARR_IS_REF)) )
                    goto next_dep; /* see below */
                  if (!sparp_fixedvalues_equal (sparp, (SPART *)base_fld_const, (SPART *)dep_fld_const))
                    goto next_dep; /* see below */
                }
            }
        }
      /* At this point all checks of dep are passed, can adjust selids and tabids */
      sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
      if (0 == breakup_shift)
        breakup_unictr = sparp->sparp_unictr++;
      for (bt_ctr = base_triples_count; bt_ctr--; /* no step */)
        {
          SPART *base_triple = base_triples[bt_ctr];
          SPART *dep_triple = dep_triples[bt_ctr];
          caddr_t new_base_tabid;
          if (0 == breakup_shift)
            {
              new_base_tabid = t_box_sprintf (200, "%s-b%d", base_triple->_.triple.tabid, breakup_unictr);
              sparp_set_triple_selid_and_tabid (sparp, base_triple, base->_.gp.selid, new_base_tabid);
              base_should_change_tabid = 0;
            }
          else
            new_base_tabid = base_triple->_.triple.tabid;
          sparp_set_triple_selid_and_tabid (sparp, dep_triple, dep->_.gp.selid, new_base_tabid);
        }
      if (dep_idx > (base_idx + 1 + breakup_shift)) /* Adjustment to keep reused tabids together. */
        {
          int swap_ctr;
          for (swap_ctr = dep_idx; swap_ctr > (base_idx + 1 + breakup_shift); swap_ctr--)
            curr->_.gp.members[swap_ctr] = curr->_.gp.members[swap_ctr-1];
          curr->_.gp.members[base_idx + 1 + breakup_shift] = dep;
        }
      breakup_shift++;
      sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);

next_dep: ;
    }
#ifdef DEBUG
  if (0 != breakup_shift)
    {
      int chk_idx;
      printf ("sparp_" "try_reuse_tabid_in_union() has found breakup in %s from %d to %d incl.\n",
        curr->_.gp.selid, base_idx, base_idx + breakup_shift);
      for (chk_idx = base_idx + breakup_shift; chk_idx > base_idx; chk_idx--)
        {
          SPART *chk = curr->_.gp.members[chk_idx];
          for (bt_ctr = base_triples_count; bt_ctr--; /* no step */)
            {
              SPART *base_triple = base_triples[bt_ctr];
              SPART *chk_triple = chk->_.gp.members[bt_ctr];
              if (!sparp_quad_maps_eq_for_breakup (sparp, base_triple->_.triple.tc_list[0]->tc_qm, chk_triple->_.triple.tc_list[0]->tc_qm))
                spar_internal_error (sparp, "sparp_" "try_reuse_tabid_in_union() has made breakup on inappropriate pair");
            }
        }
    }
#endif
  if (base_should_change_tabid)
    { /* We rename tabids in base to prevent occasional recognition of breakup in printer */
      breakup_unictr = sparp->sparp_unictr++;
      for (bt_ctr = base_triples_count; bt_ctr--; /* no step */)
        {
          SPART *base_triple = base_triples[bt_ctr];
          if (0 == breakup_shift)
            {
              caddr_t new_base_tabid;
              new_base_tabid = t_box_sprintf (200, "%s-u%d", base_triple->_.triple.tabid, breakup_unictr);
              sparp_set_triple_selid_and_tabid (sparp, base_triple, base->_.gp.selid, new_base_tabid);
            }
        }
    }
  return breakup_shift;
}

static int
sparp_qmv_forms_reusable_key_of_qm (sparp_t *sparp, qm_value_t *key_qmv, quad_map_t *qm)
{
  dk_set_t key_aliases = NULL;
  int ctr, fld_ctr;
  if (NULL == key_qmv) /* Funny case: join on field that is mapped to constant. Thus there's no actual join, only filter (FIELD = CONST) for other triples */
    return 0;
  if (!key_qmv->qmvColumnsFormKey) /* No key -- no reuse */
    return 0;
  if (!key_qmv->qmvFormat->qmfIsBijection)	/* Non-bijection format is unsafe for reuse, different SHORTs may result in same LONG */
    return 0;
  DO_BOX_FAST (qm_atable_t *, at, ctr, key_qmv->qmvATables)
    {
      if (0 > dk_set_position_of_string (key_aliases, at->qmvaAlias))
        t_set_push (&key_aliases, (caddr_t)(at->qmvaAlias));
    }
  END_DO_BOX_FAST;
  DO_BOX_FAST (qm_atable_t *, at, ctr, qm->qmATables)
    { /* If qm uses aliases not 'selected' by key_qmv field then the key value of key_qvm is not a key of whole triple */
      if (0 > dk_set_position_of_string (key_aliases, at->qmvaAlias))
        return 0;
    }
  END_DO_BOX_FAST;
  for (fld_ctr = 0; fld_ctr < SPART_TRIPLE_FIELDS_COUNT; fld_ctr++)
    { /* If any field of qm uses aliases not 'selected' by key_qmv field then the key value of key_qvm is not a key of whole triple */
      qm_value_t *fld_qmv = SPARP_FIELD_QMV_OF_QM (qm, fld_ctr);
      if (fld_qmv == key_qmv)
        continue; /* No check, key_qmv is a key for itself */
      if (NULL == fld_qmv)
        continue;
      DO_BOX_FAST (qm_atable_t *, at, ctr, fld_qmv->qmvATables)
        {
          if (0 > dk_set_position_of_string (key_aliases, at->qmvaAlias))
            return 0;
        }
      END_DO_BOX_FAST;
    }
  return 1;
}

static void
sparp_try_reuse_tabid_in_join_via_key_qname (sparp_t *sparp, SPART *curr, int base_idx, qm_value_t *key_qmv, ccaddr_t qname)
{
  SPART *base = curr->_.gp.members[base_idx];
  int dep_triple_idx, memb_count = BOX_ELEMENTS (curr->_.gp.members);
  for (dep_triple_idx = base_idx; dep_triple_idx < memb_count; dep_triple_idx++)
    {
      int dep_field_idx;
      SPART *dep_triple = curr->_.gp.members[dep_triple_idx];
      quad_map_t *dep_qm;
      if (SPAR_TRIPLE != SPART_TYPE (dep_triple))
        continue;
      if (1 != BOX_ELEMENTS (dep_triple->_.triple.tc_list))
        continue;
      if (NULL != dep_triple->_.triple.options)
        continue;
      if (!strcmp (base->_.triple.tabid, dep_triple->_.triple.tabid))
        continue; /* tabid is already reused */
      dep_qm = dep_triple->_.triple.tc_list[0]->tc_qm;
      for (dep_field_idx = 0; dep_field_idx < SPART_TRIPLE_FIELDS_COUNT; dep_field_idx++)
        {
          qm_value_t *dep_qmv = SPARP_FIELD_QMV_OF_QM (dep_qm, dep_field_idx);
          SPART *dep_field;
          int dep_field_type;
          if (key_qmv != dep_qmv) /* The key mapping differs in set of source columns or in the IRI serialization (or literal cast) */
            continue;
          dep_field = dep_triple->_.triple.tr_fields[dep_field_idx];
          dep_field_type = SPART_TYPE (dep_field);
          if (SPAR_QNAME == dep_field_type)
            {
              if (strcmp (qname, dep_field->_.lit.val))
                continue;
            }
          else if ((SPAR_VARIABLE == dep_field_type) || (SPAR_BLANK_NODE_LABEL == dep_field_type))
            {
              if (((SPART_VARR_FIXED | SPART_VARR_IS_IRI) != ((SPART_VARR_FIXED | SPART_VARR_IS_IRI) & dep_field->_.var.rvr.rvrRestrictions)) ||
                strcmp (qname, dep_field->_.var.rvr.rvrFixedValue) )
                continue;
            }
          else
            continue;
          /* Glory, glory, hallelujah; we can reuse the tabid so the final SQL query will have one join less. */
          sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
          sparp_set_triple_selid_and_tabid (sparp, dep_triple, curr->_.gp.selid, base->_.triple.tabid);
          if (dep_triple_idx > (base_idx + 1)) /* Adjustment to keep reused tabids together. The old join order of dep_triple is of zero importance because there's no more dep_triple as a separate subtable */
            {
              int swap_ctr;
              for (swap_ctr = dep_triple_idx; swap_ctr > (base_idx + 1); swap_ctr--)
                curr->_.gp.members[swap_ctr] = curr->_.gp.members[swap_ctr-1];
              curr->_.gp.members[base_idx + 1] = dep_triple;
            }
          sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
          break;
        }
    }
}

void
sparp_try_reuse_tabid_in_join (sparp_t *sparp, SPART *curr, int base_idx)
{
  SPART *base = curr->_.gp.members[base_idx];
  quad_map_t *base_qm = base->_.triple.tc_list[0]->tc_qm;
  int key_field_idx;
  for (key_field_idx = 0; key_field_idx < SPART_TRIPLE_FIELDS_COUNT; key_field_idx++)
    {
      SPART *key_field = base->_.triple.tr_fields[key_field_idx];
      ssg_valmode_t key_fmt = base->_.triple.native_formats[key_field_idx];
      qm_value_t *key_qmv;
      sparp_equiv_t *key_eq;
      int dep_ctr;
      int key_field_type;
      sparp_jso_validate_format (sparp, key_fmt);
      key_qmv = SPARP_FIELD_QMV_OF_QM (base_qm,key_field_idx);
      if (NULL == key_qmv)
        continue; /* Const field of mapping can add a filter but can not specify a unique key of row */
      key_qmv = SPARP_FIELD_QMV_OF_QM (base_qm,key_field_idx);
      if (!sparp_qmv_forms_reusable_key_of_qm (sparp, key_qmv, base_qm))
        continue;
      key_field_type = SPART_TYPE (key_field);
      if ((SPAR_BLANK_NODE_LABEL != key_field_type) && (SPAR_VARIABLE != key_field_type))
        {
          if (SPAR_QNAME == key_field_type)
            sparp_try_reuse_tabid_in_join_via_key_qname (sparp, curr, base_idx, key_qmv, key_field->_.lit.val);
          continue;
        }
      if ((SPART_VARR_FIXED | SPART_VARR_IS_IRI) == ((SPART_VARR_FIXED | SPART_VARR_IS_IRI) & key_field->_.var.rvr.rvrRestrictions))
        sparp_try_reuse_tabid_in_join_via_key_qname (sparp, curr, base_idx, key_qmv, key_field->_.var.rvr.rvrFixedValue);
      key_eq = sparp_equiv_get (sparp, curr, key_field, SPARP_EQUIV_GET_ASSERT);
      if (2 > key_eq->e_gspo_uses) /* No reuse of key -- no reuse of triples */
        continue;
      for (dep_ctr = key_eq->e_var_count; dep_ctr--; /* no step */)
        {
          SPART *dep_field = key_eq->e_vars[dep_ctr];
          int dep_triple_idx, dep_field_tr_idx;
          SPART *dep_triple = NULL;
          quad_map_t *dep_qm;
          qm_value_t *dep_qmv;
          if (NULL == dep_field->_.var.tabid) /* The variable is not a field in a triple (const read, not gspo use) */
            continue;
          if (!strcmp (dep_field->_.var.tabid, key_field->_.var.tabid)) /* Either tabid is reused already or reference to self -- nothing to do in both cases */
            continue;
          dep_field_tr_idx = dep_field->_.var.tr_idx;
          for (dep_triple_idx = BOX_ELEMENTS (curr->_.gp.members); dep_triple_idx--; /* no step */)
            {
              dep_triple = curr->_.gp.members[dep_triple_idx];
              if (SPAR_TRIPLE != dep_triple->type)
                continue;
              if (dep_triple->_.triple.tr_fields [dep_field_tr_idx] == dep_field)
                break;
            }
          if (0 > dep_triple_idx)
            {
              sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
              spar_internal_error (sparp, "sparp_" "gp_trav_reuse_tabids(): dep_field not found in members");
            }
          if (dep_triple_idx <= base_idx) /* Merge is symmetrical, so this pair of key and dep is checked from other end. In that time current dep was base and the current base was dep */
            continue;
          if (OPTIONAL_L == dep_triple->_.triple.subtype) /* Optional is bad candidate for reuse */
            continue;
          if (1 != BOX_ELEMENTS (dep_triple->_.triple.tc_list)) /* Only triples with one allowed quad mapping can be reused, unions can not */
            continue;
          dep_qm = dep_triple->_.triple.tc_list[0]->tc_qm;
#if 0 /* There's no need to check this because if QMVs match then tables are the same, otherwise names does not matter anyway */
          if (strcmp (dep_qm->qmTableName, base_qm->qmTableName)) /* Can not reuse tabid for different tables */
            continue;
#endif
          dep_qmv = SPARP_FIELD_QMV_OF_QM (dep_qm, dep_field_tr_idx);
          if (key_qmv != dep_qmv) /* The key mapping differs in set of source columns or in the IRI serialization (or literal cast) */
            continue;
          if (!sparp_qmv_forms_reusable_key_of_qm (sparp, dep_qmv, dep_qm))
            continue;
          /* Glory, glory, hallelujah; we can reuse the tabid so the final SQL query will have one join less. */
          sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
          sparp_set_triple_selid_and_tabid (sparp, dep_triple, curr->_.gp.selid, base->_.triple.tabid);
          if (dep_triple_idx > (base_idx + 1)) /* Adjustment to keep reused tabids together. The old join order of dep is of zero importance because there's no more dep as a separate subtable */
            {
              int swap_ctr;
              for (swap_ctr = dep_triple_idx; swap_ctr > (base_idx + 1); swap_ctr--)
                curr->_.gp.members[swap_ctr] = curr->_.gp.members[swap_ctr-1];
              curr->_.gp.members[base_idx + 1] = dep_triple;
            }
          sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
        }
    }
}


int
sparp_gp_trav_flatten_and_reuse_tabids (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int base_idx;
  if (SPAR_GP != curr->type)
    return 0;
  switch (curr->_.gp.subtype)
    {
    case UNION_L:
      DO_BOX_FAST (SPART *, base, base_idx, curr->_.gp.members)
        {
          int breakup_offset;
          if (NULL == sparp_gp_may_reuse_tabids_in_union (sparp, base, -1))
            continue;
          breakup_offset = sparp_try_reuse_tabid_in_union (sparp, curr, base_idx);
          base_idx += breakup_offset;
        }
      END_DO_BOX_FAST;
      break;
    case 0: case WHERE_L:
#if 1 /*!!! remove after debugging */
if (NULL != sparp->sparp_storage) {
#endif
/* First of all, flatten all members of a join that are in turn simple joins and have no filters and no execution options.
It is even OK if some equivs of these joins are conflicting or always NULL, what's important is that no one equiv should replace any filters or contain more than one variable name.
The trick is safe because no equiv-based optimization takes place after any invocation of this function.
*/
      DO_BOX_FAST_REV (SPART *, base, base_idx, curr->_.gp.members)
        {
          int eq_ctr;
          SPART **base_membs;
          sparp_equiv_t *bad_eq = NULL;
          if (SPAR_GP != base->type) /* Only GPs can be flattened to their parent GPs */
            continue;
          if (0 != base->_.gp.subtype) /* Only joins will do */
            continue;
          if (0 != BOX_ELEMENTS_0 (base->_.gp.filters)) /* Can't flatten filters */
            continue;
          if (NULL != base->_.gp.options) /* Can't flatten if that will drop options */
            continue;
          SPARP_FOREACH_GP_EQUIV (sparp, base, eq_ctr, eq)
            {
              if ((eq->e_replaces_filter) || (1 < BOX_ELEMENTS (eq->e_varnames)))
                {
                  bad_eq = eq;
                  break;
                }
              if (eq->e_rvr.rvrRestrictions & (SPART_VARR_CONFLICT | SPART_VARR_ALWAYS_NULL))
                {
                  bad_eq = eq;
                  break;
                }
              if (!(eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL))
                {
                  sparp_equiv_t *outer_eq = sparp_equiv_get (sparp, curr, (SPART *)(eq->e_varnames[0]), SPARP_EQUIV_GET_NAMESAKES);
                  if (NULL == outer_eq)
                    {
#ifdef DEBUG
                      if (eq->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED)
                        spar_internal_error (sparp, "sparp_" "gp_trav_flatten_and_reuse_tabids(): exported eq in base without eq in curr");
#endif
                      continue;
                    }
                  if ((outer_eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL) || (1 < outer_eq->e_nested_bindings))
                    {
                      bad_eq = eq;
                      break;
                    }
                }
            }
          END_SPARP_FOREACH_GP_EQUIV;
          if (NULL != bad_eq) /* Some equiv blocks optimization */
            continue;
          base_membs = sparp_gp_detach_all_members (sparp, base, NULL);
/* It is possible to loose a restriction on a variable that is not used outside the base, such as named graph var of GRAPH ?x { ... } , because
there was no receiver equiv outside and hence no propagation took place. So let's propagate now without running the whole optimization loop. */
          SPARP_FOREACH_GP_EQUIV (sparp, base, eq_ctr, eq)
            {
              if (0 == BOX_ELEMENTS_0 (eq->e_receiver_idxs))
                {
                  sparp_equiv_t *outer_eq = sparp_equiv_get (sparp, curr, (SPART *)(eq->e_varnames[0]), SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_GET_NAMESAKES);
                  sparp_equiv_connect (sparp, outer_eq, eq, 1);
                  sparp_restr_of_join_eq_from_connected_subvalue (sparp, outer_eq, eq);
                }
            }
          END_SPARP_FOREACH_GP_EQUIV;
          sparp_gp_detach_member (sparp, curr, base_idx, NULL);
          sparp_gp_attach_many_members (sparp, curr, base_membs, base_idx, NULL);
          base_idx += BOX_ELEMENTS (base_membs);
        }
      END_DO_BOX_FAST;
#if 1 /*!!! remove after debugging */
}
#endif
/* After flattening, we check for possible reuse of tabids for self-joins. */
      DO_BOX_FAST (SPART *, base, base_idx, curr->_.gp.members)
        {
          if (SPAR_TRIPLE != base->type) /* Only triples have tabids to merge */
            continue;
          if (1 != BOX_ELEMENTS (base->_.triple.tc_list)) /* Only triples with one allowed quad map can be reused, unions can not */
            continue;
          if (NULL != base->_.triple.options)
            continue;
          sparp_try_reuse_tabid_in_join (sparp, curr, base_idx);
        }
      END_DO_BOX_FAST;
      break;
    case OPTIONAL_L:
      {
        sparp_equiv_t *eq_as_filter = NULL;
        SPART *base;
        quad_map_t *base_qm;
        int eq_ctr, key_field_idx;
        if ((0 != BOX_ELEMENTS_0 (curr->_.gp.filters)) ||
          (1 != BOX_ELEMENTS_0 (curr->_.gp.members)) ||
          (SPAR_TRIPLE != SPART_TYPE (curr->_.gp.members[0])) )
          break;
        if (NULL != curr->_.gp.options)
          break;
        SPARP_FOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
          {
            if (eq->e_replaces_filter)
              {
                eq_as_filter = eq;
                break;
              }
          }
        END_SPARP_FOREACH_GP_EQUIV;
        if (NULL != eq_as_filter)
          break;
        base = curr->_.gp.members[0];
        base_qm = base->_.triple.tc_list[0]->tc_qm;
        for (key_field_idx = 0; key_field_idx < SPART_TRIPLE_FIELDS_COUNT; key_field_idx++)
          {
            SPART *key_field = base->_.triple.tr_fields[key_field_idx];
            ssg_valmode_t key_fmt = base->_.triple.native_formats[key_field_idx];
            qm_value_t *key_qmv;
            sparp_jso_validate_format (sparp, key_fmt);
            if (!SPAR_IS_BLANK_OR_VAR (key_field)) /* Non-variables can not result in tabid reuse atm, !!!TBD: support for { <pk> ?p1 ?o1 . OPTIONAL { <pk> ?p2 ?o2 } } */
              continue;
            key_qmv = SPARP_FIELD_QMV_OF_QM (base_qm,key_field_idx);
            if (!sparp_qmv_forms_reusable_key_of_qm (sparp, key_qmv, base_qm))
              continue;
            t_set_push (((dk_set_t *)(common_env)), curr);
            return 0;
          }
        break;
      }
    }
  return 0;
}

#define SPARP_QM_CONDS_SOME_A_NOT_IN_B 0x1
#define SPARP_QM_CONDS_SOME_B_NOT_IN_A 0x2

int
sparp_qm_conds_cmp (sparp_t *sparp, quad_map_t *qm_a, quad_map_t *qm_b)
{
  int a_ctr, a_count = qm_a->qmAllCondCount;
  int b_ctr, b_count = qm_b->qmAllCondCount;
  int res = 0;
  for (a_ctr = a_count; a_ctr--; /* no step */)
    {
      ccaddr_t a_cond = qm_a->qmAllConds [a_ctr];
      int a_in_b = 0;
      for (b_ctr = b_count; b_ctr--; /* no step */)
        {
          ccaddr_t b_cond = qm_b->qmAllConds [b_ctr];
          if (strcmp (a_cond, b_cond))
            continue;
          a_in_b = 1;
          break;
        }
      if (a_in_b)
        continue;
      res |= SPARP_QM_CONDS_SOME_A_NOT_IN_B;
      break;
    }
  for (b_ctr = b_count; b_ctr--; /* no step */)
    {
      ccaddr_t b_cond = qm_b->qmAllConds [b_ctr];
      int b_in_a = 0;
      for (a_ctr = a_count; a_ctr--; /* no step */)
        {
          ccaddr_t a_cond = qm_a->qmAllConds [a_ctr];
          if (strcmp (b_cond, a_cond))
            continue;
          b_in_a = 1;
          break;
        }
      if (b_in_a)
        continue;
      res |= SPARP_QM_CONDS_SOME_B_NOT_IN_A;
      break;
    }
  return res;
}

static int
sparp_try_reduce_trivial_optional_via_eq (sparp_t *sparp, SPART *opt, SPART *key_field, qm_value_t *key_qmv, sparp_equiv_t *key_recv_eq, SPART *key_asc_or_self)
{
  int dep_ctr;
  SPART *key_recv_gp = key_recv_eq->e_gp;
  if ((0 != key_recv_gp->_.gp.subtype) && (WHERE_L != key_recv_gp->_.gp.subtype))
    return 0;
  for (dep_ctr = key_recv_eq->e_var_count; dep_ctr--; /* no step */)
    {
      sparp_equiv_t *key_field_eq;
      SPART *opt_triple;
      SPART *opt_parent;
      SPART *dep_field = key_recv_eq->e_vars[dep_ctr];
      int dep_triple_idx, dep_field_tr_idx, o_p_idx, field_ctr, optimizable_field_idx = 0;
      int optimization_blocked_by_filters;	/*!< Flags if the OPTIONAL can not be eliminated because it contains conditions that can not be moved to the receiver */
      int optimizable_field_count;	/*!< Number of variable fields in OPTIONAL that are not known as NOT NULL in the receiving GP */
      int really_nullable_count;	/*!< Number of variable fields in OPTIONAL that can in principle be NULL if key is not null */
      SPART *dep_triple = NULL;
      quad_map_t *dep_qm, *opt_qm;
      qm_value_t *dep_qmv;
      if (NULL == dep_field->_.var.tabid) /* The variable is not a field in a triple (const read, not gspo use) */
        continue;
      dep_field_tr_idx = dep_field->_.var.tr_idx;
      for (dep_triple_idx = BOX_ELEMENTS (key_recv_gp->_.gp.members); dep_triple_idx--; /* no step */)
        {
          dep_triple = key_recv_gp->_.gp.members[dep_triple_idx];
          if (SPAR_TRIPLE != dep_triple->type)
            continue;
          if (dep_triple->_.triple.tr_fields [dep_field_tr_idx] == dep_field)
            break;
        }
      if (0 > dep_triple_idx)
        {
          sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
          spar_internal_error (sparp, "sparp_" "try_reduce_trivial_optional_via_eq(): dep_field not found in member triples");
        }
      if (OPTIONAL_L == dep_triple->_.triple.subtype) /* Optional is bad candidate for reuse */
        continue;
      if (1 != BOX_ELEMENTS (dep_triple->_.triple.tc_list)) /* Only triples with one allowed quad mapping can be reused, unions can not */
        continue;
      dep_qm = dep_triple->_.triple.tc_list[0]->tc_qm;
      dep_qmv = SPARP_FIELD_QMV_OF_QM (dep_qm, dep_field_tr_idx);
      if (key_qmv != dep_qmv) /* The key mapping differs in set of source columns or in the IRI serialization (or literal cast) */
        continue;
      if (!sparp_qmv_forms_reusable_key_of_qm (sparp, dep_qmv, dep_qm))
        continue;
      opt_triple = opt->_.gp.members[0];
      opt_qm = opt_triple->_.triple.tc_list[0]->tc_qm;
      if (SPARP_QM_CONDS_SOME_B_NOT_IN_A & sparp_qm_conds_cmp (sparp, dep_qm, opt_qm))
        continue; /* If some WHERE conditions of optional are not in WHERE list of required then this is true LEFT OUTER */
      optimizable_field_count = 0;
      really_nullable_count = 0;
      optimization_blocked_by_filters = 0;
      /* Now we're looking for a field that may be NOT NULL outside the OPTIONAL but should be NOT NULL inside the optional binding but may be NULL in the data set */
      for (field_ctr = SPART_TRIPLE_FIELDS_COUNT; field_ctr--; /*no step*/)
        {
          SPART *fld_expn = opt_triple->_.triple.tr_fields[field_ctr];
          qm_value_t *fld_qmv;
          sparp_equiv_t *fld_eq;
          int recv_ctr, some_recv_is_nullable;
          if (fld_expn == key_field)
            continue; /* key field can't be NULL inside OPTIONAL and non-NULL outside OPTIONAL */
          fld_qmv = SPARP_FIELD_QMV_OF_QM (opt_qm, field_ctr);
          if (!SPAR_IS_BLANK_OR_VAR (fld_expn))
            {
              if (NULL == fld_qmv)
                continue; /* Const is equal to const, otherwise it would not be wiped away before as conflict */
              /* constant in triple pattern and a quad map value implies the equality condition in the OPTIONAL, can't optimize under any circumstances */
              return 0;
            }
          if ((NULL != fld_qmv) && !(SPART_VARR_NOT_NULL & fld_qmv->qmvRange.rvrRestrictions))
            really_nullable_count++;
          some_recv_is_nullable = 0;
          fld_eq = sparp_equiv_get (sparp, opt, fld_expn, SPARP_EQUIV_GET_ASSERT);
          if (1 < fld_eq->e_gspo_uses)
            return 0; /* two vars inside equiv implies the equality condition in the OPTIONAL, can't optimize after that */
          if ((NULL != fld_qmv) && (SPART_VARR_FIXED & fld_eq->e_rvr.rvrRestrictions))
            return 0; /* a fixed value for a var implies the equality condition in the OPTIONAL, can't optimize after that */
          DO_BOX_FAST (ptrlong, recv_idx, recv_ctr, fld_eq->e_receiver_idxs)
            {
              sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, recv_idx);
              if (!(recv_eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL))
                some_recv_is_nullable = 1;
/* A non-nullable variable outside should be equal to a variable inside, that implies an equality condition in join, so we can't optimize after that.
There's one exception. If the only use of a variable is in dep_triple and it is made by same quad map value then there's no need in equality condition
because both variable outside and variable inside will produce identical SQL code, hence no need to check the equality at all. */
              if ((1 < recv_eq->e_gspo_uses) || recv_eq->e_subquery_uses || (1 < BOX_ELEMENTS_0 (recv_eq->e_subvalue_idxs)))
                return 0;
              if (1 == recv_eq->e_gspo_uses)
                {
                  int d_fld_ctr;
                  for (d_fld_ctr = SPART_TRIPLE_FIELDS_COUNT; d_fld_ctr--; /* no step */)
                    {
                      SPART *d_fld = dep_triple->_.triple.tr_fields[d_fld_ctr];
                      if (SPAR_IS_BLANK_OR_VAR (d_fld) && (d_fld->_.var.equiv_idx == recv_idx))
                        break;
                    }
                  if (0 > d_fld_ctr)
                    { /* The GSPO use is not found in dep_triple, hence that's not an exception. */
                      optimization_blocked_by_filters = 1;
                      break;
                    }
                  if (SPARP_FIELD_QMV_OF_QM (dep_qm, d_fld_ctr) != fld_qmv)
                    { /* The inner var occurs only in dep_triple, but made by other qmv, hence that's not an exception. */
                      optimization_blocked_by_filters = 1;
                      break;
                    }
                }
            }
          END_DO_BOX_FAST;
          if (!some_recv_is_nullable)
            continue;
          optimizable_field_idx = field_ctr;
          optimizable_field_count++;
        }
      if (optimization_blocked_by_filters || ((0 != really_nullable_count) && (1 < optimizable_field_count)))
        continue; /* If more than one variable is not known outside as NOT_NULL then the optimized variant may produce a solution with one optional variable bound and one NULL */
      /* Glory, glory, hallelujah; we can cut optional triple reuse the tabid so the final SQL query will have one join less. */
      sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
      key_field_eq = SPARP_EQUIV (sparp, key_field->_.var.equiv_idx);
      opt_parent = SPARP_EQUIV (sparp, key_field_eq->e_receiver_idxs[0])->e_gp;
      sparp_gp_detach_member (sparp, opt, 0, NULL);
      o_p_idx = BOX_ELEMENTS(opt_parent->_.gp.members) - 1;
      if (opt_parent->_.gp.members [o_p_idx] != opt)
        spar_internal_error (sparp, "sparp_" "try_reduce_trivial_optional_via_eq(): can not locate OPTIONAL in parent");
      sparp_gp_detach_member (sparp, opt_parent, o_p_idx, NULL);
      sparp_gp_attach_member (sparp, key_recv_gp, opt_triple, dep_triple_idx+1, NULL);
      sparp_set_triple_selid_and_tabid (sparp, opt_triple, key_recv_gp->_.gp.selid, dep_triple->_.triple.tabid);
      if (0 != really_nullable_count)
        {
          opt_triple->_.triple.subtype = OPTIONAL_L;
          if (optimizable_field_count)
            {
              SPART *optimizable_field = opt_triple->_.triple.tr_fields [optimizable_field_idx];
              if (SPAR_IS_BLANK_OR_VAR (optimizable_field))
                optimizable_field->_.var.rvr.rvrRestrictions &= ~SPART_VARR_NOT_NULL;
            }
        }
      sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
      return 1;
    }
  DO_BOX_FAST (ptrlong, dep_idx, dep_ctr, key_recv_eq->e_subvalue_idxs)
    {
      sparp_equiv_t *dep_eq = SPARP_EQUIV (sparp, dep_idx);
      if (dep_eq->e_gp == key_asc_or_self) /* -- Back to origin? -- No, thanks. */
        continue;
      if (sparp_try_reduce_trivial_optional_via_eq (sparp, opt, key_field, key_qmv, dep_eq, NULL))
        return 1;
    }
  END_DO_BOX_FAST;
  DO_BOX_FAST (ptrlong, dep_idx, dep_ctr, key_recv_eq->e_receiver_idxs)
    {
      sparp_equiv_t *dep_eq = SPARP_EQUIV (sparp, dep_idx);
      if (sparp_try_reduce_trivial_optional_via_eq (sparp, opt, key_field, key_qmv, dep_eq, key_recv_gp))
        return 1;
    }
  END_DO_BOX_FAST;
  return 0;
}

static int
sparp_reduce_trivial_optional (sparp_t *sparp, SPART *opt)
{
  SPART *base = opt->_.gp.members[0];
  quad_map_t *base_qm = base->_.triple.tc_list[0]->tc_qm;
  int key_field_idx, recv_ctr;
  for (key_field_idx = 0; key_field_idx < SPART_TRIPLE_FIELDS_COUNT; key_field_idx++)
    {
      SPART *key_field = base->_.triple.tr_fields[key_field_idx];
      ssg_valmode_t key_fmt = base->_.triple.native_formats[key_field_idx];
      qm_value_t *key_qmv;
      sparp_equiv_t *key_eq;
      sparp_jso_validate_format (sparp, key_fmt);
      if (!SPAR_IS_BLANK_OR_VAR (key_field)) /* Non-variables can not result in tabid reuse atm, !!!TBD: support for { <pk> ?p1 ?o1 . OPTIONAL { <pk> ?p2 ?o2 } } */
        continue;
      key_qmv = SPARP_FIELD_QMV_OF_QM (base_qm,key_field_idx);
      if (!sparp_qmv_forms_reusable_key_of_qm (sparp, key_qmv, base_qm))
        continue;
      key_eq = sparp_equiv_get (sparp, opt, key_field, SPARP_EQUIV_GET_ASSERT);
      DO_BOX_FAST (ptrlong, recv_idx, recv_ctr, key_eq->e_receiver_idxs)
        {
          sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, recv_idx);
          if (sparp_try_reduce_trivial_optional_via_eq (sparp, opt, key_field, key_qmv, recv_eq, opt))
            return 1;
        }
      END_DO_BOX_FAST;
    }
  return 0;
}


void
sparp_rewrite_all (sparp_t *sparp, int safely_copy_retvals)
{
  ptrlong top_type = SPART_TYPE (sparp->sparp_expr);
  if ((NULL == sparp->sparp_env->spare_storage_name) && (NULL == sparp->sparp_storage))
    sparp->sparp_storage = sparp_find_storage_by_name (NULL);
  if (SPAR_QM_SQL_FUNCALL == top_type)
    return;
  if (SPAR_CODEGEN == top_type)
    return;
  sparp_rewrite_retvals (sparp, safely_copy_retvals);
  if ((sparp->sparp_env->spare_src.ssrc_grab.rgc_pview_mode) && (NULL == sparp->sparp_parent_sparp))
    {
      sparp_rewrite_grab (sparp);
      return;
    }
  sparp_rewrite_qm (sparp);
}

/*! This fixes sparql select * where {{ select * where { ?s <knows> ?o }} . filter (?s=<me>)}.
Without the fix, the SQL text lacks s=<me> condition in WHERE because it's converted to SPART_VARR_FIXED of equiv but not printed in any triple pattern */
int
sparp_gp_trav_restore_filters_for_weird_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int eq_ctr;
  if (SPAR_GP != SPART_TYPE (curr))
    return SPAR_GPT_NODOWN;
  if (SELECT_L != curr->_.gp.subtype)
    return SPAR_GPT_ENV_PUSH; /* SPAR_GPT_ENV_PUSH is not really required for this callback by itself,
because parent gp can be obtained as an ancestor_gp, but it is required for \c sparp_gp_trav_add_graph_perm_read_filters() that can be
used in one iteration with sparp_gp_trav_restore_filters_for_weird_subq(). Permission processing is a postorder callback whereas
restoring filters is a preorder one, the postorder needs a complete stack of things */
  SPARP_FOREACH_GP_EQUIV (sparp, curr, eq_ctr, eq)
    {
      sparp_equiv_t *subq_eq, *recv_eq;
      SPART *parent_gp;
      ptrlong missing_restrictions;
      subq_eq = sparp_equiv_get (sparp,
        curr->_.gp.subquery->_.req_top.pattern, (SPART *)(eq->e_varnames[0]),
        SPARP_EQUIV_GET_NAMESAKES );
      if (NULL == subq_eq)
        continue;
      missing_restrictions = (eq->e_rvr.rvrRestrictions & ~subq_eq->e_rvr.rvrRestrictions) & SPART_VARR_FIXED;
      if (!missing_restrictions)
        continue;
      parent_gp = sts_this->sts_parent;
      if (0 != BOX_ELEMENTS_0 (eq->e_receiver_idxs))
        {
          recv_eq = SPARP_EQUIV (sparp, eq->e_receiver_idxs[0]);
          if ((0 != recv_eq->e_gspo_uses) && (recv_eq->e_rvr.rvrRestrictions & SPART_VARR_NOT_NULL) &&
            (0 == eq->e_pos1_t_in) && (0 == eq->e_pos1_t_out) )
            continue;
        }
      else
        recv_eq = NULL;
      if (missing_restrictions & SPART_VARR_FIXED)
        {
          SPART *l = NULL, *r, *filt;
          l = spartlist (sparp, 6 + (sizeof (rdf_val_range_t) / sizeof (caddr_t)),
            SPAR_VARIABLE, eq->e_varnames[0],
            parent_gp->_.gp.selid, NULL,
            (ptrlong)(0), SPART_BAD_EQUIV_IDX, SPART_RVR_LIST_OF_NULLS );
            memcpy (&(l->_.var.rvr.rvrRestrictions), &(subq_eq->e_rvr), sizeof (rdf_val_range_t));
          if (eq->e_rvr.rvrRestrictions & SPART_VARR_IS_REF)
            r = spartlist (sparp, 2, SPAR_QNAME, eq->e_rvr.rvrFixedValue);
          else if (DV_ARRAY_OF_POINTER == DV_TYPE_OF (eq->e_rvr.rvrFixedValue))
            r = (SPART *)eq->e_rvr.rvrFixedValue;
          else
            r = spartlist (sparp, 4, SPAR_LIT, eq->e_rvr.rvrFixedValue, eq->e_rvr.rvrDatatype, eq->e_rvr.rvrLanguage);
          filt = spartlist (sparp, 3, BOP_EQ, l, r);
          sparp_gp_attach_filter (sparp, parent_gp, filt, 0, NULL);
          if (NULL == recv_eq)
            {
              recv_eq = SPARP_EQUIV (sparp, l->_.var.equiv_idx);
              sparp_equiv_connect (sparp, recv_eq, eq, 1);
            }
          recv_eq->e_rvr.rvrRestrictions &= ~SPART_VARR_FIXED;
          eq->e_rvr.rvrRestrictions &= ~SPART_VARR_FIXED;
        }
    }
  END_SPARP_FOREACH_GP_EQUIV;
  return SPAR_GPT_NODOWN;
}

int
sparp_gp_trav_add_graph_perm_read_filters (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  int depth, membctr, membcount;
  if (SPAR_GP != SPART_TYPE (curr))
    return 0;
  if (SELECT_L == curr->_.gp.subtype)
    return 0;
  if (sparp->sparp_query_uses_sinvs)
    { /* Bug 14737 fix: No permission filters should be placed inside service invocations */
      int sts_idx;
      if (sparp->sparp_env->spare_storage_name == uname_virtrdf_ns_uri_DefaultServiceStorage)
        return 0;
      if (NULL != sparp_get_option (sparp, curr->_.gp.options, SPAR_SERVICE_INV))
        return 0;
      for (sts_idx = 0; NULL != sts_this[sts_idx].sts_ancestor_gp; sts_idx++)
        {
          if (NULL != sparp_get_option (sparp, sts_this[sts_idx].sts_ancestor_gp->_.gp.options, SPAR_SERVICE_INV))
            return 0;
        }
    }
  membcount = BOX_ELEMENTS_0 (curr->_.gp.members);
  for (membctr = 0; membctr < membcount; membctr++)
    {
      SPART *memb = curr->_.gp.members[membctr];
      SPART *g_expn, *g_copy, *filter;
      const SPART *g_norm_expn;
      ccaddr_t fixed_g;
      dtp_t g_norm_expn_dtp;
      int g_norm_is_var;
      SPART *gp_of_cache;
      if (SPAR_TRIPLE != memb->type)
        continue;
      g_expn = memb->_.triple.tr_graph;
      if (!spar_graph_needs_security_testing (sparp, g_expn, RDF_GRAPH_PERM_READ))
        continue;
      if (spar_plain_const_value_of_tree (g_expn, &fixed_g))
        {
          g_norm_expn = (const SPART *)fixed_g;
          g_norm_expn_dtp = DV_TYPE_OF (g_norm_expn);
          g_norm_is_var = 0;
        }
      else
        {
          g_norm_expn = g_expn;
          if (!SPAR_IS_BLANK_OR_VAR (g_norm_expn))
            continue;
          g_norm_expn_dtp = DV_ARRAY_OF_POINTER;
          g_norm_is_var = 1;
        }
      gp_of_cache = curr;
      for (depth = 0; ; depth--)
        {
          dk_set_t cached_graph_expns;
          cached_graph_expns = sts_this[depth].sts_env;
          DO_SET (SPART *, prev, &cached_graph_expns)
            {
              if (DV_TYPE_OF (prev) != g_norm_expn_dtp)
                continue;
              if (g_norm_is_var)
                {
                  if (prev->_.var.equiv_idx == g_norm_expn->_.var.equiv_idx)
                    goto g_norm_expn_is_dupe; /* see below */
                  if (!strcmp (prev->_.var.vname, g_norm_expn->_.var.vname))
                    goto g_norm_expn_is_dupe; /* see below */
                }
              else if (box_equal (prev, g_norm_expn))
                goto g_norm_expn_is_dupe; /* see below */
            }
          END_DO_SET()
          if ((OPTIONAL_L == gp_of_cache->_.gp.subtype) || (WHERE_L == gp_of_cache->_.gp.subtype))
            break;
          gp_of_cache = sts_this[depth].sts_parent;
          if (UNION_L == gp_of_cache->_.gp.subtype)
            break;
        }
      g_copy = sparp_tree_full_copy (sparp, g_norm_expn, curr);
      if (g_norm_is_var)
        {
          g_copy->_.var.tr_idx = 0;
          g_copy->_.var.tabid = NULL;
          g_copy->_.var.equiv_idx = SPART_BAD_EQUIV_IDX;
        }
      filter = spar_make_funcall (sparp, 0,
        ((NULL != sparp->sparp_gs_app_callback) ? "SPECIAL::bif:__rgs_ack_cbk" : "SPECIAL::bif:__rgs_ack"),
        (SPART **)t_list (3, g_copy, spar_exec_uid_and_gs_cbk (sparp), RDF_GRAPH_PERM_READ) );
      sparp_gp_attach_filter (sparp, curr, filter, 0, NULL);
      if (!g_norm_is_var ||
        ((SPART_VARR_NOT_NULL & g_norm_expn->_.var.rvr.rvrRestrictions) &&
          !(SPART_VARR_CONFLICT & g_norm_expn->_.var.rvr.rvrRestrictions) ) )
        t_set_push ((dk_set_t *)(&(sts_this[0].sts_env)), (SPART *)g_norm_expn);
g_norm_expn_is_dupe: ;
    }
  if ((OPTIONAL_L != curr->_.gp.subtype) &&
    (WHERE_L != curr->_.gp.subtype) &&
    (UNION_L != sts_this[0].sts_parent->_.gp.subtype) )
    {
      dk_set_t curr_graph_expns = sts_this[0].sts_env;
      dk_set_t parent_graph_expns = sts_this[-1].sts_env;
      DO_SET (SPART *, expn, &curr_graph_expns)
        {
          dtp_t expn_dtp = DV_TYPE_OF (expn);
          int expn_is_var = SPAR_IS_BLANK_OR_VAR (expn);
          DO_SET (SPART *, prev, &parent_graph_expns)
            {
              if (DV_TYPE_OF (prev) != expn_dtp)
                continue;
              if (expn_is_var)
                {
                  if (prev->_.var.equiv_idx == expn->_.var.equiv_idx)
                    goto expn_is_dupe; /* see below */
                  if (!strcmp (prev->_.var.vname, expn->_.var.vname))
                    goto expn_is_dupe; /* see below */
                }
              else if (box_equal (prev, expn))
                goto expn_is_dupe; /* see below */
            }
          END_DO_SET ()
          t_set_push (&parent_graph_expns, expn);
          if (expn_is_var)
            {
              sparp_equiv_t *expn_eq = SPARP_EQUIV (sparp, expn->_.var.equiv_idx);
              int recvctr;
              DO_BOX_FAST (ptrlong, recv_idx, recvctr, expn_eq->e_receiver_idxs)
                {
                  sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, recv_idx);
                  if (recv_eq->e_var_count)
                    t_set_push (&parent_graph_expns, recv_eq->e_vars[0]);
                }
              END_DO_BOX_FAST;
            }
expn_is_dupe: ;
        }
      END_DO_SET ()
    }
  return 0;
}

void
sparp_rewrite_qm (sparp_t *sparp)
{
  if (SPAR_CODEGEN == SPART_TYPE (sparp->sparp_expr))
    return;
  if (SPAR_QM_SQL_FUNCALL == SPART_TYPE (sparp->sparp_expr))
    return;
  sparp_rewrite_qm_preopt (sparp, 1);
  sparp_rewrite_qm_optloop (sparp, SPARP_MULTIPLE_OPTLOOPS);
  sparp_rewrite_qm_postopt (sparp);
}

int
sparp_dig_and_glue_loj_filter_for_eq (sparp_t *sparp, sparp_equiv_t *eq)
{
  SPART *gp = eq->e_gp;
  sparp_equiv_t *good_recv_eq = NULL;
  SPART *good_recv_gp;
  int recv_ctr, filter_ctr;
  if ((OPTIONAL_L != gp->_.gp.subtype) || (0 == eq->e_const_reads) || (0 == BOX_ELEMENTS_0 (eq->e_receiver_idxs)))
    return 0;
  for (recv_ctr = BOX_ELEMENTS (eq->e_receiver_idxs); recv_ctr--; /* no step */)
    {
      sparp_equiv_t *recv_eq = SPARP_EQUIV(sparp, eq->e_receiver_idxs[recv_ctr]);
      if (SPARP_EQ_IS_ASSIGNED_EXTERNALLY(recv_eq) || SPARP_EQ_IS_ASSIGNED_LOCALLY(recv_eq))
        {
          good_recv_eq = recv_eq;
          break;
        }
    }
  if (good_recv_eq == NULL)
    return 0;
  good_recv_gp = good_recv_eq->e_gp;
  for (filter_ctr = BOX_ELEMENTS_0 (gp->_.gp.filters) - gp->_.gp.glued_filters_count; filter_ctr--; /* no step */)
    {
      SPART *filt = gp->_.gp.filters[filter_ctr];
      if (!sparp_expn_reads_equiv (sparp, filt, eq))
        continue;
      good_recv_gp = good_recv_eq->e_gp;
      if (UNION_L == good_recv_gp->_.gp.subtype)
        spar_error (sparp, "Variable '%.100s' is used in OPTIONAL inside UNION but not assigned in OPTIONAL, please rephrase the query", eq->e_varnames[0]);
      sparp_gp_detach_filter (sparp, gp, filter_ctr, NULL);
      sparp_gp_attach_filter (sparp, good_recv_gp, filt, BOX_ELEMENTS_0 (good_recv_gp->_.gp.filters) - good_recv_gp->_.gp.glued_filters_count, NULL);
      good_recv_gp->_.gp.glued_filters_count += 1;
    }
  return 0;
}

void
sparp_rewrite_qm_preopt (sparp_t *sparp, int safely_copy_retvals)
{
  sparp_equiv_t **equivs /* do not set here */;
  int equiv_ctr, equiv_count;
  if (SPAR_CODEGEN == SPART_TYPE (sparp->sparp_expr))
    GPF_T1 ("sparp_" "rewrite_qm_preopt () for CODEGEN");
  if (SPAR_QM_SQL_FUNCALL == SPART_TYPE (sparp->sparp_expr))
    GPF_T1 ("sparp_" "rewrite_qm_preopt () for SQL_FUNCALL");

retry_preopt:
  sparp_rewrite_basic (sparp);
  equivs = sparp->sparp_sg->sg_equivs;
  equiv_count = sparp->sparp_sg->sg_equiv_count;
  for (equiv_ctr = sparp->sparp_first_equiv_idx; equiv_ctr < equiv_count; equiv_ctr++)
    {
      sparp_equiv_t *eq = equivs[equiv_ctr];
      if (NULL == eq)
        continue;
      if (SPARP_EQ_IS_ASSIGNED_EXTERNALLY(eq) || SPARP_EQ_IS_ASSIGNED_LOCALLY(eq))
        continue;
      if (!SPARP_EQ_IS_USED(eq))
        continue;
      if ((1 == eq->e_var_count) && SPART_VARNAME_IS_SPECIAL (eq->e_varnames[0]))
        continue; /* Special variable is not assigned in SPARQL (by BGPs or externals) but it is assigned in SQL code by codegen. Can't check, just believe in the codegen :) */
      /* At this point we know that some variable is used but not assigned. It may be non-optional variable in FILTER inside OPTIONAL or an error. */
      if (sparp_dig_and_glue_loj_filter_for_eq (sparp, eq))
        goto retry_preopt; /* see above */
      if (sparp->sparp_env->spare_signal_void_variables)
        {
          if (eq->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED)
            spar_error (sparp, "Variable '%.100s' is used in the query result set but not assigned", eq->e_varnames[0]);
          if ((0 != eq->e_const_reads) || /* note: no check for (0 != eq->e_optional_reads) */
            (0 != BOX_ELEMENTS_0 (eq->e_receiver_idxs)) )
            spar_error (sparp, "Variable '%.100s' is used in subexpressions of the query but not assigned", eq->e_varnames[0]);
        }
    }
/* Building qm_list for every triple in the tree. */
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_refresh_triple_cases, NULL,
    NULL, NULL, NULL,
    NULL );
  sparp_equiv_audit_all (sparp, 0);
  sparp_rewrite_basic (sparp);
}

int
sparp_gp_trav_rewrite_qm_optloop (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  if (sparp_rewrite_qm_optloop (sub_sparp, (ptrlong)common_env))
    sparp->sparp_rewrite_dirty = 1;
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

int
sparp_rewrite_qm_optloop (sparp_t *sparp, int opt_ctr)
{
  int res;
  int saved_sparp_rewrite_dirty = sparp->sparp_rewrite_dirty;
  if (SPAR_CODEGEN == SPART_TYPE (sparp->sparp_expr))
    GPF_T1 ("sparp_" "rewrite_qm_postopt () for CODEGEN");
  if (SPAR_QM_SQL_FUNCALL == SPART_TYPE (sparp->sparp_expr))
    GPF_T1 ("sparp_" "rewrite_qm_optloop () for SQL_FUNCALL");
  if (SPARP_MULTIPLE_OPTLOOPS == opt_ctr)
    {
      SPART *binv = sparp->sparp_expr->_.req_top.binv;
      int old_bindings_len = ((NULL != binv) ? BOX_ELEMENTS (binv->_.binv.data_rows) : -1);
      int optimization_loop_ctr = 0;
      int optimization_loop_count = 10 + ((NULL != binv) ? 2 * BOX_ELEMENTS (binv->_.binv.vars) : 0);
      while (sparp_rewrite_qm_optloop (sparp, optimization_loop_ctr))
        {
          if (0 < old_bindings_len)
            { /* Shortening BINDINGS table can be arbitrarily long but it can't be infinite so optimization_loop_ctr is left intact */
              int new_bindings_len = BOX_ELEMENTS (binv->_.binv.data_rows);
              if (new_bindings_len < old_bindings_len)
                {
                  old_bindings_len = new_bindings_len;
                  continue;
                }
            }
          if (++optimization_loop_ctr < optimization_loop_count)
            continue;
#if 0
          spar_internal_error (sparp, "SPARQL optimizer performed too many rounds of query rewriting, this looks like endless loop. Please rephrase the query.");
#else
          break; /* Now our public endpoints are protected with timeouts, we can try SQL garbage */
#endif
        }
      return optimization_loop_ctr;
    }
  sparp_equiv_audit_all (sparp, 0);
  sparp->sparp_rewrite_dirty = 0;
/* Converting to GP_UNION of every triple such that many quad maps contains triples that matches the mapping pattern */
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, (void *)((ptrlong)opt_ctr),
    sparp_gp_trav_refresh_triple_cases, sparp_gp_trav_multiqm_to_unions,
    NULL, NULL, sparp_gp_trav_rewrite_qm_optloop,
    NULL );
  sparp_trav_out_clauses (sparp, sparp->sparp_expr, (void *)((ptrlong)opt_ctr),
    NULL, NULL,
    NULL, NULL, sparp_gp_trav_rewrite_qm_optloop,
    NULL );
  sparp_equiv_audit_all (sparp, 0);
  sparp_rewrite_basic (sparp);
  sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
/* Converting join with a union into a union of joins with parts of union */
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_union_of_joins_in, sparp_gp_trav_union_of_joins_out,
    NULL, NULL, NULL,
    NULL );
  sparp_equiv_audit_all (sparp, 0);
  sparp_rewrite_basic (sparp);
/* Removal of gps that can not produce results */
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    NULL, sparp_gp_trav_detach_conflicts_out,
    NULL, NULL, NULL,
    NULL );
  sparp_equiv_audit_all (sparp, 0);
  sparp_rewrite_basic (sparp);
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    NULL, sparp_gp_trav_detach_conflicts_out,
    NULL, NULL, NULL,
    NULL );
  sparp_equiv_audit_all (sparp, 0);
  sparp_rewrite_basic (sparp);
  sparp_equiv_audit_all (sparp, 0);
  if (!(opt_ctr % 2)) /* Do this not in every loop because it's costly and it almost never give a result after first loop */
    {
      sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
        sparp_gp_trav_localize_filters, NULL,
        NULL, NULL, NULL,
        NULL );
      sparp_equiv_audit_all (sparp, 0);
      sparp_rewrite_basic (sparp);
      sparp_equiv_audit_all (sparp, 0);
    }
  sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
  sparp_trav_out_clauses (sparp, sparp->sparp_expr, sparp->sparp_expr->_.req_top.pattern,
    NULL, NULL,
    NULL, NULL, sparp_gp_trav_rewrite_qm_optloop,
    NULL );
  sparp_equiv_audit_all (sparp, SPARP_EQUIV_AUDIT_NOBAD);
  res = sparp->sparp_rewrite_dirty;
  sparp->sparp_rewrite_dirty = saved_sparp_rewrite_dirty;
  return res;
}

int
sparp_retval_should_wrap_distinct (sparp_t *sparp, SPART *tree, SPART *rv)
{
  ssg_valmode_t rv_valmode = sparp_expn_native_valmode (sparp, rv);
  if (SSG_VALMODE_SQLVAL == rv_valmode)
    return 0;
  if (SSG_VALMODE_LONG == rv_valmode)
    {
      ptrlong rv_restr = sparp_restr_bits_of_expn (sparp, rv);
      if (rv_restr & (SPART_VARR_IS_REF | SPART_VARR_LONG_EQ_SQL))
        return 0;
      return 1;
    }
  if (IS_BOX_POINTER(rv_valmode))
    {
      ptrlong rv_restr;
      if (!rv_valmode->qmfWrapDistinct)
        return 0;
      rv_restr = sparp_restr_bits_of_expn (sparp, rv);
      if (rv_restr & (SPART_VARR_IS_REF | SPART_VARR_LONG_EQ_SQL))
        return 0;
      return 1;
    }
  return 0;
}

int
sparp_some_retvals_should_wrap_distinct (sparp_t *sparp, SPART *tree)
{
  int ctr;
  SPART **retvals = tree->_.req_top.retvals;
#ifndef NDEBUG
  if (DISTINCT_L != tree->_.req_top.subtype)
    spar_internal_error (sparp, "sparp_" "some_retvals_should_wrap_distinct() for non-DISTINCT");
#endif
  DO_BOX_FAST(SPART *, rv, ctr, retvals)
    {
      if (sparp_retval_should_wrap_distinct (sparp, tree, rv))
        return 1;
    }
  END_DO_BOX_FAST;
  return 0;
}

int
sparp_gp_trav_list_external_vars_gp_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if ((SPAR_GP == SPART_TYPE (curr)) && (SELECT_L == curr->_.gp.subtype))
    {
      sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
      sparp_list_external_vars (sub_sparp, curr->_.gp.subquery, common_env);
      sparp_up_from_sub (sparp, curr, sub_sparp);
    }
  return 0;
}

int
sparp_gp_trav_list_external_vars_expn_in (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  if (SPAR_IS_BLANK_OR_VAR (curr) && (curr->_.var.rvr.rvrRestrictions & (SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL)))
    t_set_push_new_string (common_env, curr->_.var.vname);
  return 0;
}

int
sparp_gp_trav_list_external_vars_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_t *sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_list_external_vars (sub_sparp, curr->_.gp.subquery, common_env);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

void
sparp_list_external_vars (sparp_t *sparp, SPART *tree, dk_set_t *set_ret)
{
  SPART *top,*top_pattern;
  if (SPAR_REQ_TOP == SPART_TYPE (tree))
    {
      top = tree;
      top_pattern = top->_.req_top.pattern;
    }
  else
    {
      top = NULL;
      top_pattern = tree;
    }
  if (NULL != top)
    sparp_trav_out_clauses (sparp, top, set_ret,
      NULL, NULL,
      sparp_gp_trav_list_external_vars_expn_in, NULL, sparp_gp_trav_list_external_vars_expn_subq,
      NULL );
  sparp_gp_trav (sparp, top_pattern, set_ret,
    sparp_gp_trav_list_external_vars_gp_in, NULL,
    sparp_gp_trav_list_external_vars_expn_in, NULL, sparp_gp_trav_list_external_vars_expn_subq,
    NULL );
}

void
sparp_fill_sinv_varlists (sparp_t *sparp, SPART *root)
{
  int equiv_ctr, equiv_count = sparp->sparp_sg->sg_equiv_count;
  dk_set_t all_sinvs = NULL;
  for (equiv_ctr = equiv_count; equiv_ctr--; /* no step */)
    {
      sparp_equiv_t *eq = sparp->sparp_sg->sg_equivs[equiv_ctr];
      SPART *gp;
      if ((NULL == eq) || !SPARP_EQ_IS_USED(eq))
        continue;
      gp = eq->e_gp;
      if (SERVICE_L == gp->_.gp.subtype)
        t_set_pushnew (&all_sinvs, gp);
    }
  DO_SET (SPART *, gp, &(all_sinvs))
    {
      SPART *sinv = sparp_get_option (sparp, gp->_.gp.options, SPAR_SERVICE_INV);
      int varctr, eqctr;
      caddr_t **param_varnames_ptr = &(sinv->_.sinv.param_varnames);
      dk_set_t used_globals = NULL;
      dk_set_t new_used_globals_as_params = NULL;
      dk_set_t rset_varnames = NULL;
      sparp_list_external_vars (sparp, gp, &used_globals);
      /* Check if all IN variables are adequate (or adequate but disconnected by the optimizer for a good reason).
         Disconnected non-externals are silently removed from the IN list. */
      DO_BOX_FAST_REV (caddr_t, param_var_name, varctr, param_varnames_ptr[0])
        { /* The loop direction is important due to possible removals */
          sparp_equiv_t *param_xfer_equiv = sparp_equiv_get (sparp, gp, (SPART *)param_var_name, SPARP_EQUIV_GET_NAMESAKES);
          if ((NULL == param_xfer_equiv) || (0 == BOX_ELEMENTS_0 (param_xfer_equiv->e_receiver_idxs)))
            {
              if ((NULL != param_xfer_equiv) &&
                ((SPART_VARR_FIXED | SPART_VARR_ALWAYS_NULL | SPART_VARR_CONFLICT) & param_xfer_equiv->e_rvr.rvrRestrictions) )
                {
                  if (!((SPART_VARR_EXTERNAL | SPART_VARR_GLOBAL) & param_xfer_equiv->e_rvr.rvrRestrictions))
                    param_varnames_ptr[0] = t_list_remove_nth ((caddr_t)(param_varnames_ptr[0]), varctr);
                  continue;
                }
              if (0 > dk_set_position_of_string (used_globals, param_var_name))
                {
                  if (!sinv->_.sinv.in_list_implicit)
                    spar_error (sparp, "SERVICE <%.200s> (...) declares IN ?%.200s variable but an IN variable should be used both inside and outside the SERVICE clause",
                      sinv->_.sinv.endpoint, param_var_name );
                  param_varnames_ptr[0] = t_list_remove_nth ((caddr_t)(param_varnames_ptr[0]), varctr);
                }
            }
        }
      END_DO_BOX_FAST_REV;
      /* Now we try to extend list of IN vars with externals. Used externals are always passed, even if fixed, because the service can be the only place in the query where equality between external and fixed value is tested */
      DO_SET (caddr_t, globname, &used_globals)
        {
          int found = 0;
          DO_BOX_FAST_REV (caddr_t, param_var_name, varctr, sinv->_.sinv.param_varnames)
            {
              if (strcmp (param_var_name, globname))
                continue;
              found = 1;
              break;
            }
          END_DO_BOX_FAST_REV;
          if (!found)
            t_set_push (&new_used_globals_as_params, globname);
        }
      END_DO_SET()
      if (NULL != new_used_globals_as_params)
        sinv->_.sinv.param_varnames = t_list_concat ((caddr_t)(sinv->_.sinv.param_varnames), (caddr_t)t_revlist_to_array (new_used_globals_as_params));
      /* Finally what's not in extended list of param vars but in equivs of the SERVICE gp and has relations to the parent should become retvals */
      SPARP_FOREACH_GP_EQUIV (sparp, gp, eqctr, eq)
        {
          int eq_varname_ctr;
          caddr_t specimen_varname = NULL;
          if (0 == BOX_ELEMENTS_0 (eq->e_receiver_idxs))
            continue; /* The eq is used only internally, no need to return values */
          DO_BOX_FAST_REV (caddr_t, eq_var_name, eq_varname_ctr, eq->e_varnames)
            {
              DO_BOX_FAST_REV (caddr_t, param_var_name, varctr, sinv->_.sinv.param_varnames)
                {
                  if (!strcmp (param_var_name, eq_var_name))
                    goto name_from_eq_is_found_in_params; /* see below */
                }
              END_DO_BOX_FAST_REV;
              if (SPART_VARNAME_IS_GLOB (eq_var_name))
                continue;
              if (SPART_VARNAME_IS_SPECIAL (eq_var_name))
                continue;
              if (SPART_VARNAME_IS_BNODE (eq_var_name))
                continue;
              specimen_varname = eq_var_name;
            }
          END_DO_BOX_FAST_REV;
          if (NULL != specimen_varname)
            t_set_push (&rset_varnames, specimen_varname);
name_from_eq_is_found_in_params: ;
        }
      END_SPARP_FOREACH_GP_EQUIV;
      sinv->_.sinv.rset_varnames = t_list_to_array (rset_varnames);
    }
  END_DO_SET()
}

void
sparp_tweak_order_of_iter (sparp_t *sparp, SPART **obys)
{
  int oby_ctr;
  DO_BOX_FAST (SPART *, oby, oby_ctr, obys)
    {
      caddr_t lit_val;
      int col_idx;
      SPART *tree, *tree_copy;
      if (SPAR_LIT != SPART_TYPE (oby->_.oby.expn))
        continue;
      lit_val = SPAR_LIT_VAL (oby->_.oby.expn);
      if (DV_LONG_INT != DV_TYPE_OF (lit_val))
        continue;
      col_idx = unbox (lit_val);
      if ((0 >= col_idx) && (col_idx > BOX_ELEMENTS (sparp->sparp_expr->_.req_top.orig_retvals)))
        continue;
      tree = sparp->sparp_expr->_.req_top.orig_retvals [col_idx-1];
      while (SPAR_ALIAS == SPART_TYPE (tree))
        tree = tree->_.alias.arg;
      tree_copy = sparp_tree_full_copy (sparp, tree, sparp->sparp_expr->_.req_top.pattern);
      oby->_.oby.expn = tree_copy;
    }
  END_DO_BOX_FAST;
}

int
sparp_gp_trav_rewrite_qm_postopt (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
  sparp_t *sub_sparp;
  if ((SPAR_GP != SPART_TYPE (curr)) || (SELECT_L != curr->_.gp.subtype))
    return 0;
  sub_sparp = sparp_down_to_sub (sparp, curr);
  sparp_rewrite_qm_postopt (sub_sparp);
  sparp_up_from_sub (sparp, curr, sub_sparp);
  return 0;
}

int
sparp_req_top_has_limofs (SPART *tree)
{
  if (SPAR_REQ_TOP != SPART_TYPE (tree))
    return 0;
  if ((DV_LONG_INT == DV_TYPE_OF (tree->_.req_top.limit)) && (DV_LONG_INT == DV_TYPE_OF (tree->_.req_top.offset)))
    {
      /*long lim = unbox ((caddr_t)(tree->_.req_top.limit));*/
      long ofs = unbox ((caddr_t)(tree->_.req_top.offset));
      if ((NULL == tree->_.req_top.limit) && (0 == ofs))
        return 0;
      return 1;
    }
  return 2;
}

SPART *
sparp_limit_for_cutting_inner_limits (sparp_t *sparp, SPART *lim, SPART *ofs)
{
  if (NULL == lim)
    return NULL;
  if (NULL == ofs)
    return lim;
  if ((DV_LONG_INT == DV_TYPE_OF (lim)) && (DV_LONG_INT == DV_TYPE_OF (ofs)))
    return (SPART *)t_box_num_nonull (unbox ((caddr_t)lim) + ((0 < unbox ((caddr_t)ofs)) ? unbox ((caddr_t)ofs) : 0));
  return spartlist (sparp, 3, BOP_PLUS, lim,
      spar_make_funcall (sparp, 0, "bif:__max_notnull", (SPART **)t_list (2, ofs, t_box_num_nonull (0))) );
}

SPART *
sparp_cut_inner_limit_with_outer_limit (sparp_t *sparp, SPART *inner_limit, SPART *inner_offset, SPART *outer_limit)
{
  if (NULL == outer_limit)
    return inner_limit;
  if ((DV_LONG_INT == DV_TYPE_OF (outer_limit)) && (DV_LONG_INT == DV_TYPE_OF (inner_offset)) && (DV_LONG_INT == DV_TYPE_OF (inner_limit)))
    {
      boxint val = unbox ((caddr_t)outer_limit) + unbox ((caddr_t)inner_offset);
      return (unbox ((caddr_t)inner_limit) < val) ? inner_limit : (SPART *)t_box_num_nonull (val);
    }
  return spar_make_funcall (sparp, 0, "bif:__min_notnull", (SPART **)t_list (3,
      inner_limit,
      spartlist (sparp, 3, BOP_PLUS, outer_limit,
        spar_make_funcall (sparp, 0, "bif:__max_notnull", (SPART **)t_list (2, inner_offset, t_box_num_nonull (0))) ),
      t_box_num_nonull (0) ) );
}

void
spar_propagate_limit_as_option (sparp_t *sparp, SPART *tree, SPART *outer_limit)
{
  switch (SPART_TYPE (tree))
    {
    case SPAR_REQ_TOP:
      tree->_.req_top.limit = sparp_cut_inner_limit_with_outer_limit (sparp, tree->_.req_top.limit, tree->_.req_top.offset, outer_limit);
      if ((NULL != tree->_.req_top.limit) && (DISTINCT_L != tree->_.req_top.subtype) && (NULL == tree->_.req_top.groupings) && (NULL == tree->_.req_top.having))
        spar_propagate_limit_as_option (sparp, tree->_.req_top.pattern,
          sparp_limit_for_cutting_inner_limits (sparp, tree->_.req_top.limit, tree->_.req_top.offset) );
      return;
    case SPAR_GP:
      {
        int eq_ctr, filt_eq_mask;
        if (NULL != tree->_.gp.options)
          {
            SPART *lim = sparp_get_option (sparp, tree->_.gp.options, LIMIT_L);
            if (NULL != lim)
              {
                outer_limit = sparp_cut_inner_limit_with_outer_limit (sparp, lim, NULL, outer_limit);
                if (NULL != outer_limit)
                  sparp_set_option (sparp, &(tree->_.gp.options), LIMIT_L, outer_limit, SPARP_SET_OPTION_REPLACING);
              }
          }
        if (NULL == outer_limit)
          return;
        if (0 != BOX_ELEMENTS_0 (tree->_.gp.filters))
          return;
        filt_eq_mask = 0;
        SPARP_FOREACH_GP_EQUIV (sparp, tree, eq_ctr, eq)
          {
            if (eq->e_replaces_filter) return;
          }
        END_SPARP_FOREACH_GP_EQUIV;
        switch (tree->_.gp.subtype)
          {
          case SELECT_L:
            spar_propagate_limit_as_option (sparp, tree->_.gp.subquery, outer_limit);
            return;
          case UNION_L:
            {
              int memb_ctr;
              DO_BOX_FAST (SPART *, memb, memb_ctr, tree->_.gp.members)
                {
                  spar_propagate_limit_as_option (sparp, memb, outer_limit);
                }
              END_DO_BOX_FAST;
              if (NULL != outer_limit)
                sparp_set_option (sparp, &(tree->_.gp.options), LIMIT_L, outer_limit, SPARP_SET_OPTION_REPLACING);
              return;
            }
          case SERVICE_L:
            if (NULL != outer_limit)
              sparp_set_option (sparp, &(tree->_.gp.options), LIMIT_L, outer_limit, SPARP_SET_OPTION_REPLACING);
            if (DV_LONG_INT != DV_TYPE_OF (outer_limit))
              return;
            /* no break */
          case 0: case WHERE_L:
            {
              int memb_ctr, memb_count = BOX_ELEMENTS_0 (tree->_.gp.members);
              int nonoptional_ctr = 0;
              if (0 == memb_count)
                return;
              for (memb_ctr = 0; memb_ctr < memb_count; memb_ctr++)
                {
                  SPART *memb = tree->_.gp.members[memb_ctr];
                  if ((SPAR_GP != SPART_TYPE (memb)) || (OPTIONAL_L != memb->_.gp.subtype))
                    nonoptional_ctr++;
                }
              if (1 == nonoptional_ctr)
                {
                  for (memb_ctr = 0; memb_ctr < memb_count; memb_ctr++)
                    {
                      SPART *memb = tree->_.gp.members[memb_ctr];
                      spar_propagate_limit_as_option (sparp, memb, outer_limit);
                    }
                }
              if ((0 == tree->_.gp.subtype) && (NULL != outer_limit))
                sparp_set_option (sparp, &(tree->_.gp.options), LIMIT_L, outer_limit, SPARP_SET_OPTION_REPLACING);
              return;
            }
          }
        return;
      }
    case SPAR_TRIPLE:
      {
        if (NULL == outer_limit)
          return;
        sparp_set_option (sparp, &(tree->_.triple.options), LIMIT_L, outer_limit, SPARP_SET_OPTION_REPLACING);
        return;
      }
    }
}

void
sparp_rewrite_qm_postopt (sparp_t *sparp)
{
  sparp_equiv_t **equivs;
  int equiv_ctr, equiv_count;
  int retval_ctr;
  dk_set_t optionals_to_reduce;
  SPART *root = sparp->sparp_expr;
  sparp_gp_trav_cbk_t *security_cbk;
  if (SPAR_CODEGEN == SPART_TYPE (sparp->sparp_expr))
    GPF_T1 ("sparp_" "rewrite_qm_postopt () for CODEGEN");
  if (SPAR_QM_SQL_FUNCALL == SPART_TYPE (sparp->sparp_expr))
    GPF_T1 ("sparp_" "rewrite_qm_postopt () for SQL_FUNCALL");
  sparp_wpar_retvars_in_max (sparp, sparp->sparp_expr);

retry_after_reducing_optionals:
  optionals_to_reduce = NULL;
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, &optionals_to_reduce,
    sparp_gp_trav_rewrite_qm_postopt, sparp_gp_trav_flatten_and_reuse_tabids,
    NULL, NULL, sparp_gp_trav_rewrite_qm_postopt,
    NULL );
  while (NULL != optionals_to_reduce)
    {
      SPART *opt = t_set_pop (&optionals_to_reduce);
      if (sparp_reduce_trivial_optional (sparp, opt))
        {
          sparp_rewrite_qm_optloop (sparp, 1);
          goto retry_after_reducing_optionals; /* see above */
        }
    }
  sparp_remove_redundant_connections (sparp, SPARP_UNLINK_IF_ASSIGNED_EXTERNALLY);
  sparp_audit_mem (sparp);
  sparp_trav_out_clauses (sparp, sparp->sparp_expr, NULL,
    NULL, NULL,
    NULL, NULL, sparp_gp_trav_rewrite_qm_postopt,
    NULL );
  security_cbk = ((spar_graph_static_perms (sparp, NULL, RDF_GRAPH_PERM_READ) & RDF_GRAPH_PERM_READ) ? NULL :
    sparp_gp_trav_add_graph_perm_read_filters );
  sparp_gp_trav (sparp, sparp->sparp_expr->_.req_top.pattern, NULL,
    sparp_gp_trav_restore_filters_for_weird_subq, security_cbk,
    NULL, NULL, NULL,
    NULL );
/* Final processing: */
  switch (root->_.req_top.subtype)
    {
    case DELETE_L:
      if (spar_optimize_delete_of_single_triple_pattern (sparp, root))
        break; /* If optimized then there's nothing more to optimize. */
      /* no break here */
    case INSERT_L:
      spar_optimize_retvals_of_insert_or_delete (sparp, root);
      break;
    case MODIFY_L:
      spar_optimize_retvals_of_modify (sparp, root);
      break;
    }
  spar_propagate_limit_as_option (sparp, root, NULL);
  if (NULL != sparp->sparp_parent_sparp)
    goto end_of_equiv_checks; /* see below */
  equivs = sparp->sparp_sg->sg_equivs;
  equiv_count = sparp->sparp_sg->sg_equiv_count;
  for (equiv_ctr = equiv_count; equiv_ctr--; /* no step */)
    {
      sparp_equiv_t *eq = equivs[equiv_ctr];
      if (NULL == eq)
        continue;
      if (eq->e_deprecated)
        continue;
      if (!SPARP_EQ_IS_ASSIGNED_LOCALLY(eq) &&
	((0 != eq->e_const_reads) || (0 != eq->e_replaces_filter) || /* note: no check for (0 != eq->e_optional_reads) */
          (0 != BOX_ELEMENTS_0 (eq->e_receiver_idxs)) ) &&
        !(eq->e_rvr.rvrRestrictions & (SPART_VARR_FIXED | SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL)) )
        {
          if ((1 == eq->e_var_count) && SPART_VARNAME_IS_SPECIAL (eq->e_varnames[0]))
            continue; /* Special variable can not be bound in SPARQL because they does not exist in SPARQL. Thus no restricitons can be ifrerred from SPARQL context. */
          if (!sparp->sparp_env->spare_signal_void_variables)
            eq->e_rvr.rvrRestrictions |= SPART_VARR_CONFLICT;
          else if (eq->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED)
            spar_error (sparp, "Variable '%.100s' can not be bound due to mutually exclusive restrictions on its value", eq->e_varnames[0]);
          else
            spar_error (sparp, "Variable '%.100s' is used in subexpressions of the query but can not be assigned", eq->e_varnames[0]);
        }
#ifdef DEBUG
      equivs[equiv_ctr]->e_dbg_saved_gp = (SPART **)t_box_num ((ptrlong)(equivs[equiv_ctr]->e_gp));
#endif
/*
      equivs[equiv_ctr]->e_gp = NULL;
*/
    }

end_of_equiv_checks:
  DO_BOX_FAST (SPART *, retval, retval_ctr, root->_.req_top.retvals)
    {
      caddr_t name;
      int retval_type = SPART_TYPE (retval);
      if (SPAR_VARIABLE == retval_type)
        name = retval->_.var.vname;
      else if (SPAR_ALIAS == retval_type)
        name = retval->_.alias.aname;
      else
        continue;
      if (!strchr (name, '>'))
        continue;
      DO_SET (spar_propvariable_t *, pv, &(sparp->sparp_propvars))
        {
          const char *optext;
          if (strcmp (pv->sparpv_obj_var_name, name))
            continue;
          optext = ((_PLUS_GT == pv->sparpv_op) ? "+>" : "*>");
          if (pv->sparpv_obj_altered)
            spar_error (sparp, "The name of a result-set column for variable ?%.200s %s %s?%.200s%s %s; please specify some alias.",
              pv->sparpv_subj_var->_.var.vname, optext,
              ((Q_IRI_REF == pv->sparpv_verb_lexem_type) ? "<" : ""), pv->sparpv_verb_lexem_text,
              ((Q_IRI_REF == pv->sparpv_verb_lexem_type) ? ">" : ""),
              ((pv->sparpv_obj_altered & 0x2) ? "may cause subtle coding errors" : "is too long") );
          break;
        }
      END_DO_SET();
    }
  END_DO_BOX_FAST;
  if ((NULL == sparp->sparp_parent_sparp) && sparp->sparp_query_uses_sinvs)
    sparp_fill_sinv_varlists (sparp, root); /* This is global so can (and should) be made only at top level */
}


void
sparp_rewrite_grab (sparp_t *sparp)
{
  sparp_env_t *env = sparp->sparp_env;
  rdf_grab_config_t *rgc = &(env->spare_src.ssrc_grab);
  sparp_t *sparp_of_seed;	/* This will compile the statement that will collect the first set of graphs */
  sparp_t *sparp_of_iter;	/* This will compile the statement that will called while the set of graphs growth */
  sparp_t *sparp_of_final;	/* This will compile the statement that will produce the final result set */
  sparp_t *sub_sparps[3];
  caddr_t sql_texts[3];
  SPART **grab_retvals;
  caddr_t retselid;
  ptrlong top_subtype;
  dk_set_t new_vars = NULL;
  dk_set_t sa_graphs = NULL;
  sql_comp_t sc;
  int sub_sparp_ctr;
  ptrlong rgc_flags = 0;
  int use_plain_return;
  retselid = sparp->sparp_expr->_.req_top.retselid;
  top_subtype = sparp->sparp_expr->_.req_top.subtype;
  use_plain_return = (((CONSTRUCT_L == top_subtype) || (DESCRIBE_L == top_subtype)) ? 1 : 0);
  t_set_push (&(env->spare_selids), retselid);
  DO_SET (caddr_t, grab_name, &(rgc->rgc_vars))
    {
      t_set_push (&new_vars, spar_make_variable (sparp, grab_name));
    }
  END_DO_SET()
  if (NULL != rgc->rgc_destination)
    t_set_push (&sa_graphs, spar_make_qm_sql (sparp, "iri_to_id", (SPART **)t_list (1, rgc->rgc_destination), NULL));
  if (NULL != rgc->rgc_group_destination)
    t_set_push (&sa_graphs, spar_make_qm_sql (sparp, "iri_to_id", (SPART **)t_list (1, rgc->rgc_group_destination), NULL));
  t_set_pop (&(env->spare_selids));
  if (NULL != new_vars)
    grab_retvals = (SPART **)t_revlist_to_array (new_vars);
  else
    grab_retvals = sparp_treelist_full_copy (sparp, sparp->sparp_expr->_.req_top.expanded_orig_retvals, NULL);
/* Making subqueries: seed */
  sub_sparps[0] = sparp_of_seed = sparp_clone_for_variant (sparp, 0);
  sparp_of_seed->sparp_expr = sparp_tree_full_copy (sparp_of_seed, sparp->sparp_expr, NULL);
  sparp_of_seed->sparp_expr->_.req_top.shared_spare = sparp_of_seed->sparp_env;
  sparp_of_seed->sparp_expr->_.req_top.subtype = SELECT_L;
  sparp_of_seed->sparp_expr->_.req_top.retvals = grab_retvals;
  sparp_of_seed->sparp_expr->_.req_top.retvalmode_name = t_box_string ("LONG");
  sparp_of_seed->sparp_expr->_.req_top.limit = NULL;
  sparp_of_seed->sparp_expr->_.req_top.offset = 0;
  sparp_of_seed->sparp_env->spare_globals_mode = SPARE_GLOBALS_ARE_COLONUMBERED;
  sparp_of_seed->sparp_env->spare_global_num_offset = 1;
  sparp_of_seed->sparp_env->spare_src.ssrc_grab.rgc_sa_graphs = env->spare_src.ssrc_grab.rgc_sa_graphs;
  sparp_of_seed->sparp_env->spare_src.ssrc_grab.rgc_sa_preds = env->spare_src.ssrc_grab.rgc_sa_preds;
  sparp_of_seed->sparp_env->spare_src.ssrc_grab.rgc_sa_vars = env->spare_src.ssrc_grab.rgc_sa_vars;
  sparp_of_seed->sparp_env->spare_src.ssrc_grab.rgc_vars = env->spare_src.ssrc_grab.rgc_vars;
/* Making subqueries: iter */
  sub_sparps[1] = sparp_of_iter = sparp_clone_for_variant (sparp_of_seed, 0);
  sparp_of_iter->sparp_expr = sparp_tree_full_copy (sparp_of_seed, sparp_of_seed->sparp_expr, NULL);
  sparp_of_iter->sparp_expr->_.req_top.shared_spare = sparp_of_iter->sparp_env;
  if (NULL != sparp_of_iter->sparp_expr->_.req_top.order)
    sparp_tweak_order_of_iter (sparp_of_iter, sparp_of_iter->sparp_expr->_.req_top.order);
  sparp_of_iter->sparp_env->spare_globals_mode = SPARE_GLOBALS_ARE_COLONUMBERED;
  sparp_of_iter->sparp_env->spare_global_num_offset = 1;
  sparp_of_iter->sparp_env->spare_src.ssrc_grab.rgc_sa_graphs = env->spare_src.ssrc_grab.rgc_sa_graphs;
  sparp_of_iter->sparp_env->spare_src.ssrc_grab.rgc_sa_preds = env->spare_src.ssrc_grab.rgc_sa_preds;
  sparp_of_iter->sparp_env->spare_src.ssrc_grab.rgc_sa_vars = env->spare_src.ssrc_grab.rgc_sa_vars;
  sparp_of_iter->sparp_env->spare_src.ssrc_grab.rgc_vars = env->spare_src.ssrc_grab.rgc_vars;
/* Only after making the iter subquery from the seed one, seed may loose its ORDER BY */
  sparp_of_seed->sparp_expr->_.req_top.order = NULL;
/*!!! TBD: relax graph conditions in sparp_of_iter */
/* Making subqueries: final */
  sub_sparps[2] = sparp_of_final = sparp_clone_for_variant (sparp, 1);
  sparp_of_final->sparp_expr = sparp_tree_full_copy (sparp_of_seed, sparp->sparp_expr, NULL);
  sparp_of_final->sparp_expr->_.req_top.shared_spare = sparp_of_final->sparp_env;
  sparp_of_final->sparp_env->spare_globals_mode = SPARE_GLOBALS_ARE_COLONUMBERED;
  sparp_of_final->sparp_env->spare_global_num_offset = 0;
/*!!! TBD: relax graph conditions in sparp_of_final */
  for (sub_sparp_ctr = 3; sub_sparp_ctr--; /* no step */)
    {
      spar_sqlgen_t ssg;
      sparp_t *sub_sparp = sub_sparps [sub_sparp_ctr];
      sparp_rewrite_qm (sub_sparp);
      memset (&ssg, 0, sizeof (spar_sqlgen_t));
      memset (&sc, 0, sizeof (sql_comp_t));
      sc.sc_client = sub_sparp->sparp_sparqre->sparqre_cli;
      ssg.ssg_out = strses_allocate ();
      ssg.ssg_sc = &sc;
      ssg.ssg_sparp = sub_sparp;
      ssg.ssg_tree = sub_sparp->sparp_expr;
      ssg.ssg_sources = ssg.ssg_tree->_.req_top.sources; /*!!!TBD merge with environment */
      ssg.ssg_seealso_enabled = (sub_sparp_ctr < 2) ? 1 : 0;
      ssg_make_sql_query_text (&ssg);
      ssg.ssg_seealso_enabled = 0;
      sql_texts [sub_sparp_ctr] = t_strses_string (ssg.ssg_out);
      ssg_free_internals (&ssg);
    }
  if (rgc->rgc_intermediate)
    rgc_flags |= 0x0001;
  sparp->sparp_expr = spartlist (sparp, 21, SPAR_CODEGEN, /* #0 */
    t_box_num ((ptrlong)(ssg_grabber_codegen)),
    sparp_treelist_full_copy (sparp, sparp->sparp_expr->_.req_top.retvals, NULL),	/* #2 */
    t_box_dv_short_string ("sql:RDF_GRAB"),	/* #3 */
    sql_texts[0], sql_texts[1], sql_texts[2], /* #4-#6 */
    sparp_tree_full_copy (sparp, sparp->sparp_expr->_.req_top.limit, NULL),	/* #7 */
    ((NULL == rgc->rgc_consts) ? NULL :
      spar_make_vector_qm_sql (sparp, (SPART **)(t_revlist_to_array (rgc->rgc_consts))) ), /* #8 */
    ((NULL == sa_graphs) ? NULL :
      spar_make_vector_qm_sql (sparp, (SPART **)(t_revlist_to_array (sa_graphs))) ), /* #9 */
    ((NULL == rgc->rgc_sa_preds) ? NULL :
      spar_make_vector_qm_sql (sparp, (SPART **)(t_revlist_to_array (rgc->rgc_sa_preds))) ), /* #10 */
    t_box_copy (rgc->rgc_depth), t_box_copy (rgc->rgc_limit), /* #11-#12 */
    t_box_copy (rgc->rgc_base),	/* #13 */
    t_box_copy (rgc->rgc_destination), t_box_copy (rgc->rgc_group_destination),	/* #14-#15 */
    t_box_copy (rgc->rgc_resolver_name), t_box_copy (rgc->rgc_loader_name),	/* #16-#17 */
    /* no copy here, pass by ref */ sparp->sparp_env->spare_sql_refresh_free_text, /* #18 */
    (ptrlong)use_plain_return,	/* #19 */
    t_box_num (rgc_flags) );	/* #20 */
    /* Note that the uid is not in the list of codegen arguments! */
}

void
ssg_grabber_codegen (struct spar_sqlgen_s *ssg, struct spar_tree_s *spart, ...)
{
  int argctr = 0;
/* The order of declarations is important: side effect on init */
  SPART **retvals		= (SPART **)(spart->_.codegen.args [argctr++]);	/* #2 */
  caddr_t procedure_name	= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #3 */
  caddr_t seed_sql_text		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #4 */
  caddr_t iter_sql_text		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #5 */
  caddr_t final_sql_text	= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #6 */
  caddr_t ret_limit		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #7 */
  SPART *const_vector_expn	= (SPART *)(spart->_.codegen.args [argctr++]);	/* #8 */
  SPART *sa_graphs_vector_expn	= (SPART *)(spart->_.codegen.args [argctr++]);	/* #9 */
  SPART *sa_preds_vector_expn	= (SPART *)(spart->_.codegen.args [argctr++]);	/* #10 */
  caddr_t depth			= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #11 */
  caddr_t grab_limit		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #12 */
  caddr_t base			= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #13 */
  caddr_t destination		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #14 */
  caddr_t group_destination	= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #15 */
  caddr_t resolver_name		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #16 */
  caddr_t loader_name		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #17 */
  caddr_t refresh_free_text	= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #18 */
  int use_plain_return		= (ptrlong)(spart->_.codegen.args [argctr++]);	/* #19 */
  caddr_t rgc_flags		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #20 */
  int varctr, varcount = BOX_ELEMENTS (retvals);
  int need_comma;
  caddr_t call_alias = t_box_sprintf (0x100, "grabber-t%d", ssg->ssg_sparp->sparp_key_gen);
  ssg->ssg_sparp->sparp_key_gen += 1;
  if (NULL == const_vector_expn)
    const_vector_expn = (SPART *)t_NEW_DB_NULL;
  if (NULL == sa_graphs_vector_expn)
    sa_graphs_vector_expn = (SPART *)t_NEW_DB_NULL;
  if (NULL == sa_preds_vector_expn)
    sa_preds_vector_expn = (SPART *)t_NEW_DB_NULL;
  if (NULL == depth)
    depth = (caddr_t)1L;
  if (NULL == grab_limit)
    grab_limit = t_box_num (MAX_BOX_ELEMENTS);
  if (NULL == base)
    base = t_NEW_DB_NULL;
  if (NULL == destination)
    destination = t_NEW_DB_NULL;
  if (NULL == group_destination)
    group_destination = t_NEW_DB_NULL;
  if (NULL == resolver_name)
    resolver_name = t_box_dv_short_string ("DB.DBA.RDF_GRAB_RESOLVER_DEFAULT");
  if (NULL == loader_name)
    loader_name = t_box_dv_short_string ("DB.DBA.RDF_SPONGE_UP");
  if (use_plain_return)
    {
      ssg_puts ("SELECT TOP 1 ");
      ssg_prin_function_name (ssg, procedure_name);
      ssg_puts (" (");
      ssg->ssg_indent += 1;
    }
  else
    {
      ssg_puts ("SELECT ");
      if (0 == varcount) ssg_puts ("TOP 1 1 as __ask_retval "); /* IvAn/Bug12961/071109 Added special case for ASK */
      for (varctr = 0; varctr < varcount; varctr++)
        {
          char buf[30];
          char *asname;
          if (varctr)
            ssg_puts (", ");
          ssg_prin_id (ssg, call_alias);
          sprintf (buf, ".rset[%d] as ", varctr);
          ssg_puts (buf);
          asname = spar_alias_name_of_ret_column (retvals [varctr]);
          if (NULL == asname)
            {
              sprintf (buf, "callret-%d", varctr);
              asname = buf;
            }
          ssg_prin_id (ssg, asname);
        }
      ssg_newline (0);
      ssg_puts ("FROM ");
      ssg_prin_function_name (ssg, procedure_name);
      ssg_puts (" (_grabber_params, _grabber_seed, _grabber_iter, _grabber_final, _grabber_ret_limit, _grabber_consts, _grabber_sa_graphs, _grabber_sa_preds, _grabber_depth, _grabber_doc_limit, _grabber_base, _grabber_destination, _grabber_group_destination, _grabber_resolver, _grabber_loader, _refresh_free_text, _plain_ret, _grabber_flags, _uid) (rset any) ");
      ssg_prin_id (ssg, call_alias);
      ssg_newline (0);
      ssg_puts ("WHERE _grabber_params = ");
    }
  need_comma = 0;
  ssg_puts ("vector (");
  DO_SET (caddr_t, vname, &(ssg->ssg_sparp->sparp_env->spare_global_var_names))
    {
      if ('@' == vname[1])
        continue;
      if (need_comma)
        ssg_puts (", ");
      else
        need_comma = 1;
      ssg_print_global_param_name (ssg, vname);
    }
  END_DO_SET ();
  ssg_puts (")");
#define PROC_PARAM_EQ_SPART(txt,var) do { \
    if (use_plain_return) \
      { \
        ssg_puts (", "); \
        ssg_newline (0); \
      } \
    else \
      { \
        ssg_newline (0); \
        ssg_puts (" AND "); \
        ssg_puts (txt); \
        ssg_puts (" = "); \
      } \
    ssg_print_scalar_expn (ssg, (SPART *)(var), SSG_VALMODE_SQLVAL, NULL_ASNAME); \
    } while (0)
  PROC_PARAM_EQ_SPART ("_grabber_seed", seed_sql_text);
  PROC_PARAM_EQ_SPART ("_grabber_iter", iter_sql_text);
  PROC_PARAM_EQ_SPART ("_grabber_final", final_sql_text);
  PROC_PARAM_EQ_SPART ("_grabber_ret_limit", ret_limit);
  PROC_PARAM_EQ_SPART ("_grabber_consts", const_vector_expn);
  PROC_PARAM_EQ_SPART ("_grabber_sa_graphs", sa_graphs_vector_expn);
  PROC_PARAM_EQ_SPART ("_grabber_sa_preds", sa_preds_vector_expn);
  PROC_PARAM_EQ_SPART ("_grabber_depth", depth);
  PROC_PARAM_EQ_SPART ("_grabber_doc_limit", grab_limit);
  PROC_PARAM_EQ_SPART ("_grabber_base", base);
  PROC_PARAM_EQ_SPART ("_grabber_destination", destination);
  PROC_PARAM_EQ_SPART ("_grabber_group_destination", group_destination);
  PROC_PARAM_EQ_SPART ("_grabber_resolver", resolver_name);
  PROC_PARAM_EQ_SPART ("_grabber_loader", loader_name);
  PROC_PARAM_EQ_SPART ("_refresh_free_text", refresh_free_text);
  PROC_PARAM_EQ_SPART ("_plain_ret", ((ptrlong) use_plain_return));
  PROC_PARAM_EQ_SPART ("_grabber_flags", rgc_flags);
  PROC_PARAM_EQ_SPART ("_uid", spar_exec_uid_and_gs_cbk (ssg->ssg_sparp)); /* uid is not in the list of passed arguments! */
#undef PROC_PARAM_EQ_SPART
  if (use_plain_return)
    {
      ssg_putchar (')');
      ssg->ssg_indent -= 1;
      ssg_newline (0);
      ssg_puts ("FROM DB.DBA.RDF_QUAD ");
      ssg_prin_id (ssg, call_alias);
    }
}

void
ssg_select_known_graphs_codegen (struct spar_sqlgen_s *ssg, struct spar_tree_s *spart, ...)
{
  int argctr = 0;
/* The order of declarations is important: side effect on init */
  caddr_t valmode_name		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #2 */
  caddr_t formatmode_name	= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #3 */
  caddr_t retname		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #4 */
  caddr_t retselid		= (caddr_t)(spart->_.codegen.args [argctr++]);	/* #5 */
  SPART *lim_expn		= spart->_.codegen.args [argctr++];	/* #6 */
  SPART *ofs_expn		= spart->_.codegen.args [argctr++];	/* #7 */
  SPART	*tree = ssg->ssg_tree;
  ptrlong subtype = tree->_.req_top.subtype;
  const char *formatter, *agg_formatter, *agg_meta;
  ssg_valmode_t retvalmode;
  ssg_find_formatter_by_name_and_subtype (formatmode_name, SELECT_L, &formatter, &agg_formatter, &agg_meta);
  if (COUNT_DISTINCT_L == subtype)
    retvalmode = SSG_VALMODE_SQLVAL;
  else
    retvalmode = ssg_find_valmode_by_name (valmode_name);
  if (((NULL != formatter) || (NULL != agg_formatter)) && (NULL != retvalmode) && (SSG_VALMODE_LONG != retvalmode))
    spar_sqlprint_error ("'output:valmode' declaration conflicts with 'output:format'");
  if (NULL == retvalmode)
    retvalmode = ((NULL != formatter) ? SSG_VALMODE_LONG : SSG_VALMODE_SQLVAL);
  if (NULL != formatter)
    {
      ssg_puts ("SELECT "); ssg_puts (formatter); ssg_puts (" (");
      ssg_puts ("vector (");
      ssg_prin_id (ssg, retselid); ssg_putchar ('.'); ssg_prin_id (ssg, retname);
      ssg_puts ("), vector (");
      ssg_print_box_as_sql_atom (ssg, retname, SQL_ATOM_NARROW_ONLY /*???*/);
      ssg_puts (")) AS \"callret-0\" LONG VARCHAR\nFROM (");
      ssg->ssg_indent += 1;
      ssg_newline (0);
    }
  else if (NULL != agg_formatter)
    {
      ssg_puts ("SELECT "); ssg_puts (agg_formatter); ssg_puts (" (");
      if (NULL != agg_meta)
        {
          ssg_puts (agg_meta); ssg_puts (" (");
        }
      ssg_puts ("vector (");
      ssg_print_box_as_sql_atom (ssg, retname, SQL_ATOM_NARROW_ONLY /*???*/);
      if (NULL != agg_meta)
        {
          ssg_puts ("), '");
          ssg_puts (strchr (tree->_.req_top.formatmode_name, ' ')+1);
          ssg_putchar ('\'');
        }
      ssg_puts ("), vector ( __box_flags_tweak (");
      ssg_prin_id (ssg, retselid); ssg_putchar ('.'); ssg_prin_id (ssg, retname);
      ssg_puts (", 1))) AS \"aggret-0\" INTEGER FROM (");
      ssg->ssg_indent += 1;
      ssg_newline (0);
    }
  ssg_puts ("SELECT");
  if ((NULL != lim_expn) || (NULL != ofs_expn))
    ssg_print_limofs_expn (ssg, lim_expn, ofs_expn);
  if (SSG_VALMODE_LONG != retvalmode)
    ssg_puts (" __box_flags_tweak (");
  ssg_prin_id_with_suffix (ssg, retselid, "~pview");
  ssg_puts (".g_enum");
  if (SSG_VALMODE_LONG != retvalmode)
    ssg_puts (", 1)");
  ssg_puts (" AS ");
  ssg_prin_id (ssg, retname);
  ssg_puts (" FROM DB.DBA.SPARQL_SELECT_KNOWN_GRAPHS(return_iris, lim) ");
  ssg_puts ((SSG_VALMODE_LONG == retvalmode) ? "(g_enum IRI_ID) " : "(g_enum varchar) ");
  ssg_prin_id_with_suffix (ssg, retselid, "~pview");
  ssg_puts (" WHERE ");
  ssg_prin_id_with_suffix (ssg, retselid, "~pview");
  ssg_puts ((SSG_VALMODE_LONG == retvalmode) ? ".return_iris=0 " : ".return_iris=1 ");
  ssg_puts (" AND ");
  ssg_prin_id_with_suffix (ssg, retselid, "~pview");
  ssg_puts (".lim=");
  if (NULL != lim_expn)
    ssg_print_scalar_expn (ssg, spartlist (ssg->ssg_sparp, 3, BOP_PLUS, lim_expn, ofs_expn), SSG_VALMODE_SQLVAL, NULL_ASNAME);
  else
    ssg_puts ("NULL");
  if ((NULL != formatter) || (NULL != agg_formatter))
    {
      ssg_puts (") AS ");
      ssg_prin_id (ssg, retselid);
      ssg->ssg_indent--;
    }
}