@@ -545,13 +545,13 @@ def with_column(self, name: str, expr: Expr | str) -> DataFrame:
545545 return DataFrame (self .df .with_column (name , ensure_expr (expr )))
546546
547547 def with_columns (
548- self , * exprs : Expr | Iterable [Expr ], ** named_exprs : Expr
548+ self , * exprs : Expr | str | Iterable [Expr | str ], ** named_exprs : Expr | str
549549 ) -> DataFrame :
550550 """Add columns to the DataFrame.
551551
552- By passing expressions, iterables of expressions, or named expressions.
552+ By passing expressions, iterables of expressions, string SQL expressions, or named expressions.
553553 All expressions must be :class:`~datafusion.expr.Expr` objects created via
554- :func:`datafusion.col` or :func:`datafusion.lit`.
554+ :func:`datafusion.col` or :func:`datafusion.lit` or SQL expressions .
555555 To pass named expressions use the form ``name=Expr``.
556556
557557 Example usage: The following will add 4 columns labeled ``a``, ``b``, ``c``,
@@ -565,14 +565,33 @@ def with_columns(
565565 )
566566
567567 Args:
568- exprs: Either a single expression or an iterable of expressions to add.
568+ exprs: Either a single expression, an iterable of expressions to add or string SQL expressions .
569569 named_exprs: Named expressions in the form of ``name=expr``
570570
571571 Returns:
572572 DataFrame with the new columns added.
573573 """
574- expressions = ensure_expr_list (exprs )
574+ expressions = []
575+ for expr in exprs :
576+ if isinstance (expr , str ):
577+ expr = self .parse_sql_expr (expr )
578+ expressions .append (ensure_expr (expr ))
579+ elif isinstance (expr , Iterable ) and not isinstance (
580+ expr , (Expr , str , bytes , bytearray )
581+ ):
582+ expressions .extend (
583+ [
584+ self .parse_sql_expr (e ).expr
585+ if isinstance (e , str )
586+ else ensure_expr (e )
587+ for e in expr
588+ ]
589+ )
590+ else :
591+ expressions .append (ensure_expr (expr ))
592+
575593 for alias , expr in named_exprs .items ():
594+ expr = self .parse_sql_expr (expr ) if isinstance (expr , str ) else expr
576595 ensure_expr (expr )
577596 expressions .append (expr .alias (alias ).expr )
578597
0 commit comments