constant_expression.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.data_type.data_type import DataType
  11. from materialize.output_consistency.data_type.data_type_category import DataTypeCategory
  12. from materialize.output_consistency.execution.sql_dialect_adjuster import (
  13. SqlDialectAdjuster,
  14. )
  15. from materialize.output_consistency.execution.value_storage_layout import (
  16. ValueStorageLayout,
  17. )
  18. from materialize.output_consistency.expression.expression import (
  19. LeafExpression,
  20. )
  21. from materialize.output_consistency.expression.expression_characteristics import (
  22. ExpressionCharacteristics,
  23. )
  24. from materialize.output_consistency.input_data.types.string_type_provider import (
  25. TEXT_DATA_TYPE,
  26. )
  27. from materialize.output_consistency.operation.return_type_spec import ReturnTypeSpec
  28. from materialize.output_consistency.query.data_source import DataSource
  29. class ConstantExpression(LeafExpression):
  30. def __init__(
  31. self,
  32. value: str,
  33. data_type: DataType,
  34. add_quotes: bool = False,
  35. characteristics: set[ExpressionCharacteristics] = set(),
  36. is_aggregate: bool = False,
  37. ):
  38. column_name = "<none>"
  39. super().__init__(
  40. column_name,
  41. data_type,
  42. characteristics,
  43. ValueStorageLayout.ANY,
  44. data_source=None,
  45. is_aggregate=is_aggregate,
  46. is_expect_error=False,
  47. )
  48. self.value = value
  49. self.add_quotes = add_quotes
  50. def resolve_return_type_spec(self) -> ReturnTypeSpec:
  51. return self.data_type.resolve_return_type_spec(self.own_characteristics)
  52. def resolve_return_type_category(self) -> DataTypeCategory:
  53. return self.data_type.category
  54. def to_sql(
  55. self, sql_adjuster: SqlDialectAdjuster, include_alias: bool, is_root_level: bool
  56. ) -> str:
  57. return self.to_sql_as_value(sql_adjuster)
  58. def to_sql_as_value(self, sql_adjuster: SqlDialectAdjuster) -> str:
  59. sql_value = self.data_type.value_to_sql(self.value, sql_adjuster)
  60. if self.add_quotes:
  61. return f"'{sql_value}'"
  62. return sql_value
  63. def collect_vertical_table_indices(self) -> set[int]:
  64. return set()
  65. def get_data_source(self) -> DataSource | None:
  66. return None
  67. def __str__(self) -> str:
  68. return f"ConstantExpression (value={self.value}, type={self.data_type})"
  69. class ConstantStringExpression(ConstantExpression):
  70. def __init__(
  71. self,
  72. value: str,
  73. characteristics: set[ExpressionCharacteristics] = set(),
  74. ):
  75. super().__init__(
  76. value, TEXT_DATA_TYPE, add_quotes=True, characteristics=characteristics
  77. )