From e1ddf5ffcc83e2af28b33f543312a75af43aa94e Mon Sep 17 00:00:00 2001 From: pedrocarlo Date: Sun, 13 Apr 2025 19:17:34 -0300 Subject: [PATCH] Fix Unary Negate Operation on Blobs --- core/translate/expr.rs | 4 ++-- core/vdbe/execute.rs | 9 ++++++++- testing/math.test | 12 ++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/core/translate/expr.rs b/core/translate/expr.rs index e5404a644..909fb484b 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -1964,7 +1964,7 @@ pub fn translate_expr( Ok(target_register) } (UnaryOperator::Negative, _) => { - let value = -1; + let value = 0; let reg = program.alloc_register(); translate_expr(program, referenced_tables, expr, reg, resolver)?; @@ -1974,7 +1974,7 @@ pub fn translate_expr( dest: zero_reg, }); program.mark_last_insn_constant(); - program.emit_insn(Insn::Multiply { + program.emit_insn(Insn::Subtract { lhs: zero_reg, rhs: reg, dest: target_register, diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index bf4915159..a58ddec7a 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -5379,7 +5379,14 @@ pub fn exec_subtract(lhs: &OwnedValue, rhs: &OwnedValue) -> OwnedValue { (other, OwnedValue::Text(text)) => { exec_subtract(other, &cast_text_to_numeric(text.as_str())) } - _ => todo!(), + (other, OwnedValue::Blob(blob)) => { + let text = String::from_utf8_lossy(&blob); + exec_subtract(other, &cast_text_to_numeric(&text)) + } + (OwnedValue::Blob(blob), other) => { + let text = String::from_utf8_lossy(&blob); + exec_subtract(&cast_text_to_numeric(&text), other) + } }; match result { OwnedValue::Float(f) if f.is_nan() => OwnedValue::Null, diff --git a/testing/math.test b/testing/math.test index 9ebe33762..f81be8079 100755 --- a/testing/math.test +++ b/testing/math.test @@ -95,6 +95,18 @@ do_execsql_test subtract-agg-float-agg-int { SELECT sum(3.5) - sum(1) } {2.5} +do_execsql_test subtract-blob { + SELECT -x'11' +} {0} + +do_execsql_test subtract-blob-empty { + SELECT -x'' +} {0} + +do_execsql_test subtract-blob-charcter { + SELECT -'hi'; +} {0} + foreach {testnum lhs rhs ans} { 1 'a' 'a' 0 2 'a' 10 -10