risingwave_frontend/binder/
update.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
// 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::collections::{BTreeMap, HashMap};

use fixedbitset::FixedBitSet;
use itertools::Itertools;
use risingwave_common::catalog::{Schema, TableVersionId};
use risingwave_common::types::DataType;
use risingwave_common::util::iter_util::ZipEqFast;
use risingwave_sqlparser::ast::{Assignment, AssignmentValue, Expr, ObjectName, SelectItem};

use super::statement::RewriteExprsRecursive;
use super::{Binder, BoundBaseTable};
use crate::catalog::TableId;
use crate::error::{bail_bind_error, bind_error, ErrorCode, Result, RwError};
use crate::expr::{Expr as _, ExprImpl, SubqueryKind};
use crate::user::UserId;
use crate::TableCatalog;

/// Project into `exprs` in `BoundUpdate` to get the new values for updating.
#[derive(Debug, Clone, Copy)]
pub enum UpdateProject {
    /// Use the expression at the given index in `exprs`.
    Simple(usize),
    /// Use the `i`-th field of the expression (returning a struct) at the given index in `exprs`.
    Composite(usize, usize),
}

impl UpdateProject {
    /// Offset the index by `i`.
    pub fn offset(self, i: usize) -> Self {
        match self {
            UpdateProject::Simple(index) => UpdateProject::Simple(index + i),
            UpdateProject::Composite(index, j) => UpdateProject::Composite(index + i, j),
        }
    }
}

#[derive(Debug, Clone)]
pub struct BoundUpdate {
    /// Id of the table to perform updating.
    pub table_id: TableId,

    /// Version id of the table.
    pub table_version_id: TableVersionId,

    /// Name of the table to perform updating.
    pub table_name: String,

    /// Owner of the table to perform updating.
    pub owner: UserId,

    /// Used for scanning the records to update with the `selection`.
    pub table: BoundBaseTable,

    pub selection: Option<ExprImpl>,

    /// Expression used to evaluate the new values for the columns.
    pub exprs: Vec<ExprImpl>,

    /// Mapping from the index of the column to be updated, to the index of the expression in `exprs`.
    ///
    /// By constructing two `Project` nodes with `exprs` and `projects`, we can get the new values.
    pub projects: HashMap<usize, UpdateProject>,

    // used for the 'RETURNING" keyword to indicate the returning items and schema
    // if the list is empty and the schema is None, the output schema will be a INT64 as the
    // affected row cnt
    pub returning_list: Vec<ExprImpl>,

    pub returning_schema: Option<Schema>,
}

impl RewriteExprsRecursive for BoundUpdate {
    fn rewrite_exprs_recursive(&mut self, rewriter: &mut impl crate::expr::ExprRewriter) {
        self.selection =
            std::mem::take(&mut self.selection).map(|expr| rewriter.rewrite_expr(expr));

        let new_exprs = std::mem::take(&mut self.exprs)
            .into_iter()
            .map(|expr| rewriter.rewrite_expr(expr))
            .collect::<Vec<_>>();
        self.exprs = new_exprs;

        let new_returning_list = std::mem::take(&mut self.returning_list)
            .into_iter()
            .map(|expr| rewriter.rewrite_expr(expr))
            .collect::<Vec<_>>();
        self.returning_list = new_returning_list;
    }
}

fn get_col_referenced_by_generated_pk(table_catalog: &TableCatalog) -> Result<FixedBitSet> {
    let column_num = table_catalog.columns().len();
    let pk_col_id = table_catalog.pk_column_ids();
    let mut bitset = FixedBitSet::with_capacity(column_num);

    let generated_pk_col_exprs = table_catalog
        .columns
        .iter()
        .filter(|c| pk_col_id.contains(&c.column_id()))
        .flat_map(|c| c.generated_expr());
    for expr_node in generated_pk_col_exprs {
        let expr = ExprImpl::from_expr_proto(expr_node)?;
        bitset.union_with(&expr.collect_input_refs(column_num));
    }
    Ok(bitset)
}

