row_selection.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.query.data_source import DataSource
  10. class DataRowSelection:
  11. """A selection of table rows, useful when collecting involved characteristics in vertical storage layout"""
  12. def __init__(self):
  13. self.row_indices_per_data_source: dict[DataSource, set[int]] = dict()
  14. def includes_all_of_all_sources(self) -> bool:
  15. return len(self.row_indices_per_data_source) == 0
  16. def has_selection(self) -> bool:
  17. return not self.includes_all_of_all_sources()
  18. def set_row_indices(self, data_source: DataSource, row_indices: set[int]):
  19. self.row_indices_per_data_source[data_source] = row_indices
  20. def get_row_indices(self, data_source: DataSource) -> set[int]:
  21. assert not self.includes_all_of_source(data_source)
  22. return self.row_indices_per_data_source[data_source]
  23. def includes_all_of_source(self, data_source: DataSource) -> bool:
  24. return data_source not in self.row_indices_per_data_source.keys()
  25. def is_included_in_source(self, data_source: DataSource, index: int) -> bool:
  26. if self.includes_all_of_source(data_source):
  27. return True
  28. return index in self.get_row_indices(data_source)
  29. def trim_to_minimized_sources(self, data_sources: list[DataSource]):
  30. data_sources_to_remove_from_selection = []
  31. for data_source in self.row_indices_per_data_source.keys():
  32. if data_source not in data_sources:
  33. data_sources_to_remove_from_selection.append(data_source)
  34. for data_source in data_sources_to_remove_from_selection:
  35. del self.row_indices_per_data_source[data_source]
  36. ALL_ROWS_SELECTION = DataRowSelection()