diff --git a/crates/core/src/functions.rs b/crates/core/src/functions.rs index c32134054..28772de87 100644 --- a/crates/core/src/functions.rs +++ b/crates/core/src/functions.rs @@ -494,6 +494,11 @@ expr_fn!(length, string); expr_fn!(char_length, string); expr_fn!(chr, arg, "Returns the character with the given code."); expr_fn_vec!(coalesce); +expr_fn!( + contains, + string search_str, + "Return true if search_str is found within string (case-sensitive)." +); expr_fn!(cos, num); expr_fn!(cosh, num); expr_fn!(cot, num); @@ -960,6 +965,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(col))?; m.add_wrapped(wrap_pyfunction!(concat_ws))?; m.add_wrapped(wrap_pyfunction!(concat))?; + m.add_wrapped(wrap_pyfunction!(contains))?; m.add_wrapped(wrap_pyfunction!(corr))?; m.add_wrapped(wrap_pyfunction!(cos))?; m.add_wrapped(wrap_pyfunction!(cosh))?; diff --git a/python/datafusion/functions.py b/python/datafusion/functions.py index f062cbfce..dd5363dc9 100644 --- a/python/datafusion/functions.py +++ b/python/datafusion/functions.py @@ -116,6 +116,7 @@ "col", "concat", "concat_ws", + "contains", "corr", "cos", "cosh", @@ -436,6 +437,20 @@ def digest(value: Expr, method: Expr) -> Expr: return Expr(f.digest(value.expr, method.expr)) +def contains(string: Expr, search_str: Expr) -> Expr: + """Return true if ``search_str`` is found within ``string`` (case-sensitive). + + Examples: + >>> ctx = dfn.SessionContext() + >>> df = ctx.from_pydict({"a": ["the quick brown fox"]}) + >>> result = df.select( + ... dfn.functions.contains(dfn.col("a"), dfn.lit("brown")).alias("c")) + >>> result.collect_column("c")[0].as_py() + True + """ + return Expr(f.contains(string.expr, search_str.expr)) + + def concat(*args: Expr) -> Expr: """Concatenates the text representations of all the arguments. diff --git a/python/tests/test_functions.py b/python/tests/test_functions.py index 37d349c58..bb96997b5 100644 --- a/python/tests/test_functions.py +++ b/python/tests/test_functions.py @@ -745,6 +745,7 @@ def test_array_function_obj_tests(stmt, py_expr): f.split_part(column("a"), literal("l"), literal(1)), pa.array(["He", "Wor", "!"]), ), + (f.contains(column("a"), literal("ell")), pa.array([True, False, False])), (f.starts_with(column("a"), literal("Wor")), pa.array([False, True, False])), (f.strpos(column("a"), literal("o")), pa.array([5, 2, 0], type=pa.int32())), (