hqlalias.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #include "platform.h"
  15. #include "jlib.hpp"
  16. #include "jset.hpp"
  17. #include "hql.hpp"
  18. #include "hqlutil.hpp"
  19. #include "hqltrans.ipp"
  20. #include "hqlalias.hpp"
  21. //---------------------------------------------------------------------------------------------------------------------
  22. static bool isSubsetOf(IHqlExpression * search, IHqlExpression * expr)
  23. {
  24. loop
  25. {
  26. if (search->queryBody() == expr->queryBody())
  27. return true;
  28. if (expr->getOperator() != no_and)
  29. return false;
  30. //Check in order most likely to no recurse too deeply.
  31. if (isSubsetOf(search, expr->queryChild(1)))
  32. return true;
  33. expr = expr->queryChild(0);
  34. }
  35. }
  36. ConditionItem::ConditionItem(IHqlExpression * _expr, ConditionSet * _parent)
  37. : expr(_expr), parent(_parent)
  38. {
  39. }
  40. bool ConditionItem::isAlwaysConditionalOn(IHqlExpression * search) const
  41. {
  42. if (isSubsetOf(search, expr))
  43. return true;
  44. if (!parent)
  45. return false;
  46. return parent->isAlwaysConditionalOn(search);
  47. }
  48. //---------------------------------------------------------------------------------------------------------------------
  49. bool ConditionSet::addOrCondition(IHqlExpression * expr, ConditionSet * parent)
  50. {
  51. if (unconditional)
  52. return false;
  53. if (!expr)
  54. {
  55. setUnconditional();
  56. return true;
  57. }
  58. ForEachItemIn(iMerge, conditions)
  59. {
  60. ConditionItem & cur = conditions.item(iMerge);
  61. if (cur.equals(expr, parent))
  62. return false;
  63. }
  64. conditions.append(*new ConditionItem(expr, parent));
  65. return true;
  66. }
  67. void ConditionSet::setUnconditional()
  68. {
  69. unconditional = true;
  70. conditions.kill();
  71. }
  72. /*
  73. IHqlExpression * ConditionSet::getGuardCondition() const
  74. {
  75. if (unconditional)
  76. return NULL;
  77. HqlExprArray values;
  78. ForEachItemIn(i, conditions)
  79. values.append(*conditions.item(i).getCondition());
  80. OwnedITypeInfo boolType = makeBoolType();
  81. return createBalanced(no_or, boolType, values);
  82. }
  83. */
  84. bool ConditionSet::isAlwaysConditionalOn(IHqlExpression * expr)
  85. {
  86. if (!expr)
  87. return true;
  88. if (unconditional)
  89. return false;
  90. //Cache the previous search value to avoid a potentially exponential search of the caller tree.
  91. if (expr == isAlwaysCache.search)
  92. return isAlwaysCache.value;
  93. bool matches = true;
  94. ForEachItemIn(i, conditions)
  95. {
  96. ConditionItem & cur = conditions.item(i);
  97. if (!cur.isAlwaysConditionalOn(expr))
  98. {
  99. matches = false;
  100. break;
  101. }
  102. }
  103. isAlwaysCache.search.set(expr);
  104. isAlwaysCache.value = matches;
  105. return matches;
  106. }
  107. //---------------------------------------------------------------------------------------------------------------------
  108. void ConditionTracker::pushCondition(IHqlExpression * expr, ConditionSet * parent)
  109. {
  110. assertex(expr);
  111. conditionStack.append(expr);
  112. parentStack.append(parent);
  113. }
  114. void ConditionTracker::popCondition()
  115. {
  116. conditionStack.pop();
  117. parentStack.pop();
  118. }
  119. bool ConditionTracker::addActiveCondition(ConditionSet & conditions)
  120. {
  121. if (conditionStack.ordinality() == 0)
  122. return conditions.addOrCondition(NULL, NULL);
  123. return conditions.addOrCondition(conditionStack.tos(), parentStack.tos());
  124. }
  125. //---------------------------------------------------------------------------------------------------------------------
  126. class NestedIfInfo : public NewTransformInfo
  127. {
  128. public:
  129. NestedIfInfo(IHqlExpression * _original) : NewTransformInfo(_original)
  130. {
  131. isShared = false;
  132. containsIf = false;
  133. conditions = NULL;
  134. }
  135. ~NestedIfInfo() { delete conditions; }
  136. ConditionSet * queryConditions()
  137. {
  138. if (!conditions)
  139. conditions = new ConditionSet;
  140. return conditions;
  141. }
  142. public:
  143. ConditionSet * conditions;
  144. bool isShared;
  145. bool containsIf;
  146. };
  147. //MORE: Could remove dependancy on insideCompound if it was ok to have compound operators scattered through the
  148. // contents of a compound item. Probably would cause few problems, and would make life simpler
  149. class NestedIfTransformer : public NewHqlTransformer
  150. {
  151. public:
  152. NestedIfTransformer();
  153. IHqlExpression * process(IHqlExpression * expr);
  154. bool process(const HqlExprArray & exprs, HqlExprArray & transformed);
  155. protected:
  156. void analyseGatherIfs(IHqlExpression * expr);
  157. void analyseNoteConditions(IHqlExpression * expr);
  158. virtual void analyseExpr(IHqlExpression * expr);
  159. virtual IHqlExpression * createTransformed(IHqlExpression * expr);
  160. virtual ANewTransformInfo * createTransformInfo(IHqlExpression * expr)
  161. {
  162. return new NestedIfInfo(expr);
  163. }
  164. inline NestedIfInfo * queryBodyExtra(IHqlExpression * expr) { return static_cast<NestedIfInfo *>(queryTransformExtra(expr->queryBody())); }
  165. protected:
  166. unsigned numIfs;
  167. ConditionTracker tracker;
  168. };
  169. static HqlTransformerInfo nestedIfTransformerInfo("NestedIfTransformer");
  170. NestedIfTransformer::NestedIfTransformer() : NewHqlTransformer(nestedIfTransformerInfo)
  171. {
  172. numIfs = 0;
  173. }
  174. void NestedIfTransformer::analyseExpr(IHqlExpression * expr)
  175. {
  176. IHqlExpression * body = expr->queryBody();
  177. switch (pass)
  178. {
  179. case 0:
  180. analyseGatherIfs(body);
  181. break;
  182. case 1:
  183. analyseNoteConditions(body);
  184. break;
  185. }
  186. }
  187. void NestedIfTransformer::analyseGatherIfs(IHqlExpression * expr)
  188. {
  189. if (expr->getOperator() == no_if)
  190. numIfs++;
  191. NestedIfInfo * extra = queryBodyExtra(expr);
  192. if (alreadyVisited(expr))
  193. {
  194. extra->isShared = true;
  195. if (extra->containsIf)
  196. numIfs++;
  197. return;
  198. }
  199. unsigned prevIfCount = numIfs;
  200. NewHqlTransformer::analyseExpr(expr);
  201. if (prevIfCount != numIfs)
  202. extra->containsIf = true;
  203. }
  204. void NestedIfTransformer::analyseNoteConditions(IHqlExpression * expr)
  205. {
  206. node_operator op = expr->getOperator();
  207. NestedIfInfo * extra = queryBodyExtra(expr);
  208. if (extra->isShared || (op == no_if))
  209. {
  210. if (!tracker.addActiveCondition(*extra->queryConditions()))
  211. return;
  212. }
  213. if (!extra->containsIf)
  214. return;
  215. if (op == no_if)
  216. {
  217. IHqlExpression * cond = expr->queryChild(0);
  218. OwnedHqlExpr normalCond = getNormalizedCondition(cond);
  219. analyseExpr(cond);
  220. tracker.pushCondition(normalCond, extra->queryConditions());
  221. analyseExpr(expr->queryChild(1));
  222. tracker.popCondition();
  223. IHqlExpression * falseExpr = queryRealChild(expr, 2);
  224. if (falseExpr)
  225. {
  226. OwnedHqlExpr inverseCond = getInverse(normalCond);
  227. tracker.pushCondition(inverseCond, extra->queryConditions());
  228. analyseExpr(falseExpr);
  229. tracker.popCondition();
  230. }
  231. }
  232. else
  233. {
  234. NewHqlTransformer::analyseExpr(expr);
  235. }
  236. }
  237. IHqlExpression * NestedIfTransformer::createTransformed(IHqlExpression * expr)
  238. {
  239. if (expr->getOperator() == no_if)
  240. {
  241. IHqlExpression * cond = expr->queryChild(0);
  242. IHqlExpression * falseExpr = queryRealChild(expr, 2);
  243. OwnedHqlExpr normalCond = getNormalizedCondition(cond);
  244. NestedIfInfo * extra = queryBodyExtra(expr);
  245. IHqlExpression * selected = NULL;
  246. if (extra->queryConditions()->isAlwaysConditionalOn(normalCond))
  247. {
  248. selected = expr->queryChild(1);
  249. }
  250. else if (falseExpr)
  251. {
  252. OwnedHqlExpr inverseCond = getInverse(normalCond);
  253. if (extra->queryConditions()->isAlwaysConditionalOn(inverseCond))
  254. selected = falseExpr;
  255. }
  256. if (selected)
  257. {
  258. const char * branch = (selected == falseExpr) ? "false" : "true";
  259. StringBuffer exprText, locationText;
  260. appendLocation(locationText, queryLocation(expr), ": ");
  261. DBGLOG("%s%s replaced with %s branch since condition always %s", locationText.str(), queryChildNodeTraceText(exprText, expr), branch, branch);
  262. OwnedHqlExpr ret = transform(selected);
  263. return cloneMissingAnnotations(expr, ret);
  264. }
  265. }
  266. return NewHqlTransformer::createTransformed(expr);
  267. }
  268. IHqlExpression * NestedIfTransformer::process(IHqlExpression * expr)
  269. {
  270. analyse(expr, 0);
  271. if (numIfs < 2)
  272. return LINK(expr);
  273. analyse(expr, 1);
  274. return transformRoot(expr);
  275. }
  276. bool NestedIfTransformer::process(const HqlExprArray & exprs, HqlExprArray & transformed)
  277. {
  278. ForEachItemIn(i1, exprs)
  279. analyse(&exprs.item(i1), 0);
  280. if (numIfs < 2)
  281. return false;
  282. ForEachItemIn(i2, exprs)
  283. analyse(&exprs.item(i2), 1);
  284. transformRoot(exprs, transformed);
  285. return true;
  286. }
  287. IHqlExpression * optimizeNestedConditional(IHqlExpression * expr)
  288. {
  289. NestedIfTransformer transformer;
  290. return transformer.process(expr);
  291. }
  292. void optimizeNestedConditional(HqlExprArray & exprs)
  293. {
  294. NestedIfTransformer transformer;
  295. HqlExprArray transformed;
  296. if (transformer.process(exprs, transformed))
  297. exprs.swapWith(transformed);
  298. }