risingwave_frontend/binder/expr/
binary_op.rs1use risingwave_common::bail_not_implemented;
16use risingwave_common::types::{DataType, JsonbVal};
17use risingwave_sqlparser::ast::{BinaryOperator, Expr};
18
19use crate::binder::Binder;
20use crate::error::{ErrorCode, Result};
21use crate::expr::{Expr as _, ExprImpl, ExprType, FunctionCall};
22
23impl Binder {
24 pub(super) fn bind_binary_op(
25 &mut self,
26 left: Expr,
27 op: BinaryOperator,
28 mut right: Expr,
29 ) -> Result<ExprImpl> {
30 let bound_left = self.bind_expr_inner(left)?;
31
32 let mut func_types = vec![];
33
34 right = match right {
35 Expr::SomeOp(expr) => {
36 func_types.push(ExprType::Some);
37 *expr
38 }
39 Expr::AllOp(expr) => {
40 func_types.push(ExprType::All);
41 *expr
42 }
43 right => right,
44 };
45
46 let bound_right = self.bind_expr_inner(right)?;
47
48 if let BinaryOperator::Custom(name) = &op
49 && matches!(name.as_str(), "@?" | "@@")
50 {
51 return Ok(FunctionCall::new_unchecked(
54 match name.as_str() {
55 "@?" => ExprType::JsonbPathExists,
56 "@@" => ExprType::JsonbPathMatch,
57 _ => unreachable!(),
58 },
59 vec![
60 bound_left,
61 bound_right,
62 ExprImpl::literal_jsonb(JsonbVal::empty_object()), ExprImpl::literal_bool(true), ],
65 DataType::Boolean,
66 )
67 .into());
68 }
69
70 func_types.extend(Self::resolve_binary_operator(
71 op,
72 &bound_left,
73 &bound_right,
74 )?);
75
76 FunctionCall::new_binary_op_func(func_types, vec![bound_left, bound_right])
77 }
78
79 fn resolve_binary_operator(
80 op: BinaryOperator,
81 bound_left: &ExprImpl,
82 bound_right: &ExprImpl,
83 ) -> Result<Vec<ExprType>> {
84 let mut func_types = vec![];
85 let final_type = match op {
86 BinaryOperator::Plus => ExprType::Add,
87 BinaryOperator::Minus => ExprType::Subtract,
88 BinaryOperator::Multiply => ExprType::Multiply,
89 BinaryOperator::Divide => ExprType::Divide,
90 BinaryOperator::Modulo => ExprType::Modulus,
91 BinaryOperator::NotEq => ExprType::NotEqual,
92 BinaryOperator::Eq => ExprType::Equal,
93 BinaryOperator::Lt => ExprType::LessThan,
94 BinaryOperator::LtEq => ExprType::LessThanOrEqual,
95 BinaryOperator::Gt => ExprType::GreaterThan,
96 BinaryOperator::GtEq => ExprType::GreaterThanOrEqual,
97 BinaryOperator::And => ExprType::And,
98 BinaryOperator::Or => ExprType::Or,
99 BinaryOperator::Pow => ExprType::Pow,
100 BinaryOperator::Custom(name) if name == "@>" => {
101 let left_type = (!bound_left.is_untyped()).then(|| bound_left.return_type());
102 let right_type = (!bound_right.is_untyped()).then(|| bound_right.return_type());
103 match (left_type, right_type) {
104 (Some(DataType::List { .. }), Some(DataType::List { .. }))
105 | (Some(DataType::List { .. }), None)
106 | (None, Some(DataType::List { .. })) => ExprType::ArrayContains,
107 (Some(DataType::Jsonb), Some(DataType::Jsonb))
108 | (Some(DataType::Jsonb), None)
109 | (None, Some(DataType::Jsonb)) => ExprType::JsonbContains,
110 (left, right) => {
111 return Err(ErrorCode::BindError(format!(
112 "operator does not exist: {} @> {}",
113 left.map_or_else(|| String::from("unknown"), |x| x.to_string()),
114 right.map_or_else(|| String::from("unknown"), |x| x.to_string()),
115 ))
116 .into());
117 }
118 }
119 }
120 BinaryOperator::Custom(name) if name == "<@" => {
121 let left_type = (!bound_left.is_untyped()).then(|| bound_left.return_type());
122 let right_type = (!bound_right.is_untyped()).then(|| bound_right.return_type());
123 match (left_type, right_type) {
124 (Some(DataType::List { .. }), Some(DataType::List { .. }))
125 | (Some(DataType::List { .. }), None)
126 | (None, Some(DataType::List { .. })) => ExprType::ArrayContained,
127 (Some(DataType::Jsonb), Some(DataType::Jsonb))
128 | (Some(DataType::Jsonb), None)
129 | (None, Some(DataType::Jsonb)) => ExprType::JsonbContained,
130 (left, right) => {
131 return Err(ErrorCode::BindError(format!(
132 "operator does not exist: {} <@ {}",
133 left.map_or_else(|| String::from("unknown"), |x| x.to_string()),
134 right.map_or_else(|| String::from("unknown"), |x| x.to_string()),
135 ))
136 .into());
137 }
138 }
139 }
140 BinaryOperator::Custom(name) if name == "||" => {
141 let left_type = (!bound_left.is_untyped()).then(|| bound_left.return_type());
142 let right_type = (!bound_right.is_untyped()).then(|| bound_right.return_type());
143 match (left_type, right_type) {
144 (Some(DataType::List { .. }), Some(DataType::List { .. }))
146 | (Some(DataType::List { .. }), None)
147 | (None, Some(DataType::List { .. })) => ExprType::ArrayCat,
148 (Some(DataType::List { .. }), Some(_)) => ExprType::ArrayAppend,
149 (Some(_), Some(DataType::List { .. })) => ExprType::ArrayPrepend,
150
151 (Some(DataType::Varchar), _) | (_, Some(DataType::Varchar)) => {
153 ExprType::ConcatOp
154 }
155
156 (Some(DataType::Jsonb), Some(DataType::Jsonb))
157 | (Some(DataType::Jsonb), None)
158 | (None, Some(DataType::Jsonb)) => ExprType::JsonbConcat,
159
160 (Some(DataType::Bytea), Some(DataType::Bytea))
162 | (Some(DataType::Bytea), None)
163 | (None, Some(DataType::Bytea)) => ExprType::ByteaConcatOp,
164
165 (None, _) | (_, None) => ExprType::ConcatOp,
167
168 (Some(left_type), Some(right_type)) => {
170 return Err(ErrorCode::BindError(format!(
171 "operator does not exist: {} || {}",
172 left_type, right_type
173 ))
174 .into());
175 }
176 }
177 }
178 BinaryOperator::Custom(name) => match name.as_str() {
179 "&" => ExprType::BitwiseAnd,
181 "|" => ExprType::BitwiseOr,
182 "#" => ExprType::BitwiseXor,
183 "<<" => ExprType::BitwiseShiftLeft,
184 ">>" => ExprType::BitwiseShiftRight,
185 "^@" => ExprType::StartsWith,
187 "~" => ExprType::RegexpEq,
188 "~~" => ExprType::Like,
189 "~~*" => ExprType::ILike,
190 "!~" => {
191 func_types.push(ExprType::Not);
192 ExprType::RegexpEq
193 }
194 "!~~" => {
195 func_types.push(ExprType::Not);
196 ExprType::Like
197 }
198 "!~~*" => {
199 func_types.push(ExprType::Not);
200 ExprType::ILike
201 }
202 "->" => ExprType::JsonbAccess,
204 "->>" => ExprType::JsonbAccessStr,
205 "#-" => ExprType::JsonbDeletePath,
206 "#>" => ExprType::JsonbExtractPathVariadic,
207 "#>>" => ExprType::JsonbExtractPathTextVariadic,
208 "?" => ExprType::JsonbExists,
209 "?|" => ExprType::JsonbExistsAny,
210 "?&" => ExprType::JsonbExistsAll,
211 "<->" => ExprType::L2Distance,
213 _ => bail_not_implemented!(issue = 112, "binary op: {:?}", name),
214 },
215 BinaryOperator::Xor | BinaryOperator::PGQualified(_) => {
216 bail_not_implemented!(issue = 112, "binary op: {:?}", op)
217 }
218 };
219 func_types.push(final_type);
220 Ok(func_types)
221 }
222}