Skip to content

Commit

Permalink
A lot of cases work now.
Browse files Browse the repository at this point in the history
  • Loading branch information
joka921 committed Jul 5, 2024
1 parent 79d90b9 commit 9c73759
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 26 deletions.
29 changes: 15 additions & 14 deletions src/engine/sparqlExpressions/RelationalExpressionHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,22 @@ inline std::pair<ValueId, ValueId> getRangeFromVocab(
// consecutive range of IDs. For its usage see below.
template <typename S>
concept StoresStringOrId =
ad_utility::SimilarToAny<S, ValueId,
ad_utility::triple_component::LiteralOrIri,
IdOrLiteralOrIri, std::pair<Id, Id>>;
ad_utility::SimilarToAny<S, ValueId, LocalVocabEntry, IdOrLiteralOrIri,
std::pair<Id, Id>>;
// Convert a string or `IdOrLiteralOrIri` value into the (possibly empty) range
// of corresponding `ValueIds` (denoted by a `std::pair<Id, Id>`, see
// `getRangeFromVocab` above for details). This function also takes `ValueId`s
// and `pair<ValuedId, ValueId>` which are simply returned unchanged. This makes
// the usage of this function easier.
template <StoresStringOrId S>
auto makeValueId(const S& value, const EvaluationContext* context) {
if constexpr (ad_utility::isSimilar<S, ValueId>) {
return value;
} else if constexpr (ad_utility::isSimilar<S, std::pair<Id, Id>>) {
if constexpr (ad_utility::SimilarToAny<S, ValueId, std::pair<Id, Id>>) {
return value;
} else if constexpr (ad_utility::isSimilar<S, IdOrLiteralOrIri>) {
auto visitor = [context](const auto& x) {
auto res = makeValueId(x, context);
return res;
/*
if constexpr (ad_utility::isSimilar<decltype(res), Id>) {
// We need the same return type on all cases when visiting a variant, so
// we need to return a pair here. As the second element is an upper
Expand All @@ -155,13 +154,16 @@ auto makeValueId(const S& value, const EvaluationContext* context) {
} else {
return res;
}
*/
};
return std::visit(visitor, value);

} else {
static_assert(
ad_utility::isSimilar<S, ad_utility::triple_component::LiteralOrIri>);
return getRangeFromVocab(value, context);
static_assert(ad_utility::isSimilar<S, LocalVocabEntry>);
// TODO<joka921> We have to reinstate the correct handling of the equal
// ranges.
return Id::makeFromLocalVocabIndex(&value);
// return getRangeFromVocab(value, context);
}
};

Expand All @@ -176,13 +178,12 @@ inline const auto compareIdsOrStrings =
[]<StoresStringOrId T, StoresStringOrId U>(
const T& a, const U& b,
const EvaluationContext* ctx) -> valueIdComparators::ComparisonResult {
using LiteralOrIri = ad_utility::triple_component::LiteralOrIri;
if constexpr (ad_utility::isSimilar<LiteralOrIri, T> &&
ad_utility::isSimilar<LiteralOrIri, U>) {
if constexpr (ad_utility::isSimilar<LocalVocabEntry, T> &&
ad_utility::isSimilar<LocalVocabEntry, U>) {
// TODO<joka921> integrate comparison via ICU and proper handling for
// IRIs/ Literals/etc.
return valueIdComparators::fromBool(applyComparison<Comp>(
a.toStringRepresentation(), b.toStringRepresentation()));
// TODO<joka921> This is now the place to fix it....
return valueIdComparators::fromBool(applyComparison<Comp>(a, b));
} else {
auto x = makeValueId(a, ctx);
auto y = makeValueId(b, ctx);
Expand Down
6 changes: 2 additions & 4 deletions src/engine/sparqlExpressions/SparqlExpressionTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ static_assert(!std::default_initializable<VectorWithMemoryLimit<int>>);
// A class to store the results of expressions that can yield strings or IDs as
// their result (for example IF and COALESCE). It is also used for expressions
// that can only yield strings.
using IdOrLiteralOrIri =
std::variant<ValueId, ad_utility::triple_component::LiteralOrIri>;
using IdOrLiteralOrIri = std::variant<ValueId, LocalVocabEntry>;
// Printing for GTest.
void PrintTo(const IdOrLiteralOrIri& var, std::ostream* os);

Expand Down Expand Up @@ -223,8 +222,7 @@ Id constantExpressionResultToId(T&& result, LocalVocabT& localVocab) {
} else if constexpr (ad_utility::isSimilar<T, IdOrLiteralOrIri>) {
return std::visit(
[&localVocab]<typename R>(R&& el) mutable {
if constexpr (ad_utility::isSimilar<
R, ad_utility::triple_component::LiteralOrIri>) {
if constexpr (ad_utility::isSimilar<R, LocalVocabEntry>) {
return Id::makeFromLocalVocabIndex(
localVocab.getIndexAndAddIfNotContained(AD_FWD(el)));
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/global/ValueIdComparators.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,8 @@ ComparisonResult compareIdsImpl(ValueId a, ValueId b, auto comparator) {
const A& aValue, const B& bValue) -> ComparisonResult {
if constexpr (std::is_same_v<A, LocalVocabIndex> &&
std::is_same_v<B, LocalVocabIndex>) {
// TODO<joka921> This is one of the places that has to be changed once
// we want to implement correct comparisons for the local vocab that use
// ICU collation.
return fromBool(std::invoke(comparator, *aValue, *bValue));
// We have handled this case outside the visitor.
AD_FAIL();
} else if constexpr (requires() {
std::invoke(comparator, aValue, bValue);
}) {
Expand Down
1 change: 1 addition & 0 deletions src/index/IndexImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ void IndexImpl::createFromOnDiskIndex(const string& onDiskBase) {
readConfiguration();
vocab_.readFromFile(onDiskBase_ + INTERNAL_VOCAB_SUFFIX,
onDiskBase_ + EXTERNAL_VOCAB_SUFFIX);
globalSingletonComparator_ = &vocab_.getCaseComparator();

totalVocabularySize_ = vocab_.size() + vocab_.getExternalVocab().size();
LOG(DEBUG) << "Number of words in internal and external vocabulary: "
Expand Down
7 changes: 7 additions & 0 deletions src/index/IndexImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class IndexImpl {
NumNormalAndInternal numTriples_;
string indexId_;
static inline const IndexImpl* globalSingletonIndex_ = nullptr;
static inline const TripleComponentComparator* globalSingletonComparator_ =
nullptr;
/**
* @brief Maps pattern ids to sets of predicate ids.
*/
Expand Down Expand Up @@ -194,6 +196,11 @@ class IndexImpl {
return *globalSingletonIndex_;
}

static const TripleComponentComparator& staticGlobalSingletonComparator() {
AD_CORRECTNESS_CHECK(globalSingletonComparator_ != nullptr);
return *globalSingletonComparator_;
}

// For a given `Permutation::Enum` (e.g. `PSO`) return the corresponding
// `Permutation` object by reference (`pso_`).
Permutation& getPermutation(Permutation::Enum p);
Expand Down
15 changes: 15 additions & 0 deletions src/parser/LiteralOrIri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <algorithm>
#include <utility>

#include "index/IndexImpl.h"

namespace ad_utility::triple_component {
// __________________________________________
LiteralOrIri::LiteralOrIri(Iri iri) : data_{std::move(iri)} {}
Expand Down Expand Up @@ -106,4 +108,17 @@ LiteralOrIri LiteralOrIri::literalWithoutQuotes(
return LiteralOrIri(Literal::literalWithoutQuotes(rdfContentWithoutQuotes,
std::move(descriptor)));
}

// ___________________________________________
std::strong_ordering LiteralOrIri::operator<=>(const LiteralOrIri& rhs) const {
int i = IndexImpl::staticGlobalSingletonComparator().compare(
toStringRepresentation(), rhs.toStringRepresentation());
if (i < 0) {
return std::strong_ordering::less;
} else if (i > 0) {
return std::strong_ordering::greater;
} else {
return std::strong_ordering::equal;
}
}
} // namespace ad_utility::triple_component
5 changes: 1 addition & 4 deletions src/parser/LiteralOrIri.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ class alignas(16) LiteralOrIri {
}
bool operator==(const LiteralOrIri&) const = default;

auto operator<=>(const LiteralOrIri& rhs) const {
// TODO<joka921> Use something unicode-based for this.
return toStringRepresentation() <=> rhs.toStringRepresentation();
}
std::strong_ordering operator<=>(const LiteralOrIri& rhs) const;

// Return true if object contains an Iri object
bool isIri() const;
Expand Down

0 comments on commit 9c73759

Please sign in to comment.