Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement query sanity checking #129

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 9 additions & 34 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sparqlwrapper = "^2.0.0"
pydantic = "^2.9.2"


rdflib = "^7.1.1"
[tool.poetry.group.dev.dependencies]
ruff = "^0.7.0"
deptry = "^0.20.0"
Expand Down
3 changes: 2 additions & 1 deletion rdfproxy/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Generic

from SPARQLWrapper import JSON, SPARQLWrapper
from rdfproxy.checks.checkers import check_query
from rdfproxy.mapper import ModelBindingsMapper
from rdfproxy.utils._types import _TModelInstance
from rdfproxy.utils.models import Page
Expand Down Expand Up @@ -34,7 +35,7 @@ class SPARQLModelAdapter(Generic[_TModelInstance]):
def __init__(
self, target: str | SPARQLWrapper, query: str, model: type[_TModelInstance]
) -> None:
self._query = query
self._query = check_query(query)
self._model = model

self.sparql_wrapper: SPARQLWrapper = (
Expand Down
34 changes: 34 additions & 0 deletions rdfproxy/checks/checkers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""RDFProxy check runners."""

from collections.abc import Callable
from typing import Annotated, NoReturn, TypeVar

from rdfproxy.checks.query_checks import (
check_parse_query,
check_select_query,
check_solution_modifiers,
)


T = TypeVar("T")
_TCheck = Callable[[T], T | NoReturn]


def compose_left(*fns: Callable) -> Callable:
def _left_wrapper(*fns):
fn, *rest_fns = fns

if rest_fns:
return lambda *args, **kwargs: fn(_left_wrapper(*rest_fns)(*args, **kwargs))
return fn

return _left_wrapper(*reversed(fns))


def compose_checker(*checkers: _TCheck) -> _TCheck:
return compose_left(*checkers)


check_query: Annotated[
_TCheck, "Run a series of checks on a query and return the query."
] = compose_checker(check_parse_query, check_select_query, check_solution_modifiers)
29 changes: 29 additions & 0 deletions rdfproxy/checks/query_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Query checks definitions."""

from typing import NoReturn, TypeVar

from rdflib.plugins.sparql.parser import parseQuery
from rdfproxy.utils._exceptions import UnsupportedQueryException
from rdfproxy.utils.predicates import (
query_has_solution_modifiers,
query_is_select_query,
)

TQuery = TypeVar("TQuery", bound=str)


def check_parse_query(query: TQuery) -> TQuery | NoReturn:
parseQuery(query)
return query


def check_select_query(query: TQuery) -> TQuery | NoReturn:
if not query_is_select_query(query):
raise UnsupportedQueryException("Only SELECT queries are applicable.")
return query


def check_solution_modifiers(query: TQuery) -> TQuery | NoReturn:
if query_has_solution_modifiers(query):
raise UnsupportedQueryException("SPARQL solution modifieres are not supported.")
return query
4 changes: 4 additions & 0 deletions rdfproxy/utils/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ class MissingModelConfigException(Exception):

class UnboundGroupingKeyException(Exception):
"""Exception for indicating that no SPARQL binding corresponds to the requested grouping key."""


class UnsupportedQueryException(Exception):
"""Exception for indicating that a given SPARQL query is not supported."""
18 changes: 18 additions & 0 deletions rdfproxy/utils/predicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""RDFProxy predicate functions."""

import re

from rdflib.plugins.sparql.parser import parseQuery


def query_is_select_query(query: str) -> bool:
"""Check if a SPARQL query is a SELECT query."""
_, query_type = parseQuery(query)
return query_type.name == "SelectQuery"


def query_has_solution_modifiers(query: str) -> bool:
"""Predicate for checking if a SPARQL query has a solution modifier."""
pattern = r"}[^}]*\w+$"
result = re.search(pattern, query)
return bool(result)
Loading