From 03eeabef18d228dec8f902aaf04837f8d364f54b Mon Sep 17 00:00:00 2001 From: Glauber Costa Date: Sat, 16 Aug 2025 08:03:57 -0500 Subject: [PATCH] fix pragma table_info for views We were not generating table_info for views. This PR fixes it. We were so far storing columns as strings with just their names - since this is all we needed - but we will move now to store Columns. We need to convert the names to Column anyway for table_info to work. --- core/schema.rs | 22 +++++++++++++++++----- core/translate/pragma.rs | 4 ++++ core/util.rs | 22 ++++++++++++++++------ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/core/schema.rs b/core/schema.rs index 4b0e6d924..90c511bfb 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -10,7 +10,7 @@ pub struct View { pub name: String, pub sql: String, pub select_stmt: ast::Select, - pub columns: Option>, + pub columns: Option>, } /// Type alias for regular views collection @@ -424,18 +424,30 @@ impl Schema { } Stmt::CreateView { view_name: _, - columns, + columns: column_names, select, .. } => { + // Extract actual columns from the SELECT statement + let view_columns = crate::util::extract_view_columns(&select, self); + + // If column names were provided in CREATE VIEW (col1, col2, ...), + // use them to rename the columns + let mut final_columns = view_columns; + if let Some(ref names) = column_names { + for (i, indexed_col) in names.iter().enumerate() { + if let Some(col) = final_columns.get_mut(i) { + col.name = Some(indexed_col.col_name.to_string()); + } + } + } + // Create regular view let view = View { name: name.to_string(), sql: sql.to_string(), select_stmt: *select, - columns: columns.map(|cols| { - cols.into_iter().map(|c| c.col_name.to_string()).collect() - }), + columns: Some(final_columns), }; self.add_view(view); } diff --git a/core/translate/pragma.rs b/core/translate/pragma.rs index 46d09180d..bac0309cd 100644 --- a/core/translate/pragma.rs +++ b/core/translate/pragma.rs @@ -451,6 +451,10 @@ fn query_pragma( } else if let Some(view_mutex) = schema.get_materialized_view(&name) { let view = view_mutex.lock().unwrap(); emit_columns_for_table_info(&mut program, &view.columns, base_reg); + } else if let Some(view) = schema.get_view(&name) { + if let Some(ref columns) = view.columns { + emit_columns_for_table_info(&mut program, columns, base_reg); + } } } let col_names = ["cid", "name", "type", "notnull", "dflt_value", "pk"]; diff --git a/core/util.rs b/core/util.rs index 04f314bff..446fae837 100644 --- a/core/util.rs +++ b/core/util.rs @@ -216,20 +216,30 @@ pub fn parse_schema_rows( } Stmt::CreateView { view_name: _, - columns, + columns: column_names, select, .. } => { + // Extract actual columns from the SELECT statement + let view_columns = extract_view_columns(&select, schema); + + // If column names were provided in CREATE VIEW (col1, col2, ...), + // use them to rename the columns + let mut final_columns = view_columns; + if let Some(ref names) = column_names { + for (i, indexed_col) in names.iter().enumerate() { + if let Some(col) = final_columns.get_mut(i) { + col.name = Some(indexed_col.col_name.to_string()); + } + } + } + // Create regular view let view = View { name: name.to_string(), sql: sql.to_string(), select_stmt: *select, - columns: columns.map(|cols| { - cols.into_iter() - .map(|c| c.col_name.to_string()) - .collect() - }), + columns: Some(final_columns), }; schema.add_view(view); }