Skip to content

Commit

Permalink
cleanup costfn
Browse files Browse the repository at this point in the history
  • Loading branch information
Goblin80 committed Apr 30, 2024
1 parent 29ae853 commit 653cc84
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
60 changes: 21 additions & 39 deletions src/engine/joinOrdering/CostASI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,39 @@
namespace JoinOrdering::ASI {

template <typename N>
requires RelationAble<N> auto rank(QueryGraph<N>& g, N n) -> float {
// TODO: unpack hist here?
std::vector<N> seq{n};

requires RelationAble<N> auto rank(QueryGraph<N>& g, const N& n) -> float {
auto r = (T(g, n) - 1) / C(g, n);
// assert rank [0, 1]
return (T(g, seq) - 1) / C(g, seq);
AD_CONTRACT_CHECK(r >= 0 && r <= 1);
return r;
}

// TODO: std::span
template <typename N>
requires RelationAble<N>
auto T(QueryGraph<N>& g, const std::vector<N>& seq) -> float {
return std::transform_reduce(
seq.begin(), seq.end(), 1.0f, std::multiplies{}, [&](const N& n) {
return g.selectivity.at(n) * static_cast<float>(n.getCardinality());
});
requires RelationAble<N> auto T(QueryGraph<N>& g, const N& n) -> float {
return g.selectivity.at(n) * static_cast<float>(n.getCardinality());
}

template <typename N>
requires RelationAble<N>
auto C(QueryGraph<N>& g, const std::vector<N>& seq) -> float {
std::vector<N> v{};

for (auto const& x : seq)
// if (hist.contains(x) && hist.at(x).empty())
if (g.hist[x].empty())
v.push_back(x);
else
for (auto const& h : g.hist.at(x)) v.push_back(h);
requires RelationAble<N> auto C(QueryGraph<N>& g, const N& n) -> float {
auto hxs = g.hist[n];
// return 0 if Ri is root 113/637
// if (v.size() == 1 && v.front() == root) return 0;

if (v.empty()) return 0;
if (v.size() == 1)
return g.selectivity.at(v.front()) *
static_cast<float>(v.front().getCardinality()); // T(v)
if (g.root == n) return 0;

// auto s1 = seq | std::views::take(1);
// auto s2 = seq | std::views::drop(1);
// i.e: regular relation
if (hxs.empty()) return T(g, n);

auto s1 = std::vector<N>{v.front()};
auto s2 = std::vector<N>(v.begin() + 1, v.end());

// std::span(v.begin()+1, v.end())
return C(g, s1) + T(g, s1) * C(g, s2);
// otherwise compound relation
return C(g, hxs);
}

template <typename N>
requires RelationAble<N>
auto C(QueryGraph<N>& g, const std::set<N>& seq) -> float {
std::vector<N> t(seq.begin(), seq.end());
return C(g, t);
requires RelationAble<N> auto C(QueryGraph<N>& g, const std::vector<N>& seq)
-> float { // TODO: std::span
if (seq.empty()) return 0.0f;
auto s1 = seq.front();
// template instantiation depth exceeds maximum of 900
// auto s2 = seq | std::views::drop(1);
auto s2 = std::vector(seq.begin() + 1, seq.end());
return C(g, s1) + T(g, s1) * C(g, s2);
}
} // namespace JoinOrdering::ASI
9 changes: 3 additions & 6 deletions src/engine/joinOrdering/CostASI.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ namespace JoinOrdering::ASI {
* @return
*/
template <typename N>
requires RelationAble<N> auto rank(QueryGraph<N>& g, N n) -> float;
requires RelationAble<N> auto rank(QueryGraph<N>& g, const N& n) -> float;

template <typename N>
requires RelationAble<N>
auto T(QueryGraph<N>& g, const std::vector<N>& seq) -> float;
requires RelationAble<N> auto T(QueryGraph<N>& g, const N& n) -> float;

template <typename N>
requires RelationAble<N>
auto C(QueryGraph<N>& g, const std::vector<N>& seq) -> float;

template <typename N>
requires RelationAble<N>
auto C(QueryGraph<N>& g, const std::set<N>& seq) -> float;
// auto C(N n) -> float;
requires RelationAble<N> auto C(QueryGraph<N>& g, const N& n) -> float;

} // namespace JoinOrdering::ASI
8 changes: 5 additions & 3 deletions src/engine/joinOrdering/QueryGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ N QueryGraph<N>::combine(const N& a,
// to be able to apply the inverse operation (QueryGraph::uncombine)
// we keep track of the combined relation in the `hist` map
if (hist[a].empty()) hist[n].push_back(a);
// it's already a compound relation, so we graph it's original relations
// it's already a compound relation, so we grab it's original relations
else
for (auto const& x : hist[a]) hist[n].push_back(x);

Expand Down Expand Up @@ -153,8 +153,8 @@ N QueryGraph<N>::combine(const N& a,
auto cb = get_children(b);
children.insert(ca.begin(), ca.end());
children.insert(cb.begin(), cb.end());
children.erase(a); // redundant
children.erase(b); // redundant
// children.erase(a); // redundant
// children.erase(b); // redundant

// equiv. to add_rjoin(n, c, s, Direction::PARENT);
for (auto const& c : children) add_rjoin(c, n, s, Direction::CHILD);
Expand Down Expand Up @@ -284,6 +284,8 @@ auto QueryGraph<N>::get_chained_subtree(const N& n) -> N {
std::ranges::find_if(dxs, [&](const N& x) { return is_subtree(x); });

if (it != dxs.end()) return *it;

// AD_CONTRACT_CHECK(false);
throw std::runtime_error("how did we get here?");
}

Expand Down
1 change: 0 additions & 1 deletion src/engine/joinOrdering/QueryGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ requires RelationAble<N> class QueryGraph {
// ad_utility::HashMap<N, std::pair<N, EdgeInfo>> edges_;
ad_utility::HashMap<N, ad_utility::HashMap<N, EdgeInfo>> edges_;
ad_utility::HashMap<N, std::vector<N>> hist;
// ad_utility::HashMap<N, int> cardinality; // @deprecated
ad_utility::HashMap<N, float> selectivity;
N root;

Expand Down

0 comments on commit 653cc84

Please sign in to comment.