Skip to main content

quill_sql/plan/logical_planner/
plan_update.rs

1use crate::error::{QuillSQLError, QuillSQLResult};
2use crate::plan::logical_plan::{LogicalPlan, Update};
3use crate::plan::LogicalPlanner;
4use std::collections::HashMap;
5
6impl<'a> LogicalPlanner<'a> {
7    pub fn plan_update(
8        &self,
9        table: &sqlparser::ast::TableWithJoins,
10        assignments: &[sqlparser::ast::Assignment],
11        selection: &Option<sqlparser::ast::Expr>,
12    ) -> QuillSQLResult<LogicalPlan> {
13        let table_ref = match &table.relation {
14            sqlparser::ast::TableFactor::Table { name, .. } => self.bind_table_name(name)?,
15            _ => {
16                return Err(QuillSQLError::Plan(format!(
17                    "table {} is not supported",
18                    table
19                )))
20            }
21        };
22
23        let table_schema = self.context.catalog.table_schema(&table_ref)?;
24
25        let mut assignment_map = HashMap::new();
26        for assign in assignments {
27            let column_ident = assign.id.first().ok_or(QuillSQLError::Plan(format!(
28                "Assignment {} is not supported",
29                assign
30            )))?;
31            let column_name = column_ident.value.to_ascii_lowercase();
32            let value = self.bind_expr(&assign.value)?;
33            assignment_map.insert(column_name, value);
34        }
35
36        let selection = match selection {
37            Some(e) => Some(self.bind_expr(e)?),
38            None => None,
39        };
40
41        Ok(LogicalPlan::Update(Update {
42            table: table_ref,
43            table_schema,
44            assignments: assignment_map,
45            selection,
46        }))
47    }
48}