-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ullingerc
committed
Oct 20, 2024
1 parent
f90d91c
commit 91d1c15
Showing
5 changed files
with
68 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,7 @@ | |
// Chair of Algorithms and Data Structures. | ||
// Author: Christoph Ullinger <[email protected]> | ||
|
||
#ifndef QLEVER_STDEVEXPRESSION_H | ||
#define QLEVER_STDEVEXPRESSION_H | ||
#pragma once | ||
|
||
#include <cmath> | ||
#include <functional> | ||
|
@@ -40,11 +39,9 @@ auto inline numValToDouble = | |
class DeviationExpression : public SparqlExpression { | ||
private: | ||
Ptr child_; | ||
bool distinct_; | ||
|
||
public: | ||
DeviationExpression(bool distinct, Ptr&& child) | ||
: child_{std::move(child)}, distinct_{distinct} {} | ||
DeviationExpression(Ptr&& child) : child_{std::move(child)} {} | ||
|
||
// __________________________________________________________________________ | ||
ExpressionResult evaluate(EvaluationContext* context) const override { | ||
|
@@ -88,13 +85,7 @@ class DeviationExpression : public SparqlExpression { | |
|
||
auto generator = | ||
detail::makeGenerator(AD_FWD(el), context->size(), context); | ||
if (distinct_) { | ||
context->cancellationHandle_->throwIfCancelled(); | ||
devImpl(detail::getUniqueElements(context, context->size(), | ||
std::move(generator))); | ||
} else { | ||
devImpl(std::move(generator)); | ||
} | ||
devImpl(std::move(generator)); | ||
|
||
if (undef) { | ||
return IdOrLiteralOrIri{Id::makeUndefined()}; | ||
|
@@ -113,8 +104,7 @@ class DeviationExpression : public SparqlExpression { | |
// __________________________________________________________________________ | ||
[[nodiscard]] string getCacheKey( | ||
const VariableToColumnMap& varColMap) const override { | ||
return absl::StrCat("[ SQ.DEVIATION ", distinct_ ? " DISTINCT " : "", "]", | ||
child_->getCacheKey(varColMap)); | ||
return absl::StrCat("[ SQ.DEVIATION ]", child_->getCacheKey(varColMap)); | ||
} | ||
|
||
private: | ||
|
@@ -135,8 +125,7 @@ class DeviationAggExpression | |
DeviationAggExpression(bool distinct, SparqlExpression::Ptr&& child, | ||
AggregateOperation aggregateOp = AggregateOperation{}) | ||
: AggregateExpression<AggregateOperation, FinalOperation>( | ||
distinct, | ||
std::make_unique<DeviationExpression>(distinct, std::move(child)), | ||
distinct, std::make_unique<DeviationExpression>(std::move(child)), | ||
aggregateOp){}; | ||
}; | ||
|
||
|
@@ -162,13 +151,11 @@ using StdevExpressionBase = | |
DeviationAggExpression<AvgOperation, decltype(stdevFinalOperation)>; | ||
class StdevExpression : public StdevExpressionBase { | ||
using StdevExpressionBase::StdevExpressionBase; | ||
ValueId resultForEmptyGroup() const override { return Id::makeFromInt(0); } | ||
ValueId resultForEmptyGroup() const override { return Id::makeFromDouble(0); } | ||
}; | ||
|
||
} // namespace detail | ||
|
||
using detail::StdevExpression; | ||
|
||
} // namespace sparqlExpression | ||
|
||
#endif // QLEVER_STDEVEXPRESSION_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
// Chair of Algorithms and Data Structures | ||
// Author: Johannes Kalmbach <[email protected]> | ||
|
||
#include <optional> | ||
#include <type_traits> | ||
|
||
#include "./SparqlExpressionTestHelpers.h" | ||
#include "./util/GTestHelpers.h" | ||
#include "./util/IdTableHelpers.h" | ||
|
@@ -11,6 +14,9 @@ | |
#include "engine/sparqlExpressions/AggregateExpression.h" | ||
#include "engine/sparqlExpressions/CountStarExpression.h" | ||
#include "engine/sparqlExpressions/SampleExpression.h" | ||
#include "engine/sparqlExpressions/SparqlExpressionTypes.h" | ||
#include "engine/sparqlExpressions/StdevExpression.h" | ||
#include "global/ValueId.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace sparqlExpression; | ||
|
@@ -94,6 +100,33 @@ TEST(AggregateExpression, avg) { | |
testAvgString({lit("alpha"), lit("äpfel"), lit("Beta"), lit("unfug")}, U); | ||
} | ||
|
||
// Test `StdevExpression`. | ||
TEST(StdevExpression, avg) { | ||
auto testStdevId = testAggregate<StdevExpression, Id>; | ||
|
||
auto inputAsVector = std::vector{I(3), D(0), I(0), I(4), I(-2)}; | ||
VectorWithMemoryLimit<ValueId> input(inputAsVector.begin(), | ||
inputAsVector.end(), makeAllocator()); | ||
auto d = std::make_unique<SingleUseExpression>(input.clone()); | ||
auto t = TestContext{}; | ||
t.context._endIndex = 5; | ||
StdevExpression m{false, std::move(d)}; | ||
auto resAsVariant = m.evaluate(&t.context); | ||
ASSERT_NEAR(std::get<ValueId>(resAsVariant).getDouble(), 2.44949, 0.0001); | ||
|
||
testStdevId({D(2), D(2), D(2), D(2)}, D(0), true); | ||
|
||
testStdevId({I(3), U}, U); | ||
testStdevId({I(3), NaN}, NaN); | ||
|
||
testStdevId({}, D(0)); | ||
testStdevId({D(500)}, D(0)); | ||
testStdevId({D(500), D(500), D(500)}, D(0)); | ||
|
||
auto testStdevString = testAggregate<StdevExpression, IdOrLiteralOrIri, Id>; | ||
testStdevString({lit("alpha"), lit("äpfel"), lit("Beta"), lit("unfug")}, U); | ||
} | ||
|
||
// Test `MinExpression`. | ||
TEST(AggregateExpression, min) { | ||
auto testMinId = testAggregate<MinExpression, Id>; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters