Skip to content

Commit

Permalink
feat(expr): support map keys/values/entries (#18480)
Browse files Browse the repository at this point in the history
Signed-off-by: xxchan <[email protected]>
  • Loading branch information
xxchan authored and xxchan committed Sep 11, 2024
1 parent b429135 commit c93833c
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion src/expr/impl/src/scalar/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use risingwave_common::array::{ListValue, StructValue};
use risingwave_common::row::Row;
use risingwave_common::types::{
DataType, ListRef, MapRef, MapType, MapValue, ScalarRefImpl, ToOwnedDatum,
DataType, ListRef, MapRef, MapType, MapValue, ScalarRef, ScalarRefImpl, ToOwnedDatum,
};
use risingwave_expr::expr::Context;
use risingwave_expr::{function, ExprError};
Expand Down Expand Up @@ -241,6 +241,60 @@ fn map_delete(map: MapRef<'_>, key: Option<ScalarRefImpl<'_>>) -> MapValue {
MapValue::delete(map, key)
}

/// # Example
///
/// ```slt
/// query T
/// select map_keys(map{'a':1, 'b':2, 'c':3});
/// ----
/// {a,b,c}
/// ```
#[function(
"map_keys(anymap) -> anyarray",
type_infer = "|args|{
Ok(DataType::List(Box::new(args[0].as_map().key().clone())))
}"
)]
fn map_keys(map: MapRef<'_>) -> ListValue {
map.into_kv().0.to_owned_scalar()
}

/// # Example
///
/// ```slt
/// query T
/// select map_values(map{'a':1, 'b':2, 'c':3});
/// ----
/// {1,2,3}
/// ```
#[function(
"map_values(anymap) -> anyarray",
type_infer = "|args|{
Ok(DataType::List(Box::new(args[0].as_map().value().clone())))
}"
)]
fn map_values(map: MapRef<'_>) -> ListValue {
map.into_kv().1.to_owned_scalar()
}

/// # Example
///
/// ```slt
/// query T
/// select map_entries(map{'a':1, 'b':2, 'c':3});
/// ----
/// {"(a,1)","(b,2)","(c,3)"}
/// ```
#[function(
"map_entries(anymap) -> anyarray",
type_infer = "|args|{
Ok(args[0].as_map().clone().into_list())
}"
)]
fn map_entries(map: MapRef<'_>) -> ListValue {
map.into_inner().to_owned()
}

#[cfg(test)]
mod tests {
use risingwave_common::array::DataChunk;
Expand Down

0 comments on commit c93833c

Please sign in to comment.