sql_group_by.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include "sql_translation.h"
  3. namespace NSQLTranslationV1 {
  4. using namespace NSQLv1Generated;
  5. class TGroupByClause: public TSqlTranslation {
  6. enum class EGroupByFeatures {
  7. Begin,
  8. Ordinary = Begin,
  9. Expression,
  10. Rollup,
  11. Cube,
  12. GroupingSet,
  13. Empty,
  14. End,
  15. };
  16. typedef TEnumBitSet<EGroupByFeatures, static_cast<int>(EGroupByFeatures::Begin), static_cast<int>(EGroupByFeatures::End)> TGroupingSetFeatures;
  17. class TGroupByClauseCtx: public TSimpleRefCount<TGroupByClauseCtx> {
  18. public:
  19. typedef TIntrusivePtr<TGroupByClauseCtx> TPtr;
  20. TGroupingSetFeatures GroupFeatures;
  21. TMap<TString, TNodePtr> NodeAliases;
  22. size_t UnnamedCount = 0;
  23. };
  24. public:
  25. TGroupByClause(TContext& ctx, NSQLTranslation::ESqlMode mode, TGroupByClauseCtx::TPtr groupSetContext = {})
  26. : TSqlTranslation(ctx, mode)
  27. , GroupSetContext(groupSetContext ? groupSetContext : TGroupByClauseCtx::TPtr(new TGroupByClauseCtx()))
  28. , CompactGroupBy(false)
  29. {}
  30. bool Build(const TRule_group_by_clause& node);
  31. bool ParseList(const TRule_grouping_element_list& groupingListNode, EGroupByFeatures featureContext);
  32. void SetFeatures(const TString& field) const;
  33. TVector<TNodePtr>& Content();
  34. TMap<TString, TNodePtr>& Aliases();
  35. TLegacyHoppingWindowSpecPtr GetLegacyHoppingWindow() const;
  36. bool IsCompactGroupBy() const;
  37. TString GetSuffix() const;
  38. private:
  39. TMaybe<TVector<TNodePtr>> MultiplyGroupingSets(const TVector<TNodePtr>& lhs, const TVector<TNodePtr>& rhs) const;
  40. bool ResolveGroupByAndGrouping();
  41. bool GroupingElement(const TRule_grouping_element& node, EGroupByFeatures featureContext);
  42. void FeedCollection(const TNodePtr& elem, TVector<TNodePtr>& collection, bool& hasEmpty) const;
  43. bool OrdinaryGroupingSet(const TRule_ordinary_grouping_set& node, EGroupByFeatures featureContext);
  44. bool OrdinaryGroupingSetList(const TRule_ordinary_grouping_set_list& node, EGroupByFeatures featureContext);
  45. bool HoppingWindow(const TRule_hopping_window_specification& node);
  46. bool AllowUnnamed(TPosition pos, EGroupByFeatures featureContext);
  47. TGroupingSetFeatures& Features();
  48. const TGroupingSetFeatures& Features() const;
  49. bool AddAlias(const TString& label, const TNodePtr& node);
  50. TString GenerateGroupByExprName();
  51. bool IsAutogenerated(const TString* name) const;
  52. TVector<TNodePtr> GroupBySet;
  53. TGroupByClauseCtx::TPtr GroupSetContext;
  54. TLegacyHoppingWindowSpecPtr LegacyHoppingWindowSpec; // stream queries
  55. static const TString AutogenerateNamePrefix;
  56. bool CompactGroupBy;
  57. TString Suffix;
  58. };
  59. } // namespace NSQLTranslationV1