hqliter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 "jliball.hpp"
  15. #include "hql.hpp"
  16. #include "platform.h"
  17. #include "jlib.hpp"
  18. #include "jexcept.hpp"
  19. #include "jmisc.hpp"
  20. #include "javahash.hpp"
  21. #include "eclhelper.hpp"
  22. #include "hqlfunc.hpp"
  23. #include "hqlhtcpp.ipp"
  24. #include "hqlwcpp.hpp"
  25. #include "hqlcpputil.hpp"
  26. #include "hqlcerrors.hpp"
  27. #include "hqlcatom.hpp"
  28. #include "hqlpmap.hpp"
  29. #include "hqlthql.hpp"
  30. #include "hqlcset.ipp"
  31. #include "hqlfold.hpp"
  32. #include "hqltcppc.ipp"
  33. #include "hqlutil.hpp"
  34. #include "hqlcse.ipp"
  35. #include "hqliter.ipp"
  36. //===========================================================================
  37. bool isSequenceRoot(IHqlExpression * expr)
  38. {
  39. switch (expr->getOperator())
  40. {
  41. case no_newkeyindex:
  42. case no_table:
  43. case no_select:
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool canBuildSequenceInline(IHqlExpression * expr)
  49. {
  50. loop
  51. {
  52. switch (expr->getOperator())
  53. {
  54. case no_cachealias:
  55. expr = expr->queryChild(1);
  56. break;
  57. case no_newkeyindex:
  58. case no_table:
  59. case no_select:
  60. return true;
  61. case no_sorted:
  62. case no_distributed:
  63. case no_preservemeta:
  64. case no_grouped:
  65. case no_preload:
  66. case no_limit:
  67. case no_keyedlimit:
  68. case no_choosen:
  69. case no_filter:
  70. case no_hqlproject:
  71. case no_newusertable:
  72. case no_compound_diskread:
  73. case no_compound_disknormalize:
  74. case no_compound_diskaggregate:
  75. case no_compound_diskcount:
  76. case no_compound_diskgroupaggregate:
  77. case no_compound_indexread:
  78. case no_compound_indexnormalize:
  79. case no_compound_indexaggregate:
  80. case no_compound_indexcount:
  81. case no_compound_indexgroupaggregate:
  82. case no_compound_childread:
  83. case no_compound_childnormalize:
  84. case no_compound_childaggregate:
  85. case no_compound_childcount:
  86. case no_compound_childgroupaggregate:
  87. case no_compound_fetch:
  88. case no_compound_selectnew:
  89. case no_compound_inline:
  90. case no_alias_scope:
  91. expr = expr->queryChild(0);
  92. break;
  93. default:
  94. return false;
  95. }
  96. }
  97. }
  98. //-------------------------------------------------------------------
  99. void TransformSequenceBuilder::buildSequence(BuildCtx & ctx, BuildCtx * declarectx, IHqlExpression * expr)
  100. {
  101. node_operator op = expr->getOperator();
  102. switch (op)
  103. {
  104. case no_cachealias:
  105. buildSequence(ctx, declarectx, expr->queryChild(1));
  106. return;
  107. case no_newkeyindex:
  108. case no_table:
  109. case no_fetch:
  110. case no_select:
  111. case no_anon:
  112. case no_pseudods:
  113. break;
  114. case no_hqlproject:
  115. case no_newusertable:
  116. buildSequence(ctx, NULL, expr->queryChild(0));
  117. break;
  118. default:
  119. buildSequence(ctx, declarectx, expr->queryChild(0));
  120. break;
  121. }
  122. switch (op)
  123. {
  124. case no_filter:
  125. {
  126. HqlExprAttr cond;
  127. ForEachChild(i, expr)
  128. {
  129. IHqlExpression * cur = queryRealChild(expr, i);
  130. if (cur && i != 0)
  131. extendConditionOwn(cond, no_and, LINK(cur));
  132. }
  133. OwnedHqlExpr test = getInverse(cond);
  134. if (translator.queryOptions().foldFilter)
  135. test.setown(foldScopedHqlExpression(expr->queryChild(0)->queryNormalizedSelector(), test));
  136. if (translator.queryOptions().spotCSE)
  137. test.setown(spotScalarCSE(test));
  138. translator.buildFilteredReturn(ctx, test, failedFilterValue);
  139. }
  140. break;
  141. case no_hqlproject:
  142. {
  143. IHqlExpression * dataset = expr->queryChild(0);
  144. OwnedHqlExpr leftSelect = createSelector(no_left, dataset, querySelSeq(expr));
  145. OwnedHqlExpr newSelect = ensureActiveRow(dataset->queryNormalizedSelector());
  146. OwnedHqlExpr transform = replaceSelector(expr->queryChild(1), leftSelect, newSelect);
  147. //MORE: Calculate cses at this point, but we really need to have removed hqlprojects for cse to work...
  148. BuildCtx & createctx = declarectx ? *declarectx : ctx;
  149. Owned<BoundRow> tempRow = translator.declareTempAnonRow(createctx, ctx, expr);
  150. BuildCtx subctx(ctx);
  151. translator.associateSkipReturnMarker(subctx, failedFilterValue, NULL);
  152. translator.doInlineTransform(subctx, transform, tempRow);
  153. translator.bindTableCursor(ctx, expr, tempRow->queryBound());
  154. }
  155. break;
  156. case no_newusertable:
  157. {
  158. IHqlExpression * transform = expr->queryChild(2);
  159. BuildCtx & createctx = declarectx ? *declarectx : ctx;
  160. Owned<BoundRow> tempRow = translator.declareTempAnonRow(createctx, ctx, expr);
  161. BuildCtx subctx(ctx);
  162. translator.associateSkipReturnMarker(subctx, failedFilterValue, NULL);
  163. translator.doInlineTransform(subctx, transform, tempRow);
  164. translator.bindTableCursor(ctx, expr, tempRow->queryBound());
  165. }
  166. break;
  167. }
  168. }
  169. //------------------------------------------------------------------------------------------
  170. //Gather the different select levels, return the root dataset (if not in scope)
  171. IHqlExpression * gatherSelectorLevels(HqlExprArray & iterators, IHqlExpression * expr)
  172. {
  173. loop
  174. {
  175. IHqlExpression * root = queryRoot(expr);
  176. if (!root || root->getOperator() != no_select)
  177. return expr;
  178. iterators.add(*LINK(expr), 0);
  179. if (!root->hasProperty(newAtom))
  180. return NULL;
  181. expr = root->queryChild(0);
  182. }
  183. }
  184. void CompoundIteratorBuilder::bindParentCursors(BuildCtx & ctx, CursorArray & cursors)
  185. {
  186. OwnedHqlExpr colocal = createQuoted("activity", makeVoidType());
  187. ForEachItemIn(i, cursors)
  188. {
  189. BoundRow & cur = cursors.item(i);
  190. //Very similar to code in the extract builder
  191. OwnedHqlExpr colocalBound = addMemberSelector(cur.queryBound(), colocal);
  192. ctx.associateOwn(*cur.clone(colocalBound));
  193. }
  194. }
  195. void CompoundIteratorBuilder::buildCompoundIterator(BuildCtx & initctx, HqlExprArray & iterators, CursorArray & cursors)
  196. {
  197. StringBuffer s;
  198. declarectx.addQuoted("RtlCompoundIterator iter;");
  199. initctx.addQuoted(s.clear().append("iter.init(").append(iterators.ordinality()).append(");"));
  200. ForEachItemIn(i, iterators)
  201. {
  202. StringBuffer iterName, cursorName;
  203. createSingleLevelIterator(iterName, cursorName, &iterators.item(i), cursors);
  204. initctx.addQuoted(s.clear().append("iter.addIter(").append(i).append(",&").append(iterName).append(",&").append(cursorName).append(");"));
  205. }
  206. }
  207. void CompoundIteratorBuilder::createSingleLevelIterator(StringBuffer & iterName, StringBuffer & cursorName, IHqlExpression * expr, CursorArray & cursors)
  208. {
  209. LinkedHqlExpr cur = expr;
  210. IHqlExpression * root = queryRoot(cur);
  211. assertex(root->getOperator() == no_select);
  212. //First remove any new attributes from selector, since parent cursor is guaranteed to be in scope at this point.
  213. IHqlExpression * normalized = root->queryNormalizedSelector();
  214. if (root != normalized)
  215. cur.setown(replaceExpression(cur, root, normalized));
  216. createSingleIterator(iterName, cur, cursors);
  217. translator.getUniqueId(cursorName.append("row"));
  218. OwnedHqlExpr row = createVariable(cursorName, makeRowReferenceType(cur));
  219. declarectx.addDeclare(row);
  220. cursors.append(*translator.createTableCursor(cur, row, no_none, NULL));
  221. }
  222. void CompoundIteratorBuilder::createSingleIterator(StringBuffer & iterName, IHqlExpression * expr, CursorArray & cursors)
  223. {
  224. StringBuffer s;
  225. translator.getUniqueId(iterName.clear().append("iter"));
  226. //MORE: Nested class/...
  227. BuildCtx classctx(nestedctx);
  228. translator.beginNestedClass(classctx, iterName, "IRtlDatasetSimpleCursor", NULL, NULL);
  229. translator.queryEvalContext(classctx)->ensureHelpersExist();
  230. if (isArrayRowset(expr->queryType()))
  231. {
  232. classctx.addQuoted("byte * * end;");
  233. classctx.addQuoted("byte * * cur;");
  234. }
  235. else
  236. {
  237. classctx.addQuoted("byte * end;");
  238. classctx.addQuoted("byte * cur;");
  239. }
  240. IHqlExpression * root = queryRoot(expr);
  241. if (expr->queryBody() == root)
  242. {
  243. BuildCtx firstctx(classctx);
  244. firstctx.addQuotedCompound("virtual const byte * first()");
  245. createRawFirstFunc(firstctx, expr, cursors);
  246. BuildCtx nextctx(classctx);
  247. nextctx.addQuotedCompound("virtual const byte * next()");
  248. createRawNextFunc(nextctx, expr, cursors);
  249. }
  250. else
  251. {
  252. BuildCtx rawfirstctx(classctx);
  253. rawfirstctx.addQuotedCompound("inline const byte * rawFirst()");
  254. createRawFirstFunc(rawfirstctx, root, cursors);
  255. BuildCtx rawnextctx(classctx);
  256. rawnextctx.addQuotedCompound("virtual const byte * rawNext()");
  257. createRawNextFunc(rawnextctx, root, cursors);
  258. OwnedHqlExpr failValue = createTranslatedOwned(createValue(no_nullptr, makeVoidType()));
  259. TransformSequenceBuilder checkValidBuilder(translator, failValue);
  260. BuildCtx checkctx(classctx);
  261. checkctx.addQuotedCompound("inline const byte * checkValid()");
  262. bindParentCursors(checkctx, cursors);
  263. if (isArrayRowset(expr->queryType()))
  264. translator.bindTableCursor(checkctx, root, "(*cur)");
  265. else
  266. translator.bindTableCursor(checkctx, root, "cur");
  267. checkValidBuilder.buildSequence(checkctx, &classctx, expr);
  268. BoundRow * match = translator.resolveSelectorDataset(checkctx, expr);
  269. assertex(match);
  270. OwnedHqlExpr row = getPointer(match->queryBound());
  271. checkctx.addReturn(row);
  272. BuildCtx firstctx(classctx);
  273. firstctx.addQuotedCompound("virtual const byte * first()");
  274. firstctx.addQuoted("if (!rawFirst()) return NULL;");
  275. firstctx.addQuotedCompound("for (;;)");
  276. firstctx.addQuoted("const byte * valid = checkValid(); if (valid) return valid;");
  277. firstctx.addQuoted("if (!rawNext()) return NULL;");
  278. BuildCtx nextctx(classctx);
  279. nextctx.addQuotedCompound("virtual const byte * next()");
  280. nextctx.addQuotedCompound("for (;;)");
  281. nextctx.addQuoted("if (!rawNext()) return NULL;");
  282. nextctx.addQuoted("const byte * valid = checkValid(); if (valid) return valid;");
  283. }
  284. translator.endNestedClass();
  285. }
  286. void CompoundIteratorBuilder::createRawFirstFunc(BuildCtx & ctx, IHqlExpression * expr, CursorArray & cursors)
  287. {
  288. bool isOutOfLine = isArrayRowset(expr->queryType());
  289. bindParentCursors(ctx, cursors);
  290. CHqlBoundExpr boundDs;
  291. translator.buildDataset(ctx, expr, boundDs, queryNaturalFormat(expr->queryType()));
  292. if (isOutOfLine)
  293. {
  294. StringBuffer s;
  295. s.clear().append("cur = "); // more: should really be const...
  296. translator.generateExprCpp(s, boundDs.expr).append(";");
  297. ctx.addQuoted(s);
  298. OwnedHqlExpr count = translator.getBoundCount(boundDs);
  299. s.clear().append("end = cur+");
  300. translator.generateExprCpp(s, count).append(";");
  301. ctx.addQuoted(s);
  302. ctx.addQuoted("return (cur < end) ? *cur : NULL;");
  303. }
  304. else
  305. {
  306. OwnedHqlExpr address = getPointer(boundDs.expr);
  307. StringBuffer s;
  308. s.clear().append("cur = (byte *)"); // more: should really be const...
  309. translator.generateExprCpp(s, address).append(";");
  310. ctx.addQuoted(s);
  311. OwnedHqlExpr length = translator.getBoundLength(boundDs);
  312. s.clear().append("end = cur+");
  313. translator.generateExprCpp(s, length).append(";");
  314. ctx.addQuoted(s);
  315. ctx.addQuoted("return (cur < end) ? cur : NULL;");
  316. }
  317. }
  318. void CompoundIteratorBuilder::createRawNextFunc(BuildCtx & ctx, IHqlExpression * expr, CursorArray & cursors)
  319. {
  320. if (isArrayRowset(expr->queryType()))
  321. {
  322. ctx.addQuoted("cur++;");
  323. ctx.addQuoted("return (cur < end) ? *cur : NULL;");
  324. }
  325. else
  326. {
  327. bindParentCursors(ctx, cursors);
  328. translator.bindTableCursor(ctx, expr, "cur");
  329. CHqlBoundExpr bound;
  330. translator.getRecordSize(ctx, expr, bound);
  331. StringBuffer s;
  332. s.clear().append("cur+=");
  333. translator.generateExprCpp(s, bound.expr).append(";");
  334. ctx.addQuoted(s);
  335. ctx.addQuoted("return (cur < end) ? cur : NULL;");
  336. }
  337. }
  338. //------------------------------------------------------------------------------------------