hqlinline.hpp 12 KB

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