Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions _duckdb-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ class Expression:
def otherwise(self, value: Expression) -> Expression: ...
def show(self) -> None: ...
def when(self, condition: Expression, value: Expression) -> Expression: ...
def try_(self, condition: Expression, value: Expression) -> Expression: ...

class FatalException(DatabaseError): ...

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ struct DuckDBPyExpression : public enable_shared_from_this<DuckDBPyExpression> {
shared_ptr<DuckDBPyExpression> In(const py::args &args);
shared_ptr<DuckDBPyExpression> NotIn(const py::args &args);

// TRY

shared_ptr<DuckDBPyExpression> Try();

// Order modifiers

shared_ptr<DuckDBPyExpression> Ascending();
Expand Down
6 changes: 6 additions & 0 deletions src/duckdb_py/pyexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ shared_ptr<DuckDBPyExpression> DuckDBPyExpression::NotIn(const py::args &args) {
return CreateCompareExpression(ExpressionType::COMPARE_NOT_IN, args);
}

// TRY

shared_ptr<DuckDBPyExpression> DuckDBPyExpression::Try() {
return DuckDBPyExpression::InternalUnaryOperator(ExpressionType::OPERATOR_TRY, *this);
}

// COALESCE

shared_ptr<DuckDBPyExpression> DuckDBPyExpression::Coalesce(const py::args &args) {
Expand Down
8 changes: 8 additions & 0 deletions src/duckdb_py/pyexpression/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@ void DuckDBPyExpression::Initialize(py::module_ &m) {
)";
expression.def("isnotin", &DuckDBPyExpression::NotIn, docs);

docs = R"(
Create a TRY expression from self
Returns:
DuckDBPyExpression: TRY(self)
)";
expression.def("try_", &DuckDBPyExpression::Try, docs);

docs = R"(
Return the stringified version of the expression.
Expand Down
18 changes: 18 additions & 0 deletions tests/fast/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,24 @@ def test_null(self):
res2 = rel.filter(b.isnotnull()).fetchall()
assert res2 == [(1, "a"), (2, "b"), (4, "c"), (5, "a")]

def test_try(self):
con = duckdb.connect()
rel = con.sql(
"""
select * from (VALUES
(-1.0),
(0.0),
(1.0),
(NULL),
) tbl(a)
"""
)

expr = FunctionExpression("sqrt", ColumnExpression("a"))

res = rel.select(expr.try_()).fetchall()
assert res == [(None,), (0.0,), (1.0,), (None,)]

def test_sort(self):
con = duckdb.connect()
rel = con.sql(
Expand Down