pg_dialect_adjuster.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 re
  10. from materialize.output_consistency.execution.sql_dialect_adjuster import (
  11. SqlDialectAdjuster,
  12. )
  13. from materialize.output_consistency.input_data.types.date_time_types_provider import (
  14. INTERVAL_TYPE_IDENTIFIER,
  15. )
  16. class PgSqlDialectAdjuster(SqlDialectAdjuster):
  17. def adjust_type(self, type_name: str) -> str:
  18. if type_name == "DOUBLE":
  19. return "DOUBLE PRECISION"
  20. if type_name == "DOUBLE[]":
  21. return "DOUBLE PRECISION[]"
  22. return type_name
  23. def adjust_enum_value(self, string_value: str) -> str:
  24. if string_value == "DOUBLE":
  25. return "DOUBLE PRECISION"
  26. if string_value == "DOUBLE[]":
  27. return "DOUBLE PRECISION[]"
  28. return string_value
  29. def adjust_value(
  30. self, string_value: str, internal_type_identifier: str, type_name: str
  31. ) -> str:
  32. if internal_type_identifier == INTERVAL_TYPE_IDENTIFIER:
  33. # cut milliseconds away
  34. string_value = re.sub("\\.\\d+'$", "'", string_value)
  35. if type_name.startswith("INT") and string_value.startswith("-"):
  36. # wrap negative numbers in parentheses
  37. # see: https://github.com/MaterializeInc/database-issues/issues/6611
  38. string_value = f"({string_value})"
  39. return string_value