Skip to content

Commit

Permalink
⚡️ (store/sql) Don't use inner query if not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
simonwoerpel committed Jul 27, 2024
1 parent e95c10a commit 564d265
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ repos:
hooks:
- id: pyproject-flake8
additional_dependencies: [ flake8-bugbear ]
args: [ "--extend-ignore", "E203, E501" ]
args: [ "--extend-ignore", "E203, E501, W503" ]
exclude: (test_[\w]+\.py|\.csv|\.json|\.lock)$

- repo: https://github.com/codespell-project/codespell
Expand Down
14 changes: 9 additions & 5 deletions ftmq/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ def all_canonical_ids(self) -> Select:

@cached_property
def _unsorted_statements(self) -> Select:
return (
select(self.table)
.where(self.table.c.canonical_id.in_(self.canonical_ids))
.order_by(self.table.c.canonical_id)
)
where = self.clause
if (
self.q.properties
or self.q.reversed
or self.q.search_filters
or self.q.limit
):
where = self.table.c.canonical_id.in_(self.canonical_ids)
return select(self.table).where(where).order_by(self.table.c.canonical_id)

@cached_property
def _sorted_statements(self) -> Select:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[flake8]
max-line-length = 88
select = C,E,F,W,B,B950
extend-ignore = E203, E501
extend-ignore = E203, E501, W503
29 changes: 29 additions & 0 deletions tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,35 @@ def test_sql():
""",
)

# simplified if no properties, reversed or search:
q = Query()
assert _compare_str(
q.sql.statements,
"""
SELECT test_table.id, test_table.entity_id, test_table.canonical_id,
test_table.prop, test_table.prop_type, test_table.schema, test_table.value,
test_table.original_value, test_table.dataset, test_table.lang,
test_table.target, test_table.external, test_table.first_seen,
test_table.last_seen FROM test_table ORDER BY test_table.canonical_id
""",
)
q = Query().where(dataset="foo", schema="Person")
assert _compare_str(
q.sql.statements,
"""
SELECT test_table.id, test_table.entity_id, test_table.canonical_id,
test_table.prop, test_table.prop_type, test_table.schema,
test_table.value, test_table.original_value, test_table.dataset,
test_table.lang, test_table.target, test_table.external,
test_table.first_seen, test_table.last_seen FROM test_table WHERE
test_table.dataset = :dataset_1 AND test_table.schema = :schema_1 ORDER
BY test_table.canonical_id
""",
)

# but we need complex query if we want a limit:
assert "canonical_id IN" in str(q[:10].sql.statements)


def test_sql_search_query():
q = Query().search("agency", ["name"])
Expand Down
6 changes: 6 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def _run_store_test(cls: Store, proxies, **kwargs):
break
assert tested

# iterate
entities = [e for e in store.iterate()]
assert len(entities) == 474 + 151
entities = [e for e in store.iterate(dataset="eu_authorities")]
assert len(entities) == 151

view = store.default_view()
ds = make_dataset("eu_authorities")
view = store.view(ds)
Expand Down

0 comments on commit 564d265

Please sign in to comment.