use super::*;
use crate::plans::conversion::is_regex_projection;
pub struct StructNameSpace(pub(crate) Expr);
impl StructNameSpace {
pub fn field_by_index(self, index: i64) -> Expr {
self.0
.map_unary(FunctionExpr::StructExpr(StructFunction::SelectFields(nth(
index,
))))
}
pub fn field_by_names<I, S>(self, names: I) -> Expr
where
I: IntoIterator<Item = S>,
S: Into<PlSmallStr>,
{
self.field_by_names_impl(names.into_iter().map(|x| x.into()).collect())
}
fn field_by_names_impl(self, names: Arc<[PlSmallStr]>) -> Expr {
let mut selector = Selector::Empty;
let _s = &mut selector;
let names = names
.iter()
.filter(|n| {
match n.as_str() {
_ if is_regex_projection(n.as_str()) => *_s |= Selector::Matches((*n).clone()),
"*" => *_s |= Selector::Wildcard,
_ => return true,
}
false
})
.cloned()
.collect();
selector |= Selector::ByName {
names,
strict: true,
};
self.0
.map_unary(FunctionExpr::StructExpr(StructFunction::SelectFields(
selector,
)))
}
pub fn field_by_name(self, name: &str) -> Expr {
if name == "*" || is_regex_projection(name) {
return self.field_by_names([name]);
}
self.0
.map_unary(FunctionExpr::StructExpr(StructFunction::FieldByName(
name.into(),
)))
}
pub fn rename_fields<I, S>(self, names: I) -> Expr
where
I: IntoIterator<Item = S>,
S: Into<PlSmallStr>,
{
self._rename_fields_impl(names.into_iter().map(|x| x.into()).collect())
}
pub fn _rename_fields_impl(self, names: Arc<[PlSmallStr]>) -> Expr {
self.0
.map_unary(FunctionExpr::StructExpr(StructFunction::RenameFields(
names,
)))
}
#[cfg(feature = "json")]
pub fn json_encode(self) -> Expr {
self.0
.map_unary(FunctionExpr::StructExpr(StructFunction::JsonEncode))
}
pub fn with_fields(self, fields: Vec<Expr>) -> Expr {
self.0
.map_n_ary(FunctionExpr::StructExpr(StructFunction::WithFields), fields)
}
}