Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/2093#discussion_r16633886
--- Diff: python/pyspark/rdd.py ---
@@ -1777,10 +1698,28 @@ def _defaultReducePartitions(self):
else:
return self.getNumPartitions()
- # TODO: `lookup` is disabled because we can't make direct comparisons
based
- # on the key; we need to compare the hash of the key to the hash of the
- # keys in the pairs. This could be an expensive operation, since those
- # hashes aren't retained.
+ def lookup(self, key):
+ """
+ Return the list of values in the RDD for key `key`. This operation
+ is done efficiently if the RDD has a known partitioner by only
+ searching the partition that the key maps to.
+
+ >>> l = range(1000)
+ >>> rdd = sc.parallelize(zip(l, l), 10)
+ >>> rdd.lookup(42) # slow
+ [42]
+ >>> sorted = rdd.sortByKey()
+ >>> sorted.lookup(42) # fast
+ [42]
+ >>> sorted.lookup(1024)
+ []
+ """
+ self = self.filter(lambda (k, v): k == key).values()
--- End diff --
This reassignment to `self` seems potentially confusing. Could you use a
different name for the filtered RDD, such as `filtered`? I think even a
one-letter variable name, like `f`, is preferable to re-assigning `self`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]