Skip to content

Commit bbe02ff

Browse files
migrate bitwise functions to UDAF
The functions now take a single expression instead of a Vec<_>. Ref: apache/datafusion#10930
1 parent 27f5c73 commit bbe02ff

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

python/datafusion/functions.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,31 +1482,26 @@ def last_value(
14821482
)
14831483

14841484

1485-
def bit_and(*args: Expr, distinct: bool = False) -> Expr:
1485+
def bit_and(arg: Expr, distinct: bool = False) -> Expr:
14861486
"""Computes the bitwise AND of the argument."""
1487-
args = [arg.expr for arg in args]
1488-
return Expr(f.bit_and(*args, distinct=distinct))
1487+
return Expr(f.bit_and(arg.expr, distinct=distinct))
14891488

14901489

1491-
def bit_or(*args: Expr, distinct: bool = False) -> Expr:
1490+
def bit_or(arg: Expr, distinct: bool = False) -> Expr:
14921491
"""Computes the bitwise OR of the argument."""
1493-
args = [arg.expr for arg in args]
1494-
return Expr(f.bit_or(*args, distinct=distinct))
1492+
return Expr(f.bit_or(arg.expr, distinct=distinct))
14951493

14961494

1497-
def bit_xor(*args: Expr, distinct: bool = False) -> Expr:
1495+
def bit_xor(arg: Expr, distinct: bool = False) -> Expr:
14981496
"""Computes the bitwise XOR of the argument."""
1499-
args = [arg.expr for arg in args]
1500-
return Expr(f.bit_xor(*args, distinct=distinct))
1497+
return Expr(f.bit_xor(arg.expr, distinct=distinct))
15011498

15021499

1503-
def bool_and(*args: Expr, distinct: bool = False) -> Expr:
1500+
def bool_and(arg: Expr, distinct: bool = False) -> Expr:
15041501
"""Computes the boolean AND of the arugment."""
1505-
args = [arg.expr for arg in args]
1506-
return Expr(f.bool_and(*args, distinct=distinct))
1502+
return Expr(f.bool_and(arg.expr, distinct=distinct))
15071503

15081504

1509-
def bool_or(*args: Expr, distinct: bool = False) -> Expr:
1505+
def bool_or(arg: Expr, distinct: bool = False) -> Expr:
15101506
"""Computes the boolean OR of the arguement."""
1511-
args = [arg.expr for arg in args]
1512-
return Expr(f.bool_or(*args, distinct=distinct))
1507+
return Expr(f.bool_or(arg.expr, distinct=distinct))

src/functions.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,56 @@ pub fn avg(expression: PyExpr, distinct: bool) -> PyResult<PyExpr> {
9898
}
9999
}
100100

101+
#[pyfunction]
102+
pub fn bit_and(expr_x: PyExpr, distinct: bool) -> PyResult<PyExpr> {
103+
let expr = functions_aggregate::expr_fn::bit_and(expr_x.expr);
104+
if distinct {
105+
Ok(expr.distinct().build()?.into())
106+
} else {
107+
Ok(expr.into())
108+
}
109+
}
110+
111+
#[pyfunction]
112+
pub fn bit_or(expression: PyExpr, distinct: bool) -> PyResult<PyExpr> {
113+
let expr = functions_aggregate::expr_fn::bit_or(expression.expr);
114+
if distinct {
115+
Ok(expr.distinct().build()?.into())
116+
} else {
117+
Ok(expr.into())
118+
}
119+
}
120+
121+
#[pyfunction]
122+
pub fn bit_xor(expression: PyExpr, distinct: bool) -> PyResult<PyExpr> {
123+
let expr = functions_aggregate::expr_fn::bit_xor(expression.expr);
124+
if distinct {
125+
Ok(expr.distinct().build()?.into())
126+
} else {
127+
Ok(expr.into())
128+
}
129+
}
130+
131+
#[pyfunction]
132+
pub fn bool_and(expression: PyExpr, distinct: bool) -> PyResult<PyExpr> {
133+
let expr = functions_aggregate::expr_fn::bool_and(expression.expr);
134+
if distinct {
135+
Ok(expr.distinct().build()?.into())
136+
} else {
137+
Ok(expr.into())
138+
}
139+
}
140+
141+
#[pyfunction]
142+
pub fn bool_or(expression: PyExpr, distinct: bool) -> PyResult<PyExpr> {
143+
let expr = functions_aggregate::expr_fn::bool_or(expression.expr);
144+
if distinct {
145+
Ok(expr.distinct().build()?.into())
146+
} else {
147+
Ok(expr.into())
148+
}
149+
}
150+
101151
#[pyfunction]
102152
pub fn mean(expression: PyExpr, distinct: bool) -> PyResult<PyExpr> {
103153
// alias for avg
@@ -937,11 +987,6 @@ array_fn!(range, start stop step);
937987
aggregate_function!(array_agg, ArrayAgg);
938988
aggregate_function!(max, Max);
939989
aggregate_function!(min, Min);
940-
aggregate_function!(bit_and, BitAnd);
941-
aggregate_function!(bit_or, BitOr);
942-
aggregate_function!(bit_xor, BitXor);
943-
aggregate_function!(bool_and, BoolAnd);
944-
aggregate_function!(bool_or, BoolOr);
945990

946991
pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
947992
m.add_wrapped(wrap_pyfunction!(abs))?;

0 commit comments

Comments
 (0)