Skip to content

Commit

Permalink
use the correct integer PK column idx as the row-id alias
Browse files Browse the repository at this point in the history
  • Loading branch information
gvos94 committed Aug 5, 2024
1 parent 84423d2 commit 8a34c16
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
19 changes: 18 additions & 1 deletion core/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
14 changes: 7 additions & 7 deletions core/translate/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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!(),
Expand Down
18 changes: 10 additions & 8 deletions core/translate/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8a34c16

Please sign in to comment.