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
|
/*
* $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 "sparql.h"
#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"
#ifdef DEBUG
#if 0
#define sparp_check_tree(t) t_check_tree_heads((t), ((caddr_t *)(&(((SPART *)NULL)->_.triple.tc_list)))-((caddr_t *)NULL))
#else
#define sparp_check_tree(t) t_check_tree(t)
#endif
#else
#define sparp_check_tree(t)
#endif
#ifndef NDEBUG
SPART **
t_spartlist_concat (SPART **list1, SPART **list2)
{
return (SPART **)t_list_concat ((caddr_t)list1, (caddr_t)list2);
}
#endif
caddr_t *
t_modify_list (caddr_t *lst, int edit_idx, int delete_len, caddr_t *ins, int ins_len)
{
int lst_len = BOX_ELEMENTS_0 ((caddr_t)(lst));
caddr_t *res;
t_check_tree (lst);
if ((0 > edit_idx) || (0 > delete_len) || (edit_idx + delete_len > lst_len))
GPF_T1 ("t_modify_list(): bad range");
if ((ins + ins_len > lst) && (ins < lst+lst_len))
GPF_T1 ("t_modify_list(): overlapping arrays");
if (delete_len == ins_len)
{
memcpy (lst + edit_idx, ins, ins_len * sizeof (caddr_t));
t_check_tree (lst);
return lst;
}
res = (caddr_t *)t_alloc_box ((lst_len - delete_len + ins_len) * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
if (edit_idx)
memcpy (res, lst, edit_idx * sizeof (caddr_t));
if (ins_len)
memcpy (res + edit_idx, ins, ins_len * sizeof (caddr_t));
memcpy (res + edit_idx + ins_len, lst + edit_idx + delete_len, (lst_len - (edit_idx + delete_len)) * sizeof (caddr_t));
t_check_tree (res);
return res;
}
#ifndef NDEBUG
#define t_modify_spartlist(lst,edit_idx,delete_len,ins,ins_len) ((SPART **)t_modify_list ((caddr_t *)lst, edit_idx, delete_len, (caddr_t *)ins, ins_len))
#else
SPART **
t_modify_spartlist (SPART **lst, int edit_idx, int delete_len, SPART **ins, int ins_len)
{
return (SPART **)t_modify_list ((caddr_t *)lst, edit_idx, delete_len, (caddr_t *)ins, ins_len);
}
#endif
/* ROUTINES FOR SPART TREE TRAVERSAL */
int
sparp_gp_trav_int (sparp_t *sparp, SPART *tree,
sparp_trav_state_t *sts_this, void *common_env,
sparp_gp_trav_cbk_t *gp_in_cbk, sparp_gp_trav_cbk_t *gp_out_cbk,
sparp_gp_trav_cbk_t *expn_in_cbk, sparp_gp_trav_cbk_t *expn_out_cbk, sparp_gp_trav_cbk_t *expn_subq_cbk,
sparp_gp_trav_cbk_t *literal_cbk
)
{
SPART **sub_gps = NULL;
SPART **sub_expns = NULL;
SPART *fields[2];
int tree_type;
int sub_gp_count = 0, sub_expn_count = 0, ctr;
int tree_cat = 0;
int in_rescan = 0;
sparp_trav_state_t *save_sts_this = (sparp_trav_state_t *)(BADBEEF_BOX); /* To keep gcc 4.0 happy */
SPART *save_ancestor_gp = (SPART *)(BADBEEF_BOX); /* To keep gcc 4.0 happy */
int retcode = 0;
if (THR_IS_STACK_OVERFLOW (THREAD_CURRENT_THREAD, &sub_gps, 1000))
spar_internal_error (NULL, "sparp_gp_trav_int(): stack overflow");
if (sts_this == (sparp->sparp_stss + SPARP_MAX_SYNTDEPTH))
spar_error (sparp, "The nesting depth of subexpressions exceed limits of SPARQL compiler");
scan_for_children:
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (tree))
{
tree_type = SPAR_LIT;
tree_cat = 2;
goto cat_recognized;
}
tree_type = tree->type;
switch (tree_type)
{
case SPAR_LIT:
case SPAR_QNAME:
/*case SPAR_QNAME_NS:*/
{
tree_cat = 2;
break;
}
case SPAR_ALIAS:
{
tree_cat = 1;
sub_expns = &(tree->_.alias.arg);
sub_expn_count = 1;
break;
}
case SPAR_BLANK_NODE_LABEL:
case SPAR_VARIABLE:
{
tree_cat = 1;
break;
}
case SPAR_BUILT_IN_CALL:
{
tree_cat = 1;
sub_expns = tree->_.builtin.args;
sub_expn_count = BOX_ELEMENTS_0 (sub_expns);
break;
}
case SPAR_FUNCALL:
{
tree_cat = 1;
sub_expns = tree->_.funcall.argtrees;
sub_expn_count = BOX_ELEMENTS (sub_expns);
break;
}
case SPAR_GP:
{
tree_cat = 0;
sub_gps = tree->_.gp.members;
sub_gp_count = BOX_ELEMENTS (sub_gps);
sub_expns = tree->_.gp.filters;
sub_expn_count = BOX_ELEMENTS (sub_expns);
break;
}
case SPAR_REQ_TOP:
spar_internal_error (sparp, "TOP of request as subexpression");
case SPAR_TRIPLE:
{
tree_cat = 0;
sub_expns = tree->_.triple.tr_fields;
sub_expn_count = SPART_TRIPLE_FIELDS_COUNT;
break;
}
case SPAR_LIST:
{
sub_expns = tree->_.list.items;
sub_expn_count = BOX_ELEMENTS (sub_expns);
break;
}
case BOP_EQ: case SPAR_BOP_EQ: case BOP_NEQ:
case BOP_LT: case BOP_LTE: case BOP_GT: case BOP_GTE:
/*case BOP_LIKE: Like is built-in in SPARQL, not a BOP! BTW, 'IN' is also BOP */
case BOP_SAME: case BOP_NSAME:
case BOP_PLUS: case BOP_MINUS: case BOP_TIMES: case BOP_DIV: case BOP_MOD:
case BOP_AND: case BOP_OR:
{
tree_cat = 1;
sub_expns = fields;
sub_expn_count = 2;
fields[0] = tree->_.bin_exp.left;
fields[1] = tree->_.bin_exp.right;
break;
}
case BOP_NOT:
{
tree_cat = 1;
sub_expns = fields;
sub_expn_count = 1;
fields[0] = tree->_.bin_exp.left;
break;
}
case ORDER_L:
{
sub_expns = fields;
sub_expn_count = 1;
fields[0] = tree->_.oby.expn;
break;
}
default:
{
spar_internal_error (sparp, "Internal SPARQL compiler error: unsupported subexpression type");
break;
}
}
cat_recognized:
if (in_rescan)
goto process_children; /* See below */
switch (tree_cat)
{
case 0:
if (gp_in_cbk)
retcode = gp_in_cbk (sparp, tree, sts_this, common_env);
else
retcode = 0;
break;
case 1:
if (expn_in_cbk)
retcode = expn_in_cbk (sparp, tree, sts_this, common_env);
else
retcode = 0;
break;
case 2:
if (literal_cbk)
{
retcode = literal_cbk (sparp, tree, sts_this, common_env);
return retcode;
}
return 0;
default: GPF_T;
}
if (retcode & SPAR_GPT_COMPLETED)
return SPAR_GPT_COMPLETED;
save_sts_this = sts_this;
save_ancestor_gp = save_sts_this->sts_ancestor_gp;
if (retcode & SPAR_GPT_NODOWN)
goto end_process_children;
if (retcode & SPAR_GPT_ENV_PUSH)
{
sts_this++;
memset (sts_this, 0, sizeof (sparp_trav_state_t));
sts_this->sts_ancestor_gp = sts_this[-1].sts_ancestor_gp;
}
if (retcode & SPAR_GPT_RESCAN)
{
in_rescan = 1;
goto scan_for_children; /* see above */
}
process_children:
if (SPAR_GP == tree_type)
sts_this->sts_ancestor_gp = tree;
/*else
sts_this->sts_ancestor_gp = sts_this[-1].sts_ancestor_gp;*/
for (ctr = 0; ctr < sub_gp_count; ctr++)
{
sts_this->sts_parent = tree;
sts_this->sts_curr_array = sub_gps;
sts_this->sts_ofs_of_curr_in_array = ctr;
retcode = sparp_gp_trav_int (sparp, sub_gps[ctr], sts_this, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
if (retcode & SPAR_GPT_COMPLETED)
return SPAR_GPT_COMPLETED;
}
if (sub_expn_count && (
(NULL != expn_in_cbk) || (NULL != expn_out_cbk) || (NULL != expn_subq_cbk) ||
(NULL != literal_cbk) ) )
{
for (ctr = 0; ctr < sub_expn_count; ctr++)
{
SPART *sub_expn = sub_expns[ctr];
sts_this->sts_parent = tree;
sts_this->sts_curr_array = sub_expns;
sts_this->sts_ofs_of_curr_in_array = ctr;
if (SPAR_GP == SPART_TYPE (sub_expn))
{
if (NULL != expn_subq_cbk)
retcode = expn_subq_cbk (sparp, sub_expn, sts_this, common_env);
else
retcode = 0;
}
else
retcode = sparp_gp_trav_int (sparp, sub_expn, sts_this, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
if (retcode & SPAR_GPT_COMPLETED)
return SPAR_GPT_COMPLETED;
}
}
end_process_children:
save_sts_this->sts_ancestor_gp = save_ancestor_gp;
save_sts_this->sts_ancestor_gp = save_ancestor_gp;
if (retcode & SPAR_GPT_NOOUT)
return (retcode & SPAR_GPT_COMPLETED);
switch (tree_cat)
{
case 0:
if (gp_out_cbk)
retcode = gp_out_cbk (sparp, tree, save_sts_this, common_env);
else
retcode = 0;
#ifndef NDEBUG
if (retcode & SPAR_GPT_ENV_PUSH)
spar_internal_error (sparp, "SPAR_GPT_ENV_PUSH returned by gp_out_cbk");
#endif
break;
case 1:
if (expn_out_cbk)
retcode = expn_out_cbk (sparp, tree, save_sts_this, common_env);
else
retcode = 0;
#ifndef NDEBUG
if (retcode & SPAR_GPT_ENV_PUSH)
spar_internal_error (sparp, "SPAR_GPT_ENV_PUSH returned by expn_out_cbk");
#endif
break;
}
if (retcode & SPAR_GPT_COMPLETED)
return SPAR_GPT_COMPLETED;
return 0;
}
int
sparp_trav_out_clauses_int (sparp_t *sparp, SPART *req_top,
sparp_trav_state_t *sts_this, void *common_env,
sparp_gp_trav_cbk_t *gp_in_cbk, sparp_gp_trav_cbk_t *gp_out_cbk,
sparp_gp_trav_cbk_t *expn_in_cbk, sparp_gp_trav_cbk_t *expn_out_cbk, sparp_gp_trav_cbk_t *expn_subq_cbk,
sparp_gp_trav_cbk_t *literal_cbk
)
{
SPART **lists[4];
int list_ctr;
int retcode = 0;
if (SPAR_REQ_TOP != SPART_TYPE (req_top))
GPF_T1 ("sparp_" "trav_out_clauses_int(): bad req_top");
lists[0] = req_top->_.req_top.orig_retvals;
lists[1] = req_top->_.req_top.retvals;
lists[2] = req_top->_.req_top.groupings;
lists[3] = req_top->_.req_top.order;
sts_this->sts_parent = sts_this->sts_ancestor_gp = req_top->_.req_top.pattern;
for (list_ctr = 0; list_ctr <= 4; list_ctr++)
{
SPART **list = ((4 == list_ctr) ? &(req_top->_.req_top.having) : lists [list_ctr]);
int ctr, list_len = ((4 == list_ctr) ? 1 : BOX_ELEMENTS_0 (list));
for (ctr = 0; ctr < list_len; ctr++)
{
SPART *expn = list[ctr];
if (SPAR_GP == SPART_TYPE (expn))
retcode = ((NULL != expn_subq_cbk) ? expn_subq_cbk (sparp, expn, sts_this, common_env) : 0);
else retcode = sparp_gp_trav_int (sparp, expn, sts_this, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
if (retcode & SPAR_GPT_COMPLETED)
return retcode;
}
}
return 0;
}
int
sparp_gp_trav (sparp_t *sparp, SPART *tree, void *common_env,
sparp_gp_trav_cbk_t *gp_in_cbk, sparp_gp_trav_cbk_t *gp_out_cbk,
sparp_gp_trav_cbk_t *expn_in_cbk, sparp_gp_trav_cbk_t *expn_out_cbk, sparp_gp_trav_cbk_t *expn_subq_cbk,
sparp_gp_trav_cbk_t *literal_cbk )
{
return sparp_gp_trav_1 (sparp, sparp_gp_trav_int, tree, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
}
int
sparp_trav_out_clauses (sparp_t *sparp, SPART *root, void *common_env,
sparp_gp_trav_cbk_t *gp_in_cbk, sparp_gp_trav_cbk_t *gp_out_cbk,
sparp_gp_trav_cbk_t *expn_in_cbk, sparp_gp_trav_cbk_t *expn_out_cbk, sparp_gp_trav_cbk_t *expn_subq_cbk,
sparp_gp_trav_cbk_t *literal_cbk )
{
return sparp_gp_trav_1 (sparp, sparp_trav_out_clauses_int, root, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
}
int
sparp_gp_trav_1 (sparp_t *sparp, sparp_gp_trav_int_t *intcall, SPART *root, void *common_env,
sparp_gp_trav_cbk_t *gp_in_cbk, sparp_gp_trav_cbk_t *gp_out_cbk,
sparp_gp_trav_cbk_t *expn_in_cbk, sparp_gp_trav_cbk_t *expn_out_cbk, sparp_gp_trav_cbk_t *expn_subq_cbk,
sparp_gp_trav_cbk_t *literal_cbk
)
{
int res;
sparp_trav_params_t stp;
sparp_trav_state_t stss[SPARP_MAX_SYNTDEPTH+2];
if (sparp->sparp_trav_running)
spar_internal_error (sparp, "sparp_" "gp_trav_1() re-entered");
sparp->sparp_trav_running = 1;
stp.stp_gp_in_cbk = gp_in_cbk;
stp.stp_gp_out_cbk = gp_out_cbk;
stp.stp_expn_in_cbk = expn_in_cbk;
stp.stp_expn_out_cbk = expn_out_cbk;
stp.stp_expn_subq_cbk = expn_subq_cbk;
stp.stp_literal_cbk = literal_cbk;
#ifndef NDEBUG
if (NULL != sparp->sparp_stp)
spar_internal_error (sparp, "sparp_" "gp_trav_1() has non-NULL sparp_stp");
#endif
sparp->sparp_stp = &stp;
memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
stss[0].sts_parent = NULL;
stss[0].sts_curr_array = NULL;
stss[0].sts_ofs_of_curr_in_array = -1;
sparp->sparp_stss = stss;
res = intcall (sparp, root, sparp->sparp_stss + 1, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
sparp->sparp_stp = NULL;
sparp->sparp_stss = NULL;
sparp->sparp_trav_running = 0;
return (res & SPAR_GPT_COMPLETED);
}
void sparp_gp_trav_suspend (sparp_t *sparp)
{
sparp_env_t *env = sparp->sparp_env;
if (env->spare_gp_trav_is_saved)
spar_internal_error (sparp, "sparp_" "gp_trav_suspend() is called twice for same spare");
if (!sparp->sparp_trav_running)
spar_internal_error (sparp, "sparp_" "gp_trav_suspend() outside sparp_ " "gp_trav()");
env->spare_saved_stp = sparp->sparp_stp;
env->spare_saved_stss = sparp->sparp_stss;
env->spare_gp_trav_is_saved = 1;
#ifndef NDEBUG
sparp->sparp_stp = NULL;
sparp->sparp_stss = NULL;
#endif
sparp->sparp_trav_running = 0;
}
void sparp_gp_trav_resume (sparp_t *sparp)
{
sparp_env_t *env = sparp->sparp_env;
if (!env->spare_gp_trav_is_saved)
spar_internal_error (sparp, "sparp_" "gp_trav_resume() is called without sparp_" "gp_trav_suspend()");
sparp->sparp_stp = env->spare_saved_stp;
sparp->sparp_stss = env->spare_saved_stss;
sparp->sparp_trav_running = 1;
#ifndef NDEBUG
env->spare_saved_stp = NULL;
env->spare_saved_stss = NULL;
#endif
env->spare_gp_trav_is_saved = 0;
}
sparp_t *
sparp_down_to_sub (sparp_t *sparp, SPART *subq_gp_wrapper)
{
SPART *subq;
sparp_t *sub_sparp;
if ((SPAR_GP != SPART_TYPE (subq_gp_wrapper)) || (SELECT_L != subq_gp_wrapper->_.gp.subtype))
GPF_T1("sparp_" "down_to_sub (): bad subq_gp_wrapper");
subq = subq_gp_wrapper->_.gp.subquery;
if (SPAR_REQ_TOP != SPART_TYPE (subq))
spar_internal_error (sparp, "sparp_" "down_to_sub() gets strange subquery");
sparp_gp_trav_suspend (sparp);
sub_sparp = (sparp_t *)t_box_copy ((caddr_t)sparp);
sub_sparp->sparp_expr = subq;
sub_sparp->sparp_env = subq->_.req_top.shared_spare;
sub_sparp->sparp_parent_sparp = sparp;
sub_sparp->sparp_first_equiv_idx = sparp->sparp_sg->sg_equiv_count;
return sub_sparp;
}
void
sparp_up_from_sub (sparp_t *sparp, SPART *subq_gp_wrapper, sparp_t *sub_sparp)
{
sparp_gp_trav_resume (sparp);
subq_gp_wrapper->_.gp.subquery = sub_sparp->sparp_expr;
}
void
sparp_continue_gp_trav_in_sub (sparp_t *sparp, SPART *subq_gp_wrapper, void *common_env)
{
sparp_trav_params_t *stp = sparp->sparp_stp; /* This is done before sparp_down_to_sub() because it suspends and move the sparp_stp to spare */
sparp_t *sub_sparp = sparp_down_to_sub (sparp, subq_gp_wrapper);
sparp_gp_trav (sub_sparp, subq_gp_wrapper->_.gp.subquery->_.req_top.pattern, common_env,
SPARP_GP_TRAV_CALLBACK_ARGS(stp[0]) );
sparp_up_from_sub (sparp, subq_gp_wrapper, sub_sparp);
}
void
sparp_gp_localtrav_treelist (sparp_t *sparp, SPART **treelist,
void *init_stack_env, void *common_env,
sparp_gp_trav_cbk_t *gp_in_cbk, sparp_gp_trav_cbk_t *gp_out_cbk,
sparp_gp_trav_cbk_t *expn_in_cbk, sparp_gp_trav_cbk_t *expn_out_cbk, sparp_gp_trav_cbk_t *expn_subq_cbk,
sparp_gp_trav_cbk_t *literal_cbk
)
{
int ctr;
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 = init_stack_env;
stss[1].sts_curr_array = treelist;
DO_BOX_FAST (SPART *, tree, ctr, treelist)
{
stss[1].sts_ofs_of_curr_in_array = ctr;
sparp_gp_trav_int (sparp, tree, stss+1, common_env,
gp_in_cbk, gp_out_cbk,
expn_in_cbk, expn_out_cbk, expn_subq_cbk,
literal_cbk );
}
END_DO_BOX_FAST;
}
/* MACRO REWRITING */
void
spar_macro_xlate_selid (sparp_t *sparp, caddr_t *selid_ptr, spar_mproc_ctx_t *ctx)
{
if (NULL == ctx->smpc_defbody_topselid)
{
if ((NULL != selid_ptr[0]) && ('@' == selid_ptr[0][0]))
spar_internal_error (sparp, "spar_" "macro_xlate_selid(): selid not from defm but has '@'");
return;
}
if (NULL == ctx->smpc_context_selid)
spar_internal_error (sparp, "spar_" "macro_xlate_selid(): no context");
if ('@' == ctx->smpc_context_selid[0])
spar_internal_error (sparp, "spar_" "macro_xlate_selid(): context selid has '@'");
if (!strcmp (selid_ptr[0], ctx->smpc_context_selid))
return;
if (!strcmp (selid_ptr[0], ctx->smpc_defbody_topselid))
{
selid_ptr[0] = ctx->smpc_context_selid;
return;
}
selid_ptr[0] = t_box_sprintf (100, "%.70s%.20s%.10s", ctx->smpc_mcall->_.macrocall.mname, ctx->smpc_defbody_currselid, selid_ptr[0]);
}
SPART **
spar_macroprocess_define_list (sparp_t *sparp, SPART **trees, spar_mproc_ctx_t *ctx)
{
int ctr, len = BOX_ELEMENTS_0 (trees);
for (ctr = 1; ctr < len; ctr += 2)
{
SPART ***vallist = (SPART ***)(trees[ctr]);
int valctr, valcount;
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (vallist))
spar_internal_error (sparp, "spar_" "macroprocess_define_list(): invalid list of values");
valcount = BOX_ELEMENTS (vallist);
for (valctr = valcount; valctr--; /* no step */)
{
ptrlong valtype = ((ptrlong *)(vallist[valctr]))[0];
if ((SPAR_VARIABLE == valtype) || (SPAR_MACROPU == valtype))
{
vallist[valctr][1] = spar_macroprocess_tree (sparp, vallist[valctr][1], ctx);
}
}
}
return trees;
}
SPART **
spar_macroprocess_treelist (sparp_t *sparp, SPART **trees, int begin_with, spar_mproc_ctx_t *ctx)
{
int ctr, len = BOX_ELEMENTS_0 (trees);
for (ctr = begin_with; ctr < len; ctr++)
trees[ctr] = spar_macroprocess_tree (sparp, trees[ctr], ctx);
return trees;
}
SPART *
spar_macro_instantiate (sparp_t *sparp, SPART *tree, SPART *defm, SPART *mcall, spar_mproc_ctx_t *ctx)
{
SPART *cloned_body = sparp_tree_full_copy (sparp, defm->_.defmacro.body, NULL);
SPART *res = NULL;
spar_mproc_ctx_t local_ctx;
memset (&local_ctx, 0, sizeof (spar_mproc_ctx_t));
local_ctx.smpc_unictr = (sparp->sparp_unictr)++;
local_ctx.smpc_context_gp = ctx->smpc_context_gp;
local_ctx.smpc_context_selid = ctx->smpc_context_selid;
local_ctx.smpc_defbody_topselid = local_ctx.smpc_defbody_currselid = defm->_.defmacro.selid;
local_ctx.smpc_defm = defm;
local_ctx.smpc_mcall = mcall;
res = spar_macroprocess_tree (sparp, cloned_body, &local_ctx);
if (SPAR_GP != SPART_TYPE (res))
return res;
if ((0 != res->_.gp.subtype) && (DEFMACRO_L != res->_.gp.subtype))
return res;
if (0 != BOX_ELEMENTS_0 (res->_.gp.members))
{
if (0 == BOX_ELEMENTS_0 (local_ctx.smpc_ins_membs))
local_ctx.smpc_ins_membs = res->_.gp.members;
else
local_ctx.smpc_ins_membs = t_spartlist_concat (res->_.gp.members, local_ctx.smpc_ins_membs);
}
if (0 != BOX_ELEMENTS_0 (local_ctx.smpc_ins_membs))
{
if (0 == BOX_ELEMENTS_0 (ctx->smpc_ins_membs))
ctx->smpc_ins_membs = local_ctx.smpc_ins_membs;
else
ctx->smpc_ins_membs = t_spartlist_concat (ctx->smpc_ins_membs, local_ctx.smpc_ins_membs);
}
if (0 != BOX_ELEMENTS_0 (res->_.gp.filters))
{
if (0 == BOX_ELEMENTS_0 (local_ctx.smpc_ins_filts))
local_ctx.smpc_ins_filts = res->_.gp.filters;
else
local_ctx.smpc_ins_filts = t_spartlist_concat (res->_.gp.filters, local_ctx.smpc_ins_filts);
}
if (0 != BOX_ELEMENTS_0 (local_ctx.smpc_ins_filts))
{
if (0 == BOX_ELEMENTS_0 (ctx->smpc_ins_filts))
ctx->smpc_ins_filts = local_ctx.smpc_ins_filts;
else
ctx->smpc_ins_filts = t_spartlist_concat (ctx->smpc_ins_filts, local_ctx.smpc_ins_filts);
}
return NULL;
}
caddr_t
spar_macro_sign_inner_varname (sparp_t *sparp, caddr_t vname, spar_mproc_ctx_t *ctx)
{
char *tail;
caddr_t res = t_box_sprintf (100, "%.50s_%.50s_%d", vname, ctx->smpc_mcall->_.macrocall.mname, ctx->smpc_unictr);
for (tail = res; '\0' != tail[0]; tail++)
{
if ((':' == tail[0]) || ('/' == tail[0]) || ('-' == tail[0])) tail[0] = '_';
}
return res;
}
caddr_t
spar_macroprocess_varname (sparp_t *sparp, SPART *varname_or_macropu, int allow_macro_arg_namesakes, spar_mproc_ctx_t *ctx)
{
int param_index;
SPART *mcall, *argtree;
if (NULL == varname_or_macropu)
return NULL;
mcall = ctx->smpc_mcall;
if ((DV_STRING == DV_TYPE_OF (varname_or_macropu)) || (DV_UNAME == DV_TYPE_OF (varname_or_macropu)))
{
caddr_t varname = (caddr_t)varname_or_macropu;
int local_ctr;
if (NULL == mcall)
return varname;
DO_BOX_FAST_REV (caddr_t, nm, param_index, ctx->smpc_defm->_.defmacro.paramnames)
{
if (strcmp (nm, varname))
continue;
if (allow_macro_arg_namesakes)
goto param_index_found; /* see below */
else
spar_internal_error (sparp, "spar_" "macroprocess_varname(): plain varname instead of expected macropu");
}
END_DO_BOX_FAST_REV;
DO_BOX_FAST_REV (caddr_t, nm, local_ctr, ctx->smpc_defm->_.defmacro.localnames)
{
if (strcmp (nm, varname))
continue;
return spar_macro_sign_inner_varname (sparp, varname, ctx);
}
END_DO_BOX_FAST_REV;
spar_error (sparp, "The variable name '%.100s' in macro '%.100s' is neither argument name nor local name",
varname, ctx->smpc_defm->_.defmacro.mname );
return varname; /* Never reached */
}
if (SPAR_MACROPU != SPART_TYPE (varname_or_macropu))
spar_internal_error (sparp, "spar_" "macroprocess_varname(): bad arg");
if (NULL == mcall)
spar_internal_error (sparp, "spar_" "macroprocess_varname(): macroprocessing without macro call");
param_index = varname_or_macropu->_.macropu.pindex;
if (param_index >= BOX_ELEMENTS (mcall->_.macrocall.argtrees))
spar_internal_error (sparp, "spar_" "macroprocess_varname(): more macro parameters than macro call args");
param_index_found:
argtree = mcall->_.macrocall.argtrees[param_index];
if (SPAR_VARIABLE != SPART_TYPE (argtree))
spar_error (sparp, "The argument #%d (?%.100s) of macro '%.100s' should be a plain variable, because it is used as an alias name",
param_index+1, ctx->smpc_defm->_.defmacro.mname, ctx->smpc_defm->_.defmacro.paramnames[param_index] );
return argtree->_.var.vname;
}
SPART *
spar_macroprocess_tree (sparp_t *sparp, SPART *tree, spar_mproc_ctx_t *ctx)
{
int ctr;
switch (SPART_TYPE (tree))
{
case SPAR_GP:
{
SPART *saved_gp = ctx->smpc_context_gp;
caddr_t saved_selid = ctx->smpc_context_selid;
caddr_t saved_defbody_curselid = ctx->smpc_defbody_currselid;
if ('@' != tree->_.gp.selid[0]) /* We're outside a defbody (outside macro call at all or in group inside the argument */
{
if (NULL == ctx->smpc_defm) /* We're outside a macro call */
{
ctx->smpc_context_gp = tree;
ctx->smpc_context_selid = tree->_.gp.selid;
}
}
else
ctx->smpc_defbody_currselid = tree->_.gp.selid;
spar_macro_xlate_selid (sparp, &(tree->_.gp.selid), ctx);
tree->_.gp.options = spar_macroprocess_treelist (sparp, tree->_.gp.options, 0, ctx);
switch (tree->_.gp.subtype)
{
case SELECT_L:
{
SPART *subq = tree->_.gp.subquery;
spar_macro_xlate_selid (sparp, &(subq->_.req_top.retselid), ctx);
subq->_.req_top.orig_retvals = spar_macroprocess_treelist (sparp, subq->_.req_top.orig_retvals, 0, ctx);
subq->_.req_top.retvals = spar_macroprocess_treelist (sparp, subq->_.req_top.retvals, 0, ctx);
subq->_.req_top.pattern = spar_macroprocess_tree (sparp, subq->_.req_top.pattern, ctx);
subq->_.req_top.groupings = spar_macroprocess_treelist (sparp, subq->_.req_top.groupings, 0, ctx);
subq->_.req_top.having = spar_macroprocess_tree (sparp, subq->_.req_top.having, ctx);
subq->_.req_top.order = spar_macroprocess_treelist (sparp, subq->_.req_top.order, 0, ctx);
subq->_.req_top.limit = spar_macroprocess_tree (sparp, subq->_.req_top.limit, ctx);
subq->_.req_top.offset = spar_macroprocess_tree (sparp, subq->_.req_top.offset, ctx);
break;
}
case UNION_L:
DO_BOX_FAST (SPART *, memb, ctr, tree->_.gp.members)
{
tree->_.gp.members[ctr] = spar_macroprocess_tree (sparp, memb, ctx);
}
END_DO_BOX_FAST;
break;
default:
{
SPART **membs = tree->_.gp.members;
SPART **filts = tree->_.gp.filters;
int memb_ctr, memb_count = BOX_ELEMENTS (membs);
int filt_count = BOX_ELEMENTS_0 (filts);
for (memb_ctr = 0; memb_ctr < memb_count; memb_ctr++)
{
SPART *memb = membs[memb_ctr];
switch (SPART_TYPE (memb))
{
case SPAR_MACROCALL:
{
caddr_t mname = memb->_.macrocall.mname;
SPART *defm = spar_find_defmacro_by_iri_or_fields (sparp, mname, NULL);
int argctr;
if (SPAR_GP != SPART_TYPE (defm->_.defmacro.body))
spar_error (sparp, "Macro <%.200s> is expanded into expression but used as part of group pattern", mname);
if (defm->_.defmacro.subtype)
{
int context_graph_type = DEFAULT_L;
SPART *context_graph = memb->_.macrocall.context_graph;
if ((NULL != context_graph) && !SPART_IS_DEFAULT_GRAPH_BLANK (context_graph))
context_graph_type = GRAPH_L;
if (defm->_.defmacro.subtype != context_graph_type)
spar_error (sparp, "Macro <%.200s> should be used in context of %s graph but is placed into %s graph pattern", mname,
((DEFAULT_L == defm->_.defmacro.subtype) ? "default" : "named"),
((DEFAULT_L == context_graph_type) ? "default" : "named") );
}
DO_BOX_FAST (SPART *, mcarg, argctr, memb->_.macrocall.argtrees)
{
memb->_.macrocall.argtrees[argctr] = spar_macroprocess_tree (sparp, mcarg, ctx);
}
END_DO_BOX_FAST;
spar_macro_instantiate (sparp, tree, defm, memb, ctx);
tree->_.gp.members = membs = t_modify_spartlist (membs, memb_ctr, 1, ctx->smpc_ins_membs, BOX_ELEMENTS_0 (ctx->smpc_ins_membs));
ctx->smpc_ins_membs = NULL;
memb_count = BOX_ELEMENTS (membs);
memb_ctr--;
if (BOX_ELEMENTS_0 (ctx->smpc_ins_filts))
{
tree->_.gp.filters = filts = t_spartlist_concat (tree->_.gp.filters, ctx->smpc_ins_filts);
ctx->smpc_ins_filts = NULL;
}
filt_count = BOX_ELEMENTS (filts);
break;
}
case SPAR_MACROPU:
{
SPART *arg = ctx->smpc_mcall->_.macrocall.argtrees[tree->_.macropu.pindex];
SPART *arg_copy;
spar_mproc_ctx_t gparg_ctx;
if (SPAR_GP != SPART_TYPE (arg))
spar_error (sparp, "The argument #%d (?%.20s) of macro <%.200s> should be a group pattern",
tree->_.macropu.pindex, tree->_.macropu.pname, ctx->smpc_mcall->_.macrocall.mname );
arg_copy = sparp_tree_full_copy (sparp, arg, NULL);
memset (&gparg_ctx, 0, sizeof (spar_mproc_ctx_t));
gparg_ctx.smpc_unictr = (sparp->sparp_unictr)++;
gparg_ctx.smpc_context_gp = tree;
gparg_ctx.smpc_context_selid = tree->_.gp.selid;
gparg_ctx.smpc_defbody_topselid = gparg_ctx.smpc_defbody_currselid = arg_copy->_.gp.selid;
arg_copy = spar_macroprocess_tree (sparp, arg_copy, &gparg_ctx);
tree->_.gp.members = membs = t_modify_spartlist (membs, memb_ctr, 1, arg_copy->_.gp.members, BOX_ELEMENTS_0 (arg_copy->_.gp.members));
memb_count = BOX_ELEMENTS (membs);
memb_ctr--;
if (BOX_ELEMENTS_0 (arg_copy->_.gp.filters))
{
tree->_.gp.filters = filts = t_spartlist_concat (tree->_.gp.filters, arg_copy->_.gp.filters);
filt_count = BOX_ELEMENTS (filts);
}
break;
}
default:
tree->_.gp.members[memb_ctr] = spar_macroprocess_tree (sparp, memb, ctx);
if (NULL != ctx->smpc_ins_filts)
{
if (BOX_ELEMENTS_0 (ctx->smpc_ins_filts))
{
tree->_.gp.filters = filts = t_spartlist_concat (tree->_.gp.filters, ctx->smpc_ins_filts);
ctx->smpc_ins_filts = NULL;
}
filt_count = BOX_ELEMENTS (filts);
}
break;
}
}
break;
}
}
tree->_.gp.filters = spar_macroprocess_treelist (sparp, tree->_.gp.filters, 0, ctx);
ctx->smpc_defbody_currselid = saved_defbody_curselid;
ctx->smpc_context_gp = saved_gp;
ctx->smpc_context_selid = saved_selid;
return tree;
}
case SPAR_TRIPLE:
{
int ctr;
spar_macro_xlate_selid (sparp, &(tree->_.triple.selid), ctx);
if (NULL != tree->_.triple.tabid)
spar_macro_xlate_selid (sparp, &(tree->_.triple.tabid), ctx);
for (ctr = 0; ctr < SPART_TRIPLE_FIELDS_COUNT; ctr++)
{
SPART *fld = tree->_.triple.tr_fields[ctr];
SPART *new_fld = spar_macroprocess_tree (sparp, fld, ctx);
int new_fld_type = SPART_TYPE (new_fld);
switch (new_fld_type)
{
case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
/* This fails if a variable is made from MACROPU so its selid is selid from smpc_context_gp
if (strcmp (tree->_.triple.selid, new_fld->_.var.selid) ||
(NULL != new_fld->_.var.tabid && strcmp (tree->_.triple.tabid, new_fld->_.var.tabid)) )
spar_internal_error (sparp, "spar_" "macroprocess_tree(): strange macroprocessing of a triple field variable");
*/
new_fld->_.var.selid = tree->_.triple.selid;
new_fld->_.var.tabid = tree->_.triple.tabid;
new_fld->_.var.tr_idx = ctr;
new_fld->_.var.rvr.rvrRestrictions |= sparp_tr_usage_natural_restrictions[ctr];
/* no break */
case SPAR_LIT: case SPAR_QNAME:
tree->_.triple.tr_fields[ctr] = new_fld;
break;
default:
{
SPART *local_bnode1, *local_bnode2, *eq_bop;
spar_selid_push_reused (sparp, tree->_.triple.selid);
local_bnode1 = spar_make_blank_node (sparp, spar_mkid (sparp, "_:mcall"), 1);
spar_selid_pop (sparp);
tree->_.triple.tr_fields[ctr] = local_bnode1;
local_bnode1->_.var.tr_idx = ctr;
local_bnode1->_.var.tabid = tree->_.triple.tabid;
local_bnode2 = sparp_tree_full_copy (sparp, local_bnode1, NULL);
local_bnode2->_.var.tr_idx = SPART_VAR_OUTSIDE_TRIPLE;
local_bnode2->_.var.tabid = NULL;
eq_bop = spartlist (sparp, 3, BOP_EQ, local_bnode2, new_fld);
ctx->smpc_ins_filts = t_modify_spartlist (ctx->smpc_ins_filts, 0, 0, &eq_bop, 1);
break;
}
}
}
for (ctr = 1; ctr < BOX_ELEMENTS_0 (tree->_.triple.options); ctr += 2)
{
tree->_.triple.options[ctr] = spar_macroprocess_tree (sparp, tree->_.triple.options[ctr], ctx);
}
return tree;
}
case SPAR_VARIABLE:
case SPAR_BLANK_NODE_LABEL:
spar_macro_xlate_selid (sparp, &(tree->_.var.selid), ctx);
if (NULL != tree->_.var.tabid)
spar_macro_xlate_selid (sparp, &(tree->_.var.tabid), ctx);
if (NULL != ctx->smpc_defm)
tree->_.var.vname = spar_macro_sign_inner_varname (sparp, tree->_.var.vname, ctx);
return tree;
case SPAR_MACROCALL:
{
caddr_t mname;
SPART *defm;
SPART **context_membs = NULL;
SPART *res;
int context_memb_count = 0;
if (NULL != ctx->smpc_context_gp)
{
context_membs = ctx->smpc_context_gp->_.gp.members;
context_memb_count = BOX_ELEMENTS (context_membs);
}
mname = tree->_.macrocall.mname;
defm = spar_find_defmacro_by_iri_or_fields (sparp, mname, NULL);
if ((SPAR_GP == SPART_TYPE (defm->_.defmacro.body)) && (SELECT_L != defm->_.defmacro.body->_.gp.subtype))
spar_error (sparp, "Macro <%.200s> is expanded into group pattern but used as part of expression", mname);
DO_BOX_FAST (SPART *, mcarg, ctr, tree->_.macrocall.argtrees)
{
tree->_.macrocall.argtrees[ctr] = spar_macroprocess_tree (sparp, mcarg, ctx);
}
END_DO_BOX_FAST;
res = spar_macro_instantiate (sparp, tree, defm, tree, ctx);
if (NULL != ctx->smpc_context_gp)
{
ctx->smpc_context_gp->_.gp.members = t_modify_spartlist (context_membs, context_memb_count, 0, ctx->smpc_ins_membs, BOX_ELEMENTS_0 (ctx->smpc_ins_membs));
ctx->smpc_ins_membs = NULL;
if (NULL != ctx->smpc_ins_filts)
{
ctx->smpc_context_gp->_.gp.filters = t_spartlist_concat (ctx->smpc_context_gp->_.gp.filters, ctx->smpc_ins_filts);
ctx->smpc_ins_filts = NULL;
}
}
else
{
if ((NULL != ctx->smpc_ins_membs) || (NULL != ctx->smpc_ins_filts))
spar_error (sparp, "A call of macro <%.200s> forms parts oi graph pattern outside any graph pattern", mname);
}
return res;
}
case SPAR_LIT: case SPAR_QNAME: return tree;
case ORDER_L:
tree->_.oby.expn = spar_macroprocess_tree (sparp, tree->_.oby.expn, ctx);
return tree;
case SPAR_FUNCALL:
DO_BOX_FAST (SPART *, arg, ctr, tree->_.funcall.argtrees)
{
tree->_.funcall.argtrees[ctr] = spar_macroprocess_tree (sparp, arg, ctx);
}
END_DO_BOX_FAST;
return tree;
case SPAR_BUILT_IN_CALL:
DO_BOX_FAST (SPART *, arg, ctr, tree->_.builtin.args)
{
tree->_.builtin.args[ctr] = spar_macroprocess_tree (sparp, arg, ctx);
}
END_DO_BOX_FAST;
return tree;
case BOP_OR: case BOP_AND:
case BOP_PLUS: case BOP_MINUS: case BOP_TIMES: case BOP_DIV: case BOP_MOD:
case BOP_EQ: case SPAR_BOP_EQ: case BOP_NEQ: case BOP_LT: case BOP_LTE: case BOP_GT: case BOP_GTE:
case BOP_LIKE:
tree->_.bin_exp.right = spar_macroprocess_tree (sparp, tree->_.bin_exp.right, ctx);
/* no break; */
case BOP_NOT:
tree->_.bin_exp.left = spar_macroprocess_tree (sparp, tree->_.bin_exp.left, ctx);
return tree;
case SPAR_ALIAS:
tree->_.alias.arg = spar_macroprocess_tree (sparp, tree->_.alias.arg, ctx);
tree->_.alias.aname = spar_macroprocess_varname (sparp, (SPART *)(tree->_.alias.aname), 0, ctx);
return tree;
case SPAR_MACROPU:
{
int idx = tree->_.macropu.pindex;
SPART *mcall = ctx->smpc_mcall;
SPART *argtree, *argcopy;
if (NULL == mcall)
spar_internal_error (sparp, "spar_" "macroprocess_tree(): macro parameter without macro call");
if (idx >= BOX_ELEMENTS (mcall->_.macrocall.argtrees))
spar_internal_error (sparp, "spar_" "macroprocess_tree(): more macro parameters than macro call args");
argtree = mcall->_.macrocall.argtrees[idx];
argcopy = sparp_tree_full_copy (sparp, argtree, ctx->smpc_context_gp);
return argcopy;
}
case SPAR_SERVICE_INV:
{
int vctr;
tree->_.sinv.iri_params = spar_macroprocess_treelist (sparp, tree->_.sinv.iri_params, 0, ctx);
tree->_.sinv.defines = spar_macroprocess_define_list (sparp, tree->_.sinv.defines, ctx);
tree->_.sinv.sources = spar_macroprocess_treelist (sparp, tree->_.sinv.sources, 0, ctx);
if (DV_ARRAY_OF_POINTER == DV_TYPE_OF ((caddr_t)(tree->_.sinv.param_varnames)))
{
DO_BOX_FAST_REV (SPART * /* not caddr_t :) */, vname, vctr, tree->_.sinv.param_varnames)
{
tree->_.sinv.param_varnames[vctr] = spar_macroprocess_varname (sparp, vname, 1, ctx);
}
END_DO_BOX_FAST_REV;
}
DO_BOX_FAST_REV (SPART * /* not caddr_t :) */, vname, vctr, tree->_.sinv.rset_varnames)
{
tree->_.sinv.rset_varnames[vctr] = spar_macroprocess_varname (sparp, vname, 1, ctx);
}
END_DO_BOX_FAST_REV;
sparp->sparp_query_uses_sinvs++;
spar_add_service_inv_to_sg (sparp, tree);
return tree;
}
default:
{
GPF_T;
#if 0
int ctr;
DO_BOX_FAST (SPART *, sub, ctr, tree)
{
ptrlong sub_t;
if (!(DV_ARRAY_OF_POINTER == DV_TYPE_OF (sub)))
continue;
sub_t = sub->type;
if (!(((sub_t >= SPAR_MIN_TREE_TYPE) && (sub_t <= SPAR_MAX_TREE_TYPE)) || ((sub_t >= BOP_NOT) && (sub_t <= BOP_MOD))))
continue;
((SPART **)tree)[ctr] = spar_macroprocess_tree (sparp, sub, ctx);
}
END_DO_BOX_FAST;
#endif
return tree;
}
}
}
/* EQUIVALENCE CLASSES */
sparp_equiv_t *
sparp_equiv_alloc (sparp_t *sparp)
{
ptrlong eqcount = sparp->sparp_sg->sg_equiv_count;
sparp_equiv_t **eqs = sparp->sparp_sg->sg_equivs;
sparp_equiv_t *res;
if (eqcount >= 0x10000)
{
if (NULL == sparp->sparp_env->spare_storage_name)
spar_internal_error (sparp, "The SPARQL optimizer has failed to process the query with reasonable quality. The resulting SQL query is abnormally long. Please paraphrase the SPARQL query.");
else
spar_error (sparp, "The query is prohibitively inefficient if it should be executed on storage <%s>. Please optimize your mappings of relational data to RDF or paraphrase the SPARQL query.", sparp->sparp_env->spare_storage_name);
}
res = (sparp_equiv_t *)t_alloc_box (sizeof (sparp_equiv_t), DV_ARRAY_OF_POINTER);
memset (res, 0, sizeof (sparp_equiv_t));
if (BOX_ELEMENTS_INT_0 (eqs) == eqcount)
{
size_t new_size = ((NULL == eqs) ? 4 * sizeof (sparp_equiv_t *) : 2 * box_length (eqs));
sparp_equiv_t **new_eqs = (sparp_equiv_t **)t_alloc_box (new_size, DV_ARRAY_OF_POINTER);
if (NULL != eqs)
memcpy (new_eqs, eqs, box_length (eqs));
#ifdef DEBUG
if (NULL != eqs)
memset (eqs, -1, box_length (eqs));
#endif
sparp->sparp_sg->sg_equivs = eqs = new_eqs;
}
res->e_own_idx = eqcount;
#ifdef DEBUG
res->e_clone_idx = (SPART_BAD_EQUIV_IDX-1);
#endif
res->e_merge_dest_idx = SPART_BAD_EQUIV_IDX;
res->e_external_src_idx = SPART_BAD_EQUIV_IDX;
eqs[eqcount++] = res;
sparp->sparp_sg->sg_equiv_count = eqcount;
return res;
}
void
sparp_equiv_dbg_gp_print (sparp_t *sparp, SPART *tree)
{
int eq_ctr, eq_count;
eq_count = tree->_.gp.equiv_count;
SPARP_FOREACH_GP_EQUIV(sparp,tree,eq_ctr,eq)
{
int varname_count, varname_ctr;
spar_dbg_printf ((" ( %d subv (%d bindings), %d recv, %d gspo, %d const, %d opt, %d subq:",
BOX_ELEMENTS_INT_0(eq->e_subvalue_idxs), (int)(eq->e_nested_bindings), BOX_ELEMENTS_INT_0(eq->e_receiver_idxs),
(int)(eq->e_gspo_uses), (int)(eq->e_const_reads), (int)(eq->e_optional_reads), (int)(eq->e_subquery_uses) ));
varname_count = BOX_ELEMENTS (eq->e_varnames);
for (varname_ctr = 0; varname_ctr < varname_count; varname_ctr++)
{
spar_dbg_printf ((" %s", eq->e_varnames[varname_ctr]));
}
if (SPART_BAD_EQUIV_IDX != eq->e_external_src_idx)
{
spar_dbg_printf (("; parent #%d", (int)(eq->e_external_src_idx)));
}
spar_dbg_printf ((")"));
} END_SPARP_FOREACH_GP_EQUIV;
spar_dbg_printf (("\n"));
}
sparp_equiv_t *
sparp_equiv_get (sparp_t *sparp, SPART *haystack_gp, SPART *needle_var, int flags)
{
ptrlong *eq_idxs;
sparp_equiv_t *curr_eq;
SPART **curr_vars;
caddr_t needle_var_name;
int eqctr, eqcount;
int varctr, varcount;
int varnamectr, varnamecount;
eqcount = haystack_gp->_.gp.equiv_count;
eq_idxs = haystack_gp->_.gp.equiv_indexes;
#ifdef DEBUG
if (THR_IS_STACK_OVERFLOW (THREAD_CURRENT_THREAD, &sparp, 1000))
spar_internal_error (NULL, "sparp_equiv_get(): stack overflow");
switch (SPART_TYPE(needle_var))
{
case SPAR_VARIABLE: break;
case SPAR_BLANK_NODE_LABEL: break;
case SPAR_LIT:
if (flags & SPARP_EQUIV_INS_VARIABLE)
spar_internal_error (sparp, "sparp_" "equiv_get() with SPARP_EQUIV_INS_VARIABLE needs SPART * as needle_var");
if (
(flags & SPARP_EQUIV_GET_NAMESAKES) &&
((DV_STRING == DV_TYPE_OF (needle_var)) || (DV_UNAME == DV_TYPE_OF (needle_var))) )
break;
default: spar_internal_error (sparp, "sparp_" "equiv_get() with non-variable SPART *needle_var"); break;
}
if ((flags & SPARP_EQUIV_INS_VARIABLE) && strcmp (needle_var->_.var.selid, haystack_gp->_.gp.selid))
{
/* this masqeraded an error:
if (needle_var->_.var.selid == uname_nil)
needle_var->_.var.selid = haystack_gp->_.gp.selid;
else */
spar_internal_error (sparp, "sparp_" "equiv_get() with SPARP_EQUIV_INS_VARIABLE and wrong selid");
}
#endif
needle_var_name = (
((DV_STRING == DV_TYPE_OF (needle_var)) || (DV_UNAME == DV_TYPE_OF (needle_var))) ?
((caddr_t)(needle_var)) : spar_var_name_of_ret_column (needle_var) );
#ifndef NDEBUG
if (!IS_BOX_POINTER (needle_var_name))
GPF_T;
#endif
#ifdef DEBUG
if (BOX_ELEMENTS_INT_0 (eq_idxs) < eqcount)
spar_internal_error (sparp, "gp.equivs overflow");
#endif
for (eqctr = 0; eqctr < eqcount; eqctr++)
{
curr_eq = SPARP_EQUIV (sparp, eq_idxs[eqctr]);
if (NULL == curr_eq)
spar_internal_error (sparp, "sparp_" "equiv_get() at gp with deleted eq in use");
varnamecount = BOX_ELEMENTS (curr_eq->e_varnames);
for (varnamectr = 0; varnamectr < varnamecount; varnamectr++)
{
#ifndef NDEBUG
if (!IS_BOX_POINTER (curr_eq->e_varnames[varnamectr]))
GPF_T;
#endif
if (!strcmp (curr_eq->e_varnames[varnamectr], needle_var_name))
{
goto namesake_found; /* see below */
}
}
}
if (! (SPARP_EQUIV_INS_CLASS & flags))
goto retnull;
curr_eq = sparp_equiv_alloc (sparp);
curr_eq->e_gp = haystack_gp;
if (SPAR_GP != haystack_gp->type)
spar_internal_error (sparp, "sparp_" "equiv_get() with non-gp haystack");
if (BOX_ELEMENTS_INT_0 (eq_idxs) == eqcount)
{
size_t new_size = ((NULL == eq_idxs) ? 4 * sizeof (ptrlong) : 2 * box_length (eq_idxs));
ptrlong *new_eq_idxs = (ptrlong *)t_alloc_box (new_size, DV_ARRAY_OF_LONG);
if (NULL != eq_idxs)
memcpy (new_eq_idxs, eq_idxs, box_length (eq_idxs));
haystack_gp->_.gp.equiv_indexes = eq_idxs = new_eq_idxs;
}
eq_idxs[eqcount++] = curr_eq->e_own_idx;
haystack_gp->_.gp.equiv_count = eqcount;
curr_eq->e_varnames = t_list (1, needle_var_name);
if (SPARP_EQUIV_INS_VARIABLE & flags)
{
curr_eq->e_vars = (SPART **)t_list (2, needle_var, NULL);
needle_var->_.var.equiv_idx = curr_eq->e_own_idx;
curr_eq->e_var_count = 1;
if (SPARP_EQUIV_ADD_GSPO_USE & flags)
curr_eq->e_gspo_uses++;
if (SPARP_EQUIV_ADD_CONST_READ & flags)
curr_eq->e_const_reads++;
if (SPARP_EQUIV_ADD_SUBQUERY_USE & flags)
spar_internal_error (sparp, "SPARP_EQUIV_INS_VARIABLE conflicts with SPARP_EQUIV_ADD_SUBQUERY_USE");
if (SPARP_EQUIV_ADD_OPTIONAL_READ & flags)
spar_internal_error (sparp, "SPARP_EQUIV_INS_VARIABLE conflicts with SPARP_EQUIV_ADD_OPTIONAL_READ");
}
else
{
curr_eq->e_vars = (SPART **)t_list (1, NULL);
curr_eq->e_var_count = 0;
if (SPARP_EQUIV_ADD_SUBQUERY_USE & flags)
curr_eq->e_subquery_uses++;
if (SPARP_EQUIV_ADD_OPTIONAL_READ & flags)
curr_eq->e_optional_reads++;
}
#ifdef SPARQL_DEBUG
sparp_equiv_dbg_gp_print (sparp, haystack_gp);
#endif
return curr_eq;
namesake_found:
if (SPARP_EQUIV_GET_NAMESAKES & flags)
{
if (SPARP_EQUIV_ADD_SUBQUERY_USE & flags)
curr_eq->e_subquery_uses++;
if (SPARP_EQUIV_ADD_OPTIONAL_READ & flags)
curr_eq->e_optional_reads++;
return curr_eq;
}
curr_vars = curr_eq->e_vars;
varcount = curr_eq->e_var_count;
for (varctr = 0; varctr < varcount; varctr++)
{
if (curr_vars[varctr] == needle_var)
return curr_eq;
}
if (! (SPARP_EQUIV_INS_VARIABLE & flags))
goto retnull;
if (BOX_ELEMENTS_INT (curr_vars) == varcount)
{
SPART **new_vars = (SPART **)t_alloc_box (2 * box_length (curr_vars), DV_ARRAY_OF_POINTER);
memcpy (new_vars, curr_vars, box_length (curr_vars));
#ifdef DEBUG
memset (curr_vars, -1, box_length (curr_vars));
#endif
curr_eq->e_vars = curr_vars = new_vars;
}
if (SPARP_EQUIV_ADD_GSPO_USE & flags)
curr_eq->e_gspo_uses++;
if (SPARP_EQUIV_ADD_CONST_READ & flags)
curr_eq->e_const_reads++;
curr_vars[varcount++] = needle_var;
needle_var->_.var.equiv_idx = curr_eq->e_own_idx;
curr_eq->e_var_count = varcount;
#ifdef SPARQL_DEBUG
sparp_equiv_dbg_gp_print (sparp, haystack_gp);
#endif
return curr_eq;
retnull:
if (SPARP_EQUIV_GET_ASSERT & flags)
spar_internal_error (sparp, "sparp_" "equiv_get(): attempt of returning NULL when SPARP_EQUIV_GET_ASSERT & flags");
return NULL;
}
sparp_equiv_t *
sparp_equiv_get_ro (sparp_equiv_t **equivs, ptrlong equiv_count, SPART *haystack_gp, SPART *needle_var, int flags)
{
ptrlong *eq_idxs;
sparp_equiv_t *curr_eq;
SPART **curr_vars;
caddr_t needle_var_name;
int eqctr, eqcount;
int varctr, varcount;
int varnamectr, varnamecount;
#ifdef DEBUG
if (THR_IS_STACK_OVERFLOW (THREAD_CURRENT_THREAD, &equivs, 1000))
spar_internal_error (NULL, "sparp_equiv_get_ro(): stack overflow");
switch (SPART_TYPE(needle_var))
{
case SPAR_VARIABLE: break;
case SPAR_BLANK_NODE_LABEL: break;
case SPAR_RETVAL:
if (flags & SPARP_EQUIV_GET_NAMESAKES)
break;
spar_internal_error (NULL, "sparp_equiv_get_ro() with SPAR_RETVAL SPART *needle_var and without SPARP_EQUIV_GET_NAMESAKES");
break;
case SPAR_LIT:
if (
(flags & SPARP_EQUIV_GET_NAMESAKES) &&
((DV_STRING == DV_TYPE_OF (needle_var)) || (DV_UNAME == DV_TYPE_OF (needle_var))) )
break;
default: spar_internal_error (NULL, "sparp_equiv_get_ro() with non-variable SPART *needle_var"); break;
}
#endif
needle_var_name = (
((DV_STRING == DV_TYPE_OF (needle_var)) || (DV_UNAME == DV_TYPE_OF (needle_var))) ?
((caddr_t)(needle_var)) : needle_var->_.var.vname );
if (SPAR_BINDINGS_INV == haystack_gp->type)
{
DO_BOX_FAST_REV (SPART *, var, eqctr, haystack_gp->_.binv.vars)
{
if (!strcmp (var->_.var.vname, needle_var_name))
return equivs[var->_.var.equiv_idx];
}
END_DO_BOX_FAST_REV;
goto retnull;
}
eqcount = haystack_gp->_.gp.equiv_count;
eq_idxs = haystack_gp->_.gp.equiv_indexes;
#ifdef DEBUG
if (BOX_ELEMENTS_INT_0 (eq_idxs) < eqcount)
spar_internal_error (NULL, "sparp_equiv_get_ro(): gp.equivs overflow");
#endif
for (eqctr = 0; eqctr < eqcount; eqctr++)
{
#ifdef DEBUG
if (eq_idxs[eqctr] >= equiv_count)
spar_internal_error (NULL, "sparp_equiv_get_ro(): run out out equivs");
#endif
curr_eq = equivs[eq_idxs[eqctr]];
varnamecount = BOX_ELEMENTS (curr_eq->e_varnames);
for (varnamectr = 0; varnamectr < varnamecount; varnamectr++)
{
if (!strcmp (curr_eq->e_varnames[varnamectr], needle_var_name))
{
goto namesake_found; /* see below */
}
}
}
goto retnull;
namesake_found:
if (SPARP_EQUIV_GET_NAMESAKES & flags)
return curr_eq;
curr_vars = curr_eq->e_vars;
varcount = curr_eq->e_var_count;
for (varctr = 0; varctr < varcount; varctr++)
{
if (curr_vars[varctr] == needle_var)
return curr_eq;
}
retnull:
if (SPARP_EQUIV_GET_ASSERT & flags)
spar_internal_error (NULL, "sparp_equiv_get_ro(): attempt of returning NULL when SPARP_EQUIV_GET_ASSERT & flags");
return NULL;
}
sparp_equiv_t *
sparp_equiv_get_subvalue_ro (sparp_equiv_t **equivs, ptrlong equiv_count, SPART *haystack_gp, sparp_equiv_t *receiver)
{
int sub_ctr, sub_len;
int gp_ctr, gp_len;
sparp_equiv_t *res = NULL;
sub_len = BOX_ELEMENTS_INT_0 (receiver->e_subvalue_idxs);
gp_len = haystack_gp->_.gp.equiv_count;
for (sub_ctr = 0; sub_ctr < sub_len; sub_ctr++)
{
int subeq_idx = receiver->e_subvalue_idxs[sub_ctr];
#ifdef DEBUG
if (subeq_idx >= equiv_count)
spar_internal_error (NULL, "sparp_equiv_get_subvalue_ro(): run out out equivs");
#endif
for (gp_ctr = 0; gp_ctr < gp_len; gp_ctr++)
{
if (haystack_gp->_.gp.equiv_indexes[gp_ctr] != subeq_idx)
continue;
/* The result is either 'good' or 'bad'.
'Good' is one returned from the triple of the gp, hence it's non-NULL for sure.
'Bad' is returned from an underlaying select, hence can be NULL. */
res = equivs[subeq_idx];
if (res->e_var_count > 0)
return res; /* Good can be returned immediately. */
}
}
return res; /* Bad, if any, can wait. */
}
void *
sparp_tree_uses_var_of_eq (sparp_t *sparp, SPART *tree, sparp_equiv_t *equiv)
{
int ctr;
if (SPAR_TRIPLE == SPART_TYPE (tree))
{
for (ctr = equiv->e_var_count; ctr--; /* no step */)
{
caddr_t var_tabid = equiv->e_vars[ctr]->_.var.tabid;
if ((NULL != var_tabid) && !strcmp (var_tabid, tree->_.triple.tabid))
return equiv->e_vars[ctr];
}
}
else
{
#ifdef DEBUG
if (SPAR_GP != SPART_TYPE (tree))
spar_internal_error (sparp, "sparp_tree_uses_var_of_eq(): bad tree");
#endif
DO_BOX_FAST_REV (ptrlong, sub_eq_idx, ctr, equiv->e_subvalue_idxs)
{
sparp_equiv_t *sub_equiv = SPARP_EQUIV (sparp, sub_eq_idx);
if (sub_equiv->e_gp == tree)
return sub_equiv;
}
END_DO_BOX_FAST_REV;
}
return NULL;
}
/* Returns 1 if connection exists (or added), 0 otherwise. GPFs if tries to add the second up */
int
sparp_equiv_connect (sparp_t *sparp, sparp_equiv_t *outer, sparp_equiv_t *inner, int add_if_missing)
{
int i_ctr, i_count;
int o_listed_in_i = 0;
#ifdef DEBUG
int o_ctr, o_count;
int i_listed_in_o = 0;
o_count = BOX_ELEMENTS_0 (outer->e_subvalue_idxs);
for (o_ctr = o_count; o_ctr--; /* no step */)
{
if (outer->e_subvalue_idxs[o_ctr] == inner->e_own_idx)
{
i_listed_in_o = 1;
break;
}
}
#endif
i_count = BOX_ELEMENTS_0 (inner->e_receiver_idxs);
for (i_ctr = i_count; i_ctr--; /* no step */)
{
if (inner->e_receiver_idxs[i_ctr] == outer->e_own_idx)
{
o_listed_in_i = 1;
break;
}
}
if (o_listed_in_i)
{
#ifdef DEBUG
if (!i_listed_in_o)
spar_internal_error (sparp, "sparp_" "equiv_connect(): unidirectional link (1) ?");
#endif
return 2;
}
#ifdef DEBUG
if (i_listed_in_o)
spar_internal_error (sparp, "sparp_" "equiv_connect(): unidirectional link (2) ?");
#endif
if (!add_if_missing)
return 0;
outer->e_subvalue_idxs = (ptrlong *)t_list_concat_tail ((caddr_t)(outer->e_subvalue_idxs), 1, (ptrlong)(inner->e_own_idx));
inner->e_receiver_idxs = (ptrlong *)t_list_concat_tail ((caddr_t)(inner->e_receiver_idxs), 1, (ptrlong)(outer->e_own_idx));
if (SPARP_EQ_IS_ASSIGNED_LOCALLY(inner))
outer->e_nested_bindings += 1;
return 1;
}
/* Returns 1 if connection existed and removed. */
int
sparp_equiv_disconnect (sparp_t *sparp, sparp_equiv_t *outer, sparp_equiv_t *inner)
{
int o_ctr, o_count, i_ctr, i_count;
int o_listed_in_i = -1;
int i_listed_in_o = -1;
o_count = BOX_ELEMENTS_0 (outer->e_subvalue_idxs);
for (o_ctr = o_count; o_ctr--; /* no step */)
{
if (outer->e_subvalue_idxs[o_ctr] == inner->e_own_idx)
{
i_listed_in_o = o_ctr;
break;
}
}
i_count = BOX_ELEMENTS_0 (inner->e_receiver_idxs);
for (i_ctr = i_count; i_ctr--; /* no step */)
{
if (inner->e_receiver_idxs[i_ctr] == outer->e_own_idx)
{
o_listed_in_i = i_ctr;
break;
}
}
if (-1 == o_listed_in_i)
{
#ifdef DEBUG
if (-1 != i_listed_in_o)
spar_internal_error (sparp, "sparp_" "equiv_disconnect(): unidirectional link (1) ?");
#endif
return 0;
}
#ifdef DEBUG
if (-1 == i_listed_in_o)
spar_internal_error (sparp, "sparp_" "equiv_disconnect(): unidirectional link (2) ?");
#endif
outer->e_subvalue_idxs = (ptrlong *)t_list_remove_nth ((caddr_t)(outer->e_subvalue_idxs), i_listed_in_o);
inner->e_receiver_idxs = (ptrlong *)t_list_remove_nth ((caddr_t)(inner->e_receiver_idxs), o_listed_in_i);
if (SPARP_EQ_IS_ASSIGNED_LOCALLY (inner))
outer->e_nested_bindings -= 1;
return 1;
}
void
sparp_equiv_remove_var (sparp_t *sparp, sparp_equiv_t *eq, SPART *var)
{
int namesakes_count = 0;
int varctr;
int hit_idx = -1;
if (NULL == eq)
{
int eq_idx = var->_.var.equiv_idx;
if (SPART_BAD_EQUIV_IDX == eq_idx)
return;
if (eq_idx >= sparp->sparp_sg->sg_equiv_count)
spar_internal_error (sparp, "sparp_" "equiv_remove_var(): eq_idx is too big");
eq = SPARP_EQUIV (sparp, eq_idx);
if (NULL == eq)
spar_internal_error (sparp, "sparp_" "equiv_remove_var(): eq is merged and disabled");
}
for (varctr = eq->e_var_count; varctr--; /*no step*/)
{
SPART *curr_var = eq->e_vars [varctr];
if (curr_var == var)
{
if (0 <= hit_idx)
spar_internal_error (sparp, "sparp_" "equiv_remove_var(): duplicate occurrence of var in equiv ?");
hit_idx = varctr;
}
if (!strcmp (curr_var->_.var.vname, var->_.var.vname))
namesakes_count++;
}
if (0 > hit_idx)
spar_internal_error (sparp, "sparp_" "equiv_remove_var(): var is not in equiv ?");
if (1 > namesakes_count)
spar_internal_error (sparp, "sparp_" "equiv_remove_var(): no namesakes of var in equiv ?");
eq->e_vars[hit_idx] = eq->e_vars[eq->e_var_count - 1];
eq->e_vars[eq->e_var_count - 1] = NULL;
eq->e_var_count--;
var->_.var.equiv_idx = SPART_BAD_EQUIV_IDX;
if (1 == namesakes_count)
{ /* The name of variable is no longer in explicit use, but it can not be removed from eq.
The reason is that it still may be used to establish relationship between this eq and namesake of the removed variable in other gps.
An example is name "s1" in OPTIONAL clause of ?s1 ?p1 ?o1 OPTIONAL { ?s2 ?p2 ?o2 . FILTER (?s1 = ?s2) }
When filter is optimized away, the only ?s1 in OPTIONAL disappears but ?s1 outside still should find its namesake in OPTIONAL to not forget ON (s1 = s2) after LEFT OUTER JOIN.
At the same time, the unused name should not appear at the first place of \c e_varnames list because that may result in inaccurate alias in result list.
So it should be reordered in the list in such a way that it will never appear at the first position if there are any better names. */
int count = BOX_ELEMENTS (eq->e_varnames), ctr;
for (ctr = count; ctr--; /* no step */)
{
if (strcmp (eq->e_varnames [ctr], var->_.var.vname))
continue;
while (++ctr < count) eq->e_varnames [ctr-1] = eq->e_varnames [ctr];
eq->e_varnames [count-1] = var->_.var.vname;
break;
}
}
}
sparp_equiv_t *
sparp_equiv_clone (sparp_t *sparp, sparp_equiv_t *orig, SPART *cloned_gp)
{
sparp_equiv_t *tgt;
if (orig->e_cloning_serial == sparp->sparp_sg->sg_cloning_serial)
spar_internal_error (sparp, "sparp_" "equiv_clone(): can't make second clone of equiv during same gp cloning");
#ifndef NDEBUG
if (orig->e_deprecated)
spar_internal_error (sparp, "sparp_" "equiv_clone(): weird cloning of deprecated equiv");
#endif
tgt = sparp_equiv_alloc (sparp);
orig->e_cloning_serial = sparp->sparp_sg->sg_cloning_serial;
orig->e_clone_idx = tgt->e_own_idx;
tgt->e_gp = cloned_gp;
tgt->e_varnames = (caddr_t *)t_full_box_copy_tree ((caddr_t)(orig->e_varnames));
tgt->e_vars = (SPART **)t_alloc_box (box_length (orig->e_vars), DV_ARRAY_OF_POINTER); /* no real copying of e_vars */
/* no copying for e_var_count */
/* no copying for e_gspo_uses */
/* no copying for e_nested_bindings */
/* no copying for e_const_reads */
/* no copying for e_optional_reads */
/* no copying for e_subquery_uses */
tgt->e_replaces_filter = orig->e_replaces_filter;
sparp_rvr_copy (sparp, &(tgt->e_rvr), &(orig->e_rvr));
/* no copying for e_subvalue_idxs */
/* no copying for e_receiver_idxs */
/* no copying for e_clone_idx */
/* no copying for e_cloning_serial */
if (SPART_BAD_EQUIV_IDX != orig->e_external_src_idx)
{
sparp_equiv_t *esrc = SPARP_EQUIV(sparp, orig->e_external_src_idx);
while (SPART_BAD_EQUIV_IDX != esrc->e_merge_dest_idx)
{
sparp_equiv_t *merged_esrc = SPARP_EQUIV(sparp, esrc->e_merge_dest_idx);
esrc = merged_esrc;
}
if (esrc->e_cloning_serial == sparp->sparp_sg->sg_cloning_serial)
tgt->e_external_src_idx = orig->e_external_src_idx;
}
tgt->e_merge_dest_idx = orig->e_merge_dest_idx;
#ifdef DEBUG
/* no copying for e_dbg_saved_gp */
#endif
/* Add more lines here when more fields added to struct sparp_equiv_s. */
return tgt;
}
#if 0
sparp_equiv_t *
sparp_equiv_exact_copy (sparp_t *sparp, sparp_equiv_t *orig)
{
sparp_equiv_t *tgt;
if (orig->e_cloning_serial == sparp->sparp_sg->sg_cloning_serial)
spar_internal_error (sparp, "sparp_" "equiv_clone(): can't make second clone of equiv during same gp cloning");
tgt = (sparp_equiv_t *)t_alloc_box (sizeof (sparp_equiv_t), DV_ARRAY_OF_POINTER);
memcpy (tgt, orig, sizeof (sparp_equiv_t));
/* no real copying of e_gp */
tgt->e_varnames = (caddr_t *)t_full_box_copy_tree ((caddr_t)(orig->e_varnames));
tgt->e_vars = (SPART **)t_alloc_box (box_length (orig->e_vars), DV_ARRAY_OF_POINTER); /* no real copying of e_vars */
sparp_rvr_copy (sparp, &(tgt->e_rvr), &(orig->e_rvr));
tgt->e_subvalue_idxs = (caddr_t *)t_full_box_copy_tree ((caddr_t)(orig->e_subvalue_idxs));
tgt->e_receiver_idxs = (caddr_t *)t_full_box_copy_tree ((caddr_t)(orig->e_receiver_idxs));
/* Check if more lines are needed here when more fields added to struct sparp_equiv_s. */
return tgt;
}
#endif
int
sparp_equiv_restrict_by_constant (sparp_t *sparp, sparp_equiv_t *pri, ccaddr_t datatype, SPART *value)
{
rdf_val_range_t tmp;
sparp_rvr_set_by_constant (sparp, &tmp, datatype, value);
sparp_rvr_tighten (sparp, &tmp, &(pri->e_rvr), ~0);
if (tmp.rvrRestrictions & SPART_VARR_CONFLICT)
return SPARP_EQUIV_MERGE_ROLLBACK;
sparp_rvr_copy (sparp, &(pri->e_rvr), &tmp);
return SPARP_EQUIV_MERGE_OK;
}
void
sparp_equiv_remove (sparp_t *sparp, sparp_equiv_t *eq)
{
SPART *eq_gp = eq->e_gp;
ptrlong eq_own_idx = eq->e_own_idx;
ptrlong *eq_gp_indexes = eq_gp->_.gp.equiv_indexes;
int ctr1, len;
#ifndef NDEBUG
if (
(0 != eq->e_const_reads) ||
(0 != eq->e_gspo_uses) ||
(0 != eq->e_subquery_uses) ||
(0 != eq->e_var_count) ||
eq->e_replaces_filter ||
(0 != BOX_ELEMENTS_INT_0 (eq->e_receiver_idxs)) ||
(0 != BOX_ELEMENTS_INT_0 (eq->e_subvalue_idxs)) ||
(SPART_VARR_EXPORTED & eq->e_rvr.rvrRestrictions) )
spar_internal_error (sparp, "sparp_" "equiv_remove (): can't remove equiv that is still in use");
memset (eq, -1, sizeof (sparp_equiv_t));
#endif
for (ctr1 = len = eq_gp->_.gp.equiv_count; ctr1--; /* no step */)
{
if (eq_gp_indexes[ctr1] != eq_own_idx)
continue;
eq_gp_indexes[ctr1] = eq_gp_indexes[len-1];
eq_gp_indexes[len-1] = 0;
len--;
goto found_in_gp;
}
spar_internal_error (sparp, "sparp_" "equiv_remove (): failed to remove eq from its gp");
found_in_gp:
sparp->sparp_sg->sg_equivs[eq_own_idx] = NULL;
eq_gp->_.gp.equiv_count = len;
}
int
sparp_equiv_merge (sparp_t *sparp, sparp_equiv_t *pri, sparp_equiv_t *sec)
{
int ctr1, ret;
#ifdef DEBUG
int ctr2;
#endif
SPART *sec_gp;
if (pri == sec)
return SPARP_EQUIV_MERGE_DUPE;
sparp_equiv_audit_all (sparp, 0);
sec_gp = sec->e_gp;
#ifdef DEBUG
if (pri->e_gp != sec_gp)
spar_internal_error (sparp, "sparp_" "equiv_merge () can not merge equivs from two different gps");
for (ctr1 = BOX_ELEMENTS_INT_0 (sec->e_varnames); ctr1--; /* no step*/)
{
for (ctr2 = BOX_ELEMENTS_INT_0 (pri->e_varnames); ctr2--; /* no step*/)
{
if (!strcmp (sec->e_varnames[ctr1], pri->e_varnames[ctr2]))
spar_internal_error (sparp, "sparp_" "equiv_merge (): same variable name in two different equivs of same gp");
}
}
for (ctr1 = sec->e_var_count; ctr1--; /* no step*/)
{
for (ctr2 = pri->e_var_count; ctr2--; /* no step*/)
{
if (sec->e_vars[ctr1] == pri->e_vars[ctr2])
spar_internal_error (sparp, "sparp_" "equiv_merge (): same variable in two different equivs of same gp");
}
}
#endif
#if 0 /* I can't recall why did I add these restrictions, probably to cut the amount of testing. Now they're preventing from a bugfixing, but related situations are still not tested entirely */
if ((pri->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED) && (sec->e_rvr.rvrRestrictions & SPART_VARR_EXPORTED))
return SPARP_EQUIV_MERGE_ROLLBACK;
if ((0 != pri->e_subquery_uses) || (0 != sec->e_subquery_uses))
return SPARP_EQUIV_MERGE_ROLLBACK;
#endif
ret = sparp_equiv_restrict_by_constant (sparp, pri, sec->e_rvr.rvrDatatype, (SPART *)(sec->e_rvr.rvrFixedValue));
if (SPARP_EQUIV_MERGE_ROLLBACK == ret)
return ret;
pri->e_varnames = t_list_concat ((caddr_t)(pri->e_varnames), (caddr_t)(sec->e_varnames));
sec->e_varnames = t_list (0);
if (0 < sec->e_var_count)
{
SPART **new_vars = (SPART **) t_alloc_box ((pri->e_var_count + sec->e_var_count) * sizeof (SPART *), DV_ARRAY_OF_POINTER);
memcpy (new_vars, pri->e_vars, pri->e_var_count * sizeof (SPART *));
memcpy (new_vars + pri->e_var_count, sec->e_vars, sec->e_var_count * sizeof (SPART *));
for (ctr1 = sec->e_var_count; ctr1--; /* no step*/)
sec->e_vars[ctr1]->_.var.equiv_idx = pri->e_own_idx;
pri->e_vars = new_vars;
sec->e_vars = (SPART **)t_list (1, NULL);
pri->e_var_count += sec->e_var_count;
sec->e_var_count = 0;
}
pri->e_replaces_filter |= sec->e_replaces_filter;
sec->e_replaces_filter = 0;
sparp_rvr_tighten (sparp, &(pri->e_rvr), &(sec->e_rvr), ~0);
pri->e_gspo_uses += sec->e_gspo_uses;
sec->e_gspo_uses = 0;
sec->e_nested_bindings = 0;
pri->e_const_reads += sec->e_const_reads;
sec->e_const_reads = 0;
while (BOX_ELEMENTS_INT_0 (sec->e_subvalue_idxs))
{
ptrlong sub_idx = sec->e_subvalue_idxs[0];
sparp_equiv_t *sub_eq = SPARP_EQUIV(sparp,sub_idx);
sparp_equiv_disconnect (sparp, sec, sub_eq);
sparp_equiv_connect (sparp, pri, sub_eq, 1);
}
while (BOX_ELEMENTS_INT_0 (sec->e_receiver_idxs))
{
ptrlong recv_idx = sec->e_receiver_idxs[0];
sparp_equiv_t *recv_eq = SPARP_EQUIV(sparp,recv_idx);
sparp_equiv_disconnect (sparp, recv_eq, sec);
sparp_equiv_connect (sparp, recv_eq, pri, 1);
}
pri->e_nested_bindings = 0;
DO_BOX_FAST_REV (ptrlong, sub_idx, ctr1, pri->e_subvalue_idxs)
{
sparp_equiv_t *sub_eq = SPARP_EQUIV(sparp,sub_idx);
if (SPARP_EQ_IS_ASSIGNED_LOCALLY(sub_eq))
pri->e_nested_bindings += 1;
}
END_DO_BOX_FAST;
sec->e_merge_dest_idx = pri->e_own_idx;
if (SPART_VARR_EXPORTED & sec->e_rvr.rvrRestrictions)
{
pri->e_rvr.rvrRestrictions |= SPART_VARR_EXPORTED;
sec->e_rvr.rvrRestrictions &= ~SPART_VARR_EXPORTED;
}
sparp_equiv_remove (sparp, sec);
return ret;
}
caddr_t
rvr_string_fixedvalue (rdf_val_range_t *rvr)
{
caddr_t fv = (caddr_t) (rvr->rvrFixedValue);
dtp_t fv_dtp = DV_TYPE_OF (fv);
if (DV_ARRAY_OF_POINTER == fv_dtp)
{
fv = ((SPART *)fv)->_.lit.val;
fv_dtp = DV_TYPE_OF (fv);
}
if (IS_STRING_DTP (fv_dtp))
return fv;
return NULL;
}
int
sparp_fixedvalues_equal (sparp_t *sparp, SPART *first, SPART *second)
{
caddr_t first_val, first_language = NULL;
caddr_t second_val, second_language = NULL;
if (first == second)
return 1;
if (DV_ARRAY_OF_POINTER == DV_TYPE_OF (first))
{
first_val = first->_.lit.val;
if (SPAR_LIT == first->type)
first_language = first->_.lit.language;
}
else
first_val = (caddr_t)(first);
if (DV_ARRAY_OF_POINTER == DV_TYPE_OF (second))
{
second_val = second->_.lit.val;
if (SPAR_LIT == second->type)
second_language = second->_.lit.language;
}
else
second_val = (caddr_t)(second);
if (strcmp (
((NULL == first_language) ? "" : first_language),
((NULL == second_language) ? "" : second_language) ) )
return 0;
if (DVC_MATCH != cmp_boxes (first_val, second_val, NULL, NULL))
return 0;
return 1;
}
int
rvr_can_be_tightned (sparp_t *sparp, rdf_val_range_t *dest, rdf_val_range_t *add_on, int assume_binv_var_and_eq)
{
if (dest->rvrRestrictions & SPART_VARR_CONFLICT)
return 0; /* Can't be tighened --- it's conflict already, no matter how many reasons do we have for that */
if (add_on->rvrRestrictions & ~(dest->rvrRestrictions) &
( SPART_VARR_IS_REF | SPART_VARR_IS_IRI | SPART_VARR_IS_BLANK | SPART_VARR_IRI_CALC | SPART_VARR_IS_LIT |
SPART_VARR_TYPED | SPART_VARR_FIXED | SPART_VARR_NOT_NULL | SPART_VARR_LONG_EQ_SQL | SPART_VARR_ALWAYS_NULL ) )
return 1;
if ((add_on->rvrRestrictions & SPART_VARR_TYPED) && strcmp (dest->rvrDatatype, add_on->rvrDatatype))
return 1; /* Both are typed (if add_on is typed and dest is not then flag diff woud return 1 before), different types would tighten to the conflict */
if ( strcmp (
((NULL == dest->rvrLanguage) ? "" : dest->rvrLanguage),
((NULL == add_on->rvrLanguage) ? "" : add_on->rvrLanguage) ) )
return 1; /* different languages would tighten to the conflict */
if ((add_on->rvrRestrictions & SPART_VARR_FIXED) && !sparp_fixedvalues_equal (sparp, (SPART *)(dest->rvrFixedValue), (SPART *)(add_on->rvrFixedValue)))
return 1; /* different fixed values would tighten to the conflict */
if (add_on->rvrRestrictions & SPART_VARR_IRI_CALC)
{
if (assume_binv_var_and_eq)
{
if (add_on->rvrSprintffCount < dest->rvrSprintffCount)
return 1;
}
else
{
if ((add_on->rvrSprintffCount != dest->rvrSprintffCount) || memcmp (add_on->rvrSprintffs, dest->rvrSprintffs, add_on->rvrSprintffCount * sizeof (ccaddr_t)))
return 1;
}
}
if (add_on->rvrRedCutCount)
{
if (assume_binv_var_and_eq)
{
if (add_on->rvrRedCutCount > dest->rvrRedCutCount)
return 1;
}
else
{
if ((add_on->rvrRedCutCount != dest->rvrRedCutCount) || memcmp (add_on->rvrRedCuts, dest->rvrRedCuts, add_on->rvrRedCutCount * sizeof (ccaddr_t)))
return 1;
}
}
return 0;
}
int
sparp_equivs_have_same_fixedvalue (sparp_t *sparp, sparp_equiv_t *first_eq, sparp_equiv_t *second_eq)
{
if (!(SPART_VARR_FIXED & first_eq->e_rvr.rvrRestrictions))
return 0;
if (!(SPART_VARR_FIXED & second_eq->e_rvr.rvrRestrictions))
return 0;
if ( strcmp (
((NULL == first_eq->e_rvr.rvrDatatype) ? "" : first_eq->e_rvr.rvrDatatype),
((NULL == second_eq->e_rvr.rvrDatatype) ? "" : second_eq->e_rvr.rvrDatatype) ) )
return 0;
return sparp_fixedvalues_equal (sparp, (SPART *)(first_eq->e_rvr.rvrFixedValue), (SPART *)(second_eq->e_rvr.rvrFixedValue));
}
void
sparp_rvr_add_iri_classes (sparp_t *sparp, rdf_val_range_t *rvr, ccaddr_t *add_classes, ptrlong add_count)
{
int len = rvr->rvrIriClassCount;
int ctr, addctr;
int oldsize, newmax;
newmax = len + add_count;
oldsize = BOX_ELEMENTS_0 (rvr->rvrIriClasses);
if (oldsize < newmax)
{
int newsize = oldsize ? oldsize : 1;
ccaddr_t *new_buf;
do newsize *= 2; while (newsize < newmax);
new_buf = (ccaddr_t *)t_alloc_box (newsize * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
if (NULL != rvr->rvrIriClasses)
memcpy (new_buf, rvr->rvrIriClasses, oldsize * sizeof (caddr_t));
rvr->rvrIriClasses = new_buf;
}
#if 0 /* Version with no class hierarchy and alphabetcally sorted rvrIriClasses */
ctr = 0;
for (addctr = 0; addctr < add_count; addctr++)
{
int cmp, movectr;
next_ctr:
#ifdef DEBUG
if ((0 < ctr) && (0 <= strcmp (rvr->rvrIriClasses [ctr-1], rvr->rvrIriClasses [ctr])))
spar_internal_error (sparp, "sparp_" "rvr_add_iri_classes(): misordered e_iri_classes");
if ((0 < addctr) && (0 <= strcmp (add_classes [addctr-1], add_classes [addctr])))
spar_internal_error (sparp, "sparp_" "rvr_add_iri_classes(): misordered add_classes");
#endif
cmp = ((ctr >= len) ? 2 : strcmp (rvr->rvrIriClasses [ctr], add_classes [addctr]));
if (cmp < 0)
{
ctr++;
goto next_ctr;
}
if (0 == cmp)
continue;
for (movectr = len; movectr > ctr; movectr--)
rvr->rvrIriClasses [movectr] = rvr->rvrIriClasses [movectr - 1];
rvr->rvrIriClasses [ctr] = add_classes [addctr];
len++;
}
#else /* Version with nonrecursive class hierarchy and unsorted rvrIriClasses */
for (addctr = 0; addctr < add_count; addctr++)
{
int cmpctr, cmpcount;
ccaddr_t addon = add_classes [addctr];
caddr_t *addon_superclasses, *addon_subclasses;
for (ctr = 0; ctr < len; ctr++)
{
ccaddr_t old = rvr->rvrIriClasses [ctr];
if (old == addon) /* Already here */
goto skip_addon; /* see below */
}
addon_superclasses = jso_triple_get_objs ((caddr_t *)(sparp->sparp_sparqre->sparqre_qi), (caddr_t) addon, (caddr_t) uname_virtrdf_ns_uri_isSubclassOf);
cmpcount = BOX_ELEMENTS (addon_superclasses);
for (ctr = 0; ctr < len; ctr++)
{
ccaddr_t old = rvr->rvrIriClasses [ctr];
for (cmpctr = 0; cmpctr < cmpcount; cmpctr++)
{
if (old == addon_superclasses [cmpctr]) /* A superclass is already here */
{
dk_free_tree ((caddr_t)addon_superclasses);
goto skip_addon; /* see below */
}
}
}
dk_free_tree ((caddr_t)addon_superclasses);
addon_subclasses = jso_triple_get_subjs ((caddr_t *)(sparp->sparp_sparqre->sparqre_qi), (caddr_t) uname_virtrdf_ns_uri_isSubclassOf, (caddr_t) addon);
cmpcount = BOX_ELEMENTS (addon_subclasses);
for (ctr = 0; ctr < len; ctr++)
{
ccaddr_t old = rvr->rvrIriClasses [ctr];
for (cmpctr = 0; cmpctr < cmpcount; cmpctr++)
{
if (old == addon_subclasses [cmpctr]) /* old is redundant because addon superclass will be added */
{
if (ctr < (len-1))
rvr->rvrIriClasses [ctr] = rvr->rvrIriClasses [len - 1];
len--;
}
}
}
dk_free_tree ((caddr_t)addon_subclasses);
rvr->rvrIriClasses [len++] = addon;
skip_addon: ;
}
#endif
rvr->rvrIriClassCount = len;
}
void
sparp_rvr_intersect_iri_classes (sparp_t *sparp, rdf_val_range_t *rvr, ccaddr_t *isect_classes, ptrlong isect_count)
{
int len = rvr->rvrIriClassCount;
#if 0 /* Version with no class hierarchy and alphabetcally sorted rvrIriClasses */
int ctr, movectr = 0, isectctr = 0;
for (ctr = 0; ctr < len; ctr++)
{
int cmp;
next_isectctr:
#ifdef DEBUG
if ((0 < ctr) && (0 <= strcmp (rvr->rvrIriClasses [ctr-1], rvr->rvrIriClasses [ctr])))
spar_internal_error (sparp, "sparp_" "rvr_intersect_iri_classes(): misordered e_iri_classes");
if ((0 < isectctr) && (0 <= strcmp (isect_classes [isectctr-1], isect_classes [isectctr])))
spar_internal_error (sparp, "sparp_" "rvr_intersect_iri_classes(): misordered isect_classes");
#endif
cmp = ((isectctr >= isect_count) ? -2 : strcmp (rvr->rvrIriClasses [ctr], isect_classes [isectctr]));
if (cmp > 0)
{
isectctr++;
goto next_isectctr;
}
if (0 == cmp)
rvr->rvrIriClasses [movectr++] = rvr->rvrIriClasses [ctr];
}
rvr->rvrIriClassCount = movectr;
#else /* Version with nonrecursive class hierarchy and unsorted rvrIriClasses */
int ctr;
for (ctr = 0; ctr < len; ctr++)
{
ccaddr_t old = rvr->rvrIriClasses [ctr];
caddr_t *old_superclasses, *old_subclasses;
int cmpctr, cmpcount, isectctr;
for (isectctr = 0; isectctr < isect_count; isectctr++)
{
if (isect_classes [isectctr] == old) /* Found in isect */
goto test_next_old; /* see below */
}
old_superclasses = jso_triple_get_objs ((caddr_t *)(sparp->sparp_sparqre->sparqre_qi), (caddr_t) old, (caddr_t) uname_virtrdf_ns_uri_isSubclassOf);
cmpcount = BOX_ELEMENTS (old_superclasses);
for (isectctr = 0; isectctr < isect_count; isectctr++)
{
for (cmpctr = 0; cmpctr < cmpcount; cmpctr++)
{
if (isect_classes [isectctr] == old_superclasses [cmpctr]) /* Found in isect */
{
dk_free_tree ((caddr_t)old_superclasses);
goto test_next_old; /* see below */
}
}
}
dk_free_tree ((caddr_t)old_superclasses);
/* At this point we know that \c old is out of intersection. Let's remove it. */
if (ctr < (len-1))
rvr->rvrIriClasses [ctr] = rvr->rvrIriClasses [len - 1];
len--;
/* Now we should add subclasses of \c old that are in the \c isect_classes */
old_subclasses = jso_triple_get_subjs ((caddr_t *)(sparp->sparp_sparqre->sparqre_qi), (caddr_t) uname_virtrdf_ns_uri_isSubclassOf, (caddr_t) old);
cmpcount = BOX_ELEMENTS (old_subclasses);
for (cmpctr = 0; cmpctr < cmpcount; cmpctr++)
{
caddr_t old_sub = old_subclasses [cmpctr];
for (isectctr = 0; isectctr < isect_count; isectctr++)
{
if (isect_classes [isectctr] == old_sub) /* Found in isect */
{
int sctr;
int oldsize, newmax;
for (sctr = len; sctr--; /*no step*/)
{
if (rvr->rvrIriClasses [sctr] == old_sub)
goto test_next_subclass; /* see below */
}
oldsize = BOX_ELEMENTS_0 (rvr->rvrIriClasses);
newmax = len + isect_count;
if (oldsize < (len+1))
{
int newsize = oldsize ? oldsize : 1;
ccaddr_t *new_buf;
do newsize *= 2; while (newsize < newmax);
new_buf = (ccaddr_t *)t_alloc_box (newsize * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
if (NULL != rvr->rvrIriClasses)
memcpy (new_buf, rvr->rvrIriClasses, oldsize * sizeof (caddr_t));
rvr->rvrIriClasses = new_buf;
}
rvr->rvrIriClasses [len++] = old_sub;
goto test_next_subclass; /* see below */
}
}
test_next_subclass: ;
}
dk_free_tree ((caddr_t)old_subclasses);
test_next_old: ;
}
rvr->rvrIriClassCount = len;
#endif
}
void
sparp_rvr_add_red_cuts (sparp_t *sparp, rdf_val_range_t *rvr, ccaddr_t *add_cuts, ptrlong add_count)
{
int old_len, len;
int ctr, addctr;
int oldsize, newmax;
old_len = len = rvr->rvrRedCutCount;
newmax = len + add_count;
oldsize = BOX_ELEMENTS_0 (rvr->rvrRedCuts);
if (oldsize < newmax)
{
int newsize = oldsize ? oldsize : 1;
ccaddr_t *new_buf;
do newsize *= 2; while (newsize < newmax);
new_buf = (ccaddr_t *)t_alloc_box (newsize * sizeof (caddr_t), DV_ARRAY_OF_POINTER);
if (NULL != rvr->rvrRedCuts)
memcpy (new_buf, rvr->rvrRedCuts, oldsize * sizeof (caddr_t));
rvr->rvrRedCuts = new_buf;
}
for (addctr = add_count; addctr--; /* no step */)
{
ccaddr_t addon = add_cuts [addctr];
for (ctr = old_len; ctr--; /* no step */)
{
ccaddr_t old = rvr->rvrRedCuts [ctr];
if (old == addon) /* Already here */
goto skip_addon; /* see below */
}
rvr->rvrRedCuts [len++] = addon;
skip_addon: ;
}
rvr->rvrRedCutCount = len;
}
void
sparp_rvr_intersect_red_cuts (sparp_t *sparp, rdf_val_range_t *rvr, ccaddr_t *isect_cuts, ptrlong isect_count)
{
int len = rvr->rvrRedCutCount;
int ctr;
for (ctr = 0; ctr < len; ctr++)
{
ccaddr_t old = rvr->rvrRedCuts [ctr];
int isectctr;
for (isectctr = 0; isectctr < isect_count; isectctr++)
{
if (isect_cuts [isectctr] == old) /* Found in isect */
goto test_next_old; /* see below */
}
/* At this point we know that \c old is out of intersection. Let's remove it. */
if (ctr < (len-1))
rvr->rvrRedCuts [ctr] = rvr->rvrRedCuts [len - 1];
len--;
test_next_old: ;
}
rvr->rvrRedCutCount = len;
}
int
rvr_sprintffs_like (caddr_t value, rdf_val_range_t *rvr)
{
int ctr;
#ifdef DEBUG
if (!(SPART_VARR_SPRINTFF & rvr->rvrRestrictions))
GPF_T1 ("Invalid call of rvr_sprintffs_like()");
#endif
for (ctr = rvr->rvrSprintffCount; ctr--; /* no step */)
{
if (sprintff_like (value, rvr->rvrSprintffs[ctr]))
return 1+ctr;
}
return 0;
}
#ifdef DEBUG
void
dbg_sparp_rvr_audit (const char *file, int line, sparp_t *sparp, const rdf_val_range_t *rvr)
{
caddr_t err = NULL;
int ctr;
#define GOTO_RVR_ERR(x) { err = x; goto rvr_err; }
if (!(rvr->rvrRestrictions & SPART_VARR_SPRINTFF) && (0 != rvr->rvrSprintffCount))
GOTO_RVR_ERR("nonzero rvrSprintffCount when not SPART_VARR_SPRINTFF");
if ((rvr->rvrRestrictions & SPART_VARR_FIXED) && (0 != rvr->rvrSprintffCount))
GOTO_RVR_ERR("nonzero rvrSprintffCount when SPART_VARR_FIXED");
if ((rvr->rvrRestrictions & SPART_VARR_FIXED) && (NULL == rvr->rvrFixedValue))
GOTO_RVR_ERR("NULL rvrFixedValue when SPART_VARR_FIXED");
if (!(rvr->rvrRestrictions & SPART_VARR_FIXED) && (NULL != rvr->rvrFixedValue))
GOTO_RVR_ERR("non-NULL rvrFixedValue when not SPART_VARR_FIXED");
/*
if ((rvr->rvrRestrictions & SPART_VARR_TYPED) && (NULL == rvr->rvrDatatype))
GOTO_RVR_ERR("NULL rvrDatatype when SPART_VARR_TYPED");
if (!(rvr->rvrRestrictions & SPART_VARR_TYPED) && (NULL != rvr->rvrDatatype))
GOTO_RVR_ERR("non-NULL rvrDatatype when not SPART_VARR_TYPED");
*/
if ((rvr->rvrRestrictions & SPART_VARR_FIXED) && (rvr->rvrRestrictions & SPART_VARR_SPRINTFF))
GOTO_RVR_ERR("SPART_VARR_FIXED and SPART_VARR_SPRINTFF");
if (rvr->rvrRestrictions & SPART_VARR_SPRINTFF)
{
for (ctr = 0; ctr < rvr->rvrSprintffCount; ctr++)
{
ccaddr_t sff = rvr->rvrSprintffs[ctr];
dtp_t sff_dtp = DV_TYPE_OF (sff);
if (!IS_STRING_DTP(sff_dtp))
GOTO_RVR_ERR("non-string sprintff");
}
}
if (DV_ARRAY_OF_POINTER == DV_TYPE_OF (rvr->rvrFixedValue))
{
if (!SPAR_LIT_OR_QNAME_VAL ((SPART *)(rvr->rvrFixedValue)))
GOTO_RVR_ERR("weird non-NULL rvrFixedValue");
}
return;
rvr_err:
spar_internal_error (sparp, t_box_sprintf (1000, "sparp_" "rvr_audit (%s:%d): %s", file, line, err));
}
#endif
rdf_val_range_t *
sparp_rvr_copy (sparp_t *sparp, rdf_val_range_t *dest, const rdf_val_range_t *src)
{
if (SPARP_RVR_CREATE == dest)
dest = (rdf_val_range_t *)t_alloc (sizeof (rdf_val_range_t));
#ifdef DEBUG
if ((src->rvrRestrictions & SPART_VARR_IS_REF) && (src->rvrRestrictions & SPART_VARR_IS_LIT))
dbg_printf (("sparp" "_rvr_copy will copy %x (ref|lit)\n", (unsigned)(src->rvrRestrictions)));
#endif
if (src->rvrRestrictions & SPART_VARR_CONFLICT)
{
memset (dest, 0, sizeof (rdf_val_range_t));
dest->rvrRestrictions = SPART_VARR_CONFLICT;
return dest;
}
sparp_rvr_audit (sparp, src);
memcpy (dest, src, sizeof (rdf_val_range_t));
if (NULL != src->rvrSprintffs)
dest->rvrSprintffs = (ccaddr_t *)t_box_copy ((caddr_t)(src->rvrSprintffs));
if (NULL != src->rvrIriClasses)
dest->rvrIriClasses = (ccaddr_t *)t_box_copy ((caddr_t)(src->rvrIriClasses));
if (NULL != src->rvrRedCuts)
dest->rvrRedCuts = (ccaddr_t *)t_box_copy ((caddr_t)(src->rvrRedCuts));
sparp_rvr_audit (sparp, dest);
return dest;
}
void
sparp_rvr_set_by_constant (sparp_t *sparp, rdf_val_range_t *dest, ccaddr_t datatype, SPART *value)
{
memset (dest, 0, sizeof (rdf_val_range_t));
if (NULL != datatype)
{
dest->rvrDatatype = datatype;
dest->rvrRestrictions |= SPART_VARR_TYPED;
}
if (NULL != value)
{
if (SPAR_QNAME == SPART_TYPE (value))
{
#ifdef DEBUG
if (DV_UNAME != DV_TYPE_OF (value->_.lit.val))
GPF_T1 ("sparp_" "rvr_set_by_constant(): bad QNAME");
#endif
dest->rvrFixedValue = value->_.lit.val;
dest->rvrRestrictions |= (SPART_VARR_IS_REF | SPART_VARR_FIXED | SPART_VARR_NOT_NULL);
/* 0123456789 */
if (!strncmp (value->_.lit.val, "nodeID://", 9))
dest->rvrRestrictions |= SPART_VARR_IS_BLANK;
else
dest->rvrRestrictions |= SPART_VARR_IS_IRI;
}
else if (DV_UNAME == DV_TYPE_OF (value))
{
dest->rvrFixedValue = (ccaddr_t)value;
dest->rvrRestrictions |= (SPART_VARR_IS_REF | SPART_VARR_FIXED | SPART_VARR_NOT_NULL);
/* 0123456789 */
if (!strncmp ((ccaddr_t)value, "nodeID://", 9))
dest->rvrRestrictions |= SPART_VARR_IS_BLANK;
else
dest->rvrRestrictions |= SPART_VARR_IS_IRI;
}
else
{
#ifdef DEBUG
if (SPAR_LIT != SPART_TYPE (value))
GPF_T1("sparp_" "rvr_set_by_constant(): value is neither QNAME nor a literal");
#endif
dest->rvrFixedValue = (ccaddr_t)value;
dest->rvrRestrictions |= (SPART_VARR_IS_LIT | SPART_VARR_FIXED | SPART_VARR_NOT_NULL);
}
}
}
void
sparp_rvr_tighten (sparp_t *sparp, rdf_val_range_t *dest, rdf_val_range_t *addon, int changeable_flags)
{
ptrlong new_restr;
new_restr = (dest->rvrRestrictions | (addon->rvrRestrictions & changeable_flags));
#ifdef DEBUG
if (((new_restr & SPART_VARR_IS_REF) && (new_restr & SPART_VARR_IS_LIT)) &&
!((dest->rvrRestrictions & SPART_VARR_IS_REF) && (dest->rvrRestrictions & SPART_VARR_IS_LIT)) )
dbg_printf (("sparp" "_rvr_tighten will tighten %x with %x (ref|lit)\n", (unsigned)(dest->rvrRestrictions), (unsigned)(addon->rvrRestrictions)));
if ((new_restr & SPART_VARR_CONFLICT) &&
!(dest->rvrRestrictions & SPART_VARR_CONFLICT) )
dbg_printf (("sparp" "_rvr_tighten will tighten %x with %x (SPART_VARR_CONFLICT)\n", (unsigned)(dest->rvrRestrictions), (unsigned)(addon->rvrRestrictions)));
#endif
if (new_restr & SPART_VARR_CONFLICT)
goto conflict; /* see below */
sparp_rvr_audit (sparp, dest);
sparp_rvr_audit (sparp, addon);
if (dest->rvrDatatype != addon->rvrDatatype)
{
if (dest->rvrRestrictions & addon->rvrRestrictions & SPART_VARR_TYPED)
goto conflict; /* see below */
else
{
ccaddr_t isect_dt = sparp_largest_intersect_superdatatype (sparp, dest->rvrDatatype, addon->rvrDatatype);
dest->rvrDatatype = isect_dt;
}
}
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_FIXED)
{
if (dest->rvrRestrictions & SPART_VARR_FIXED)
{
if (!sparp_fixedvalues_equal (sparp, (SPART *)(dest->rvrFixedValue), (SPART *)(addon->rvrFixedValue)))
goto conflict; /* see below */
}
else
{
#ifdef DEBUG
if (SPAR_LIT != SPART_TYPE ((SPART *)(addon->rvrFixedValue)))
GPF_T1("sparp_" "rvr_tighten(): addon->rvrFixedValue is not a literal");
#endif
dest->rvrFixedValue = addon->rvrFixedValue;
}
}
if (new_restr & SPART_VARR_FIXED)
{
caddr_t fv = rvr_string_fixedvalue (dest);
if (NULL != fv)
{
int ctr;
if (dest->rvrRestrictions & SPART_VARR_SPRINTFF)
{
for (ctr = dest->rvrSprintffCount; ctr--; /* no step */)
{
if (sprintff_like (fv, dest->rvrSprintffs[ctr]))
goto fv_like_dest_sff;
}
goto conflict; /* see below */
}
fv_like_dest_sff:
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_SPRINTFF)
{
for (ctr = addon->rvrSprintffCount; ctr--; /* no step */)
{
if (sprintff_like (fv, addon->rvrSprintffs[ctr]))
goto fv_like_addon_sff;
}
goto conflict; /* see below */
}
fv_like_addon_sff:
new_restr &= ~SPART_VARR_SPRINTFF; /* With fixed string value, there's no need in sprintffs at all */
dest->rvrSprintffCount = 0;
goto end_of_sff_processing; /* see below */
}
}
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_SPRINTFF)
{
if (dest->rvrRestrictions & SPART_VARR_SPRINTFF)
{
sparp_rvr_intersect_sprintffs (sparp, dest, addon->rvrSprintffs, addon->rvrSprintffCount);
if (0 == dest->rvrSprintffCount)
goto conflict; /* see below */
}
else
{
dest->rvrSprintffs = (ccaddr_t *) t_box_copy ((caddr_t)(addon->rvrSprintffs));
dest->rvrSprintffCount = addon->rvrSprintffCount;
}
}
end_of_sff_processing:
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_IRI_CALC)
{
if (dest->rvrRestrictions & SPART_VARR_IRI_CALC)
{
sparp_rvr_intersect_iri_classes (sparp, dest, addon->rvrIriClasses, addon->rvrIriClassCount);
if (0 == dest->rvrIriClassCount)
#if 0 /*!!! TBD enable when iri classes are valid */
goto conflict; /* see below */
#else
new_restr &= ~SPART_VARR_IRI_CALC;
#endif
}
else
{
dest->rvrIriClasses = (ccaddr_t *) t_box_copy ((caddr_t)(addon->rvrIriClasses));
dest->rvrIriClassCount = addon->rvrIriClassCount;
}
}
if (0 != addon->rvrRedCutCount)
sparp_rvr_add_red_cuts (sparp, dest, addon->rvrRedCuts, addon->rvrRedCutCount);
if (dest->rvrRestrictions & SPART_VARR_FIXED)
{
int cut_ctr;
for (cut_ctr = dest->rvrRedCutCount; cut_ctr--; /* no step */)
{
ccaddr_t cut_val = dest->rvrRedCuts [cut_ctr];
if (sparp_fixedvalues_equal (sparp, (SPART *)cut_val, (SPART *)(dest->rvrFixedValue)))
goto conflict; /* see below */
}
}
if (
((new_restr & SPART_VARR_IS_REF) && (new_restr & SPART_VARR_IS_LIT)) ||
((new_restr & SPART_VARR_IS_BLANK) && (new_restr & SPART_VARR_IS_IRI)) ||
((new_restr & SPART_VARR_ALWAYS_NULL) &&
(new_restr & (SPART_VARR_NOT_NULL | SPART_VARR_IS_LIT | SPART_VARR_IS_REF)) ) )
goto conflict; /* see below */
#if 0
do {
dk_session_t *ses = strses_allocate ();
caddr_t strg;
spart_dump_rvr (ses, dest, 0);
strg = strses_string (ses);
printf ("Nice tighten op: %s\n", strg);
dk_free_box ((caddr_t)ses);
dk_free_box (strg);
} while (0);
#endif
dest->rvrRestrictions = new_restr;
sparp_rvr_audit (sparp, dest);
sparp_rvr_audit (sparp, addon);
return;
conflict:
new_restr |= SPART_VARR_CONFLICT;
dest->rvrRestrictions = new_restr;
/* sparp_rvr_audit (sparp, dest); -- that's not valid here */
return;
#if 0
always_null:
new_restr = SPART_VARR_ALWAYS_NULL | (dest->rvrRestrictions & (SPART_VARR_EXPORTED | SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL));
memset (dest, 0, sizeof (rdf_val_range_t));
dest->rvrRestrictions = new_restr;
sparp_rvr_audit (sparp, dest);
return;
#endif
}
#if 0 /* Attention: this code is not complete */
void
sparp_rvr_override (sparp_t *sparp, rdf_val_range_t *dest, rdf_val_range_t *addon, int changeable_flags, int reasons_for_conflict)
{
ptrlong new_restr;
new_restr = (dest->rvrRestrictions | (addon->rvrRestrictions & changeable_flags));
if (new_restr & SPART_VARR_CONFLICT)
goto conflict; /* see below */
sparp_rvr_audit (sparp, dest);
sparp_rvr_audit (sparp, addon);
if (dest->rvrDatatype != addon->rvrDatatype)
{
if (dest->rvrRestrictions & addon->rvrRestrictions & SPART_VARR_TYPED)
{
if (reasons_for_conflict & SPART_VARR_TYPED)
goto conflict; /* see below */
else
dest->rvrDatatype = addon->rvrDatatype;
}
else
{
ccaddr_t isect_dt = sparp_largest_intersect_superdatatype (sparp, dest->rvrDatatype, addon->rvrDatatype);
dest->rvrDatatype = isect_dt;
}
}
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_FIXED)
{
if (dest->rvrRestrictions & SPART_VARR_FIXED)
{
if (!sparp_fixedvalues_equal (sparp, (SPART *)(dest->rvrFixedValue), (SPART *)(addon->rvrFixedValue)))
{
if (reasons_for_conflict & SPART_VARR_FIXED)
goto conflict; /* see below */
else
dest->rvrFixedValue = addon->rvrFixedValue;
}
}
else
{
#ifdef DEBUG
if (SPAR_LIT != SPART_TYPE ((SPART *)(addon->rvrFixedValue)))
GPF_T1("sparp_" "rvr_tighten(): addon->rvrFixedValue is not a literal");
#endif
dest->rvrFixedValue = addon->rvrFixedValue;
}
}
if (new_restr & SPART_VARR_FIXED)
{
caddr_t fv = rvr_string_fixedvalue (dest);
if (NULL != fv)
{
int ctr;
if (dest->rvrRestrictions & SPART_VARR_SPRINTFF)
{
for (ctr = dest->rvrSprintffCount; ctr--; /* no step */)
{
if (sprintff_like (fv, dest->rvrSprintffs[ctr]))
goto fv_like_dest_sff; /* see below */
}
if (reasons_for_conflict & SPART_VARR_SPRINTFF)
goto conflict; /* see below */
else
goto turn_sff_off; /* see below */
}
fv_like_dest_sff:
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_SPRINTFF)
{
for (ctr = addon->rvrSprintffCount; ctr--; /* no step */)
{
if (sprintff_like (fv, addon->rvrSprintffs[ctr]))
goto fv_like_addon_sff; /* see below */
}
if (reasons_for_conflict & SPART_VARR_SPRINTFF)
goto conflict; /* see below */
else
goto turn_sff_off; /* see below */
}
fv_like_addon_sff:
turn_sff_off:
new_restr &= ~SPART_VARR_SPRINTFF; /* With fixed string value, there's no need in sprintffs at all */
dest->rvrSprintffCount = 0;
goto end_of_sff_processing; /* see below */
}
}
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_SPRINTFF)
{
if (dest->rvrRestrictions & SPART_VARR_SPRINTFF)
{
sparp_rvr_intersect_sprintffs (sparp, dest, addon->rvrSprintffs, addon->rvrSprintffCount);
if (0 == dest->rvrSprintffCount)
goto conflict; /* see below */
}
else
{
dest->rvrSprintffs = (ccaddr_t *) t_box_copy ((caddr_t)(addon->rvrSprintffs));
dest->rvrSprintffCount = addon->rvrSprintffCount;
}
}
end_of_sff_processing:
if (addon->rvrRestrictions & changeable_flags & SPART_VARR_IRI_CALC)
{
if (dest->rvrRestrictions & SPART_VARR_IRI_CALC)
{
sparp_rvr_intersect_iri_classes (sparp, dest, addon->rvrIriClasses, addon->rvrIriClassCount);
if (0 == dest->rvrIriClassCount)
#if 0 /*!!! TBD enable when iri classes are valid */
goto conflict; /* see below */
#else
new_restr &= ~SPART_VARR_IRI_CALC;
#endif
}
else
{
dest->rvrIriClasses = (ccaddr_t *) t_box_copy ((caddr_t)(addon->rvrIriClasses));
dest->rvrIriClassCount = addon->rvrIriClassCount;
}
}
if (0 != addon->rvrRedCutCount)
sparp_rvr_add_red_cuts (sparp, dest, addon->rvrRedCuts, addon->rvrRedCutCount);
if (dest->rvrRestrictions & SPART_VARR_FIXED)
{
int cut_ctr;
for (cut_ctr = dest->rvrRedCutCount; cut_ctr--; /* no step */)
{
ccaddr_t cut_val = dest->rvrRedCuts [cut_ctr];
if (sparp_fixedvalues_equal (sparp, (SPART *)cut_val, (SPART *)(dest->rvrFixedValue)))
goto conflict; /* see below */
}
}
if (
((new_restr & SPART_VARR_IS_REF) && (new_restr & SPART_VARR_IS_LIT)) ||
((new_restr & SPART_VARR_IS_BLANK) && (new_restr & SPART_VARR_IS_IRI)) ||
((new_restr & SPART_VARR_ALWAYS_NULL) &&
(new_restr & (SPART_VARR_NOT_NULL | SPART_VARR_IS_LIT | SPART_VARR_IS_REF)) ) )
goto conflict; /* see below */
#if 0
do {
dk_session_t *ses = strses_allocate ();
caddr_t strg;
spart_dump_rvr (ses, dest, 0);
strg = strses_string (ses);
printf ("Nice tighten op: %s\n", strg);
dk_free_box ((caddr_t)ses);
dk_free_box (strg);
} while (0);
#endif
dest->rvrRestrictions = new_restr;
sparp_rvr_audit (sparp, dest);
sparp_rvr_audit (sparp, addon);
return;
conflict:
new_restr |= SPART_VARR_CONFLICT;
dest->rvrRestrictions = new_restr;
/* sparp_rvr_audit (sparp, dest); -- that's not valid here */
return;
#if 0
always_null:
new_restr = SPART_VARR_ALWAYS_NULL | (dest->rvrRestrictions & (SPART_VARR_EXPORTED | SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL));
memset (dest, 0, sizeof (rdf_val_range_t));
dest->rvrRestrictions = new_restr;
sparp_rvr_audit (sparp, dest);
return;
#endif
}
#endif
void
sparp_rvr_loose (sparp_t *sparp, rdf_val_range_t *dest, rdf_val_range_t *addon, int changeable_flags)
{
ptrlong new_restr;
if (addon->rvrRestrictions & SPART_VARR_CONFLICT)
return;
if (dest->rvrRestrictions & SPART_VARR_CONFLICT)
{
int persistent_restr = (dest->rvrRestrictions & (SPART_VARR_EXPORTED | SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL));
sparp_rvr_copy (sparp, dest, addon);
dest->rvrRestrictions |= persistent_restr;
sparp_rvr_audit (sparp, dest);
return;
}
sparp_rvr_audit (sparp, dest);
sparp_rvr_audit (sparp, addon);
/* Can't loose these flags: */
changeable_flags &= ((ptrlong)(SPART_VARR__ALL)) &
~(SPART_VARR_EXPORTED | SPART_VARR_GLOBAL | SPART_VARR_EXTERNAL);
new_restr = dest->rvrRestrictions & (addon->rvrRestrictions | ~changeable_flags);
if (dest->rvrDatatype != addon->rvrDatatype)
{
ccaddr_t union_dt = sparp_smallest_union_superdatatype (sparp, dest->rvrDatatype, addon->rvrDatatype);
new_restr &= ~SPART_VARR_TYPED;
dest->rvrDatatype = union_dt;
}
if (new_restr & changeable_flags & SPART_VARR_FIXED)
{
if (!sparp_fixedvalues_equal (sparp, (SPART *)(dest->rvrFixedValue), (SPART *)(addon->rvrFixedValue)))
new_restr &= ~SPART_VARR_FIXED;
}
if (!(new_restr & changeable_flags & SPART_VARR_FIXED))
{ /* We may preserve the knowledge of fixed value in formats */
if (dest->rvrRestrictions & changeable_flags & SPART_VARR_FIXED)
{
caddr_t fv = rvr_string_fixedvalue (dest);
if (NULL != fv)
{
ccaddr_t fv_fmt = sprintff_from_strg (fv, 1);
if (0 != dest->rvrSprintffCount)
spar_internal_error (sparp, "sparp_" "rvr_loose (): bad dest: nonzero rvrSprintffCount when SPART_VARR_FIXED");
sparp_rvr_add_sprintffs (sparp, dest, &fv_fmt, 1);
dest->rvrRestrictions |= SPART_VARR_SPRINTFF;
new_restr |= (addon->rvrRestrictions & changeable_flags & SPART_VARR_SPRINTFF);
}
}
if ((addon->rvrRestrictions & changeable_flags & SPART_VARR_FIXED) &&
(dest->rvrRestrictions & SPART_VARR_SPRINTFF) )
{
caddr_t fv = rvr_string_fixedvalue (addon);
if (NULL != fv)
{
ccaddr_t fv_fmt = sprintff_from_strg (fv, 1);
if (0 != addon->rvrSprintffCount)
spar_internal_error (sparp, "sparp_" "rvr_loose (): bad addon: nonzero rvrSprintffCount when SPART_VARR_FIXED");
sparp_rvr_add_sprintffs (sparp, dest, &fv_fmt, 1);
dest->rvrRestrictions |= SPART_VARR_SPRINTFF;
new_restr |= SPART_VARR_SPRINTFF;
dest->rvrFixedValue = NULL;
goto end_of_sff_processing; /* see below */
}
}
dest->rvrFixedValue = NULL;
}
if (new_restr & changeable_flags & SPART_VARR_SPRINTFF)
sparp_rvr_add_sprintffs (sparp, dest, addon->rvrSprintffs, addon->rvrSprintffCount);
else
dest->rvrSprintffCount = 0;
end_of_sff_processing:
if (new_restr & changeable_flags & SPART_VARR_IRI_CALC)
sparp_rvr_add_iri_classes (sparp, dest, addon->rvrIriClasses, addon->rvrIriClassCount);
sparp_rvr_intersect_red_cuts (sparp, dest, addon->rvrRedCuts, addon->rvrRedCutCount);
dest->rvrRestrictions = new_restr;
sparp_rvr_audit (sparp, dest);
sparp_rvr_audit (sparp, addon);
}
#ifndef NDEBUG
void
sparp_equiv_tighten (sparp_t *sparp, sparp_equiv_t *eq, rdf_val_range_t *addon, int changeable_flags)
{
if (eq->e_deprecated)
spar_internal_error (sparp, "sparp_" "equiv_tighten(): can't change deprecated eq");
sparp_rvr_tighten (sparp, &(eq->e_rvr), addon, changeable_flags);
}
#if 0
void
sparp_equiv_override (sparp_t *sparp, sparp_equiv_t *eq, rdf_val_range_t *addon, int changeable_flags)
{
if (eq->e_deprecated)
spar_internal_error (sparp, "sparp_" "equiv_tighten(): can't change deprecated eq");
sparp_rvr_override (sparp, &(eq->e_rvr), addon, changeable_flags);
}
#endif
void
sparp_equiv_loose (sparp_t *sparp, sparp_equiv_t *eq, rdf_val_range_t *addon, int changeable_flags)
{
if (eq->e_deprecated)
spar_internal_error (sparp, "sparp_" "equiv_loose(): can't change deprecated eq");
if (addon->rvrRestrictions & SPART_VARR_CONFLICT)
return;
sparp_rvr_loose (sparp, &(eq->e_rvr), addon, changeable_flags);
}
#endif
/* RELATIONS BETWEEN QUAD MAP FORMATS AND BUILT-IN VALMODES */
ccaddr_t
sparp_smallest_union_superdatatype (sparp_t *sparp, ccaddr_t iri1, ccaddr_t iri2)
{
if (iri1 == iri2)
return iri1;
if ((NULL == iri1) || (NULL == iri2))
return NULL;
#ifdef DEBUG
if ((DV_UNAME != DV_TYPE_OF (iri1)) || (DV_UNAME != DV_TYPE_OF (iri2)))
spar_internal_error (sparp, "sparp_" "smallest_union_superdatatype(): non-UNAME datatype IRI");
#endif
if (strcmp (iri1, iri2) > 0)
{
ccaddr_t swap;
swap = iri1; iri1 = iri2; iri2 = swap;
}
if ((uname_xmlschema_ns_uri_hash_any == iri1) || (uname_xmlschema_ns_uri_hash_any == iri2))
return uname_xmlschema_ns_uri_hash_any;
if ((uname_xmlschema_ns_uri_hash_anyURI == iri1) || (uname_xmlschema_ns_uri_hash_anyURI == iri2))
return uname_xmlschema_ns_uri_hash_any; /* anyURI nd non-anyURI result in any, because they're probably for an IRI REF and a literal */
if ((uname_xmlschema_ns_uri_hash_double == iri1) && (uname_xmlschema_ns_uri_hash_float == iri2))
return uname_xmlschema_ns_uri_hash_double;
if ((uname_xmlschema_ns_uri_hash_decimal == iri1) && (uname_xmlschema_ns_uri_hash_integer == iri2))
return uname_xmlschema_ns_uri_hash_decimal;
return uname_xmlschema_ns_uri_hash_any;
}
ccaddr_t
sparp_largest_intersect_superdatatype (sparp_t *sparp, ccaddr_t iri1, ccaddr_t iri2)
{
if (iri1 == iri2)
return iri1;
if (NULL == iri1)
return iri2;
if (NULL == iri2)
return iri1;
#ifdef DEBUG
if ((DV_UNAME != DV_TYPE_OF (iri1)) || (DV_UNAME != DV_TYPE_OF (iri2)))
spar_internal_error (sparp, "sparp_" "largest_intersect_subdatatype(): non-UNAME datatype IRI");
#endif
if (strcmp (iri1, iri2) > 0)
{
ccaddr_t swap;
swap = iri1; iri1 = iri2; iri2 = swap;
}
if (uname_xmlschema_ns_uri_hash_any == iri1)
return iri2;
if (uname_xmlschema_ns_uri_hash_any == iri2)
return iri1;
if ((uname_xmlschema_ns_uri_hash_anyURI == iri1) || (uname_xmlschema_ns_uri_hash_anyURI == iri2))
return uname_xmlschema_ns_uri_hash_anyURI;
if ((uname_xmlschema_ns_uri_hash_double == iri1) && (uname_xmlschema_ns_uri_hash_float == iri2))
return uname_xmlschema_ns_uri_hash_float;
if ((uname_xmlschema_ns_uri_hash_decimal == iri1) && (uname_xmlschema_ns_uri_hash_integer == iri2))
return uname_xmlschema_ns_uri_hash_integer;
return iri1;
}
/* BASIC TREE MODIFICATION FUNCTIONS */
SPART *
sparp_gp_detach_member_int (sparp_t *sparp, SPART *parent_gp, int member_idx, dk_set_t *touched_equivs_set_ptr)
{
SPART *memb;
SPART **old_members = parent_gp->_.gp.members;
#ifdef DEBUG
int old_len = BOX_ELEMENTS (old_members);
if ((0 > member_idx) || (old_len <= member_idx))
spar_internal_error (sparp, "sparp_" "gp_detach_member_int(): bad member_idx");
#endif
memb = old_members[member_idx];
if (SPAR_GP == SPART_TYPE (memb))
{
int memb_eq_ctr;
#ifdef DEBUG
int parent_eq_ctr;
#endif
SPARP_REVFOREACH_GP_EQUIV (sparp, memb, memb_eq_ctr, eq)
{
while (BOX_ELEMENTS_0 (eq->e_receiver_idxs))
{
sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, eq->e_receiver_idxs[0]);
sparp_equiv_disconnect (sparp, recv_eq, eq);
if (NULL != touched_equivs_set_ptr)
t_set_pushnew (touched_equivs_set_ptr, recv_eq);
}
eq->e_rvr.rvrRestrictions &= ~SPART_VARR_EXPORTED;
}
END_SPARP_REVFOREACH_GP_EQUIV;
#ifdef DEBUG
SPARP_REVFOREACH_GP_EQUIV (sparp, parent_gp, parent_eq_ctr, parent_eq)
{
int subv_eq_ctr;
DO_BOX_FAST (ptrlong, subv_eq_idx, subv_eq_ctr, parent_eq->e_subvalue_idxs)
{
sparp_equiv_t *subv_eq = SPARP_EQUIV (sparp, subv_eq_idx);
SPART *subv_gp = sparp_find_gp_by_eq_idx (sparp, subv_eq_idx);
if (subv_gp != subv_eq->e_gp)
spar_internal_error (sparp, "sparp_" "gp_detach_member_int(): subv_gp != subv_eq->e_gp");
if (subv_gp == memb)
spar_internal_error (sparp, "sparp_" "gp_detach_member_int(): receiver not disconnected");
}
END_DO_BOX_FAST;
}
END_SPARP_REVFOREACH_GP_EQUIV;
#endif
}
else
{
int fld_ctr;
#ifdef DEBUG
if (SPAR_TRIPLE != SPART_TYPE (memb))
spar_internal_error (sparp, "sparp_" "gp_detach_member_int(): type of memb is neither SPAR_GP nor SPAR_TRIPLE");
#endif
for (fld_ctr = SPART_TRIPLE_FIELDS_COUNT; fld_ctr--; /*no step*/)
{
SPART *field = memb->_.triple.tr_fields [fld_ctr];
if (SPAR_IS_BLANK_OR_VAR(field))
{
sparp_equiv_t *parent_eq = sparp_equiv_get (sparp, parent_gp, field, SPARP_EQUIV_GET_ASSERT);
sparp_equiv_remove_var (sparp, parent_eq, field);
if (NULL != touched_equivs_set_ptr)
t_set_pushnew (touched_equivs_set_ptr, parent_eq);
parent_eq->e_gspo_uses--;
}
}
if (NULL != memb->_.triple.options)
{
int eq_ctr;
SPARP_FOREACH_GP_EQUIV (sparp, parent_gp, eq_ctr, eq)
{
int var_ctr;
for (var_ctr = eq->e_var_count; var_ctr--; /* no step */) /* The order is important due to sparp_equiv_remove_var() */
{
SPART *var = eq->e_vars[var_ctr];
if (NULL == var->_.var.tabid)
continue;
if (strcmp (var->_.var.tabid, memb->_.triple.tabid))
continue;
sparp_equiv_remove_var (sparp, eq, var);
if (NULL != touched_equivs_set_ptr)
t_set_pushnew (touched_equivs_set_ptr, eq);
eq->e_gspo_uses--;
/* this masqeraded an error: var->_.var.selid = uname_nil; */
}
}
END_SPARP_FOREACH_GP_EQUIV;
#ifdef DEBUG
{
SPART **opts = memb->_.triple.options;
int optctr;
for (optctr = BOX_ELEMENTS (opts)-1; 0 < optctr; optctr -= 2)
{
SPART *var = opts[optctr];
if (SPAR_VARIABLE == SPART_TYPE (var))
{
sparp_equiv_t *eq = sparp_equiv_get (sparp, parent_gp, var, 0);
if (NULL != eq)
spar_internal_error (sparp, "sparp_" "gp_detach_member_int (): option var is not removed from equiv");
}
}
}
#endif
}
}
return memb;
}
caddr_t
sparp_clone_id (sparp_t *sparp, caddr_t orig_name)
{
int orig_len = strlen (orig_name);
char buf[100];
if (orig_len > 20)
{
int hash;
BYTE_BUFFER_HASH (hash, orig_name, orig_len);
memcpy (buf, orig_name, 20);
sprintf (buf + 20, "X%c%c%c%c-c%d",
'A' + (hash & 0x10), 'A' + ((hash >> 4) & 0x10),
'A' + ((hash >> 8) & 0x10), 'A' + ((hash >> 12) & 0x10),
(int)(sparp->sparp_sg->sg_cloning_serial) );
}
else
sprintf (buf, "%s-c%d", orig_name, (int)(sparp->sparp_sg->sg_cloning_serial));
return t_box_dv_short_string (buf);
}
SPART **
sparp_treelist_full_clone_int (sparp_t *sparp, SPART **origs, SPART *parent_gp);
SPART *
sparp_tree_full_clone_int (sparp_t *sparp, SPART *orig, SPART *parent_gp)
{
int eq_ctr, arg_ctr, fld_ctr, eq_idx, cloned_eq_idx;
SPART *tgt;
switch (SPART_TYPE (orig))
{
case SPAR_ALIAS:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.alias.arg = sparp_tree_full_clone_int (sparp, orig->_.alias.arg, parent_gp);
return tgt;
case SPAR_BLANK_NODE_LABEL: case SPAR_VARIABLE:
if (NULL == parent_gp)
return sparp_tree_full_copy (sparp, orig, parent_gp);
tgt = (SPART *)t_box_copy ((caddr_t) orig);
eq_idx = orig->_.var.equiv_idx;
if (SPART_BAD_EQUIV_IDX != eq_idx)
{
sparp_equiv_t *eq = SPARP_EQUIV (sparp, eq_idx);
sparp_equiv_t *cloned_eq;
int var_pos;
if (eq->e_cloning_serial != sparp->sparp_sg->sg_cloning_serial)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): eq not cloned for a variable");
tgt->_.var.equiv_idx = eq->e_clone_idx;
cloned_eq = SPARP_EQUIV (sparp, eq->e_clone_idx);
for (var_pos = eq->e_var_count; var_pos--; /*no step*/)
if (eq->e_vars [var_pos] == orig)
break;
if (0 > var_pos)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): orig var is not in its eq");
#if 0
if (var_pos >= BOX_ELEMENTS (cloned_eq->e_vars))
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): mismatch of lengths of buffers for variables in orig and clone equivs");
if (NULL != cloned_eq->e_vars [var_pos])
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): cloned variable overwrites an var in cloned equiv");
cloned_eq->e_vars [var_pos] = tgt;
if (cloned_eq->e_var_count <= var_pos)
cloned_eq->e_var_count = var_pos + 1;
#else
if (BOX_ELEMENTS (cloned_eq->e_vars) <= cloned_eq->e_var_count)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): mismatch of lengths of buffer for variables in clone equiv and the number of variables");
cloned_eq->e_vars [cloned_eq->e_var_count++] = tgt;
#endif
if (NULL != orig->_.var.tabid)
cloned_eq->e_gspo_uses += 1;
else
cloned_eq->e_const_reads += 1;
}
else
{
sparp_equiv_t *cloned_eq = sparp_equiv_get (sparp, parent_gp, tgt, 0);
if (NULL != cloned_eq)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): cloned variable is in equiv, original is not");
}
#ifdef DEBUG
if (strcmp (orig->_.var.selid, parent_gp->_.gp.selid))
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): orig var is not from parent gp");
#endif
tgt->_.var.selid = sparp_clone_id (sparp, orig->_.var.selid);
if (NULL != orig->_.var.tabid)
tgt->_.var.tabid = sparp_clone_id (sparp, orig->_.var.tabid);
/* tgt->_.var.vname = t_box_copy (orig->_.var.vname); */
sparp_rvr_copy (sparp, &(tgt->_.var.rvr), &(orig->_.var.rvr));
return tgt;
case SPAR_GP:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.gp.equiv_indexes = (ptrlong *)t_box_copy ((caddr_t) orig->_.gp.equiv_indexes);
for (eq_ctr = 0; eq_ctr < orig->_.gp.equiv_count; eq_ctr++)
{
sparp_equiv_t *eq, *cloned_eq;
eq_idx = orig->_.gp.equiv_indexes [eq_ctr];
if (SPART_BAD_EQUIV_IDX == eq_idx)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): bad equiv idx in list of gp.equiv_indexes");
eq = SPARP_EQUIV (sparp, eq_idx);
if (orig != eq->e_gp)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): orig != e_gp");
cloned_eq = sparp_equiv_clone (sparp, eq, tgt);
tgt->_.gp.equiv_indexes [eq_ctr] = cloned_eq->e_own_idx;
if (NULL != parent_gp)
{
int recv_ctr;
ptrlong *cloned_recvs = (ptrlong *)t_box_copy ((caddr_t)(eq->e_receiver_idxs));
cloned_eq->e_receiver_idxs = cloned_recvs;
DO_BOX_FAST_REV (ptrlong, recv, recv_ctr, cloned_recvs)
{
sparp_equiv_t *recv_eq = SPARP_EQUIV (sparp, recv);
if (recv_eq->e_cloning_serial != sparp->sparp_sg->sg_cloning_serial)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): recv eq not cloned");
cloned_recvs [recv_ctr] = recv_eq->e_clone_idx;
}
END_DO_BOX_FAST_REV;
}
else
{
if (0 != BOX_ELEMENTS_0 (eq->e_receiver_idxs))
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): nonempty recv list without parent, maybe clone source is not detached");
cloned_eq->e_receiver_idxs = (ptrlong *)t_list (0);
}
}
if (NULL != orig->_.gp.filters)
tgt->_.gp.filters = sparp_treelist_full_clone_int (sparp, orig->_.gp.filters, orig);
tgt->_.gp.members = sparp_treelist_full_clone_int (sparp, orig->_.gp.members, orig);
if (NULL != orig->_.gp.subquery)
tgt->_.gp.subquery = sparp_tree_full_clone_int (sparp, orig->_.gp.subquery, orig);
if (NULL != orig->_.gp.options)
tgt->_.gp.options = sparp_treelist_full_clone_int (sparp, orig->_.gp.options, orig);
tgt->_.gp.selid = sparp_clone_id (sparp, orig->_.gp.selid);
for (eq_ctr = 0; eq_ctr < orig->_.gp.equiv_count; eq_ctr++)
{
sparp_equiv_t *eq;
sparp_equiv_t *cloned_eq;
eq_idx = orig->_.gp.equiv_indexes [eq_ctr];
cloned_eq_idx = tgt->_.gp.equiv_indexes [eq_ctr];
if (SPART_BAD_EQUIV_IDX == eq_idx)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): bad equiv idx in list of gp.equiv_indexes");
eq = SPARP_EQUIV (sparp, eq_idx);
cloned_eq = SPARP_EQUIV (sparp, cloned_eq_idx);
if (NULL != eq->e_subvalue_idxs)
{
int subv_ctr;
ptrlong *cloned_subs = (ptrlong *)t_box_copy ((caddr_t)(eq->e_subvalue_idxs));
cloned_eq->e_subvalue_idxs = cloned_subs;
DO_BOX_FAST_REV (ptrlong, subv, subv_ctr, cloned_subs)
{
sparp_equiv_t *subv_eq = SPARP_EQUIV (sparp, subv);
sparp_equiv_t *cloned_subv_eq;
if (subv_eq->e_cloning_serial != sparp->sparp_sg->sg_cloning_serial)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): subv eq not cloned");
cloned_subs [subv_ctr] = subv_eq->e_clone_idx;
cloned_subv_eq = SPARP_EQUIV (sparp, subv_eq->e_clone_idx);
if (NULL == subv_eq->e_receiver_idxs)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): subv has no receivers");
if (NULL == cloned_subv_eq->e_receiver_idxs)
{
int subv_recv_ctr;
ptrlong *cloned_sub_recvs = (ptrlong *)t_box_copy ((caddr_t)(subv_eq->e_receiver_idxs));
cloned_subv_eq->e_receiver_idxs = cloned_sub_recvs;
DO_BOX_FAST_REV (ptrlong, subv_recv, subv_recv_ctr, cloned_sub_recvs)
{
sparp_equiv_t *subv_recv_eq = SPARP_EQUIV (sparp, subv_recv);
if (subv_recv_eq->e_cloning_serial != sparp->sparp_sg->sg_cloning_serial)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): recv eq of subv eq not cloned");
cloned_sub_recvs [subv_recv_ctr] = subv_recv_eq->e_clone_idx;
}
END_DO_BOX_FAST_REV;
}
}
END_DO_BOX_FAST_REV;
}
#if 0 /* old clone, with preserved positions of variables */
cloned_eq->e_var_count = eq->e_var_count;
#else
if (cloned_eq->e_var_count > eq->e_var_count)
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): mismatching number of cloned variables");
#endif
cloned_eq->e_gspo_uses = eq->e_gspo_uses;
cloned_eq->e_nested_bindings = eq->e_nested_bindings;
cloned_eq->e_const_reads = eq->e_const_reads;
cloned_eq->e_optional_reads = eq->e_optional_reads;
cloned_eq->e_subquery_uses = eq->e_subquery_uses;
}
return tgt;
case SPAR_LIT: case SPAR_QNAME: /* case SPAR_QNAME_NS: */
return (SPART *)t_full_box_copy_tree ((caddr_t)orig);
case SPAR_TRIPLE:
if (NULL == parent_gp)
return sparp_tree_full_copy (sparp, orig, parent_gp);
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.triple.selid = sparp_clone_id (sparp, orig->_.triple.selid);
tgt->_.triple.tabid = sparp_clone_id (sparp, orig->_.triple.tabid);
for (fld_ctr = SPART_TRIPLE_FIELDS_COUNT; fld_ctr--; /*no step*/)
{
sparp_jso_validate_format (sparp, tgt->_.triple.native_formats[fld_ctr]);
tgt->_.triple.tr_fields[fld_ctr] = sparp_tree_full_clone_int (sparp, orig->_.triple.tr_fields[fld_ctr], parent_gp);
}
if (NULL != orig->_.triple.options)
tgt->_.triple.options = sparp_treelist_full_clone_int (sparp, orig->_.triple.options, parent_gp);
return tgt;
case SPAR_BUILT_IN_CALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.builtin.args = sparp_treelist_full_clone_int (sparp, orig->_.builtin.args, parent_gp);
return tgt;
case SPAR_FUNCALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.funcall.argtrees = sparp_treelist_full_clone_int (sparp, orig->_.funcall.argtrees, parent_gp);
return tgt;
case SPAR_REQ_TOP:
{
SPART *orig_pattern = orig->_.req_top.pattern;
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.req_top.pattern = sparp_tree_full_clone_int (sparp, orig->_.req_top.pattern, parent_gp); /* Should be before everything else to clone equivs */
tgt->_.req_top.retvals = sparp_treelist_full_clone_int (sparp, orig->_.req_top.retvals, orig_pattern);
tgt->_.req_top.orig_retvals = sparp_treelist_full_clone_int (sparp, orig->_.req_top.orig_retvals, orig_pattern);
tgt->_.req_top.expanded_orig_retvals = sparp_treelist_full_clone_int (sparp, orig->_.req_top.expanded_orig_retvals, orig_pattern);
/* !!! TBD something with retselid :) */
tgt->_.req_top.groupings = sparp_treelist_full_clone_int (sparp, orig->_.req_top.groupings, orig_pattern);
tgt->_.req_top.having = sparp_tree_full_clone_int (sparp, orig->_.req_top.having, orig_pattern);
tgt->_.req_top.order = sparp_treelist_full_clone_int (sparp, orig->_.req_top.order, orig_pattern);
return tgt;
}
case BOP_EQ: case SPAR_BOP_EQ: case BOP_NEQ:
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_SAME: case BOP_NSAME:
case BOP_PLUS: case BOP_MINUS: case BOP_TIMES: case BOP_DIV: case BOP_MOD:
case BOP_AND: case BOP_OR: case BOP_NOT:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.bin_exp.left = sparp_tree_full_clone_int (sparp, orig->_.bin_exp.left, parent_gp);
if (NULL != orig->_.bin_exp.right)
tgt->_.bin_exp.right = sparp_tree_full_clone_int (sparp, orig->_.bin_exp.right, parent_gp);
return tgt;
case ORDER_L:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.oby.expn = sparp_tree_full_clone_int (sparp, orig->_.oby.expn, parent_gp);
return tgt;
case SPAR_QM_SQL_FUNCALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
/*tgt->_.qm_sql_funcall.fname = t_box_copy (orig->_.qm_sql_funcall.fname);*/
tgt->_.qm_sql_funcall.fixed = (SPART **)t_box_copy ((caddr_t) orig->_.qm_sql_funcall.fixed);
DO_BOX_FAST_REV (SPART *, arg, arg_ctr, orig->_.qm_sql_funcall.fixed)
{
tgt->_.qm_sql_funcall.fixed[arg_ctr] = sparp_tree_full_clone_int (sparp, arg, parent_gp);
}
END_DO_BOX_FAST_REV;
tgt->_.qm_sql_funcall.named = (SPART **)t_box_copy ((caddr_t) orig->_.qm_sql_funcall.named);
DO_BOX_FAST_REV (SPART *, arg, arg_ctr, orig->_.qm_sql_funcall.named)
{
tgt->_.qm_sql_funcall.named[arg_ctr] = sparp_tree_full_clone_int (sparp, arg, parent_gp);
}
END_DO_BOX_FAST_REV;
return tgt;
case SPAR_LIST:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.list.items = sparp_treelist_full_clone_int (sparp, orig->_.list.items, parent_gp);
return tgt;
case SPAR_SERVICE_INV:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.sinv.own_idx = 0; /* will be set by spar_add_service_inv_to_sg() below */
/* endpoint, iri_params, syntax, storage_iri, in_list_implicit are not copied --- no need so far */
tgt->_.sinv.param_varnames = (caddr_t *)t_box_copy ((caddr_t)(orig->_.sinv.param_varnames));
tgt->_.sinv.rset_varnames = (caddr_t *)t_box_copy ((caddr_t)(orig->_.sinv.rset_varnames));
tgt->_.sinv.defines = sparp_treelist_full_copy (sparp, orig->_.sinv.defines, parent_gp);
spar_add_service_inv_to_sg (sparp, tgt);
return tgt;
case SPAR_DEFMACRO:
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): attempt of copying a macro definition");
return NULL;
case SPAR_MACROCALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
/*tgt->_.macrocall.mname = t_box_copy (orig->_.macrocall.mname);*/
tgt->_.macrocall.argtrees = sparp_treelist_full_clone_int (sparp, orig->_.macrocall.argtrees, parent_gp);
tgt->_.macrocall.context_graph = sparp_tree_full_clone_int (sparp, orig->_.macrocall.context_graph, parent_gp);
return tgt;
case SPAR_MACROPU:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
/*tgt->_.macropu.pname = t_box_copy (orig->_.macropu.pname);*/
return tgt;
/* Add more cases right above this line when introducing more SPAR_nnn constants */
default: break; /* No need to copy names and literals because we will never change them in-place. */
}
spar_internal_error (sparp, "sparp_" "tree_full_clone_int(): unsupported type of expression");
return NULL; /* to keep C compiler happy */
}
SPART **
sparp_treelist_full_clone_int (sparp_t *sparp, SPART **origs, SPART *parent_gp)
{
SPART **tgts = (SPART **)t_box_copy ((caddr_t) origs);
int ctr;
DO_BOX_FAST_REV (SPART *, org, ctr, origs)
{
tgts [ctr] = sparp_tree_full_clone_int (sparp, org, parent_gp);
}
END_DO_BOX_FAST_REV;
return tgts;
}
SPART **
sparp_treelist_full_clone (sparp_t *sparp, SPART **origs)
{
SPART **tgt;
sparp_equiv_audit_all (sparp, 0);
sparp_audit_mem (sparp);
sparp->sparp_sg->sg_cloning_serial++;
tgt = sparp_treelist_full_clone_int (sparp, origs, NULL);
sparp->sparp_sg->sg_cloning_serial++;
sparp_equiv_audit_all (sparp, 0);
sparp_audit_mem (sparp);
t_check_tree (tgt);
return tgt;
}
SPART *
sparp_gp_full_clone (sparp_t *sparp, SPART *gp)
{
SPART *tgt;
sparp_equiv_audit_all (sparp, 0);
sparp_audit_mem (sparp);
sparp->sparp_sg->sg_cloning_serial++;
tgt = sparp_tree_full_clone_int (sparp, gp, NULL);
sparp->sparp_sg->sg_cloning_serial++;
sparp_equiv_audit_all (sparp, 0);
sparp_audit_mem (sparp);
t_check_tree (tgt);
return tgt;
}
SPART *
sparp_tree_full_copy (sparp_t *sparp, const SPART *orig, const SPART *parent_gp)
{
int defctr, defcount, fld_ctr, eq_idx;
SPART *tgt;
switch (SPART_TYPE (orig))
{
case SPAR_ALIAS:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.alias.aname = (caddr_t)sparp_tree_full_copy (sparp, (SPART *)(orig->_.alias.aname), parent_gp); /* not t_box_copy, because it can be macropu */
tgt->_.alias.arg = sparp_tree_full_copy (sparp, orig->_.alias.arg, parent_gp);
return tgt;
case SPAR_BLANK_NODE_LABEL: case SPAR_VARIABLE:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
eq_idx = orig->_.var.equiv_idx;
tgt->_.var.vname = t_box_copy (orig->_.var.vname);
sparp_rvr_copy (sparp, &(tgt->_.var.rvr), (rdf_val_range_t *) &(orig->_.var.rvr));
return tgt;
case SPAR_GRAPH:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.graph.expn = sparp_tree_full_copy (sparp, orig->_.graph.expn, parent_gp);
return tgt;
case SPAR_GP:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.gp.members = sparp_treelist_full_copy (sparp, orig->_.gp.members, (SPART *) orig);
tgt->_.gp.filters = sparp_treelist_full_copy (sparp, orig->_.gp.filters, (SPART *) orig);
tgt->_.gp.subquery = sparp_tree_full_copy (sparp, orig->_.gp.subquery, (SPART *) orig);
tgt->_.gp.equiv_indexes = (ptrlong *)t_box_copy ((caddr_t)(orig->_.gp.equiv_indexes));
tgt->_.gp.options = sparp_treelist_full_copy (sparp, orig->_.gp.options, (SPART *) orig);
return tgt;
case SPAR_LIT: case SPAR_QNAME: /* case SPAR_QNAME_NS: */
return (SPART *)t_full_box_copy_tree ((caddr_t)orig);
case ORDER_L:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.oby.expn = sparp_tree_full_copy (sparp, orig->_.oby.expn, parent_gp);
return tgt;
case SPAR_TRIPLE:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
for (fld_ctr = SPART_TRIPLE_FIELDS_COUNT; fld_ctr--; /*no step*/)
{
sparp_jso_validate_format (sparp, tgt->_.triple.native_formats[fld_ctr]);
tgt->_.triple.tr_fields[fld_ctr] = sparp_tree_full_copy (sparp, orig->_.triple.tr_fields[fld_ctr], parent_gp);
}
tgt->_.triple.options = sparp_treelist_full_copy (sparp, orig->_.triple.options, parent_gp);
return tgt;
case SPAR_BUILT_IN_CALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.builtin.args = sparp_treelist_full_copy (sparp, orig->_.builtin.args, parent_gp);
return tgt;
case SPAR_FUNCALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.funcall.argtrees = sparp_treelist_full_copy (sparp, orig->_.funcall.argtrees, parent_gp);
return tgt;
case SPAR_REQ_TOP:
if (0 != sparp->sparp_sg->sg_equiv_count)
spar_internal_error (sparp, "sparp_tree_full_copy() is used to copy req_top with nonzero equiv_count");
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.req_top.retvals = sparp_treelist_full_copy (sparp, orig->_.req_top.retvals, parent_gp);
tgt->_.req_top.orig_retvals = sparp_treelist_full_copy (sparp, orig->_.req_top.orig_retvals, parent_gp);
tgt->_.req_top.expanded_orig_retvals = sparp_treelist_full_copy (sparp, orig->_.req_top.expanded_orig_retvals, parent_gp);
tgt->_.req_top.sources = sparp_treelist_full_copy (sparp, orig->_.req_top.sources, parent_gp);
tgt->_.req_top.pattern = sparp_tree_full_copy (sparp, orig->_.req_top.pattern, parent_gp);
tgt->_.req_top.groupings = sparp_treelist_full_copy (sparp, orig->_.req_top.groupings, parent_gp);
tgt->_.req_top.having = sparp_tree_full_copy (sparp, orig->_.req_top.having, parent_gp);
tgt->_.req_top.order = sparp_treelist_full_copy (sparp, orig->_.req_top.order, parent_gp);
tgt->_.req_top.limit = sparp_tree_full_copy (sparp, orig->_.req_top.limit, parent_gp);
tgt->_.req_top.offset = sparp_tree_full_copy (sparp, orig->_.req_top.offset, parent_gp);
return tgt;
case BOP_EQ: case SPAR_BOP_EQ: case BOP_NEQ:
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_SAME: case BOP_NSAME:
case BOP_PLUS: case BOP_MINUS: case BOP_TIMES: case BOP_DIV: case BOP_MOD:
case BOP_AND: case BOP_OR: case BOP_NOT:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.bin_exp.left = sparp_tree_full_copy (sparp, orig->_.bin_exp.left, parent_gp);
if (NULL != orig->_.bin_exp.right)
tgt->_.bin_exp.right = sparp_tree_full_copy (sparp, orig->_.bin_exp.right, parent_gp);
return tgt;
case SPAR_QM_SQL_FUNCALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.qm_sql_funcall.fname = t_box_copy (orig->_.qm_sql_funcall.fname);
tgt->_.qm_sql_funcall.fixed = sparp_treelist_full_copy (sparp, orig->_.qm_sql_funcall.fixed, parent_gp);
tgt->_.qm_sql_funcall.named = sparp_treelist_full_copy (sparp, orig->_.qm_sql_funcall.named, parent_gp);
return tgt;
case SPAR_LIST:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.list.items = sparp_treelist_full_copy (sparp, orig->_.list.items, parent_gp);
return tgt;
case SPAR_SERVICE_INV:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.sinv.iri_params = sparp_treelist_full_copy (sparp, orig->_.sinv.iri_params, parent_gp);
tgt->_.sinv.param_varnames = (caddr_t *)t_box_copy ((caddr_t)(orig->_.sinv.param_varnames));
tgt->_.sinv.rset_varnames = (caddr_t *)t_box_copy ((caddr_t)(orig->_.sinv.rset_varnames));
tgt->_.sinv.defines = (SPART **)t_box_copy ((caddr_t)(orig->_.sinv.defines));
defcount = BOX_ELEMENTS_0 (tgt->_.sinv.defines);
for (defctr = 1; defctr < defcount; defctr += 2)
{
SPART ***vals = (SPART ***)(t_box_copy ((caddr_t)(orig->_.sinv.defines[defctr])));
int valctr, valcount = BOX_ELEMENTS_0 (vals);
tgt->_.sinv.defines[defctr] = (void *)vals;
for (valctr = valcount; valctr--; /* no step */)
vals[valctr] = sparp_treelist_full_copy (sparp, vals[valctr], parent_gp);
}
tgt->_.sinv.sources = sparp_treelist_full_copy (sparp, orig->_.sinv.sources, parent_gp);
return tgt;
case SPAR_DEFMACRO:
spar_internal_error (sparp, "sparp_" "tree_full_copy(): should not copy macro defs");
case SPAR_MACROCALL:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
tgt->_.macrocall.mname = t_box_copy (orig->_.macrocall.mname);
tgt->_.macrocall.argtrees = sparp_treelist_full_copy (sparp, orig->_.macrocall.argtrees, parent_gp);
tgt->_.macrocall.context_graph = sparp_tree_full_copy (sparp, orig->_.macrocall.context_graph, parent_gp);
return tgt;
case SPAR_MACROPU:
tgt = (SPART *)t_box_copy ((caddr_t) orig);
return tgt;
/* Add more cases right above this line when introducing more SPAR_nnn constants */
default: break; /* No need to copy names and literals because we will never change them in-place. */
}
spar_internal_error (sparp, "sparp_" "tree_full_copy(): unsupported type of expression");
return NULL; /* to keep C compiler happy */
}
SPART **
sparp_treelist_full_copy (sparp_t *sparp, SPART **origs, const SPART *parent_gp)
{
SPART **tgts = (SPART **)t_box_copy ((caddr_t) origs);
int ctr;
DO_BOX_FAST_REV (SPART *, org, ctr, origs)
{
tgts [ctr] = sparp_tree_full_copy (sparp, org, parent_gp);
}
END_DO_BOX_FAST_REV;
return tgts;
}
SPART *
sparp_gp_detach_member (sparp_t *sparp, SPART *parent_gp, int member_idx, sparp_equiv_t ***touched_equivs_ptr)
{
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL != touched_equivs_ptr) ? &touched_equivs_set : NULL);
SPART **old_members = parent_gp->_.gp.members;
SPART *memb;
sparp_check_tree (parent_gp);
memb = sparp_gp_detach_member_int (sparp, parent_gp, member_idx, touched_equivs_set_ptr);
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
parent_gp->_.gp.members = (SPART **)t_list_remove_nth ((caddr_t)old_members, member_idx);
sparp_equiv_audit_all (sparp, 0);
return memb;
}
SPART **
sparp_gp_detach_all_members (sparp_t *sparp, SPART *parent_gp, sparp_equiv_t ***touched_equivs_ptr)
{
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL != touched_equivs_ptr) ? &touched_equivs_set : NULL);
SPART **old_members = parent_gp->_.gp.members;
int member_idx;
sparp_check_tree (parent_gp);
for (member_idx = BOX_ELEMENTS (old_members); member_idx--; /* no step */)
sparp_gp_detach_member_int (sparp, parent_gp, member_idx, touched_equivs_set_ptr);
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
parent_gp->_.gp.members = (SPART **)t_list(0);
sparp_equiv_audit_all (sparp, 0);
return old_members;
}
void
sparp_gp_attach_member_int (sparp_t *sparp, SPART *parent_gp, SPART *memb, dk_set_t *touched_equivs_set_ptr)
{
/*SPART **old_members = parent_gp->_.gp.members;*/
if (SPAR_GP == SPART_TYPE (memb))
{
int memb_eq_ctr;
SPARP_REVFOREACH_GP_EQUIV (sparp, memb, memb_eq_ctr, eq)
{
int varname_idx;
DO_BOX_FAST (caddr_t, varname, varname_idx, eq->e_varnames)
{
sparp_equiv_t *parent_eq;
parent_eq = sparp_equiv_get (sparp, parent_gp, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_INS_CLASS);
sparp_equiv_connect (sparp, parent_eq, eq, 1);
}
END_DO_BOX_FAST;
}
END_SPARP_REVFOREACH_GP_EQUIV;
}
else
{
int fld_ctr;
#ifdef DEBUG
if (SPAR_TRIPLE != SPART_TYPE (memb))
spar_internal_error (sparp, "sparp_" "gp_attach_member(): type of memb is neither SPAR_GP nor SPAR_TRIPLE");
#endif
memb->_.triple.selid = /*t_box_copy*/ (parent_gp->_.gp.selid);
for (fld_ctr = SPART_TRIPLE_FIELDS_COUNT; fld_ctr--; /*no step*/)
{
SPART *field = memb->_.triple.tr_fields [fld_ctr];
if (SPAR_IS_BLANK_OR_VAR(field))
{
sparp_equiv_t *parent_eq;
field->_.var.selid = memb->_.triple.selid;
parent_eq = sparp_equiv_get (sparp, parent_gp, field, SPARP_EQUIV_INS_VARIABLE | SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_ADD_GSPO_USE);
}
}
if (NULL != memb->_.triple.options)
{
sparp_set_options_selid_and_tabid (sparp, memb->_.triple.options, memb->_.triple.selid, memb->_.triple.tabid);
sparp_gp_trav_cu_in_options (sparp, parent_gp, memb, memb->_.triple.options, NULL);
}
}
}
void
sparp_gp_attach_member (sparp_t *sparp, SPART *parent_gp, SPART *memb, int insert_before_idx, sparp_equiv_t ***touched_equivs_ptr)
{
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL != touched_equivs_ptr) ? &touched_equivs_set : NULL);
SPART **old_members = parent_gp->_.gp.members;
sparp_check_tree (parent_gp);
sparp_check_tree (memb);
#ifdef DEBUG
if ((0 > insert_before_idx) || (BOX_ELEMENTS (parent_gp->_.gp.members) < insert_before_idx))
spar_internal_error (sparp, "sparp_" "gp_attach_member(): bad member_idx");
#endif
parent_gp->_.gp.members = (SPART **)t_list_insert_before_nth ((caddr_t)old_members, (caddr_t)memb, insert_before_idx);
sparp_gp_attach_member_int (sparp, parent_gp, memb, touched_equivs_set_ptr);
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
sparp_equiv_audit_all (sparp, 0);
}
void
sparp_gp_attach_many_members (sparp_t *sparp, SPART *parent_gp, SPART **new_members, int insert_before_idx, sparp_equiv_t ***touched_equivs_ptr)
{
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL != touched_equivs_ptr) ? &touched_equivs_set : NULL);
SPART **old_members = parent_gp->_.gp.members;
int ins_count = BOX_ELEMENTS (new_members);
int memb_ctr;
sparp_check_tree (parent_gp);
sparp_check_tree (new_members);
#ifdef DEBUG
if ((0 > insert_before_idx) || (BOX_ELEMENTS (parent_gp->_.gp.members) < insert_before_idx))
spar_internal_error (sparp, "sparp_" "gp_attach_member(): bad member_idx");
#endif
if (0 < ins_count)
{
parent_gp->_.gp.members = (SPART **)t_list_insert_many_before_nth ((caddr_t)old_members, (caddr_t *)new_members, ins_count, insert_before_idx);
for (memb_ctr = ins_count; memb_ctr--; /*no step*/)
sparp_gp_attach_member_int (sparp, parent_gp, new_members [memb_ctr], touched_equivs_set_ptr);
}
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
sparp_equiv_audit_all (sparp, 0);
}
int
sparp_gp_detach_filter_expn_in_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
if (SPAR_IS_BLANK_OR_VAR (curr))
{
dk_set_t *touched_equivs_set_ptr = (dk_set_t *)common_env;
int idx = curr->_.var.equiv_idx;
sparp_equiv_t *eq;
if (SPART_BAD_EQUIV_IDX == idx)
return 0;
eq = SPARP_EQUIV (sparp, idx);
if (NULL != touched_equivs_set_ptr)
if (0 > dk_set_position (touched_equivs_set_ptr[0], eq))
t_set_push (touched_equivs_set_ptr, eq);
sparp_equiv_remove_var (sparp, eq, curr);
eq->e_const_reads -= 1;
}
return 0;
}
int
sparp_gp_distinct_varnames_expn_in_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
if (SPAR_VARIABLE == SPART_TYPE (curr))
t_set_push_new_string ((dk_set_t *)common_env, curr->_.var.vname);
return 0;
}
int
sparp_gp_distinct_varnames_expn_subq_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
sparp_distinct_varnames_of_tree (sparp, curr->_.req_top.pattern, common_env);
return 0;
}
void
sparp_distinct_varnames_of_tree (sparp_t *sparp, SPART *tree, dk_set_t *acc)
{
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, tree, stss + 1, acc,
NULL, NULL,
sparp_gp_distinct_varnames_expn_in_cbk, NULL, sparp_gp_distinct_varnames_expn_subq_cbk,
NULL );
}
int
sparp_gp_detach_filter_expn_subq_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
dk_set_t distinct_varnames = NULL;
SPART *gp = sts_this->sts_ancestor_gp;
sparp_distinct_varnames_of_tree (sparp, curr->_.gp.subquery->_.req_top.pattern, &distinct_varnames);
DO_SET (caddr_t, varname, &distinct_varnames)
{
sparp_equiv_t *eq = sparp_equiv_get (sparp, gp, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES);
if (NULL != eq)
eq->e_optional_reads -= 1;
}
END_DO_SET();
return 0;
}
SPART *
sparp_gp_detach_filter (sparp_t *sparp, SPART *parent_gp, int filter_idx, sparp_equiv_t ***touched_equivs_ptr)
{
sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
SPART *filt;
SPART **old_filters = parent_gp->_.gp.filters;
int old_len = BOX_ELEMENTS (old_filters);
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL == touched_equivs_ptr) ? NULL : &touched_equivs_set);
#ifdef DEBUG
if ((0 > filter_idx) || (old_len <= filter_idx))
spar_internal_error (sparp, "sparp_" "gp_detach_filter(): bad filter_idx");
#endif
filt = old_filters [filter_idx];
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 = parent_gp;
sparp_gp_trav_int (sparp, filt, stss + 1, touched_equivs_set_ptr,
NULL, NULL,
sparp_gp_detach_filter_expn_in_cbk, NULL, sparp_gp_detach_filter_expn_subq_cbk,
NULL );
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
parent_gp->_.gp.filters = (SPART **)t_list_remove_nth ((caddr_t)old_filters, filter_idx);
if (filter_idx >= (old_len - parent_gp->_.gp.glued_filters_count))
parent_gp->_.gp.glued_filters_count -= 1;
sparp_equiv_audit_all (sparp, 0);
return filt;
}
SPART **
sparp_gp_detach_all_filters (sparp_t *sparp, SPART *parent_gp, sparp_equiv_t ***touched_equivs_ptr)
{
sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
SPART **filters = parent_gp->_.gp.filters;
int filt_ctr;
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL == touched_equivs_ptr) ? NULL : &touched_equivs_set);
if (0 != parent_gp->_.gp.glued_filters_count)
spar_internal_error (sparp, "sparp_" "gp_detach_all_filters(): optimization tries to break the semantics of LEFT OUTER JOIN for OPTIONAL clause");
DO_BOX_FAST_REV (SPART *, filt, filt_ctr, filters)
{
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 = parent_gp;
sparp_gp_trav_int (sparp, filt, stss + 1, touched_equivs_set_ptr,
NULL, NULL,
sparp_gp_detach_filter_expn_in_cbk, NULL, sparp_gp_detach_filter_expn_subq_cbk,
NULL );
}
END_DO_BOX_FAST_REV;
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
parent_gp->_.gp.filters = (SPART **)t_list (0);
sparp_equiv_audit_all (sparp, 0);
return filters;
}
int
sparp_gp_attach_filter_cbk (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
if (SPAR_IS_BLANK_OR_VAR (curr))
{
dk_set_t *touched_equivs_set_ptr = (dk_set_t *) common_env;
SPART *parent_gp;
sparp_equiv_t *eq;
int idx = curr->_.var.equiv_idx;
if (SPART_BAD_EQUIV_IDX != idx)
spar_internal_error (sparp, "sparp_" "gp_attach_filter_cbk(): attempt to attach a filter with used variable");
parent_gp = (SPART *)(sts_this[-1].sts_env);
curr->_.var.selid = t_box_copy (parent_gp->_.gp.selid);
eq = sparp_equiv_get (sparp, parent_gp, curr, SPARP_EQUIV_INS_CLASS | SPARP_EQUIV_INS_VARIABLE | SPARP_EQUIV_ADD_CONST_READ);
if (NULL != touched_equivs_set_ptr)
if (0 > dk_set_position (touched_equivs_set_ptr[0], eq))
t_set_push (touched_equivs_set_ptr, eq);
/*!!!TBD: the following is added to hide errors like 'Variable ':named_graphs' is used in subexpressions of the query but not assigned'.
These errors should be additionally checked when list of global variables is known, say in xsl:for-each-sparql implementation. */
eq->e_rvr.rvrRestrictions |= ((SPART_VARR_GLOBAL | SPART_VARR_EXPORTED) & curr->_.var.rvr.rvrRestrictions);
}
return 0;
}
void
sparp_gp_attach_filter (sparp_t *sparp, SPART *parent_gp, SPART *new_filt, int insert_before_idx, sparp_equiv_t ***touched_equivs_ptr)
{
sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
SPART **old_filters = parent_gp->_.gp.filters;
int old_len = BOX_ELEMENTS (old_filters);
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL == touched_equivs_ptr) ? NULL : &touched_equivs_set);
#ifdef DEBUG
if ((0 > insert_before_idx) || (old_len < insert_before_idx))
spar_internal_error (sparp, "sparp_" "gp_attach_filter(): bad insert_before_idx");
#endif
parent_gp->_.gp.filters = (SPART **)t_list_insert_before_nth ((caddr_t)old_filters, (caddr_t)new_filt, insert_before_idx);
if (insert_before_idx > (old_len - parent_gp->_.gp.glued_filters_count))
parent_gp->_.gp.glued_filters_count += 1;
memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
stss[0].sts_ofs_of_curr_in_array = -1;
stss[0].sts_env = parent_gp;
sparp_gp_trav_int (sparp, new_filt, stss + 1, touched_equivs_set_ptr,
NULL, NULL,
sparp_gp_attach_filter_cbk, NULL, NULL,
NULL );
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
sparp_equiv_audit_all (sparp, 0);
}
void
sparp_gp_attach_many_filters (sparp_t *sparp, SPART *parent_gp, SPART **new_filters, int insert_before_idx, sparp_equiv_t ***touched_equivs_ptr)
{
sparp_trav_state_t stss [SPARP_MAX_SYNTDEPTH+2];
SPART **old_filters = parent_gp->_.gp.filters;
int old_len = BOX_ELEMENTS (old_filters);
int filt_ctr, ins_count;
dk_set_t touched_equivs_set = NULL;
dk_set_t *touched_equivs_set_ptr = ((NULL == touched_equivs_ptr) ? NULL : &touched_equivs_set);
#ifdef DEBUG
if ((0 > insert_before_idx) || (old_len < insert_before_idx))
spar_internal_error (sparp, "sparp_" "gp_attach_filter(): bad insert_before_idx");
#endif
ins_count = BOX_ELEMENTS (new_filters);
if (0 == ins_count)
{
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)t_list (0);
return;
}
parent_gp->_.gp.filters = (SPART **)t_list_insert_many_before_nth ((caddr_t)old_filters, (caddr_t *)new_filters, ins_count, insert_before_idx);
if (insert_before_idx > (old_len - parent_gp->_.gp.glued_filters_count))
parent_gp->_.gp.glued_filters_count += ins_count;
memset (stss, 0, sizeof (sparp_trav_state_t) * (SPARP_MAX_SYNTDEPTH+2));
stss[0].sts_ofs_of_curr_in_array = -1;
stss[0].sts_env = parent_gp;
for (filt_ctr = ins_count; filt_ctr--; /*no step*/)
sparp_gp_trav_int (sparp, new_filters [filt_ctr], stss + 1, touched_equivs_set_ptr,
NULL, NULL,
sparp_gp_attach_filter_cbk, NULL, NULL,
NULL );
if (NULL != touched_equivs_ptr)
touched_equivs_ptr[0] = (sparp_equiv_t **)(t_revlist_to_array (touched_equivs_set));
sparp_equiv_audit_all (sparp, 0);
}
int
sparp_gp_trav_gp_deprecate_expn_subq (sparp_t *sparp, SPART *curr, sparp_trav_state_t *sts_this, void *common_env)
{
sparp_gp_deprecate (sparp, curr, 1);
return 0;
}
void
sparp_gp_tighten_by_eq_replaced_filters (sparp_t *sparp, SPART *dest, SPART *orig, int remove_origin)
{
int eq_ctr;
SPARP_FOREACH_GP_EQUIV (sparp, orig, eq_ctr, orig_eq)
{
int varname_ctr;
if (!orig_eq->e_replaces_filter)
continue;
if (NULL != dest)
{
DO_BOX_FAST (caddr_t, varname, varname_ctr, orig_eq->e_varnames)
{
sparp_equiv_t *dest_eq = sparp_equiv_get (sparp, dest, (SPART *)varname, SPARP_EQUIV_GET_NAMESAKES | SPARP_EQUIV_INS_CLASS);
sparp_equiv_tighten (sparp, dest_eq, &(orig_eq->e_rvr), orig_eq->e_replaces_filter);
dest_eq->e_replaces_filter |= orig_eq->e_replaces_filter;
}
END_DO_BOX_FAST;
}
if (remove_origin)
orig_eq->e_replaces_filter = 0;
}
END_SPARP_FOREACH_GP_EQUIV;
}
void
sparp_gp_deprecate (sparp_t *sparp, SPART *gp, int suppress_eq_check)
{
int eq_ctr, memb_ctr, filt_ctr;
#ifdef DEBUG
if (SPART_BAD_GP_SUBTYPE == gp->_.gp.subtype)
spar_internal_error (sparp, "sparp_" "gp_deprecate(): gp re-deprecation");
sparp_equiv_audit_gp (sparp, gp, 0, NULL);
#endif
SPARP_FOREACH_GP_EQUIV (sparp, gp, eq_ctr, eq)
{
if (eq->e_deprecated)
spar_internal_error (sparp, "sparp_" "gp_deprecate(): equiv re-deprecation");
if (eq->e_replaces_filter && !suppress_eq_check)
spar_internal_error (sparp, "sparp_" "gp_deprecate(): equiv replaces filter but under deprecation");
eq->e_deprecated = 1;
}
END_SPARP_FOREACH_GP_EQUIV;
DO_BOX_FAST (SPART *, memb, memb_ctr, gp->_.gp.members)
{
if (SPAR_GP == memb->type)
sparp_gp_deprecate (sparp, memb, 1);
}
END_DO_BOX_FAST;
if (SELECT_L == gp->_.gp.subtype)
sparp_req_top_deprecate (sparp, gp->_.gp.subquery);
DO_BOX_FAST (SPART *, filt, filt_ctr, gp->_.gp.members)
{
sparp_trav_state_t fake_stack;
sparp_gp_trav_int (sparp, filt, &fake_stack, NULL,
NULL, NULL,
NULL, NULL, sparp_gp_trav_gp_deprecate_expn_subq,
NULL );
}
END_DO_BOX_FAST;
gp->_.gp.subtype = SPART_BAD_GP_SUBTYPE;
}
void
sparp_req_top_deprecate (sparp_t *sparp, SPART *top)
{
sparp_trav_state_t fake_stack;
sparp_gp_deprecate (sparp, top->_.req_top.pattern, 1);
sparp_trav_out_clauses_int (sparp, top, &fake_stack, NULL,
NULL, NULL,
NULL, NULL, sparp_gp_trav_gp_deprecate_expn_subq,
NULL );
}
/* MISC. SPARP_FIND_XXX FUNCTIONS */
SPART *
sparp_find_gp_by_alias_int (sparp_t *sparp, SPART *gp, caddr_t alias)
{
int ctr;
if (SPAR_GP != SPART_TYPE (gp))
return NULL;
if (!strcmp (gp->_.gp.selid, alias))
return gp;
for (ctr = BOX_ELEMENTS_INT_0 (gp->_.gp.members); ctr--; /*no step*/)
{
SPART *res = sparp_find_gp_by_alias_int (sparp, gp->_.gp.members[ctr], alias);
if (NULL != res)
return res;
}
return NULL;
}
SPART *
sparp_find_gp_by_alias (sparp_t *sparp, caddr_t alias)
{
do {
SPART *res = sparp_find_gp_by_alias_int (sparp, sparp->sparp_expr->_.req_top.pattern, alias);
if (NULL != res)
return res;
sparp = sparp->sparp_parent_sparp;
} while (NULL != sparp);
return NULL;
}
SPART *
sparp_find_triple_of_var_or_retval (sparp_t *sparp, SPART *gp, SPART *var, int need_strong_match)
{
int ctr;
SPART **members;
ptrlong tr_idx;
if (NULL == var->_.var.tabid)
return NULL;
if (NULL == gp)
{
gp = sparp_find_gp_by_alias (sparp, var->_.var.selid);
if (NULL == gp)
return NULL;
}
members = gp->_.gp.members;
tr_idx = var->_.var.tr_idx;
for (ctr = BOX_ELEMENTS_0 (members); ctr--; /*no step*/)
{
SPART *memb = members[ctr];
SPART *fld;
if (SPAR_TRIPLE != SPART_TYPE (memb))
continue;
if (strcmp (memb->_.triple.tabid, var->_.var.tabid))
continue;
if (tr_idx < SPART_TRIPLE_FIELDS_COUNT)
{
fld = memb->_.triple.tr_fields[tr_idx];
if (need_strong_match)
{
if (fld == var)
return memb;
}
else
{
if (SPAR_IS_BLANK_OR_VAR (fld) && !strcmp (fld->_.var.vname, var->_.var.vname))
return memb;
}
}
else
{
fld = sparp_get_option (sparp, memb->_.triple.options, tr_idx);
if (need_strong_match)
{
if (fld == var)
return memb;
}
else
{
if (SPAR_IS_BLANK_OR_VAR (fld) && !strcmp (fld->_.var.vname, var->_.var.vname))
return memb;
}
}
}
if (need_strong_match)
spar_internal_error (sparp, "sparp_" "find_triple_of_var_or_retval(): triple not found for strong match");
return NULL;
}
qm_value_t *
sparp_find_qmv_of_var_or_retval (sparp_t *sparp, SPART *var_triple, SPART *gp, SPART *var)
{
int tr_idx = var->_.var.tr_idx;
quad_map_t *qm;
qm_value_t *qmv;
if (SPAR_BINDINGS_INV == gp->type)
spar_internal_error (sparp, "sparp_" "find_qmv_of_var_or_retval(): call for binding invocation");
if (tr_idx >= SPART_TRIPLE_FIELDS_COUNT)
spar_internal_error (sparp, "sparp_" "find_qmv_of_var_or_retval(): side effect var passed");
if (NULL == var_triple)
{
switch (SPART_TYPE (var))
{
case SPAR_BLANK_NODE_LABEL:
case SPAR_VARIABLE:
var_triple = sparp_find_triple_of_var_or_retval (sparp, gp, var, 1);
break;
case SPAR_RETVAL:
var_triple = sparp_find_triple_of_var_or_retval (sparp, gp, var, 0);
break;
default:
spar_internal_error (sparp, "sparp_" "find_qmv_of_var_or_retval(): var expected");
break;
}
if (NULL == var_triple)
spar_internal_error (sparp, "sparp_" "find_qmv_of_var_or_retval(): can't find triple");
}
qm = var_triple->_.triple.tc_list[0]->tc_qm;
qmv = JSO_FIELD_ACCESS(qm_value_t *, qm, qm_field_map_offsets[tr_idx])[0];
return qmv;
}
SPART *sparp_find_gp_by_eq_idx_int (sparp_t *sparp, SPART *gp, ptrlong eq_idx)
{
int ctr;
if (SPAR_GP != SPART_TYPE (gp))
return NULL;
for (ctr = gp->_.gp.equiv_count; ctr--; /*no step*/)
{
if (gp->_.gp.equiv_indexes[ctr] == eq_idx)
return gp;
}
for (ctr = BOX_ELEMENTS_INT_0 (gp->_.gp.members); ctr--; /*no step*/)
{
SPART *res = sparp_find_gp_by_eq_idx_int (sparp, gp->_.gp.members[ctr], eq_idx);
if (NULL != res)
return res;
}
if (SELECT_L == gp->_.gp.subtype)
return sparp_find_gp_by_eq_idx_int (sparp, gp->_.gp.subquery->_.req_top.pattern, eq_idx);
return 0;
}
SPART *sparp_find_gp_by_eq_idx (sparp_t *sparp, ptrlong eq_idx)
{
#ifdef DEBUG
if (SPART_BAD_EQUIV_IDX == eq_idx)
spar_internal_error (sparp, "sparp_" "find_gp_by_eq_idx(): bad eq_idx");
if (eq_idx >= sparp->sparp_sg->sg_equiv_count)
spar_internal_error (sparp, "sparp_" "find_gp_by_eq_idx(): eq_idx is too big");
if (NULL == sparp->sparp_sg->sg_equivs [eq_idx])
spar_internal_error (sparp, "sparp_" "find_gp_by_eq_idx(): eq_idx of merged and disabled equiv");
#endif
do {
SPART *res = sparp_find_gp_by_eq_idx_int (sparp, sparp->sparp_expr->_.req_top.pattern, eq_idx);
if (NULL != res)
return res;
sparp = sparp->sparp_parent_sparp;
} while (NULL != sparp);
return NULL;
}
int
sparp_find_language_dialect_by_service (sparp_t *sparp, SPART *service_expn)
{
caddr_t service_iri = SPAR_LIT_OR_QNAME_VAL (service_expn);
caddr_t *lang_strs;
int res;
if ((DV_STRING != DV_TYPE_OF (service_iri)) && (DV_UNAME != DV_TYPE_OF (service_iri)))
return 0;
lang_strs = jso_triple_get_objs ((caddr_t *)(sparp->sparp_sparqre->sparqre_qi), service_iri, uname_virtrdf_ns_uri_dialect);
if (1 != BOX_ELEMENTS_0 (lang_strs))
return 0;
if (1 != sscanf (lang_strs[0], "%08x", &res))
return 0;
if (0 > res)
return 0;
return res;
}
quad_storage_t *
sparp_find_storage_by_name (ccaddr_t name)
{
jso_class_descr_t *quad_storage_cd;
jso_rtti_t *ds_rtti;
if (NULL == name)
name = uname_virtrdf_ns_uri_DefaultQuadStorage;
if ('\0' == name[0])
return NULL;
jso_get_cd_and_rtti (
uname_virtrdf_ns_uri_QuadStorage,
name,
&quad_storage_cd, &ds_rtti, 1 );
if ((NULL != ds_rtti) && (JSO_STATUS_LOADED == ds_rtti->jrtti_status))
return (quad_storage_t *)(ds_rtti->jrtti_self);
return NULL;
}
quad_map_t *
sparp_find_quad_map_by_name (ccaddr_t name)
{
jso_class_descr_t *quad_map_cd;
jso_rtti_t *ds_rtti;
jso_get_cd_and_rtti (
uname_virtrdf_ns_uri_QuadMap,
name,
&quad_map_cd, &ds_rtti, 1 );
if ((NULL != ds_rtti) && (JSO_STATUS_LOADED == ds_rtti->jrtti_status))
return (quad_map_t *)(ds_rtti->jrtti_self);
return NULL;
}
SPART *
sparp_find_origin_of_external_var (sparp_t *sparp, SPART *var, int find_exact_specimen)
{
sparp_equiv_t *eq, *esrc, *esub_res_eq = NULL;
SPART *esub_res_gp = NULL, *esub_res = NULL;
SPART *rv;
int vctr, subv_ctr;
#ifdef DEBUG
if (!(SPART_VARR_EXTERNAL & var->_.var.rvr.rvrRestrictions))
spar_internal_error (sparp, "sparp_" "find_origin_of_external_var(): non-external variable as argument");
#endif
eq = SPARP_EQUIV(sparp, var->_.var.equiv_idx);
if (SPART_BAD_EQUIV_IDX == eq->e_external_src_idx)
spar_internal_error (sparp, "sparp_" "find_origin_of_external_var(): bad e_external_src_idx");
esrc = SPARP_EQUIV(sparp, eq->e_external_src_idx);
while (SPART_BAD_EQUIV_IDX != esrc->e_merge_dest_idx)
{
sparp_equiv_t *merged_esrc = SPARP_EQUIV(sparp, esrc->e_merge_dest_idx);
esrc = merged_esrc;
}
if (SPAR_BINDINGS_INV == esrc->e_gp->type) /* An external variable may come from bindings invocation instead of a GP. Binding var is the only choice then. */
return esrc->e_vars[0];
if (UNION_L == esrc->e_gp->_.gp.subtype) /* No one specimen from (one branch of) union can reliably represent all cases a union can produce. */
{
esub_res_eq = esrc;
esub_res_gp = esrc->e_gp;
goto make_rv; /* see below */
}
/* The best origin is triple pattern right in the GP because this increases the chance that SQL optimizer will find a good place for some condition on variable from subquery */
for (vctr = esrc->e_var_count; vctr--; /*no step*/)
{
SPART *source = esrc->e_vars[vctr];
if ((NULL != source->_.var.tabid) && !strcmp (source->_.var.vname, var->_.var.vname))
return source;
}
#if 0
/* If nothing really good is found then let's find any appropriate item. */
for (vctr = esrc->e_var_count; vctr--; /*no step*/)
{
SPART *source = esrc->e_vars[vctr];
if (!strcmp (source->_.var.vname, var->_.var.vname))
return source;
}
#endif
/* If nothing is found then a fake variable should be created. This is for cases like ?friend at top GP of query
<code>
sparql select ((select count(1) where { ?friend <knows> ?z }))
where {{select * where { <me> <knows> ?friend }} option (transitive...)}.
</code>
*/
DO_BOX_FAST (ptrlong, subeq_idx, subv_ctr, esrc->e_subvalue_idxs)
{
sparp_equiv_t *esub_eq = SPARP_EQUIV (sparp, subeq_idx);
SPART *esub_gp = esub_eq->e_gp;
if ((OPTIONAL_L == esub_gp->_.gp.subtype) && (NULL != esub_res_gp) && (OPTIONAL_L != esub_res_gp->_.gp.subtype))
continue; /* There is a better variant already */
for (vctr = esub_eq->e_var_count; vctr--; /*no step*/)
{
SPART *source = esub_eq->e_vars[vctr];
if (strcmp (source->_.var.vname, var->_.var.vname))
continue;
if ((NULL == source->_.var.tabid) && (NULL != esub_res) && (NULL != esub_res->_.var.tabid))
continue;
esub_res_eq = esub_eq;
esub_res_gp = esub_gp;
esub_res = source;
}
}
END_DO_BOX_FAST;
if (NULL != esub_res)
{
if (find_exact_specimen)
return esub_res;
goto make_rv; /* see below */
}
DO_BOX_FAST (ptrlong, subeq_idx, subv_ctr, esrc->e_subvalue_idxs)
{
sparp_equiv_t *esub_eq = SPARP_EQUIV (sparp, subeq_idx);
SPART *esub_gp = esub_eq->e_gp;
if (SELECT_L != esub_gp->_.gp.subtype)
continue;
if (esub_eq == eq) /* Are we back to the starting point of the search? No good, ignoring. */
continue;
esub_res_eq = esub_eq;
esub_res_gp = esub_gp;
goto make_rv; /* see below */
}
END_DO_BOX_FAST;
spar_internal_error (sparp, "sparp_" "find_origin_of_external_var(): external source equiv is found, external source var is not");
make_rv:
rv = (SPART *)t_alloc_box (sizeof (SPART), DV_ARRAY_OF_POINTER);
rv->type = SPAR_RETVAL;
rv->_.retval.equiv_idx = esub_res_eq->e_own_idx;
rv->_.retval.gp = esub_res_gp;
memcpy (&(rv->_.retval.rvr), &(esub_res_eq->e_rvr), sizeof (rdf_val_range_t));
rv->_.retval.selid = esub_res_gp->_.gp.selid;
rv->_.retval.vname = var->_.var.vname;
return rv;
}
int
sparp_find_sinv_rset_pos_of_varname (sparp_t *sparp, SPART *service_gp, caddr_t e_varname)
{
int pos;
sparp_equiv_t *e_eq;
SPART *sinv = sparp_get_option (sparp, service_gp->_.gp.options, SPAR_SERVICE_INV);
/* An optimistic search first: */
DO_BOX_FAST_REV (caddr_t, ret_vname, pos, sinv->_.sinv.rset_varnames)
{
if (!strcmp (e_varname, ret_vname))
return pos;
}
END_DO_BOX_FAST_REV;
/* In order to avoid bug 14730, now it's time to check for appropriate synonyms */
e_eq = sparp_equiv_get (sparp, service_gp, (SPART *)e_varname, SPARP_EQUIV_GET_NAMESAKES);
if (NULL != e_eq)
{
int vnamectr;
DO_BOX_FAST_REV (caddr_t, e_eq_varname, vnamectr, e_eq->e_varnames)
{
DO_BOX_FAST_REV (caddr_t, ret_vname, pos, sinv->_.sinv.rset_varnames)
{
if (!strcmp (e_eq_varname, ret_vname))
return pos;
}
END_DO_BOX_FAST_REV;
}
END_DO_BOX_FAST_REV;
}
return -1;
}
SPART *
sparp_find_subexpn_in_retlist (sparp_t *sparp, ccaddr_t varname, SPART **retvals, int return_alias)
{
int retvalctr;
DO_BOX_FAST (SPART *, retval, retvalctr, retvals)
{
switch (SPART_TYPE (retval))
{
case SPAR_ALIAS:
if (!strcmp (retval->_.alias.aname, varname))
return (return_alias ? retval : retval->_.alias.arg);
break;
case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
if (!strcmp (retval->_.var.vname, varname))
return retval;
break;
}
}
END_DO_BOX_FAST;
return NULL;
}
int
sparp_subexpn_position1_in_retlist (sparp_t *sparp, ccaddr_t varname, SPART **retvals)
{
int retvalctr;
DO_BOX_FAST (SPART *, retval, retvalctr, retvals)
{
switch (SPART_TYPE (retval))
{
case SPAR_ALIAS:
if (!strcmp (retval->_.alias.aname, varname))
return 1+retvalctr;
break;
case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
if (!strcmp (retval->_.var.vname, varname))
return 1+retvalctr;
break;
}
}
END_DO_BOX_FAST;
return 0;
}
/* MISCELLANIA */
SPART **
sparp_get_options_of_tree (sparp_t *sparp, SPART *tree)
{
switch (SPART_TYPE (tree))
{
case SPAR_GP: return tree->_.gp.options;
case SPAR_TRIPLE: return tree->_.triple.options;
}
return NULL;
}
void
sparp_validate_options_of_tree (sparp_t *sparp, SPART *tree, SPART **options)
{
int ttype = SPART_TYPE (tree);
int has_ft = 0;
int has_geo = 0;
int has_inference = 0;
int has_transitive = 0;
int needs_transitive = 0;
int has_service = 0;
SPART **subq_orig_retvals = NULL;
SPART **in_list = NULL;
SPART **out_list = NULL;
ptrlong direction = 0;
int idx;
if (NULL == options)
return;
if ((SPAR_GP == ttype) && (SELECT_L == tree->_.gp.subtype))
subq_orig_retvals = tree->_.gp.subquery->_.req_top.orig_retvals;
for (idx = BOX_ELEMENTS_0 (options) - 2; idx >= 0; idx -= 2)
{
ptrlong key = ((ptrlong)(options [idx]));
SPART *val = options [idx+1];
switch (key)
{
case INFERENCE_L: has_inference = 1; continue;
case OFFBAND_L: case SCORE_L: case SCORE_LIMIT_L: has_ft = 1; continue;
case IFP_L: 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: has_inference = 1; continue;
case TABLE_OPTION_L: continue;
case TRANSITIVE_L: has_transitive = 1; continue;
case T_CYCLES_ONLY_L:
case T_DISTINCT_L:
case T_END_FLAG_L:
case T_EXISTS_L:
case T_MAX_L:
case T_MIN_L:
case T_NO_CYCLES_L:
case T_NO_ORDER_L:
case T_SHORTEST_ONLY_L:
needs_transitive++; continue;
case T_DIRECTION_L: needs_transitive++;
direction = (ptrlong)(val);
if ((direction > 3) || (direction < 1))
spar_error (sparp, "The value of T_DIRECTION option should be an integer value 1, 2 or 3");
continue;
case T_IN_L: case T_OUT_L:
{
int v_ctr;
if (NULL == subq_orig_retvals)
spar_error (sparp, "T_IN and T_OUT options can be used only for SELECT subquery because they should refer to result-set of a subquery");
((T_IN_L == key) ? &in_list : &out_list)[0] = val->_.list.items;
DO_BOX_FAST (SPART *, v, v_ctr, val->_.list.items)
{
int pos1_ret;
SPART *ret;
if (SPART_VARNAME_IS_GLOB(v->_.var.vname))
spar_error (sparp, "Global variable ?%.100s can not be used in %s option",
v->_.var.vname, ((T_IN_L == key) ? "T_IN" : "T_OUT") );
pos1_ret = sparp_subexpn_position1_in_retlist (sparp, v->_.var.vname, subq_orig_retvals);
if (0 == pos1_ret)
spar_error (sparp, "Variable ?%.100s is used in %s option but not in the result-set of a subquery",
v->_.var.vname, ((T_IN_L == key) ? "T_IN" : "T_OUT") );
ret = subq_orig_retvals [pos1_ret-1];
if (SPAR_ALIAS != SPART_TYPE (ret))
subq_orig_retvals [pos1_ret-1] = spartlist (sparp, 4, SPAR_ALIAS, ret, v->_.var.vname, SSG_VALMODE_AUTO);
}
END_DO_BOX_FAST;
continue;
}
case T_STEP_L:
{
SPART *arg = val->_.alias.arg;
if (SPAR_VARIABLE == SPART_TYPE (arg))
{
caddr_t vname = arg->_.var.vname;
if (NULL == sparp_find_subexpn_in_retlist (sparp, vname, subq_orig_retvals, 1))
spar_error (sparp, "Variable ?%.100s is used in T_STEP option but not in the result-set of a subquery",
vname );
}
else
{
if (strcmp ((caddr_t)arg, "path_id") && strcmp ((caddr_t)arg, "step_no"))
spar_error (sparp, "T_STEP option support only \"path_id\" and \"step_no\" output values, not \"%.100s\"",
(caddr_t)arg );
}
if (sparp_subexpn_position1_in_retlist (sparp, val->_.alias.aname, subq_orig_retvals))
spar_error (sparp, "The alias ?%.100s of T_STEP option conflicts with same name in the result-set of a subquery",
(caddr_t)(val->_.alias.aname) );
continue;
}
case SERVICE_L:
has_service = 1;
continue;
default: spar_internal_error (sparp, "sparp_" "validate_options_of_tree(): unsupported option");
}
}
switch (ttype)
{
case SPAR_GP:
if (has_inference)
spar_error (sparp, "Inference options can be specified only for plain triple patterns, not for group patterns");
if (has_ft || has_geo)
spar_error (sparp, "%s options can be specified only for triple patterns with special predicates, not for group patterns", (has_ft ? "Free-text" : "Spatial"));
if (needs_transitive && !has_transitive)
spar_error (sparp, "Transitive-specific options are used without TRANSITIVE option");
break;
case SPAR_TRIPLE:
if (needs_transitive || has_transitive)
spar_error (sparp, "Transitive-specific options can be specified only for group patterns, not for triple patterns");
if (has_ft || has_geo)
spar_error (sparp, "%s options can be specified only for triple patterns with special predicates, not for plain patterns", (has_ft ? "Free-text" : "Spatial"));
if (has_service)
spar_internal_error (sparp, "Triple pattern has SERVICE invocation options, probably due to invalid optimization");
break;
default:
if (has_inference)
spar_error (sparp, "Inference options can be specified only for plain triple patterns");
if (needs_transitive || has_transitive)
spar_error (sparp, "Transitive-specific options can be specified only for group patterns");
if (has_service)
spar_internal_error (sparp, "misplaced SERVICE invocation options");
break;
}
if (has_transitive)
{
if (NULL == in_list)
spar_error (sparp, "TRANSITIVE option require T_IN option as well");
if (NULL == out_list)
spar_error (sparp, "TRANSITIVE option require T_OUT option as well");
if (BOX_ELEMENTS (in_list) != BOX_ELEMENTS (out_list))
spar_error (sparp, "Mismatch in length of T_IN and T_OUT lists of names");
}
if (has_service && (has_inference || has_transitive || needs_transitive ))
spar_error (sparp, "SERVICE invocation can not have inference or transitivity options, consider using nested groups");
}
SPART *
sparp_get_option (sparp_t *sparp, SPART **options, ptrlong key)
{
int idx;
for (idx = BOX_ELEMENTS_0 (options) - 2; idx >= 0; idx -= 2)
{
if (((ptrlong)(options [idx])) == key)
return options [idx+1];
}
return (SPART *)NULL;
}
SPART *
sparp_set_option (sparp_t *sparp, SPART ***options_ptr, ptrlong key, SPART *value, ptrlong mode)
{
SPART **options = options_ptr[0];
int idx;
for (idx = BOX_ELEMENTS_0 (options) - 2; idx >= 0; idx -= 2)
{
if (((ptrlong)(options [idx])) != key)
continue;
switch (mode)
{
case SPARP_SET_OPTION_NEW:
spar_internal_error (sparp, "SPARP_SET_OPTION_NEW with existing option");
return NULL;
case SPARP_SET_OPTION_REPLACING:
options [idx+1] = value;
return options [idx+1];
case SPARP_SET_OPTION_APPEND1:
if (SPAR_LIST != SPART_TYPE (options [idx+1]))
spar_internal_error (sparp, "SPARP_SET_OPTION_APPEND1 with existing option that is not SPAR_LIST");
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (value))
spar_internal_error (sparp, "SPARP_SET_OPTION_APPEND1 with non-array value");
options [idx+1]->_.list.items = (SPART **)t_list_concat_tail ((caddr_t)(options [idx+1]->_.list.items), 1, (caddr_t)value);
return options [idx+1];
default: spar_internal_error (sparp, "sparp_set_option(): bad mode");
}
}
switch (mode)
{
case SPARP_SET_OPTION_NEW: break;
case SPARP_SET_OPTION_REPLACING: break;
case SPARP_SET_OPTION_APPEND1:
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (value))
spar_internal_error (sparp, "SPARP_SET_OPTION_APPEND1 with non-array value");
value = spartlist (sparp, 2, SPAR_LIST, value);
return NULL;
default: spar_internal_error (sparp, "sparp_set_option(): bad mode");
}
options_ptr[0] = (SPART **)t_list_concat_tail ((caddr_t)options, 2, (ptrlong)key, value);
return value;
}
caddr_t
spar_var_name_of_ret_column (SPART *tree)
{
switch (DV_TYPE_OF (tree))
{
case DV_ARRAY_OF_POINTER:
switch (tree->type)
{
case SPAR_ALIAS: return spar_var_name_of_ret_column (tree->_.alias.arg);
case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL: return tree->_.var.vname;
}
return NULL;
/*
case DV_STRING: case DV_UNAME:
return (ccaddr_t)tree;
*/
}
return NULL;
}
caddr_t
spar_alias_name_of_ret_column (SPART *tree)
{
if ((DV_ARRAY_OF_POINTER == DV_TYPE_OF (tree)) &&
(SPAR_ALIAS == tree->type) )
return tree->_.alias.aname;
return spar_var_name_of_ret_column (tree);
}
int
spar_plain_const_value_of_tree (SPART *tree, ccaddr_t *cval_ret)
{
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (tree))
{
cval_ret[0] = (caddr_t)tree;
return SPAR_LIT;
}
switch (tree->type)
{
case SPAR_LIT:
if ((NULL != tree->_.lit.datatype) || (NULL != tree->_.lit.language))
break;
cval_ret[0] = tree->_.lit.val;
return SPAR_LIT;
case SPAR_QNAME:
cval_ret[0] = tree->_.qname.val;
return SPAR_QNAME;
case SPAR_VARIABLE: case SPAR_BLANK_NODE_LABEL:
if (!(SPART_VARR_FIXED & tree->_.var.rvr.rvrRestrictions))
break;
if (SPART_VARR_IS_IRI & tree->_.var.rvr.rvrRestrictions)
{
cval_ret[0] = tree->_.var.rvr.rvrFixedValue;
return SPAR_QNAME;
}
if (SPART_VARR_IS_LIT & tree->_.var.rvr.rvrRestrictions)
{
cval_ret[0] = tree->_.var.rvr.rvrFixedValue;
return SPAR_LIT;
}
}
cval_ret[0] = NULL;
return 0;
}
/* DEBUGGING */
const char *
spart_dump_opname (ptrlong opname, int is_op)
{
if (is_op)
switch (opname)
{
case BOP_AND: return "boolean operation 'AND'";
case BOP_OR: return "boolean operation 'OR'";
case BOP_NOT: return "boolean operation 'NOT'";
case BOP_EQ: return "boolean operation '='";
case SPAR_BOP_EQ: return "special equality";
case BOP_NEQ: return "boolean operation '!='";
case BOP_LT: return "boolean operation '<'";
case BOP_LTE: return "boolean operation '<='";
case BOP_GT: return "boolean operation '>'";
case BOP_GTE: return "boolean operation '>='";
/*case BOP_LIKE: Like is built-in in SPARQL, not a BOP! return "boolean operation 'like'"; */
case BOP_SAME: return "boolean operation '=='";
case BOP_NSAME: return "boolean operation '!=='";
case BOP_PLUS: return "arithmetic operation '+'";
case BOP_MINUS: return "arithmetic operation '-'";
case BOP_TIMES: return "arithmetic operation '*'";
case BOP_DIV: return "arithmetic operation 'div'";
case BOP_MOD: return "arithmetic operation 'mod'";
}
switch (opname)
{
case _LBRA: return "quad mapping parent group name";
case ASC_L: return "ascending order";
case ASK_L: return "ASK result-mode";
case BOUND_L: return "BOUND builtin";
case CONSTRUCT_L: return "CONSTRUCT result-mode";
case CREATE_L: return "quad mapping name";
case DATATYPE_L: return "DATATYPE builtin";
case DEFAULT_L: return "default graph context";
case DEFMACRO_L: return "DEFMACRO";
case DESC_L: return "descending";
case DESCRIBE_L: return "DESCRIBE result-mode";
case DISTINCT_L: return "SELECT DISTINCT result-mode";
case false_L: return "false boolean";
case FILTER_L: return "FILTER";
/* case FROM_L: return "FROM"; */
case GRAPH_L: return "GRAPH gp";
case IN_L: return "IN";
case IRI_L: return "IRI builtin";
case LANG_L: return "LANG builtin";
case LIKE_L: return "LIKE";
case LIMIT_L: return "LIMIT";
case MACRO_L: return "macro invocation";
/* case NAMED_L: return "NAMED"; */
case NIL_L: return "NIL";
case OBJECT_L: return "OBJECT";
case OFFBAND_L: return "OFFBAND";
case OFFSET_L: return "OFFSET";
case OPTIONAL_L: return "OPTIONAL gp";
case ORDER_L: return "ORDER";
case PREDICATE_L: return "PREDICATE";
case PREFIX_L: return "PREFIX";
case SCORE_L: return "SCORE";
case SCORE_LIMIT_L: return "SCORE_LIMIT";
case SELECT_L: return "SELECT result-mode";
case SERVICE_L: return "SERVICE gp";
case SUBJECT_L: return "SUBJECT";
case true_L: return "true boolean";
case UNION_L: return "UNION gp";
case WHERE_L: return "WHERE gp";
case SPAR_BLANK_NODE_LABEL: return "blank node label";
case SPAR_BUILT_IN_CALL: return "built-in call";
case SPAR_FUNCALL: return "function call";
case SPAR_GP: return "group pattern";
case SPAR_LIT: return "lit";
case SPAR_QNAME: return "QName";
/*case SPAR_QNAME_NS: return "QName NS";*/
case SPAR_REQ_TOP: return "SPARQL query";
case SPAR_VARIABLE: return "Variable";
case SPAR_TRIPLE: return "Triple";
}
return NULL;
}
char *spart_dump_addr (void *addr)
{
return NULL;
}
void spart_dump_long (void *addr, dk_session_t *ses, int is_op)
{
if (!IS_BOX_POINTER(addr))
{
const char *op_descr = spart_dump_opname((ptrlong)(addr), is_op);
if (NULL != op_descr)
{
SES_PRINT (ses, op_descr);
return;
}
}
else
{
char *addr_descr = spart_dump_addr(addr);
if (NULL != addr_descr)
{
SES_PRINT (ses, addr_descr);
return;
}
}
{
char buf[30];
sprintf (buf, "LONG " BOXINT_FMT, unbox (addr));
SES_PRINT (ses, buf);
return;
}
}
void spart_dump_varr_bits (dk_session_t *ses, int varr_bits, int replaces_filters_bits)
{
char buf[200];
char *tail = buf;
#define VARR_BIT(b,txt) \
do { \
if (varr_bits & (b)) { \
const char *t = (txt); \
while ('\0' != (tail[0] = (t++)[0])) tail++; \
if (replaces_filters_bits & (b)) { (tail++)[0] = '!'; } \
} \
} while (0);
VARR_BIT (SPART_VARR_CONFLICT, " CONFLICT");
VARR_BIT (SPART_VARR_GLOBAL, " GLOBAL");
VARR_BIT (SPART_VARR_EXTERNAL, " EXTERNAL");
VARR_BIT (SPART_VARR_ALWAYS_NULL, " always-NULL");
VARR_BIT (SPART_VARR_NOT_NULL, " notNULL");
VARR_BIT (SPART_VARR_FIXED, " fixed");
VARR_BIT (SPART_VARR_TYPED, " typed");
VARR_BIT (SPART_VARR_IS_LIT, " lit");
VARR_BIT (SPART_VARR_IRI_CALC, " IRI-namecalc");
VARR_BIT (SPART_VARR_SPRINTFF, " SprintfF");
VARR_BIT (SPART_VARR_IS_BLANK, " bnode");
VARR_BIT (SPART_VARR_IS_IRI, " IRI");
VARR_BIT (SPART_VARR_IS_REF, " reference");
VARR_BIT (SPART_VARR_EXPORTED, " exported");
session_buffered_write (ses, buf, tail-buf);
}
void spart_dump_rvr (dk_session_t *ses, rdf_val_range_t *rvr, int replaces_filters_bits)
{
char buf[300];
char *tail = buf;
int len;
int varr_bits = rvr->rvrRestrictions;
ccaddr_t fixed_dt = rvr->rvrDatatype;
ccaddr_t fixed_val = rvr->rvrFixedValue;
spart_dump_varr_bits (ses, varr_bits, replaces_filters_bits);
if (varr_bits & SPART_VARR_TYPED)
{
len = sprintf (tail, "; dt=%.100s", fixed_dt);
tail += len;
}
if (varr_bits & SPART_VARR_FIXED)
{
dtp_t dtp = DV_TYPE_OF (fixed_val);
const char *dtp_name = dv_type_title (dtp);
const char *meta = "";
const char *lit_dt = NULL;
const char *lit_lang = NULL;
if (DV_ARRAY_OF_POINTER == dtp)
{
SPART *fixed_tree = ((SPART *)fixed_val);
if (SPAR_QNAME == SPART_TYPE (fixed_tree))
{
meta = " QName";
fixed_val = fixed_tree->_.lit.val;
}
else if (SPAR_LIT == SPART_TYPE (fixed_tree))
{
meta = " lit";
fixed_val = fixed_tree->_.lit.val;
lit_dt = fixed_tree->_.lit.datatype;
lit_lang = fixed_tree->_.lit.language;
}
dtp = DV_TYPE_OF (fixed_val);
dtp_name = dv_type_title (dtp);
}
if (IS_STRING_DTP (dtp))
len = sprintf (tail, "; fixed%s %s '%.100s'", meta, dtp_name, fixed_val);
else if (DV_LONG_INT == dtp)
len = sprintf (tail, "; fixed%s %s %ld", meta, dtp_name, (long)(unbox (fixed_val)));
else
len = sprintf (tail, "; fixed%s %s", meta, dtp_name);
tail += len;
if (NULL != lit_dt)
tail += sprintf (tail, "^^'%.50s'", lit_dt);
if (NULL != lit_lang)
tail += sprintf (tail, "@'%.50s'", lit_lang);
SES_PRINT (ses, buf);
}
if (rvr->rvrIriClassCount)
{
int iricctr;
SES_PRINT (ses, "; IRI classes");
for (iricctr = 0; iricctr < rvr->rvrIriClassCount; iricctr++)
{
SES_PRINT (ses, " ");
SES_PRINT (ses, rvr->rvrIriClasses[iricctr]);
}
}
if (rvr->rvrRedCutCount)
{
int rcctr;
SES_PRINT (ses, "; Not one of");
for (rcctr = 0; rcctr < rvr->rvrRedCutCount; rcctr++)
{
SES_PRINT (ses, " ");
SES_PRINT (ses, rvr->rvrRedCuts[rcctr]);
}
}
if (rvr->rvrSprintffs)
{
int sffctr;
SES_PRINT (ses, "; Formats ");
for (sffctr = 0; sffctr < rvr->rvrSprintffCount; sffctr++)
{
SES_PRINT (ses, " |");
SES_PRINT (ses, rvr->rvrSprintffs[sffctr]);
SES_PRINT (ses, "|");
}
}
}
void
spart_dump_eq (int eq_ctr, sparp_equiv_t *eq, dk_session_t *ses)
{
int varname_count, varname_ctr, var_ctr;
char buf[100];
session_buffered_write_char ('\n', ses);
if (NULL == eq)
{
sprintf (buf, "#%d: merged and destroyed", eq_ctr);
SES_PRINT (ses, buf);
return;
}
sprintf (buf, "#%d: %s( %d subv (%d bindings), %d recv, %d gspo, %d const, %d opt, %d subq:", eq_ctr,
(eq->e_deprecated ? "deprecated " : ""),
BOX_ELEMENTS_INT_0(eq->e_subvalue_idxs), (int)(eq->e_nested_bindings), BOX_ELEMENTS_INT_0(eq->e_receiver_idxs),
(int)(eq->e_gspo_uses), (int)(eq->e_const_reads), (int)(eq->e_optional_reads), (int)(eq->e_subquery_uses) );
SES_PRINT (ses, buf);
varname_count = BOX_ELEMENTS (eq->e_varnames);
for (varname_ctr = 0; varname_ctr < varname_count; varname_ctr++)
{
SES_PRINT (ses, " ");
SES_PRINT (ses, eq->e_varnames[varname_ctr]);
}
SES_PRINT (ses, " in");
for (var_ctr = 0; var_ctr < eq->e_var_count; var_ctr++)
{
SPART *var = eq->e_vars[var_ctr];
SES_PRINT (ses, " ");
SES_PRINT (ses, ((NULL != var->_.var.tabid) ? var->_.var.tabid : var->_.var.selid));
}
SES_PRINT (ses, ";"); spart_dump_rvr (ses, &(eq->e_rvr), eq->e_replaces_filter);
if (SPART_BAD_EQUIV_IDX != eq->e_external_src_idx)
{
sprintf (buf, "; parent #%d", (int)(eq->e_external_src_idx));
SES_PRINT (ses, buf);
}
SES_PRINT (ses, ")");
}
void
spart_dump (void *tree_arg, dk_session_t *ses, int indent, const char *title, int hint)
{
SPART *tree = (SPART *) tree_arg;
int ctr;
if ((NULL == tree) && (hint < 0))
return;
if (indent > 0)
{
session_buffered_write_char ('\n', ses);
for (ctr = indent; ctr--; /*no step*/ )
session_buffered_write_char (' ', ses);
}
else
{
session_buffered_write_char ('\t', ses);
indent = -indent;
}
if (title)
{
SES_PRINT (ses, title);
SES_PRINT (ses, ": ");
}
if (DV_ARRAY_OF_POINTER != DV_TYPE_OF (tree))
{
hint = 0;
}
if ((-1 == hint) && IS_BOX_POINTER(tree))
{
if ((SPART_HEAD >= BOX_ELEMENTS(tree)) || IS_BOX_POINTER (tree->type))
{
SES_PRINT (ses, "special: ");
hint = -2;
}
}
if (!hint)
hint = DV_TYPE_OF (tree);
switch (hint)
{
case -1:
{
int childrens;
char buf[50];
if (!IS_BOX_POINTER(tree))
{
SES_PRINT (ses, "[");
spart_dump_long (tree, ses, 0);
SES_PRINT (ses, "]");
goto printed;
}
sprintf (buf, "(line %ld) ", (long) unbox(tree->srcline));
SES_PRINT (ses, buf);
childrens = BOX_ELEMENTS (tree);
switch (tree->type)
{
case SPAR_ALIAS:
{
sprintf (buf, "ALIAS:");
SES_PRINT (ses, buf);
spart_dump (tree->_.alias.aname, ses, indent+2, "ALIAS NAME", 0);
spart_dump (tree->_.alias.arg, ses, indent+2, "VALUE", -1);
/* _.alias.native is temp so it is not printed */
break;
}
case SPAR_BLANK_NODE_LABEL:
{
sprintf (buf, "BLANK NODE:");
SES_PRINT (ses, buf);
spart_dump (tree->_.var.vname, ses, -(indent+2), "NAME", 0);
spart_dump (tree->_.var.selid, ses, -(indent+2), "SELECT ID", 0);
spart_dump (tree->_.var.tabid, ses, -(indent+2), "TABLE ID", 0);
break;
}
case SPAR_BUILT_IN_CALL:
{
sprintf (buf, "BUILT-IN CALL:");
SES_PRINT (ses, buf);
if (tree->_.builtin.desc_ofs)
SES_PRINT (ses, sparp_bif_descs[tree->_.builtin.desc_ofs].sbd_name);
else
spart_dump_long ((void *)(tree->_.builtin.btype), ses, -1);
spart_dump (tree->_.builtin.args, ses, indent+2, "ARGUMENT", -2);
break;
}
case SPAR_FUNCALL:
{
int argctr, argcount = BOX_ELEMENTS (tree->_.funcall.argtrees);
spart_dump (tree->_.funcall.qname, ses, indent+2, "FUNCTION NAME", 0);
if (tree->_.funcall.agg_mode)
spart_dump ((void *)(tree->_.funcall.agg_mode), ses, indent+2, "AGGREGATE MODE", 0);
for (argctr = 0; argctr < argcount; argctr++)
spart_dump (tree->_.funcall.argtrees[argctr], ses, indent+2, "ARGUMENT", -1);
break;
}
case SPAR_GP:
{
int eq_count, eq_ctr;
sprintf (buf, "GRAPH PATTERN:");
SES_PRINT (ses, buf);
spart_dump_long ((void *)(tree->_.gp.subtype), ses, -1);
spart_dump (tree->_.gp.members, ses, indent+2, "MEMBERS", -2);
spart_dump (tree->_.gp.subquery, ses, indent+2, "SUBQUERY", -1);
spart_dump (tree->_.gp.filters, ses, indent+2, "FILTERS", -2);
spart_dump (tree->_.gp.selid, ses, indent+2, "SELECT ID", 0);
spart_dump (tree->_.gp.options, ses, indent+2, "OPTIONS", -2);
/* spart_dump (tree->_.gp.results, ses, indent+2, "RESULTS", -2); */
session_buffered_write_char ('\n', ses);
for (ctr = indent+2; ctr--; /*no step*/ )
session_buffered_write_char (' ', ses);
sprintf (buf, "EQUIVS:");
SES_PRINT (ses, buf);
eq_count = tree->_.gp.equiv_count;
for (eq_ctr = 0; eq_ctr < eq_count; eq_ctr++)
{
sprintf (buf, " %d", (int)(tree->_.gp.equiv_indexes[eq_ctr]));
SES_PRINT (ses, buf);
}
break;
}
case SPAR_LIT:
{
sprintf (buf, "LITERAL:");
SES_PRINT (ses, buf);
spart_dump (tree->_.lit.val, ses, indent+2, "VALUE", 0);
if (tree->_.lit.datatype)
spart_dump (tree->_.lit.datatype, ses, indent+2, "DATATYPE", 0);
if (tree->_.lit.language)
spart_dump (tree->_.lit.language, ses, indent+2, "LANGUAGE", 0);
break;
}
case SPAR_QNAME:
{
sprintf (buf, "QNAME:");
SES_PRINT (ses, buf);
spart_dump (tree->_.lit.val, ses, indent+2, "IRI", 0);
break;
}
/*case SPAR_QNAME_NS:
{
sprintf (buf, "QNAME_NS:");
SES_PRINT (ses, buf);
spart_dump (tree->_.lit.val, ses, indent+2, "NAMESPACE", 0);
break;
}*/
case SPAR_REQ_TOP:
{
sprintf (buf, "REQUEST TOP NODE (");
SES_PRINT (ses, buf);
spart_dump_long ((void *)(tree->_.req_top.subtype), ses, 1);
SES_PRINT (ses, "):");
if (NULL != tree->_.req_top.retvalmode_name)
spart_dump (tree->_.req_top.retvalmode_name, ses, indent+2, "VALMODE FOR RETVALS", 0);
if (NULL != tree->_.req_top.formatmode_name)
spart_dump (tree->_.req_top.formatmode_name, ses, indent+2, "SERIALIZATION FORMAT", 0);
if (NULL != tree->_.req_top.storage_name)
spart_dump (tree->_.req_top.storage_name, ses, indent+2, "RDF DATA STORAGE", 0);
if (IS_BOX_POINTER(tree->_.req_top.retvals))
spart_dump (tree->_.req_top.retvals, ses, indent+2, "RETVALS", -2);
else
spart_dump (tree->_.req_top.retvals, ses, indent+2, "RETVALS", 0);
spart_dump (tree->_.req_top.retselid, ses, indent+2, "RETVALS SELECT ID", 0);
spart_dump (tree->_.req_top.sources, ses, indent+2, "SOURCES", -2);
spart_dump (tree->_.req_top.pattern, ses, indent+2, "PATTERN", -1);
spart_dump (tree->_.req_top.groupings, ses, indent+2, "GROUPINGS", -2);
spart_dump (tree->_.req_top.having, ses, indent+2, "HAVING", -1);
spart_dump (tree->_.req_top.order, ses, indent+2, "ORDER", -2);
spart_dump ((void *)(tree->_.req_top.limit), ses, indent+2, "LIMIT", -1);
spart_dump ((void *)(tree->_.req_top.offset), ses, indent+2, "OFFSET", -1);
spart_dump (tree->_.req_top.binv, ses, indent+2, "BINDINGS", -1);
break;
}
case SPAR_VARIABLE:
{
sprintf (buf, "VARIABLE:");
SES_PRINT (ses, buf);
spart_dump_rvr (ses, &(tree->_.var.rvr), 0);
if (NULL != tree->_.var.tabid)
{
ptrlong tr_idx = tree->_.var.tr_idx;
static const char *field_full_names[] = {"graph", "subject", "predicate", "object"};
if (tr_idx < SPART_TRIPLE_FIELDS_COUNT)
{
sprintf (buf, " (%s)", field_full_names[tr_idx]);
SES_PRINT (ses, buf);
}
else
spart_dump_opname (tr_idx, 0);
}
spart_dump (tree->_.var.vname, ses, indent+2, "NAME", 0);
spart_dump (tree->_.var.selid, ses, -(indent+2), "SELECT ID", 0);
spart_dump (tree->_.var.tabid, ses, -(indent+2), "TABLE ID", 0);
spart_dump ((void*)(tree->_.var.equiv_idx), ses, -(indent+2), "EQUIV", 0);
break;
}
case SPAR_TRIPLE:
{
sprintf (buf, "TRIPLE:");
SES_PRINT (ses, buf);
if (tree->_.triple.ft_type)
{
sprintf (buf, " ft predicate %d", (int)(tree->_.triple.ft_type));
SES_PRINT (ses, buf);
}
spart_dump (tree->_.triple.options, ses, indent+2, "OPTIONS", -2);
spart_dump (tree->_.triple.tr_graph, ses, indent+2, "GRAPH", -1);
spart_dump (tree->_.triple.tr_subject, ses, indent+2, "SUBJECT", -1);
spart_dump (tree->_.triple.tr_predicate, ses, indent+2, "PREDICATE", -1);
spart_dump (tree->_.triple.tr_object, ses, indent+2, "OBJECT", -1);
spart_dump (tree->_.triple.selid, ses, indent+2, "SELECT ID", 0);
spart_dump (tree->_.triple.tabid, ses, indent+2, "TABLE ID", 0);
break;
}
case SPAR_SERVICE_INV:
{
sprintf (buf, "SERVICE INV:");
SES_PRINT (ses, buf);
spart_dump (tree->_.sinv.endpoint, ses, indent+2, "ENDPOINT", -1);
spart_dump (tree->_.sinv.iri_params, ses, indent+2, "IRI PARAMS", -2);
spart_dump (tree->_.sinv.syntax, ses, indent+2, "SYNTAX", -1);
spart_dump (tree->_.sinv.param_varnames, ses, indent+2, "PARAM VARNAMES", -2);
spart_dump (tree->_.sinv.rset_varnames, ses, indent+2, "RSET VARNAMES", -2);
spart_dump (tree->_.sinv.defines, ses, indent+2, "DEFINES", -2);
break;
}
case SPAR_BINDINGS_INV:
{
/*int varctr, varcount = BOX_ELEMENTS (tree->_.binv.vars);*/
int rowctr, rowcount = BOX_ELEMENTS (tree->_.binv.data_rows);
sprintf (buf, "BINDINGS INV (own idx %ld):", (long)(tree->_.binv.own_idx));
SES_PRINT (ses, buf);
spart_dump (tree->_.binv.vars, ses, indent+2, "VARS", -2);
for (rowctr = 0; rowctr < rowcount; rowctr = ((rowctr < 4) || (rowctr >= rowcount - 4)) ? (rowctr + 1) : (rowcount - 4))
{
char rowtitle[100];
unsigned int rowmask = tree->_.binv.data_rows_mask[rowctr] - '/';
sprintf (rowtitle, "ROW %d/%d (%s%s)", rowctr, rowcount,
(rowmask ? "disabled via " : "enabled"),
(rowmask ? tree->_.binv.vars[rowmask-1]->_.var.vname : "") );
spart_dump (tree->_.binv.data_rows[rowctr], ses, indent+2, rowtitle, -2);
}
break;
}
case BOP_EQ: case SPAR_BOP_EQ: case BOP_NEQ:
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_SAME: case BOP_NSAME:
case BOP_PLUS: case BOP_MINUS: case BOP_TIMES: case BOP_DIV: case BOP_MOD:
case BOP_AND: case BOP_OR: case BOP_NOT:
{
sprintf (buf, "OPERATOR EXPRESSION ("/*, tree->type*/);
SES_PRINT (ses, buf);
spart_dump_long ((void *)(tree->type), ses, 1);
SES_PRINT (ses, "):");
spart_dump (tree->_.bin_exp.left, ses, indent+2, "LEFT", -1);
spart_dump (tree->_.bin_exp.right, ses, indent+2, "RIGHT", -1);
break;
}
case ORDER_L:
{
sprintf (buf, "ORDERING ("/*, tree->_.oby.direction*/);
SES_PRINT (ses, buf);
spart_dump_long ((void *)(tree->_.oby.direction), ses, 1);
SES_PRINT (ses, "):");
spart_dump (tree->_.oby.expn, ses, indent+2, "CRITERION", -1);
break;
}
case SPAR_GRAPH:
{
sprintf (buf, "DATA SOURCE: ");
switch (tree->_.graph.subtype)
{
case SPART_GRAPH_FROM: strcat (buf, "FROM (default)"); break;
case SPART_GRAPH_GROUP: strcat (buf, "FROM (default, group)"); break;
case SPART_GRAPH_NAMED: strcat (buf, "FROM NAMED"); break;
case SPART_GRAPH_NOT_FROM: strcat (buf, "NOT FROM"); break;
case SPART_GRAPH_NOT_GROUP: strcat (buf, "NOT FROM (group)"); break;
case SPART_GRAPH_NOT_NAMED: strcat (buf, "NOT NAMED"); break;
default: GPF_T;
}
SES_PRINT (ses, buf);
if (NULL != tree->_.graph.iri)
spart_dump (tree->_.graph.iri, ses, indent+2, "IRI", 0);
if (NULL != tree->_.graph.expn)
spart_dump (tree->_.graph.expn, ses, indent+2, "EXPN", 0);
break;
}
case NAMED_L:
{
sprintf (buf, "FROM NAMED:");
SES_PRINT (ses, buf);
spart_dump (tree->_.lit.val, ses, indent+2, "IRI", 0);
break;
}
case SPAR_LIST:
{
sprintf (buf, "LIST:");
SES_PRINT (ses, buf);
spart_dump (tree->_.list.items, ses, indent+2, "ITEMS", -2);
break;
}
case SPAR_DEFMACRO:
{
int namectr, namecount;
spart_dump (tree->_.defmacro.mname, ses, indent+2, "DEFINITION OF MACRO NAME", 0);
spart_dump (tree->_.defmacro.quad_pattern, ses, indent+2, "QUAD PATTERN ITEMS", -2);
namecount = BOX_ELEMENTS_0 (tree->_.defmacro.paramnames);
for (namectr = 0; namectr < namecount; namectr++)
spart_dump (tree->_.defmacro.paramnames[namectr], ses, indent+2, "PARAMETER", 0);
namecount = BOX_ELEMENTS_0 (tree->_.defmacro.localnames);
for (namectr = 0; namectr < namecount; namectr++)
spart_dump (tree->_.defmacro.localnames[namectr], ses, indent+2, "LOCAL VAR", 0);
spart_dump (tree->_.defmacro.body, ses, indent+2, "BODY", -1);
if (tree->_.defmacro.aggregate_count)
spart_dump ((void *)(tree->_.defmacro.aggregate_count), ses, indent+2, "AGGREGATE COUNT", 0);
break;
}
case SPAR_MACROCALL:
{
int argctr, argcount = BOX_ELEMENTS (tree->_.macrocall.argtrees);
sprintf (buf, "MACRO <%.50s> CALL, id %s", tree->_.macrocall.mname, tree->_.macrocall.mid);
SES_PRINT (ses, buf);
spart_dump (tree->_.macrocall.argtrees, ses, indent+2, "CONTEXT GRAPH", -1);
for (argctr = 0; argctr < argcount; argctr++)
spart_dump (tree->_.macrocall.argtrees[argctr], ses, indent+2, "ARGUMENT", -1);
break;
}
case SPAR_MACROPU:
{
sprintf (buf, "MACRO PARAM %s (#%ld), type %ld\n", tree->_.macropu.pname, (long)(tree->_.macropu.pindex), (long)(tree->_.macropu.pumode));
SES_PRINT (ses, buf);
break;
}
default:
{
sprintf (buf, "NODE OF TYPE %ld (", (ptrlong)(tree->type));
SES_PRINT (ses, buf);
spart_dump_long ((void *)(tree->type), ses, 0);
sprintf (buf, ") with %d children:\n", childrens-SPART_HEAD);
SES_PRINT (ses, buf);
for (ctr = SPART_HEAD; ctr < childrens; ctr++)
spart_dump (((void **)(tree))[ctr], ses, indent+2, NULL, 0);
break;
}
}
break;
}
case DV_ARRAY_OF_POINTER:
{
int childrens = BOX_ELEMENTS (tree);
char buf[50];
sprintf (buf, "ARRAY with %d children: {", childrens);
SES_PRINT (ses, buf);
for (ctr = 0; ctr < childrens; ctr++)
spart_dump (((void **)(tree))[ctr], ses, indent+2, NULL, 0);
if (indent > 0)
{
session_buffered_write_char ('\n', ses);
for (ctr = indent; ctr--; /*no step*/ )
session_buffered_write_char (' ', ses);
}
SES_PRINT (ses, " }");
break;
}
case -2:
{
int childrens = BOX_ELEMENTS (tree);
char buf[50];
if (0 == childrens)
{
SES_PRINT (ses, "EMPTY ARRAY");
break;
}
sprintf (buf, "ARRAY OF NODES with %d children: {", childrens);
SES_PRINT (ses, buf);
for (ctr = 0; ctr < childrens; ctr++)
spart_dump (((void **)(tree))[ctr], ses, indent+2, NULL, -1);
if (indent > 0)
{
session_buffered_write_char ('\n', ses);
for (ctr = indent; ctr--; /*no step*/ )
session_buffered_write_char (' ', ses);
}
SES_PRINT (ses, " }");
break;
}
#if 0
case -3:
{
char **execname = (char **)id_hash_get (xpf_reveng, (caddr_t)(&tree));
SES_PRINT (ses, "native code started at ");
if (NULL == execname)
{
char buf[30];
sprintf (buf, "0x%p", (void *)tree);
SES_PRINT (ses, buf);
}
else
{
SES_PRINT (ses, "label '");
SES_PRINT (ses, execname[0]);
SES_PRINT (ses, "'");
}
break;
}
#endif
case DV_LONG_INT:
{
char buf[30];
sprintf (buf, "LONG %ld", (long)(unbox ((ccaddr_t)tree)));
SES_PRINT (ses, buf);
break;
}
case DV_STRING:
{
SES_PRINT (ses, "STRING `");
SES_PRINT (ses, (char *)(tree));
SES_PRINT (ses, "'");
break;
}
case DV_UNAME:
{
SES_PRINT (ses, "UNAME `");
SES_PRINT (ses, (char *)(tree));
SES_PRINT (ses, "'");
break;
}
case DV_SYMBOL:
{
SES_PRINT (ses, "SYMBOL `");
SES_PRINT (ses, (char *)(tree));
SES_PRINT (ses, "'");
break;
}
case DV_NUMERIC:
{
numeric_t n = (numeric_t)(tree);
char buf[0x100];
SES_PRINT (ses, "NUMERIC ");
numeric_to_string (n, buf, 0x100);
SES_PRINT (ses, buf);
}
default:
{
char buf[30];
sprintf (buf, "UNEXPECTED TYPE (%u)", (unsigned)(DV_TYPE_OF (tree)));
SES_PRINT (ses, buf);
break;
}
}
printed:
if (0 == indent)
session_buffered_write_char ('\n', ses);
}
int sparp_valmode_is_correct (ssg_valmode_t fmt)
{
jso_rtti_t *rtti;
if (!IS_BOX_POINTER (fmt))
return 1;
if ((qm_format_default_iri_ref == fmt) || (qm_format_default_iri_ref_nullable == fmt) ||
(qm_format_default_ref == fmt) || (qm_format_default_ref_nullable == fmt) ||
(qm_format_default == fmt) || (qm_format_default_nullable == fmt) )
return 2;
rtti = gethash (fmt, jso_rttis_of_structs);
if ((NULL != rtti) && (fmt == rtti->jrtti_self))
{
return 3;
}
#ifndef NDEBUG
box_tag_modify (fmt, DV_XQI);
#endif
return 0;
}
void sparp_jso_validate_format (sparp_t *sparp, ssg_valmode_t fmt)
{
if (!sparp_valmode_is_correct (fmt))
spar_internal_error (sparp, "sparp_jso_validate_format(): custom format does not have JSO RTTI");
}
#if 0
void ssg_jso_validate_format (spar_sqlgen_t *ssg, ssg_valmode_t fmt)
{
if (!sparp_valmode_is_correct (fmt))
spar_sqlprint_error ("ssg_jso_validate_format(): custom format does not have JSO RTTI");
}
#endif
|