Skip to content

feat: add fill_null/nan expressions #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions python/datafusion/expr.py
Original file line number Diff line number Diff line change
@@ -406,6 +406,18 @@ def is_not_null(self) -> Expr:
"""Returns ``True`` if this expression is not null."""
return Expr(self.expr.is_not_null())

def fill_nan(self, value: Any | Expr | None = None) -> Expr:
"""Fill NaN values with a provided value."""
if not isinstance(value, Expr):
value = Expr.literal(value)
return Expr(functions_internal.nanvl(self.expr, value.expr))

def fill_null(self, value: Any | Expr | None = None) -> Expr:
"""Fill NULL values with a provided value."""
if not isinstance(value, Expr):
value = Expr.literal(value)
return Expr(functions_internal.nvl(self.expr, value.expr))

_to_pyarrow_types = {
float: pa.float64(),
int: pa.int64(),
6 changes: 6 additions & 0 deletions python/datafusion/functions.py
Original file line number Diff line number Diff line change
@@ -186,6 +186,7 @@
"min",
"named_struct",
"nanvl",
"nvl",
"now",
"nth_value",
"nullif",
@@ -673,6 +674,11 @@ def nanvl(x: Expr, y: Expr) -> Expr:
return Expr(f.nanvl(x.expr, y.expr))


def nvl(x: Expr, y: Expr) -> Expr:
"""Returns ``x`` if ``x`` is not ``NULL``. Otherwise returns ``y``."""
return Expr(f.nvl(x.expr, y.expr))


def octet_length(arg: Expr) -> Expr:
"""Returns the number of bytes of a string."""
return Expr(f.octet_length(arg.expr))
33 changes: 30 additions & 3 deletions python/tests/test_expr.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

import pyarrow
import pyarrow as pa
import pytest
from datafusion import SessionContext, col
from datafusion.expr import (
@@ -125,8 +125,8 @@ def test_sort(test_ctx):
def test_relational_expr(test_ctx):
ctx = SessionContext()

batch = pyarrow.RecordBatch.from_arrays(
[pyarrow.array([1, 2, 3]), pyarrow.array(["alpha", "beta", "gamma"])],
batch = pa.RecordBatch.from_arrays(
[pa.array([1, 2, 3]), pa.array(["alpha", "beta", "gamma"])],
names=["a", "b"],
)
df = ctx.create_dataframe([[batch]], name="batch_array")
@@ -216,3 +216,30 @@ def test_display_name_deprecation():
# returns appropriate result
assert name == expr.schema_name()
assert name == "foo"


@pytest.fixture
def df():
ctx = SessionContext()

# create a RecordBatch and a new DataFrame from it
batch = pa.RecordBatch.from_arrays(
[pa.array([1, 2, None]), pa.array([4, None, 6]), pa.array([None, None, 8])],
names=["a", "b", "c"],
)

return ctx.from_arrow(batch)


def test_fill_null(df):
df = df.select(
col("a").fill_null(100).alias("a"),
col("b").fill_null(25).alias("b"),
col("c").fill_null(1234).alias("c"),
)
df.show()
result = df.collect()[0]

assert result.column(0) == pa.array([1, 2, 100])
assert result.column(1) == pa.array([4, 25, 6])
assert result.column(2) == pa.array([1234, 1234, 8])
6 changes: 6 additions & 0 deletions src/functions.rs
Original file line number Diff line number Diff line change
@@ -490,6 +490,11 @@ expr_fn!(
x y,
"Returns x if x is not NaN otherwise returns y."
);
expr_fn!(
nvl,
x y,
"Returns x if x is not NULL otherwise returns y."
);
expr_fn!(nullif, arg_1 arg_2);
expr_fn!(octet_length, args, "Returns number of bytes in the string. Since this version of the function accepts type character directly, it will not strip trailing spaces.");
expr_fn_vec!(overlay);
@@ -913,6 +918,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_wrapped(wrap_pyfunction!(min))?;
m.add_wrapped(wrap_pyfunction!(named_struct))?;
m.add_wrapped(wrap_pyfunction!(nanvl))?;
m.add_wrapped(wrap_pyfunction!(nvl))?;
m.add_wrapped(wrap_pyfunction!(now))?;
m.add_wrapped(wrap_pyfunction!(nullif))?;
m.add_wrapped(wrap_pyfunction!(octet_length))?;