Skip to content

Commit

Permalink
Correct output forSELECT with OFFSET and content type `applicatio…
Browse files Browse the repository at this point in the history
…n/sparql-results+json` (#1535)

Fixes the issue that an `,` was present in the result JSON before the first binding if the `OFFSET` was non-zero.

Fixes #1531
  • Loading branch information
Qup42 authored Oct 4, 2024
1 parent 060bd89 commit 342e06d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/engine/ExportQueryExecutionTrees.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,13 +626,15 @@ ad_utility::streams::stream_generator ExportQueryExecutionTrees::
return binding.dump();
};

bool isFirstRow = true;
for (const auto& [idTable, range] : getRowIndices(limitAndOffset, *result)) {
for (uint64_t i : range) {
if (i != 0) [[likely]] {
if (!isFirstRow) [[likely]] {
co_yield ",";
}
co_yield getBinding(idTable, i);
cancellationHandle->throwIfCancelled();
isFirstRow = false;
}
}

Expand Down
47 changes: 47 additions & 0 deletions test/ExportQueryExecutionTreesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,53 @@ TEST(ExportQueryExecutionTrees, MultipleVariables) {
runSelectQueryTestCase(testCaseMultipleVariables);
}

// ____________________________________________________________________________
TEST(ExportQueryExecutionTrees, LimitOffset) {
std::string kg = "<a> <b> <c> . <d> <e> <f> . <g> <h> <i> . <j> <k> <l>";
std::string objectQuery =
"SELECT ?s WHERE { ?s ?p ?o } ORDER BY ?s LIMIT 2 OFFSET 1";
std::string expectedXml = makeXMLHeader({"s"}) +
R"(
<result>
<binding name="s"><uri>d</uri></binding>
</result>
<result>
<binding name="s"><uri>g</uri></binding>
</result>)" + xmlTrailer;
TestCaseSelectQuery testCaseLimitOffset{
kg, objectQuery, 2,
// TSV
"?s\n"
"<d>\n"
"<g>\n",
// CSV
"s\n"
"d\n"
"g\n",
[]() {
nlohmann::json j;
j.push_back(std::vector{
"<d>"s,
});
j.push_back(std::vector{
"<g>"s,
});
return j;
}(),
[]() {
nlohmann::json j;
j["head"]["vars"].push_back("s");
auto& bindings = j["results"]["bindings"];
bindings.emplace_back();
bindings.back()["s"] = makeJSONBinding(std::nullopt, "uri", "d");
bindings.emplace_back();
bindings.back()["s"] = makeJSONBinding(std::nullopt, "uri", "g");
return j;
}(),
expectedXml};
runSelectQueryTestCase(testCaseLimitOffset);
}

// ____________________________________________________________________________
TEST(ExportQueryExecutionTrees, BinaryExport) {
std::string kg = "<s> <p> 31 . <s> <o> 42";
Expand Down

0 comments on commit 342e06d

Please sign in to comment.