Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
Signed-off-by: Praneeth Bedapudi <[email protected]>
  • Loading branch information
bedapudi6788 committed Feb 19, 2024
1 parent 568693e commit 4f5cde0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion liteindex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .defined_index import DefinedIndex
from .defined_index import DefinedIndex, get_defined_index_names_in_db
from .defined_serializers import DefinedTypes
from .kv_index import KVIndex
from .function_cache import function_cache
Expand Down
29 changes: 26 additions & 3 deletions liteindex/defined_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@
import threading


def get_defined_index_names_in_db(db_path):
with sqlite3.connect(db_path, uri=True) as conn:
all_table_names = {
_[0]
for _ in conn.execute(
"SELECT name FROM sqlite_master WHERE type='table';"
).fetchall()
}

res = []
for table_name in all_table_names:
if (
not table_name.startswith("__")
and not table_name.startswith("sqlite_")
and f"__{table_name}_meta" in all_table_names
):
res.append(table_name)

return res


class DefinedIndex:
def __init__(
self, name, schema=None, db_path=None, ram_cache_mb=64, compression_level=-1
Expand Down Expand Up @@ -604,12 +625,14 @@ def distinct(self, key, query={}):
_[0] for _ in self.__connection.execute(sql_query, sql_params).fetchall()
}

def distinct_count(self, key, query={}):
def distinct_count(self, key, query={}, min_count=0, top_n=None):
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,
min_count=min_count,
top_n=top_n,
)

return {
Expand Down Expand Up @@ -729,12 +752,12 @@ def optimize_for_query(self, keys, is_unique=False):

if key_hashes:
self.__connection.execute(
f"CREATE {'UNIQUE' if is_unique else ''} INDEX IF NOT EXISTS idx_{self.name}_{'_'.join(key_hashes)} ON {self.name} ({','.join(key_hashes)})"
f"""CREATE {'UNIQUE' if is_unique else ''} INDEX IF NOT EXISTS "idx_{self.name}_{'_'.join(key_hashes)}" ON {self.name} ({','.join(['"' + _ + '"' for _ in key_hashes])})"""
)

for size_hash in size_hashes:
self.__connection.execute(
f"CREATE INDEX IF NOT EXISTS idx_{self.name}_{size_hash} ON {self.name} ({size_hash})"
f"""CREATE INDEX IF NOT EXISTS "idx_{self.name}_{size_hash}" ON {self.name} ({size_hash})"""
)

self.__connection.commit()
Expand Down
19 changes: 14 additions & 5 deletions liteindex/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,26 @@ def distinct_query(table_name, column, query, schema):
return query_str, params


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

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

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

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

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

if top_n is not None:
query_str += f" ORDER BY COUNT(*) DESC LIMIT {top_n}"

return query_str, params

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.dev56"
VERSION = "0.0.2.dev57"

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

0 comments on commit 4f5cde0

Please sign in to comment.