impl Binder {
    pub(super) fn bind_update(
        &mut self,
        name: ObjectName,
        assignments: Vec<Assignment>,
        selection: Option<Expr>,
        returning_items: Vec<SelectItem>,
    ) -> Result<BoundUpdate> {
        let (schema_name, table_name) = Self::resolve_schema_qualified_name(&self.db_name, name)?;

        let table_catalog = self.resolve_dml_table(schema_name.as_deref(), &table_name, false)?;
        let default_columns_from_catalog =
            table_catalog.default_columns().collect::<BTreeMap<_, _>>();
        if !returning_items.is_empty() && table_catalog.has_generated_column() {
            return Err(RwError::from(ErrorCode::BindError(
                "`RETURNING` clause is not supported for tables with generated columns".to_string(),
            )));
        }

        let table_id = table_catalog.id;
        let owner = table_catalog.owner;
        let table_version_id = table_catalog.version_id().expect("table must be versioned");
        let cols_refed_by_generated_pk = get_col_referenced_by_generated_pk(table_catalog)?;

        let table = self.bind_table(schema_name.as_deref(), &table_name, None)?;

        let selection = selection.map(|expr| self.bind_expr(expr)).transpose()?;

        let mut exprs = Vec::new();
        let mut projects = HashMap::new();

        macro_rules! record {
            ($id:expr, $project:expr) => {
                let id_index = $id.as_input_ref().unwrap().index;
                projects
                    .try_insert(id_index, $project)
                    .map_err(|_e| bind_error!("multiple assignments to the same column"))?;
            };
        }

        for Assignment { id, value } in assignments {
            let ids: Vec<_> = id
                .into_iter()
                .map(|id| self.bind_expr(Expr::Identifier(id)))
                .try_collect()?;

            match (ids.as_slice(), value) {
                // `SET col1 = DEFAULT`, `SET (col1, col2, ...) = DEFAULT`
                (ids, AssignmentValue::Default) => {
                    for id in ids {
                        let id_index = id.as_input_ref().unwrap().index;
                        let expr = default_columns_from_catalog
                            .get(&id_index)
                            .cloned()
                            .unwrap_or_else(|| ExprImpl::literal_null(id.return_type()));

                        exprs.push(expr);
                        record!(id, UpdateProject::Simple(exprs.len() - 1));
                    }
                }

                // `SET col1 = expr`
                ([id], AssignmentValue::Expr(expr)) => {
                    let expr = self.bind_expr(expr)?.cast_assign(id.return_type())?;
                    exprs.push(expr);
                    record!(id, UpdateProject::Simple(exprs.len() - 1));
                }
                // `SET (col1, col2, ...) = (val1, val2, ...)`
                (ids, AssignmentValue::Expr(Expr::Row(values))) => {
                    if ids.len() != values.len() {
                        bail_bind_error!("number of columns does not match number of values");
                    }

                    for (id, value) in ids.iter().zip_eq_fast(values) {
                        let expr = self.bind_expr(value)?.cast_assign(id.return_type())?;
                        exprs.push(expr);
                        record!(id, UpdateProject::Simple(exprs.len() - 1));
                    }
                }
                // `SET (col1, col2, ...) = (SELECT ...)`
                (ids, AssignmentValue::Expr(Expr::Subquery(subquery))) => {
                    let expr = self.bind_subquery_expr(*subquery, SubqueryKind::UpdateSet)?;

                    if expr.return_type().as_struct().len() != ids.len() {
                        bail_bind_error!("number of columns does not match number of values");
                    }

                    let target_type = DataType::new_unnamed_struct(
                        ids.iter().map(|id| id.return_type()).collect(),
                    );
                    let expr = expr.cast_assign(target_type)?;

                    exprs.push(expr);

                    for (i, id) in ids.iter().enumerate() {
                        record!(id, UpdateProject::Composite(exprs.len() - 1, i));
                    }
                }

                (_ids, AssignmentValue::Expr(_expr)) => {
                    bail_bind_error!("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression");
                }
            }
        }

        // Check whether updating these columns is allowed.
        for &id_index in projects.keys() {
            if (table.table_catalog.pk())
                .iter()
                .any(|k| k.column_index == id_index)
            {
                return Err(ErrorCode::BindError(
                    "update modifying the PK column is unsupported".to_owned(),
                )
                .into());
            }
            if (table.table_catalog.generated_col_idxes()).contains(&id_index) {
                return Err(ErrorCode::BindError(
                    "update modifying the generated column is unsupported".to_owned(),
                )
                .into());
            }
            if cols_refed_by_generated_pk.contains(id_index) {
                return Err(ErrorCode::BindError(
                    "update modifying the column referenced by generated columns that are part of the primary key is not allowed".to_owned(),
                )
                .into());
            }

            let col = &table.table_catalog.columns()[id_index];
            if !col.can_dml() {
                bail_bind_error!("update modifying column `{}` is unsupported", col.name());
            }
        }

        let (returning_list, fields) = self.bind_returning_list(returning_items)?;
        let returning = !returning_list.is_empty();

        Ok(BoundUpdate {
            table_id,
            table_version_id,
            table_name,
            owner,
            table,
            selection,
            projects,
            exprs,
            returning_list,
            returning_schema: if returning {
                Some(Schema { fields })
            } else {
                None
            },
        })
    }
}