Merge pull request #147 from benclmnt/feat/where-and

This commit is contained in:
Pekka Enberg
2024-07-16 21:35:01 +03:00
committed by GitHub
4 changed files with 90 additions and 98 deletions

View File

@@ -602,7 +602,15 @@ fn translate_condition_expr(
let lhs_reg = program.alloc_register();
let rhs_reg = program.alloc_register();
let _ = translate_expr(program, select, lhs, lhs_reg);
match lhs.as_ref() {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
}
let _ = translate_expr(program, select, rhs, rhs_reg);
match rhs.as_ref() {
ast::Expr::Literal(_) => program.mark_last_insn_constant(),
_ => {}
}
match op {
ast::Operator::Greater => {
if jump_if_true {

View File

@@ -66,6 +66,33 @@ pub enum AggContext {
GroupConcat(OwnedValue),
}
impl std::cmp::PartialOrd<OwnedValue> for OwnedValue {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
match (self, other) {
(OwnedValue::Integer(int_left), OwnedValue::Integer(int_right)) => {
int_left.partial_cmp(int_right)
}
(OwnedValue::Integer(int_left), OwnedValue::Float(float_right)) => {
float_right.partial_cmp(&(*int_left as f64))
}
(OwnedValue::Float(float_left), OwnedValue::Integer(int_right)) => {
float_left.partial_cmp(&(*int_right as f64))
}
(OwnedValue::Float(float_left), OwnedValue::Float(float_right)) => {
float_left.partial_cmp(float_right)
}
(OwnedValue::Text(text_left), OwnedValue::Text(text_right)) => {
text_left.partial_cmp(text_right)
}
(OwnedValue::Blob(blob_left), OwnedValue::Blob(blob_right)) => {
blob_left.partial_cmp(blob_right)
}
(OwnedValue::Null, OwnedValue::Null) => Some(std::cmp::Ordering::Equal),
_ => None,
}
}
}
impl std::ops::Add<OwnedValue> for OwnedValue {
type Output = OwnedValue;

View File

@@ -612,29 +612,15 @@ impl Program {
let rhs = *rhs;
let target_pc = *target_pc;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
if lhs == rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
if lhs == rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Text(lhs), OwnedValue::Text(rhs)) => {
if lhs == rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
(_, OwnedValue::Null) | (OwnedValue::Null, _) => {
state.pc = target_pc;
}
_ => {
todo!();
if &state.registers[lhs] == &state.registers[rhs] {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
}
}
@@ -648,29 +634,15 @@ impl Program {
let rhs = *rhs;
let target_pc = *target_pc;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
if lhs != rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
if lhs != rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Text(lhs), OwnedValue::Text(rhs)) => {
if lhs != rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
(_, OwnedValue::Null) | (OwnedValue::Null, _) => {
state.pc = target_pc;
}
_ => {
todo!();
if &state.registers[lhs] != &state.registers[rhs] {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
}
}
@@ -684,22 +656,15 @@ impl Program {
let rhs = *rhs;
let target_pc = *target_pc;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
if lhs < rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
if lhs < rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
(_, OwnedValue::Null) | (OwnedValue::Null, _) => {
state.pc = target_pc;
}
_ => {
todo!();
if &state.registers[lhs] < &state.registers[rhs] {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
}
}
@@ -713,22 +678,15 @@ impl Program {
let rhs = *rhs;
let target_pc = *target_pc;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
if lhs <= rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
if lhs <= rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
(_, OwnedValue::Null) | (OwnedValue::Null, _) => {
state.pc = target_pc;
}
_ => {
todo!();
if &state.registers[lhs] <= &state.registers[rhs] {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
}
}
@@ -742,22 +700,15 @@ impl Program {
let rhs = *rhs;
let target_pc = *target_pc;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
if lhs > rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
if lhs > rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
(_, OwnedValue::Null) | (OwnedValue::Null, _) => {
state.pc = target_pc;
}
_ => {
todo!();
if &state.registers[lhs] > &state.registers[rhs] {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
}
}
@@ -771,22 +722,15 @@ impl Program {
let rhs = *rhs;
let target_pc = *target_pc;
match (&state.registers[lhs], &state.registers[rhs]) {
(OwnedValue::Integer(lhs), OwnedValue::Integer(rhs)) => {
if lhs >= rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Float(rhs)) => {
if lhs >= rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
(_, OwnedValue::Null) | (OwnedValue::Null, _) => {
state.pc = target_pc;
}
_ => {
todo!();
if &state.registers[lhs] >= &state.registers[rhs] {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
}
}

View File

@@ -96,3 +96,16 @@ Jamie|88
Jamie|41
Jamie|73
}
do_execsql_test where-float-int {
select * from products where price > 50 and name != 'hat';
} {2|cap|82.0
5|sweatshirt|74.0
6|shorts|70.0
7|jeans|78.0
8|sneakers|82.0
11|accessories|81.0}
do_execsql_test where-multiple-and {
select * from products where price > 50 and name != 'sweatshirt' and price < 75;
} {6|shorts|70.0}