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
implement builtin_string/instr_binary #4340
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -903,6 +903,28 @@ impl ScalarFunc { | |
Ok(Some(Cow::Borrowed(b""))) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn instr_binary<'a, 'b: 'a>( | ||
&'b self, | ||
ctx: &mut EvalContext, | ||
row: &'a [Datum], | ||
) -> Result<Option<i64>> { | ||
let s = try_opt!(self.children[0].eval_string_and_decode(ctx, row)); | ||
let substr = try_opt!(self.children[1].eval_string_and_decode(ctx, row)); | ||
if s.is_empty() { | ||
return Ok(Some(0)); | ||
} | ||
if substr.is_empty() { | ||
return Ok(Some(0)); | ||
} | ||
let s = String::from(s); | ||
let substr = String::from(substr); | ||
match s.find(&substr) { | ||
Some(x) => Ok(Some(1 + s[..x].chars().count() as i64)), | ||
None => Ok(Some(0)), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can extract the Ok(Some(match s.find(&substr) {
Some(x) => 1 + s[..x].chars().count() as i64,
None => 0,
})) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think no necessary. |
||
} | ||
} | ||
|
||
// when target_len is 0, return Some(0), means the pad function should return empty string | ||
|
@@ -1850,7 +1872,7 @@ mod tests { | |
Datum::Bytes("CAFÉ".as_bytes().to_vec()), | ||
Datum::Bytes("数据库".as_bytes().to_vec()), | ||
Datum::Bytes("قاعدة البيانات".as_bytes().to_vec()), | ||
Datum::Bytes( "НОЧЬ НА ОКРАИНЕ МОСКВЫ".as_bytes().to_vec()), | ||
Datum::Bytes("НОЧЬ НА ОКРАИНЕ МОСКВЫ".as_bytes().to_vec()), | ||
], | ||
Datum::Bytes( | ||
"忠犬ハチ公CAFÉ数据库قاعدة البياناتНОЧЬ НА ОКРАИНЕ МОСКВЫ" | ||
|
@@ -1885,6 +1907,7 @@ mod tests { | |
assert_eq!(res, exp); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_concat_ws() { | ||
let cases = vec![ | ||
|
@@ -1903,7 +1926,7 @@ mod tests { | |
Datum::Bytes("CAFÉ".as_bytes().to_vec()), | ||
Datum::Bytes("数据库".as_bytes().to_vec()), | ||
Datum::Bytes("قاعدة البيانات".as_bytes().to_vec()), | ||
Datum::Bytes( "НОЧЬ НА ОКРАИНЕ МОСКВЫ".as_bytes().to_vec()), | ||
Datum::Bytes("НОЧЬ НА ОКРАИНЕ МОСКВЫ".as_bytes().to_vec()), | ||
], | ||
Datum::Bytes( | ||
"忠犬ハチ公,CAFÉ,数据库,قاعدة البيانات,НОЧЬ НА ОКРАИНЕ МОСКВЫ" | ||
|
@@ -2373,6 +2396,7 @@ mod tests { | |
assert_eq!(got, exp); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_trim_3_args() { | ||
let tests = vec![ | ||
|
@@ -3259,4 +3283,43 @@ mod tests { | |
assert_eq!(got, exp); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_instr_binary() { | ||
let cases: Vec<(&str, &str, i64)> = vec![ | ||
("a", "abcdefg", 1), | ||
("0", "abcdefg", 0), | ||
("c", "abcdefg", 3), | ||
("F", "abcdefg", 0), | ||
("cd", "abcdefg", 3), | ||
(" ", "abcdefg", 0), | ||
("", "", 0), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the result is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AndreMouche Thanks for the review.The same problem in #4337 . |
||
("eFg", "abcdefg", 0), | ||
("deF", "abcdefg", 0), | ||
("字节", "a多字节", 3), | ||
("a", "a多字节", 1), | ||
("bar", "foobarbar", 4), | ||
("bAr", "foobarbar", 0), | ||
("好世", "你好世界", 2), | ||
]; | ||
|
||
for (substr, s, exp) in cases { | ||
let substr = Datum::Bytes(substr.as_bytes().to_vec()); | ||
let s = Datum::Bytes(s.as_bytes().to_vec()); | ||
let got = eval_func(ScalarFuncSig::InstrBinary, &[s, substr]).unwrap(); | ||
assert_eq!(got, Datum::I64(exp)) | ||
} | ||
|
||
let null_cases = vec![ | ||
(Datum::Null, Datum::Bytes(b"".to_vec()), Datum::Null), | ||
(Datum::Null, Datum::Bytes(b"foobar".to_vec()), Datum::Null), | ||
(Datum::Bytes(b"".to_vec()), Datum::Null, Datum::Null), | ||
(Datum::Bytes(b"bar".to_vec()), Datum::Null, Datum::Null), | ||
(Datum::Null, Datum::Null, Datum::Null), | ||
]; | ||
for (substr, s, exp) in null_cases { | ||
let got = eval_func(ScalarFuncSig::InstrBinary, &[substr, s]).unwrap(); | ||
assert_eq!(got, exp); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we needn't decode it since it is a binary.