risingwave_frontend/binder/
query.rs

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

use risingwave_common::catalog::Schema;
use risingwave_common::types::DataType;
use risingwave_common::util::sort_util::{ColumnOrder, OrderType};
use risingwave_sqlparser::ast::{
    Cte, CteInner, Expr, Fetch, OrderByExpr, Query, SetExpr, SetOperator, Value, With,
};
use thiserror_ext::AsReport;

use super::bind_context::BindingCteState;
use super::statement::RewriteExprsRecursive;
use super::BoundValues;
use crate::binder::bind_context::{BindingCte, RecursiveUnion};
use crate::binder::{Binder, BoundSetExpr};
use crate::error::{ErrorCode, Result};
use crate::expr::{CorrelatedId, Depth, ExprImpl, ExprRewriter};

/// A validated sql query, including order and union.
/// An example of its relationship with `BoundSetExpr` and `BoundSelect` can be found here: <https://bit.ly/3GQwgPz>
#[derive(Debug, Clone)]
pub struct BoundQuery {
    pub body: BoundSetExpr,
    pub order: Vec<ColumnOrder>,
    pub limit: Option<u64>,
    pub offset: Option<u64>,
    pub with_ties: bool,
    pub extra_order_exprs: Vec<ExprImpl>,
}

impl BoundQuery {
    /// The schema returned by this [`BoundQuery`].
    pub fn schema(&self) -> std::borrow::Cow<'_, Schema> {
        self.body.schema()
    }

    /// The types returned by this [`BoundQuery`].
    pub fn data_types(&self) -> Vec<DataType> {
        self.schema().data_types()
    }

    /// Checks whether this query contains references to outer queries.
    ///
    /// Note there are 3 cases:
    /// ```sql
    /// select 1 from a having exists ( -- this is self
    ///   select 1 from b where exists (
    ///     select b1 from c
    ///   )
    /// );
    ///
    /// select 1 from a having exists ( -- this is self
    ///   select 1 from b where exists (
    ///     select a1 from c
    ///   )
    /// );
    ///
    /// select 1 from a where exists (
    ///   select 1 from b having exists ( -- this is self, not the one above
    ///     select a1 from c
    ///   )
    /// );
    /// ```
    /// We assume `self` is the subquery after `having`. In other words, the query with `from b` in
    /// first 2 examples and the query with `from c` in the last example.
    ///
    /// * The first example is uncorrelated, because it is self-contained and does not depend on
    ///   table `a`, although there is correlated input ref (`b1`) in it.
    /// * The second example is correlated, because it depend on a correlated input ref (`a1`) that
    ///   goes out.
    /// * The last example is also correlated. because it cannot be evaluated independently either.
    pub fn is_correlated(&self, depth: Depth) -> bool {
        self.body.is_correlated(depth + 1)
            || self
                .extra_order_exprs
                .iter()
                .any(|e| e.has_correlated_input_ref_by_depth(depth + 1))
    }

    pub fn collect_correlated_indices_by_depth_and_assign_id(
        &mut self,
        depth: Depth,
        correlated_id: CorrelatedId,
    ) -> Vec<usize> {
        let mut correlated_indices = vec![];

        correlated_indices.extend(
            self.body
                .collect_correlated_indices_by_depth_and_assign_id(depth + 1, correlated_id),
        );

        correlated_indices.extend(self.extra_order_exprs.iter_mut().flat_map(|expr| {
            expr.collect_correlated_indices_by_depth_and_assign_id(depth + 1, correlated_id)
        }));
        correlated_indices
    }

    /// Simple `VALUES` without other clauses.
    pub fn with_values(values: BoundValues) -> Self {
        BoundQuery {
            body: BoundSetExpr::Values(values.into()),
            order: vec![],
            limit: None,
            offset: None,
            with_ties: false,
            extra_order_exprs: vec![],
        }
    }
}

impl RewriteExprsRecursive for BoundQuery {
    fn rewrite_exprs_recursive(&mut self, rewriter: &mut impl ExprRewriter) {
        let new_extra_order_exprs = std::mem::take(&mut self.extra_order_exprs)
            .into_iter()
            .map(|expr| rewriter.rewrite_expr(expr))
            .collect::<Vec<_>>();
        self.extra_order_exprs = new_extra_order_exprs;

        self.body.rewrite_exprs_recursive(rewriter);
    }
}

impl Binder {
    /// Bind a [`Query`].
    ///
    /// Before binding the [`Query`], we push the current [`BindContext`](super::BindContext) to the
    /// stack and create a new context, because it may be a subquery.
    ///
    /// After finishing binding, we pop the previous context from the stack.
    pub fn bind_query(&mut self, query: Query) -> Result<BoundQuery> {
        self.push_context();
        let result = self.bind_query_inner(query);
        self.pop_context()?;
        result
    }

