Skip to content

Commit

Permalink
add context to array_to_string
Browse files Browse the repository at this point in the history
Signed-off-by: Runji Wang <[email protected]>
  • Loading branch information
wangrunji0408 committed Sep 13, 2023
1 parent a486d63 commit e65fe2c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/common/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ impl DataType {
///
/// ```
/// use risingwave_common::types::DataType::*;
/// assert_eq!(List(Box::new(Int32)).unnest_list(), Int32);
/// assert_eq!(List(Box::new(List(Box::new(Int32)))).unnest_list(), Int32);
/// assert_eq!(List(Box::new(Int32)).unnest_list(), &Int32);
/// assert_eq!(List(Box::new(List(Box::new(Int32)))).unnest_list(), &Int32);
/// ```
pub fn unnest_list(&self) -> Self {
pub fn unnest_list(&self) -> &Self {
match self {
DataType::List(inner) => inner.unnest_list(),
_ => self.clone(),
_ => self,
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/expr/src/vector_op/array_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use risingwave_common::array::*;
use risingwave_common::types::ToText;
use risingwave_expr_macro::function;

use crate::expr::Context;

/// Converts each array element to its text representation, and concatenates those
/// separated by the delimiter string. If `null_string` is given and is not NULL,
/// then NULL array entries are represented by that string; otherwise, they are omitted.
Expand Down Expand Up @@ -81,7 +83,8 @@ use risingwave_expr_macro::function;
/// one,*,three,four
/// ```
#[function("array_to_string(list, varchar) -> varchar")]
fn array_to_string(array: ListRef<'_>, delimiter: &str, writer: &mut impl Write) {
fn array_to_string(array: ListRef<'_>, delimiter: &str, ctx: &Context, writer: &mut impl Write) {
let element_data_type = ctx.arg_types[0].unnest_list();
let mut first = true;
for element in array.flatten() {
let Some(element) = element else { continue };
Expand All @@ -90,7 +93,7 @@ fn array_to_string(array: ListRef<'_>, delimiter: &str, writer: &mut impl Write)
} else {
first = false;
}
element.write(writer).unwrap();
element.write_with_type(element_data_type, writer).unwrap();
}
}

Expand All @@ -99,8 +102,10 @@ fn array_to_string_with_null(
array: ListRef<'_>,
delimiter: &str,
null_string: &str,
ctx: &Context,
writer: &mut impl Write,
) {
let element_data_type = ctx.arg_types[0].unnest_list();
let mut first = true;
for element in array.flatten() {
if !first {
Expand All @@ -109,7 +114,7 @@ fn array_to_string_with_null(
first = false;
}
match element {
Some(s) => s.write(writer).unwrap(),
Some(s) => s.write_with_type(element_data_type, writer).unwrap(),
None => write!(writer, "{}", null_string).unwrap(),
}
}
Expand Down

0 comments on commit e65fe2c

Please sign in to comment.