summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2022-01-06 22:35:27 +1000
committerGitHub <noreply@github.com>2022-01-06 22:35:27 +1000
commit4cdd742d23ce8f9ec1c8c8efa44962f5db73bee1 (patch)
tree3f2b5471c990ee211b63abcf3318f4aa7e3235ff
parent6aefe60ecd1f4c0b069919d898b3ed42a2d9cd03 (diff)
parentc6c288253e982635e94866f18940f609765098ef (diff)
downloadrdflib-4cdd742d23ce8f9ec1c8c8efa44962f5db73bee1.tar.gz
Merge pull request #1645 from GreenfishK/translate_algebra_tests
added three tests to cover changes made by the pull request #1361
-rw-r--r--rdflib/plugins/sparql/algebra.py18
-rw-r--r--test/test_translate_algebra.py20
-rw-r--r--test/translate_algebra/test_functions__functions_on_strings.txt1
-rw-r--r--test/translate_algebra/test_graph_patterns__group_and_nested_concat.txt7
-rw-r--r--test/translate_algebra/test_graph_patterns__group_and_substr.txt9
-rw-r--r--tox.ini6
6 files changed, 53 insertions, 8 deletions
diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py
index 805e62a8..a6e20b0f 100644
--- a/rdflib/plugins/sparql/algebra.py
+++ b/rdflib/plugins/sparql/algebra.py
@@ -847,9 +847,15 @@ def translateAlgebra(query_algebra: Query):
with open("query.txt", "w") as file:
file.write(filedata)
+ aggr_vars = collections.defaultdict(list) # type: dict
+
def convert_node_arg(node_arg):
if isinstance(node_arg, Identifier):
- return node_arg.n3()
+ if node_arg in aggr_vars.keys():
+ grp_var = aggr_vars[node_arg].pop(0).n3()
+ return grp_var
+ else:
+ return node_arg.n3()
elif isinstance(node_arg, CompValue):
return "{" + node_arg.name + "}"
elif isinstance(node_arg, Expr):
@@ -959,7 +965,9 @@ def translateAlgebra(query_algebra: Query):
raise ExpressionNotCoveredException(
"This expression might not be covered yet."
)
- agg_func_name = agg_func.name.split("_")[1]
+ aggr_vars[agg_func.res].append(agg_func.vars)
+
+ agg_func_name = agg_func.name.split('_')[1]
distinct = ""
if agg_func.distinct:
distinct = agg_func.distinct + " "
@@ -1022,7 +1030,7 @@ def translateAlgebra(query_algebra: Query):
if isinstance(c.expr, Identifier):
var = c.expr.n3()
if c.order is not None:
- cond = var + "(" + c.order + ")"
+ cond = c.order + "(" + var + ")"
else:
cond = var
order_conditions.append(cond)
@@ -1233,7 +1241,7 @@ def translateAlgebra(query_algebra: Query):
"{Builtin_STRLEN}", "STRLEN(" + convert_node_arg(node.arg) + ")"
)
elif node.name.endswith("Builtin_SUBSTR"):
- args = [node.arg.n3(), node.start]
+ args = [convert_node_arg(node.arg), node.start]
if node.length:
args.append(node.length)
expr = "SUBSTR(" + ", ".join(args) + ")"
@@ -1294,7 +1302,7 @@ def translateAlgebra(query_algebra: Query):
)
elif node.name.endswith("Builtin_CONCAT"):
expr = "CONCAT({vars})".format(
- vars=", ".join(elem.n3() for elem in node.arg)
+ vars=", ".join(convert_node_arg(elem) for elem in node.arg)
)
replace("{Builtin_CONCAT}", expr)
elif node.name.endswith("Builtin_LANGMATCHES"):
diff --git a/test/test_translate_algebra.py b/test/test_translate_algebra.py
index c94ab180..0d0d83f2 100644
--- a/test/test_translate_algebra.py
+++ b/test/test_translate_algebra.py
@@ -119,6 +119,16 @@ algebra_tests = [
'Test if "group" gets properly translated into the query text.',
),
AlgebraTest(
+ "test_graph_patterns__group_and_substr",
+ 'Test if a query with a variable that is used in the "GROUP BY" clause '
+ 'and in the SUBSTR function gets properly translated into the query text.',
+ ),
+ AlgebraTest(
+ "test_graph_patterns__group_and_nested_concat",
+ 'Test if a query with a nested concat expression in the select clause which '
+ 'uses a group variable gets properly translated into the query text.',
+ ),
+ AlgebraTest(
"test_graph_patterns__having",
'Test if "having" gets properly translated into the query text.',
),
@@ -266,7 +276,7 @@ def test_all_files_used(data_path: Path) -> None:
@pytest.mark.parametrize("test_spec", [test.pytest_param() for test in algebra_tests])
def test_roundtrip(test_spec: AlgebraTest, data_path: Path) -> None:
"""
- Query remains the same over two successuive parse and translate cycles.
+ Query remains the same over two successive parse and translate cycles.
"""
query_text = (data_path / test_spec.filename).read_text()
@@ -274,18 +284,22 @@ def test_roundtrip(test_spec: AlgebraTest, data_path: Path) -> None:
query_tree = parser.parseQuery(query_text)
query_algebra = algebra.translateQuery(query_tree)
query_from_algebra = translateAlgebra(query_algebra)
+ logging.debug(
+ "query_from_query_from_algebra = \n%s",
+ query_from_algebra,
+ )
query_tree_2 = parser.parseQuery(query_from_algebra)
query_algebra_2 = algebra.translateQuery(query_tree_2)
query_from_query_from_algebra = translateAlgebra(query_algebra_2)
logging.debug(
"query_from_query_from_algebra = \n%s",
- _format_query(query_from_query_from_algebra),
+ query_from_query_from_algebra,
)
assert (
query_from_algebra == query_from_query_from_algebra
), f"failed expectation: {test_spec.description}"
- # TODO: Execute the raw query (query_text) and the reconstitued query
+ # TODO: Execute the raw query (query_text) and the reconstituted query
# (query_from_query_from_algebra) against a well defined graph and ensure
# they yield the same result.
diff --git a/test/translate_algebra/test_functions__functions_on_strings.txt b/test/translate_algebra/test_functions__functions_on_strings.txt
index a5921c6e..633b771f 100644
--- a/test/translate_algebra/test_functions__functions_on_strings.txt
+++ b/test/translate_algebra/test_functions__functions_on_strings.txt
@@ -12,6 +12,7 @@ select
(concat("foo"@en, "bar"@en) as ?concat)
(replace("abcd", "b", "Z") as ?replace)
(regex(substr("foobar", 4), "bar", "bar") as ?regex)
+(regex(substr("foobar", 4, 1), "b", "b") as ?regex2)
where {
?s ?p ?o .
FILTER langMatches(lang(?o), "EN" )
diff --git a/test/translate_algebra/test_graph_patterns__group_and_nested_concat.txt b/test/translate_algebra/test_graph_patterns__group_and_nested_concat.txt
new file mode 100644
index 00000000..c47bf50d
--- /dev/null
+++ b/test/translate_algebra/test_graph_patterns__group_and_nested_concat.txt
@@ -0,0 +1,7 @@
+PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+
+SELECT
+(concat(str(?a), concat("foo"@en, "bar"@en)) as ?nested_concat)
+WHERE {
+ ?a rdf:value ?val .
+} GROUP BY ?a
diff --git a/test/translate_algebra/test_graph_patterns__group_and_substr.txt b/test/translate_algebra/test_graph_patterns__group_and_substr.txt
new file mode 100644
index 00000000..9d990969
--- /dev/null
+++ b/test/translate_algebra/test_graph_patterns__group_and_substr.txt
@@ -0,0 +1,9 @@
+PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+
+SELECT
+(SUM(?val) AS ?sum)
+(COUNT(?a) AS ?count)
+(SUBSTR(?a, 4, 1) as ?a_substr)
+WHERE {
+ ?a rdf:value ?val .
+} GROUP BY ?a
diff --git a/tox.ini b/tox.ini
index c09483da..3d0121d6 100644
--- a/tox.ini
+++ b/tox.ini
@@ -45,3 +45,9 @@ setenv =
PYTHONHASHSEED = 0
commands =
sphinx-build -n -T -b html -d {envtmpdir}/doctrees docs docs/_build/html
+
+[pytest]
+# log_cli = true
+# log_cli_level = DEBUG
+log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
+log_cli_date_format=%Y-%m-%d %H:%M:%S