Repository navigation
Support * wildcards in filter_values (#119) - #206
ChitrarupGuha wants to merge 2 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Hi @ChitrarupGuha! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
|
||
| return [ | ||
| feature for feature in data if feature["properties"].get(property) in values | ||
| exact = [v for v in values if not (isinstance(v, str) and "*" in v)] |
There was a problem hiding this comment.
@ChitrarupGuha -- slightly concerned about the performance concern with this regex that could add up slightly more on each call. Each * becomes a .*, so a pattern with k wildcards can backtrack in roughly O(nᵏ) time on near-miss input.
Can we try something like this and test in test_filter.py for an perf degradations? Matching each middle piece at its leftmost position is always correct, so there's no backtracking and str.find keeps the check linear.
Quick AI code of what I was thinking about,
def _compile_glob(pattern: str) -> tuple[str, list[str], str]:
first, *rest = pattern.split("*")
*mid, last = rest
return first, mid, last
def _glob_match(glob: tuple[str, list[str], str], s: str) -> bool:
first, mid, last = glob
if len(s) < len(first) + len(last):
return False
if not (s.startswith(first) and s.endswith(last)):
return False
pos, end = len(first), len(s) - len(last)
for piece in mid:
i = s.find(piece, pos, end)
if i < 0:
return False
pos = i + len(piece)
return TrueAnd then,
exact = {v for v in values if not (isinstance(v, str) and "*" in v)}
globs = [_compile_glob(v) for v in values if isinstance(v, str) and "*" in v]
def matches(candidate) -> bool:
return if candidate in exact then True else isinstance(candidate, str) and any(_glob_match(g, candidate) for g in globs)
return [f for f in data if matches(f["properties"].get(property))]There was a problem hiding this comment.
Thanks! Switched to the str.find approach, added a perf test and edge cases (consecutive *, prefix/suffix overlap). Kept exact as a list so unhashable property values don't raise; happy to switch to a set if you prefer.
There was a problem hiding this comment.
Thanks, @ChitrarupGuha ! Just to verify, the tests succeeded, correct?
I have approved.
There was a problem hiding this comment.
Yes, the 15 filter_values tests pass locally (including the new perf test), and CI is green on the latest commit. Thanks for the review!
|
Hi @caglarpir -- can we merge this? |
filter_valuesnow accepts*as a wildcard in string values, matching any run of characters (e.g.object--support--*).*match exactly, as before..stays a literal dot.tests/utils/test_filter.py.Addresses #119.