hqlcpputil.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "jexcept.hpp"
  17. #include "jmisc.hpp"
  18. #include "workunit.hpp"
  19. #include "hql.hpp"
  20. #include "hqlexpr.hpp"
  21. #include "hqlfold.hpp"
  22. #include "hqlstmt.hpp"
  23. #include "hqltrans.ipp"
  24. #include "hqlutil.hpp"
  25. #include "hqlattr.hpp"
  26. #include "hqlcatom.hpp"
  27. #include "hqlfunc.hpp"
  28. #include "hqlcpp.ipp"
  29. #include "hqlcpputil.hpp"
  30. //===========================================================================
  31. static ITypeInfo * cachedVoidType;
  32. static IHqlExpression * cachedBoolValue[2];
  33. static IHqlExpression * cachedZero;
  34. static IHqlExpression * cachedNullChar;
  35. static IHqlExpression * defaultAttrExpr;
  36. static IHqlExpression * selfAttrExpr;
  37. ITypeInfo * boolType;
  38. ITypeInfo * sizetType;
  39. ITypeInfo * signedType;
  40. ITypeInfo * unsignedType;
  41. ITypeInfo * defaultIntegralType;
  42. ITypeInfo * counterType;
  43. ITypeInfo * unknownDataType;
  44. ITypeInfo * unknownStringType;
  45. ITypeInfo * unknownVarStringType;
  46. ITypeInfo * unknownUtf8Type;
  47. ITypeInfo * constUnknownVarStringType;
  48. ITypeInfo * unknownUnicodeType;
  49. ITypeInfo * fposType;
  50. ITypeInfo * doubleType;
  51. IHqlExpression * skipActionMarker;
  52. IHqlExpression * skipReturnMarker;
  53. IHqlExpression * subGraphMarker;
  54. IHqlExpression * removedAssignTag;
  55. IHqlExpression * internalAttrExpr;
  56. IHqlExpression * activityIdMarkerExpr;
  57. IHqlExpression * conditionalRowMarkerExpr;
  58. //===========================================================================
  59. MODULE_INIT(INIT_PRIORITY_STANDARD)
  60. {
  61. boolType = makeBoolType();
  62. signedType = makeIntType(sizeof(signed), true);
  63. unsignedType = makeIntType(sizeof(unsigned), false);
  64. sizetType = makeIntType(sizeof(size32_t), false);
  65. defaultIntegralType = makeIntType(8, true);
  66. counterType = makeIntType(8, false);
  67. unknownDataType = makeDataType(UNKNOWN_LENGTH);
  68. unknownStringType = makeStringType(UNKNOWN_LENGTH, NULL, NULL);
  69. unknownVarStringType = makeVarStringType(UNKNOWN_LENGTH);
  70. unknownUtf8Type = makeUtf8Type(UNKNOWN_LENGTH, 0);
  71. constUnknownVarStringType = makeConstantModifier(LINK(unknownVarStringType));
  72. unknownUnicodeType = makeUnicodeType(UNKNOWN_LENGTH, 0);
  73. fposType = makeIntType(8, false);
  74. doubleType = makeRealType(8);
  75. cachedVoidType = makeVoidType();
  76. cachedBoolValue[false] = createConstant(false);
  77. cachedBoolValue[true] = createConstant(true);
  78. cachedZero = createIntConstant(0);
  79. cachedNullChar = createConstant(createCharValue(0, makeCharType()));
  80. defaultAttrExpr = createAttribute(defaultAtom);
  81. selfAttrExpr = createAttribute(selfAtom);
  82. skipActionMarker = createAttribute(skipActionMarkerAtom);
  83. skipReturnMarker = createAttribute(skipReturnMarkerAtom);
  84. subGraphMarker = createAttribute(subgraphAtom);
  85. removedAssignTag = createAttribute(_internal_Atom);
  86. internalAttrExpr = createAttribute(internalAtom);
  87. activityIdMarkerExpr = createAttribute(activityIdMarkerAtom);
  88. conditionalRowMarkerExpr = createAttribute(_conditionalRowMarker_Atom);
  89. return true;
  90. }
  91. MODULE_EXIT()
  92. {
  93. conditionalRowMarkerExpr->Release();
  94. activityIdMarkerExpr->Release();
  95. internalAttrExpr->Release();
  96. removedAssignTag->Release();
  97. subGraphMarker->Release();
  98. skipReturnMarker->Release();
  99. skipActionMarker->Release();
  100. selfAttrExpr->Release();
  101. defaultAttrExpr->Release();
  102. boolType->Release();
  103. cachedVoidType->Release();
  104. cachedBoolValue[false]->Release();
  105. cachedBoolValue[true]->Release();
  106. cachedZero->Release();
  107. cachedNullChar->Release();
  108. unsignedType->Release();
  109. signedType->Release();
  110. defaultIntegralType->Release();
  111. sizetType->Release();
  112. counterType->Release();
  113. unknownDataType->Release();
  114. unknownStringType->Release();
  115. unknownVarStringType->Release();
  116. unknownUtf8Type->Release();
  117. constUnknownVarStringType->Release();
  118. unknownUnicodeType->Release();
  119. fposType->Release();
  120. doubleType->Release();
  121. }
  122. //===========================================================================
  123. IHqlExpression * getZero() { return LINK(cachedZero); }
  124. ITypeInfo * queryBoolType() { return boolType; }
  125. ITypeInfo * queryVoidType() { return cachedVoidType; }
  126. IHqlExpression * queryBoolExpr(bool value){ return cachedBoolValue[value]; }
  127. IHqlExpression * queryNullChar() { return cachedNullChar; }
  128. IHqlExpression * queryZero() { return cachedZero; }
  129. IHqlExpression * getDefaultAttr() { return LINK(defaultAttrExpr); }
  130. IHqlExpression * getSelfAttr() { return LINK(selfAttrExpr); }
  131. IHqlExpression * queryActivityIdMarker() { return activityIdMarkerExpr; }
  132. IHqlExpression * queryConditionalRowMarker() { return conditionalRowMarkerExpr; }
  133. //===========================================================================
  134. ITypeInfo * getArrayElementType(ITypeInfo * itemType)
  135. {
  136. // use a var string type to get better C++ generated...
  137. if (storePointerInArray(itemType))
  138. return makeVarStringType(UNKNOWN_LENGTH);
  139. return LINK(itemType);
  140. }
  141. ITypeInfo * getConcatResultType(IHqlExpression * expr)
  142. {
  143. assertex(!"not sure if this is unicode safe, but appears not to be used");
  144. //first work out the maximum size of the target
  145. unsigned max = expr->numChildren();
  146. unsigned idx;
  147. unsigned totalSize = 0;
  148. bool unknown = false;
  149. type_t resultType = type_string;
  150. for (idx = 0; idx < max; idx++)
  151. {
  152. ITypeInfo * type = expr->queryChild(idx)->queryType();
  153. unsigned size = type->getStringLen();
  154. if (size == UNKNOWN_LENGTH)
  155. unknown = true;
  156. else
  157. totalSize += size;
  158. if (type->getTypeCode() == type_varstring)
  159. resultType = type_varstring;
  160. }
  161. if (unknown)
  162. totalSize = 1023;
  163. if (resultType == type_string)
  164. return makeStringType(totalSize, NULL, NULL);
  165. return makeVarStringType(totalSize);
  166. }
  167. bool isCompare3Valued(ITypeInfo * type)
  168. {
  169. type = type->queryPromotedType();
  170. switch (type->getTypeCode())
  171. {
  172. case type_string: case type_data:
  173. if (type->getSize() != 1)
  174. return true;
  175. break;
  176. case type_qstring:
  177. case type_varstring:
  178. case type_unicode:
  179. case type_varunicode:
  180. case type_decimal:
  181. case type_utf8:
  182. return true;
  183. }
  184. return false;
  185. }
  186. bool storePointerInArray(ITypeInfo * type)
  187. {
  188. return type->isReference() && isTypePassedByAddress(type);
  189. }
  190. //Convert no_dataset_alias(expr, uid) to expr'
  191. IHqlExpression * normalizeDatasetAlias(IHqlExpression * expr)
  192. {
  193. IHqlExpression * uid = expr->queryProperty(_uid_Atom);
  194. assertex(uid);
  195. return appendOwnedOperand(expr->queryChild(0), LINK(uid));
  196. }
  197. //---------------------------------------------------------------------------
  198. bool isSelectSortedTop(IHqlExpression * selectExpr)
  199. {
  200. IHqlExpression * index = selectExpr->queryChild(1);
  201. if (matchesConstantValue(index, 1))
  202. {
  203. IHqlExpression * ds = selectExpr->queryChild(0);
  204. return ((ds->getOperator() == no_sort) || (ds->getOperator() == no_topn));
  205. }
  206. return false;
  207. }
  208. ITypeInfo * makeRowReferenceType(IHqlExpression * ds)
  209. {
  210. ITypeInfo * recordType = ds ? LINK(ds->queryRecordType()) : NULL;
  211. ITypeInfo * rowType = makeReferenceModifier(makeRowType(recordType));
  212. if (ds)
  213. {
  214. ITypeInfo * dsType = ds->queryType();
  215. if (hasLinkedRow(dsType))
  216. rowType = makeAttributeModifier(rowType, getLinkCountedAttr());
  217. if (hasOutOfLineModifier(dsType))
  218. rowType = makeOutOfLineModifier(rowType);
  219. }
  220. return rowType;
  221. }
  222. ITypeInfo * makeRowReferenceType(const CHqlBoundExpr & bound)
  223. {
  224. return makeRowReferenceType(bound.expr);
  225. }
  226. IHqlExpression * addMemberSelector(IHqlExpression * expr, IHqlExpression * selector)
  227. {
  228. if (!expr)
  229. return NULL;
  230. if (expr->getOperator() == no_variable)
  231. return createValue(no_pselect, expr->getType(), LINK(selector), LINK(expr));
  232. if (expr->numChildren() == 0)
  233. return LINK(expr);
  234. HqlExprArray args;
  235. ForEachChild(i, expr)
  236. args.append(*addMemberSelector(expr->queryChild(i), selector));
  237. return expr->clone(args);
  238. }
  239. //Only called on translated expressions
  240. IHqlExpression * addExpressionModifier(IHqlExpression * expr, typemod_t modifier, IInterface * extra)
  241. {
  242. //Not sure which is best implementation...
  243. #if 1
  244. return createValue(no_typetransfer, makeModifier(expr->getType(), modifier, LINK(extra)), LINK(expr));
  245. #else
  246. HqlExprArray args;
  247. unwindChildren(args, expr);
  248. return createValue(expr->getOperator(), makeModifier(expr->getType(), modifier, LINK(extra)), args);
  249. #endif
  250. }
  251. static void expandFieldNames(StringBuffer & out, IHqlExpression * record, StringBuffer & prefix, const char * sep, IHqlExpression * formatFunc)
  252. {
  253. ForEachChild(i, record)
  254. {
  255. IHqlExpression * cur = record->queryChild(i);
  256. switch (cur->getOperator())
  257. {
  258. case no_record:
  259. expandFieldNames(out, cur, prefix, sep, formatFunc);
  260. break;
  261. case no_ifblock:
  262. expandFieldNames(out, cur->queryChild(1), prefix, sep, formatFunc);
  263. break;
  264. case no_field:
  265. {
  266. StringBuffer lowerName;
  267. lowerName.append(cur->queryName()).toLowerCase();
  268. if (formatFunc)
  269. {
  270. HqlExprArray args;
  271. args.append(*createConstant(lowerName.str()));
  272. OwnedHqlExpr bound = createBoundFunction(NULL, formatFunc, args, NULL, true);
  273. OwnedHqlExpr folded = foldHqlExpression(bound, NULL, HFOthrowerror|HFOfoldimpure|HFOforcefold);
  274. assertex(folded->queryValue());
  275. lowerName.clear();
  276. getStringValue(lowerName, folded);
  277. }
  278. switch (cur->queryType()->getTypeCode())
  279. {
  280. case type_record:
  281. case type_row:
  282. {
  283. unsigned len = prefix.length();
  284. prefix.append(lowerName).append(".");
  285. expandFieldNames(out, cur->queryRecord(), prefix, sep, formatFunc);
  286. prefix.setLength(len);
  287. break;
  288. }
  289. default:
  290. {
  291. if (out.length())
  292. out.append(sep);
  293. out.append(prefix).append(lowerName);
  294. break;
  295. }
  296. }
  297. break;
  298. }
  299. }
  300. }
  301. }
  302. void expandFieldNames(StringBuffer & out, IHqlExpression * record, const char * sep, IHqlExpression * formatFunc)
  303. {
  304. StringBuffer prefix;
  305. expandFieldNames(out, record, prefix, sep, formatFunc);
  306. }
  307. IHqlExpression * ensurePositiveOrZeroInt64(IHqlExpression * expr)
  308. {
  309. if (!expr->queryType()->isSigned())
  310. return LINK(expr);
  311. Owned<ITypeInfo> type = makeIntType(8, true);
  312. if (isCast(expr) && expr->queryType() == type)
  313. {
  314. ITypeInfo * uncastType = expr->queryChild(0)->queryType();
  315. if (!uncastType->isSigned() && uncastType->isInteger() && uncastType->getSize() < 8)
  316. return LINK(expr);
  317. }
  318. OwnedHqlExpr cast = ensureExprType(expr, type);
  319. IValue * value = cast->queryValue();
  320. Owned<IValue> zeroValue = type->castFrom(true, I64C(0));
  321. OwnedHqlExpr zero = createConstant(LINK(zeroValue));
  322. if (value)
  323. {
  324. if (value->compare(zeroValue) < 0)
  325. return LINK(zero);
  326. return LINK(cast);
  327. }
  328. //A bit convoluted, but we only want to evaluate impure expressions (e.g., random()!) once.
  329. //So force them to appear pure (so get commoned up), wrap in an alias, and then create the conditional assignment
  330. if (!cast->isPure())
  331. {
  332. OwnedHqlExpr localAttr = createLocalAttribute();
  333. OwnedHqlExpr pure = createValue(no_pure, cast->getType(), LINK(cast));
  334. cast.setown(createAlias(pure, localAttr));
  335. }
  336. return createValue(no_if, LINK(type), createBoolExpr(no_lt, LINK(cast), LINK(zero)), LINK(zero), LINK(cast));
  337. }
  338. void getOutputLibraryName(SCMStringBuffer & libraryName, IConstWorkUnit * wu)
  339. {
  340. wu->getApplicationValue("LibraryModule", "name", libraryName);
  341. }
  342. IHqlExpression * projectCreateSetDataset(IHqlExpression * expr)
  343. {
  344. IHqlExpression * ds = expr->queryChild(0);
  345. IHqlExpression * select = expr->queryChild(1);
  346. IHqlExpression * record = ds->queryRecord();
  347. //Project down to a single field if necessary. Not needed if selecting the only field in the dataset.
  348. if (queryRealChild(record, 1) || (select->getOperator() != no_select) || (record->queryChild(0) != select->queryChild(1)) || ds->queryNormalizedSelector() != select->queryChild(0))
  349. {
  350. HqlExprArray assigns;
  351. OwnedHqlExpr targetField;
  352. if (select->getOperator() == no_select)
  353. targetField.set(select->queryChild(1));
  354. else
  355. targetField.setown(createField(valueAtom, select->getType(), NULL));
  356. IHqlExpression * newRecord = createRecord(targetField);
  357. assigns.append(*createAssign(createSelectExpr(getSelf(newRecord), LINK(targetField)), LINK(select)));
  358. IHqlExpression * newTransform = createValue(no_newtransform, makeTransformType(LINK(newRecord->queryRecordType())), assigns);
  359. HqlExprArray args;
  360. args.append(*LINK(ds));
  361. args.append(*newRecord);
  362. args.append(*newTransform);
  363. OwnedHqlExpr projectedDs = createDataset(no_newusertable, args);
  364. return createValue(no_createset, expr->getType(), LINK(projectedDs), createSelectExpr(LINK(projectedDs), LINK(targetField)));
  365. }
  366. return LINK(expr);
  367. }
  368. IHqlExpression * mapInternalFunctionParameters(IHqlExpression * expr)
  369. {
  370. switch (expr->getOperator())
  371. {
  372. case no_sortlist:
  373. {
  374. HqlExprArray args;
  375. ForEachChild(i, expr)
  376. args.append(*mapInternalFunctionParameters(expr->queryChild(i)));
  377. return cloneOrLink(expr, args);
  378. }
  379. case no_param:
  380. {
  381. ITypeInfo * type = expr->queryType();
  382. //String parameters need to be passed as c++ const string parameters
  383. switch (type->getTypeCode())
  384. {
  385. case type_string:
  386. case type_varstring:
  387. case type_data:
  388. case type_qstring:
  389. case type_unicode:
  390. case type_utf8:
  391. case type_varunicode:
  392. if (!expr->hasProperty(constAtom))
  393. return appendOwnedOperand(expr, createAttribute(constAtom));
  394. break;
  395. }
  396. break;
  397. }
  398. }
  399. return LINK(expr);
  400. }