1use itertools::Itertools;
16use risingwave_common::bail_not_implemented;
17use risingwave_common::types::{
18 DataType, DateTimeField, Decimal, Interval, MapType, ScalarImpl, StructType,
19};
20use risingwave_sqlparser::ast::{DateTimeField as AstDateTimeField, Expr, Value};
21use thiserror_ext::AsReport;
22
23use crate::binder::Binder;
24use crate::error::{ErrorCode, Result};
25use crate::expr::{Expr as _, ExprImpl, ExprType, FunctionCall, Literal, align_types};
26
27impl Binder {
28 pub fn bind_value(&mut self, value: Value) -> Result<Literal> {
29 match value {
30 Value::Number(s) => self.bind_number(s),
31 Value::SingleQuotedString(s) => self.bind_string(s),
32 Value::CstyleEscapedString(s) => self.bind_string(s.value),
33 Value::Boolean(b) => self.bind_bool(b),
34 Value::Null => Ok(Literal::new_untyped(None)),
37 Value::Interval {
38 value,
39 leading_field,
40 leading_precision: None,
42 last_field: None,
43 fractional_seconds_precision: None,
44 } => self.bind_interval(value, leading_field),
45 _ => bail_not_implemented!("value: {:?}", value),
46 }
47 }
48
49 pub(super) fn bind_string(&mut self, s: String) -> Result<Literal> {
50 Ok(Literal::new_untyped(Some(s)))
51 }
52
53 fn bind_bool(&mut self, b: bool) -> Result<Literal> {
54 Ok(Literal::new(Some(ScalarImpl::Bool(b)), DataType::Boolean))
55 }
56
57 fn bind_number(&mut self, mut s: String) -> Result<Literal> {
58 let prefix_start = match s.starts_with('-') {
59 true => 1,
60 false => 0,
61 };
62 let base = match prefix_start + 2 <= s.len() {
63 true => match &s[prefix_start..prefix_start + 2] {
64 "0x" => 16,
66 "0o" => 8,
67 "0b" => 2,
68 _ => 10,
69 },
70 false => 10,
71 };
72 if base != 10 {
73 s.replace_range(prefix_start..prefix_start + 2, "");
74 }
75
76 let (data, data_type) = if let Ok(int_32) = i32::from_str_radix(&s, base) {
77 (Some(ScalarImpl::Int32(int_32)), DataType::Int32)
78 } else if let Ok(int_64) = i64::from_str_radix(&s, base) {
79 (Some(ScalarImpl::Int64(int_64)), DataType::Int64)
80 } else if let Ok(decimal) = Decimal::from_str_radix(&s, base) {
81 (Some(ScalarImpl::Decimal(decimal)), DataType::Decimal)
83 } else if let Some(scientific) = Decimal::from_scientific(&s) {
84 (Some(ScalarImpl::Decimal(scientific)), DataType::Decimal)
85 } else {
86 return Err(ErrorCode::BindError(format!("Number {s} overflows")).into());
87 };
88 Ok(Literal::new(data, data_type))
89 }
90
91 fn bind_interval(
92 &mut self,
93 s: String,
94 leading_field: Option<AstDateTimeField>,
95 ) -> Result<Literal> {
96 let interval =
97 Interval::parse_with_fields(&s, leading_field.map(Self::bind_date_time_field))
98 .map_err(|e| ErrorCode::BindError(e.to_report_string()))?;
99 let datum = Some(ScalarImpl::Interval(interval));
100 let literal = Literal::new(datum, DataType::Interval);
101
102 Ok(literal)
103 }
104
105 pub(crate) fn bind_date_time_field(field: AstDateTimeField) -> DateTimeField {
106 match field {
109 AstDateTimeField::Year => DateTimeField::Year,
110 AstDateTimeField::Month => DateTimeField::Month,
111 AstDateTimeField::Day => DateTimeField::Day,
112 AstDateTimeField::Hour => DateTimeField::Hour,
113 AstDateTimeField::Minute => DateTimeField::Minute,
114 AstDateTimeField::Second => DateTimeField::Second,
115 }
116 }
117
118 pub(super) fn bind_array(&mut self, exprs: Vec<Expr>) -> Result<ExprImpl> {
120 if exprs.is_empty() {
121 return Err(ErrorCode::BindError("cannot determine type of empty array\nHINT: Explicitly cast to the desired type, for example ARRAY[]::integer[].".into()).into());
122 }
123 let mut exprs = exprs
124 .into_iter()
125 .map(|e| self.bind_expr_inner(e))
126 .collect::<Result<Vec<ExprImpl>>>()?;
127 let element_type = align_types(exprs.iter_mut())?;
128 let expr: ExprImpl = FunctionCall::new_unchecked(
129 ExprType::Array,
130 exprs,
131 DataType::List(Box::new(element_type)),
132 )
133 .into();
134 Ok(expr)
135 }
136
137 pub(super) fn bind_map(&mut self, entries: Vec<(Expr, Expr)>) -> Result<ExprImpl> {
138 if entries.is_empty() {
139 return Err(ErrorCode::BindError("cannot determine type of empty map\nHINT: Explicitly cast to the desired type, for example MAP{}::map(int,int).".into()).into());
140 }
141 let mut keys = Vec::with_capacity(entries.len());
142 let mut values = Vec::with_capacity(entries.len());
143 for (k, v) in entries {
144 keys.push(self.bind_expr_inner(k)?);
145 values.push(self.bind_expr_inner(v)?);
146 }
147 let key_type = align_types(keys.iter_mut())?;
148 let value_type = align_types(values.iter_mut())?;
149
150 let keys: ExprImpl = FunctionCall::new_unchecked(
151 ExprType::Array,
152 keys,
153 DataType::List(Box::new(key_type.clone())),
154 )
155 .into();
156 let values: ExprImpl = FunctionCall::new_unchecked(
157 ExprType::Array,
158 values,
159 DataType::List(Box::new(value_type.clone())),
160 )
161 .into();
162
163 let expr: ExprImpl = FunctionCall::new_unchecked(
164 ExprType::MapFromKeyValues,
165 vec![keys, values],
166 DataType::Map(MapType::from_kv(key_type, value_type)),
167 )
168 .into();
169 Ok(expr)
170 }
171
172 pub(super) fn bind_array_cast(
173 &mut self,
174 exprs: Vec<Expr>,
175 element_type: Box<DataType>,
176 ) -> Result<ExprImpl> {
177 let exprs = exprs
178 .into_iter()
179 .map(|e| self.bind_cast_inner(e, *element_type.clone()))
180 .collect::<Result<Vec<ExprImpl>>>()?;
181
182 let expr: ExprImpl =
183 FunctionCall::new_unchecked(ExprType::Array, exprs, DataType::List(element_type))
184 .into();
185 Ok(expr)
186 }
187
188 pub(super) fn bind_map_cast(
189 &mut self,
190 entries: Vec<(Expr, Expr)>,
191 map_type: MapType,
192 ) -> Result<ExprImpl> {
193 let mut keys = Vec::with_capacity(entries.len());
194 let mut values = Vec::with_capacity(entries.len());
195 for (k, v) in entries {
196 keys.push(self.bind_cast_inner(k, map_type.key().clone())?);
197 values.push(self.bind_cast_inner(v, map_type.value().clone())?);
198 }
199
200 let keys: ExprImpl = FunctionCall::new_unchecked(
201 ExprType::Array,
202 keys,
203 DataType::List(Box::new(map_type.key().clone())),
204 )
205 .into();
206 let values: ExprImpl = FunctionCall::new_unchecked(
207 ExprType::Array,
208 values,
209 DataType::List(Box::new(map_type.value().clone())),
210 )
211 .into();
212
213 let expr: ExprImpl = FunctionCall::new_unchecked(
214 ExprType::MapFromKeyValues,
215 vec![keys, values],
216 DataType::Map(map_type),
217 )
218 .into();
219 Ok(expr)
220 }
221
222 pub(super) fn bind_index(&mut self, obj: Expr, index: Expr) -> Result<ExprImpl> {
223 let obj = self.bind_expr_inner(obj)?;
224 match obj.return_type() {
225 DataType::List(return_type) => Ok(FunctionCall::new_unchecked(
226 ExprType::ArrayAccess,
227 vec![obj, self.bind_expr_inner(index)?],
228 *return_type,
229 )
230 .into()),
231 DataType::Map(m) => Ok(FunctionCall::new_unchecked(
232 ExprType::MapAccess,
233 vec![obj, self.bind_expr_inner(index)?],
234 m.value().clone(),
235 )
236 .into()),
237 data_type => Err(ErrorCode::BindError(format!(
238 "index operator applied to type {}, which is not a list or map",
239 data_type
240 ))
241 .into()),
242 }
243 }
244
245 pub(super) fn bind_array_range_index(
246 &mut self,
247 obj: Expr,
248 start: Option<Box<Expr>>,
249 end: Option<Box<Expr>>,
250 ) -> Result<ExprImpl> {
251 let obj = self.bind_expr_inner(obj)?;
252 let start = match start {
253 None => ExprImpl::literal_int(1),
254 Some(expr) => self
255 .bind_expr_inner(*expr)?
256 .cast_implicit(DataType::Int32)?,
257 };
258 let end = match end {
261 None => ExprImpl::literal_int(i32::MAX),
262 Some(expr) => self
263 .bind_expr_inner(*expr)?
264 .cast_implicit(DataType::Int32)?,
265 };
266 match obj.return_type() {
267 DataType::List(return_type) => Ok(FunctionCall::new_unchecked(
268 ExprType::ArrayRangeAccess,
269 vec![obj, start, end],
270 DataType::List(return_type),
271 )
272 .into()),
273 data_type => Err(ErrorCode::BindError(format!(
274 "array range index applied to type {}, which is not a list",
275 data_type
276 ))
277 .into()),
278 }
279 }
280
281 pub(super) fn bind_row(&mut self, exprs: Vec<Expr>) -> Result<ExprImpl> {
283 let exprs = exprs
284 .into_iter()
285 .map(|e| self.bind_expr_inner(e))
286 .collect::<Result<Vec<ExprImpl>>>()?;
287 let data_type =
288 StructType::unnamed(exprs.iter().map(|e| e.return_type()).collect_vec()).into();
289 let expr: ExprImpl = FunctionCall::new_unchecked(ExprType::Row, exprs, data_type).into();
290 Ok(expr)
291 }
292}
293
294#[cfg(test)]
295mod tests {
296 use risingwave_common::types::test_utils::IntervalTestExt;
297 use risingwave_expr::expr::build_from_prost;
298 use risingwave_sqlparser::ast::Value::Number;
299
300 use super::*;
301 use crate::binder::test_utils::mock_binder;
302 use crate::expr::Expr;
303
304 #[tokio::test]
305 async fn test_bind_value() {
306 use std::str::FromStr;
307
308 let mut binder = mock_binder();
309 let values = [
310 "1",
311 "111111111111111",
312 "111111111.111111",
313 "111111111111111111111111",
314 "0.111111",
315 "-0.01",
316 ];
317 let data = [
318 Some(ScalarImpl::Int32(1)),
319 Some(ScalarImpl::Int64(111111111111111)),
320 Some(ScalarImpl::Decimal(
321 Decimal::from_str("111111111.111111").unwrap(),
322 )),
323 Some(ScalarImpl::Decimal(
324 Decimal::from_str("111111111111111111111111").unwrap(),
325 )),
326 Some(ScalarImpl::Decimal(Decimal::from_str("0.111111").unwrap())),
327 Some(ScalarImpl::Decimal(Decimal::from_str("-0.01").unwrap())),
328 ];
329 let data_type = [
330 DataType::Int32,
331 DataType::Int64,
332 DataType::Decimal,
333 DataType::Decimal,
334 DataType::Decimal,
335 DataType::Decimal,
336 ];
337
338 for i in 0..values.len() {
339 let value = Value::Number(String::from(values[i]));
340 let res = binder.bind_value(value).unwrap();
341 let ans = Literal::new(data[i].clone(), data_type[i].clone());
342 assert_eq!(res, ans);
343 }
344 }
345
346 #[tokio::test]
347 async fn test_bind_radix() {
348 let mut binder = mock_binder();
349
350 for (input, expected) in [
351 ("0x42e3", ScalarImpl::Int32(0x42e3)),
352 ("-0x40", ScalarImpl::Int32(-0x40)),
353 ("0b1101", ScalarImpl::Int32(0b1101)),
354 ("-0b101", ScalarImpl::Int32(-0b101)),
355 ("0o664", ScalarImpl::Int32(0o664)),
356 ("-0o755", ScalarImpl::Int32(-0o755)),
357 ("2147483647", ScalarImpl::Int32(2147483647)),
358 ("2147483648", ScalarImpl::Int64(2147483648)),
359 ("-2147483648", ScalarImpl::Int32(-2147483648)),
360 ("0x7fffffff", ScalarImpl::Int32(0x7fffffff)),
361 ("0x80000000", ScalarImpl::Int64(0x80000000)),
362 ("-0x80000000", ScalarImpl::Int32(-0x80000000)),
363 ] {
364 let lit = binder.bind_number(input.into()).unwrap();
365 assert_eq!(lit.get_data().as_ref().unwrap(), &expected);
366 }
367 }
368
369 #[tokio::test]
370 async fn test_bind_scientific_number() {
371 use std::str::FromStr;
372
373 let mut binder = mock_binder();
374 let values = [
375 ("1e6"),
376 ("1.25e6"),
377 ("1.25e1"),
378 ("1e-2"),
379 ("1.25e-2"),
380 ("1e15"),
381 ];
382 let data = [
383 Some(ScalarImpl::Decimal(Decimal::from_str("1000000").unwrap())),
384 Some(ScalarImpl::Decimal(Decimal::from_str("1250000").unwrap())),
385 Some(ScalarImpl::Decimal(Decimal::from_str("12.5").unwrap())),
386 Some(ScalarImpl::Decimal(Decimal::from_str("0.01").unwrap())),
387 Some(ScalarImpl::Decimal(Decimal::from_str("0.0125").unwrap())),
388 Some(ScalarImpl::Decimal(
389 Decimal::from_str("1000000000000000").unwrap(),
390 )),
391 ];
392 let data_type = [
393 DataType::Decimal,
394 DataType::Decimal,
395 DataType::Decimal,
396 DataType::Decimal,
397 DataType::Decimal,
398 DataType::Decimal,
399 ];
400
401 for i in 0..values.len() {
402 let res = binder.bind_value(Number(values[i].to_owned())).unwrap();
403 let ans = Literal::new(data[i].clone(), data_type[i].clone());
404 assert_eq!(res, ans);
405 }
406 }
407
408 #[test]
409 fn test_array_expr() {
410 let expr: ExprImpl = FunctionCall::new_unchecked(
411 ExprType::Array,
412 vec![ExprImpl::literal_int(11)],
413 DataType::List(Box::new(DataType::Int32)),
414 )
415 .into();
416 let expr_pb = expr.to_expr_proto();
417 let expr = build_from_prost(&expr_pb).unwrap();
418 match expr.return_type() {
419 DataType::List(datatype) => {
420 assert_eq!(datatype, Box::new(DataType::Int32));
421 }
422 _ => panic!("unexpected type"),
423 };
424 }
425
426 #[test]
427 fn test_array_index_expr() {
428 let array_expr = FunctionCall::new_unchecked(
429 ExprType::Array,
430 vec![ExprImpl::literal_int(11), ExprImpl::literal_int(22)],
431 DataType::List(Box::new(DataType::Int32)),
432 )
433 .into();
434
435 let expr: ExprImpl = FunctionCall::new_unchecked(
436 ExprType::ArrayAccess,
437 vec![array_expr, ExprImpl::literal_int(1)],
438 DataType::Int32,
439 )
440 .into();
441
442 let expr_pb = expr.to_expr_proto();
443 let expr = build_from_prost(&expr_pb).unwrap();
444 assert_eq!(expr.return_type(), DataType::Int32);
445 }
446
447 #[tokio::test]
448 async fn test_bind_interval() {
449 let mut binder = mock_binder();
450 let values = [
451 "1 hour",
452 "1 h",
453 "1 year",
454 "6 second",
455 "2 minutes",
456 "1 month",
457 ];
458 let data = vec![
459 Literal::new(
460 Some(ScalarImpl::Interval(Interval::from_minutes(60))),
461 DataType::Interval,
462 ),
463 Literal::new(
464 Some(ScalarImpl::Interval(Interval::from_minutes(60))),
465 DataType::Interval,
466 ),
467 Literal::new(
468 Some(ScalarImpl::Interval(Interval::from_ymd(1, 0, 0))),
469 DataType::Interval,
470 ),
471 Literal::new(
472 Some(ScalarImpl::Interval(Interval::from_millis(6 * 1000))),
473 DataType::Interval,
474 ),
475 Literal::new(
476 Some(ScalarImpl::Interval(Interval::from_minutes(2))),
477 DataType::Interval,
478 ),
479 Literal::new(
480 Some(ScalarImpl::Interval(Interval::from_month(1))),
481 DataType::Interval,
482 ),
483 ];
484
485 for i in 0..values.len() {
486 let value = Value::Interval {
487 value: values[i].to_owned(),
488 leading_field: None,
489 leading_precision: None,
490 last_field: None,
491 fractional_seconds_precision: None,
492 };
493 assert_eq!(binder.bind_value(value).unwrap(), data[i]);
494 }
495 }
496}