Path: blob/main/crates/polars-plan/src/dsl/functions/concat.rs
6940 views
use super::*;12#[cfg(all(feature = "concat_str", feature = "strings"))]3/// Horizontally concat string columns in linear time4pub fn concat_str<E: AsRef<[Expr]>>(s: E, separator: &str, ignore_nulls: bool) -> Expr {5let input = s.as_ref().to_vec();6let separator = separator.into();78Expr::Function {9input,10function: StringFunction::ConcatHorizontal {11delimiter: separator,12ignore_nulls,13}14.into(),15}16}1718#[cfg(all(feature = "concat_str", feature = "strings"))]19/// Format the results of an array of expressions using a format string20pub fn format_str<E: AsRef<[Expr]>>(format: &str, args: E) -> PolarsResult<Expr> {21let mut args: std::collections::VecDeque<Expr> = args.as_ref().to_vec().into();2223// Parse the format string, and separate substrings between placeholders24let segments: Vec<&str> = format.split("{}").collect();2526polars_ensure!(27segments.len() - 1 == args.len(),28ShapeMismatch: "number of placeholders should equal the number of arguments"29);3031let mut exprs: Vec<Expr> = Vec::new();3233for (i, s) in segments.iter().enumerate() {34if i > 0 {35if let Some(arg) = args.pop_front() {36exprs.push(arg);37}38}3940if !s.is_empty() {41exprs.push(lit(s.to_string()))42}43}4445Ok(concat_str(exprs, "", false))46}4748/// Concat lists entries.49pub fn concat_list<E: AsRef<[IE]>, IE: Into<Expr> + Clone>(s: E) -> PolarsResult<Expr> {50let s: Vec<_> = s.as_ref().iter().map(|e| e.clone().into()).collect();5152polars_ensure!(!s.is_empty(), ComputeError: "`concat_list` needs one or more expressions");5354Ok(Expr::Function {55input: s,56function: FunctionExpr::ListExpr(ListFunction::Concat),57})58}5960/// Horizontally concatenate columns into a single array-type column.61pub fn concat_arr(input: Vec<Expr>) -> PolarsResult<Expr> {62feature_gated!("dtype-array", {63polars_ensure!(!input.is_empty(), ComputeError: "`concat_arr` needs one or more expressions");6465Ok(Expr::Function {66input,67function: FunctionExpr::ArrayExpr(ArrayFunction::Concat),68})69})70}7172pub fn concat_expr<E: AsRef<[IE]>, IE: Into<Expr> + Clone>(73s: E,74rechunk: bool,75) -> PolarsResult<Expr> {76let s: Vec<_> = s.as_ref().iter().map(|e| e.clone().into()).collect();77polars_ensure!(!s.is_empty(), ComputeError: "`concat_expr` needs one or more expressions");78Ok(Expr::n_ary(FunctionExpr::ConcatExpr(rechunk), s))79}808182