summary.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from typing import Optional
  2. from clickhouse_connect.datatypes.registry import get_from_name
  3. from clickhouse_connect.driver.query import QueryResult
  4. class QuerySummary:
  5. summary = {}
  6. def __init__(self, summary: Optional[dict] = None):
  7. if summary is not None:
  8. self.summary = summary
  9. @property
  10. def written_rows(self) -> int:
  11. return int(self.summary.get('written_rows', 0))
  12. def written_bytes(self) -> int:
  13. return int(self.summary.get('written_bytes', 0))
  14. def query_id(self) -> str:
  15. return self.summary.get('query_id', '')
  16. def as_query_result(self) -> QueryResult:
  17. data = []
  18. column_names = []
  19. column_types = []
  20. str_type = get_from_name('String')
  21. int_type = get_from_name('Int64')
  22. for key, value in self.summary.items():
  23. column_names.append(key)
  24. if value.isnumeric():
  25. data.append(int(value))
  26. column_types.append(int_type)
  27. else:
  28. data.append(value)
  29. column_types.append(str_type)
  30. return QueryResult([data], column_names=tuple(column_names), column_types=tuple(column_types))