Skip to content

Commit

Permalink
Some improvements.
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Kalmbach <[email protected]>
  • Loading branch information
joka921 committed Dec 20, 2024
1 parent 39ca3cb commit 40511b3
Showing 1 changed file with 46 additions and 49 deletions.
95 changes: 46 additions & 49 deletions src/engine/sparqlExpressions/StringExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,49 @@ class StringExpressionImpl : public SparqlExpression {
}
};

// Same as the `StringExpressionImpl` above, but with the LiteralOrValueGetter.
template <size_t N, typename Function,
typename... AdditionalNonStringValueGetters>
class LiteralExpressionImpl : public SparqlExpression {
private:
using ExpressionWithStr = NARY<N, FV<Function, LiteralOrIriValueGetter,
AdditionalNonStringValueGetters...>>;
using ExpressionWithoutStr =
NARY<N, FV<Function, LiteralOrIriValueGetterWithXsdStringFilter,
AdditionalNonStringValueGetters...>>;

SparqlExpression::Ptr impl_;

public:
explicit LiteralExpressionImpl(
SparqlExpression::Ptr child,
std::same_as<SparqlExpression::Ptr> auto... children)
requires(sizeof...(children) + 1 == N) {
AD_CORRECTNESS_CHECK(child != nullptr);
if (child->isStrExpression()) {
auto childrenOfStr = std::move(*child).moveChildrenOut();
AD_CORRECTNESS_CHECK(childrenOfStr.size() == 1);
impl_ = std::make_unique<ExpressionWithStr>(
std::move(childrenOfStr.at(0)), std::move(children)...);

Check warning on line 135 in src/engine/sparqlExpressions/StringExpressions.cpp

View check run for this annotation

Codecov / codecov/patch

src/engine/sparqlExpressions/StringExpressions.cpp#L132-L135

Added lines #L132 - L135 were not covered by tests
} else {
impl_ = std::make_unique<ExpressionWithoutStr>(std::move(child),
std::move(children)...);
}
}

ExpressionResult evaluate(EvaluationContext* context) const override {
return impl_->evaluate(context);
}
std::string getCacheKey(const VariableToColumnMap& varColMap) const override {
return impl_->getCacheKey(varColMap);
}

Check warning on line 147 in src/engine/sparqlExpressions/StringExpressions.cpp

View check run for this annotation

Codecov / codecov/patch

src/engine/sparqlExpressions/StringExpressions.cpp#L145-L147

Added lines #L145 - L147 were not covered by tests

private:
std::span<SparqlExpression::Ptr> childrenImpl() override {
return impl_->children();
}
};

// Lift a `Function` that takes one or multiple `std::string`s (possibly via
// references) and returns an `Id` or `std::string` to a function that takes the
// same number of `std::optional<std::string>` and returns `Id` or
Expand Down Expand Up @@ -248,55 +291,9 @@ class SubstrImpl {
}
};

// Implementation of the `SUBSTR` SPARQL function. It dynamically
// selects the appropriate value getter for the first argument based on whether
// it is a `STR()` expression (using
// `LiteralOrIriValueGetterWithXsdStringFilter`) or another type (using
// `LiteralOrIriValueGetter`).
class SubstrExpressionImpl : public SparqlExpression {
private:
using ExpressionWithStr =
NARY<3, FV<SubstrImpl, LiteralOrIriValueGetterWithXsdStringFilter,
NumericValueGetter, NumericValueGetter>>;
using ExpressionWithoutStr =
NARY<3, FV<SubstrImpl, LiteralOrIriValueGetter, NumericValueGetter,
NumericValueGetter>>;

SparqlExpression::Ptr impl_;

public:
explicit SubstrExpressionImpl(
SparqlExpression::Ptr child,
std::same_as<SparqlExpression::Ptr> auto... children)
requires(sizeof...(children) + 1 == 3) {
AD_CORRECTNESS_CHECK(child != nullptr);

if (child->isStrExpression()) {
auto childrenOfStr = std::move(*child).moveChildrenOut();
AD_CORRECTNESS_CHECK(childrenOfStr.size() == 1);
impl_ = std::make_unique<ExpressionWithStr>(
std::move(childrenOfStr.at(0)), std::move(children)...);
} else {
impl_ = std::make_unique<ExpressionWithoutStr>(std::move(child),
std::move(children)...);
}
}

ExpressionResult evaluate(EvaluationContext* context) const override {
return impl_->evaluate(context);
}

std::string getCacheKey(const VariableToColumnMap& varColMap) const override {
return impl_->getCacheKey(varColMap);
}

private:
std::span<SparqlExpression::Ptr> childrenImpl() override {
return impl_->children();
}
};

using SubstrExpression = SubstrExpressionImpl;
using SubstrExpression =
LiteralExpressionImpl<3, SubstrImpl, NumericValueGetter,
NumericValueGetter>;

// STRSTARTS
[[maybe_unused]] auto strStartsImpl = [](std::string_view text,
Expand Down

0 comments on commit 40511b3

Please sign in to comment.