Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support subtract in translate_expr() (not in condition expressions yet) #403

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/translate/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,13 @@ pub fn translate_expr(
dest: target_register,
});
}
ast::Operator::Subtract => {
program.emit_insn(Insn::Subtract {
lhs: e1_reg,
rhs: e2_reg,
dest: target_register,
});
}
ast::Operator::Multiply => {
program.emit_insn(Insn::Multiply {
lhs: e1_reg,
Expand Down
9 changes: 9 additions & 0 deletions core/vdbe/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ pub fn insn_to_str(
0,
format!("r[{}]=r[{}]+r[{}]", dest, lhs, rhs),
),
Insn::Subtract { lhs, rhs, dest } => (
"Subtract",
*lhs as i32,
*rhs as i32,
*dest as i32,
OwnedValue::Text(Rc::new("".to_string())),
0,
format!("r[{}]=r[{}]-r[{}]", dest, lhs, rhs),
),
Insn::Multiply { lhs, rhs, dest } => (
"Multiply",
*lhs as i32,
Expand Down
60 changes: 60 additions & 0 deletions core/vdbe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ pub enum Insn {
rhs: usize,
dest: usize,
},
// Subtract rhs from lhs and store in dest
Subtract {
lhs: usize,
rhs: usize,
dest: usize,
},
// Multiply two registers and store the result in a third register.
Multiply {
lhs: usize,
Expand Down Expand Up @@ -645,6 +651,60 @@ impl Program {
}
state.pc += 1;
}
Insn::Subtract { lhs, rhs, dest } => {
let lhs = *lhs;
let rhs = *rhs;
let dest = *dest;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
state.registers[dest] = OwnedValue::Integer(lhs - rhs);
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
state.registers[dest] = OwnedValue::Float(lhs - rhs);
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs))
| (OwnedValue::Integer(rhs), OwnedValue::Float(lhs)) => {
state.registers[dest] = OwnedValue::Float(lhs - *rhs as f64);
}
(OwnedValue::Null, _) | (_, OwnedValue::Null) => {
state.registers[dest] = OwnedValue::Null;
}
(OwnedValue::Agg(aggctx), other) | (other, OwnedValue::Agg(aggctx)) => {
match other {
OwnedValue::Null => {
state.registers[dest] = OwnedValue::Null;
}
OwnedValue::Integer(i) => match aggctx.final_value() {
OwnedValue::Float(acc) => {
state.registers[dest] = OwnedValue::Float(acc - *i as f64);
}
OwnedValue::Integer(acc) => {
state.registers[dest] = OwnedValue::Integer(acc - i);
}
_ => {
todo!("{:?}", aggctx);
}
},
OwnedValue::Float(f) => match aggctx.final_value() {
OwnedValue::Float(acc) => {
state.registers[dest] = OwnedValue::Float(acc - f);
}
OwnedValue::Integer(acc) => {
state.registers[dest] = OwnedValue::Float(*acc as f64 - f);
}
_ => {
todo!("{:?}", aggctx);
}
},
rest => unimplemented!("{:?}", rest),
}
}
others => {
todo!("{:?}", others);
}
}
state.pc += 1;
}
Insn::Multiply { lhs, rhs, dest } => {
let lhs = *lhs;
let rhs = *rhs;
Expand Down
4 changes: 4 additions & 0 deletions testing/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ do_execsql_test select-add {
select u.age + 1 from users u where u.age = 91 limit 1;
} {92}

do_execsql_test select-subtract {
select u.age - 1 from users u where u.age = 91 limit 1;
} {90}

do_execsql_test case-insensitive-columns {
select u.aGe + 1 from USERS u where U.AGe = 91 limit 1;
} {92}
Expand Down
Loading