Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement functions UUID() and STRUUID() #1366

Merged
merged 35 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
99da434
Added conversion str to int
realHannes Apr 25, 2024
424d023
templated function for toNumeric, add declaration to NaryExpression.h
realHannes Apr 25, 2024
0117e82
str to num for SparqlExpression implemented + added test
realHannes Apr 25, 2024
35fd0b1
Merge branch 'ad-freiburg:master' into master
realHannes Apr 26, 2024
94356c2
Update src/engine/sparqlExpressions/StringExpressions.cpp
realHannes Apr 26, 2024
decc8ba
Update src/engine/sparqlExpressions/StringExpressions.cpp
realHannes Apr 26, 2024
850152c
Update src/engine/sparqlExpressions/StringExpressions.cpp
realHannes Apr 26, 2024
d650d67
Update src/engine/sparqlExpressions/StringExpressions.cpp
realHannes Apr 26, 2024
46cc697
using now absl::from_chars() and stripping whitespaces for string to …
realHannes Apr 26, 2024
7fc5c28
added new functions to processIriFuntionCall() (for string to number)
realHannes Apr 26, 2024
efb0e24
renaming to: toIntExpression and toDoubleExpression for later more ge…
realHannes Apr 26, 2024
a88537c
made format (clang-format-16)
realHannes Apr 26, 2024
ca1e2e0
Update src/parser/sparqlParser/SparqlQleverVisitor.cpp
realHannes Apr 29, 2024
4adc831
Update src/parser/sparqlParser/SparqlQleverVisitor.cpp
realHannes Apr 29, 2024
d0f0d63
renaming in NaryExpression.h for accordance with other function, addi…
realHannes Apr 29, 2024
a118609
added test coverage for function calls makeIntExpression and make Dou…
realHannes Apr 29, 2024
062052e
toNumeric has now correct behavior and uses absl::from_chars() and st…
realHannes Apr 29, 2024
6d0f42a
made clang-format for NaryExpressionImpl.h
realHannes Apr 29, 2024
f90b8e2
Merge branch 'ad-freiburg:master' into master
realHannes May 6, 2024
fb88493
Merge branch 'ad-freiburg:master' into master
realHannes May 15, 2024
b2eb514
Merge remote-tracking branch 'upstream/master'
realHannes May 16, 2024
b165ac1
Merge branch 'ad-freiburg:master' into master
realHannes Jun 1, 2024
7a3dfb2
Merge branch 'master' of https://github.com/realHannes/qlever
realHannes Jun 1, 2024
fc0ad3a
Merge branch 'ad-freiburg:master' into master
realHannes Jun 6, 2024
f3e6086
Merge branch 'ad-freiburg:master' into master
realHannes Jun 7, 2024
4dcffc0
started implementation for Uuid
realHannes Jun 8, 2024
1f02cae
implemented struuid() and uuid()
realHannes Jun 8, 2024
3c27e9d
moved uuid expressions to separate file + fix in random
realHannes Jun 9, 2024
d25d402
added test coverage + changes for sonarcloud
realHannes Jun 9, 2024
c40f846
Merge branch 'ad-freiburg:master' into uuid_and_str_uuid
realHannes Jun 12, 2024
635b27e
implemented suggested changes #1366
realHannes Jun 12, 2024
d749b7d
made format
realHannes Jun 12, 2024
c5535e5
use namespace detail
realHannes Jun 12, 2024
15a929f
Merge branch 'ad-freiburg:master' into uuid_and_str_uuid
realHannes Jun 13, 2024
35b061b
correct usage of namespace #1366
realHannes Jun 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/engine/sparqlExpressions/UuidExpressions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024, University of Freiburg,
// Chair of Algorithms and Data Structures
// Author: Hannes Baumann <[email protected]>

// Test for UuidExpressionImpl can be found in RandomExpressionTest.cpp

#pragma once

#include "engine/sparqlExpressions/SparqlExpression.h"
#include "util/ChunkedForLoop.h"
#include "util/Random.h"

