data_value.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.data_type.data_type import DataType
  10. from materialize.output_consistency.data_type.data_type_category import DataTypeCategory
  11. from materialize.output_consistency.data_value.source_column_identifier import (
  12. SourceColumnIdentifier,
  13. )
  14. from materialize.output_consistency.execution.sql_dialect_adjuster import (
  15. SqlDialectAdjuster,
  16. )
  17. from materialize.output_consistency.execution.value_storage_layout import (
  18. ValueStorageLayout,
  19. )
  20. from materialize.output_consistency.expression.expression import LeafExpression
  21. from materialize.output_consistency.expression.expression_characteristics import (
  22. ExpressionCharacteristics,
  23. )
  24. from materialize.output_consistency.operation.return_type_spec import ReturnTypeSpec
  25. from materialize.output_consistency.query.data_source import DataSource
  26. from materialize.output_consistency.selection.row_selection import DataRowSelection
  27. class DataValue(LeafExpression):
  28. """A simple value (in contrast to an `ExpressionWithArgs`) for HORIZONTAL storage"""
  29. def __init__(
  30. self,
  31. value: str,
  32. data_type: DataType,
  33. value_identifier: str,
  34. characteristics: set[ExpressionCharacteristics],
  35. is_null_value: bool,
  36. is_pg_compatible: bool = True,
  37. ):
  38. column_name = (
  39. f"{data_type.internal_identifier.lower()}_{value_identifier.lower()}"
  40. )
  41. super().__init__(
  42. column_name,
  43. data_type,
  44. characteristics,
  45. ValueStorageLayout.HORIZONTAL,
  46. data_source=DataSource(table_index=None),
  47. is_aggregate=False,
  48. is_expect_error=False,
  49. )
  50. self.value = value
  51. self.vertical_table_indices: set[int] = set()
  52. self.is_null_value = is_null_value
  53. self.is_pg_compatible = is_pg_compatible
  54. def resolve_return_type_spec(self) -> ReturnTypeSpec:
  55. return self.data_type.resolve_return_type_spec(self.own_characteristics)
  56. def resolve_return_type_category(self) -> DataTypeCategory:
  57. return self.data_type.category
  58. def to_sql_as_value(self, sql_adjuster: SqlDialectAdjuster) -> str:
  59. return self.data_type.value_to_sql(self.value, sql_adjuster)
  60. def recursively_collect_involved_characteristics(
  61. self, row_selection: DataRowSelection
  62. ) -> set[ExpressionCharacteristics]:
  63. return self.own_characteristics
  64. def collect_vertical_table_indices(self) -> set[int]:
  65. return self.vertical_table_indices
  66. def get_source_column_identifier(self) -> SourceColumnIdentifier:
  67. source_column_identifier = super().get_source_column_identifier()
  68. assert source_column_identifier is not None
  69. return source_column_identifier
  70. def __str__(self) -> str:
  71. return f"DataValue (column='{self.column_name}', value={self.value}, type={self.data_type})"