yql_opt_window.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <yql/essentials/core/expr_nodes/yql_expr_nodes.h>
  3. namespace NYql {
  4. struct TTypeAnnotationContext;
  5. TExprNode::TPtr ExpandCalcOverWindow(const TExprNode::TPtr& node, TExprContext& ctx, TTypeAnnotationContext& types);
  6. TExprNodeList ExtractCalcsOverWindow(const TExprNode::TPtr& node, TExprContext& ctx);
  7. TExprNode::TPtr RebuildCalcOverWindowGroup(TPositionHandle pos, const TExprNode::TPtr& input, const TExprNodeList& calcs, TExprContext& ctx);
  8. enum EFrameType {
  9. FrameByRows,
  10. FrameByRange,
  11. FrameByGroups,
  12. };
  13. using NNodes::TCoWinOnBase;
  14. using NNodes::TCoFrameBound;
  15. bool IsUnbounded(const NNodes::TCoFrameBound& bound);
  16. bool IsCurrentRow(const NNodes::TCoFrameBound& bound);
  17. class TWindowFrameSettings {
  18. public:
  19. static TWindowFrameSettings Parse(const TExprNode& node, TExprContext& ctx);
  20. static TMaybe<TWindowFrameSettings> TryParse(const TExprNode& node, TExprContext& ctx);
  21. // This two functions can only be used for FrameByRows or FrameByGroups
  22. TMaybe<i32> GetFirstOffset() const;
  23. TMaybe<i32> GetLastOffset() const;
  24. TCoFrameBound GetFirst() const;
  25. TCoFrameBound GetLast() const;
  26. bool IsNonEmpty() const { return NeverEmpty; }
  27. bool IsCompact() const { return Compact; }
  28. EFrameType GetFrameType() const { return Type; }
  29. private:
  30. EFrameType Type = FrameByRows;
  31. TExprNode::TPtr First;
  32. TMaybe<i32> FirstOffset;
  33. TExprNode::TPtr Last;
  34. TMaybe<i32> LastOffset;
  35. bool NeverEmpty = false;
  36. bool Compact = false;
  37. };
  38. struct TSessionWindowParams {
  39. TSessionWindowParams()
  40. : Traits(nullptr)
  41. , Key(nullptr)
  42. , KeyType(nullptr)
  43. , ParamsType(nullptr)
  44. , Init(nullptr)
  45. , Update(nullptr)
  46. , SortTraits(nullptr)
  47. {}
  48. void Reset();
  49. TExprNode::TPtr Traits;
  50. TExprNode::TPtr Key;
  51. const TTypeAnnotationNode* KeyType;
  52. const TTypeAnnotationNode* ParamsType;
  53. TExprNode::TPtr Init;
  54. TExprNode::TPtr Update;
  55. TExprNode::TPtr SortTraits;
  56. };
  57. struct TSortParams {
  58. TExprNode::TPtr Key;
  59. TExprNode::TPtr Order;
  60. };
  61. // Lambda(input: Stream/List<T>) -> Stream/List<Tuple<T, SessionKey, SessionState, ....>>
  62. // input is assumed to be partitioned by partitionKeySelector
  63. TExprNode::TPtr ZipWithSessionParamsLambda(TPositionHandle pos, const TExprNode::TPtr& partitionKeySelector,
  64. const TExprNode::TPtr& sessionKeySelector, const TExprNode::TPtr& sessionInit,
  65. const TExprNode::TPtr& sessionUpdate, TExprContext& ctx);
  66. // input should be List/Stream of structs + see above
  67. TExprNode::TPtr AddSessionParamsMemberLambda(TPositionHandle pos,
  68. TStringBuf sessionStartMemberName, TStringBuf sessionParamsMemberName,
  69. const TExprNode::TPtr& partitionKeySelector,
  70. const TExprNode::TPtr& sessionKeySelector, const TExprNode::TPtr& sessionInit,
  71. const TExprNode::TPtr& sessionUpdate, TExprContext& ctx);
  72. // input should be List/Stream of structs + see above
  73. TExprNode::TPtr AddSessionParamsMemberLambda(TPositionHandle pos,
  74. TStringBuf sessionStartMemberName, const TExprNode::TPtr& partitionKeySelector,
  75. const TSessionWindowParams& sessionWindowParams, TExprContext& ctx);
  76. }