diff --git a/python/pyspark/sql/classic/column.py b/python/pyspark/sql/classic/column.py index c893050872f4..92c57438c739 100644 --- a/python/pyspark/sql/classic/column.py +++ b/python/pyspark/sql/classic/column.py @@ -309,12 +309,16 @@ def eqNullSafe( def __and__( self, other: Union[ParentColumn, "LiteralType", "DecimalLiteral", "DateTimeLiteral"] ) -> ParentColumn: - return _bin_op("and", self, other) + from pyspark.sql.functions import lit + + return _bin_op("and", self, lit(other)) def __or__( self, other: Union[ParentColumn, "LiteralType", "DecimalLiteral", "DateTimeLiteral"] ) -> ParentColumn: - return _bin_op("or", self, other) + from pyspark.sql.functions import lit + + return _bin_op("or", self, lit(other)) def __invert__(self) -> ParentColumn: return _func_op("not", self) @@ -322,12 +326,16 @@ def __invert__(self) -> ParentColumn: def __rand__( self, other: Union[ParentColumn, "LiteralType", "DecimalLiteral", "DateTimeLiteral"] ) -> ParentColumn: - return _bin_op("and", self, other) + from pyspark.sql.functions import lit + + return _bin_op("and", self, lit(other)) def __ror__( self, other: Union[ParentColumn, "LiteralType", "DecimalLiteral", "DateTimeLiteral"] ) -> ParentColumn: - return _bin_op("or", self, other) + from pyspark.sql.functions import lit + + return _bin_op("or", self, lit(other)) # container operators def __contains__(self, item: Any) -> None: diff --git a/python/pyspark/sql/tests/test_column.py b/python/pyspark/sql/tests/test_column.py index ac599fab1357..ecfcae36c955 100644 --- a/python/pyspark/sql/tests/test_column.py +++ b/python/pyspark/sql/tests/test_column.py @@ -94,6 +94,14 @@ def test_column_operators(self): cs.startswith("a"), cs.endswith("a"), ci.eqNullSafe(cs), + sf.col("b") & sf.lit(True), + sf.col("b") & True, + sf.lit(True) & sf.col("b"), + True & sf.col("b"), + sf.col("b") | sf.lit(True), + sf.col("b") | True, + sf.lit(True) | sf.col("b"), + True | sf.col("b"), ) self.assertTrue(all(isinstance(c, Column) for c in css)) self.assertTrue(isinstance(ci.cast(LongType()), Column))