From 7a1da051d0efc95bbac2e255bbd85e689b1e5e1d Mon Sep 17 00:00:00 2001 From: Nikita Sivukhin Date: Mon, 29 Sep 2025 11:36:12 +0400 Subject: [PATCH] remove usage of expr.to_string() and fix normalize util test --- core/schema.rs | 39 +++++++++++++++++++++++++-------------- core/util.rs | 5 ++--- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/core/schema.rs b/core/schema.rs index 44cd3f01c..5a4571bfd 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -25,8 +25,8 @@ use crate::util::{ module_args_from_sql, module_name_from_sql, type_from_name, IOExt, UnparsedFromSqlIndex, }; use crate::{ - contains_ignore_ascii_case, eq_ignore_ascii_case, match_ignore_ascii_case, Connection, - LimboError, MvCursor, MvStore, Pager, RefValue, SymbolTable, VirtualTable, + bail_parse_error, contains_ignore_ascii_case, eq_ignore_ascii_case, match_ignore_ascii_case, + Connection, LimboError, MvCursor, MvStore, Pager, RefValue, SymbolTable, VirtualTable, }; use crate::{util::normalize_ident, Result}; use core::fmt; @@ -1137,8 +1137,8 @@ pub fn create_table( Expr::Literal(Literal::String(value)) => { value.trim_matches('\'').to_owned() } - _ => { - todo!("Unsupported primary key expression"); + expr => { + bail_parse_error!("unsupported primary key expression: {}", expr) } }; primary_key_columns @@ -1156,16 +1156,24 @@ pub fn create_table( if conflict_clause.is_some() { unimplemented!("ON CONFLICT not implemented"); } + let mut unique_columns = Vec::with_capacity(columns.len()); + for column in columns { + match column.expr.as_ref() { + Expr::Id(id) => unique_columns.push(( + id.as_str().to_string(), + column.order.unwrap_or(SortOrder::Asc), + )), + Expr::Literal(Literal::String(value)) => unique_columns.push(( + value.trim_matches('\'').to_owned(), + column.order.unwrap_or(SortOrder::Asc), + )), + expr => { + bail_parse_error!("unsupported unique key expression: {}", expr) + } + } + } let unique_set = UniqueSet { - columns: columns - .iter() - .map(|column| { - ( - column.expr.as_ref().to_string(), - column.order.unwrap_or(SortOrder::Asc), - ) - }) - .collect(), + columns: unique_columns, is_primary_key: false, }; unique_sets.push(unique_set); @@ -1785,7 +1793,10 @@ impl Index { let index_name = normalize_ident(idx_name.name.as_str()); let mut index_columns = Vec::with_capacity(columns.len()); for col in columns.into_iter() { - let name = normalize_ident(&col.expr.to_string()); + let name = normalize_ident(match col.expr.as_ref() { + Expr::Id(col_name) | Expr::Name(col_name) => col_name.as_str(), + _ => crate::bail_parse_error!("cannot use expressions in CREATE INDEX"), + }); let Some((pos_in_table, _)) = table.get_column(&name) else { return Err(crate::LimboError::InternalError(format!( "Column {} is in index {} but not found in table {}", diff --git a/core/util.rs b/core/util.rs index 255f1f357..16309c915 100644 --- a/core/util.rs +++ b/core/util.rs @@ -1346,9 +1346,8 @@ pub mod tests { #[test] fn test_normalize_ident() { assert_eq!(normalize_ident("foo"), "foo"); - assert_eq!(normalize_ident("`foo`"), "foo"); - assert_eq!(normalize_ident("[foo]"), "foo"); - assert_eq!(normalize_ident("\"foo\""), "foo"); + assert_eq!(normalize_ident("FOO"), "foo"); + assert_eq!(normalize_ident("ὈΔΥΣΣΕΎΣ"), "ὀδυσσεύς"); } #[test]