Skip to content

Commit

Permalink
Add test for permutation and fix permutation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinTF committed Oct 15, 2024
1 parent cade5b9 commit 2417653
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/engine/Union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ std::vector<size_t> Union::computePermutation() const {
originIndex = startOfUndefColumns;
startOfUndefColumns++;
}
permutation[originIndex] = targetColIdx;
permutation[targetColIdx] = originIndex;
}
return permutation;
}
Expand Down
59 changes: 53 additions & 6 deletions test/UnionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Vars = std::vector<std::optional<Variable>>;
} // namespace

// A simple test for computing a union.
TEST(UnionTest, computeUnion) {
TEST(Union, computeUnion) {
auto* qec = ad_utility::testing::getQec();
IdTable left = makeIdTableFromVector({{V(1)}, {V(2)}, {V(3)}});
auto leftT = ad_utility::makeExecutionTree<ValuesForTesting>(
Expand All @@ -30,7 +30,7 @@ TEST(UnionTest, computeUnion) {
auto rightT = ad_utility::makeExecutionTree<ValuesForTesting>(
qec, right.clone(), Vars{Variable{"?u"}, Variable{"?x"}});

Union u{ad_utility::testing::getQec(), leftT, rightT};
Union u{qec, leftT, rightT};
auto resultTable = u.computeResultOnlyForTesting();
const auto& result = resultTable.idTable();

Expand All @@ -42,7 +42,7 @@ TEST(UnionTest, computeUnion) {

// A test with large inputs to test the chunked writing that is caused by the
// timeout checks.
TEST(UnionTest, computeUnionLarge) {
TEST(Union, computeUnionLarge) {
auto* qec = ad_utility::testing::getQec();
VectorTable leftInput, rightInput, expected;
size_t numInputsL = 1'500'000u;
Expand All @@ -65,15 +65,15 @@ TEST(UnionTest, computeUnionLarge) {
auto rightT = ad_utility::makeExecutionTree<ValuesForTesting>(
qec, makeIdTableFromVector(rightInput), Vars{Variable{"?u"}});

Union u{ad_utility::testing::getQec(), leftT, rightT};
Union u{qec, leftT, rightT};
auto resultTable = u.computeResultOnlyForTesting();
const auto& result = resultTable.idTable();

ASSERT_EQ(result, makeIdTableFromVector(expected));
}

// _____________________________________________________________________________
TEST(UnionTest, computeUnionLazy) {
TEST(Union, computeUnionLazy) {
auto runTest = [](bool nonLazyChilds,
ad_utility::source_location loc =
ad_utility::source_location::current()) {
Expand All @@ -90,7 +90,7 @@ TEST(UnionTest, computeUnionLazy) {
qec, std::move(right), Vars{Variable{"?u"}, Variable{"?x"}}, false,
std::vector<ColumnIndex>{}, LocalVocab{}, std::nullopt, nonLazyChilds);

Union u{ad_utility::testing::getQec(), std::move(leftT), std::move(rightT)};
Union u{qec, std::move(leftT), std::move(rightT)};
auto resultTable = u.computeResultOnlyForTesting(true);
ASSERT_FALSE(resultTable.isFullyMaterialized());
auto& result = resultTable.idTables();
Expand All @@ -113,3 +113,50 @@ TEST(UnionTest, computeUnionLazy) {
runTest(false);
runTest(true);
}

// _____________________________________________________________________________
TEST(Union, ensurePermutationIsAppliedCorrectly) {
using Var = Variable;
auto* qec = ad_utility::testing::getQec();
auto leftT = ad_utility::makeExecutionTree<ValuesForTesting>(
qec, makeIdTableFromVector({{1, 2, 3, 4, 5}}),
Vars{Var{"?a"}, Var{"?b"}, Var{"?c"}, Var{"?d"}, Var{"?e"}});

auto rightT = ad_utility::makeExecutionTree<ValuesForTesting>(
qec, makeIdTableFromVector({{6, 7, 8}}),
Vars{Var{"?b"}, Var{"?a"}, Var{"?e"}});

Union u{qec, std::move(leftT), std::move(rightT)};

{
qec->getQueryTreeCache().clearAll();
auto resultTable = u.computeResultOnlyForTesting(true);
ASSERT_FALSE(resultTable.isFullyMaterialized());
auto& result = resultTable.idTables();

auto U = Id::makeUndefined();
auto expected1 = makeIdTableFromVector({{1, 2, 3, 4, 5}});
auto expected2 = makeIdTableFromVector({{V(7), V(6), U, U, V(8)}});

auto iterator = result.begin();
ASSERT_NE(iterator, result.end());
ASSERT_EQ(*iterator, expected1);

++iterator;
ASSERT_NE(iterator, result.end());
ASSERT_EQ(*iterator, expected2);

ASSERT_EQ(++iterator, result.end());
}

{
qec->getQueryTreeCache().clearAll();
auto resultTable = u.computeResultOnlyForTesting();
ASSERT_TRUE(resultTable.isFullyMaterialized());

auto U = Id::makeUndefined();
auto expected =
makeIdTableFromVector({{1, 2, 3, 4, 5}, {V(7), V(6), U, U, V(8)}});
EXPECT_EQ(resultTable.idTable(), expected);
}
}

0 comments on commit 2417653

Please sign in to comment.