1#include "duckdb/common/exception.hpp"
2#include "duckdb/common/operator/comparison_operators.hpp"
3#include "duckdb/common/value_operations/value_operations.hpp"
4#include "duckdb/planner/expression/bound_comparison_expression.hpp"
5
6namespace duckdb {
7
8//===--------------------------------------------------------------------===//
9// Comparison Operations
10//===--------------------------------------------------------------------===//
11
12struct ValuePositionComparator {
13 // Return true if the positional Values definitely match.
14 // Default to the same as the final value
15 template <typename OP>
16 static inline bool Definite(const Value &lhs, const Value &rhs) {
17 return Final<OP>(lhs, rhs);
18 }
19
20 // Select the positional Values that need further testing.
21 // Usually this means Is Not Distinct, as those are the semantics used by Postges
22 template <typename OP>
23 static inline bool Possible(const Value &lhs, const Value &rhs) {
24 return ValueOperations::NotDistinctFrom(left: lhs, right: rhs);
25 }
26
27 // Return true if the positional Values definitely match in the final position
28 // This needs to be specialised.
29 template <typename OP>
30 static inline bool Final(const Value &lhs, const Value &rhs) {
31 return false;
32 }
33
34 // Tie-break based on length when one of the sides has been exhausted, returning true if the LHS matches.
35 // This essentially means that the existing positions compare equal.
36 // Default to the same semantics as the OP for idx_t. This works in most cases.
37 template <typename OP>
38 static inline bool TieBreak(const idx_t lpos, const idx_t rpos) {
39 return OP::Operation(lpos, rpos);
40 }
41};
42
43// Equals must always check every column
44template <>
45inline bool ValuePositionComparator::Definite<duckdb::Equals>(const Value &lhs, const Value &rhs) {
46 return false;
47}
48
49template <>
50inline bool ValuePositionComparator::Final<duckdb::Equals>(const Value &lhs, const Value &rhs) {
51 return ValueOperations::NotDistinctFrom(left: lhs, right: rhs);
52}
53
54// NotEquals must check everything that matched
55template <>
56inline bool ValuePositionComparator::Possible<duckdb::NotEquals>(const Value &lhs, const Value &rhs) {
57 return true;
58}
59
60template <>
61inline bool ValuePositionComparator::Final<duckdb::NotEquals>(const Value &lhs, const Value &rhs) {
62 return ValueOperations::NotDistinctFrom(left: lhs, right: rhs);
63}
64
65// Non-strict inequalities must use strict comparisons for Definite
66template <>
67bool ValuePositionComparator::Definite<duckdb::LessThanEquals>(const Value &lhs, const Value &rhs) {
68 return !ValuePositionComparator::Definite<duckdb::GreaterThan>(lhs, rhs);
69}
70
71template <>
72bool ValuePositionComparator::Final<duckdb::GreaterThan>(const Value &lhs, const Value &rhs) {
73 return ValueOperations::DistinctGreaterThan(left: lhs, right: rhs);
74}
75
76template <>
77bool ValuePositionComparator::Final<duckdb::LessThanEquals>(const Value &lhs, const Value &rhs) {
78 return !ValuePositionComparator::Final<duckdb::GreaterThan>(lhs, rhs);
79}
80
81template <>
82bool ValuePositionComparator::Definite<duckdb::GreaterThanEquals>(const Value &lhs, const Value &rhs) {
83 return !ValuePositionComparator::Definite<duckdb::GreaterThan>(lhs: rhs, rhs: lhs);
84}
85
86template <>
87bool ValuePositionComparator::Final<duckdb::GreaterThanEquals>(const Value &lhs, const Value &rhs) {
88 return !ValuePositionComparator::Final<duckdb::GreaterThan>(lhs: rhs, rhs: lhs);
89}
90
91// Strict inequalities just use strict for both Definite and Final
92template <>
93bool ValuePositionComparator::Final<duckdb::LessThan>(const Value &lhs, const Value &rhs) {
94 return ValuePositionComparator::Final<duckdb::GreaterThan>(lhs: rhs, rhs: lhs);
95}
96
97template <class OP>
98static bool TemplatedBooleanOperation(const Value &left, const Value &right) {
99 const auto &left_type = left.type();
100 const auto &right_type = right.type();
101 if (left_type != right_type) {
102 Value left_copy = left;
103 Value right_copy = right;
104
105 LogicalType comparison_type = BoundComparisonExpression::BindComparison(left_type, right_type);
106 if (!left_copy.DefaultTryCastAs(target_type: comparison_type) || !right_copy.DefaultTryCastAs(target_type: comparison_type)) {
107 return false;
108 }
109 D_ASSERT(left_copy.type() == right_copy.type());
110 return TemplatedBooleanOperation<OP>(left_copy, right_copy);
111 }
112 switch (left_type.InternalType()) {
113 case PhysicalType::BOOL:
114 return OP::Operation(left.GetValueUnsafe<bool>(), right.GetValueUnsafe<bool>());
115 case PhysicalType::INT8:
116 return OP::Operation(left.GetValueUnsafe<int8_t>(), right.GetValueUnsafe<int8_t>());
117 case PhysicalType::INT16:
118 return OP::Operation(left.GetValueUnsafe<int16_t>(), right.GetValueUnsafe<int16_t>());
119 case PhysicalType::INT32:
120 return OP::Operation(left.GetValueUnsafe<int32_t>(), right.GetValueUnsafe<int32_t>());
121 case PhysicalType::INT64:
122 return OP::Operation(left.GetValueUnsafe<int64_t>(), right.GetValueUnsafe<int64_t>());
123 case PhysicalType::UINT8:
124 return OP::Operation(left.GetValueUnsafe<uint8_t>(), right.GetValueUnsafe<uint8_t>());
125 case PhysicalType::UINT16:
126 return OP::Operation(left.GetValueUnsafe<uint16_t>(), right.GetValueUnsafe<uint16_t>());
127 case PhysicalType::UINT32:
128 return OP::Operation(left.GetValueUnsafe<uint32_t>(), right.GetValueUnsafe<uint32_t>());
129 case PhysicalType::UINT64:
130 return OP::Operation(left.GetValueUnsafe<uint64_t>(), right.GetValueUnsafe<uint64_t>());
131 case PhysicalType::INT128:
132 return OP::Operation(left.GetValueUnsafe<hugeint_t>(), right.GetValueUnsafe<hugeint_t>());
133 case PhysicalType::FLOAT:
134 return OP::Operation(left.GetValueUnsafe<float>(), right.GetValueUnsafe<float>());
135 case PhysicalType::DOUBLE:
136 return OP::Operation(left.GetValueUnsafe<double>(), right.GetValueUnsafe<double>());
137 case PhysicalType::INTERVAL:
138 return OP::Operation(left.GetValueUnsafe<interval_t>(), right.GetValueUnsafe<interval_t>());
139 case PhysicalType::VARCHAR:
140 return OP::Operation(StringValue::Get(value: left), StringValue::Get(value: right));
141 case PhysicalType::STRUCT: {
142 auto &left_children = StructValue::GetChildren(value: left);
143 auto &right_children = StructValue::GetChildren(value: right);
144 // this should be enforced by the type
145 D_ASSERT(left_children.size() == right_children.size());
146 idx_t i = 0;
147 for (; i < left_children.size() - 1; ++i) {
148 if (ValuePositionComparator::Definite<OP>(left_children[i], right_children[i])) {
149 return true;
150 }
151 if (!ValuePositionComparator::Possible<OP>(left_children[i], right_children[i])) {
152 return false;
153 }
154 }
155 return ValuePositionComparator::Final<OP>(left_children[i], right_children[i]);
156 }
157 case PhysicalType::LIST: {
158 auto &left_children = ListValue::GetChildren(value: left);
159 auto &right_children = ListValue::GetChildren(value: right);
160 for (idx_t pos = 0;; ++pos) {
161 if (pos == left_children.size() || pos == right_children.size()) {
162 return ValuePositionComparator::TieBreak<OP>(left_children.size(), right_children.size());
163 }
164 if (ValuePositionComparator::Definite<OP>(left_children[pos], right_children[pos])) {
165 return true;
166 }
167 if (!ValuePositionComparator::Possible<OP>(left_children[pos], right_children[pos])) {
168 return false;
169 }
170 }
171 return false;
172 }
173 default:
174 throw InternalException("Unimplemented type for value comparison");
175 }
176}
177
178bool ValueOperations::Equals(const Value &left, const Value &right) {
179 if (left.IsNull() || right.IsNull()) {
180 throw InternalException("Comparison on NULL values");
181 }
182 return TemplatedBooleanOperation<duckdb::Equals>(left, right);
183}
184
185bool ValueOperations::NotEquals(const Value &left, const Value &right) {
186 return !ValueOperations::Equals(left, right);
187}
188
189bool ValueOperations::GreaterThan(const Value &left, const Value &right) {
190 if (left.IsNull() || right.IsNull()) {
191 throw InternalException("Comparison on NULL values");
192 }
193 return TemplatedBooleanOperation<duckdb::GreaterThan>(left, right);
194}
195
196bool ValueOperations::GreaterThanEquals(const Value &left, const Value &right) {
197 return !ValueOperations::GreaterThan(left: right, right: left);
198}
199
200bool ValueOperations::LessThan(const Value &left, const Value &right) {
201 return ValueOperations::GreaterThan(left: right, right: left);
202}
203
204bool ValueOperations::LessThanEquals(const Value &left, const Value &right) {
205 return !ValueOperations::GreaterThan(left, right);
206}
207
208bool ValueOperations::NotDistinctFrom(const Value &left, const Value &right) {
209 if (left.IsNull() && right.IsNull()) {
210 return true;
211 }
212 if (left.IsNull() != right.IsNull()) {
213 return false;
214 }
215 return TemplatedBooleanOperation<duckdb::Equals>(left, right);
216}
217
218bool ValueOperations::DistinctFrom(const Value &left, const Value &right) {
219 return !ValueOperations::NotDistinctFrom(left, right);
220}
221
222bool ValueOperations::DistinctGreaterThan(const Value &left, const Value &right) {
223 if (left.IsNull() && right.IsNull()) {
224 return false;
225 } else if (right.IsNull()) {
226 return false;
227 } else if (left.IsNull()) {
228 return true;
229 }
230 return TemplatedBooleanOperation<duckdb::GreaterThan>(left, right);
231}
232
233bool ValueOperations::DistinctGreaterThanEquals(const Value &left, const Value &right) {
234 return !ValueOperations::DistinctGreaterThan(left: right, right: left);
235}
236
237bool ValueOperations::DistinctLessThan(const Value &left, const Value &right) {
238 return ValueOperations::DistinctGreaterThan(left: right, right: left);
239}
240
241bool ValueOperations::DistinctLessThanEquals(const Value &left, const Value &right) {
242 return !ValueOperations::DistinctGreaterThan(left, right);
243}
244
245} // namespace duckdb
246