query.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #pragma once
  2. #include "block.h"
  3. #include <util/generic/string.h>
  4. #include <cstdint>
  5. #include <functional>
  6. #include <memory>
  7. namespace NClickHouse {
  8. /**
  9. * Settings of individual query.
  10. */
  11. struct TQuerySettings {
  12. /// Максимальное количество потоков выполнения запроса. По-умолчанию - определять автоматически.
  13. int MaxThreads = 0;
  14. /// Считать минимумы и максимумы столбцов результата.
  15. bool Extremes = false;
  16. /// Тихо пропускать недоступные шарды.
  17. bool SkipUnavailableShards = false;
  18. /// Write statistics about read rows, bytes, time elapsed, etc.
  19. bool OutputFormatWriteStatistics = true;
  20. /// Use client timezone for interpreting DateTime string values, instead of adopting server timezone.
  21. bool UseClientTimeZone = false;
  22. // connect_timeout
  23. // max_block_size
  24. // distributed_group_by_no_merge = false
  25. // strict_insert_defaults = 0
  26. // network_compression_method = LZ4
  27. // priority = 0
  28. };
  29. struct TException {
  30. int Code = 0;
  31. TString Name;
  32. TString DisplayText;
  33. TString StackTrace;
  34. /// Pointer to nested exception.
  35. std::unique_ptr<TException> Nested;
  36. };
  37. struct TProfile {
  38. ui64 rows = 0;
  39. ui64 blocks = 0;
  40. ui64 bytes = 0;
  41. ui64 rows_before_limit = 0;
  42. bool applied_limit = false;
  43. bool calculated_rows_before_limit = false;
  44. };
  45. struct TProgress {
  46. ui64 rows = 0;
  47. ui64 bytes = 0;
  48. ui64 total_rows = 0;
  49. };
  50. class TQueryEvents {
  51. public:
  52. virtual ~TQueryEvents() {
  53. }
  54. /// Some data was received.
  55. virtual void OnData(const TBlock& block) = 0;
  56. virtual void OnServerException(const TException& e) = 0;
  57. virtual void OnProfile(const TProfile& profile) = 0;
  58. virtual void OnProgress(const TProgress& progress) = 0;
  59. virtual void OnFinish() = 0;
  60. };
  61. using TExceptionCallback = std::function<void(const TException& e)>;
  62. using TProfileCallback = std::function<void(const TProfile& profile)>;
  63. using TProgressCallback = std::function<void(const TProgress& progress)>;
  64. using TSelectCallback = std::function<void(const TBlock& block)>;
  65. class TQuery: public TQueryEvents {
  66. public:
  67. TQuery();
  68. TQuery(const char* query);
  69. TQuery(const TString& query);
  70. TQuery(const TString& query, const TString& query_id);
  71. ~TQuery();
  72. ///
  73. inline TString GetText() const {
  74. return Query_;
  75. }
  76. inline TString GetId() const {
  77. return QueryId_;
  78. }
  79. /// Set handler for receiving result data.
  80. inline TQuery& OnData(TSelectCallback cb) {
  81. SelectCb_ = cb;
  82. return *this;
  83. }
  84. /// Set handler for receiving server's exception.
  85. inline TQuery& OnException(TExceptionCallback cb) {
  86. ExceptionCb_ = cb;
  87. return *this;
  88. }
  89. /// Set handler for receiving a profile of query execution.
  90. inline TQuery& OnProfile(TProfileCallback pb) {
  91. ProfileCb_ = pb;
  92. return *this;
  93. }
  94. /// Set handler for receiving a progress of query exceution.
  95. inline TQuery& OnProgress(TProgressCallback cb) {
  96. ProgressCb_ = cb;
  97. return *this;
  98. }
  99. private:
  100. void OnData(const TBlock& block) override {
  101. if (SelectCb_) {
  102. SelectCb_(block);
  103. }
  104. }
  105. void OnServerException(const TException& e) override {
  106. if (ExceptionCb_) {
  107. ExceptionCb_(e);
  108. }
  109. }
  110. void OnProfile(const TProfile& profile) override {
  111. if (ProfileCb_) {
  112. ProfileCb_(profile);
  113. }
  114. }
  115. void OnProgress(const TProgress& progress) override {
  116. if (ProgressCb_) {
  117. ProgressCb_(progress);
  118. }
  119. }
  120. void OnFinish() override {
  121. }
  122. private:
  123. TString Query_;
  124. TString QueryId_;
  125. TExceptionCallback ExceptionCb_;
  126. TProfileCallback ProfileCb_;
  127. TProgressCallback ProgressCb_;
  128. TSelectCallback SelectCb_;
  129. };
  130. }