Skip to content

Commit

Permalink
Replace std::string by TripelComponent in Values class
Browse files Browse the repository at this point in the history
TODO 1: Add words to the local vocabulary.
TODO 2: Actually issue a query to the endpoint (currently uses fixed
values clause, to test whether the whole thing works in principle).
  • Loading branch information
Hannah Bast committed Oct 23, 2022
1 parent 1cc57ba commit ade9752
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/engine/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Service::Service(QueryExecutionContext* qec,
"\"Michael Fred Phelps, II\"@en"},
{"<http://wallscope.co.uk/resource/olympics/athlete/"
"BirgitFischerSchmidt>",
"\"Birgit Fischer-Schmidt\"@en"}};
// 73643}};
"\"73264732\"^^<http://www.w3.org/2001/XMLSchema#int>"}};
// "\"Birgit Fischer-Schmidt\"@en"}};
resultAsValues_ = std::make_unique<Values>(qec, std::move(sparqlValues));
}

Expand Down
27 changes: 16 additions & 11 deletions src/engine/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ string Values::asStringImpl(size_t indent) const {
}
os << ") {";
for (size_t i = 0; i < _values._values.size(); i++) {
const vector<string>& v = _values._values[i];
const vector<TripleComponent>& v = _values._values[i];
os << "(";
for (size_t j = 0; j < v.size(); j++) {
os << v[j];
Expand Down Expand Up @@ -57,7 +57,7 @@ string Values::getDescriptor() const {
}
os << " and values ";
for (size_t i = 0; i < _values._values.size(); i++) {
const vector<string>& v = _values._values[i];
const vector<TripleComponent>& v = _values._values[i];
os << "(";
for (size_t j = 0; j < v.size(); j++) {
os << v[j];
Expand Down Expand Up @@ -107,13 +107,13 @@ void Values::computeMultiplicities() {
return;
}
_multiplicities.resize(_values._variables.size());
ad_utility::HashSet<string> values;
ad_utility::HashSet<TripleComponent> values;
for (size_t col = 0; col < _values._variables.size(); col++) {
values.clear();
size_t count = 0;
size_t distinct = 0;
for (size_t j = 0; j < _values._values.size(); j++) {
const std::string& v = _values._values[j][col];
const TripleComponent& v = _values._values[j][col];
count++;
if (values.count(v) == 0) {
distinct++;
Expand All @@ -133,7 +133,8 @@ void Values::computeResult(ResultTable* result) {
ResultTable::ResultType::KB);

size_t resWidth = getResultWidth();
CALL_FIXED_SIZE_1(resWidth, writeValues, &result->_idTable, index, _values);
CALL_FIXED_SIZE_1(resWidth, writeValues, &result->_idTable, index, _values,
result->_localVocab);
}

auto Values::sanitizeValues(SparqlValues&& values) -> SparqlValues {
Expand Down Expand Up @@ -187,23 +188,27 @@ auto Values::sanitizeValues(SparqlValues&& values) -> SparqlValues {

template <size_t I>
void Values::writeValues(IdTable* res, const Index& index,
const SparqlValues& values) {
const SparqlValues& values,
std::shared_ptr<ResultTable::LocalVocab> localVocab) {
IdTableStatic<I> result = res->moveToStatic<I>();
result.resize(values._values.size());
size_t numActuallyWritten = 0;
size_t numSkipped = 0;
for (const auto& row : values._values) {
for (size_t colIdx = 0; colIdx < result.cols(); colIdx++) {
Id id;
if (!index.getId(row[colIdx], &id)) {
getWarnings().push_back("The word " + row[colIdx] +
" is not part of the vocabulary.");
const TripleComponent& tc = row[colIdx];
std::optional<Id> id = tc.toValueId(index.getVocab());
if (!id) {
AD_CHECK(tc.isString());
getWarnings().push_back(
absl::StrCat("The word ", row[colIdx].toString(),
" is not part of the vocabulary."));
numSkipped++;
// this goto is a continue in the outer loop. The current row was not
// sucessfully written, so the numActuallyWritten index is not advanced
goto skipRow;
}
result(numActuallyWritten, colIdx) = id;
result(numActuallyWritten, colIdx) = id.value();
}
numActuallyWritten++;
skipRow:; // the label for the goto. Jumping to this label is basically
Expand Down
3 changes: 2 additions & 1 deletion src/engine/Values.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Values : public Operation {

template <size_t I>
void writeValues(IdTable* res, const Index& index,
const SparqlValues& values);
const SparqlValues& values,
std::shared_ptr<ResultTable::LocalVocab> localVocab);

/// remove all completely undefined values and variables
/// throw if nothing remains
Expand Down
2 changes: 1 addition & 1 deletion src/parser/GraphPatternOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SparqlValues {
// The variables to which the values will be bound
std::vector<std::string> _variables;
// A table storing the values in their string form
std::vector<std::vector<std::string>> _values;
std::vector<std::vector<TripleComponent>> _values;
};

/// A `SERVICE` clause.
Expand Down
6 changes: 6 additions & 0 deletions src/parser/TripleComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ class TripleComponent {
/// Equality comparison between two `TripleComponent`s.
bool operator==(const TripleComponent&) const = default;

/// Hash value for `TripleComponent` object.
template <typename H>
friend H AbslHashValue(H h, const TripleComponent& tc) {
return H::combine(std::move(h), tc._variant);
}

/// Check which type the underlying variants hold.
[[nodiscard]] bool isString() const {
return std::holds_alternative<std::string>(_variant);
Expand Down
13 changes: 8 additions & 5 deletions src/parser/sparqlParser/SparqlQleverVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ SparqlValues Visitor::visit(Parser::InlineDataFullContext* ctx) {
}

// ____________________________________________________________________________________
vector<std::string> Visitor::visit(Parser::DataBlockSingleContext* ctx) {
vector<TripleComponent> Visitor::visit(Parser::DataBlockSingleContext* ctx) {
if (ctx->NIL())
reportError(ctx,
"No values were specified in DataBlock."
Expand All @@ -738,16 +738,19 @@ vector<std::string> Visitor::visit(Parser::DataBlockSingleContext* ctx) {
}

// ____________________________________________________________________________________
std::string Visitor::visit(Parser::DataBlockValueContext* ctx) {
TripleComponent Visitor::visit(Parser::DataBlockValueContext* ctx) {
// Return a string
if (ctx->iri()) {
return visit(ctx->iri());
} else if (ctx->rdfLiteral()) {
// TODO<joka921> This is still wrong for XSD literals
return visit(ctx->rdfLiteral());
return TurtleStringParser<TokenizerCtre>::parseTripleObject(
visit(ctx->rdfLiteral()));
} else if (ctx->numericLiteral()) {
return std::visit([](auto intOrDouble) {
return TripleComponent{intOrDouble}; },
visitTypesafe(ctx->numericLiteral()));
// TODO implement
reportError(ctx, "Numbers in values clauses are not supported.");
// reportError(ctx, "Numbers in values clauses are not supported.");
} else if (ctx->booleanLiteral()) {
// TODO implement
reportError(ctx, "Booleans in values clauses are not supported.");
Expand Down
6 changes: 4 additions & 2 deletions src/parser/sparqlParser/SparqlQleverVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ class SparqlQleverVisitor {
[[nodiscard]] parsedQuery::SparqlValues visit(
Parser::InlineDataFullContext* ctx);

[[nodiscard]] vector<std::string> visit(Parser::DataBlockSingleContext* ctx);
[[nodiscard]] vector<TripleComponent> visit(
Parser::DataBlockSingleContext* ctx);

[[nodiscard]] std::string visit(Parser::DataBlockValueContext* ctx);
[[nodiscard]] TripleComponent visitTypesafe(
Parser::DataBlockValueContext* ctx);

[[nodiscard]] GraphPatternOperation visit(
Parser::MinusGraphPatternContext* ctx);
Expand Down
2 changes: 1 addition & 1 deletion test/SparqlAntlrParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ TEST(SparqlParser, DataBlock) {
auto expectDataBlockFails = ExpectParseFails<&Parser::dataBlock>();
expectDataBlock(
"?test { \"foo\" }",
m::Values(vector<string>{"?test"}, vector<vector<string>>{{"\"foo\""}}));
m::Values(vector<string>{"?test"}, vector<vector<TripleComponent>>{{"\"foo\""}}));
// These are not implemented yet in dataBlockValue
// (numericLiteral/booleanLiteral)
// TODO<joka921/qup42> implement
Expand Down
9 changes: 5 additions & 4 deletions test/SparqlAntlrParserTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "engine/sparqlExpressions/SparqlExpressionPimpl.h"
#include "parser/Alias.h"
#include "parser/ParsedQuery.h"
#include "parser/TripleComponent.h"
#include "parser/SparqlParserHelpers.h"
#include "parser/data/OrderKey.h"
#include "parser/data/VarOrTerm.h"
Expand Down Expand Up @@ -423,9 +424,9 @@ auto GroupByVariables =
testing::UnorderedElementsAreArray(vars));
};

auto Values =
[](const vector<string>& vars,
const vector<vector<string>>& values) -> Matcher<const p::Values&> {
auto Values = [](const vector<string>& vars,
const vector<vector<TripleComponent>>& values)
-> Matcher<const p::Values&> {
// TODO Refactor GraphPatternOperation::Values / SparqlValues s.t. this
// becomes a trivial Eq matcher.
using SparqlValues = p::SparqlValues;
Expand All @@ -436,7 +437,7 @@ auto Values =
};

auto InlineData = [](const vector<string>& vars,
const vector<vector<string>>& values)
const vector<vector<TripleComponent>>& values)
-> Matcher<const p::GraphPatternOperation&> {
// TODO Refactor GraphPatternOperation::Values / SparqlValues s.t. this
// becomes a trivial Eq matcher.
Expand Down
6 changes: 3 additions & 3 deletions test/SparqlParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ TEST(ParserTest, testParse) {

vector<string> vvars = {"?a"};
ASSERT_EQ(vvars, values1._variables);
vector<vector<string>> vvals = {{"<1>"}, {"\"2\""}};
vector<vector<TripleComponent>> vvals = {{"<1>"}, {"\"2\""}};
ASSERT_EQ(vvals, values1._values);

vvars = {"?b", "?c"};
Expand All @@ -273,7 +273,7 @@ TEST(ParserTest, testParse) {

vector<string> vvars = {"?a"};
ASSERT_EQ(vvars, values1._variables);
vector<vector<string>> vvals = {{"<Albert_Einstein>"}};
vector<vector<TripleComponent>> vvals = {{"<Albert_Einstein>"}};
ASSERT_EQ(vvals, values1._values);

vvars = {"?b", "?c"};
Expand Down Expand Up @@ -306,7 +306,7 @@ TEST(ParserTest, testParse) {
const auto& values1 = std::get<p::Values>(pq.children()[0])._inlineValues;
vector<string> vvars = {"?citytype"};
ASSERT_EQ(vvars, values1._variables);
vector<vector<string>> vvals = {
vector<vector<TripleComponent>> vvals = {
{"<http://www.wikidata.org/entity/Q515>"},
{"<http://www.wikidata.org/entity/Q262166>"}};
ASSERT_EQ(vvals, values1._values);
Expand Down

0 comments on commit ade9752

Please sign in to comment.