enum_constant.py 1.4 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. from __future__ import annotations
  10. from materialize.output_consistency.enum.enum_data_type import EnumDataType
  11. from materialize.output_consistency.expression.constant_expression import (
  12. ConstantExpression,
  13. )
  14. from materialize.output_consistency.expression.expression_characteristics import (
  15. ExpressionCharacteristics,
  16. )
  17. from materialize.util import stable_int_hash
  18. ENUM_DATA_TYPE = EnumDataType()
  19. class EnumConstant(ConstantExpression):
  20. """A constant SQL value"""
  21. def __init__(
  22. self,
  23. value: str,
  24. add_quotes: bool,
  25. characteristics: set[ExpressionCharacteristics],
  26. tags: set[str] | None = None,
  27. ):
  28. super().__init__(
  29. value, ENUM_DATA_TYPE, add_quotes, characteristics, is_aggregate=False
  30. )
  31. self.tags = tags
  32. def hash(self) -> int:
  33. return stable_int_hash(self.value)
  34. def __str__(self) -> str:
  35. return f"EnumConstant (value={self.value})"
  36. def is_tagged(self, tag: str) -> bool:
  37. if self.tags is None:
  38. return False
  39. return tag in self.tags