[query] freeze when necessary to avoid hashing issues#12265
Merged
Conversation
Contributor
Author
|
bump for review! I fixed the lint issues. |
tpoterba
approved these changes
Oct 6, 2022
Contributor
tpoterba
left a comment
There was a problem hiding this comment.
womp, looked earlier but didn't review. Looks fine, don't see a way around the dict hack with less complexity than the hack.
tpoterba
previously requested changes
Oct 7, 2022
| def _convert_from_json(self, x): | ||
| return frozendict({self.key_type._convert_from_json_na(elt['key']): self.value_type._convert_from_json_na(elt['value']) for elt in x}) | ||
| def _convert_from_json(self, x, _should_freeze: bool = False): | ||
| return frozendict({self.key_type._convert_from_json_na(elt['key'], _should_freeze=True): |
Contributor
There was a problem hiding this comment.
actually, can this return a normal dict if we don't need to freeze?
| def _convert_from_json(self, x): | ||
| return frozenset({self.element_type._convert_from_json_na(elt) for elt in x}) | ||
| def _convert_from_json(self, x, _should_freeze: bool = False): | ||
| return frozenset({self.element_type._convert_from_json_na(elt, _should_freeze=True) for elt in x}) |
tpoterba
approved these changes
Oct 7, 2022
Contributor
Author
|
I added some types and also fixed a bug where we would fail to return the decoded version of an ndarray in one branch of the if. |
CHANGELOG: Fix long-standing bug wherein `hl.agg.collect_as_set` and `hl.agg.counter` error when applied to types which, in Python, are unhashable. For example, `hl.agg.counter(t.list_of_genes)` will not error when `t.list_of_genes` is a list. Instead, the counter dictionary will use `FrozenList` keys from the `frozenlist` package.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
CHANGELOG: Fix long-standing bug wherein
hl.agg.collect_as_setandhl.agg.countererror when applied to types which, in Python, are unhashable. For example,hl.agg.counter(t.list_of_genes)will not error whent.list_of_genesis a list. Instead, the counter dictionary will useFrozenListkeys from thefrozenlistpackage.Hey @iris-garden ! I figured this was good reviewing practice for you and also a chance to see how we convert data to/from JSON and to/from the JVM (by way of this "encoded" representation which is a binary one). The details of that are not super important to this PR, but you might take a peek to understand the change.
The main issue here is that in Python, you can't write:
Because sets must contain "hashable" data. Python lists are not hashable because they're mutable. This is transitively a problem. For example, the following also fails with the same error because the list inside the tuple is mutable thus the tuple is not (safely) hashable.
Hail's internal language is fully immutable, so every type can be placed inside a set (or used as the keys of a dict). When we convert from Hail's internal representation to Python, we cannot use mutable types in hashable positions. Unfortunately, we also need to maintain backwards compatibility with the way the code currently works. You can see this pretty clearly in the difference between
hl.agg.collectandhl.agg.collect_as_set:collected_lsshould be[[1, 2, 3]]whereascollected_as_set_lsnecessarily uses hashable types:{frozenlist([1, 2, 3])}.Things are particularly subtle with dictionaries whose keys must always be hashable and whose values need only be hashable if the dictionary itself must be hashable. For example:
in_listwill be[{frozenlist(["hello"]): ["goodbye"]}](because we should be backwards compatible with the expectation that lists in value-position are mutable) butin_setwill be{{frozenlist(["hello"]): frozenlist(["goodbye"])}}(because the dictionary is inside a set and therefore it, itself, must be hashable).Aside: I also do an ugly subclass to put a type parameter on the
FrozenListclass fromfrozenlist, a Python library.cc: @tpoterba , dang this was trickier than I hoped.