namespace sparqlExpression {
namespace detail::uuidExpression {

using LiteralOrIri = ad_utility::triple_component::LiteralOrIri;

inline constexpr auto fromLiteral = [](std::string_view str) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. inline is redundant for constexpr values, so it can be dropped.
  2. Those inline functions are a bit prone to multiple definition errors etc, better move them into a namespace sparqlExpression::uuidExpression::detail or something.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow the tests for builtInCall don't pass if i remove the inline.

return LiteralOrIri{
ad_utility::triple_component::Literal::literalWithNormalizedContent(
asNormalizedStringViewUnsafe(str))};
};

inline constexpr auto fromIri = [](std::string_view str) {
return LiteralOrIri{
ad_utility::triple_component::Iri::fromStringRepresentation(
absl::StrCat("<urn:uuid:"sv, str, ">"sv))};
};

inline constexpr auto litUuidKey = [](int64_t randId) {
return absl::StrCat("STRUUID "sv, randId);
};

inline constexpr auto iriUuidKey = [](int64_t randId) {
return absl::StrCat("UUID "sv, randId);
};

// With UuidExpressionImpl<fromIri, iriUuidKey>, the UUIDs are returned as an
// Iri object: <urn:uuid:b9302fb5-642e-4d3b-af19-29a8f6d894c9> (example). With
// UuidExpressionImpl<fromLiteral,, litUuidKey>, the UUIDs are returned as an
// Literal object: "73cd4307-8a99-4691-a608-b5bda64fb6c1" (example).
template <auto FuncConv, auto FuncKey>
class UuidExpressionImpl : public SparqlExpression {
private:
int64_t randId_ = ad_utility::FastRandomIntGenerator<int64_t>{}();

public:
ExpressionResult evaluate(EvaluationContext* context) const override {
VectorWithMemoryLimit<IdOrLiteralOrIri> result{context->_allocator};
const size_t numElements = context->_endIndex - context->_beginIndex;
result.reserve(numElements);
ad_utility::UuidGenerator uuidGen;

if (context->_isPartOfGroupBy) {
return FuncConv(uuidGen());
}

ad_utility::chunkedForLoop<1000>(
0, numElements,
[&result, &uuidGen](size_t) { result.push_back(FuncConv(uuidGen())); },
[context]() { context->cancellationHandle_->throwIfCancelled(); });
return result;
}

string getCacheKey(
[[maybe_unused]] const VariableToColumnMap& varColMap) const override {
return FuncKey(randId_);
}

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

} // namespace detail::uuidExpression

using namespace detail::uuidExpression;
realHannes marked this conversation as resolved.
Show resolved Hide resolved
using UuidExpression = UuidExpressionImpl<fromIri, iriUuidKey>;
using StrUuidExpression = UuidExpressionImpl<fromLiteral, litUuidKey>;

} // namespace sparqlExpression
7 changes: 7 additions & 0 deletions src/parser/sparqlParser/SparqlQleverVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "engine/sparqlExpressions/RandomExpression.h"
#include "engine/sparqlExpressions/RegexExpression.h"
#include "engine/sparqlExpressions/RelationalExpressions.h"
#include "engine/sparqlExpressions/UuidExpressions.h"
#include "parser/SparqlParser.h"
#include "parser/TokenizerCtre.h"
#include "parser/TurtleParser.h"
Expand Down Expand Up @@ -2015,6 +2016,12 @@ ExpressionPtr Visitor::visit([[maybe_unused]] Parser::BuiltInCallContext* ctx) {
} else if (functionName == "rand") {
AD_CONTRACT_CHECK(argList.empty());
return std::make_unique<RandomExpression>();
} else if (functionName == "uuid") {
AD_CONTRACT_CHECK(argList.empty());
return std::make_unique<UuidExpression>();
} else if (functionName == "struuid") {
AD_CONTRACT_CHECK(argList.empty());
return std::make_unique<StrUuidExpression>();
} else if (functionName == "ceil") {
return createUnary(&makeCeilExpression);
} else if (functionName == "abs") {
Expand Down
16 changes: 16 additions & 0 deletions src/util/Random.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <algorithm>
#include <array>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <cstring>
#include <random>
#include <type_traits>
Expand Down Expand Up @@ -104,6 +106,20 @@ class RandomDoubleGenerator {
std::uniform_real_distribution<double> _distribution;
};

// GENERATE UUID
class UuidGenerator {
public:
explicit UuidGenerator(
RandomSeed seed = RandomSeed::make(std::random_device{}()))
: randomEngine_{seed.get()}, gen_(randomEngine_) {}

std::string operator()() { return boost::uuids::to_string(gen_()); }

private:
std::mt19937_64 randomEngine_;
boost::uuids::basic_random_generator<std::mt19937_64> gen_;
};

/// Randomly shuffle range denoted by `[begin, end)`
template <typename RandomIt>
void randomShuffle(RandomIt begin, RandomIt end,
Expand Down
89 changes: 89 additions & 0 deletions test/RandomExpressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Chair of Algorithms and Data Structures.
// Author: Johannes Kalmbach <[email protected]>

#include <unordered_set>

#include "./SparqlExpressionTestHelpers.h"
#include "engine/sparqlExpressions/RandomExpression.h"
#include "engine/sparqlExpressions/UuidExpressions.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

Expand Down Expand Up @@ -58,3 +61,89 @@ TEST(RandomExpression, simpleMemberFunctions) {
// probability of `1 / 2^64` of a spurious failure.
ASSERT_NE(cacheKey, RandomExpression{}.getCacheKey({}));
}

// The tests for UUID expressions follow almost exactly the pattern
// of the above defined test for RandomExpression.
TEST(UuidExpression, simpleMemberFunctionStrUuid) {
StrUuidExpression strUuid;
ASSERT_TRUE(strUuid.getUnaggregatedVariables().empty());
auto cacheKeyStrUuid = strUuid.getCacheKey({});
ASSERT_THAT(cacheKeyStrUuid, ::testing::StartsWith("STRUUID "));
ASSERT_EQ(cacheKeyStrUuid, strUuid.getCacheKey({}));
StrUuidExpression strUuid2;
ASSERT_NE(cacheKeyStrUuid, strUuid2.getCacheKey({}));
}

TEST(UuidExpression, simpleMemberFunctionLitUuid) {
UuidExpression iriUuid;
ASSERT_TRUE(iriUuid.getUnaggregatedVariables().empty());
auto cacheKeyIriUuid = iriUuid.getCacheKey({});
ASSERT_THAT(cacheKeyIriUuid, ::testing::StartsWith("UUID "));
ASSERT_EQ(cacheKeyIriUuid, iriUuid.getCacheKey({}));
UuidExpression iriUuid2;
ASSERT_NE(cacheKeyIriUuid, iriUuid2.getCacheKey({}));
}

TEST(UuidExpression, evaluateStrUuidExpression) {
TestContext testContext{};
auto& evaluationContext = testContext.context;
evaluationContext._beginIndex = 43;
evaluationContext._endIndex = 1044;
auto resultAsVariant = StrUuidExpression{}.evaluate(&evaluationContext);

using V = VectorWithMemoryLimit<IdOrLiteralOrIri>;
ASSERT_TRUE(std::holds_alternative<V>(resultAsVariant));
const auto& resultVector = std::get<V>(resultAsVariant);
ASSERT_EQ(resultVector.size(), 1001);

// check that none of the results equals all previous results
std::unordered_set<std::string> strUuids;
for (auto uuid : resultVector) {
ASSERT_TRUE(std::holds_alternative<LiteralOrIri>(uuid));
LiteralOrIri litUuid = std::get<LiteralOrIri>(uuid);
ASSERT_TRUE(litUuid.isLiteral());
std::string_view strUuid =
asStringViewUnsafe(litUuid.getLiteral().getContent());
ASSERT_EQ(strUuids.find(std::string(strUuid)), strUuids.end());
strUuids.insert(std::string(strUuid));
}

evaluationContext._isPartOfGroupBy = true;
auto resultAsVariant2 = StrUuidExpression{}.evaluate(&evaluationContext);
ASSERT_TRUE(std::holds_alternative<IdOrLiteralOrIri>(resultAsVariant2));
IdOrLiteralOrIri litOrIriUuid = std::get<IdOrLiteralOrIri>(resultAsVariant2);
ASSERT_TRUE(std::holds_alternative<LiteralOrIri>(litOrIriUuid));
ASSERT_TRUE(std::get<LiteralOrIri>(litOrIriUuid).isLiteral());
}

TEST(UuidExpression, evaluateUuidExpression) {
TestContext testContext{};
auto& evaluationContext = testContext.context;
evaluationContext._beginIndex = 43;
evaluationContext._endIndex = 1044;
auto resultAsVariant = UuidExpression{}.evaluate(&evaluationContext);

using V = VectorWithMemoryLimit<IdOrLiteralOrIri>;
ASSERT_TRUE(std::holds_alternative<V>(resultAsVariant));
const auto& resultVector = std::get<V>(resultAsVariant);
ASSERT_EQ(resultVector.size(), 1001);

// check that none of the results equals all of the other results
std::unordered_set<std::string> strUuids;
for (auto uuid : resultVector) {
ASSERT_TRUE(std::holds_alternative<LiteralOrIri>(uuid));
LiteralOrIri litUuid = std::get<LiteralOrIri>(uuid);
ASSERT_TRUE(litUuid.isIri());
std::string_view iriUuid =
asStringViewUnsafe(litUuid.getIri().getContent());
ASSERT_EQ(strUuids.find(std::string(iriUuid)), strUuids.end());
strUuids.insert(std::string(iriUuid));
}

evaluationContext._isPartOfGroupBy = true;
auto resultAsVariant2 = UuidExpression{}.evaluate(&evaluationContext);
ASSERT_TRUE(std::holds_alternative<IdOrLiteralOrIri>(resultAsVariant2));
IdOrLiteralOrIri litOrIriUuid = std::get<IdOrLiteralOrIri>(resultAsVariant2);
ASSERT_TRUE(std::holds_alternative<LiteralOrIri>(litOrIriUuid));
ASSERT_TRUE(std::get<LiteralOrIri>(litOrIriUuid).isIri());
}
26 changes: 26 additions & 0 deletions test/RandomTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <algorithm>
#include <array>
#include <concepts>
#include <ctre.hpp>
#include <random>
#include <ranges>
#include <type_traits>
#include <unordered_set>

#include "../test/util/RandomTestHelpers.h"
#include "util/Exception.h"
Expand Down Expand Up @@ -235,4 +237,28 @@ TEST(RandomShuffleTest, Seed) {
});
}

