Skip to content

Commit

Permalink
get distinct_count
Browse files Browse the repository at this point in the history
Signed-off-by: Praneeth Bedapudi <[email protected]>
  • Loading branch information
bedapudi6788 committed Feb 11, 2024
1 parent 8ce4fe7 commit 8ca41e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions liteindex/defined_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from .query_parser import (
search_query,
distinct_query,
distinct_count_query,
count_query,
delete_query,
group_by_query,
Expand Down Expand Up @@ -571,6 +572,19 @@ def distinct(self, key, query={}):
_[0] for _ in self.__connection.execute(sql_query, sql_params).fetchall()
}

def distinct_count(self, key, query={}):
sql_query, sql_params = distinct_count_query(
table_name=self.name,
column=key,
query={k: v for k, v in query.items()},
schema=self.schema,
)

return {
_[0]: _[1]
for _ in self.__connection.execute(sql_query, sql_params).fetchall()
}

def group(self, keys, query={}):
if isinstance(keys, str):
keys = [keys]
Expand Down
15 changes: 15 additions & 0 deletions liteindex/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ def distinct_query(table_name, column, query, schema):
return query_str, params


def distinct_count_query(table_name, column, query, schema):
# Prepare the query
where_conditions, params = parse_query(query, schema)

if schema[column] == "json":
query_str = f"SELECT JSON_EXTRACT({column}, '$[*]'), COUNT(*) FROM {table_name}"
else:
query_str = f"SELECT {column}, COUNT(*) FROM {table_name}"
if where_conditions:
query_str += f" WHERE {' AND '.join(where_conditions)}"
query_str += f" GROUP BY {column}"

return query_str, params


def group_by_query(table_name, columns, query, schema):
# Prepare the query
where_conditions, params = parse_query(query, schema)
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.2.dev48"
VERSION = "0.0.2.dev49"

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

0 comments on commit 8ca41e3

Please sign in to comment.