From 992bb4b435e1a33c2e431c3ce09f4834c78b030a Mon Sep 17 00:00:00 2001 From: xJoskiy Date: Fri, 24 Jan 2025 19:11:12 +0300 Subject: [PATCH] Add tuple enum --- src/core/algorithms/dc/model/column_operand.h | 33 ++++++++++--------- src/core/algorithms/dc/model/dc.cpp | 6 ++-- src/core/algorithms/dc/model/predicate.h | 11 ++++--- src/core/algorithms/dc/parser/dc_parser.cpp | 4 +-- .../algorithms/dc/verifier/dc_verifier.cpp | 12 +++---- 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/core/algorithms/dc/model/column_operand.h b/src/core/algorithms/dc/model/column_operand.h index 52f462b412..3c797d279f 100644 --- a/src/core/algorithms/dc/model/column_operand.h +++ b/src/core/algorithms/dc/model/column_operand.h @@ -15,6 +15,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,18 +32,19 @@ namespace algos::dc { // tuple (s). class ColumnOperand { private: - std::optional column_; - std::optional is_first_tuple_; + Column const* column_; + std::optional tuple_; model::Type const* type_; std::byte const* val_; public: - ColumnOperand() noexcept : val_(nullptr) {}; + ColumnOperand() noexcept : column_(nullptr), 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) { + ColumnOperand(std::string const& str_val, model::Type const* type) + : column_(nullptr), type_(type) { std::byte* val = type->Allocate(); type->ValueFromStr(val, str_val); val_ = val; @@ -49,7 +52,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 { @@ -70,7 +73,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 { @@ -81,7 +84,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 { @@ -89,13 +92,13 @@ class ColumnOperand { } Column const* GetColumn() const { - assert(column_.has_value()); - return column_.value(); + assert(column_ != nullptr); + return column_; } - bool IsFirstTuple() const { - assert(is_first_tuple_.has_value()); - return is_first_tuple_.value(); + Tuple GetTuple() const { + assert(tuple_.has_value()); + return tuple_.value(); } model::Type const* GetType() const noexcept { @@ -118,7 +121,7 @@ class ColumnOperand { std::string ToString() const { std::string res; if (IsVariable()) { - res = (is_first_tuple_.value() ? "t." : "s.") + column_.value()->GetName(); + res = (tuple_.value() == Tuple::kT ? "t." : "s.") + column_->GetName(); } else { res = type_->ValueToString(val_); } diff --git a/src/core/algorithms/dc/model/dc.cpp b/src/core/algorithms/dc/model/dc.cpp index 834509b86b..246fd885c8 100644 --- a/src/core/algorithms/dc/model/dc.cpp +++ b/src/core/algorithms/dc/model/dc.cpp @@ -39,11 +39,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); diff --git a/src/core/algorithms/dc/model/predicate.h b/src/core/algorithms/dc/model/predicate.h index 1480f5a225..f6cc0dfefb 100644 --- a/src/core/algorithms/dc/model/predicate.h +++ b/src/core/algorithms/dc/model/predicate.h @@ -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 diff --git a/src/core/algorithms/dc/parser/dc_parser.cpp b/src/core/algorithms/dc/parser/dc_parser.cpp index 8fe30bb4ef..73f87be990 100644 --- a/src/core/algorithms/dc/parser/dc_parser.cpp +++ b/src/core/algorithms/dc/parser/dc_parser.cpp @@ -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> 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 diff --git a/src/core/algorithms/dc/verifier/dc_verifier.cpp b/src/core/algorithms/dc/verifier/dc_verifier.cpp index a3a306a0e6..dbf7b760c7 100644 --- a/src/core/algorithms/dc/verifier/dc_verifier.cpp +++ b/src/core/algorithms/dc/verifier/dc_verifier.cpp @@ -158,17 +158,17 @@ dc::DC DCVerifier::GetDC(std::vector const& no_diseq_preds, bool DCVerifier::VerifyMixed(dc::DC const& dc) { std::vector predicates = dc.GetPredicates(); std::vector 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: