1#[macro_export]
35macro_rules! forward {
36 (sync, $expr:expr, $method:ident($($arg:expr),* $(,)?)) => {
37 ($expr).$method($($arg),*)
38 };
39 (async, $expr:expr, $method:ident($($arg:expr),* $(,)?)) => {
40 ($expr).$method($($arg),*).await
41 };
42}
43
44mod and_or;
46mod expr_input_ref;
47mod expr_literal;
48mod expr_some_all;
49pub(crate) mod expr_udf;
50pub(crate) mod wrapper;
51
52mod build;
53pub mod test_utils;
54mod value;
55
56use std::future::Future;
57use std::sync::Arc;
58
59use risingwave_common::array::{ArrayRef, DataChunk};
60use risingwave_common::row::OwnedRow;
61use risingwave_common::types::{DataType, Datum};
62
63pub use self::build::*;
64pub use self::expr_input_ref::InputRefExpression;
65pub use self::expr_literal::LiteralExpression;
66pub use self::value::{ValueImpl, ValueRef};
67pub use self::wrapper::*;
68pub use super::{ExprError, Result};
69
70#[auto_impl::auto_impl(&, Box, Arc)]
72pub trait ExpressionInfo: std::fmt::Debug + Sync + Send {
73 fn return_type(&self) -> DataType;
75
76 fn input_ref_index(&self) -> Option<usize> {
78 None
79 }
80}
81
82#[auto_impl::auto_impl(&, Box, Arc)]
88pub trait SyncExpression: ExpressionInfo {
89 fn eval(&self, input: &DataChunk) -> Result<ArrayRef> {
93 let value = self.eval_v2(input)?;
94 Ok(match value {
95 ValueImpl::Array(array) => array,
96 ValueImpl::Scalar { value, capacity } => {
97 let mut builder = self.return_type().create_array_builder(capacity);
98 builder.append_n(capacity, value);
99 builder.finish().into()
100 }
101 })
102 }
103
104 fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl> {
109 self.eval(input).map(ValueImpl::Array)
110 }
111
112 fn eval_row(&self, input: &OwnedRow) -> Result<Datum>;
114
115 fn eval_const(&self) -> Result<Datum> {
117 Err(ExprError::NotConstant)
118 }
119}
120
121pub trait AsyncExpression: ExpressionInfo {
123 fn eval<'a>(
127 &'a self,
128 input: &'a DataChunk,
129 ) -> impl Future<Output = Result<ArrayRef>> + Send + 'a {
130 async move {
131 let value = self.eval_v2(input).await?;
132 Ok(match value {
133 ValueImpl::Array(array) => array,
134 ValueImpl::Scalar { value, capacity } => {
135 let mut builder = self.return_type().create_array_builder(capacity);
136 builder.append_n(capacity, value);
137 builder.finish().into()
138 }
139 })
140 }
141 }
142
143 fn eval_v2<'a>(
148 &'a self,
149 input: &'a DataChunk,
150 ) -> impl Future<Output = Result<ValueImpl>> + Send + 'a {
151 async move { self.eval(input).await.map(ValueImpl::Array) }
152 }
153
154 fn eval_row<'a>(
156 &'a self,
157 input: &'a OwnedRow,
158 ) -> impl Future<Output = Result<Datum>> + Send + 'a;
159}
160
161#[async_trait::async_trait]
163pub trait AsyncDynExpression: ExpressionInfo {
164 async fn eval(&self, input: &DataChunk) -> Result<ArrayRef>;
166
167 async fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl>;
170
171 async fn eval_row(&self, input: &OwnedRow) -> Result<Datum>;
173}
174
175#[async_trait::async_trait]
176impl<E> AsyncDynExpression for E
177where
178 E: AsyncExpression,
179{
180 async fn eval(&self, input: &DataChunk) -> Result<ArrayRef> {
181 AsyncExpression::eval(self, input).await
182 }
183
184 async fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl> {
185 AsyncExpression::eval_v2(self, input).await
186 }
187
188 async fn eval_row(&self, input: &OwnedRow) -> Result<Datum> {
189 AsyncExpression::eval_row(self, input).await
190 }
191}
192
193impl AsyncExpression for Arc<dyn AsyncDynExpression> {
194 async fn eval(&self, input: &DataChunk) -> Result<ArrayRef> {
195 AsyncDynExpression::eval(self.as_ref(), input).await
196 }
197
198 async fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl> {
199 AsyncDynExpression::eval_v2(self.as_ref(), input).await
200 }
201
202 async fn eval_row(&self, input: &OwnedRow) -> Result<Datum> {
203 AsyncDynExpression::eval_row(self.as_ref(), input).await
204 }
205}
206
207#[derive(Clone, Debug)]
209pub enum BoxedExpression {
210 Sync(Arc<dyn SyncExpression>),
211 Async(Arc<dyn AsyncDynExpression>),
212}
213
214pub fn try_into_sync_exprs(
218 exprs: Vec<BoxedExpression>,
219) -> std::result::Result<Vec<Arc<dyn SyncExpression>>, Vec<BoxedExpression>> {
220 try_convert_all(
221 exprs,
222 |expr| match expr {
223 BoxedExpression::Sync(expr) => Ok(expr),
224 expr @ BoxedExpression::Async(_) => Err(expr),
225 },
226 BoxedExpression::Sync,
227 )
228}
229
230pub fn try_convert_all<T, U>(
232 items: Vec<T>,
233 mut try_convert: impl FnMut(T) -> std::result::Result<U, T>,
234 recover: impl Fn(U) -> T,
235) -> std::result::Result<Vec<U>, Vec<T>> {
236 let mut converted = Vec::with_capacity(items.len());
237 let mut items = items.into_iter();
238 loop {
239 let Some(item) = items.next() else {
240 return Ok(converted);
241 };
242 match try_convert(item) {
243 Ok(item) => converted.push(item),
244 Err(item) => {
245 let items = converted
246 .into_iter()
247 .map(recover)
248 .chain(std::iter::once(item))
249 .chain(items)
250 .collect();
251 return Err(items);
252 }
253 }
254 }
255}
256
257impl BoxedExpression {
258 pub fn return_type(&self) -> DataType {
260 match self {
261 Self::Sync(expr) => expr.return_type(),
262 Self::Async(expr) => expr.return_type(),
263 }
264 }
265
266 pub fn input_ref_index(&self) -> Option<usize> {
268 match self {
269 Self::Sync(expr) => expr.input_ref_index(),
270 Self::Async(expr) => expr.input_ref_index(),
271 }
272 }
273
274 pub fn eval_const(&self) -> Result<Datum> {
276 match self {
277 Self::Sync(expr) => expr.eval_const(),
278 Self::Async(_) => Err(ExprError::NotConstant),
279 }
280 }
281
282 pub async fn eval(&self, input: &DataChunk) -> Result<ArrayRef> {
284 match self {
285 Self::Sync(expr) => expr.eval(input),
286 Self::Async(expr) => AsyncDynExpression::eval(expr.as_ref(), input).await,
287 }
288 }
289
290 pub async fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl> {
293 match self {
294 Self::Sync(expr) => expr.eval_v2(input),
295 Self::Async(expr) => AsyncDynExpression::eval_v2(expr.as_ref(), input).await,
296 }
297 }
298
299 pub async fn eval_row(&self, input: &OwnedRow) -> Result<Datum> {
301 match self {
302 Self::Sync(expr) => expr.eval_row(input),
303 Self::Async(expr) => AsyncDynExpression::eval_row(expr.as_ref(), input).await,
304 }
305 }
306}
307
308impl<E> From<E> for BoxedExpression
309where
310 E: SyncExpression + 'static,
311{
312 fn from(expr: E) -> Self {
313 Self::Sync(Arc::new(expr))
314 }
315}
316
317impl ExpressionInfo for BoxedExpression {
318 fn return_type(&self) -> DataType {
319 self.return_type()
320 }
321
322 fn input_ref_index(&self) -> Option<usize> {
323 self.input_ref_index()
324 }
325}
326
327impl AsyncExpression for BoxedExpression {
328 async fn eval(&self, input: &DataChunk) -> Result<ArrayRef> {
329 match self {
330 Self::Sync(expr) => expr.eval(input),
331 Self::Async(expr) => AsyncDynExpression::eval(expr.as_ref(), input).await,
332 }
333 }
334
335 async fn eval_v2(&self, input: &DataChunk) -> Result<ValueImpl> {
336 match self {
337 Self::Sync(expr) => expr.eval_v2(input),
338 Self::Async(expr) => AsyncDynExpression::eval_v2(expr.as_ref(), input).await,
339 }
340 }
341
342 async fn eval_row(&self, input: &OwnedRow) -> Result<Datum> {
343 match self {
344 Self::Sync(expr) => expr.eval_row(input),
345 Self::Async(expr) => expr.as_ref().eval_row(input).await,
346 }
347 }
348}
349
350#[easy_ext::ext(SyncExpressionBoxExt)]
356impl<E: SyncExpression + 'static> E {
357 pub fn boxed(self) -> BoxedExpression {
359 BoxedExpression::Sync(Arc::new(self))
360 }
361}
362
363#[easy_ext::ext(AsyncExpressionBoxExt)]
365impl<E: AsyncExpression + 'static> E {
366 pub fn boxed(self) -> BoxedExpression {
368 BoxedExpression::Async(Arc::new(self))
369 }
370}
371
372#[derive(Debug)]
386pub enum NonStrictExpression {
387 Sync(Arc<dyn SyncExpression>),
388 Async(Arc<dyn AsyncDynExpression>),
389}
390
391impl NonStrictExpression {
392 pub fn for_test(inner: impl Into<BoxedExpression>) -> NonStrictExpression {
396 match inner.into() {
397 BoxedExpression::Sync(inner) => Self::Sync(inner),
398 BoxedExpression::Async(inner) => Self::Async(inner),
399 }
400 }
401
402 pub fn new_topmost(
408 inner: impl Into<BoxedExpression>,
409 error_report: impl EvalErrorReport + 'static,
410 ) -> NonStrictExpression {
411 match inner.into() {
412 BoxedExpression::Sync(inner) => {
413 let inner = wrapper::non_strict::NonStrict::new(inner, error_report);
414 Self::Sync(Arc::new(inner))
415 }
416 BoxedExpression::Async(inner) => {
417 let inner = wrapper::non_strict::NonStrict::new(inner, error_report);
418 Self::Async(Arc::new(inner))
419 }
420 }
421 }
422
423 pub fn return_type(&self) -> DataType {
425 match self {
426 Self::Sync(expr) => expr.return_type(),
427 Self::Async(expr) => expr.return_type(),
428 }
429 }
430
431 pub async fn eval_infallible(&self, input: &DataChunk) -> ArrayRef {
435 match self {
436 Self::Sync(expr) => expr.eval(input),
437 Self::Async(expr) => AsyncDynExpression::eval(expr.as_ref(), input).await,
438 }
439 .expect("evaluation failed")
440 }
441
442 pub async fn eval_row_infallible(&self, input: &OwnedRow) -> Datum {
447 match self {
448 Self::Sync(expr) => expr.eval_row(input),
449 Self::Async(expr) => AsyncDynExpression::eval_row(expr.as_ref(), input).await,
450 }
451 .expect("evaluation failed")
452 }
453
454 pub fn into_inner(self) -> BoxedExpression {
456 match self {
457 Self::Sync(expr) => BoxedExpression::Sync(expr),
458 Self::Async(expr) => BoxedExpression::Async(expr),
459 }
460 }
461
462 pub fn inner(&self) -> &dyn ExpressionInfo {
464 match self {
465 Self::Sync(expr) => expr.as_ref(),
466 Self::Async(expr) => expr.as_ref(),
467 }
468 }
469}
470
471#[derive(Debug)]
483pub struct Context {
484 pub arg_types: Vec<DataType>,
485 pub return_type: DataType,
486 pub variadic: bool,
488}