hqliter.cpp 13 KB

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