Skip to content

Commit

Permalink
Add tuple enum
Browse files Browse the repository at this point in the history
  • Loading branch information
xJoskiy committed Jan 25, 2025
1 parent dd85211 commit 3d0b351
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 33 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
Expand Up @@ -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:
Expand All @@ -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();
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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_);
}
Expand Down
22 changes: 12 additions & 10 deletions src/core/algorithms/dc/model/dc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,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 = [this, is_first_tuple](Predicate const& pred) {
auto check = [this, 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);
Expand Down Expand Up @@ -95,15 +95,17 @@ void DC::ConvertEqualities() {
auto right = pred.GetRightOperand();
if (pred.IsVariable() and pred.IsCrossColumn() and pred.IsCrossTuple() and
pred.GetOperator().GetType() == dc::OperatorType::kEqual) {
auto left_op = dc::ColumnOperand(left.GetColumn(), left.IsFirstTuple(), left.GetType());
auto right_op =
dc::ColumnOperand(right.GetColumn(), right.IsFirstTuple(), right.GetType());
// auto left_op = dc::ColumnOperand(left.GetColumn(), left.GetTuple(), left.GetType());
// auto right_op =
// dc::ColumnOperand(right.GetColumn(), right.GetTuple(), right.GetType());

auto less_equal = dc::Operator(dc::OperatorType::kLessEqual);
auto greater_equal = dc::Operator(dc::OperatorType::kGreaterEqual);
// auto less_equal = dc::Operator(dc::OperatorType::kLessEqual);
// auto greater_equal = dc::Operator(dc::OperatorType::kGreaterEqual);

res.emplace_back(less_equal, left_op, right_op);
res.emplace_back(greater_equal, left_op, right_op);
// res.emplace_back(less_equal, left_op, right_op);
// res.emplace_back(greater_equal, left_op, right_op);
res.emplace_back(dc::OperatorType::kLessEqual, left, right);
res.emplace_back(dc::OperatorType::kGreaterEqual, left, right);
} else {
res.emplace_back(pred);
}
Expand Down
11 changes: 7 additions & 4 deletions src/core/algorithms/dc/model/predicate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -56,7 +53,7 @@ class Predicate {

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

bool IsOneTuple() const {
Expand All @@ -74,6 +71,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
Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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:
Expand Down

0 comments on commit 3d0b351

Please sign in to comment.