sortertraits.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2017-2026, Manticore Software LTD (https://manticoresearch.com)
  3. // Copyright (c) 2001-2016, Andrew Aksyonoff
  4. // Copyright (c) 2008-2016, Sphinx Technologies Inc
  5. // All rights reserved
  6. //
  7. // This program is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License. You should have
  9. // received a copy of the GPL license along with this program; if you
  10. // did not, you can find it at http://www.gnu.org/
  11. //
  12. #pragma once
  13. #include "sphinxsort.h"
  14. #include "aggregate.h"
  15. #include "sphinxfilter.h"
  16. class MatchSorter_c : public ISphMatchSorter
  17. {
  18. public:
  19. void SetState ( const CSphMatchComparatorState & tState ) override;
  20. const CSphMatchComparatorState & GetState() const override { return m_tState; }
  21. void SetSchema ( ISphSchema * pSchema, bool bRemapCmp ) override;
  22. const ISphSchema * GetSchema() const override { return ( ISphSchema *) m_pSchema; }
  23. void SetColumnar ( columnar::Columnar_i * pColumnar ) override { m_pColumnar = pColumnar; }
  24. int64_t GetTotalCount() const override { return m_iTotal; }
  25. void CloneTo ( ISphMatchSorter * pTrg ) const override;
  26. bool CanBeCloned() const override;
  27. void SetFilteredAttrs ( const sph::StringSet & hAttrs, bool bAddDocid ) override;
  28. void TransformPooled2StandalonePtrs ( GetBlobPoolFromMatch_fn fnBlobPoolFromMatch, GetColumnarFromMatch_fn fnGetColumnarFromMatch, bool bFinalizeSorters ) override;
  29. void SetRandom ( bool bRandom ) override { m_bRandomize = bRandom; }
  30. bool IsRandom() const override { return m_bRandomize; }
  31. int GetMatchCapacity() const override { return m_iMatchCapacity; }
  32. RowTagged_t GetJustPushed() const override { return m_tJustPushed; }
  33. VecTraits_T<RowTagged_t> GetJustPopped() const override { return m_dJustPopped; }
  34. protected:
  35. SharedPtr_t<ISphSchema> m_pSchema; ///< sorter schema (adds dynamic attributes on top of index schema)
  36. CSphMatchComparatorState m_tState; ///< protected to set m_iNow automatically on SetState() calls
  37. StrVec_t m_dTransformed;
  38. columnar::Columnar_i * m_pColumnar = nullptr;
  39. bool m_bRandomize = false;
  40. int64_t m_iTotal = 0;
  41. int m_iMatchCapacity = 0;
  42. RowTagged_t m_tJustPushed;
  43. CSphTightVector<RowTagged_t> m_dJustPopped;
  44. };
  45. /// match-sorting priority queue traits
  46. class CSphMatchQueueTraits : public MatchSorter_c, ISphNoncopyable
  47. {
  48. public:
  49. explicit CSphMatchQueueTraits ( int iSize );
  50. ~CSphMatchQueueTraits () override;
  51. int GetLength() override { return Used(); }
  52. void SwapMatchQueueTraits ( CSphMatchQueueTraits& rhs );
  53. const VecTraits_T<CSphMatch> & GetMatches() const { return m_dData; }
  54. protected:
  55. int m_iSize; // size of internal struct we can operate
  56. CSphFixedVector<CSphMatch> m_dData;
  57. CSphTightVector<int> m_dIData; // indexes into m_pData, to avoid extra moving of matches themselves
  58. CSphMatch * Last() const { return &m_dData[m_dIData.Last ()]; }
  59. CSphMatch & Get ( int iElem ) const { return m_dData[m_dIData[iElem]]; }
  60. CSphMatch & Add();
  61. int Used() const { return m_dIData.GetLength(); }
  62. bool IsEmpty() const { return m_dIData.IsEmpty(); }
  63. void ResetAfterFlatten() { m_dIData.Resize(0); }
  64. int ResetDynamic ( int iMaxUsed );
  65. int ResetDynamicFreeData ( int iMaxUsed );
  66. };
  67. class MatchCloner_c
  68. {
  69. public:
  70. void SetSchema ( const ISphSchema * pSchema );
  71. // clone plain part (incl. pointers) from src to dst
  72. // keep group part (aggregates, group_concat) of dst intact
  73. // it assumes that tDst m_pDynamic contains correct data, or wiped away.
  74. void CloneKeepingAggrs ( CSphMatch & tDst, const CSphMatch & tSrc );
  75. // clone plain part (incl. pointers) from src to dst
  76. // group part (aggregates, group_concat) is not copied
  77. void CloneWithoutAggrs ( CSphMatch & tDst, const CSphMatch & tSrc );
  78. // just write group part (aggregates, group_concat) without cloning
  79. // assumes tDst has allocated m_pDynamic. Fixme! look to #881 again...
  80. void CopyAggrs ( CSphMatch & tDst, const CSphMatch & tSrc );
  81. // copy group part (aggregates)
  82. // move group_concat part without reallocating
  83. void MoveAggrs ( CSphMatch & tDst, CSphMatch & tSrc );
  84. void AddRaw ( const CSphAttrLocator& tLoc ) { m_dAttrsGrp.Add(tLoc); }
  85. void AddPtr ( const CSphAttrLocator &tLoc ) { m_dAttrsPtr.Add(tLoc); }
  86. void ResetAttrs();
  87. // (re)fill m_dMyPtrRows and m_dOtherPtrRows from m_dAttrsPtr
  88. void CommitPtrs();
  89. private:
  90. CSphFixedVector<CSphRowitem> m_dRowBuf { 0 };
  91. CSphVector<CSphAttrLocator> m_dAttrsGrp; // locators for grouping attrs (@groupby, @count, @distinct, etc.)
  92. CSphVector<CSphAttrLocator> m_dAttrsPtr; // locators for group_concat attrs
  93. CSphVector<int> m_dMyPtrRows; // rowids matching m_dAttrsPtr. i.e. grpconcat ptr result I own
  94. CSphVector<int> m_dOtherPtrRows; // rest rowids NOT matching m_dAttrsPtr. i.e. other ptr results
  95. const CSphSchemaHelper * m_pSchema = nullptr;
  96. bool m_bPtrRowsCommited = false; // readiness of m_dMyPtrRows and m_dOtherPtrRows
  97. };
  98. class BaseGroupSorter_c : public BlobPool_c, protected CSphGroupSorterSettings
  99. {
  100. public:
  101. BaseGroupSorter_c ( const CSphGroupSorterSettings & tSettings ) { (CSphGroupSorterSettings&)(*this) = tSettings; }
  102. ~BaseGroupSorter_c() override { ResetAggregates(); }
  103. protected:
  104. MatchCloner_c m_tPregroup;
  105. CSphVector<AggrFunc_i *> m_dAggregates;
  106. void SetColumnar ( columnar::Columnar_i * pColumnar );
  107. void SetupBaseGrouper ( ISphSchema * pSchema, int iDistinct, CSphVector<AggrFunc_i *> * pAvgs = nullptr );
  108. // HAVING filtering
  109. bool EvalHAVING ( const CSphMatch& tMatch );
  110. void AggrUpdate ( CSphMatch & tDst, const CSphMatch & tSrc, bool bGrouped, bool bMerge = false );
  111. void AggrSetup ( CSphMatch & tDst, const CSphMatch & tSrc, bool bMerge = false );
  112. void AggrUngroup ( CSphMatch & tMatch );
  113. private:
  114. void ResetAggregates();
  115. };