print_query_result.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from materialize.output_consistency.query.query_result import (
  10. QueryResult,
  11. )
  12. def print_query_outcome(outcome: QueryResult) -> None:
  13. print("+++ Query outcome")
  14. print(f"-- strategy={outcome.strategy}")
  15. print(f"-- sql={outcome.sql}")
  16. print(
  17. f"-- row_count={outcome.row_count()}, column_count={outcome.query_column_count}"
  18. )
  19. column_lengths = _determine_column_lengths(outcome, min_length=1, max_length=100)
  20. for row_index in range(0, outcome.row_count()):
  21. row_values = []
  22. for col_index in range(0, outcome.query_column_count):
  23. value = outcome.result_rows[row_index][col_index]
  24. str_value = str(value) if value is not None else "NULL"
  25. str_value = str_value.ljust(column_lengths[col_index])
  26. row_values.append(str_value)
  27. print(" | ".join(row_values))
  28. def _determine_column_lengths(
  29. outcome: QueryResult, min_length: int, max_length: int
  30. ) -> list[int]:
  31. column_lengths = [min_length for _ in range(0, outcome.query_column_count)]
  32. for row_index in range(0, outcome.row_count()):
  33. for col_index in range(0, outcome.query_column_count):
  34. value = outcome.result_rows[row_index][col_index]
  35. value_length = len(str(value))
  36. column_lengths[col_index] = min(
  37. max(column_lengths[col_index], value_length), max_length
  38. )
  39. return column_lengths