From 2f4d76ec6ddb77759fcbaaaa68e995dceb6c50e2 Mon Sep 17 00:00:00 2001 From: Nikita Sivukhin Date: Thu, 25 Sep 2025 12:44:32 +0400 Subject: [PATCH] remove pattern matching over Name::Quoted --- core/incremental/view.rs | 4 +--- core/schema.rs | 4 ++-- core/translate/emitter.rs | 2 +- core/translate/expr.rs | 4 ++-- core/translate/index.rs | 13 +++++------- core/translate/insert.rs | 2 +- core/translate/logical.rs | 4 +--- core/translate/upsert.rs | 27 +++++++++---------------- core/vdbe/execute.rs | 11 +++++----- sql_generation/model/query/predicate.rs | 11 +++++----- 10 files changed, 34 insertions(+), 48 deletions(-) diff --git a/core/incremental/view.rs b/core/incremental/view.rs index 44692aa8a..e7b85b4b4 100644 --- a/core/incremental/view.rs +++ b/core/incremental/view.rs @@ -448,9 +448,7 @@ impl IncrementalView { // Store the alias mapping if there is an alias if let Some(alias_enum) = alias { let alias_name = match alias_enum { - ast::As::As(name) | ast::As::Elided(name) => match name { - ast::Name::Ident(s) | ast::Name::Quoted(s) => s, - }, + ast::As::As(name) | ast::As::Elided(name) => name.as_str(), }; aliases.insert(alias_name.to_string(), table_name.to_string()); } diff --git a/core/schema.rs b/core/schema.rs index 13bd620fb..d10a709db 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -37,7 +37,7 @@ use std::sync::Mutex; use tracing::trace; use turso_parser::ast::{self, ColumnDefinition, Expr, Literal, SortOrder, TableOptions}; use turso_parser::{ - ast::{Cmd, CreateTableBody, Name, ResultColumn, Stmt}, + ast::{Cmd, CreateTableBody, ResultColumn, Stmt}, parser::Parser, }; @@ -1927,7 +1927,7 @@ impl Index { match e { Expr::Literal(_) | Expr::RowId { .. } => {} // Unqualified identifier: must be a column of the target table or ROWID - Expr::Id(Name::Ident(n)) | Expr::Id(Name::Quoted(n)) => { + Expr::Id(n) => { let n = n.as_str(); if !ROWID_STRS.iter().any(|s| s.eq_ignore_ascii_case(n)) && !has_col(n) { ok = false; diff --git a/core/translate/emitter.rs b/core/translate/emitter.rs index 48cb4a45e..a91d81439 100644 --- a/core/translate/emitter.rs +++ b/core/translate/emitter.rs @@ -1844,7 +1844,7 @@ fn rewrite_where_for_update_registers( } } } - Expr::Id(ast::Name::Ident(name)) | Expr::Id(ast::Name::Quoted(name)) => { + Expr::Id(name) => { let normalized = normalize_ident(name.as_str()); if ROWID_STRS .iter() diff --git a/core/translate/expr.rs b/core/translate/expr.rs index dddfec85e..d18eb1392 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -3302,10 +3302,10 @@ pub fn bind_and_rewrite_expr<'a>( top_level_expr, &mut |expr: &mut ast::Expr| -> Result { match expr { - ast::Expr::Id(ast::Name::Ident(n)) if n.eq_ignore_ascii_case("true") => { + ast::Expr::Id(n) if n.as_str().eq_ignore_ascii_case("true") => { *expr = ast::Expr::Literal(ast::Literal::Numeric("1".to_string())); } - ast::Expr::Id(ast::Name::Ident(n)) if n.eq_ignore_ascii_case("false") => { + ast::Expr::Id(n) if n.as_str().eq_ignore_ascii_case("false") => { *expr = ast::Expr::Literal(ast::Literal::Numeric("0".to_string())); } // Rewrite anonymous variables in encounter order. diff --git a/core/translate/index.rs b/core/translate/index.rs index e78b9dc02..4273f00e8 100644 --- a/core/translate/index.rs +++ b/core/translate/index.rs @@ -21,7 +21,7 @@ use crate::{ insn::{IdxInsertFlags, Insn, RegisterOrLiteral}, }, }; -use turso_parser::ast::{self, Expr, SortOrder, SortedColumn}; +use turso_parser::ast::{Expr, SortOrder, SortedColumn}; use super::schema::{emit_schema_entry, SchemaEntryType, SQLITE_TABLEID}; @@ -350,16 +350,13 @@ fn resolve_sorted_columns<'a>( ) -> crate::Result> { let mut resolved = Vec::with_capacity(cols.len()); for sc in cols { - let ident = normalize_ident(match sc.expr.as_ref() { + let ident = match sc.expr.as_ref() { // SQLite supports indexes on arbitrary expressions, but we don't (yet). // See "How to use indexes on expressions" in https://www.sqlite.org/expridx.html - Expr::Id(ast::Name::Ident(col_name)) - | Expr::Id(ast::Name::Quoted(col_name)) - | Expr::Name(ast::Name::Ident(col_name)) - | Expr::Name(ast::Name::Quoted(col_name)) => col_name, + Expr::Id(col_name) | Expr::Name(col_name) => col_name.as_str(), _ => crate::bail_parse_error!("Error: cannot use expressions in CREATE INDEX"), - }); - let Some(col) = table.get_column(&ident) else { + }; + let Some(col) = table.get_column(ident) else { crate::bail_parse_error!( "Error: column '{ident}' does not exist in table '{}'", table.name diff --git a/core/translate/insert.rs b/core/translate/insert.rs index dae59ac3f..55d0bfc9d 100644 --- a/core/translate/insert.rs +++ b/core/translate/insert.rs @@ -1740,7 +1740,7 @@ pub fn rewrite_partial_index_where( &mut |e: &mut ast::Expr| -> crate::Result { match e { // NOTE: should not have ANY Expr::Columns bound to the expr - Expr::Id(ast::Name::Ident(name)) | Expr::Id(ast::Name::Quoted(name)) => { + Expr::Id(name) => { let normalized = normalize_ident(name.as_str()); if let Some(reg) = col_reg(&normalized) { *e = Expr::Register(reg); diff --git a/core/translate/logical.rs b/core/translate/logical.rs index 4c27a506b..aefbbc862 100644 --- a/core/translate/logical.rs +++ b/core/translate/logical.rs @@ -415,9 +415,7 @@ impl<'a> LogicalPlanBuilder<'a> { // Convert Name to String fn name_to_string(name: &ast::Name) -> String { - match name { - ast::Name::Ident(s) | ast::Name::Quoted(s) => s.clone(), - } + name.as_str().to_string() } // Build a SELECT statement diff --git a/core/translate/upsert.rs b/core/translate/upsert.rs index 9755802c8..040a4b88f 100644 --- a/core/translate/upsert.rs +++ b/core/translate/upsert.rs @@ -77,27 +77,22 @@ pub struct ConflictTarget { // Extract `(column, optional_collate)` from an ON CONFLICT target Expr. // Accepts: Id, Qualified, DoublyQualified, Parenthesized, Collate fn extract_target_key(e: &ast::Expr) -> Option { - use ast::Name::{Ident, Quoted}; match e { ast::Expr::Collate(inner, c) => { let mut tk = extract_target_key(inner.as_ref())?; - let cstr = match c { - Ident(s) | Quoted(s) => s.as_str(), - }; + let cstr = c.as_str(); tk.collate = Some(cstr.to_ascii_lowercase()); Some(tk) } ast::Expr::Parenthesized(v) if v.len() == 1 => extract_target_key(&v[0]), - ast::Expr::Id(Ident(name)) | ast::Expr::Id(Quoted(name)) => Some(ConflictTarget { - col_name: normalize_ident(name), + ast::Expr::Id(name) => Some(ConflictTarget { + col_name: normalize_ident(name.as_str()), collate: None, }), // t.a or db.t.a: accept ident or quoted in the column position ast::Expr::Qualified(_, col) | ast::Expr::DoublyQualified(_, _, col) => { - let cname = match col { - Ident(s) | Quoted(s) => s.as_str(), - }; + let cname = col.as_str(); Some(ConflictTarget { col_name: normalize_ident(cname), collate: None, @@ -192,20 +187,19 @@ fn index_keys(idx: &Index) -> Vec { /// Columns referenced by the partial WHERE (empty if none). fn partial_index_cols(idx: &Index, table: &Table) -> HashSet { - use ast::{Expr, Name}; + use ast::Expr; let Some(expr) = &idx.where_clause else { return HashSet::new(); }; let mut out = HashSet::new(); let _ = walk_expr(expr, &mut |e: &ast::Expr| -> crate::Result { match e { - Expr::Id(Name::Ident(n) | Name::Quoted(n)) => { + Expr::Id(n) => { if let Some((i, _)) = table.get_column_by_name(&normalize_ident(n.as_str())) { out.insert(i); } } - Expr::Qualified(ns, Name::Ident(c) | Name::Quoted(c)) - | Expr::DoublyQualified(_, ns, Name::Ident(c) | Name::Quoted(c)) => { + Expr::Qualified(ns, c) | Expr::DoublyQualified(_, ns, c) => { // Only count columns that belong to this table let nsn = normalize_ident(ns.as_str()); let tname = normalize_ident(table.get_name()); @@ -891,7 +885,7 @@ fn rewrite_expr_to_registers( insertion: Option<&Insertion>, allow_excluded: bool, ) -> crate::Result { - use ast::{Expr, Name}; + use ast::Expr; let table_name_norm = table_name.map(normalize_ident); // Map a column name to a register within the row image at `base_start`. @@ -911,8 +905,7 @@ fn rewrite_expr_to_registers( e, &mut |expr: &mut ast::Expr| -> crate::Result { match expr { - Expr::Qualified(ns, Name::Ident(c) | Name::Quoted(c)) - | Expr::DoublyQualified(_, ns, Name::Ident(c) | Name::Quoted(c)) => { + Expr::Qualified(ns, c) | Expr::DoublyQualified(_, ns, c) => { let ns = normalize_ident(ns.as_str()); let c = normalize_ident(c.as_str()); // Handle EXCLUDED.* if enabled @@ -940,7 +933,7 @@ fn rewrite_expr_to_registers( } } // Unqualified id -> row image (CURRENT/NEW depending on caller) - Expr::Id(Name::Ident(name)) | Expr::Id(Name::Quoted(name)) => { + Expr::Id(name) => { if let Some(r) = col_reg_from_row_image(&normalize_ident(name.as_str())) { *expr = Expr::Register(r); } diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index 4b1bdd4d1..bd1bb227b 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -73,7 +73,7 @@ use super::{ }; use parking_lot::RwLock; use rand::{thread_rng, Rng}; -use turso_parser::ast; +use turso_parser::ast::{self, Name}; use turso_parser::parser::Parser; use super::{ @@ -5320,11 +5320,12 @@ pub fn op_function( for column in &mut columns { match column.expr.as_mut() { - ast::Expr::Id(ast::Name::Ident(id)) - | ast::Expr::Id(ast::Name::Quoted(id)) - if normalize_ident(id) == rename_from => + ast::Expr::Id(id) + if normalize_ident(id.as_str()) == rename_from => { - *id = column_def.col_name.as_str().to_owned(); + *id = Name::Ident( + column_def.col_name.as_str().to_owned(), + ); } _ => {} } diff --git a/sql_generation/model/query/predicate.rs b/sql_generation/model/query/predicate.rs index 50a72fee4..38cec3f5d 100644 --- a/sql_generation/model/query/predicate.rs +++ b/sql_generation/model/query/predicate.rs @@ -98,17 +98,16 @@ pub fn expr_to_value( table: &T, ) -> Option { match expr { - ast::Expr::DoublyQualified(_, _, ast::Name::Ident(col_name)) - | ast::Expr::DoublyQualified(_, _, ast::Name::Quoted(col_name)) - | ast::Expr::Qualified(_, ast::Name::Ident(col_name)) - | ast::Expr::Qualified(_, ast::Name::Quoted(col_name)) - | ast::Expr::Id(ast::Name::Ident(col_name)) => { + ast::Expr::DoublyQualified(_, _, col_name) + | ast::Expr::Qualified(_, col_name) + | ast::Expr::Id(col_name) => { let columns = table.columns().collect::>(); + let col_name = col_name.as_str(); assert_eq!(row.len(), columns.len()); columns .iter() .zip(row.iter()) - .find(|(column, _)| column.column.name == *col_name) + .find(|(column, _)| column.column.name == col_name) .map(|(_, value)| value) .cloned() }