hqlinline.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef __HQLINLINE_HPP_
  15. #define __HQLINLINE_HPP_
  16. class ActivityInstance;
  17. class SerializationRow;
  18. class EvalContext;
  19. // A parent extract represents the set of fields etc. which are used from the parent activity,
  20. // which are local to the point that the executeChildActivity() is called. The type indicates
  21. // the reason it is being created.
  22. // There should always an EvalContext associated with a ParentExtract
  23. class ParentExtract : public HqlExprAssociation
  24. {
  25. public:
  26. ParentExtract(HqlCppTranslator & _translator, PEtype _type, IHqlExpression * _graphId, GraphLocalisation _localisation, EvalContext * _container);
  27. ~ParentExtract();
  28. //HqlExprAssociation
  29. virtual AssocKind getKind() { return AssocExtract; }
  30. void beginCreateExtract(BuildCtx & buildctx, bool doDeclare);
  31. void beginNestedExtract(BuildCtx & clonectx);
  32. void beginReuseExtract();
  33. void endCreateExtract(CHqlBoundExpr & boundExtract);
  34. void endUseExtract(BuildCtx & ctx);
  35. IHqlExpression * queryExtractName() { return boundExtract.expr; }
  36. bool canEvaluate(IHqlExpression * expr);
  37. bool canSerializeFields() const { return buildctx != NULL; }
  38. void associateCursors(BuildCtx & declarectx, BuildCtx & evalctx, GraphLocalisation childLocalisation);
  39. void beginChildActivity(BuildCtx & declareCtx, BuildCtx & startCtx, GraphLocalisation childLocalisation, IHqlExpression * colocal, bool nested, bool ignoreSelf, ActivityInstance * activityRequiringCast);
  40. void endChildActivity();
  41. void addSerializedExpression(IHqlExpression * value, ITypeInfo * type);
  42. void buildAssign(IHqlExpression * serializedTarget, IHqlExpression * originalValue);
  43. AliasKind evaluateExpression(BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt, IHqlExpression * colocal, bool evaluateLocally);
  44. void setAllowDestructor() { canDestroyExtract = true; }
  45. inline GraphLocalisation queryLocalisation() { return localisation; }
  46. bool requiresOnStart() const;
  47. bool insideChildQuery() const;
  48. bool areGraphResultsAccessible(IHqlExpression * searchGraphId) const;
  49. protected:
  50. void ensureAccessible(BuildCtx & ctx, IHqlExpression * expr, const CHqlBoundExpr & bound, CHqlBoundExpr & tgt, IHqlExpression * colocal);
  51. void gatherActiveRows(BuildCtx & ctx);
  52. protected:
  53. HqlCppTranslator & translator;
  54. EvalContext * container;
  55. SerializationRow * serialization; // fields that are serialized to the children
  56. SerializationRow * childSerialization; // same serialization, but as it is bound in the child.
  57. CursorArray colocalBoundCursors; // what rows/cursors are available at the point of executeChildGraph()
  58. CursorArray nonlocalBoundCursors; // what rows/cursors are available at the point of executeChildGraph()
  59. GraphLocalisation localisation; // what kind of localisation do ALL the children have?
  60. CursorArray inheritedCursors;
  61. CursorArray localCursors; // does not include colocal
  62. CursorArray cursorToBind;
  63. CHqlBoundExpr boundBuilder; // may have wrapper, or be a char[n]
  64. CHqlBoundExpr boundExtract; // always a reference to a row. for "extract"
  65. Owned<BuildCtx> buildctx; // may be null if nested extract
  66. PEtype type;
  67. IHqlExpression * graphId;
  68. bool canDestroyExtract;
  69. };
  70. class CtxCollection : public CInterface
  71. {
  72. public:
  73. CtxCollection(BuildCtx & _declareCtx) : clonectx(_declareCtx), childctx(_declareCtx), declarectx(_declareCtx) {}
  74. void createFunctionStructure(HqlCppTranslator & translator, BuildCtx & ctx, bool canEvaluate, const char * serializeFunc);
  75. public:
  76. BuildCtx clonectx;
  77. BuildCtx childctx; // child.onCreate() is called from here..
  78. BuildCtx declarectx;
  79. //following are always null for nested classes, always created for others.
  80. Owned<BuildCtx> evalctx;
  81. Owned<BuildCtx> serializectx;
  82. Owned<BuildCtx> deserializectx;
  83. };
  84. //A potential location for an extract to be created...
  85. class EvalContext : public HqlExprAssociation
  86. {
  87. public:
  88. EvalContext(HqlCppTranslator & _translator, ParentExtract * _parentExtract, EvalContext * _parent);
  89. virtual AssocKind getKind() { return AssocExtractContext; }
  90. virtual IHqlExpression * createGraphLookup(unique_id_t id, bool isChild);
  91. virtual AliasKind evaluateExpression(BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt, bool evaluateLocally) = 0;
  92. virtual bool isColocal() { return true; }
  93. virtual ActivityInstance * queryActivity();
  94. virtual bool isLibraryContext() { return false; }
  95. virtual void tempCompatiablityEnsureSerialized(const CHqlBoundTarget & tgt) = 0;
  96. virtual bool getInvariantMemberContext(BuildCtx * ctx, BuildCtx * * declarectx, BuildCtx * * initctx, bool isIndependentMaybeShared, bool invariantEachStart) { return false; }
  97. void ensureContextAvailable() { ensureHelpersExist(); }
  98. virtual bool evaluateInParent(BuildCtx & ctx, IHqlExpression * expr, bool hasOnStart);
  99. bool hasParent() { return parent != NULL; }
  100. bool needToEvaluateLocally(BuildCtx & ctx, IHqlExpression * expr);
  101. public://only used by friends
  102. virtual void callNestedHelpers(const char * memberName) = 0;
  103. virtual void ensureHelpersExist() = 0;
  104. virtual bool isRowInvariant(IHqlExpression * expr) { return false; }
  105. bool requiresOnStart() const;
  106. bool insideChildQuery() const;
  107. bool areGraphResultsAccessible(IHqlExpression * graphId) const;
  108. protected:
  109. Owned<ParentExtract> parentExtract; // extract of the parent EvalContext
  110. HqlCppTranslator & translator;
  111. EvalContext * parent;
  112. OwnedHqlExpr colocalMember;
  113. };
  114. class ClassEvalContext : public EvalContext
  115. {
  116. friend class ActivityInstance;
  117. friend class GlobalClassBuilder;
  118. public:
  119. ClassEvalContext(HqlCppTranslator & _translator, ParentExtract * _parentExtract, EvalContext * _parent, BuildCtx & createctx, BuildCtx & startctx);
  120. virtual AliasKind evaluateExpression(BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt, bool evaluateLocally);
  121. virtual bool isRowInvariant(IHqlExpression * expr);
  122. virtual void tempCompatiablityEnsureSerialized(const CHqlBoundTarget & tgt);
  123. virtual bool getInvariantMemberContext(BuildCtx * ctx, BuildCtx * * declarectx, BuildCtx * * initctx, bool isIndependentMaybeShared, bool invariantEachStart);
  124. protected:
  125. void cloneAliasInClass(CtxCollection & ctxs, const CHqlBoundExpr & bound, CHqlBoundExpr & tgt);
  126. IHqlExpression * cloneExprInClass(CtxCollection & ctxs, IHqlExpression * expr);
  127. void createMemberAlias(CtxCollection & ctxs, BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt);
  128. void doCallNestedHelpers(const char * member, const char * acticity);
  129. void ensureSerialized(CtxCollection & ctxs, const CHqlBoundTarget & tgt);
  130. protected:
  131. CtxCollection onCreate;
  132. CtxCollection onStart;
  133. };
  134. class GlobalClassEvalContext : public ClassEvalContext
  135. {
  136. public:
  137. GlobalClassEvalContext(HqlCppTranslator & _translator, ParentExtract * _parentExtract, EvalContext * _parent, BuildCtx & createctx, BuildCtx & startctx);
  138. virtual void callNestedHelpers(const char * memberName);
  139. virtual IHqlExpression * createGraphLookup(unique_id_t id, bool isChild) { throwUnexpected(); }
  140. virtual void ensureHelpersExist();
  141. virtual bool isColocal() { return false; }
  142. };
  143. class ActivityEvalContext : public ClassEvalContext
  144. {
  145. friend class ActivityInstance;
  146. public:
  147. ActivityEvalContext(HqlCppTranslator & _translator, ActivityInstance * _activity, ParentExtract * _parentExtract, EvalContext * _parent, IHqlExpression * _colocal, BuildCtx & createctx, BuildCtx & startctx);
  148. virtual void callNestedHelpers(const char * memberName);
  149. virtual IHqlExpression * createGraphLookup(unique_id_t id, bool isChild);
  150. virtual void ensureHelpersExist();
  151. virtual bool isColocal() { return (colocalMember != NULL); }
  152. virtual ActivityInstance * queryActivity();
  153. void createMemberAlias(CtxCollection & ctxs, BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt);
  154. protected:
  155. ActivityInstance * activity;
  156. };
  157. class NestedEvalContext : public ClassEvalContext
  158. {
  159. public:
  160. NestedEvalContext(HqlCppTranslator & _translator, const char * _memberName, ParentExtract * _parentExtract, EvalContext * _parent, IHqlExpression * _colocal, BuildCtx & createctx, BuildCtx & startctx);
  161. virtual void callNestedHelpers(const char * memberName);
  162. virtual IHqlExpression * createGraphLookup(unique_id_t id, bool isChild);
  163. virtual void ensureHelpersExist();
  164. void initContext();
  165. virtual bool evaluateInParent(BuildCtx & ctx, IHqlExpression * expr, bool hasOnStart);
  166. protected:
  167. bool helpersExist;
  168. protected:
  169. StringAttr memberName;
  170. };
  171. class MemberEvalContext : public EvalContext
  172. {
  173. public:
  174. MemberEvalContext(HqlCppTranslator & _translator, ParentExtract * _parentExtract, EvalContext * _parent, BuildCtx & _ctx);
  175. virtual void callNestedHelpers(const char * memberName);
  176. virtual void ensureHelpersExist();
  177. virtual bool isRowInvariant(IHqlExpression * expr);
  178. virtual IHqlExpression * createGraphLookup(unique_id_t id, bool isChild);
  179. virtual AliasKind evaluateExpression(BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt, bool evaluateLocally);
  180. virtual bool getInvariantMemberContext(BuildCtx * ctx, BuildCtx * * declarectx, BuildCtx * * initctx, bool isIndependentMaybeShared, bool invariantEachStart);
  181. virtual void tempCompatiablityEnsureSerialized(const CHqlBoundTarget & tgt);
  182. void initContext();
  183. protected:
  184. BuildCtx ctx;
  185. };
  186. class LibraryEvalContext : public EvalContext
  187. {
  188. public:
  189. LibraryEvalContext(HqlCppTranslator & _translator);
  190. virtual AliasKind evaluateExpression(BuildCtx & ctx, IHqlExpression * value, CHqlBoundExpr & tgt, bool evaluateLocally);
  191. virtual bool isColocal() { return false; }
  192. virtual bool isLibraryContext() { return true; }
  193. virtual void tempCompatiablityEnsureSerialized(const CHqlBoundTarget & tgt);
  194. void associateExpression(BuildCtx & ctx, IHqlExpression * value);
  195. void ensureContextAvailable() { }
  196. public:
  197. virtual void callNestedHelpers(const char * memberName) {}
  198. virtual void ensureHelpersExist() {}
  199. protected:
  200. HqlExprArray values;
  201. HqlExprArray bound;
  202. };
  203. #endif