Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce multiqueries for dataset lookup #33 #230

Merged
merged 9 commits into from
Sep 1, 2021
Merged

Introduce multiqueries for dataset lookup #33 #230

merged 9 commits into from
Sep 1, 2021

Conversation

nickeopti
Copy link
Contributor

Make it possible to query datasets like /datasets?year=[2016,2018]&band=02. This closes #33.

The parsing of lists of values is implemented rather naively, but it works.

The driver.get_datasets() method now accepts where: Mapping[str, Union[str, List[str]]]. Handling both cases (conditions either string or list of strings) have introduced some somewhat convoluted code to create the SQL queries, but I think it's about as good as it gets without a larger rewrite.

@codecov
Copy link

codecov bot commented Aug 10, 2021

Codecov Report

Merging #230 (20cbe67) into main (1037297) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #230      +/-   ##
==========================================
+ Coverage   98.46%   98.47%   +0.01%     
==========================================
  Files          45       45              
  Lines        2211     2233      +22     
  Branches      276      282       +6     
==========================================
+ Hits         2177     2199      +22     
  Misses         19       19              
  Partials       15       15              
Impacted Files Coverage Δ
terracotta/drivers/base.py 100.00% <100.00%> (ø)
terracotta/drivers/mysql.py 100.00% <100.00%> (ø)
terracotta/drivers/raster_base.py 95.86% <100.00%> (ø)
terracotta/drivers/sqlite.py 100.00% <100.00%> (ø)
terracotta/handlers/datasets.py 100.00% <100.00%> (ø)
terracotta/server/datasets.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1037297...20cbe67. Read the comment docs.

Copy link
Collaborator

@dionhaefner dionhaefner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, some minor changes, and please add a test for this.

@@ -354,10 +355,19 @@ def get_datasets(self, where: Mapping[str, str] = None,
if not all(key in self.key_names for key in where.keys()):
raise exceptions.InvalidKeyError('Encountered unrecognized keys in '
'where clause')
where_fragment = ' AND '.join([f'{key}=%s' for key in where.keys()])
where = {
key: value if isinstance(value, list) else [value]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a

if isinstance(value, str):
    value = [value]

Comment on lines 360 to 367
for key, value in where.items()
}
conditions = [
'(%s)' % ' OR '.join([f'{key}=%s'] * len(value))
for key, value in where.items()
]
values = list(itertools.chain(*where.values()))
where_fragment = ' AND '.join(conditions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going a bit too far with the comprehensions. I think a straightforward loop might be more readable?

Comment on lines 256 to 265
where = {
key: value if isinstance(value, list) else [value]
for key, value in where.items()
}
conditions = [
'(%s)' % ' OR '.join([f'{key}=?'] * len(value))
for key, value in where.items()
]
values = list(itertools.chain(*where.values()))
where_fragment = ' AND '.join(conditions)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A delicate detail of style in code bases - the cases in which list comprehensions are used and where not. 😄I think we generally keep it down to very simple cases. Readability of the code is the property to optimize for.

For readability, I also prefer format strings over the old %s notation...

Copy link
Collaborator

@dionhaefner dionhaefner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

@dionhaefner dionhaefner merged commit 0966da1 into main Sep 1, 2021
@dionhaefner dionhaefner deleted the issue33 branch September 1, 2021 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce multiqueries for dataset lookup
3 participants