From 2b86f89d8dc98aaaa95aa3f2a7d9f286d0ff23fa Mon Sep 17 00:00:00 2001 From: gandeevanr Date: Sun, 4 Aug 2024 18:32:34 -0700 Subject: [PATCH] use the correct integer PK column idx as the row-id alias --- core/schema.rs | 19 ++++++++++++++++++- core/translate/expr.rs | 14 +++++++------- core/translate/insert.rs | 18 ++++++++++-------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/core/schema.rs b/core/schema.rs index b910a81e2..851fab6f1 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -43,6 +43,13 @@ impl Table { matches!(self, Table::Pseudo(_)) } + pub fn get_rowid_alias_column(&self) -> Option<(usize, &Column)> { + match self { + Table::BTree(table) => table.get_rowid_alias_column(), + Table::Pseudo(_) => None, + } + } + pub fn column_is_rowid_alias(&self, col: &Column) -> bool { match self { Table::BTree(table) => table.column_is_rowid_alias(col), @@ -112,6 +119,16 @@ pub struct BTreeTable { } impl BTreeTable { + pub fn get_rowid_alias_column(&self) -> Option<(usize, &Column)> { + if self.primary_key_column_names.len() == 1 { + let (idx, col) = self.get_column(&self.primary_key_column_names[0]).unwrap(); + if self.column_is_rowid_alias(col) { + return Some((idx, col)); + } + } + None + } + pub fn column_is_rowid_alias(&self, col: &Column) -> bool { col.primary_key && col.ty == Type::Integer @@ -230,7 +247,7 @@ fn create_table( let name = col_name.0.to_string(); let ty = match col_def.col_type { Some(data_type) => { - let type_name = data_type.name.as_str(); + let type_name = data_type.name.as_str().to_uppercase(); if type_name.contains("INTEGER") { Type::Integer } else if type_name.contains("CHAR") diff --git a/core/translate/expr.rs b/core/translate/expr.rs index 10217f9a0..b8cd71da9 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -393,9 +393,9 @@ pub fn translate_expr( ast::Expr::FunctionCallStar { .. } => todo!(), ast::Expr::Id(ident) => { // let (idx, col) = table.unwrap().get_column(&ident.0).unwrap(); - let (idx, col_type, cursor_id, is_primary_key) = + let (idx, col_type, cursor_id, is_rowid_alias) = resolve_ident_table(program, &ident.0, select, cursor_hint)?; - if is_primary_key { + if is_rowid_alias { program.emit_insn(Insn::RowId { cursor_id, dest: target_register, @@ -648,12 +648,12 @@ pub fn resolve_ident_table( .iter() .enumerate() .find(|(_, col)| col.name == *ident) - .map(|(idx, col)| (idx, col.ty, col.primary_key)); + .map(|(idx, col)| (idx, col.ty, table.column_is_rowid_alias(col))); let mut idx; let mut col_type; - let mut is_primary_key; + let mut is_rowid_alias; if res.is_some() { - (idx, col_type, is_primary_key) = res.unwrap(); + (idx, col_type, is_rowid_alias) = res.unwrap(); // overwrite if cursor hint is provided if let Some(cursor_hint) = cursor_hint { let cols = &program.cursor_ref[cursor_hint].1; @@ -665,11 +665,11 @@ pub fn resolve_ident_table( }) { idx = res.0; col_type = res.1.ty; - is_primary_key = res.1.primary_key; + is_rowid_alias = table.column_is_rowid_alias(res.1); } } let cursor_id = program.resolve_cursor_id(&join.identifier, cursor_hint); - found.push((idx, col_type, cursor_id, is_primary_key)); + found.push((idx, col_type, cursor_id, is_rowid_alias)); } } Table::Pseudo(_) => todo!(), diff --git a/core/translate/insert.rs b/core/translate/insert.rs index 38affd594..8d525d719 100644 --- a/core/translate/insert.rs +++ b/core/translate/insert.rs @@ -128,15 +128,17 @@ pub fn translate_insert( ); if table.has_rowid() { - let key_reg = column_registers_start + 1; let row_id_reg = column_registers_start; - // copy key to rowid - program.emit_insn(Insn::Copy { - src_reg: key_reg, - dst_reg: row_id_reg, - amount: 0, - }); - program.emit_insn(Insn::SoftNull { reg: key_reg }); + if let Some(rowid_alias_column) = table.get_rowid_alias_column() { + let key_reg = column_registers_start + 1 + rowid_alias_column.0; + // copy key to rowid + program.emit_insn(Insn::Copy { + src_reg: key_reg, + dst_reg: row_id_reg, + amount: 0, + }); + program.emit_insn(Insn::SoftNull { reg: key_reg }); + } let notnull_label = program.allocate_label(); program.emit_insn_with_label_dependency(