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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::any::Any;
use tipb::FieldType;
use super::super::function::RpnFnMeta;
use tidb_query_datatype::codec::data_type::ScalarValue;
#[derive(Debug)]
pub enum RpnExpressionNode {
FnCall {
func_meta: RpnFnMeta,
args_len: usize,
field_type: FieldType,
metadata: Box<dyn Any + Send>,
},
Constant {
value: ScalarValue,
field_type: FieldType,
},
ColumnRef { offset: usize },
}
impl RpnExpressionNode {
#[cfg(test)]
pub fn field_type(&self) -> &FieldType {
match self {
RpnExpressionNode::FnCall { field_type, .. } => field_type,
RpnExpressionNode::Constant { field_type, .. } => field_type,
RpnExpressionNode::ColumnRef { .. } => panic!(),
}
}
#[cfg(test)]
pub fn expr_tp(&self) -> tipb::ExprType {
use tidb_query_datatype::EvalType;
use tipb::ExprType;
match self {
RpnExpressionNode::FnCall { .. } => ExprType::ScalarFunc,
RpnExpressionNode::Constant { value, .. } => match value.eval_type() {
EvalType::Bytes => ExprType::Bytes,
EvalType::DateTime => ExprType::MysqlTime,
EvalType::Decimal => ExprType::MysqlDecimal,
EvalType::Duration => ExprType::MysqlDuration,
EvalType::Int => ExprType::Int64,
EvalType::Json => ExprType::MysqlJson,
EvalType::Real => ExprType::Float64,
EvalType::Enum => ExprType::MysqlEnum,
EvalType::Set => ExprType::MysqlSet,
},
RpnExpressionNode::ColumnRef { .. } => ExprType::ColumnRef,
}
}
#[cfg(test)]
pub fn fn_call_func(&self) -> RpnFnMeta {
match self {
RpnExpressionNode::FnCall { func_meta, .. } => *func_meta,
_ => panic!(),
}
}
#[cfg(test)]
pub fn constant_value(&self) -> &ScalarValue {
match self {
RpnExpressionNode::Constant { value, .. } => value,
_ => panic!(),
}
}
}
#[derive(Debug)]
pub struct RpnExpression(Vec<RpnExpressionNode>);
impl std::ops::Deref for RpnExpression {
type Target = Vec<RpnExpressionNode>;
fn deref(&self) -> &Vec<RpnExpressionNode> {
&self.0
}
}
impl std::ops::DerefMut for RpnExpression {
fn deref_mut(&mut self) -> &mut Vec<RpnExpressionNode> {
&mut self.0
}
}
impl From<Vec<RpnExpressionNode>> for RpnExpression {
fn from(v: Vec<RpnExpressionNode>) -> Self {
Self(v)
}
}
impl AsRef<[RpnExpressionNode]> for RpnExpression {
fn as_ref(&self) -> &[RpnExpressionNode] {
self.0.as_ref()
}
}
impl AsMut<[RpnExpressionNode]> for RpnExpression {
fn as_mut(&mut self) -> &mut [RpnExpressionNode] {
self.0.as_mut()
}
}
impl RpnExpression {
pub fn ret_field_type<'a>(&'a self, schema: &'a [FieldType]) -> &'a FieldType {
assert!(!self.0.is_empty());
let last_node = self.0.last().unwrap();
match last_node {
RpnExpressionNode::FnCall { field_type, .. } => field_type,
RpnExpressionNode::Constant { field_type, .. } => field_type,
RpnExpressionNode::ColumnRef { offset } => &schema[*offset],
}
}
pub fn into_inner(self) -> Vec<RpnExpressionNode> {
self.0
}
pub fn is_last_constant(&self) -> bool {
assert!(!self.0.is_empty());
matches!(self.0.last().unwrap(), RpnExpressionNode::Constant { .. })
}
}