Skip to main content

quill_sql/plan/logical_planner/
plan_create_index.rs

1use crate::error::{QuillSQLError, QuillSQLResult};
2use crate::plan::logical_plan::{CreateIndex, LogicalPlan};
3
4use super::LogicalPlanner;
5
6impl<'a> LogicalPlanner<'a> {
7    pub fn plan_create_index(
8        &self,
9        index_name: &sqlparser::ast::ObjectName,
10        table_name: &sqlparser::ast::ObjectName,
11        using: Option<&sqlparser::ast::Ident>,
12        columns: &[sqlparser::ast::OrderByExpr],
13    ) -> QuillSQLResult<LogicalPlan> {
14        let index_name = index_name.0.first().map_or(
15            Err(QuillSQLError::Plan(format!(
16                "Index name {index_name} is not expected"
17            ))),
18            |ident| Ok(ident.value.clone()),
19        )?;
20        let table = self.bind_table_name(table_name)?;
21        let mut columns_expr = vec![];
22        for col in columns.iter() {
23            let col_expr = self.bind_order_by_expr(col)?;
24            columns_expr.push(col_expr);
25        }
26        validate_index_engine(using)?;
27        let table_schema = self.context.catalog.table_schema(&table)?;
28        Ok(LogicalPlan::CreateIndex(CreateIndex {
29            index_name,
30            table,
31            table_schema,
32            columns: columns_expr,
33        }))
34    }
35}
36
37fn validate_index_engine(using: Option<&sqlparser::ast::Ident>) -> QuillSQLResult<()> {
38    let Some(using) = using else {
39        return Ok(());
40    };
41    match using.value.to_ascii_lowercase().as_str() {
42        "holt" => Ok(()),
43        other => Err(QuillSQLError::NotSupport(format!(
44            "index backend {other} is not supported; only USING HOLT is supported"
45        ))),
46    }
47}