block.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "block.h"
  2. #include <util/generic/yexception.h>
  3. namespace NClickHouse {
  4. TBlock::TIterator::TIterator(const TBlock& block)
  5. : Block_(block)
  6. , Idx_(0)
  7. {
  8. }
  9. const TString& TBlock::TIterator::Name() const {
  10. return Block_.Columns_[Idx_].Name;
  11. }
  12. TTypeRef TBlock::TIterator::Type() const {
  13. return Block_.Columns_[Idx_].Column->Type();
  14. }
  15. TColumnRef TBlock::TIterator::Column() const {
  16. return Block_.Columns_[Idx_].Column;
  17. }
  18. void TBlock::TIterator::Next() {
  19. ++Idx_;
  20. }
  21. bool TBlock::TIterator::IsValid() const {
  22. return Idx_ < Block_.Columns_.size();
  23. }
  24. TBlock::TBlock()
  25. : Rows_(0)
  26. {
  27. }
  28. TBlock::TBlock(size_t cols, size_t rows)
  29. : Rows_(rows)
  30. {
  31. Columns_.reserve(cols);
  32. }
  33. TBlock::~TBlock() = default;
  34. void TBlock::AppendColumn(const TString& name, const TColumnRef& col) {
  35. if (Columns_.empty()) {
  36. Rows_ = col->Size();
  37. } else if (col->Size() != Rows_) {
  38. ythrow yexception()
  39. << "all clumns in block must have same count of rows";
  40. }
  41. Columns_.push_back(TColumnItem{name, col});
  42. }
  43. /// Count of columns in the block.
  44. size_t TBlock::GetColumnCount() const {
  45. return Columns_.size();
  46. }
  47. const TBlockInfo& TBlock::Info() const {
  48. return Info_;
  49. }
  50. /// Count of rows in the block.
  51. size_t TBlock::GetRowCount() const {
  52. return Rows_;
  53. }
  54. void TBlock::AppendBlock(const TBlock& block) {
  55. if (block.GetRowCount() == 0) {
  56. return;
  57. }
  58. size_t columnCount = GetColumnCount();
  59. if (columnCount == 0) {
  60. Rows_ = block.GetRowCount();
  61. Columns_ = block.Columns_;
  62. return;
  63. }
  64. if (columnCount != block.GetColumnCount()) {
  65. ythrow yexception() << "Can't concatenate two blocks. Different number of columns (current_block: "
  66. << columnCount << ", added: " << block.GetColumnCount() << ")";
  67. }
  68. for (size_t i = 0; i < columnCount; ++i) {
  69. if (Columns_[i].Name != block.Columns_[i].Name) {
  70. ythrow yexception() << "Can't concatenate two blocks. Different names of columns (current_block: "
  71. << Columns_[i].Name << ", added: " << block.Columns_[i].Name << ")";
  72. }
  73. }
  74. for (size_t i = 0; i < columnCount; ++i) {
  75. Columns_[i].Column->Append(block.Columns_[i].Column);
  76. }
  77. Rows_ += block.GetRowCount();
  78. }
  79. TColumnRef TBlock::operator[](size_t idx) const {
  80. if (idx < Columns_.size()) {
  81. return Columns_[idx].Column;
  82. }
  83. ythrow yexception() << "column index is out of range";
  84. }
  85. }