    /// Bind a [`Query`] using the current [`BindContext`](super::BindContext).
    pub(super) fn bind_query_inner(
        &mut self,
        Query {
            with,
            body,
            order_by,
            limit,
            offset,
            fetch,
        }: Query,
    ) -> Result<BoundQuery> {
        let mut with_ties = false;
        let limit = match (limit, fetch) {
            (None, None) => None,
            (
                None,
                Some(Fetch {
                    with_ties: fetch_with_ties,
                    quantity,
                }),
            ) => {
                with_ties = fetch_with_ties;
                match quantity {
                    Some(v) => Some(parse_non_negative_i64("LIMIT", &v)? as u64),
                    None => Some(1),
                }
            }
            (Some(limit), None) => Some(parse_non_negative_i64("LIMIT", &limit)? as u64),
            (Some(_), Some(_)) => unreachable!(), // parse error
        };
        let offset = offset
            .map(|s| parse_non_negative_i64("OFFSET", &s))
            .transpose()?
            .map(|v| v as u64);

        if let Some(with) = with {
            self.bind_with(with)?;
        }
        let body = self.bind_set_expr(body)?;
        let name_to_index =
            Self::build_name_to_index(body.schema().fields().iter().map(|f| f.name.clone()));
        let mut extra_order_exprs = vec![];
        let visible_output_num = body.schema().len();
        let order = order_by
            .into_iter()
            .map(|order_by_expr| {
                self.bind_order_by_expr_in_query(
                    order_by_expr,
                    &name_to_index,
                    &mut extra_order_exprs,
                    visible_output_num,
                )
            })
            .collect::<Result<_>>()?;
        Ok(BoundQuery {
            body,
            order,
            limit,
            offset,
            with_ties,
            extra_order_exprs,
        })
    }

    pub fn build_name_to_index(names: impl Iterator<Item = String>) -> HashMap<String, usize> {
        let mut m = HashMap::new();
        names.enumerate().for_each(|(index, name)| {
            m.entry(name)
                // Ambiguous (duplicate) output names are marked with usize::MAX.
                // This is not necessarily an error as long as not actually referenced.
                .and_modify(|v| *v = usize::MAX)
                .or_insert(index);
        });
        m
    }

    /// Bind an `ORDER BY` expression in a [`Query`], which can be either:
    /// * an output-column name
    /// * index of an output column
    /// * an arbitrary expression
    ///
    /// Refer to `bind_group_by_expr_in_select` to see their similarities and differences.
    ///
    /// # Arguments
    ///
    /// * `name_to_index` - visible output column name -> index. Ambiguous (duplicate) output names
    ///   are marked with `usize::MAX`.
    /// * `visible_output_num` - the number of all visible output columns, including duplicates.
    fn bind_order_by_expr_in_query(
        &mut self,
        OrderByExpr {
            expr,
            asc,
            nulls_first,
        }: OrderByExpr,
        name_to_index: &HashMap<String, usize>,
        extra_order_exprs: &mut Vec<ExprImpl>,
        visible_output_num: usize,
    ) -> Result<ColumnOrder> {
        let order_type = OrderType::from_bools(asc, nulls_first);
        let column_index = match expr {
            Expr::Identifier(name) if let Some(index) = name_to_index.get(&name.real_value()) => {
                match *index != usize::MAX {
                    true => *index,
                    false => {
                        return Err(ErrorCode::BindError(format!(
                            "ORDER BY \"{}\" is ambiguous",
                            name.real_value()
                        ))
                        .into())
                    }
                }
            }
            Expr::Value(Value::Number(number)) => match number.parse::<usize>() {
                Ok(index) if 1 <= index && index <= visible_output_num => index - 1,
                _ => {
                    return Err(ErrorCode::InvalidInputSyntax(format!(
                        "Invalid ordinal number in ORDER BY: {}",
                        number
                    ))
                    .into())
                }
            },
            expr => {
                extra_order_exprs.push(self.bind_expr(expr)?);
                visible_output_num + extra_order_exprs.len() - 1
            }
        };
        Ok(ColumnOrder::new(column_index, order_type))
    }

    fn bind_with(&mut self, with: With) -> Result<()> {
        for cte_table in with.cte_tables {
            // note that the new `share_id` for the rcte is generated here
            let share_id = self.next_share_id();
            let Cte { alias, cte_inner } = cte_table;
            let table_name = alias.name.real_value();

            if with.recursive {
                if let CteInner::Query(query) = cte_inner {
                    let (
                        SetExpr::SetOperation {
                            op: SetOperator::Union,
                            all,
                            corresponding,
                            left,
                            right,
                        },
                        with,
                    ) = Self::validate_rcte(query)?
                    else {
                        return Err(ErrorCode::BindError(
                            "expect `SetOperation` as the return type of validation".into(),
                        )
                        .into());
                    };

                    // validated in `validate_rcte`
                    assert!(
                        !corresponding.is_corresponding(),
                        "`CORRESPONDING` is not supported in recursive CTE"
                    );

                    let entry = self
                        .context
                        .cte_to_relation
                        .entry(table_name)
                        .insert_entry(Rc::new(RefCell::new(BindingCte {
                            share_id,
                            state: BindingCteState::Init,
                            alias,
                        })))
                        .get()
                        .clone();

                    self.bind_rcte(with, entry, *left, *right, all)?;
                } else {
                    return Err(ErrorCode::BindError(
                        "RECURSIVE CTE only support query".to_string(),
                    )
                    .into());
                }
            } else {
                match cte_inner {
                    CteInner::Query(query) => {
                        let bound_query = self.bind_query(query)?;
                        self.context.cte_to_relation.insert(
                            table_name,
                            Rc::new(RefCell::new(BindingCte {
                                share_id,
                                state: BindingCteState::Bound {
                                    query: either::Either::Left(bound_query),
                                },
                                alias,
                            })),
                        );
                    }
                    CteInner::ChangeLog(from_table_name) => {
                        self.push_context();
                        let from_table_relation =
                            self.bind_relation_by_name(from_table_name.clone(), None, None)?;
                        self.pop_context()?;
                        self.context.cte_to_relation.insert(
                            table_name,
                            Rc::new(RefCell::new(BindingCte {
                                share_id,
                                state: BindingCteState::ChangeLog {
                                    table: from_table_relation,
                                },
                                alias,
                            })),
                        );
                    }
                }
            }
        }
        Ok(())
    }

