Skip to content

Commit

Permalink
fix(expr): fix lpad and rpad on empty fill string (risingwavelabs…
Browse files Browse the repository at this point in the history
…#8970)

Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 authored Apr 4, 2023
1 parent 368ed93 commit 6687739
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/expr/src/vector_op/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ pub fn lpad(s: &str, length: i32, writer: &mut dyn Write) {
/// select lpad('hi', 5, 'xy');
/// ----
/// xyxhi
///
/// query T
/// select lpad('hi', 5, '');
/// ----
/// hi
/// ```
#[function("lpad(varchar, int32, varchar) -> varchar")]
pub fn lpad_fill(s: &str, length: i32, fill: &str, writer: &mut dyn Write) {
Expand All @@ -122,6 +127,8 @@ pub fn lpad_fill(s: &str, length: i32, fill: &str, writer: &mut dyn Write) {
for c in s.chars().take(length as usize) {
write!(writer, "{c}").unwrap();
}
} else if fill_len == 0 {
write!(writer, "{s}").unwrap();
} else {
let mut remaining_length = length as usize - s_len;
while remaining_length >= fill_len {
Expand Down Expand Up @@ -177,6 +184,12 @@ pub fn rpad(s: &str, length: i32, writer: &mut dyn Write) {
/// select rpad('abcdef', 3, '0');
/// ----
/// abc
///
/// query T
/// select rpad('hi', 5, '');
/// ----
/// hi
/// ```
#[function("rpad(varchar, int32, varchar) -> varchar")]
pub fn rpad_fill(s: &str, length: i32, fill: &str, writer: &mut dyn Write) {
let s_len = s.chars().count();
Expand All @@ -190,6 +203,8 @@ pub fn rpad_fill(s: &str, length: i32, fill: &str, writer: &mut dyn Write) {
for c in s.chars().take(length as usize) {
write!(writer, "{c}").unwrap();
}
} else if fill_len == 0 {
write!(writer, "{s}").unwrap();
} else {
write!(writer, "{s}").unwrap();
let mut remaining_length = length as usize - s_len;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/regress/data/sql/strings.sql
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,13 @@ SELECT lpad('hi', 5, 'xy');
SELECT lpad('hi', 5);
SELECT lpad('hi', -5, 'xy');
SELECT lpad('hello', 2);
--@ SELECT lpad('hi', 5, '');
SELECT lpad('hi', 5, '');

SELECT rpad('hi', 5, 'xy');
SELECT rpad('hi', 5);
SELECT rpad('hi', -5, 'xy');
SELECT rpad('hello', 2);
--@ SELECT rpad('hi', 5, '');
SELECT rpad('hi', 5, '');

SELECT ltrim('zzzytrim', 'xyz');

Expand Down

0 comments on commit 6687739

Please sign in to comment.