Skip to content

Commit f14858e

Browse files
committed
fix distinct for json
Signed-off-by: Praneeth Bedapudi <[email protected]>
1 parent 8c3bbba commit f14858e

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

liteindex/query_parser.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,16 @@ def distinct_query(table_name, column, query, schema):
246246

247247
# Build the query string
248248
if schema[column] == "json":
249-
query_str = f"SELECT DISTINCT JSON_EXTRACT({column}, '$[*]') FROM {table_name}"
249+
query_str = f"""
250+
SELECT DISTINCT value
251+
FROM (
252+
SELECT json_each.value
253+
FROM {table_name}, json_each({table_name}.{column})
254+
) subquery
255+
"""
250256
else:
251257
query_str = f"SELECT DISTINCT {column} FROM {table_name}"
258+
252259
if where_conditions:
253260
query_str += f" WHERE {' AND '.join(where_conditions)}"
254261

@@ -259,18 +266,20 @@ def distinct_count_query(table_name, column, query, schema, min_count=0, top_n=N
259266
where_conditions, params = parse_query(query, schema)
260267

261268
if schema[column] == "json":
262-
query_str = f"SELECT JSON_EXTRACT({column}, '$[*]') AS extracted_column, COUNT(*) AS count FROM {table_name}"
269+
query_str = f"""
270+
SELECT value, COUNT(*) AS count
271+
FROM (
272+
SELECT json_each.value
273+
FROM {table_name}, json_each({table_name}.{column})
274+
) subquery
275+
"""
263276
else:
264277
query_str = f"SELECT {column}, COUNT(*) AS count FROM {table_name}"
265278

266279
if where_conditions:
267280
query_str += f" WHERE {' AND '.join(where_conditions)}"
268281

269-
if schema[column] == "json":
270-
query_str += " GROUP BY extracted_column"
271-
else:
272-
query_str += f" GROUP BY {column}"
273-
282+
query_str += " GROUP BY value"
274283
query_str += f" HAVING COUNT(*) >= {min_count}"
275284

276285
if top_n is not None:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
1919
AUTHOR = "BEDAPUDI PRANEETH"
2020
REQUIRES_PYTHON = ">=3.6.0"
21-
VERSION = "0.0.3"
21+
VERSION = "0.0.3.1"
2222

2323
# What packages are required for this module to be executed?
2424
REQUIRED = []

0 commit comments

Comments
 (0)