operations_test.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. import time
  10. from materialize.scalability.operation.operation_data import OperationData
  11. from materialize.scalability.operation.scalability_operation import (
  12. Operation,
  13. SimpleSqlOperation,
  14. )
  15. class EmptyOperation(Operation):
  16. def _execute(self, data: OperationData) -> OperationData:
  17. return data
  18. class EmptySqlStatement(SimpleSqlOperation):
  19. def sql_statement(self) -> str:
  20. return ""
  21. class SleepInPython(Operation):
  22. def __init__(self, duration_in_sec: float) -> None:
  23. self.duration_in_sec = duration_in_sec
  24. def _execute(self, data: OperationData) -> OperationData:
  25. time.sleep(self.duration_in_sec)
  26. return data
  27. class SleepInEnvironmentd(SimpleSqlOperation):
  28. """Run mz_sleep() in a constant query so that the sleep actually happens in environmentd."""
  29. def __init__(self, duration_in_sec: float) -> None:
  30. self.duration_in_sec = duration_in_sec
  31. def sql_statement(self) -> str:
  32. return f"SELECT mz_unsafe.mz_sleep({self.duration_in_sec});"
  33. class SleepInClusterd(SimpleSqlOperation):
  34. """Run mz_sleep() in a manner that it will be run in clusterd.
  35. The first part of the UNION is what makes this query non-constant,
  36. but at the same time it matches no rows, so mz_sleep will only run once,
  37. with the input being the `1` from `SELECT 1`
  38. """
  39. def __init__(self, duration_in_sec: float) -> None:
  40. self.duration_in_sec = duration_in_sec
  41. def sql_statement(self) -> str:
  42. return f"""
  43. SELECT mz_unsafe.mz_sleep(f1 * {self.duration_in_sec})
  44. FROM (
  45. (SELECT * FROM t1 WHERE f1 < 0)
  46. UNION ALL
  47. (SELECT 1)
  48. );
  49. """