-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcomparison_expression.h
113 lines (100 loc) · 3.77 KB
/
comparison_expression.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//===----------------------------------------------------------------------===//
//
// BusTub
//
// comparison_expression.h
//
// Identification: src/include/expression/comparison_expression.h
//
// Copyright (c) 2015-19, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#pragma once
#include <string>
#include <utility>
#include <vector>
#include "catalog/schema.h"
#include "execution/expressions/abstract_expression.h"
#include "fmt/format.h"
#include "storage/table/tuple.h"
#include "type/value_factory.h"
namespace bustub {
/** ComparisonType represents the type of comparison that we want to perform. */
enum class ComparisonType { Equal, NotEqual, LessThan, LessThanOrEqual, GreaterThan, GreaterThanOrEqual };
/**
* ComparisonExpression represents two expressions being compared.
*/
class ComparisonExpression : public AbstractExpression {
public:
/** Creates a new comparison expression representing (left comp_type right). */
ComparisonExpression(AbstractExpressionRef left, AbstractExpressionRef right, ComparisonType comp_type)
: AbstractExpression({std::move(left), std::move(right)}, TypeId::BOOLEAN), comp_type_{comp_type} {}
auto Evaluate(const Tuple *tuple, const Schema &schema) const -> Value override {
Value lhs = GetChildAt(0)->Evaluate(tuple, schema);
Value rhs = GetChildAt(1)->Evaluate(tuple, schema);
return ValueFactory::GetBooleanValue(PerformComparison(lhs, rhs));
}
auto EvaluateJoin(const Tuple *left_tuple, const Schema &left_schema, const Tuple *right_tuple,
const Schema &right_schema) const -> Value override {
Value lhs = GetChildAt(0)->EvaluateJoin(left_tuple, left_schema, right_tuple, right_schema);
Value rhs = GetChildAt(1)->EvaluateJoin(left_tuple, left_schema, right_tuple, right_schema);
return ValueFactory::GetBooleanValue(PerformComparison(lhs, rhs));
}
/** @return the string representation of the expression node and its children */
auto ToString() const -> std::string override {
return fmt::format("({}{}{})", *GetChildAt(0), comp_type_, *GetChildAt(1));
}
BUSTUB_EXPR_CLONE_WITH_CHILDREN(ComparisonExpression);
ComparisonType comp_type_;
private:
auto PerformComparison(const Value &lhs, const Value &rhs) const -> CmpBool {
switch (comp_type_) {
case ComparisonType::Equal:
return lhs.CompareEquals(rhs);
case ComparisonType::NotEqual:
return lhs.CompareNotEquals(rhs);
case ComparisonType::LessThan:
return lhs.CompareLessThan(rhs);
case ComparisonType::LessThanOrEqual:
return lhs.CompareLessThanEquals(rhs);
case ComparisonType::GreaterThan:
return lhs.CompareGreaterThan(rhs);
case ComparisonType::GreaterThanOrEqual:
return lhs.CompareGreaterThanEquals(rhs);
default:
BUSTUB_ASSERT(false, "Unsupported comparison type.");
}
}
};
} // namespace bustub
template <>
struct fmt::formatter<bustub::ComparisonType> : formatter<string_view> {
template <typename FormatContext>
auto format(bustub::ComparisonType c, FormatContext &ctx) const {
string_view name;
switch (c) {
case bustub::ComparisonType::Equal:
name = "=";
break;
case bustub::ComparisonType::NotEqual:
name = "!=";
break;
case bustub::ComparisonType::LessThan:
name = "<";
break;
case bustub::ComparisonType::LessThanOrEqual:
name = "<=";
break;
case bustub::ComparisonType::GreaterThan:
name = ">";
break;
case bustub::ComparisonType::GreaterThanOrEqual:
name = ">=";
break;
default:
name = "Unknown";
break;
}
return formatter<string_view>::format(name, ctx);
}
};