Skip to content

Commit

Permalink
Add more control over grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
CannonLock committed May 9, 2024
1 parent ab66f7f commit 7b86c84
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 1 addition & 6 deletions api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ async def select_sources_sub_table(

stmt = (
select(*selected_columns)
.order_by(*query_parser.get_order_by_columns())
.limit(page_size)
.offset(page_size * page)
.where(query_parser.where_expressions())
Expand All @@ -253,12 +254,6 @@ async def select_sources_sub_table(
query_parser.get_group_by_column()
)

if (
query_parser.get_order_by_columns() is not None
and query_parser.get_group_by_column() is None
):
stmt = stmt.order_by(*query_parser.get_order_by_columns())

x = str(stmt.compile(compile_kwargs={
"literal_binds": True
}))
Expand Down
16 changes: 13 additions & 3 deletions api/query_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ParserException(Exception):
def get_filter_query_params(request: Request) -> list[tuple[str, str]]:
"""Returns the query params that are not page or page_size"""

return [*filter(lambda x: x[0] not in ["page", "page_size"], request.query_params.items())]
return [*filter(lambda x: x[0] not in ["page", "page_size"], request.query_params.multi_items())]


def cast_to_column_type(column: Column, value):
Expand Down Expand Up @@ -224,7 +224,17 @@ def get_order_by_columns(self):
continue

if query_param.operators[0] == "order_by":
order_by_columns.append(query_param.column)
# Check if the order_by is valid
if query_param.value not in ["asc", "desc"]:
raise HTTPException(
status_code=400,
detail=f"Query is invalid. Use asc or desc for order_by"
)

if query_param.value == "asc":
order_by_columns.append(query_param.column.asc())
else:
order_by_columns.append(query_param.column.desc())

return order_by_columns

Expand Down Expand Up @@ -257,7 +267,7 @@ def _decompose_encoded_expression(self, encoded_expression) -> tuple:

# If group_by or order_by, then there is no value
if len(encoded_expression_split) == 1:
if encoded_expression_split[0] not in ["group_by", "order_by"]:
if encoded_expression_split[0] not in ["group_by"]:
raise ParserException(f"Query is invalid.")

return encoded_expression_split[:1], ""
Expand Down
1 change: 1 addition & 0 deletions api/routes/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async def get_multiple_ingest_process(page: int = 0, page_size: int = 50,
.limit(page_size) \
.offset(page_size * page) \
.where(and_(query_parser.where_expressions())) \
.order_by(*query_parser.get_order_by_columns()) \
.options(joinedload(IngestProcessSchema.source).defer(Sources.rgeom).defer(Sources.web_geom)) \
.options(selectinload(IngestProcessSchema.tags))

Expand Down

0 comments on commit 7b86c84

Please sign in to comment.