Change the aliases in change_map (which maps old-alias -> new-alias), relabelling any references to them in select columns and the where clause.
(self, change_map)
| 998 | self.unref_alias(alias, unref_amount) |
| 999 | |
| 1000 | def change_aliases(self, change_map): |
| 1001 | """ |
| 1002 | Change the aliases in change_map (which maps old-alias -> new-alias), |
| 1003 | relabelling any references to them in select columns and the where |
| 1004 | clause. |
| 1005 | """ |
| 1006 | if not change_map: |
| 1007 | return self |
| 1008 | # If keys and values of change_map were to intersect, an alias might be |
| 1009 | # updated twice (e.g. T4 -> T5, T5 -> T6, so also T4 -> T6) depending |
| 1010 | # on their order in change_map. |
| 1011 | assert set(change_map).isdisjoint(change_map.values()) |
| 1012 | |
| 1013 | # 1. Update references in "select" (normal columns plus aliases), |
| 1014 | # "group by" and "where". |
| 1015 | self.where.relabel_aliases(change_map) |
| 1016 | if isinstance(self.group_by, tuple): |
| 1017 | self.group_by = tuple( |
| 1018 | [col.relabeled_clone(change_map) for col in self.group_by] |
| 1019 | ) |
| 1020 | self.select = tuple([col.relabeled_clone(change_map) for col in self.select]) |
| 1021 | self.annotations = self.annotations and { |
| 1022 | key: col.relabeled_clone(change_map) |
| 1023 | for key, col in self.annotations.items() |
| 1024 | } |
| 1025 | |
| 1026 | # 2. Rename the alias in the internal table/alias datastructures. |
| 1027 | for old_alias, new_alias in change_map.items(): |
| 1028 | if old_alias not in self.alias_map: |
| 1029 | continue |
| 1030 | alias_data = self.alias_map[old_alias].relabeled_clone(change_map) |
| 1031 | self.alias_map[new_alias] = alias_data |
| 1032 | self.alias_refcount[new_alias] = self.alias_refcount[old_alias] |
| 1033 | del self.alias_refcount[old_alias] |
| 1034 | del self.alias_map[old_alias] |
| 1035 | |
| 1036 | table_aliases = self.table_map[alias_data.table_name] |
| 1037 | for pos, alias in enumerate(table_aliases): |
| 1038 | if alias == old_alias: |
| 1039 | table_aliases[pos] = new_alias |
| 1040 | break |
| 1041 | |
| 1042 | # 3. Rename the direct external aliases and the ones of combined |
| 1043 | # queries (union, intersection, difference). |
| 1044 | self.external_aliases = { |
| 1045 | # Table is aliased or it's being changed and thus is aliased. |
| 1046 | change_map.get(alias, alias): (aliased or alias in change_map) |
| 1047 | for alias, aliased in self.external_aliases.items() |
| 1048 | } |
| 1049 | for combined_query in self.combined_queries: |
| 1050 | external_change_map = { |
| 1051 | alias: aliased |
| 1052 | for alias, aliased in change_map.items() |
| 1053 | if alias in combined_query.external_aliases |
| 1054 | } |
| 1055 | combined_query.change_aliases(external_change_map) |
| 1056 | |
| 1057 | def bump_prefix(self, other_query, exclude=None): |
no test coverage detected