Skip to content

Commit

Permalink
Merge pull request #4236 from ckan/4236-datastore-total-fix
Browse files Browse the repository at this point in the history
datastore_search fix: total incorrect when distinct=true
  • Loading branch information
amercader committed Aug 17, 2018
2 parents 0cca7c9 + 45b71be commit 047b3f3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions ckanext/datastore/backend/postgres.py
Expand Up @@ -1230,9 +1230,9 @@ def search_data(context, data_dict):
).replace('%', '%%')
sql_fmt = u'''
SELECT '[' || array_to_string(array_agg(j.v), ',') || ']' FROM (
SELECT '[' || {select} || ']' v
SELECT {distinct} '[' || {select} || ']' v
FROM (
SELECT {distinct} * FROM "{resource}" {ts_query}
SELECT * FROM "{resource}" {ts_query}
{where} {sort} LIMIT {limit} OFFSET {offset}) as z
) AS j'''
elif records_format == u'csv':
Expand Down Expand Up @@ -1287,9 +1287,11 @@ def search_data(context, data_dict):
_insert_links(data_dict, limit, offset)

if data_dict.get('include_total', True):
count_sql_string = u'''SELECT {distinct} count(*)
FROM "{resource}" {ts_query} {where};'''.format(
count_sql_string = u'''SELECT count(*) FROM (
SELECT {distinct} {select}
FROM "{resource}" {ts_query} {where}) as t;'''.format(
distinct=distinct,
select=select_columns,
resource=resource_id,
ts_query=ts_query,
where=where_clause)
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datastore/tests/test_search.py
Expand Up @@ -284,7 +284,7 @@ def test_search_distinct(self):
res_dict = json.loads(res.body)
assert res_dict['success'] is True
result = res_dict['result']
assert result['total'] == 2
assert result['total'] == 1
assert result['records'] == [{u'author': 'tolstoy'}], result['records']

def test_search_filters(self):
Expand Down

0 comments on commit 047b3f3

Please sign in to comment.