forked from databendlabs/databend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_arithmetic.rs
95 lines (82 loc) · 2.65 KB
/
function_arithmetic.rs
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
// Copyright 2020 The FuseQuery Authors.
//
// Code is licensed under AGPL License, Version 3.0.
use std::fmt;
use crate::datablocks::DataBlock;
use crate::datavalues;
use crate::datavalues::{
DataColumnarValue, DataSchema, DataType, DataValue, DataValueArithmeticOperator,
};
use crate::error::FuseQueryResult;
use crate::functions::Function;
#[derive(Clone)]
pub struct ArithmeticFunction {
depth: usize,
op: DataValueArithmeticOperator,
left: Box<Function>,
right: Box<Function>,
}
impl ArithmeticFunction {
pub fn try_create(
op: DataValueArithmeticOperator,
args: &[Function],
) -> FuseQueryResult<Function> {
Ok(Function::Arithmetic(ArithmeticFunction {
depth: 0,
op,
left: Box::from(args[0].clone()),
right: Box::from(args[1].clone()),
}))
}
pub fn return_type(&self, input_schema: &DataSchema) -> FuseQueryResult<DataType> {
datavalues::numerical_coercion(
format!("{}", self.op).as_str(),
&self.left.return_type(input_schema)?,
&self.right.return_type(input_schema)?,
)
}
pub fn nullable(&self, _input_schema: &DataSchema) -> FuseQueryResult<bool> {
Ok(false)
}
pub fn set_depth(&mut self, depth: usize) {
self.left.set_depth(depth);
self.right.set_depth(depth + 1);
self.depth = depth;
}
pub fn eval(&mut self, block: &DataBlock) -> FuseQueryResult<DataColumnarValue> {
Ok(DataColumnarValue::Array(
datavalues::data_array_arithmetic_op(
self.op.clone(),
&self.left.eval(block)?,
&self.right.eval(block)?,
)?,
))
}
pub fn accumulate(&mut self, block: &DataBlock) -> FuseQueryResult<()> {
self.left.accumulate(&block)?;
self.right.accumulate(&block)
}
pub fn accumulate_result(&self) -> FuseQueryResult<Vec<DataValue>> {
Ok([
&self.left.accumulate_result()?[..],
&self.right.accumulate_result()?[..],
]
.concat())
}
pub fn merge_state(&mut self, states: &[DataValue]) -> FuseQueryResult<()> {
self.left.merge_state(states)?;
self.right.merge_state(states)
}
pub fn merge_result(&self) -> FuseQueryResult<DataValue> {
datavalues::data_value_arithmetic_op(
self.op.clone(),
self.left.merge_result()?,
self.right.merge_result()?,
)
}
}
impl fmt::Display for ArithmeticFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} {} {:?}", self.left, self.op, self.right)
}
}