llkv_runtime/runtime_context/
alter.rs

1//! ALTER TABLE operations for RuntimeContext.
2//!
3//! This module contains all ALTER TABLE operations:
4//! - RENAME COLUMN
5//! - ALTER COLUMN TYPE
6//! - DROP COLUMN
7
8use crate::canonical_table_name;
9use llkv_executor::translation::sql_type_to_arrow;
10use llkv_result::{Error, Result};
11use llkv_storage::pager::Pager;
12use simd_r_drive_entry_handle::EntryHandle;
13
14use super::RuntimeContext;
15
16impl<P> RuntimeContext<P>
17where
18    P: Pager<Blob = EntryHandle> + Send + Sync,
19{
20    /// Renames a column in a table by delegating to the catalog layer.
21    pub fn rename_column(
22        &self,
23        table_name: &str,
24        old_column_name: &str,
25        new_column_name: &str,
26    ) -> Result<()> {
27        let (_, canonical_table) = canonical_table_name(table_name)?;
28
29        // Get table ID
30        let table_id = self
31            .catalog
32            .table_id(&canonical_table)
33            .ok_or_else(|| Error::CatalogError(format!("table '{}' not found", table_name)))?;
34
35        // Delegate to catalog layer
36        self.catalog_service
37            .rename_column(table_id, old_column_name, new_column_name)?;
38
39        // Invalidate cached executor table so subsequent operations see updated schema.
40        self.remove_table_entry(&canonical_table);
41
42        Ok(())
43    }
44
45    /// Alters the data type of a column by delegating to the catalog layer.
46    pub fn alter_column_type(
47        &self,
48        table_name: &str,
49        column_name: &str,
50        new_data_type_sql: &str,
51    ) -> Result<()> {
52        let (_, canonical_table) = canonical_table_name(table_name)?;
53
54        // Get table ID
55        let table_id = self
56            .catalog
57            .table_id(&canonical_table)
58            .ok_or_else(|| Error::CatalogError(format!("table '{}' not found", table_name)))?;
59
60        // Parse SQL type string to Arrow DataType
61        let arrow_type = sql_type_to_arrow(new_data_type_sql)?;
62
63        // Delegate to catalog layer
64        self.catalog_service
65            .alter_column_type(table_id, column_name, &arrow_type)?;
66
67        // Invalidate cached executor table so subsequent operations see updated schema.
68        self.remove_table_entry(&canonical_table);
69
70        Ok(())
71    }
72
73    /// Drops a column from a table by delegating to the catalog layer.
74    pub fn drop_column(&self, table_name: &str, column_name: &str) -> Result<()> {
75        let (_, canonical_table) = canonical_table_name(table_name)?;
76
77        // Get table ID
78        let table_id = self
79            .catalog
80            .table_id(&canonical_table)
81            .ok_or_else(|| Error::CatalogError(format!("table '{}' not found", table_name)))?;
82
83        // Delegate to catalog layer
84        self.catalog_service.drop_column(table_id, column_name)?;
85
86        // Invalidate cached executor table so subsequent operations see updated schema.
87        self.remove_table_entry(&canonical_table);
88
89        Ok(())
90    }
91}