Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tuple enum
Browse files Browse the repository at this point in the history
xJoskiy committed Jan 27, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 893660c commit 9e2a146
Showing 5 changed files with 31 additions and 26 deletions.
24 changes: 13 additions & 11 deletions src/core/algorithms/dc/model/column_operand.h
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@

namespace algos::dc {

enum class Tuple { kS, kT, kMixed };

// @brief Represents a column operand for Predicate.
//
// A predicate (e.g., t.A == s.A) comprises three elements:
@@ -30,15 +32,15 @@ namespace algos::dc {
class ColumnOperand {
private:
boost::optional<Column const*> column_;
boost::optional<bool> is_first_tuple_;
boost::optional<dc::Tuple> tuple_;
model::Type const* type_;
std::byte const* val_;
std::byte const* val_;

public:
ColumnOperand() noexcept : val_(nullptr) {};

ColumnOperand(Column const* column, bool is_first_tuple, model::Type const* type)
: column_(column), is_first_tuple_(is_first_tuple), type_(type), val_(nullptr) {}
ColumnOperand(Column const* column, dc::Tuple tuple, model::Type const* type)
: column_(column), tuple_(tuple), type_(type), val_(nullptr) {}

ColumnOperand(std::string const& str_val, model::Type const* type) : type_(type) {
std::byte* val = type->Allocate();
@@ -48,7 +50,7 @@ class ColumnOperand {

ColumnOperand(ColumnOperand const& rhs) : type_(rhs.type_) {
if (rhs.IsVariable()) {
is_first_tuple_ = rhs.is_first_tuple_;
tuple_ = rhs.tuple_;
column_ = rhs.column_;
val_ = nullptr;
} else {
@@ -69,7 +71,7 @@ class ColumnOperand {
std::swap(type_, rhs.type_);
std::swap(val_, rhs.val_);
std::swap(column_, rhs.column_);
std::swap(is_first_tuple_, rhs.is_first_tuple_);
std::swap(tuple_, rhs.tuple_);
}

bool operator==(ColumnOperand const& rhs) const {
@@ -80,7 +82,7 @@ class ColumnOperand {
return type_->Compare(GetVal(), rhs.GetVal()) == model::CompareResult::kEqual;
}

return GetColumn() == rhs.GetColumn() && IsFirstTuple() == rhs.IsFirstTuple();
return column_ == rhs.column_ && tuple_ == rhs.tuple_;
}

bool operator!=(ColumnOperand const& rhs) const {
@@ -92,9 +94,9 @@ class ColumnOperand {
return column_.get();
}

bool IsFirstTuple() const {
assert(is_first_tuple_.is_initialized());
return is_first_tuple_.get();
Tuple GetTuple() const {
assert(tuple_.is_initialized());
return tuple_.get();
}

model::Type const* GetType() const noexcept {
@@ -117,7 +119,7 @@ class ColumnOperand {
std::string ToString() const {
std::string res;
if (IsVariable()) {
res = (is_first_tuple_.get() ? "t." : "s.") + column_.get()->GetName();
res = (tuple_.get() == Tuple::kT ? "t." : "s.") + column_.get()->GetName();
} else {
res = type_->ValueToString(val_);
}
6 changes: 3 additions & 3 deletions src/core/algorithms/dc/model/dc.cpp
Original file line number Diff line number Diff line change
@@ -48,11 +48,11 @@ bool DC::CheckOneInequality() const {

bool DC::CheckOneTuple() const {
ColumnOperand operand = predicates_.front().GetVariableOperand();
bool is_first_tuple = operand.IsFirstTuple();
dc::Tuple tuple = operand.GetTuple();

auto check = [is_first_tuple](Predicate const& pred) {
auto check = [tuple](Predicate const& pred) {
return (pred.IsConstant() or pred.IsOneTuple()) and
pred.GetVariableOperand().IsFirstTuple() == is_first_tuple;
pred.GetVariableOperand().GetTuple() == tuple;
};

return std::all_of(predicates_.begin(), predicates_.end(), check);
11 changes: 7 additions & 4 deletions src/core/algorithms/dc/model/predicate.h
Original file line number Diff line number Diff line change
@@ -14,9 +14,6 @@ namespace algos::dc {
// A predicate (e.g., t.A == s.A) comprises three elements: the column
// operand from the first tuple ("t.A"), the comparison operator ("=="),
// and the column operand from the second tuple ("s.A").
//
// In case of constant DC, predicate may contain constant
// values instead of column operands thus variant is utilized
class Predicate {
private:
Operator op_;
@@ -53,7 +50,7 @@ class Predicate {
}

bool IsCrossTuple() const {
return IsVariable() and l_.IsFirstTuple() != r_.IsFirstTuple();
return IsVariable() and l_.GetTuple() != r_.GetTuple();
}

bool IsOneTuple() const {
@@ -71,6 +68,12 @@ class Predicate {
std::string ToString() const {
return l_.ToString() + " " + op_.ToString() + " " + r_.ToString();
}

Tuple GetTuple() const {
if (IsConstant()) return GetVariableOperand().GetTuple();
if (IsCrossTuple()) return Tuple::kMixed;
return l_.GetTuple();
}
};

} // namespace algos::dc
4 changes: 2 additions & 2 deletions src/core/algorithms/dc/parser/dc_parser.cpp
Original file line number Diff line number Diff line change
@@ -139,7 +139,7 @@ bool DCParser::IsVarOperand(std::string op) const {

ColumnOperand DCParser::ConvertToVariableOperand(std::string operand) const {
Column* column = nullptr;
bool is_first_tuple = operand.front() == 't';
dc::Tuple tuple = operand.front() == 't' ? dc::Tuple::kT : dc::Tuple::kS;
std::string name(operand.begin() + 2, operand.end());
std::vector<std::unique_ptr<Column>> const& cols = relation_->GetSchema()->GetColumns();
if (!cols.front()->GetName().empty()) { // Has header
@@ -162,7 +162,7 @@ ColumnOperand DCParser::ConvertToVariableOperand(std::string operand) const {
}

size_t col_ind = column->GetIndex();
return {column, is_first_tuple, &data_[col_ind].GetType()};
return {column, tuple, &data_[col_ind].GetType()};
}

} // namespace algos::dc
12 changes: 6 additions & 6 deletions src/core/algorithms/dc/verifier/dc_verifier.cpp
Original file line number Diff line number Diff line change
@@ -158,17 +158,17 @@ dc::DC DCVerifier::GetDC(std::vector<dc::Predicate> const& no_diseq_preds,
bool DCVerifier::VerifyMixed(dc::DC const& dc) {
std::vector<dc::Predicate> predicates = dc.GetPredicates();
std::vector<dc::Predicate> s_predicates, t_predicates, mixed_predicates;

for (auto pred : predicates) {
bool left_op_from_first_tuple = pred.GetLeftOperand().IsFirstTuple();
bool right_op_from_first_tuple = pred.GetRightOperand().IsFirstTuple();
switch (left_op_from_first_tuple + right_op_from_first_tuple) {
case 0: // s.A op s.B
dc::Tuple tuple = pred.GetTuple();
switch (tuple) {
case dc::Tuple::kS: // s.A op s.B
s_predicates.emplace_back(pred);
break;
case 1: // s.A op t.B or t.A op s.B
case dc::Tuple::kMixed: // s.A op t.B or t.A op s.B
mixed_predicates.emplace_back(pred);
break;
case 2: // t.A op t.B
case dc::Tuple::kT: // t.A op t.B
t_predicates.emplace_back(pred);
break;
default:

0 comments on commit 9e2a146

Please sign in to comment.