Implement std::cmp::PartialOrd for OwnedValue

This commit is contained in:
Bennett Clement
2024-07-16 08:32:10 +08:00
parent fdf77cee2b
commit 4d077cf3e5
4 changed files with 70 additions and 220 deletions

View File

@@ -570,25 +570,6 @@ fn translate_condition_expr(
expr: &ast::Expr,
target_jump: BranchOffset,
jump_if_true: bool, // if true jump to target on op == true, if false invert op
) -> Result<()> {
match expr {
ast::Expr::Binary(e1, op, e2) => match op {
ast::Operator::And => {
let _ = translate_condition_expr(program, select, e1, jump_target)?;
let _ = translate_condition_expr(program, select, e2, jump_target)?;
Ok(())
}
_ => translate_condition_expr_leaf(program, select, expr, jump_target),
},
_ => translate_condition_expr_leaf(program, select, expr, jump_target),
}
}
fn translate_condition_expr_leaf(
program: &mut ProgramBuilder,
select: &Select,
expr: &ast::Expr,
jump_target: BranchOffset,
) -> Result<()> {
match expr {
ast::Expr::Between { .. } => todo!(),

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

@@ -585,46 +585,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::Integer(lhs), OwnedValue::Float(rhs)) => {
if (*lhs as f64) == *rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
if *lhs == (*rhs as f64) {
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;
}
}
}
}
@@ -638,46 +607,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::Integer(lhs), OwnedValue::Float(rhs)) => {
if (*lhs as f64) != *rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
if *lhs != (*rhs as f64) {
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;
}
}
}
}
@@ -691,39 +629,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::Integer(lhs), OwnedValue::Float(rhs)) => {
if (*lhs as f64) < *rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
if *lhs < (*rhs as f64) {
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;
}
}
}
}
@@ -737,39 +651,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::Integer(lhs), OwnedValue::Float(rhs)) => {
if (*lhs as f64) <= *rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
if *lhs <= (*rhs as f64) {
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;
}
}
}
}
@@ -783,39 +673,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::Integer(lhs), OwnedValue::Float(rhs)) => {
if (*lhs as f64) > *rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
if *lhs > (*rhs as f64) {
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;
}
}
}
}
@@ -829,39 +695,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::Integer(lhs), OwnedValue::Float(rhs)) => {
if (*lhs as f64) >= *rhs {
state.pc = target_pc;
} else {
state.pc += 1;
}
}
(OwnedValue::Float(lhs), OwnedValue::Integer(rhs)) => {
if *lhs >= (*rhs as f64) {
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

@@ -108,19 +108,6 @@ do_execsql_test where-clause-lte {
select count(1) from users where id <= 2000;
} {2000}
do_execsql_test where-and {
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 != 'boots' and price <= 70;
} {6|shorts|70.0}
do_execsql_test where-clause-unary-true {
select count(1) from users where 1;
} {10000}
@@ -253,3 +240,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}