Skip to content

Commit 8f45fa4

Browse files
timsaucerclaude
andcommitted
Add missing contains string function
Expose the upstream DataFusion `contains(string, search_str)` function which returns true if search_str is found within string (case-sensitive). Note: the other functions from #1450 (instr, position, substring_index) already exist — instr and position are aliases for strpos, and substring_index is exposed as substr_index. Closes #1450 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5be412b commit 8f45fa4

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

crates/core/src/functions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ expr_fn!(length, string);
494494
expr_fn!(char_length, string);
495495
expr_fn!(chr, arg, "Returns the character with the given code.");
496496
expr_fn_vec!(coalesce);
497+
expr_fn!(
498+
contains,
499+
string search_str,
500+
"Return true if search_str is found within string (case-sensitive)."
501+
);
497502
expr_fn!(cos, num);
498503
expr_fn!(cosh, num);
499504
expr_fn!(cot, num);
@@ -960,6 +965,7 @@ pub(crate) fn init_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
960965
m.add_wrapped(wrap_pyfunction!(col))?;
961966
m.add_wrapped(wrap_pyfunction!(concat_ws))?;
962967
m.add_wrapped(wrap_pyfunction!(concat))?;
968+
m.add_wrapped(wrap_pyfunction!(contains))?;
963969
m.add_wrapped(wrap_pyfunction!(corr))?;
964970
m.add_wrapped(wrap_pyfunction!(cos))?;
965971
m.add_wrapped(wrap_pyfunction!(cosh))?;

python/datafusion/functions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"col",
117117
"concat",
118118
"concat_ws",
119+
"contains",
119120
"corr",
120121
"cos",
121122
"cosh",
@@ -436,6 +437,20 @@ def digest(value: Expr, method: Expr) -> Expr:
436437
return Expr(f.digest(value.expr, method.expr))
437438

438439

440+
def contains(string: Expr, search_str: Expr) -> Expr:
441+
"""Return true if ``search_str`` is found within ``string`` (case-sensitive).
442+
443+
Examples:
444+
>>> ctx = dfn.SessionContext()
445+
>>> df = ctx.from_pydict({"a": ["the quick brown fox"]})
446+
>>> result = df.select(
447+
... dfn.functions.contains(dfn.col("a"), dfn.lit("brown")).alias("c"))
448+
>>> result.collect_column("c")[0].as_py()
449+
True
450+
"""
451+
return Expr(f.contains(string.expr, search_str.expr))
452+
453+
439454
def concat(*args: Expr) -> Expr:
440455
"""Concatenates the text representations of all the arguments.
441456

0 commit comments

Comments
 (0)