Skip to content

Commit 8a9e931

Browse files
committed
Consolidate numeric binary evaluators
1 parent 088ee78 commit 8a9e931

16 files changed

Lines changed: 260 additions & 722 deletions

File tree

src/execution/dql/join/nested_loop_join.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,10 @@ mod test {
462462
use crate::planner::Childrens;
463463
use crate::storage::rocksdb::{RocksStorage, RocksTransaction};
464464
use crate::storage::Storage;
465-
use crate::types::evaluator::int32::Int32GtBinaryEvaluator;
466-
use crate::types::evaluator::BinaryEvaluatorBox;
465+
use crate::types::evaluator::binary_create;
467466
use crate::types::LogicalType;
468467
use crate::utils::lru::SharedLruCache;
468+
use std::borrow::Cow;
469469
use std::collections::HashSet;
470470
use std::hash::RandomState;
471471
use std::sync::Arc;
@@ -598,11 +598,9 @@ mod test {
598598
ColumnRef::from(ColumnCatalog::new("c4".to_owned(), true, desc.clone())),
599599
3,
600600
)),
601-
evaluator: Some(BinaryEvaluatorBox::new(
602-
Arc::new(Int32GtBinaryEvaluator),
603-
LogicalType::Integer,
604-
BinaryOperator::Gt,
605-
)),
601+
evaluator: Some(
602+
binary_create(Cow::Owned(LogicalType::Integer), BinaryOperator::Gt).unwrap(),
603+
),
606604
ty: LogicalType::Boolean,
607605
};
608606

