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
|
//---------------------------------------------------------------------
// <copyright file="CTreeGenerator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
//using System.Diagnostics; // Please use PlanCompiler.Assert instead of Debug.Assert in this class...
// It is fine to use Debug.Assert in cases where you assert an obvious thing that is supposed
// to prevent from simple mistakes during development (e.g. method argument validation
// in cases where it was you who created the variables or the variables had already been validated or
// in "else" clauses where due to code changes (e.g. adding a new value to an enum type) the default
// "else" block is chosen why the new condition should be treated separately). This kind of asserts are
// (can be) helpful when developing new code to avoid simple mistakes but have no or little value in
// the shipped product.
// PlanCompiler.Assert *MUST* be used to verify conditions in the trees. These would be assumptions
// about how the tree was built etc. - in these cases we probably want to throw an exception (this is
// what PlanCompiler.Assert does when the condition is not met) if either the assumption is not correct
// or the tree was built/rewritten not the way we thought it was.
// Use your judgment - if you rather remove an assert than ship it use Debug.Assert otherwise use
// PlanCompiler.Assert.
namespace System.Data.Query.PlanCompiler
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Common.CommandTrees;
using System.Data.Common.CommandTrees.ExpressionBuilder;
using System.Data.Common.Utils;
using System.Data.Metadata.Edm;
using System.Data.Query.InternalTrees;
using System.Globalization;
internal class CTreeGenerator : BasicOpVisitorOfT<DbExpression>
{
#region Nested Types
/// <summary>
/// The VarInfo class tracks how a single IQT Var should be referenced in terms of CQT Expressions.
/// The tracked Var must have been introduced by an IQT RelOp that was converted to a DbExpression that
/// is subsequently used in a DbExpressionBinding, otherwise the Var is either a ParameterVar or a locally
/// defined Var, which are tracked by the parameters collection of the Command and the VarDefScope
/// class, respectively.
/// An IQT Var that is tracked by a VarInfo instance is reachable in the following way:
/// 1. By a DbVariableReferenceExpression that references the Variable of the DbExpressionBinding that contains the DbExpression that logically publishes the IQT Var.
/// This is tracked by the PublisherName property of the RelOpInfo class, which is used to track Vars brought into scope by a DbExpressionBinding.
/// Without an enclosing RelOpInfo, the VarInfo is unbound and cannot be used to instantiate a CQT expression tree that is the equivalent of a VarRef of the IQT Var)
/// 2. By zero or more PropertyRefExpressions starting with a property of the DbVariableReferenceExpression created in step 1.
/// These PropertyRefExpressions are introduced on top of the DbVariableReferenceExpression because of Join or ApplyExpressions that
/// occur in the CQT between the expression that publishes the Var and the expression higher in the tree that contains a VarRefOp
/// to the IQT Var that must be resolved to a CQT DbExpression. In such cases the DbExpression that logically publishes
/// the IQT Var will have a record return Type.
/// The required property names are tracked, in order, in the PropertyPath property of this class.
/// The PrependProperty method is used to update the DbPropertyExpression path required to reach
/// the DbVariableReferenceExpression when the referenced Variable becomes part of such a record-typed output.
/// </summary>
private class VarInfo
{
#region Private Member Variables
private Var _var;
private List<string> _propertyChain = new List<string>();
#endregion
/// <summary>
/// Gets the Var tracked by this VarInfo instance
/// </summary>
internal Var Var { get { return _var; } }
/// <summary>
/// Gets the names, in order of use, that should be used to build DbPropertyExpression around an initial DbVariableReferenceExpression in order to build a DbExpression subtree that correctly references the tracked IQT Var
/// </summary>
internal List<string> PropertyPath { get { return _propertyChain; } }
/// <summary>
/// Constructs a new VarInfo instance that tracks the specified Var.
/// </summary>
/// <param name="target">The IQT Var that this VarInfo instance should track.</param>
internal VarInfo(Var target)
{
_var = target;
}
/// <summary>
/// Adds a property name to the beginning of the property path for this VarInfo instance.
/// Each time a new record structure is constructed on top of the expression that logically
/// publishes this var, another DbPropertyExpression is required around the DbVariableReferenceExpression used
/// to reach the Var in the CQT. Each new DbPropertyExpression must be added immediately around the
/// DbVariableReferenceExpression, with previous PropertyExpressions now referring to the new DbPropertyExpression.
/// Therefore the new property name added by this method is inserted at the start of the property path.
/// See the Visit methods for the Join/ApplyOps for examples of using this method to adjust the property path.
/// </summary>
/// <param name="propName">The new property name to insert at the start of the property path for the Var tracked by this VarInfo instance</param>
internal void PrependProperty(string propName)
{
_propertyChain.Insert(0, propName);
}
}
/// <summary>
/// Groups a set of VarInfo instances together and allows certain operations (Bind/Unbind/PrependProperty)
/// to be performed on all instances in the VarInfoList with a single call.
/// </summary>
private class VarInfoList : List<VarInfo>
{
/// <summary>
/// Constructs a new, empty VarInfoList.
/// </summary>
internal VarInfoList() : base() { }
/// <summary>
/// Constructs a new VarInfoList that contains the specified VarInfo instances.
/// </summary>
/// <param name="elements"></param>
internal VarInfoList(IEnumerable<VarInfo> elements) : base(elements) { }
/// <summary>
/// Prepends the specified property name to the property path of all VarInfo instances in this list.
/// </summary>
/// <param name="propName"></param>
internal void PrependProperty(string propName)
{
foreach (VarInfo vInf in this)
{
vInf.PropertyPath.Insert(0, propName);
}
}
/// <summary>
/// Attempts to retrieve the VarInfo instance that tracks the specified IQT Var, if it is contained by this VarInfoList.
/// </summary>
/// <param name="targetVar">The required IQT Var</param>
/// <param name="varInfo">Contains the VarInfo instance that tracks the specified Var if this method returns true</param>
/// <returns>True if this list contains a VarInfo instance that tracks the specified Var; otherwise false</returns>
internal bool TryGetInfo(Var targetVar, out VarInfo varInfo)
{
varInfo = null;
foreach (VarInfo info in this)
{
if (info.Var == targetVar)
{
varInfo = info;
return true;
}
}
return false;
}
}
/// <summary>
/// IqtVarScope is used to represent one or more IQT Vars that are currently in scope and can be mapped to a corresponding CQT DbExpression subtree.
/// </summary>
private abstract class IqtVarScope
{
/// <summary>
/// Attempts to resolve the specified IQT Var by building or mapping to a CQT DbExpression subtree. Overridden in derived classes.
/// </summary>
/// <param name="targetVar">The IQT Var to resolve</param>
/// <param name="resultExpr">If the methods returns true, the DbExpression to which the Var was resolved; otherwise null</param>
/// <returns>True if the specified Var was successfully resolved; otherwise false</returns>
internal abstract bool TryResolveVar(Var targetVar, out DbExpression resultExpr);
}
private abstract class BindingScope : IqtVarScope
{
private readonly VarInfoList _definedVars;
internal BindingScope(IEnumerable<VarInfo> boundVars)
{
_definedVars = new VarInfoList(boundVars);
}
/// <summary>
/// Information (current binding name, property path) about the Vars logically published by the Publisher expression
/// </summary>
internal VarInfoList PublishedVars { get { return _definedVars; } }
/// <summary>
/// Implements the abstract IqtVarScope.TryResolveVar method. If the specified Var was published by this scope's DbExpression, it is mapped to a CQT DbExpression by calling CreateExpression on the VarInfo used to track it.
/// </summary>
/// <param name="targetVar">The Var to resolve</param>
/// <param name="resultExpr">If the method returns true, the DbExpression to which the Var was resolved; otherwise null</param>
/// <returns>True if the specified Var was successfully resolved; otherwise false</returns>
internal override bool TryResolveVar(Var targetVar, out DbExpression resultExpr)
{
resultExpr = null;
VarInfo foundInfo = null;
if (_definedVars.TryGetInfo(targetVar, out foundInfo))
{
resultExpr = this.BindingReference;
foreach (string propName in foundInfo.PropertyPath)
{
resultExpr = resultExpr.Property(propName);
}
return true;
}
return false;
}
protected abstract DbVariableReferenceExpression BindingReference { get; }
}
/// <summary>
/// Represents a collection of IQT Vars that were brought into scope by a DbExpression used in a DbExpressionBinding. This class is also used to associate those Vars with that DbExpression, which is considered the logical 'publisher' of the Vars.
/// </summary>
private class RelOpInfo : BindingScope
{
private readonly DbExpressionBinding _binding;
internal RelOpInfo(string bindingName, DbExpression publisher, IEnumerable<VarInfo> publishedVars)
: base(publishedVars)
{
PlanCompiler.Assert(TypeSemantics.IsCollectionType(publisher.ResultType), "non-collection type used as RelOpInfo publisher");
_binding = publisher.BindAs(bindingName);
}
/// <summary>
/// The unique name assigned to the CQT DbExpression that logically publishes the PublishedVars. Used primarily in ExpressionBindings that contain that DbExpression
/// </summary>
internal string PublisherName
{
get { return _binding.VariableName; }
}
/// <summary>
/// The CQT DbExpression that logically publishes the PublishedVars
/// </summary>
internal DbExpression Publisher { get { return _binding.Expression; } }
/// <summary>
/// Creates a new DbExpressionBinding that binds the publisher DbExpression under the binding name
/// </summary>
/// <returns>The new DbExpressionBinding</returns>
internal DbExpressionBinding CreateBinding()
{
return _binding;
}
protected override DbVariableReferenceExpression BindingReference
{
get { return _binding.Variable; }
}
}
/// <summary>
/// Represents a collection of IQT Vars that were brought into scope by a DbExpression used in a DbGroupExpressionBinding.
/// </summary>
private class GroupByScope : BindingScope
{
private readonly DbGroupExpressionBinding _binding;
private bool _referenceGroup;
internal GroupByScope(DbGroupExpressionBinding binding, IEnumerable<VarInfo> publishedVars)
: base(publishedVars)
{
_binding = binding;
}
/// <summary>
/// Returns the DbGroupExpressionBinding that backs this group-by scope
/// </summary>
/// <returns>The new DbExpressionBinding</returns>
internal DbGroupExpressionBinding Binding { get { return _binding; } }
internal void SwitchToGroupReference()
{
PlanCompiler.Assert(!_referenceGroup, "SwitchToGroupReference called more than once on the same GroupByScope?");
_referenceGroup = true;
}
protected override DbVariableReferenceExpression BindingReference
{
get { return (_referenceGroup ? _binding.GroupVariable : _binding.Variable); }
}
}
/// <summary>
/// Represents a collection of IQT Vars that are in scope because they are defined locally (by VarDefOps) to an IQT Op that is being visited.
/// </summary>
private class VarDefScope : IqtVarScope
{
private Dictionary<Var, DbExpression> _definedVars;
internal VarDefScope(Dictionary<Var, DbExpression> definedVars)
{
_definedVars = definedVars;
}
/// <summary>
/// Implements the abstract IqtVarScope.TryResolveVar method. If the specified Var exists in this scope, it is resolved by mapping it to the DbExpression that was produced by converting the IQT child Node of the VarDefOp that defines it to a CQT DbExpression subtree.
/// </summary>
/// <param name="targetVar">The Var to resolve</param>
/// <param name="resultExpr">If the method returns true, the DbExpression to which the Var was resolved; otherwise null</param>
/// <returns>True if the specified Var was successfully resolved; otherwise false</returns>
internal override bool TryResolveVar(Var targetVar, out DbExpression resultExpr)
{
resultExpr = null;
DbExpression foundExpr = null;
if (_definedVars.TryGetValue(targetVar, out foundExpr))
{
resultExpr = foundExpr;
return true;
}
return false;
}
}
#endregion
#region Private Instance Members
private Command _iqtCommand;
private DbQueryCommandTree _queryTree;
private Dictionary<ParameterVar, DbParameterReferenceExpression> _addedParams = new Dictionary<ParameterVar, DbParameterReferenceExpression>();
private Stack<IqtVarScope> _bindingScopes = new Stack<IqtVarScope>();
private Stack<VarDefScope> _varScopes = new Stack<VarDefScope>();
private Dictionary<DbExpression, RelOpInfo> _relOpState = new Dictionary<DbExpression, RelOpInfo>();
private AliasGenerator _applyAliases = new AliasGenerator("Apply");
private AliasGenerator _distinctAliases = new AliasGenerator("Distinct");
private AliasGenerator _exceptAliases = new AliasGenerator("Except");
private AliasGenerator _extentAliases = new AliasGenerator("Extent");
private AliasGenerator _filterAliases = new AliasGenerator("Filter");
private AliasGenerator _groupByAliases = new AliasGenerator("GroupBy");
private AliasGenerator _intersectAliases = new AliasGenerator("Intersect");
private AliasGenerator _joinAliases = new AliasGenerator("Join");
private AliasGenerator _projectAliases = new AliasGenerator("Project");
private AliasGenerator _sortAliases = new AliasGenerator("Sort");
private AliasGenerator _unionAllAliases = new AliasGenerator("UnionAll");
private AliasGenerator _elementAliases = new AliasGenerator("Element");
private AliasGenerator _singleRowTableAliases = new AliasGenerator("SingleRowTable");
private AliasGenerator _limitAliases = new AliasGenerator("Limit");
private AliasGenerator _skipAliases = new AliasGenerator("Skip");
#endregion
#region (pseudo) Public API
internal static DbCommandTree Generate(Command itree, Node toConvert)
{
CTreeGenerator treeGenerator = new CTreeGenerator(itree, toConvert);
return treeGenerator._queryTree;
}
#endregion
#region Constructors (private)
private CTreeGenerator(Command itree, Node toConvert)
{
_iqtCommand = itree;
DbExpression queryExpression = VisitNode(toConvert);
_queryTree = DbQueryCommandTree.FromValidExpression(itree.MetadataWorkspace, DataSpace.SSpace, queryExpression);
}
#endregion
#region RelOp Helpers and PublishedVar State Maintenance
/// <summary>
/// Asserts that the specified DbExpression is a 'RelOp' DbExpression, i.e. it is considered the publisher of one or more (IQT) RelVars.
/// </summary>
/// <param name="expr">The DbExpression on which to Assert</param>
private void AssertRelOp(DbExpression expr)
{
PlanCompiler.Assert(_relOpState.ContainsKey(expr),"not a relOp expression?");
}
/// <summary>
/// Update the DbExpression to RelOpInfo map to indicate that the specified DbExpression logically publishes the Vars
/// tracked in VarInfoList and that they should be bound under the specified name.
/// </summary>
/// <param name="name">The name under which the Vars tracked in VarInfoList are initially considered bound. This will be a unique name based on what kind of RelOp the specified DbExpression (the publisher) corresponds to</param>
/// <param name="expr">The DbExpression that is considered the logical publisher of the Vars tracked in publishedVars</param>
/// <param name="publishedVars">A VarInfoList that contains VarInfo instances that track the IQT Vars that are logically published by the specified DbExpression</param>
/// <returns>A new RelOpInfo instance that associates the given binding name and published Vars with the specified DbExpression. This RelOpInfo is also added to the DbExpression to RelOpInfo map</returns>
private RelOpInfo PublishRelOp(string name, DbExpression expr, VarInfoList publishedVars)
{
RelOpInfo retInfo = new RelOpInfo(name, expr, publishedVars);
_relOpState.Add(expr, retInfo);
return retInfo;
}
/// <summary>
/// Removes an entry in the DbExpression to RelOpInfo map, 'consuming' it so that it is not visible higher in the converted CQT.
/// </summary>
/// <param name="expr">The DbExpression for which the corresponding RelOpEntry should be removed</param>
/// <returns>The RelOpInfo that was removed from the DbExpression to RelOpInfo map</returns>
private RelOpInfo ConsumeRelOp(DbExpression expr)
{
AssertRelOp(expr);
RelOpInfo retInfo = _relOpState[expr];
_relOpState.Remove(expr);
return retInfo;
}
private RelOpInfo VisitAsRelOp(Node inputNode)
{
// Assert that the Op is actually a RelOp before attempting to use it
PlanCompiler.Assert(inputNode.Op is RelOp, "Non-RelOp used as DbExpressionBinding Input");
//
// Visit the Op. This Visit method of this class that actually processes the Op will
// publish the Vars produced by the resulting DbExpression in the DbExpression to RelOpInfo
// map, then return that DbExpression.
//
DbExpression inputExpr = VisitNode(inputNode);
//
// Retrieve the RelOpInfo for the DbExpression, that was published as part of the above call.
// ConsumeRelOp is called to both retrieve and remove the RelOpInfo instance since it is being
// used here in a DbExpressionBinding.
//
return ConsumeRelOp(inputExpr);
}
#endregion
#region Var Scope Maintenance
private void PushExpressionBindingScope(RelOpInfo inputState)
{
PlanCompiler.Assert(inputState != null && inputState.PublisherName != null && inputState.PublishedVars != null , "Invalid RelOpInfo produced by DbExpressionBinding Input");
_bindingScopes.Push(inputState);
}
/// <summary>
/// Visit a Node that will be used as the basis of a DbExpressionBinding, optionally pushing the
/// Vars that are logically published by the DbExpression produced from the Node's Op onto the expression binding scopes stack.
/// </summary>
/// <param name="inputNode">The Node to Visit</param>
/// <param name="pushScope">Indicates whether or not the Vars published by the converted form of the Node's Op should be brought into scope before this method returns</param>
/// <returns>The RelOpInfo that corresponds to the given Node, which details the DbExpression it was converted to, the Vars that are logically published by that DbExpression, and the unique name under which those Vars should be bound</returns>
private RelOpInfo EnterExpressionBindingScope(Node inputNode, bool pushScope)
{
RelOpInfo inputInfo = VisitAsRelOp(inputNode);
//
// If the pushScope flag is set, push the RelOpInfo onto the binding scopes stack to bring
// the Vars it tracks into scope.
//
if (pushScope)
{
PushExpressionBindingScope(inputInfo);
}
//
// Return the RelOpInfo that was produced by the input Node to the caller, providing access to the
// DbExpression that the Node's Op was converted to, the Vars that are logically published by that DbExpression,
// and the unique binding name that the Vars are considered bound under - that name should be used in
// the DbExpressionBinding that uses the DbExpression.
//
return inputInfo;
}
private RelOpInfo EnterExpressionBindingScope(Node inputNode)
{
return EnterExpressionBindingScope(inputNode, true);
}
private void ExitExpressionBindingScope(RelOpInfo scope, bool wasPushed)
{
if (wasPushed)
{
PlanCompiler.Assert(_bindingScopes.Count > 0, "ExitExpressionBindingScope called on empty ExpressionBindingScope stack");
RelOpInfo bindingScope = (RelOpInfo)_bindingScopes.Pop();
PlanCompiler.Assert(bindingScope == scope, "ExitExpressionBindingScope called on incorrect expression");
}
}
private void ExitExpressionBindingScope(RelOpInfo scope)
{
ExitExpressionBindingScope(scope, true);
}
private GroupByScope EnterGroupByScope(Node inputNode)
{
RelOpInfo inputInfo = VisitAsRelOp(inputNode);
// The current binding name is saved for use later as the VarName in the DbGroupExpressionBinding.
string varName = inputInfo.PublisherName;
// Generate the GroupVarName, and rebind the Input Vars under that name
string groupVarName = string.Format(CultureInfo.InvariantCulture, "{0}Group", varName);
DbGroupExpressionBinding newBinding = inputInfo.CreateBinding().Expression.GroupBindAs(varName, groupVarName);
GroupByScope newScope = new GroupByScope(newBinding, inputInfo.PublishedVars);
_bindingScopes.Push(newScope);
return newScope;
}
private void ExitGroupByScope(GroupByScope scope)
{
PlanCompiler.Assert(_bindingScopes.Count > 0, "ExitGroupByScope called on empty ExpressionBindingScope stack");
GroupByScope groupScope = (GroupByScope)_bindingScopes.Pop();
PlanCompiler.Assert(groupScope == scope, "ExitGroupByScope called on incorrect expression");
}
/// <summary>
/// Converts a list of VarDefOp Nodes into Expressions, builds a map of Var to DbExpression for each
/// defined Var, and pushes a new VarDefScope containing the map onto the stack of 'in scope' Vars.
/// </summary>
/// <param name="varDefNodes">A list of Nodes. Each Node in the list must reference a VarDefOp</param>
private void EnterVarDefScope(List<Node> varDefNodes)
{
//
// Create a new dictionary to act as the Var to DbExpression map
//
Dictionary<Var, DbExpression> varDefs = new Dictionary<Var, DbExpression>();
//
// For each Node in the list:
// 1. Assert that the Node is actually referencing a VarDefOp
// 2. Assert that the Var defined by the VarDefOp is actually a ComputedVar
// 3. Visit the Child0 Node of the Node to produce the CQT DbExpression that defines the Var
// 4. Add the returned DbExpression to the Var to DbExpression map.
foreach (Node childNode in varDefNodes)
{
VarDefOp defOp = childNode.Op as VarDefOp;
PlanCompiler.Assert(defOp != null, "VarDefListOp contained non-VarDefOp child node");
PlanCompiler.Assert(defOp.Var is ComputedVar, "VarDefOp defined non-Computed Var");
varDefs.Add(defOp.Var, VisitNode(childNode.Child0));
}
//
// Finally, construct and push a new VarDefScope based on the Var to DbExpression map onto the stack
// of locally 'in scope' IQT ComputedVars. All of the Vars defined in the original list are brought into scope by
// this final step, and are not in scope until this is done. Therefore it is not valid for any Var
// in the original list to refer to a Var that occurs previously in the list (left-correlation).
//
_varScopes.Push(new VarDefScope(varDefs));
}
/// <summary>
/// A convenience method to create a new VarDefScope from the specified VarDefListOp Node
/// </summary>
/// <param name="varDefListNode">The Node that references the VarDefListOp. Its children will be used as the basis of the new VarDefScope</param>
private void EnterVarDefListScope(Node varDefListNode)
{
PlanCompiler.Assert(varDefListNode.Op is VarDefListOp, "EnterVarDefListScope called with non-VarDefListOp");
EnterVarDefScope(varDefListNode.Children);
}
/// <summary>
/// Asserts that the top of the scope stack is actually a VarDefScope, and then pops it to remove the locally defined Vars from scope.
/// </summary>
private void ExitVarDefScope()
{
PlanCompiler.Assert(_varScopes.Count > 0, "ExitVarDefScope called on empty VarDefScope stack");
_varScopes.Pop();
}
/// <summary>
/// Resolves an IQT Var to a CQT DbExpression.
/// There are 3 possible ways for an IQT Var to resolve to a valid reference expressed as a CQT DbExpression:
/// 1. The specified Var is a valid ParameterVar in the IQT Command being converted:
/// This resolves simply to ParameterRefExpression. A Parameter that corresponds to the ParameterVar
/// is declared on the CQT DbCommandTree is this has not already been done.
/// 2. The specified Var is a ComputedVar that is defined locally to the Op being visited. In this case
/// The DbExpression produced by converting the VarDefOp that defines the Var is returned.
/// 3. Otherwise, the Var must have been brought into scope because the DbExpression that logically produces it is
/// being used in a DbExpressionBinding which is currently in scope. Each RelOpInfo on the ExpressionBindingScopes stack
/// is asked to resolve the Var, if one of the RelOpInfo scopes is tracking the Var it will construct an appropriate combination
/// of DbVariableReferenceExpression and PropertyRefExpressions that are sufficient to logically reference the Var.
/// If none of the 3 above conditions are satisfied then the Var is unresolvable in the CQT being constructed and
/// the original IQT Command must be considered invalid for the purposes of this conversion.
/// </summary>
/// <param name="referencedVar">The IQT Var to resolve</param>
/// <returns>The CQT DbExpression to which the specified Var resolves</returns>
private DbExpression ResolveVar(Var referencedVar)
{
DbExpression retExpr = null;
ParameterVar paramVar = referencedVar as ParameterVar;
if (paramVar != null)
{
//
// If there is already a parameter expression that corresponds to this parameter Var, reuse it.
//
DbParameterReferenceExpression paramRef;
if (!_addedParams.TryGetValue(paramVar, out paramRef))
{
paramRef = DbExpressionBuilder.Parameter(paramVar.Type, paramVar.ParameterName);
_addedParams[paramVar] = paramRef;
}
retExpr = paramRef;
}
else
{
ComputedVar compVar = referencedVar as ComputedVar;
if (compVar != null)
{
//
// If this is a ComputedVar, first check if it is defined locally to the Node of the Op being visited.
// Such local ComputedVars are only directly accessible from the Op being converted, so only the topmost
// ComputedVar scope on the stack should be considered.
//
if (_varScopes.Count > 0)
{
if (!_varScopes.Peek().TryResolveVar(compVar, out retExpr))
{
retExpr = null;
}
}
}
if (null == retExpr)
{
//
// If the Var was not resolved as a locally defined ComputedVar, then it must now be a Var that was brought
// into scope by a DbExpressionBinding in order to be considered valid. Each DbExpressionBinding scope (represented as a RelOpInfo)
// on the binding scopes stack from top to bottom is asked in turn to resolve the Var, breaking if the Var is successfully resolved.
//
DbExpression foundExpr = null;
foreach (IqtVarScope scope in _bindingScopes)
{
if (scope.TryResolveVar(referencedVar, out foundExpr))
{
retExpr = foundExpr;
break;
}
}
}
}
PlanCompiler.Assert(retExpr != null, string.Format(CultureInfo.InvariantCulture, "Unresolvable Var used in Command: VarType={0}, Id={1}", Enum.GetName(typeof(VarType), referencedVar.VarType), referencedVar.Id));
return retExpr;
}
#endregion
#region Visitor Helpers
/// <summary>
/// Asserts that the specified Node has exactly 2 child Nodes
/// </summary>
/// <param name="n">The Node on which to Assert</param>
private static void AssertBinary(Node n)
{
PlanCompiler.Assert(2 == n.Children.Count, string.Format(CultureInfo.InvariantCulture, "Non-Binary {0} encountered", n.Op.GetType().Name));
}
private DbExpression VisitChild(Node n, int index)
{
PlanCompiler.Assert(n.Children.Count > index, "VisitChild called with invalid index");
return VisitNode(n.Children[index]);
}
private new List<DbExpression> VisitChildren(Node n)
{
List<DbExpression> retList = new List<DbExpression>();
foreach (Node argNode in n.Children)
{
retList.Add(VisitNode(argNode));
}
return retList;
}
#endregion
#region IOpVisitor<DbExpression> Members
#region ScalarOp Conversions
protected override DbExpression VisitConstantOp(ConstantBaseOp op, Node n)
{
//
// Simple conversion using the same constant value as the ConstantBaseOp in a CQT DbConstantExpression
//
return DbExpressionBuilder.Constant(op.Type, op.Value);
}
public override DbExpression Visit(ConstantOp op, Node n)
{
return VisitConstantOp(op, n);
}
public override DbExpression Visit(InternalConstantOp op, Node n)
{
return VisitConstantOp(op, n);
}
public override DbExpression Visit(NullOp op, Node n)
{
return DbExpressionBuilder.Null(op.Type);
}
public override DbExpression Visit(NullSentinelOp op, Node n)
{
return VisitConstantOp(op, n);
}
public override DbExpression Visit(ConstantPredicateOp op, Node n)
{
//
// Create a "true=true" for "true" predicates,
// Create a "true=false" expression for false predicates
//
return DbExpressionBuilder.True.Equal(op.IsTrue ? DbExpressionBuilder.True : DbExpressionBuilder.False);
}
public override DbExpression Visit(FunctionOp op, Node n)
{
//
// FunctionOp becomes DbFunctionExpression that references the same EdmFunction metadata and
// with argument Expressions produced by converting the child nodes of the FunctionOp's Node
//
return op.Function.Invoke(VisitChildren(n));
}
public override DbExpression Visit(PropertyOp op, Node n)
{
// We should never see this Op - should have been eliminated in NTE
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(RelPropertyOp op, Node n)
{
// should have been eliminated in NTE
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(ArithmeticOp op, Node n)
{
//
// ArithmeticOp converts to a DbArithmeticExpression with an appropriate DbExpressionKind.
//
DbExpression resultExpr = null;
if (OpType.UnaryMinus == op.OpType)
{
// If the OpType is Unary minus, only 1 child Node is required.
resultExpr = VisitChild(n, 0).UnaryMinus();
}
else
{
// Otherwise this is a binary operator, so visit the left and right child Nodes
// and convert to CQT DbExpression based on the OpType.
DbExpression left = VisitChild(n, 0);
DbExpression right = VisitChild(n, 1);
switch (op.OpType)
{
case OpType.Divide:
{
resultExpr = left.Divide(right);
}
break;
case OpType.Minus:
{
resultExpr = left.Minus(right);
}
break;
case OpType.Modulo:
{
resultExpr = left.Modulo(right);
}
break;
case OpType.Multiply:
{
resultExpr = left.Multiply(right);
}
break;
case OpType.Plus:
{
resultExpr = left.Plus(right);
}
break;
default:
{
resultExpr = null;
}
break;
}
}
// The result DbExpression will only be null if a new OpType is added and this code is not updated
PlanCompiler.Assert(resultExpr != null, string.Format(CultureInfo.InvariantCulture, "ArithmeticOp OpType not recognized: {0}", Enum.GetName(typeof(OpType), op.OpType)));
return resultExpr;
}
public override DbExpression Visit(CaseOp op, Node n)
{
//
// CaseOp converts directly to DbCaseExpression.
// If no 'Else' Node is present a new DbNullExpression typed to the result Type of the CaseOp is used as the DbCaseExpression's Else expression.
// Otherwise the converted form of the 'Else' Node is used.
// This method assumes that the child Nodes of the CaseOp's Node are as follows:
// When1, Then1[..., WhenN, ThenN][, Else]
// that is, at least one When/Then pair MUST be present, subsequent When/Then pairs and the final Else are optional.
// This is the count of Nodes that contribute to the When/Then pairs, NOT the count of those pairs.
int caseCount = n.Children.Count;
// Verify the assumption made that at least one case is present.
PlanCompiler.Assert(caseCount > 1, "Invalid CaseOp: At least 2 child Nodes (1 When/Then pair) must be present");
List<DbExpression> whens = new List<DbExpression>();
List<DbExpression> thens = new List<DbExpression>();
DbExpression elseExpr = null;
if(0 == n.Children.Count % 2)
{
// If the number of child Nodes is divisible by 2, it is assumed that they are When/Then pairs without the optional Else Node.
// The Else DbExpression defaults to a properly typed DbNullExpression.
elseExpr = DbExpressionBuilder.Null(op.Type);
}
else
{
// Otherwise, an Else Node is present as the last child Node. It's CQT DbExpression form is used as the Else DbExpression.
// The count of child Nodes that contribute to the When/Then pairs must now be reduced by 1.
caseCount = caseCount - 1;
elseExpr = VisitChild(n, n.Children.Count - 1);
}
// Convert the When/Then Nodes in pairs until the number of converted Nodes is equal to the number of Nodes that contribute to the When/Then pairs.
for(int idx = 0; idx < caseCount; idx += 2)
{
whens.Add(VisitChild(n, idx));
thens.Add(VisitChild(n, idx + 1));
}
// Create and return a new DbCaseExpression using the When and Then DbExpression lists and the (converted or DbNullExpression) Else DbExpression.
return DbExpressionBuilder.Case(whens, thens, elseExpr);
}
public override DbExpression Visit(ComparisonOp op, Node n)
{
//
// ComparisonOp converts to a DbComparisonExpression with an appropriate DbExpressionKind.
// The ComparisonOp is only convertible to a DbComparisonExpression if it has 2 child Nodes
//
AssertBinary(n);
DbExpression left = VisitChild(n, 0);
DbExpression right = VisitChild(n, 1);
DbExpression compExpr = null;
switch (op.OpType)
{
case OpType.EQ:
{
compExpr = left.Equal(right);
}
break;
case OpType.NE:
{
compExpr = left.NotEqual(right);
}
break;
case OpType.LT:
{
compExpr = left.LessThan(right);
}
break;
case OpType.GT:
{
compExpr = left.GreaterThan(right);
}
break;
case OpType.LE:
{
compExpr = left.LessThanOrEqual(right);
}
break;
case OpType.GE:
{
compExpr = left.GreaterThanOrEqual(right);
}
break;
default:
{
compExpr = null;
}
break;
}
// The result DbExpression will only be null if a new OpType is added and this code is not updated
PlanCompiler.Assert(compExpr != null, string.Format(CultureInfo.InvariantCulture, "ComparisonOp OpType not recognized: {0}", Enum.GetName(typeof(OpType), op.OpType)));
return compExpr;
}
public override DbExpression Visit(ConditionalOp op, Node n)
{
//
// Boolean ConditionalOps convert to the corresponding And/Or/DbNotExpression. The IsNull ConditionalOp converts to DbIsNullExpression.
// In all cases the OpType is used to determine what kind of DbExpression to create.
//
// There will always be at least one argument (IsNull, Not) and should be at most 2 (And, Or).
DbExpression left = VisitChild(n, 0);
DbExpression condExpr = null;
switch (op.OpType)
{
case OpType.IsNull:
{
condExpr = left.IsNull();
}
break;
case OpType.And:
{
condExpr = left.And(VisitChild(n, 1));
}
break;
case OpType.Or:
{
condExpr = left.Or(VisitChild(n, 1));
}
break;
case OpType.Not:
{
// Convert Not(Not(<expression>)) to just <expression>. This is taken into account here
// because LeftSemi/AntiJoin conversions generate intermediate Not(Exists(<Op>)) IQT Nodes,
// which would then be converted to Not(Not(IsEmpty(<expression>)) if the following code were not present.
DbNotExpression notExpr = left as DbNotExpression;
if (notExpr != null)
{
condExpr = notExpr.Argument;
}
else
{
condExpr = left.Not();
}
}
break;
default:
{
condExpr = null;
}
break;
}
// The result DbExpression will only be null if a new OpType is added and this code is not updated
PlanCompiler.Assert(condExpr != null, string.Format(CultureInfo.InvariantCulture, "ConditionalOp OpType not recognized: {0}", Enum.GetName(typeof(OpType), op.OpType)));
return condExpr;
}
public override DbExpression Visit(LikeOp op, Node n)
{
//
// LikeOp converts to DbLikeExpression, with the conversions of the
// Node's first, second and third child nodes providing the
// Input, Pattern and Escape expressions.
//
return DbExpressionBuilder.Like(
VisitChild(n, 0),
VisitChild(n, 1),
VisitChild(n, 2)
);
}
public override DbExpression Visit(AggregateOp op, Node n)
{
// AggregateOp may only occur as the immediate child of a VarDefOp that is itself the
// child of a VarDefListOp used as the 'Aggregates' collection of a GroupByOp.
// As such, aggregates are handled directly during processing of GroupByOp.
// If this method is called an AggregateOp was encountered at some other (invalid) point in the IQT.
PlanCompiler.Assert(false, "AggregateOp encountered outside of GroupByOp");
throw EntityUtil.NotSupported(System.Data.Entity.Strings.Iqt_CTGen_UnexpectedAggregate);
}
public override DbExpression Visit(NavigateOp op, Node n)
{
// we should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(NewEntityOp op, Node n)
{
// We should never see this Op - should have been eliminated in NTE
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(NewInstanceOp op, Node n)
{
// We should never see this Op - should have been eliminated in NTE
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(DiscriminatedNewEntityOp op, Node n)
{
// We should never see this Op - should have been eliminated in NTE
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(NewMultisetOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(NewRecordOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(RefOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(VarRefOp op, Node n)
{
return ResolveVar(op.Var);
}
public override DbExpression Visit(TreatOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(CastOp op, Node n)
{
// Direct conversion to DbCastExpression with the same Type and converted argument DbExpression
return VisitChild(n, 0).CastTo(op.Type);
}
/// <summary>
/// A SoftCastOp is intended to be used only for promotion (and/or equivalence)
/// and should be ignored in the CTree
/// </summary>
/// <param name="op">the softcast Op</param>
/// <param name="n">the node</param>
/// <returns></returns>
public override DbExpression Visit(SoftCastOp op, Node n)
{
// Microsoft 9/21/06 - temporarily removing check here
// because the assert wrongly fails in some cases where the types are promotable,
// but the facets are not. Put this back when that issue is solved.
//
// PlanCompiler.Assert(TypeSemantics.IsEquivalentOrPromotableTo(n.Child0.Op.Type, op.Type),
// "Invalid use of SoftCastOp: Type " + n.Child0.Op.Type.Identity + " is not promotable to " + op.Type);
return VisitChild(n, 0);
}
public override DbExpression Visit(IsOfOp op, Node n)
{
// Direct conversion to DbIsOfExpression (with DbExpressionKind.IsOf) with the same Type and converted argument DbExpression
if (op.IsOfOnly)
return VisitChild(n, 0).IsOfOnly(op.IsOfType);
else
return VisitChild(n, 0).IsOf(op.IsOfType);
}
public override DbExpression Visit(ExistsOp op, Node n)
{
//
// Exists requires a RelOp input set
//
DbExpression inputExpr = VisitNode(n.Child0);
//
// Information about the Vars published by the RelOp argument does not need to be maintained
// since they may not now be used higher in the CQT.
//
ConsumeRelOp(inputExpr);
//
// Exists --> Not(IsEmpty(Input set)) via DbExpressionBuilder.Exists
//
return inputExpr.IsEmpty().Not();
}
public override DbExpression Visit(ElementOp op, Node n)
{
// We create this op when turning ApplyOp into a scalar subquery
DbExpression inputExpr = VisitNode(n.Child0);
AssertRelOp(inputExpr);
ConsumeRelOp(inputExpr);
DbElementExpression elementExpr = DbExpressionBuilder.CreateElementExpressionUnwrapSingleProperty(inputExpr);
return elementExpr;
}
public override DbExpression Visit(GetRefKeyOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(GetEntityRefOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(CollectOp op, Node n)
{
// We should never get here
throw EntityUtil.NotSupported();
}
#endregion
#region RelOp Conversions
/// <summary>
/// Generates a name for the specified Var.
/// If the Var has a name (TryGetName), then we use the name to look up
/// the right alias generator, and get a column name from the alias generator
/// Otherwise, we simply get a name from the default alias generator
/// </summary>
/// <param name="projectedVar">the var in question</param>
/// <param name="aliasMap">map to identify the appropriate alias generator</param>
/// <param name="defaultAliasGenerator">the default alias generator</param>
/// <param name="alreadyUsedNames">list of already used names</param>
/// <returns></returns>
private static string GenerateNameForVar(Var projectedVar, Dictionary<string, AliasGenerator> aliasMap,
AliasGenerator defaultAliasGenerator, Dictionary<string, string> alreadyUsedNames)
{
string columnName;
AliasGenerator aliasGenerator;
if (projectedVar.TryGetName(out columnName))
{
if (!aliasMap.TryGetValue(columnName, out aliasGenerator))
{
//
// No existing column in the current row with the same name. Create
// an alias-generator for future use
//
aliasGenerator = new AliasGenerator(columnName);
aliasMap[columnName] = aliasGenerator;
}
else
{
//
// Column name collides with another name in the same row.
// Use the alias-generator to generate a new name
//
columnName = aliasGenerator.Next();
}
}
else
{
//
// Must be a computed column or some such. Use the default alias generator
//
aliasGenerator = defaultAliasGenerator;
columnName = aliasGenerator.Next();
}
// Check to see if I've used this name already
while (alreadyUsedNames.ContainsKey(columnName))
{
columnName = aliasGenerator.Next();
}
alreadyUsedNames[columnName] = columnName;
return columnName;
}
/// <summary>
/// Called by both Visit(ProjectOp) and VisitSetOpArgument to create a DbProjectExpression
/// based on the RelOpInfo of the projection input and the set of projected Vars.
/// Note:
/// The projected Vars must have already been brought into scope (by one of the
/// methods such as EnterExpressionBinding, EnterVarDefScope, etc) before this method
/// is called, or the projected Vars will not be successfully resolved.
/// Both Visit(ProjectOp) and VisitSetOpArgument do this"
/// 1. Visit(ProjectOp) takes both DbExpressionBinding and VarDef based Vars into account
/// 2. The Vars produced by a SetOpArgument projection are only allowed to be DbExpressionBinding
/// based and are brought into scope when the original SetOp argument Node is visited.
/// </summary>
/// <param name="sourceInfo"></param>
/// <param name="outputVars"></param>
/// <returns></returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1309:UseOrdinalStringComparison", MessageId = "System.Collections.Generic.Dictionary`2<System.String,System.String>.#ctor(System.Collections.Generic.IEqualityComparer`1<System.String>)"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1309:UseOrdinalStringComparison", MessageId = "System.Collections.Generic.Dictionary`2<System.String,System.Data.Common.Utils.AliasGenerator>.#ctor(System.Collections.Generic.IEqualityComparer`1<System.String>)")]
private DbExpression CreateProject(RelOpInfo sourceInfo, IEnumerable<Var> outputVars)
{
//
// For each Var produced by the ProjectOp, call ResolveVar to retrieve the correct CQT DbExpression.
// This will either be a DbExpression that references the CQT Var under which the IQT is currently
// bound (if it is in scope) or it will be a copy of the DbExpression subtree that defines the Var,
// if the Var is a ComputedVar defined by a local VarDefOp.
// A new column name is generated to project the DbExpression, and the VarInfoList produced
// by this conversion is updated to include a new VarInfo indicating that the projected Var can be
// reached in the DbProjectExpression returned from this method via a property reference to the generated column name.
// This is the only path element required since the Vars are an immediate product of the ProjectOp, which also hides any Vars below it.
// Hence a new VarInfoList is constructed and published by the new DbProjectExpression.
// The list of column name/DbExpression pairs is built to use later when constructing the projection expression.
//
VarInfoList projectedInfo = new VarInfoList();
List<KeyValuePair<string, DbExpression>> projectedCols = new List<KeyValuePair<string, DbExpression>>();
AliasGenerator colGen = new AliasGenerator("C");
Dictionary<string, AliasGenerator> aliasMap = new Dictionary<string, AliasGenerator>(StringComparer.InvariantCultureIgnoreCase);
Dictionary<string, string> alreadyUsedAliases = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
foreach (Var projectedVar in outputVars)
{
string columnName = GenerateNameForVar(projectedVar, aliasMap, colGen, alreadyUsedAliases);
DbExpression columnValue = ResolveVar(projectedVar);
projectedCols.Add(new KeyValuePair<string, DbExpression>(columnName, columnValue));
VarInfo colInfo = new VarInfo(projectedVar);
colInfo.PrependProperty(columnName);
projectedInfo.Add(colInfo);
}
//
// Create a new DbProjectExpression with the converted Input and a new row (DbNewInstanceExpression) projection using the
// previously constructed column names and Expressions to define the shape of the resulting row. The Input is bound
// under the Var publisher name specified by its RelOpInfo, which will be a unique name based on the type of RelOp it was converted from.
//
DbExpression retExpr = sourceInfo.CreateBinding().Project(DbExpressionBuilder.NewRow(projectedCols));
//
// Publish the Vars produced by the new DbProjectExpression:
// PublisherName: The next Project alias.
// PublishedVars: The PublishedVars of the Project are those specified in the VarSet of the ProjectOp, reachable using the generated column names.
//
PublishRelOp(_projectAliases.Next(), retExpr, projectedInfo);
return retExpr;
}
/// <summary>
/// Called by both ScanTableOp and UnnestOp Visitor pattern methods to determine
/// the shape of the output of the converted form of those Ops, in terms of the
/// IQT Vars that are published by the resulting DbExpression and how those Vars should
/// be reached.
/// </summary>
/// <param name="targetTable">The table that is logically produced by the Op. For non-record sourceTypes, this should consist of a single column that logically constitutes the entire 'table'</param>
/// <returns>A VarInfoList containing VarInfo instances that correctly track the Var or Vars produced by the targetTable, in accordance with the shape of the sourceType</returns>
private static VarInfoList GetTableVars(Table targetTable)
{
VarInfoList outputVars = new VarInfoList();
if (targetTable.TableMetadata.Flattened)
{
// For a flat table, one Var per table column must be produced.
// There should be a ColumnVar in the targetTable's Columns collection for each
// column in the record type (for the table), and the VarInfo instances created here will track the
// fact that each ColumnVar should be reached via DbPropertyExpression of the record column's name
for (int idx = 0; idx < targetTable.Columns.Count; idx++)
{
VarInfo colInfo = new VarInfo(targetTable.Columns[idx]);
colInfo.PrependProperty(targetTable.TableMetadata.Columns[idx].Name);
outputVars.Add(colInfo);
}
}
else
{
// Otherwise, a single Var must be produced, which is immediately reachable
outputVars.Add(new VarInfo(targetTable.Columns[0]));
}
return outputVars;
}
public override DbExpression Visit(ScanTableOp op, Node n)
{
//
// Currently 2 different types of 'Table' (i.e. Extent) are supported:
// Record Extents (for example an S-Space table or view)
// Entity Extents (for example a C-Space EntitySet)
// These extents produce results of different shapes - the EntitySet case produces simply
// A collection of Entities, not a collection of records with a single Entity-typed column
// This distinction is handled in the common GetTableVars method shared by ScanTableOp and
// UnnestOp Visitor pattern methods.
//
PlanCompiler.Assert(op.Table.TableMetadata.Extent != null, "Invalid TableMetadata used in ScanTableOp - no Extent specified");
//
// We don't expect to see any view expressions here
//
PlanCompiler.Assert(!n.HasChild0, "views are not expected here");
VarInfoList outputVars = GetTableVars(op.Table);
// ScanTable converts to ExtentExpression
DbExpression retExpr = op.Table.TableMetadata.Extent.Scan();
//
// Publish the Vars that are logically produced by the ExtentExpression:
// PublisherName: The next Extent alias
// PublishedVars: The single Var (for an Entity extent) or multiple column-bound Vars (for a structured type extent)
// that are logically published by the ExtentExpression.
//
PublishRelOp(_extentAliases.Next(), retExpr, outputVars);
// Return the ExtentExpression
return retExpr;
}
public override DbExpression Visit(ScanViewOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
/// <summary>
/// Translate UnnestOp which is assumed (at this stage) to wrap a native ScalarOp
/// that returns a collection (e.g. a table-valued function node).
/// </summary>
public override DbExpression Visit(UnnestOp op, Node n)
{
// support Unnest(VarDef(input)) -> input
// where input is presumed to have a collection type (e.g. TVF)
PlanCompiler.Assert(n.Child0.Op.OpType == OpType.VarDef,
"an unnest's child must be a VarDef");
// get input (first child of VarDef)
Node input = n.Child0.Child0;
// translate input
DbExpression expr = input.Op.Accept(this, input);
// verify that the result is actually a collection
PlanCompiler.Assert(expr.ResultType.EdmType.BuiltInTypeKind == BuiltInTypeKind.CollectionType,
"the input to unnest must yield a collection after plan compilation");
// collect table vars for the unnest
VarInfoList outputVars = GetTableVars(op.Table);
PublishRelOp(_extentAliases.Next(), expr, outputVars);
return expr;
}
/// <summary>
/// Builds up an "empty" projection over the input node. Well, in reality, we build
/// up a dummy projection node - which simply selects out some constant (which
/// is never used). This is useful in scenarios where the outputs are
/// uninteresting, but the input row count is
/// </summary>
/// <param name="relOpNode">the relOp node</param>
/// <returns></returns>
private RelOpInfo BuildEmptyProjection(Node relOpNode)
{
//
// Ignore the projectOp at the root - if any
//
if (relOpNode.Op.OpType == OpType.Project)
{
relOpNode = relOpNode.Child0;
}
//
// Visit the Input RelOp, bring its Var(s) into scope, and retrieve and consume the RelOpInfo that describes its published Vars
//
RelOpInfo sourceInfo = EnterExpressionBindingScope(relOpNode);
//
// Create a new DbProjectExpression with the converted Input and a new row (DbNewInstanceExpression) projection using the
// previously constructed column names and Expressions to define the shape of the resulting row. The Input is bound
// under the Var publisher name specified by its RelOpInfo, which will be a unique name based on the type of RelOp it was converted from.
//
DbExpression constExpr = DbExpressionBuilder.Constant(1);
List<KeyValuePair<string, DbExpression>> projectedCols = new List<KeyValuePair<string, DbExpression>>();
projectedCols.Add(new KeyValuePair<string, DbExpression>("C0", constExpr));
DbExpression retExpr = sourceInfo.CreateBinding().Project(DbExpressionBuilder.NewRow(projectedCols));
// Publish the Vars produced by the new DbProjectExpression:
// PublisherName: The next Project alias.
// PublishedVars: The PublishedVars of the Project are those specified in the VarSet of the ProjectOp, reachable using the generated column names.
//
PublishRelOp(_projectAliases.Next(), retExpr, new VarInfoList());
// remove the Input's Vars from scope, unbinding the Input's VarInfos.
ExitExpressionBindingScope(sourceInfo);
RelOpInfo relOpInfo = ConsumeRelOp(retExpr);
return relOpInfo;
}
/// <summary>
/// Build up a Project Op with exactly the Vars that we want. If the input is
/// a Project already, piggyback on it, and get the Vars we want. Otherwise,
/// create a new ProjectOp, and define the specified Vars
///
/// Note that the ProjectOp's output (element) type will be a record with the fields
/// in exactly the order specified by the projectionVars argument
///
/// </summary>
/// <param name="relOpNode">the input relOpNode to cap with a Project</param>
/// <param name="projectionVars">List of vars we are interested in</param>
/// <returns>A ProjectOp that produces the right set of Vars</returns>
private RelOpInfo BuildProjection(Node relOpNode, IEnumerable<Var> projectionVars)
{
DbExpression retExpr = null;
//
// If the input is a ProjectOp, then simply invoke the ProjectOp handler, but
// use the requested Vars instead
//
ProjectOp projectOp = relOpNode.Op as ProjectOp;
if (projectOp != null)
{
retExpr = VisitProject(projectOp, relOpNode, projectionVars);
}
else
{
//
// Otherwise, treat it in a very similar fashion to a normal projectOp. The
// only difference being that we have no VarDefList argument
//
//
// Visit the Input RelOp, bring its Var(s) into scope, and retrieve and consume the RelOpInfo that describes its published Vars
//
RelOpInfo sourceInfo = EnterExpressionBindingScope(relOpNode);
//
// Call CreateProject to convert resolve the projected Vars,
// create the DbProjectExpression, and associate the projected Vars
// with the new DbProjectExpression in the DbExpression -> RelOpInfo map.
//
retExpr = CreateProject(sourceInfo, projectionVars);
// remove the Input's Vars from scope, unbinding the Input's VarInfos.
ExitExpressionBindingScope(sourceInfo);
}
RelOpInfo relOpInfo = ConsumeRelOp(retExpr);
return relOpInfo;
}
DbExpression VisitProject(ProjectOp op, Node n, IEnumerable<Var> varList)
{
//
// Visit the Input RelOp, bring its Var(s) into scope, and retrieve and consume the RelOpInfo that describes its published Vars
//
RelOpInfo sourceInfo = EnterExpressionBindingScope(n.Child0);
//
// With the Input in scope, visit the VarDefs from the ProjectOp's VarDefList and bring their ComputedVars into scope
//
if (n.Children.Count > 1)
{
EnterVarDefListScope(n.Child1);
}
//
// Call CreateProject to convert resolve the projected Vars,
// create the DbProjectExpression, and associate the projected Vars
// with the new DbProjectExpression in the DbExpression -> RelOpInfo map.
//
DbExpression retExpr = CreateProject(sourceInfo, varList);
// Take the local ComputedVars from the ProjectOp's VarDefList out of scope.
if (n.Children.Count > 1)
{
ExitVarDefScope();
}
// remove the Input's Vars from scope, unbinding the Input's VarInfos.
ExitExpressionBindingScope(sourceInfo);
return retExpr;
}
public override DbExpression Visit(ProjectOp op, Node n)
{
return VisitProject(op, n, op.Outputs);
}
public override DbExpression Visit(FilterOp op, Node n)
{
//
// Visit the Input RelOp, bring its Var(s) into scope, and retrieve and consume the RelOpInfo that describes its published Vars
//
RelOpInfo inputInfo = EnterExpressionBindingScope(n.Child0);
//
// Visit the Predicate with the Input Var(s) in scope and assert that the predicate is valid
//
DbExpression predicateExpr = VisitNode(n.Child1);
PlanCompiler.Assert(TypeSemantics.IsPrimitiveType(predicateExpr.ResultType, PrimitiveTypeKind.Boolean), "Invalid FilterOp Predicate (non-ScalarOp or non-Boolean result)");
//
// Create a new DbFilterExpression with the converted Input and Predicate.
// The RelOpState produced from the Input (above) indicates the name that should be used
// in the DbExpressionBinding for the Input expression (this is the name that the
// Input's Vars were brought into scope with in EnterExpressionBindingScope).
//
DbExpression retExpr = inputInfo.CreateBinding().Filter(predicateExpr);
//
// Remove the Input's Var(s) from scope and unbind the Input's VarInfo(s)
//
ExitExpressionBindingScope(inputInfo);
//
// Update the tracked RelOpInfo for the new DbFilterExpression. This consists of:
// PublisherName: The next Filter alias.
// PublishedVars: The PublishedVars of the Filter are the same (now unbound) PublishedVars of its Input.
//
PublishRelOp(_filterAliases.Next(), retExpr, inputInfo.PublishedVars);
return retExpr;
}
private List<DbSortClause> VisitSortKeys(IList<InternalTrees.SortKey> sortKeys)
{
VarVec sortVars = _iqtCommand.CreateVarVec();
List<DbSortClause> sortClauses = new List<DbSortClause>();
foreach (InternalTrees.SortKey sortKey in sortKeys)
{
//
// If we've already seen the same Var, then ignore it
//
if (sortVars.IsSet(sortKey.Var))
{
continue;
}
else
{
sortVars.Set(sortKey.Var);
}
DbSortClause sortClause = null;
DbExpression keyExpression = ResolveVar(sortKey.Var);
if (!string.IsNullOrEmpty(sortKey.Collation))
{
sortClause = (sortKey.AscendingSort ? keyExpression.ToSortClause(sortKey.Collation) : keyExpression.ToSortClauseDescending(sortKey.Collation));
}
else
{
sortClause = (sortKey.AscendingSort ? keyExpression.ToSortClause() : keyExpression.ToSortClauseDescending());
}
sortClauses.Add(sortClause);
}
return sortClauses;
}
public override DbExpression Visit(SortOp op, Node n)
{
//
// Visit the Input RelOp, bring its Var(s) into scope, and retrieve and consume the RelOpInfo that describes its published Vars
//
RelOpInfo inputInfo = EnterExpressionBindingScope(n.Child0);
PlanCompiler.Assert(!n.HasChild1, "SortOp can have only one child");
//
// Visit the SortKeys with the Input's Vars in scope and create the DbSortExpression
//
DbExpression retExpr = inputInfo.CreateBinding().Sort(VisitSortKeys(op.Keys));
//
// Remove the Input's Vars from scope
//
ExitExpressionBindingScope(inputInfo);
//
// Update the tracked RelOpInfo for the new DbSortExpression. This consists of:
// PublisherName: The next Sort alias.
// PublishedVars: The PublishedVars of the Sort are the same as its Input.
//
PublishRelOp(_sortAliases.Next(), retExpr, inputInfo.PublishedVars);
return retExpr;
}
private DbExpression CreateLimitExpression(DbExpression argument, DbExpression limit, bool withTies)
{
PlanCompiler.Assert(!withTies, "Limit with Ties is not currently supported");
return argument.Limit(limit);
}
public override DbExpression Visit(ConstrainedSortOp op, Node n)
{
DbExpression retExpr = null;
RelOpInfo inputInfo = null;
string alias = null;
bool nullSkip = (OpType.Null == n.Child1.Op.OpType);
bool nullLimit = (OpType.Null == n.Child2.Op.OpType);
PlanCompiler.Assert(!nullSkip || !nullLimit, "ConstrainedSortOp with no Skip Count and no Limit?");
if (op.Keys.Count == 0)
{
// Without SortKeys, this ConstrainedSortOp must represent a Limit operation applied to the input.
PlanCompiler.Assert(nullSkip, "ConstrainedSortOp without SortKeys cannot have Skip Count");
//
// Visit the input Node and retrieve its RelOpInfo
//
DbExpression inputExpr = this.VisitNode(n.Child0);
inputInfo = ConsumeRelOp(inputExpr);
//
// Create the DbLimitExpression using the converted form of the input Node's Child2 Node (the Limit Node)
// together with the input DbExpression created above.
//
retExpr = this.CreateLimitExpression(inputExpr, this.VisitNode(n.Child2), op.WithTies);
alias = _limitAliases.Next();
}
else
{
//
// Bring the Input into scope and visit the SortKeys to produce the equivalent SortClauses,
// then remove the Input's Vars from scope.
//
inputInfo = EnterExpressionBindingScope(n.Child0);
List<DbSortClause> sortOrder = VisitSortKeys(op.Keys);
ExitExpressionBindingScope(inputInfo);
//
// SortKeys are present, so one of the following cases must be true:
// - Child1 (Skip Count) is non-NullOp, Child2 (Limit) is non-NullOp => Limit(Skip(input))
// - Child1 (Skip Count) is non-NullOp, Child2 (Limit) is NullOp => Skip(input)
// - Child1 (Skip Count) is NullOp, Child2 (Limit) is non-NullOp => Limit(Sort(input))
//
if (!nullSkip && !nullLimit)
{
// Limit(Skip(input))
retExpr =
this.CreateLimitExpression(
inputInfo.CreateBinding().Skip(sortOrder, VisitChild(n, 1)),
VisitChild(n, 2),
op.WithTies
);
alias = _limitAliases.Next();
}
else if (!nullSkip && nullLimit)
{
// Skip(input)
retExpr = inputInfo.CreateBinding().Skip(sortOrder, VisitChild(n, 1));
alias = _skipAliases.Next();
}
else if (nullSkip && !nullLimit)
{
// Limit(Sort(input))
retExpr =
this.CreateLimitExpression(
inputInfo.CreateBinding().Sort(sortOrder),
VisitChild(n, 2),
op.WithTies
);
alias = _limitAliases.Next();
}
}
//
// Update the tracked RelOpInfo for the new expression. This consists of:
// PublisherName: The next Skip or Limit alias depending on which expression is topmost.
// PublishedVars: The PublishedVars of the Skip/Limit are the same as its Input.
//
PublishRelOp(alias, retExpr, inputInfo.PublishedVars);
return retExpr;
}
public override DbExpression Visit(GroupByOp op, Node n)
{
//
// Track the Vars that are logically published by this GroupBy. These will be
//
VarInfoList publishedVars = new VarInfoList();
//
// Visit the Input, publish its Vars and bring them into scope under a new binding name
//
GroupByScope inputInfo = EnterGroupByScope(n.Child0);
//
// With the Input in scope, visit the VarDefs for the Keys and build the Var to DbExpression map for them
//
EnterVarDefListScope(n.Child1);
//
// With the mappings for the Key Vars in scope, build the Name/DbExpression key column pairs by
// generating a new column alias (prefixed with 'K') and resolving the Var, for each Var in the Keys Var list.
// The list of output Vars that represent aggregates is also built here, by starting with a list
// of all output Vars and removing each Key Var as it is processed.
//
AliasGenerator keyAliases = new AliasGenerator("K");
List<KeyValuePair<string, DbExpression>> keyExprs = new List<KeyValuePair<string, DbExpression>>();
List<Var> outputAggVars = new List<Var>(op.Outputs);
foreach (Var keyVar in op.Keys)
{
//
// Generate the alias and resolve the Key Var. This will find and retrieve the DbExpression to which
// the Key Var maps, which is most likely the VarDef scope that was just entered by visiting the Key VarDefListOp Node.
// Track the Name/DbExpression pairs for use later in CreateGroupByExpression.
//
string keyColName = keyAliases.Next();
keyExprs.Add(new KeyValuePair<string, DbExpression>(keyColName, ResolveVar(keyVar)));
//
// Create a new VarInfo to track this key Var. To begin with it is reachable through
// a property reference of the column under which it is bound, i.e. using the column alias
// generated above, so the VarInfo property path is set up to contain just that name
// (the VarInfo has no binding name until later in this method when PublishRelOp is called).
//
VarInfo keyColInfo = new VarInfo(keyVar);
keyColInfo.PrependProperty(keyColName);
publishedVars.Add(keyColInfo);
//
// Remove the Key Var from the list of Aggregate Outputs
//
outputAggVars.Remove(keyVar);
}
//
// After this point, no Vars in the output Vars list of the GroupBy may be defined by the
// VarDefOps from the Keys VarDefListOp Node, so the Keys VarDefScope should be removed from the scope stack.
//
ExitVarDefScope();
//
// The Vars published by the Input are currently in scope under the binding name that was generated for the Input (Extent0, Filter3, etc).
// This is correct while the Keys are processed, however it is not correct for the aggregates. While the aggregates have access to exactly
// the same Vars, they must be bound under a different name, which will become the GroupVarName used later in this method when a DbGroupExpressionBinding
// is constructed. The GroupVarName is generated simply by appending 'Group' to the (already unique) binding name that was generated for the Input (to yield Extent0Group, Filter3Group, etc).
// In aggregate arguments, the GroupBy input's Vars must be accessed using the Group variable
inputInfo.SwitchToGroupReference();
// Build the map of Var to Aggregate. The Aggregates VarDefListOp Node child of the GroupByOp's Node is
// processed here to build the map. This is the only location in an IQT Command where an AggregateOp is valid.
Dictionary<Var, DbAggregate> aggMap = new Dictionary<Var,DbAggregate>();
Node aggRootNode = n.Child2;
PlanCompiler.Assert(aggRootNode.Op is VarDefListOp, "Invalid Aggregates VarDefListOp Node encountered in GroupByOp");
foreach (Node aggVarDefNode in aggRootNode.Children)
{
VarDefOp aggVarDef = aggVarDefNode.Op as VarDefOp;
PlanCompiler.Assert(aggVarDef != null, "Non-VarDefOp Node encountered as child of Aggregates VarDefListOp Node");
Var aggVar = aggVarDef.Var;
PlanCompiler.Assert(aggVar is ComputedVar, "Non-ComputedVar encountered in Aggregate VarDefOp");
Node aggOpNode = aggVarDefNode.Child0;
DbExpression aggDef = VisitNode(aggOpNode.Child0);
AggregateOp funcAggOp = aggOpNode.Op as AggregateOp;
PlanCompiler.Assert(funcAggOp != null, "Non-Aggregate Node encountered as child of Aggregate VarDefOp Node");
DbFunctionAggregate newFuncAgg;
if (funcAggOp.IsDistinctAggregate)
{
newFuncAgg = funcAggOp.AggFunc.AggregateDistinct(aggDef);
}
else
{
newFuncAgg = funcAggOp.AggFunc.Aggregate(aggDef);
}
PlanCompiler.Assert(outputAggVars.Contains(aggVar), "Defined aggregate Var not in Output Aggregate Vars list?");
aggMap.Add(aggVar, newFuncAgg);
}
//
// The Vars published by the Input should no longer be considered in scope, so call ExitExpressionBindingScope to pop them off the scope stack.
//
ExitGroupByScope(inputInfo);
//
// Process the list of Aggregate Vars using the Var to Aggregate map created in the code above.
// Note that since there is no dedicated Aggregates VarSet on the GroupByOp, it is necessary to
// process the Vars in the OutputVars set, but beginning with the Var that immediately follows
// the last Key Var.
// Each Var is mapped to a previously created Aggregate using the Var to Aggregate map. The end
// result is a list of name CQT Aggregates that can be used in the call to CreateGroupByExpression.
//
AliasGenerator aggAliases = new AliasGenerator("A");
List<KeyValuePair<string, DbAggregate>> aggregates = new List<KeyValuePair<string, DbAggregate>>();
foreach(Var aggVar in outputAggVars)
{
// Generate a new column name for the Aggregate that will be prefixed with 'A'.
string aggColName = aggAliases.Next();
// Map the Var to an Aggregate and add it to the list under the newly generated column name
aggregates.Add(new KeyValuePair<string, DbAggregate>(aggColName, aggMap[aggVar]));
// Create a new VarInfo that will track the Aggregate Var up the CQT. Its property path is
// initialized to the newly generated column name to indicate that the Var must be reached
// with a DbPropertyExpression of the column name.
VarInfo aggColInfo = new VarInfo(aggVar);
aggColInfo.PrependProperty(aggColName);
// Add the Aggregate VarInfo to the list of VarInfos that are tracking the Vars that are
// logically published by the DbGroupByExpression that will result from this method.
publishedVars.Add(aggColInfo);
}
//
// Create the DbGroupByExpression. The binding name of the input is used together with the
// generated group name and the input DbExpression to create a DbGroupExpressionBinding.
// The list of named Keys and Aggregates built above are specified in the call to CreateGroupExpressionBinding.
//
DbExpression retExpr = inputInfo.Binding.GroupBy(keyExprs, aggregates);
PublishRelOp(_groupByAliases.Next(), retExpr, publishedVars);
return retExpr;
}
public override DbExpression Visit(GroupByIntoOp op, Node n)
{
// We should never see this Op
throw EntityUtil.NotSupported();
}
#region JoinOp Conversions - CrossJoinOp, InnerJoinOp, FullOuterJoinOp, LeftOuterJoinOp
/// <summary>
/// Massages the input to a join node.
///
/// If the input is a Filter(ScanTable), we throw in a dummy project over
/// this input. This projectOp simply looks at the "referenced" columns of
/// the table, and uses those as the projection Vars
/// Otherwise, sqlgen does not really know which columns are referenced, and
/// ends up adding a projection with all columns of the table.
///
/// NOTE: We may want to do this for Apply as well
/// </summary>
/// <param name="joinInputNode">one of the inputs to the join node</param>
/// <returns>RelopInfo for the transformed input</returns>
private RelOpInfo VisitJoinInput(Node joinInputNode)
{
RelOpInfo relOpInfo;
if (joinInputNode.Op.OpType == OpType.Filter && joinInputNode.Child0.Op.OpType == OpType.ScanTable)
{
ScanTableOp scanTableOp = (ScanTableOp)joinInputNode.Child0.Op;
//
// #479385: Handle "empty" projection lists
//
if (scanTableOp.Table.ReferencedColumns.IsEmpty)
{
relOpInfo = BuildEmptyProjection(joinInputNode);
}
else
{
relOpInfo = BuildProjection(joinInputNode, scanTableOp.Table.ReferencedColumns);
}
}
else
{
relOpInfo = EnterExpressionBindingScope(joinInputNode, false);
}
return relOpInfo;
}
/// <summary>
/// Called by all Visitor pattern method that handle binary JoinOps (Inner, FullOuter, LeftOuter)
/// </summary>
/// <param name="joinNode">The IQT Node that references the JoinOp</param>
/// <param name="joinKind">The CQT DbExpressionKind that represents the type of join to create</param>
/// <returns></returns>
private DbExpression VisitBinaryJoin(Node joinNode, DbExpressionKind joinKind)
{
//
// Visit and retrieve RelOpInfo for the left Input, but do not bring its published Vars
// into scope. Passing the value false as the 'pushScope' argument indicates that
// EnterExpressionBindingScope should visit the specified Node, retrieve (and remove)
// its RelOpInfo from the DbExpression to RelOpInfo map, but not push that RelOpInfo onto
// the scope stack.
// The Vars are not brought into scope in order to prevent Left-correlation - the Vars of
// the left Input should not be visible to the right Input of the same join.
//
RelOpInfo leftInfo = VisitJoinInput(joinNode.Child0);
// Do the same for the right Input to the join.
RelOpInfo rightInfo = VisitJoinInput(joinNode.Child1);
bool scopesPushed = false;
DbExpression joinCond = null;
if (joinNode.Children.Count > 2)
{
// If a Join condition Node is present, bring the Vars from the left and right arguments into scope
// and visit the Join condition Node's Op to convert the join condition. The scopesPushed flag is updated
// to true to indicate that the Var scopes should be removed from the scope stack when this method completes.
scopesPushed = true;
PushExpressionBindingScope(leftInfo);
PushExpressionBindingScope(rightInfo);
joinCond = VisitNode(joinNode.Child2);
}
else
{
// There is no join condition, so the default condition - DbConstantExpression(True) - is used.
// The Vars from the left and right Inputs to the join need not be brought into scope, so the scopesPushed flag is not updated.
joinCond = DbExpressionBuilder.True;
}
// Create a new DbJoinExpression using bindings created by the RelOpInfos of the left and right Inputs,
// the specified Join type, and the converted or default Join condition DbExpression.
DbExpression retExpr = DbExpressionBuilder.CreateJoinExpressionByKind(
joinKind,
joinCond,
leftInfo.CreateBinding(),
rightInfo.CreateBinding()
);
// Create a new VarInfoList to hold the output Vars that are logically published by the new DbJoinExpression
VarInfoList outputVars = new VarInfoList();
//
// Remove the right argument from scope. If the scopesPushed flag is true then the RelOpInfo
// will be popped from the scope stack.
//
ExitExpressionBindingScope(rightInfo, scopesPushed);
// In the record type that results from the join, the Vars published by the left argument
// must now be accessed using an additional DbPropertyExpression that specifies the column name
// used in the join (which was also the binding name used in the DbExpressionBinding created as part of the join)
// PrependProperty is called on the published Vars of the right argument to reflect this, then they
// are added to the overall set of Vars that are logically published by the new DbJoinExpression.
// Note that calling ExitExpressionBindingScope has already unbound these Vars, making them
// ready for use by the consumer of the new DbJoinExpression.
rightInfo.PublishedVars.PrependProperty(rightInfo.PublisherName);
outputVars.AddRange(rightInfo.PublishedVars);
// Repeat the above steps for the left argument to the join
ExitExpressionBindingScope(leftInfo, scopesPushed);
leftInfo.PublishedVars.PrependProperty(leftInfo.PublisherName);
outputVars.AddRange(leftInfo.PublishedVars);
//
// Update the tracked RelOpInfo for the new DbJoinExpression. This consists of:
// PublisherName: The next Join alias.
// PublishedVars: The PublishedVars of the Join are the (now unbound) PublishedVars of both Inputs, with appropriate column names prepended to their property paths.
//
PublishRelOp(_joinAliases.Next(), retExpr, outputVars);
return retExpr;
}
public override DbExpression Visit(CrossJoinOp op, Node n)
{
// Create a new list of DbExpressionBinding to track the bindings that will be used in the new DbJoinExpression
List<DbExpressionBinding> inputBindings = new List<DbExpressionBinding>();
// Create a new VarInfoList to track the Vars that will be logically published by the new DbJoinExpression
VarInfoList outputVars = new VarInfoList();
//
// For each Input Node:
// 1. Visit and retrieve RelOpInfo for the Node, but do not bring it's Vars into scope
// (again to avoid Left-correlation between join Inputs).
// 2. Use the RelOpInfo to create a correct Expressionbinding and add it to the list of ExpressionBindings
// 3. Call ExitExpressionBinding, indicating that the RelOpInfo was not originally pushed onto the scope stack
// and so its published Vars should simply be unbound and the attempt should not be made to pop it from the scope stack.
// 4. Update the property path for the Vars published by the Input to start with the same column name as was just used in the DbExpressionBinding for the Input
// 5. Add the Vars published by the Input to the overall set of Vars that will be logically published by the new DbJoinExpression (created below).
//
foreach (Node inputNode in n.Children)
{
RelOpInfo inputInfo = VisitJoinInput(inputNode);
inputBindings.Add(inputInfo.CreateBinding());
ExitExpressionBindingScope(inputInfo, false);
inputInfo.PublishedVars.PrependProperty(inputInfo.PublisherName);
outputVars.AddRange(inputInfo.PublishedVars);
}
// Create a new DbJoinExpression from the list of DbExpressionBinding (implicitly creating a CrossJoin)
DbExpression retExpr = DbExpressionBuilder.CrossJoin(inputBindings);
// Update the DbExpression to RelOpInfo map to indicate that the overall set of Vars collected above are logically published by the new DbJoinExpression
// PublisherName will be the next Join alias.
PublishRelOp(_joinAliases.Next(), retExpr, outputVars);
// Return the new DbJoinExpression
return retExpr;
}
public override DbExpression Visit(InnerJoinOp op, Node n)
{
// Use common handling for binary Join Ops
return VisitBinaryJoin(n, DbExpressionKind.InnerJoin);
}
public override DbExpression Visit(LeftOuterJoinOp op, Node n)
{
// Use common handling for binary Join Ops
return VisitBinaryJoin(n, DbExpressionKind.LeftOuterJoin);
}
public override DbExpression Visit(FullOuterJoinOp op, Node n)
{
// Use common handling for binary Join Ops
return VisitBinaryJoin(n, DbExpressionKind.FullOuterJoin);
}
#endregion
#region ApplyOp Conversions - CrossApplyOp, OuterApplyOp
/// <summary>
/// Called by both CrossApply and OuterApply visitor pattern methods - command handling of both types of Apply operation
/// </summary>
/// <param name="applyNode">The Node that references the ApplyOp</param>
/// <param name="applyKind">The CQT DbExpressionKind that corresponds to the ApplyOp (DbExpressionKind.CrossApply for CrossApplyOp, DbExpressionKind.OuterApply for OuterApplyOp)</param>
/// <returns>A new CqtResult containing a DbApplyExpression with the correct ApplyType</returns>
private DbExpression VisitApply(Node applyNode, DbExpressionKind applyKind)
{
//
// Visit the Input and bring its Vars into scope for the Apply
//
RelOpInfo inputInfo = EnterExpressionBindingScope(applyNode.Child0);
//
// Visit the Apply - there is no need to bring its Vars into scope
//
RelOpInfo applyInfo = EnterExpressionBindingScope(applyNode.Child1, false);
DbExpression retExpr = DbExpressionBuilder.CreateApplyExpressionByKind(
applyKind,
inputInfo.CreateBinding(),
applyInfo.CreateBinding());
//
// Unbind the Apply Vars by calling ExitExpressionBindingScope and indicating that the specified scope was not pushed onto the scope stack.
//
ExitExpressionBindingScope(applyInfo, false);
//
// Remove the Input Vars from scope and unbind them
//
ExitExpressionBindingScope(inputInfo);
//
// Update the property path to the Input and Apply vars appropriately based on the names used in their ExpressionBindings, which will then form the column names in the record output type of the AppyExpression
//
inputInfo.PublishedVars.PrependProperty(inputInfo.PublisherName);
applyInfo.PublishedVars.PrependProperty(applyInfo.PublisherName);
//
// Build and publish the set of IQT Vars logically published by the DbApplyExpression
// PublisherName: The next Apply alias.
// PublishedVars: The PublishedVars of the Apply consists of the Vars published by the input plus those published by the apply.
//
VarInfoList outputVars = new VarInfoList();
outputVars.AddRange(inputInfo.PublishedVars);
outputVars.AddRange(applyInfo.PublishedVars);
PublishRelOp(_applyAliases.Next(), retExpr, outputVars);
return retExpr;
}
public override DbExpression Visit(CrossApplyOp op, Node n)
{
// Use common handling for Apply Ops
return VisitApply(n, DbExpressionKind.CrossApply);
}
public override DbExpression Visit(OuterApplyOp op, Node n)
{
// Use common handling for Apply Ops
return VisitApply(n, DbExpressionKind.OuterApply);
}
#endregion
#region SetOp Conversions - ExceptOp, IntersectOp, UnionAllOp
/// <summary>
/// Called by VisitSetOp to convert each argument.
/// Determines whether a column-reordering projection should be applied to
/// the argument, and applies that projection if necessary during conversion
/// to a DbExpression. A different projection is applied if no Nodes higher in
/// the IQT consume the vars produced by the SetOp argument.
/// </summary>
/// <param name="argNode">
/// A Node that provides one of the arguments to the SetOp
/// </param>
/// <param name="outputVars">
/// Defines the expected order of the Output Vars of the SetOp
/// </param>
/// <param name="argVars">
/// The VarMap for the SetOp argument represented by the node.
/// This specifies the Output (SetOp-produced) Var to Input (Argument-produced)
/// Var mappings for the Vars in the outputVars enumerable.
/// </param>
/// <returns>
/// A DbExpression that is the converted form of the argument
/// (with an appropriate column-reording projection applied if necessary)
/// </returns>
private DbExpression VisitSetOpArgument(Node argNode, VarVec outputVars, VarMap argVars)
{
RelOpInfo sourceInfo;
List<Var> projectionVars = new List<Var>();
//
// If the list of output vars is empty, no higher Nodes required the vars produced by
// this SetOp argument. A projection must therefore be applied that performs the equivalent
// of 'SELECT true FROM <SetOp Argument>'.
//
if (outputVars.IsEmpty)
{
sourceInfo = BuildEmptyProjection(argNode);
}
else
{
//
// Build up the list of Vars that we want as the output for this argument.
// The "outputVars" argument defines the order in which we need the outputs
//
foreach (Var v in outputVars)
{
projectionVars.Add(argVars[v]);
}
//
// Build up a ProjectOp over the input that produces the required Output vars
//
sourceInfo = BuildProjection(argNode, projectionVars);
}
return sourceInfo.Publisher;
}
/// <summary>
/// Called by UnionAll, Intersect and Except (SetOp) visitor pattern methods
/// </summary>
/// <param name="op">The visited SetOp</param>
/// <param name="n">The Node that references the SetOp</param>
/// <param name="alias">Alias to use when publishing the SetOp's Vars</param>
/// <param name="setOpBuilder">Callback to construct the SetOp DbExpression from the left and right arguments</param>
/// <returns>The DbExpression equivalent of the SetOp</returns>
private DbExpression VisitSetOp(SetOp op, Node n, AliasGenerator alias, Func<DbExpression, DbExpression, DbExpression> setOpExpressionBuilder)
{
//
// To be convertible to a CQT Except/Intersect/DbUnionAllExpression, the SetOp must have exactly 2 arguments.
//
AssertBinary(n);
//
// Convert the left and right arguments to expressions.
//
DbExpression left = VisitSetOpArgument(n.Child0, op.Outputs, op.VarMap[0]);
DbExpression right = VisitSetOpArgument(n.Child1, op.Outputs, op.VarMap[1]);
//
// If the output of the SetOp is a collection of records then the Vars produced
// by the SetOp must be prepended with the names of the record type's columns as
// they are tracked up the tree by VarInfo instances.
//
CollectionType outputType = TypeHelpers.GetEdmType<CollectionType>(TypeHelpers.GetCommonTypeUsage(left.ResultType, right.ResultType));
IEnumerator<EdmProperty> properties = null;
RowType outputElementType = null;
if (TypeHelpers.TryGetEdmType<RowType>(outputType.TypeUsage, out outputElementType))
{
properties = outputElementType.Properties.GetEnumerator();
}
//
// The published Vars of the DbExpression produced from the SetOp must be its Output Vars.
// These Output Vars are mapped to the Vars of each of the SetOp's arguments using an array
// of VarMaps (one for each argument) on the SetOp.
// A VarInfo instance is added to the published Vars list for each Output Var of the SetOp.
// If the output type of the SetOp is a collection of a record type then each Var's PropertyPath
// is updated to be the name of the corresponding record column.
//
VarInfoList publishedVars = new VarInfoList();
foreach (Var outputVar in op.Outputs)
{
VarInfo newVarInfo = new VarInfo(outputVar);
// Prepend a property name to this var, if the output is a record type
if (outputElementType != null)
{
if (!properties.MoveNext())
{
PlanCompiler.Assert(false, "Record columns don't match output vars");
}
newVarInfo.PrependProperty(properties.Current.Name);
}
publishedVars.Add(newVarInfo);
}
DbExpression retExpr = setOpExpressionBuilder(left, right);
PublishRelOp(alias.Next(), retExpr, publishedVars);
return retExpr;
}
public override DbExpression Visit(UnionAllOp op, Node n)
{
return VisitSetOp(op, n, _unionAllAliases, DbExpressionBuilder.UnionAll);
}
public override DbExpression Visit(IntersectOp op, Node n)
{
return VisitSetOp(op, n, _intersectAliases, DbExpressionBuilder.Intersect);
}
public override DbExpression Visit(ExceptOp op, Node n)
{
return VisitSetOp(op, n, _exceptAliases, DbExpressionBuilder.Except);
}
#endregion
public override DbExpression Visit(DerefOp op, Node n)
{
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(DistinctOp op, Node n)
{
//
// Build a projection above the input that gets the "keys" of the
// DistinctOp.
//
RelOpInfo sourceInfo = BuildProjection(n.Child0, op.Keys);
//
// Build the Distinct expression now
//
DbExpression distinctExpr = sourceInfo.Publisher.Distinct();
//
// Publish the DbDistinctExpression's Vars:
// PublisherName: The next Distinct alias
// PublishedVars: The PublishedVars of the Distinct are the same (rebound) Vars published by its input
//
PublishRelOp(_distinctAliases.Next(), distinctExpr, sourceInfo.PublishedVars);
return distinctExpr;
}
/// <summary>
/// Convert SRO(e) => NewMultiset(Element(e'))
/// where e' is the CTree version of e
/// Add a Project over e, if it does not already have a ProjectOp
/// </summary>
/// <param name="op"></param>
/// <param name="n"></param>
/// <returns></returns>
public override DbExpression Visit(SingleRowOp op, Node n)
{
RelOpInfo inputInfo;
DbExpression inputExpr;
//
// Build a Projection over the child - sqlgen gets very confused otherwise
//
if (n.Child0.Op.OpType != OpType.Project)
{
ExtendedNodeInfo childNodeInfo = _iqtCommand.GetExtendedNodeInfo(n.Child0);
//
// #484757: Handle "empty" projection lists due to projection pruning
//
if (childNodeInfo.Definitions.IsEmpty)
{
inputInfo = BuildEmptyProjection(n.Child0);
}
else
{
inputInfo = BuildProjection(n.Child0, childNodeInfo.Definitions);
}
inputExpr = inputInfo.Publisher;
}
else
{
inputExpr = VisitNode(n.Child0);
AssertRelOp(inputExpr);
inputInfo = ConsumeRelOp(inputExpr);
}
DbElementExpression elementExpr = inputExpr.Element();
List<DbExpression> collectionElements = new List<DbExpression>();
collectionElements.Add(elementExpr);
DbNewInstanceExpression collectionExpr = DbExpressionBuilder.NewCollection(collectionElements);
PublishRelOp(_elementAliases.Next(), collectionExpr, inputInfo.PublishedVars);
return collectionExpr;
}
/// <summary>
/// Convert SingleRowTableOp into NewMultisetOp(1) - a single element
/// collection. The element type of the collection doesn't really matter
/// </summary>
/// <param name="op">SingleRowTableOp</param>
/// <param name="n">current subtree</param>
/// <returns>CQT expression</returns>
public override DbExpression Visit(SingleRowTableOp op, Node n)
{
DbNewInstanceExpression collectionExpr = DbExpressionBuilder.NewCollection(new [] { DbExpressionBuilder.Constant(1) });
PublishRelOp(_singleRowTableAliases.Next(), collectionExpr, new VarInfoList());
return collectionExpr;
}
#endregion
#region Variable Definition Ops
public override DbExpression Visit(VarDefOp op, Node n)
{
//
// VarDef and VarDefList are handled in the conversion of the Ops in which they are valid (by calls to EnterVarDefScope/EnterVarDefListScope).
// If this method is called a VarDefOp exists in an invalid location in the IQT
//
PlanCompiler.Assert(false, "Unexpected VarDefOp");
throw EntityUtil.NotSupported(System.Data.Entity.Strings.Iqt_CTGen_UnexpectedVarDef);
}
public override DbExpression Visit(VarDefListOp op, Node n)
{
//
// VarDef and VarDefList are handled in the conversion of the Ops in which they are valid (by calls to EnterVarDefScope/EnterVarDefListScope).
// If this method is called a VarDefListOp exists in an invalid location in the IQT
//
PlanCompiler.Assert(false, "Unexpected VarDefListOp");
throw EntityUtil.NotSupported(System.Data.Entity.Strings.Iqt_CTGen_UnexpectedVarDefList);
}
#endregion
#region PhysicalOps
/// <summary>
/// Translates the PhysicalProjectOp. Handles two cases. If the child is a ProjectOp,
/// then we simply piggyback on the ProjectOp method, but with our list of Vars.
///
/// Otherwise, we visit the child, and then create a DbProjectExpression above it.
///
/// The reason we special case the first scenario is because we do not want to add
/// an extra Project over a Project-over-Sort expression tree. This causes bad
/// problems later down the line
/// </summary>
/// <param name="op">the PhysicalProjectOp</param>
/// <param name="n">current subtree</param>
/// <returns>the CQT expression corresponding to this subtree</returns>
public override DbExpression Visit(PhysicalProjectOp op, Node n)
{
PlanCompiler.Assert(n.Children.Count == 1, "more than one input to physicalProjectOp?");
//
// Prune the output vars from the PhysicalProjectOp
//
VarList prunedOutputs = new VarList();
foreach (Var v in op.Outputs)
{
if (!prunedOutputs.Contains(v))
{
prunedOutputs.Add(v);
}
}
op.Outputs.Clear();
op.Outputs.AddRange(prunedOutputs);
//
// Build a Projection over the input with exactly the Vars that we want
//
RelOpInfo sourceInfo = BuildProjection(n.Child0, op.Outputs);
return sourceInfo.Publisher;
}
public override DbExpression Visit(SingleStreamNestOp op, Node n)
{
throw EntityUtil.NotSupported();
}
public override DbExpression Visit(MultiStreamNestOp op, Node n)
{
throw EntityUtil.NotSupported();
}
#endregion
#endregion
}
}
|