Escape character is ignored in LIKE function #1051

This commit is contained in:
l.gualtieri 2025-03-01 13:21:20 +01:00
parent 1de73b389e
commit 6449c79e93
3 changed files with 28 additions and 4 deletions

View file

@ -2159,7 +2159,7 @@ fn translate_like_base(
lhs,
op,
rhs,
escape: _,
escape,
..
} = expr
else {
@ -2167,10 +2167,22 @@ fn translate_like_base(
};
match op {
ast::LikeOperator::Like | ast::LikeOperator::Glob => {
let start_reg = program.alloc_registers(2);
let arg_count = if matches!(escape, Some(_)) { 3 } else { 2 };
let start_reg = program.alloc_registers(arg_count);
let mut constant_mask = 0;
translate_and_mark(program, referenced_tables, lhs, start_reg + 1, resolver)?;
let _ = translate_expr(program, referenced_tables, rhs, start_reg, resolver)?;
if arg_count == 3 {
if let Some(escape) = escape {
translate_and_mark(
program,
referenced_tables,
escape,
start_reg + 2,
resolver,
)?;
}
}
if matches!(rhs.as_ref(), ast::Expr::Literal(_)) {
program.mark_last_insn_constant();
constant_mask = 1;
@ -2186,7 +2198,7 @@ fn translate_like_base(
dest: target_register,
func: FuncCtx {
func: Func::Scalar(func),
arg_count: 2,
arg_count: arg_count,
},
});
}

View file

@ -1,6 +1,7 @@
use crate::vdbe::builder::CursorType;
use super::{Insn, InsnReference, OwnedValue, Program};
use crate::function::{Func, ScalarFunc};
use std::rc::Rc;
pub fn insn_to_str(
@ -936,7 +937,14 @@ pub fn insn_to_str(
*constant_mask,
*start_reg as i32,
*dest as i32,
OwnedValue::build_text(&func.func.to_string()),
{
let s = if matches!(&func.func, Func::Scalar(ScalarFunc::Like)) {
format!("like({})", func.arg_count)
} else {
func.func.to_string()
};
OwnedValue::build_text(&s)
},
0,
if func.arg_count == 0 {
format!("r[{}]=func()", dest)

View file

@ -74,6 +74,10 @@ John|Johnson
Stephen|Stephens
Robert|Roberts}
do_execsql_test where-like-another-column-prefix {
select count(*) from users where last_name like 'Pe#rry' escape '#';
} {19}
do_execsql_test where-like-impossible {
select * from products where 'foobar' like 'fooba';
} {}