Skip to content

Commit

Permalink
fix distinct for json
Browse files Browse the repository at this point in the history
Signed-off-by: Praneeth Bedapudi <[email protected]>
  • Loading branch information
bedapudi6788 committed Oct 11, 2024
1 parent 8c3bbba commit f14858e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions liteindex/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,16 @@ def distinct_query(table_name, column, query, schema):

# Build the query string
if schema[column] == "json":
query_str = f"SELECT DISTINCT JSON_EXTRACT({column}, '$[*]') FROM {table_name}"
query_str = f"""
SELECT DISTINCT value
FROM (
SELECT json_each.value
FROM {table_name}, json_each({table_name}.{column})
) subquery
"""
else:
query_str = f"SELECT DISTINCT {column} FROM {table_name}"

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

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

if schema[column] == "json":
query_str = f"SELECT JSON_EXTRACT({column}, '$[*]') AS extracted_column, COUNT(*) AS count FROM {table_name}"
query_str = f"""
SELECT value, COUNT(*) AS count
FROM (
SELECT json_each.value
FROM {table_name}, json_each({table_name}.{column})
) subquery
"""
else:
query_str = f"SELECT {column}, COUNT(*) AS count FROM {table_name}"

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

if schema[column] == "json":
query_str += " GROUP BY extracted_column"
else:
query_str += f" GROUP BY {column}"

query_str += " GROUP BY value"
query_str += f" HAVING COUNT(*) >= {min_count}"

if top_n is not None:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
EMAIL = "[email protected]"
AUTHOR = "BEDAPUDI PRANEETH"
REQUIRES_PYTHON = ">=3.6.0"
VERSION = "0.0.3"
VERSION = "0.0.3.1"

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

0 comments on commit f14858e

Please sign in to comment.