    /// syntactically validate the recursive cte ast with the current support features in rw.
    fn validate_rcte(query: Query) -> Result<(SetExpr, Option<With>)> {
        let Query {
            with,
            body,
            order_by,
            limit,
            offset,
            fetch,
        } = query;

        /// the input clause should not be supported.
        fn should_be_empty<T>(v: Option<T>, clause: &str) -> Result<()> {
            if v.is_some() {
                return Err(ErrorCode::BindError(format!(
                    "`{clause}` is not supported in recursive CTE"
                ))
                .into());
            }
            Ok(())
        }

        should_be_empty(order_by.first(), "ORDER BY")?;
        should_be_empty(limit, "LIMIT")?;
        should_be_empty(offset, "OFFSET")?;
        should_be_empty(fetch, "FETCH")?;

        let SetExpr::SetOperation {
            op: SetOperator::Union,
            all,
            corresponding,
            left,
            right,
        } = body
        else {
            return Err(
                ErrorCode::BindError("`UNION` is required in recursive CTE".to_string()).into(),
            );
        };

        if !all {
            return Err(ErrorCode::BindError(
                "only `UNION ALL` is supported in recursive CTE now".to_string(),
            )
            .into());
        }

        if corresponding.is_corresponding() {
            return Err(ErrorCode::BindError(
                "`CORRESPONDING` is not supported in recursive CTE".to_string(),
            )
            .into());
        }

        Ok((
            SetExpr::SetOperation {
                op: SetOperator::Union,
                all,
                corresponding,
                left,
                right,
            },
            with,
        ))
    }

    fn bind_rcte(
        &mut self,
        with: Option<With>,
        entry: Rc<RefCell<BindingCte>>,
        left: SetExpr,
        right: SetExpr,
        all: bool,
    ) -> Result<()> {
        self.push_context();
        let result = self.bind_rcte_inner(with, entry, left, right, all);
        self.pop_context()?;
        result
    }

    fn bind_rcte_inner(
        &mut self,
        with: Option<With>,
        entry: Rc<RefCell<BindingCte>>,
        left: SetExpr,
        right: SetExpr,
        all: bool,
    ) -> Result<()> {
        if let Some(with) = with {
            self.bind_with(with)?;
        }

        // We assume `left` is the base term, otherwise the implementation may be very hard.
        // The behavior is the same as PostgreSQL's.
        // reference: <https://www.postgresql.org/docs/16/sql-select.html#:~:text=the%20recursive%20self%2Dreference%20must%20appear%20on%20the%20right%2Dhand%20side%20of%20the%20UNION>
        let mut base = self.bind_set_expr(left)?;

        entry.borrow_mut().state = BindingCteState::BaseResolved { base: base.clone() };

        // Reset context for right side, but keep `cte_to_relation`.
        let new_context = std::mem::take(&mut self.context);
        self.context
            .cte_to_relation
            .clone_from(&new_context.cte_to_relation);
        // bind the rest of the recursive cte
        let mut recursive = self.bind_set_expr(right)?;
        // Reset context for the set operation.
        self.context = Default::default();
        self.context.cte_to_relation = new_context.cte_to_relation;

        Self::align_schema(&mut base, &mut recursive, SetOperator::Union)?;
        let schema = base.schema().into_owned();

        let recursive_union = RecursiveUnion {
            all,
            base: Box::new(base),
            recursive: Box::new(recursive),
            schema,
        };

        entry.borrow_mut().state = BindingCteState::Bound {
            query: either::Either::Right(recursive_union),
        };

        Ok(())
    }
}

// TODO: Make clause a const generic param after <https://github.com/rust-lang/rust/issues/95174>.
fn parse_non_negative_i64(clause: &str, s: &str) -> Result<i64> {
    match s.parse::<i64>() {
        Ok(v) => {
            if v < 0 {
                Err(ErrorCode::InvalidInputSyntax(format!("{clause} must not be negative")).into())
            } else {
                Ok(v)
            }
        }
        Err(e) => Err(ErrorCode::InvalidInputSyntax(e.to_report_string()).into()),
    }
}