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) {
150 (Some(DataType::List { .. }), Some(DataType::List { .. }))
152 | (Some(DataType::List { .. }), None)
153 | (None, Some(DataType::List { .. })) => ExprType::ArrayCat,
154 (Some(DataType::List { .. }), Some(_)) => ExprType::ArrayAppend,
155 (Some(_), Some(DataType::List { .. })) => ExprType::ArrayPrepend,
156
157 (Some(DataType::Varchar), _) | (_, Some(DataType::Varchar)) => {
159 ExprType::ConcatOp
160 }
161
162 (Some(DataType::Jsonb), Some(DataType::Jsonb))
164 | (Some(DataType::Jsonb), None)
165 | (None, Some(DataType::Jsonb)) => ExprType::JsonbConcat,
166
167 (Some(DataType::Bytea), Some(DataType::Bytea))
169 | (Some(DataType::Bytea), None)
170 | (None, Some(DataType::Bytea)) => ExprType::ByteaConcatOp,
171
172 (Some(DataType::Vector(_)), Some(DataType::Vector(_)))
174 | (Some(DataType::Vector(_)), None)
175 | (None, Some(DataType::Vector(_))) => ExprType::VecConcat,
176
177 (None, _) | (_, None) => ExprType::ConcatOp,
182
183 (Some(left_type), Some(right_type)) => {
185 return Err(ErrorCode::BindError(format!(
186 "operator does not exist: {} || {}",
187 left_type, right_type
188 ))
189 .into());
190 }
191 }
192 }
193 BinaryOperator::Custom(name) => match name.as_str() {
194 "&" => ExprType::BitwiseAnd,
196 "|" => ExprType::BitwiseOr,
197 "#" => ExprType::BitwiseXor,
198 "<<" => ExprType::BitwiseShiftLeft,
199 ">>" => ExprType::BitwiseShiftRight,
200 "^@" => ExprType::StartsWith,
202 "~" => ExprType::RegexpEq,
203 "~~" => ExprType::Like,
204 "~~*" => ExprType::ILike,
205 "!~" => {
206 func_types.push(ExprType::Not);
207 ExprType::RegexpEq
208 }
209 "!~~" => {
210 func_types.push(ExprType::Not);
211 ExprType::Like
212 }
213 "!~~*" => {
214 func_types.push(ExprType::Not);
215 ExprType::ILike
216 }
217 "->" => ExprType::JsonbAccess,
219 "->>" => ExprType::JsonbAccessStr,
220 "#-" => ExprType::JsonbDeletePath,
221 "#>" => ExprType::JsonbExtractPathVariadic,
222 "#>>" => ExprType::JsonbExtractPathTextVariadic,
223 "?" => ExprType::JsonbExists,
224 "?|" => ExprType::JsonbExistsAny,
225 "?&" => ExprType::JsonbExistsAll,
226 "<->" => ExprType::L2Distance,
228 "<=>" => ExprType::CosineDistance,
229 "<+>" => ExprType::L1Distance,
230 "<#>" => {
231 func_types.push(ExprType::Neg);
232 ExprType::InnerProduct
233 }
234 _ => bail_not_implemented!(issue = 112, "binary op: {:?}", name),
235 },
236 BinaryOperator::Xor | BinaryOperator::PGQualified(_) => {
237 bail_not_implemented!(issue = 112, "binary op: {:?}", op)
238 }
239 };
240 func_types.push(final_type);
241 Ok(func_types)
242 }
243}