src/expression/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -876,8 +876,7 @@ mod test {
876876
use crate::storage::rocksdb::{RocksStorage, RocksTransaction};
877877
use crate::storage::{Storage, Transaction};
878878
use crate::types::evaluator::boolean::BooleanNotUnaryEvaluator;
879-
use crate::types::evaluator::int32::Int32PlusBinaryEvaluator;
880-
use crate::types::evaluator::{cast_create, BinaryEvaluatorBox, UnaryEvaluatorBox};
879+
use crate::types::evaluator::{binary_create, cast_create, UnaryEvaluatorBox};
881880
use crate::types::value::{DataValue, Utf8Type};
882881
use crate::types::CharLengthUnits;
883882
use crate::types::LogicalType;
@@ -1098,11 +1097,10 @@ mod test {
10981097
op: BinaryOperator::Plus,
10991098
left_expr: Box::new(ScalarExpression::Empty),
11001099
right_expr: Box::new(ScalarExpression::Empty),
1101-
evaluator: Some(BinaryEvaluatorBox::new(
1102-
Arc::new(Int32PlusBinaryEvaluator),
1103-
LogicalType::Integer,
1100+
evaluator: Some(binary_create(
1101+
Cow::Owned(LogicalType::Integer),
11041102
BinaryOperator::Plus,
1105-
)),
1103+
)?),
11061104
ty: LogicalType::Integer,
11071105
},
11081106
Some(&context),

src/types/evaluator/binary.rs

Lines changed: 215 additions & 227 deletions
Large diffs are not rendered by default.

src/types/evaluator/date.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::numeric_binary_evaluator_definition;
1615
use crate::types::evaluator::cast::{cast_fail, to_char, to_varchar};
1716
use crate::types::evaluator::DataValue;
1817
use crate::types::CharLengthUnits;
1918
use crate::types::LogicalType;
2019
use chrono::NaiveDate;
2120

22-
numeric_binary_evaluator_definition!(Date, DataValue::Date32);
2321
crate::define_cast_evaluator!(
2422
Date32ToCharCastEvaluator {
2523
len: u32,

src/types/evaluator/datetime.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::numeric_binary_evaluator_definition;
1615
use crate::types::evaluator::cast::{cast_fail, to_char, to_varchar};
1716
use crate::types::evaluator::DataValue;
1817
use crate::types::CharLengthUnits;
1918
use crate::types::LogicalType;
2019
use chrono::{DateTime, Datelike, Timelike};
2120

22-
numeric_binary_evaluator_definition!(DateTime, DataValue::Date64);
2321
crate::define_cast_evaluator!(
2422
Date64ToCharCastEvaluator {
2523
len: u32,

src/types/evaluator/decimal.rs

Lines changed: 0 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::errors::DatabaseError;
1615
use crate::types::evaluator::cast::{to_char, to_varchar};
17-
use crate::types::evaluator::BinaryEvaluator;
1816
use crate::types::evaluator::DataValue;
1917
use crate::types::CharLengthUnits;
2018
use ordered_float::OrderedFloat;
2119
use rust_decimal::prelude::ToPrimitive;
22-
use serde::{Deserialize, Serialize};
23-
use std::hint;
24-
25-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
26-
pub struct DecimalPlusBinaryEvaluator;
27-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
28-
pub struct DecimalMinusBinaryEvaluator;
29-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
30-
pub struct DecimalMultiplyBinaryEvaluator;
31-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
32-
pub struct DecimalDivideBinaryEvaluator;
33-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
34-
pub struct DecimalGtBinaryEvaluator;
35-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
36-
pub struct DecimalGtEqBinaryEvaluator;
37-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
38-
pub struct DecimalLtBinaryEvaluator;
39-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
40-
pub struct DecimalLtEqBinaryEvaluator;
41-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
42-
pub struct DecimalEqBinaryEvaluator;
43-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
44-
pub struct DecimalNotEqBinaryEvaluator;
45-
#[derive(Debug, PartialEq, Eq, Clone, Hash, Serialize, Deserialize)]
46-
pub struct DecimalModBinaryEvaluator;
47-
impl BinaryEvaluator for DecimalPlusBinaryEvaluator {
48-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
49-
Ok(match (left, right) {
50-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Decimal(v1 + v2),
51-
(DataValue::Decimal(_), DataValue::Null)
52-
| (DataValue::Null, DataValue::Decimal(_))
53-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
54-
_ => unsafe { hint::unreachable_unchecked() },
55-
})
56-
}
57-
}
58-
impl BinaryEvaluator for DecimalMinusBinaryEvaluator {
59-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
60-
Ok(match (left, right) {
61-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Decimal(v1 - v2),
62-
(DataValue::Decimal(_), DataValue::Null)
63-
| (DataValue::Null, DataValue::Decimal(_))
64-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
65-
_ => unsafe { hint::unreachable_unchecked() },
66-
})
67-
}
68-
}
6920

7021
crate::define_cast_evaluator!(DecimalToFloatCastEvaluator, DataValue::Decimal(value) => {
7122
Ok(DataValue::Float32(OrderedFloat(value.to_f32().ok_or_else(|| {
@@ -124,106 +75,6 @@ crate::define_cast_evaluator!(DecimalToUIntegerCastEvaluator, DataValue::Decimal
12475
crate::define_cast_evaluator!(DecimalToUBigintCastEvaluator, DataValue::Decimal(value) => {
12576
Ok(DataValue::UInt64(crate::decimal_to_int_cast!(*value, u64)))
12677
});
127-
impl BinaryEvaluator for DecimalMultiplyBinaryEvaluator {
128-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
129-
Ok(match (left, right) {
130-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Decimal(v1 * v2),
131-
(DataValue::Decimal(_), DataValue::Null)
132-
| (DataValue::Null, DataValue::Decimal(_))
133-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
134-
_ => unsafe { hint::unreachable_unchecked() },
135-
})
136-
}
137-
}
138-
impl BinaryEvaluator for DecimalDivideBinaryEvaluator {
139-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
140-
Ok(match (left, right) {
141-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Decimal(v1 / v2),
142-
(DataValue::Decimal(_), DataValue::Null)
143-
| (DataValue::Null, DataValue::Decimal(_))
144-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
145-
_ => unsafe { hint::unreachable_unchecked() },
146-
})
147-
}
148-
}
149-
impl BinaryEvaluator for DecimalGtBinaryEvaluator {
150-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
151-
Ok(match (left, right) {
152-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Boolean(v1 > v2),
153-
(DataValue::Decimal(_), DataValue::Null)
154-
| (DataValue::Null, DataValue::Decimal(_))
155-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
156-
_ => unsafe { hint::unreachable_unchecked() },
157-
})
158-
}
159-
}
160-
impl BinaryEvaluator for DecimalGtEqBinaryEvaluator {
161-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
162-
Ok(match (left, right) {
163-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Boolean(v1 >= v2),
164-
(DataValue::Decimal(_), DataValue::Null)
165-
| (DataValue::Null, DataValue::Decimal(_))
166-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
167-
_ => unsafe { hint::unreachable_unchecked() },
168-
})
169-
}
170-
}
171-
impl BinaryEvaluator for DecimalLtBinaryEvaluator {
172-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
173-
Ok(match (left, right) {
174-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Boolean(v1 < v2),
175-
(DataValue::Decimal(_), DataValue::Null)
176-
| (DataValue::Null, DataValue::Decimal(_))
177-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
178-
_ => unsafe { hint::unreachable_unchecked() },
179-
})
180-
}
181-
}
182-
impl BinaryEvaluator for DecimalLtEqBinaryEvaluator {
183-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
184-
Ok(match (left, right) {
185-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Boolean(v1 <= v2),
186-
(DataValue::Decimal(_), DataValue::Null)
187-
| (DataValue::Null, DataValue::Decimal(_))
188-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
189-
_ => unsafe { hint::unreachable_unchecked() },
190-
})
191-
}
192-
}
193-
impl BinaryEvaluator for DecimalEqBinaryEvaluator {
194-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
195-
Ok(match (left, right) {
196-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Boolean(v1 == v2),
197-
(DataValue::Decimal(_), DataValue::Null)
198-
| (DataValue::Null, DataValue::Decimal(_))
199-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
200-
_ => unsafe { hint::unreachable_unchecked() },
201-
})
202-
}
203-
}
204-
impl BinaryEvaluator for DecimalNotEqBinaryEvaluator {
205-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
206-
Ok(match (left, right) {
207-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Boolean(v1 != v2),
208-
(DataValue::Decimal(_), DataValue::Null)
209-
| (DataValue::Null, DataValue::Decimal(_))
210-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
211-
_ => unsafe { hint::unreachable_unchecked() },
212-
})
213-
}
214-
}
215-
impl BinaryEvaluator for DecimalModBinaryEvaluator {
216-
fn binary_eval(&self, left: &DataValue, right: &DataValue) -> Result<DataValue, DatabaseError> {
217-
Ok(match (left, right) {
218-
(DataValue::Decimal(v1), DataValue::Decimal(v2)) => DataValue::Decimal(v1 % v2),
219-
(DataValue::Decimal(_), DataValue::Null)
220-
| (DataValue::Null, DataValue::Decimal(_))
221-
| (DataValue::Null, DataValue::Null) => DataValue::Null,
222-
_ => unsafe { hint::unreachable_unchecked() },
223-
})
224-
}
225-
}
226-
22778
#[cfg(all(test, not(target_arch = "wasm32")))]
22879
mod test {
22980
use super::*;

0 commit comments

Comments
 (0)