From e65fe2c2af82b3bf58bb34db4b5d39bbec2294bc Mon Sep 17 00:00:00 2001 From: Runji Wang Date: Wed, 13 Sep 2023 13:26:08 +0800 Subject: [PATCH] add context to `array_to_string` Signed-off-by: Runji Wang --- src/common/src/types/mod.rs | 8 ++++---- src/expr/src/vector_op/array_to_string.rs | 11 ++++++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/common/src/types/mod.rs b/src/common/src/types/mod.rs index 14debb3f4e181..f8989413483eb 100644 --- a/src/common/src/types/mod.rs +++ b/src/common/src/types/mod.rs @@ -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, } } diff --git a/src/expr/src/vector_op/array_to_string.rs b/src/expr/src/vector_op/array_to_string.rs index 92e3d9d172642..8dee164a56cf5 100644 --- a/src/expr/src/vector_op/array_to_string.rs +++ b/src/expr/src/vector_op/array_to_string.rs @@ -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. @@ -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 }; @@ -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(); } } @@ -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 { @@ -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(), } }