Skip to content

Commit

Permalink
apply limit and offset in query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ssssarah committed Oct 25, 2023
1 parent b2ad03d commit 962d5cf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
23 changes: 16 additions & 7 deletions kgforge/core/archetypes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,27 @@ def sparql(
) -> List[Resource]:
rewrite = params.get("rewrite", True)

qr = SPARQLQueryBuilder.handle_sparql_query(
query=query,
model_context=self.context(),
metadata_context=None, # TODO something else?
rewrite=rewrite,
qr = (
SPARQLQueryBuilder.rewrite_sparql(
query,
self.context(),
metadata_context=None # TODO smth else?
)
if self.context() is not None and rewrite
else query
)

qr = SPARQLQueryBuilder.apply_limit_and_offset_to_query(
query=qr,
limit=limit,
offset=offset,
default_limit=DEFAULT_LIMIT,
default_offset=DEFAULT_OFFSET,
debug=debug
default_offset=DEFAULT_OFFSET
)

if debug:
SPARQLQueryBuilder.debug_query(qr)

return self._sparql(qr)

def _sparql(self, query: str) -> List[Resource]:
Expand Down
35 changes: 24 additions & 11 deletions kgforge/core/archetypes/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,27 @@ def sparql(
) -> List[Resource]:
rewrite = params.get("rewrite", True)

qr = SPARQLQueryBuilder.handle_sparql_query(
query=query,
model_context=self.model_context,
metadata_context=self.service.metadata_context,
rewrite=rewrite,
qr = (
SPARQLQueryBuilder.rewrite_sparql(
query,
context=self.model_context,
metadata_context=self.service.metadata_context,
)
if self.model_context is not None and rewrite
else query
)

qr = SPARQLQueryBuilder.apply_limit_and_offset_to_query(
qr,
limit=limit,
offset=offset,
default_limit=DEFAULT_LIMIT,
default_offset=DEFAULT_OFFSET,
debug=debug
default_offset=DEFAULT_OFFSET
)

if debug:
SPARQLQueryBuilder.debug_query(qr)

return self._sparql(qr)

def _sparql(self, query: str) -> List[Resource]:
Expand All @@ -410,12 +419,16 @@ def elastic(
self, query: str, debug: bool, limit: int = DEFAULT_LIMIT, offset: int = DEFAULT_OFFSET
) -> List[Resource]:
query_dict = json.loads(query)
if limit:
query_dict["size"] = limit
if offset:
query_dict["from"] = offset

query_dict = ESQueryBuilder.apply_limit_and_offset_to_query(
query_dict,
limit=limit, default_limit=None,
offset=offset, default_offset=None
)

if debug:
ESQueryBuilder.debug_query(query_dict)

return self._elastic(json.dumps(query_dict))

def _elastic(self, query: str) -> List[Resource]:
Expand Down
10 changes: 10 additions & 0 deletions kgforge/core/commons/es_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ def build(
def build_resource_from_response(query: str, response: Dict, context: Context, *args, **params) -> List[Resource]:
not_supported()

@staticmethod
def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default_offset):
# TODO should there be an elastic search default limit?
if limit:
query["size"] = limit
if offset:
query["from"] = offset

return query


def _look_up_known_parent_paths(f, last_path, property_path, m):
if (
Expand Down
4 changes: 4 additions & 0 deletions kgforge/core/commons/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ def debug_query(query):
else:
print(*["Submitted query:", *query.splitlines()], sep="\n ")

@staticmethod
@abstractmethod
def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default_offset):
pass
32 changes: 8 additions & 24 deletions kgforge/core/commons/sparql_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class CategoryDataType(Enum):

class SPARQLQueryBuilder(QueryBuilder):


@staticmethod
def build(
schema: Dict,
Expand Down Expand Up @@ -345,38 +346,21 @@ def _replace_in_sparql(

return qr


@staticmethod
def handle_sparql_query(
query: str, rewrite: bool,
limit: Optional[int],
offset: Optional[int],
default_limit: int,
default_offset: int,
model_context: Context,
metadata_context: Optional[Context],
debug: bool
):
qr = (
SPARQLQueryBuilder.rewrite_sparql(query, model_context, metadata_context)
if model_context is not None and rewrite
else query
)
def apply_limit_and_offset_to_query(query, limit, default_limit, offset, default_offset):
if limit:
qr = SPARQLQueryBuilder._replace_in_sparql(
qr, "LIMIT", limit, default_limit,
query = SPARQLQueryBuilder._replace_in_sparql(
query, "LIMIT", limit, default_limit,
re.compile(r" LIMIT \d+", flags=re.IGNORECASE)
)
if offset:
qr = SPARQLQueryBuilder._replace_in_sparql(
qr, "OFFSET", offset, default_offset,
query = SPARQLQueryBuilder._replace_in_sparql(
query, "OFFSET", offset, default_offset,
re.compile(r" OFFSET \d+", flags=re.IGNORECASE)
)

if debug:
SPARQLQueryBuilder.debug_query(qr)

return qr

return query

def _box_value_as_full_iri(value):
return f"<{value}>" if is_valid_url(value) else value

0 comments on commit 962d5cf

Please sign in to comment.