use the correct integer PK column idx as the row-id alias

This commit is contained in:
gandeevanr
2024-08-04 18:32:34 -07:00
parent a765828206
commit 2b86f89d8d
3 changed files with 35 additions and 16 deletions

View File

@@ -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")

View File

@@ -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!(),

View File

@@ -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(