TEST(UuidGeneratorTest, StrUuidGeneratorTest) {
// Test few times that the returned UUID str is not
// "00000000-0000-0000-0000-000000000000" (nil-UUID)
// and that none of the str-UUIDS is rquivalent to the already
// created ones.
realHannes marked this conversation as resolved.
Show resolved Hide resolved
// Pattern for checking that UUID is properly formatted
static constexpr auto uuidPattern = ctll::fixed_string{
"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{"
"3}-[0-9a-fA-F]{12}$"};
boost::uuids::string_generator getUuid;
UuidGenerator gen = UuidGenerator();
std::unordered_set<std::string> setUuids;
size_t i = 0;
while (i < 100) {
std::string strUuid = gen();
boost::uuids::uuid uuid = getUuid(strUuid.data());
ASSERT_EQ(uuid.is_nil(), false);
ASSERT_EQ(setUuids.find(strUuid), setUuids.end());
ASSERT_TRUE(ctre::match<uuidPattern>(strUuid));
setUuids.insert(strUuid);
i++;
}
}

} // namespace ad_utility
3 changes: 3 additions & 0 deletions test/SparqlAntlrParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "engine/sparqlExpressions/LiteralExpression.h"
#include "engine/sparqlExpressions/RandomExpression.h"
#include "engine/sparqlExpressions/RegexExpression.h"
#include "engine/sparqlExpressions/UuidExpressions.h"
#include "parser/ConstructClause.h"
#include "parser/SparqlParserHelpers.h"
#include "parser/sparqlParser/SparqlQleverVisitor.h"
Expand Down Expand Up @@ -1397,6 +1398,8 @@ TEST(SparqlParser, builtInCall) {
expectBuiltInCall("ISNUMERIC(?x)", matchUnary(&makeIsNumericExpression));
expectBuiltInCall("BOUND(?x)", matchUnary(&makeBoundExpression));
expectBuiltInCall("RAND()", matchPtr<RandomExpression>());
expectBuiltInCall("STRUUID()", matchPtr<StrUuidExpression>());
expectBuiltInCall("UUID()", matchPtr<UuidExpression>());
expectBuiltInCall("COALESCE(?x)", matchUnary(makeCoalesceExpressionVariadic));
expectBuiltInCall("COALESCE()", matchNary(makeCoalesceExpressionVariadic));
expectBuiltInCall("COALESCE(?x, ?y, ?z)",
Expand Down
Loading