quill_sql/execution/physical_plan/
create_index.rs

1use crate::catalog::{SchemaRef, EMPTY_SCHEMA_REF};
2use crate::error::QuillSQLError;
3use crate::expression::{ColumnExpr, Expr};
4use crate::plan::logical_plan::OrderByExpr;
5use crate::storage::tuple::Tuple;
6use crate::utils::table_ref::TableReference;
7use crate::{
8    error::QuillSQLResult,
9    execution::{ExecutionContext, VolcanoExecutor},
10};
11use std::sync::Arc;
12
13#[derive(Debug, derive_new::new)]
14pub struct PhysicalCreateIndex {
15    pub name: String,
16    pub table: TableReference,
17    pub table_schema: SchemaRef,
18    pub columns: Vec<OrderByExpr>,
19}
20
21impl VolcanoExecutor for PhysicalCreateIndex {
22    fn next(&self, context: &mut ExecutionContext) -> QuillSQLResult<Option<Tuple>> {
23        let mut key_indices = vec![];
24        for col in self.columns.iter() {
25            match col.expr.as_ref() {
26                Expr::Column(ColumnExpr { name, .. }) => {
27                    key_indices.push(self.table_schema.index_of(None, name)?);
28                }
29                _ => {
30                    return Err(QuillSQLError::Execution(format!(
31                        "The expr should be column instead of {}",
32                        col.expr
33                    )))
34                }
35            }
36        }
37        let key_schema = Arc::new(self.table_schema.project(&key_indices)?);
38        context
39            .catalog
40            .create_index(self.name.clone(), &self.table, key_schema)?;
41        Ok(None)
42    }
43    fn output_schema(&self) -> SchemaRef {
44        EMPTY_SCHEMA_REF.clone()
45    }
46}
47
48impl std::fmt::Display for PhysicalCreateIndex {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        write!(f, "CreateIndex: {}", self.name)
51    }
52}